-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileProvider.hpp
55 lines (49 loc) · 1.64 KB
/
FileProvider.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once
#include <filesystem>
#include <string>
class FileProvider
{
public:
explicit FileProvider(const std::string &baseImg)
{
mDirectory = baseImg.substr(0, baseImg.find_last_of('/') + 1).c_str();
std::string mSelectedFile = baseImg.substr(baseImg.find_last_of('/') + 1);
mId = getNumberFromStr(mSelectedFile);
}
int
getNumberFromStr(const std::string &filename)
{
std::string v;
std::copy_if(filename.begin(), filename.end(), std::back_inserter(v), ::isdigit);
if (v.empty())
return 0;
return std::stoi(v);
}
bool isNewFileReady(std::string &outNewFileName)
{
const std::filesystem::path sandbox{mDirectory};
for (auto const &dir_entry : std::filesystem::directory_iterator{sandbox})
{
std::string file{dir_entry.path()};
file = file.substr(file.find_last_of('/') + 1);
std::string file_ext = file.substr(file.find_last_of(".") + 1);
std::transform(file_ext.begin(), file_ext.end(), file_ext.begin(),
[](unsigned char c)
{ return std::tolower(c); });
if (file_ext != "jpg" && file_ext != "jpeg" && file_ext != "jpe" && file_ext != "bmp")
continue;
int num = getNumberFromStr(file);
if (num > mId)
{
mId = num;
mSelectedFile = file;
outNewFileName = dir_entry.path();
return true;
}
}
return false;
}
std::string mDirectory;
std::string mSelectedFile;
int mId;
};