Repository navigation
Expand file tree
/
Copy pathCmFile.h
More file actions
200 lines (162 loc) · 6.86 KB
/
Copy pathCmFile.h
File metadata and controls
200 lines (162 loc) · 6.86 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#pragma once
struct CmFile
{
static string BrowseFile(const char* strFilter = "Images (*.jpg;*.png)\0*.jpg;*.png\0All (*.*)\0*.*\0\0", bool isOpen = true);
static string BrowseFolder();
static inline string GetFolder(CStr& path);
static inline string GetSubFolder(CStr& path);
static inline string GetName(CStr& path);
static inline string GetNameNE(CStr& path);
static inline string GetPathNE(CStr& path);
static inline string GetNameNoSuffix(CStr& path, CStr &suffix);
// Get file names from a wildcard. Eg: GetNames("D:\\*.jpg", imgNames);
static int GetNames(CStr &nameW, vecS &names, string &dir);
static int GetNames(CStr &nameW, vecS &names){string dir; return GetNames(nameW, names, dir);};
static int GetNames(CStr& rootFolder, CStr &fileW, vecS &names);
static int GetNamesNE(CStr& nameWC, vecS &names, string &dir, string &ext);
static int GetNamesNE(CStr& rootFolder, CStr &fileW, vecS &names);
static int GetNamesNE(CStr& nameWC, vecS &names) {string dir, ext; return GetNamesNE(nameWC, names, dir, ext);}
static int GetNamesNoSuffix(CStr& nameWC, vecS &namesNS, CStr suffix, string &dir); //e.g. suffix = "_C.jpg"
static int GetNamesNoSuffix(CStr& nameWC, vecS &namesNS, CStr suffix) {string dir; return GetNamesNoSuffix(nameWC, namesNS, suffix, dir);}
static inline string GetExtention(CStr name);
static inline bool FileExist(CStr& filePath);
static inline bool FilesExist(CStr& fileW);
static inline bool FolderExist(CStr& strPath);
static inline string GetWkDir();
static BOOL MkDir(CStr& path);
// Eg: RenameImages("D:/DogImages/*.jpg", "F:/Images", "dog", ".jpg");
static int Rename(CStr& srcNames, CStr& dstDir, const char* nameCommon, const char* nameExt);
static void RenameSuffix(CStr dir, CStr orgSuf, CStr dstSuf);
static int ChangeImgFormat(CStr &imgW, CStr dstW); // "./*.jpg", "./Out/%s_O.png"
static inline void RmFile(CStr& fileW);
static void RmFolder(CStr& dir);
static void CleanFolder(CStr& dir, bool subFolder = false);
static int GetSubFolders(CStr& folder, vecS& subFolders);
static string GetFatherFolder(CStr &folder) {return GetFolder(folder.substr(0, folder.size() - 1));}
inline static BOOL Copy(CStr &src, CStr &dst, BOOL failIfExist = FALSE);
inline static BOOL Move(CStr &src, CStr &dst, DWORD dwFlags = MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH);
static BOOL Move2Dir(CStr &srcW, CStr dstDir);
static BOOL Copy2Dir(CStr &srcW, CStr dstDir);
//Load mask image and threshold thus noisy by compression can be removed
static Mat LoadMask(CStr& fileName);
static void WriteNullFile(CStr& fileName) {FILE *f = fopen(_S(fileName), "w"); fclose(f);}
static void AppendStr(CStr fileName, CStr str);
static void ChkImgs(CStr &imgW);
static void RunProgram(CStr &fileName, CStr ¶meters = "", bool waiteF = false, bool showW = true);
static string GetCompName(); // Get the name of computer
static void SegOmpThrdNum(double ratio = 0.8);
// Copy files and add suffix. e.g. copyAddSuffix("./*.jpg", "./Imgs/", "_Img.jpg")
static void copyAddSuffix(CStr &srcW, CStr &dstDir, CStr &dstSuffix);
static vecS loadStrList(CStr &fName);
// Write matrix to binary file
static bool matWrite(CStr& filename, CMat& M);
static bool matWrite(FILE *f, CMat& M); // default FILE mode: "wb"
// Read matrix from binary file
static bool matRead( const string& filename, Mat& M);
static bool matRead(FILE *f, Mat& M); // default FILE mode: "rb"
// Needs 7-Zip to be installed and 7z.exe to be put under available path. compressLevel = 9 gives maximal compression
static void ZipFiles(CStr &filesW, CStr &zipFileName, int compressLevel = 0);
static void CmFile::UnZipFiles(CStr &zipFileName, CStr &tgtDir, bool overwriteWarning = true);
};
/************************************************************************/
/* Implementation of inline functions */
/************************************************************************/
string CmFile::GetFolder(CStr& path)
{
return path.substr(0, path.find_last_of("\\/")+1);
}
string CmFile::GetSubFolder(CStr& path)
{
string folder = path.substr(0, path.find_last_of("\\/"));
return folder.substr(folder.find_last_of("\\/")+1);
}
string CmFile::GetName(CStr& path)
{
int start = path.find_last_of("\\/")+1;
int end = path.find_last_not_of(' ')+1;
return path.substr(start, end - start);
}
string CmFile::GetNameNE(CStr& path)
{
int start = path.find_last_of("\\/")+1;
int end = path.find_last_of('.');
if (end >= 0)
return path.substr(start, end - start);
else
return path.substr(start, path.find_last_not_of(' ')+1 - start);
}
string CmFile::GetNameNoSuffix(CStr& path, CStr &suffix)
{
int start = path.find_last_of("\\/")+1;
int end = path.size() - suffix.size();
CV_Assert(path.substr(end) == suffix);
if (end >= 0)
return path.substr(start, end - start);
else
return path.substr(start, path.find_last_not_of(' ')+1 - start);
}
string CmFile::GetPathNE(CStr& path)
{
int end = path.find_last_of('.');
if (end >= 0)
return path.substr(0, end);
else
return path.substr(0, path.find_last_not_of(' ') + 1);
}
string CmFile::GetExtention(CStr name)
{
return name.substr(name.find_last_of('.'));
}
BOOL CmFile::Copy(CStr &src, CStr &dst, BOOL failIfExist)
{
return ::CopyFileA(src.c_str(), dst.c_str(), failIfExist);
}
BOOL CmFile::Move(CStr &src, CStr &dst, DWORD dwFlags)
{
return MoveFileExA(src.c_str(), dst.c_str(), dwFlags);
}
void CmFile::RmFile(CStr& fileW)
{
vecS names;
string dir;
int fNum = CmFile::GetNames(fileW, names, dir);
for (int i = 0; i < fNum; i++)
::DeleteFileA(_S(dir + names[i]));
}
// Test whether a file exist
bool CmFile::FileExist(CStr& filePath)
{
if (filePath.size() == 0)
return false;
DWORD attr = GetFileAttributesA(_S(filePath));
return attr == FILE_ATTRIBUTE_NORMAL || attr == FILE_ATTRIBUTE_ARCHIVE;//GetLastError() != ERROR_FILE_NOT_FOUND;
}
bool CmFile::FilesExist(CStr& fileW)
{
vecS names;
int fNum = GetNames(fileW, names);
return fNum > 0;
}
string CmFile::GetWkDir()
{
string wd;
wd.resize(1024);
DWORD len = GetCurrentDirectoryA(1024, &wd[0]);
wd.resize(len);
return wd;
}
bool CmFile::FolderExist(CStr& strPath)
{
int i = (int)strPath.size() - 1;
for (; i >= 0 && (strPath[i] == '\\' || strPath[i] == '/'); i--)
;
string str = strPath.substr(0, i+1);
WIN32_FIND_DATAA wfd;
HANDLE hFind = FindFirstFileA(_S(str), &wfd);
bool rValue = (hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
FindClose(hFind);
return rValue;
}
/************************************************************************/
/* Implementations */
/************************************************************************/