951e62e51be267c693098cb824786625e0c06e09
[tar-legacy.git] / AutoRadar_installer / FileSystemHelper.h
1 #include <Windows.h>
2 #include <vector>
3 #include <string>
4
5 namespace fs
6 {
7 std::vector<std::string> getFilesInDirectory(std::string folder) {
8 std::vector<std::string> names;
9 std::string search_path = folder + "/*.*";
10 WIN32_FIND_DATA fd;
11 HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd);
12 if (hFind != INVALID_HANDLE_VALUE) {
13 do {
14 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
15 names.push_back(folder + "\\" + fd.cFileName);
16 }
17 } while (::FindNextFile(hFind, &fd));
18 ::FindClose(hFind);
19 }
20 for (auto && s : names) s = s.substr(s.size());
21
22 return names;
23 }
24
25 std::vector<std::string> getFilesInDirectoryRecursive(std::string folder, std::vector<std::string>* vec = NULL) {
26 std::vector<std::string>* v = vec;
27
28 std::vector<std::string> names;
29 if (v == NULL) // First iteration
30 v = &names;
31
32 std::string search_path = folder + "/*.*";
33 WIN32_FIND_DATA fd;
34 HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd);
35 if (hFind != INVALID_HANDLE_VALUE) {
36 do {
37 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
38 v->push_back(folder + "\\" + fd.cFileName);
39 }
40 else {
41 if ((std::string(fd.cFileName) != ".") && (std::string(fd.cFileName) != ".."))
42 getFilesInDirectoryRecursive(folder + "\\" + fd.cFileName, v);
43 }
44 } while (::FindNextFile(hFind, &fd));
45 ::FindClose(hFind);
46 }
47
48 for (auto && s : names) s = s.substr(folder.size());
49
50 return *v;
51 }
52
53 void copyFile(std::string src, std::string dst) {
54 std::ifstream _src(src, std::ios::binary);
55 std::ofstream _dst(dst, std::ios::binary);
56
57 _dst << _src.rdbuf();
58 }
59
60
61 bool dirExists(const std::string& dirName_in) {
62 DWORD ftyp = GetFileAttributesA(dirName_in.c_str());
63 if (ftyp == INVALID_FILE_ATTRIBUTES)
64 return false; //something is wrong with your path!
65
66 if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
67 return true; // this is a directory!
68
69 return false; // this is not a directory!
70 }
71
72 std::string getDirName(std::string f) {
73 return f.substr(0, f.size() - split(sutil::ReplaceAll(f, "/", "\\"), '\\').back().size());
74 }
75 }