27 #define _CRT_SECURE_NO_DEPRECATE
30 #include "FileHandling.hh"
33 # include <Base/Common/W32Compat.hh>
39 # include <sys/time.h>
40 # include <sys/types.h>
44 # include <sys/stat.h>
53 #include "Base/Debug/Error.hh"
69 int FileHandling::RemoveDir(
const string &subdir)
74 command =
"rmdir /S /Q "+subdir;
76 command =
"rm -rf "+subdir;
78 res = system(command.c_str());
80 perror(command.c_str());
86 int FileHandling::CreateDir(
const string &subdir)
91 command =
"md "+subdir;
93 command =
"mkdir "+subdir;
96 res = system(command.c_str());
98 perror(command.c_str());
104 int FileHandling::CreateDirRecursive(
const string &dir)
106 vector<string> parts;
107 FileHandling::SplitPath(dir, parts);
111 vector<string> subdirs(parts.size());
113 subdirs[0] = (FileHandling::IsAbsolute(dir) ?
"" :
".\\") + parts[0];
115 subdirs[0] = (FileHandling::IsAbsolute(dir) ?
"/" :
"./") + parts[0];
117 for (
unsigned int i = 1; i < parts.size(); i++) {
119 subdirs[i] = subdirs[i-1] +
"\\" + parts[i];
121 subdirs[i] = subdirs[i-1] +
"/" + parts[i];
125 int last = (int)subdirs.size()-1;
126 while (!FileHandling::FileExists(subdirs[last]) && last >= 0) last--;
129 for (
int j = last+1; j < (int)subdirs.size() && res == 0; j++) {
130 res = FileHandling::CreateDir(subdirs[j]);
138 bool FileHandling::FileExists(
const string &file)
141 int res = stat(file.c_str(), &statbuf);
143 if (res < 0)
return false;
147 int FileHandling::GetDirectoryContent(
const string &Dir,
148 const string &prefix,
149 const string &suffix,
150 vector<string> &content,
151 const bool GetFullPath)
154 int pl = (int)prefix.length();
155 int sl = (int)suffix.length();
159 std::string tmp = Dir+
"/*";
161 TCHAR searchPath[MAX_PATH];
163 lstrcpy(searchPath, tmp.c_str());
167 HANDLE sh = FindFirstFile(searchPath, &ffd);
168 if(INVALID_HANDLE_VALUE == sh)
return -1;
176 filename = ffd.cFileName;
177 if (filename ==
".." || filename==
".")
continue;
179 int l = (int)filename.length();
180 if (pl ==0 || (l>=pl && filename.substr(0,pl) == prefix)) {
181 if (sl==0 || (l>=sl && filename.substr(l-sl,sl) ==suffix)) {
182 if (GetFullPath) content.push_back(Dir+
"/"+filename);
183 else content.push_back(filename);
187 }
while (FindNextFile(sh, &ffd));
193 dp = opendir (Dir.c_str());
195 perror (Dir.c_str());
198 while ( (ep = readdir (dp)) != NULL) {
199 filename = ep->d_name;
200 if (filename ==
".." || filename==
".")
continue;
202 int l = filename.length();
203 if (pl ==0 || (l >= pl && filename.substr(0,pl) == prefix)) {
205 if (sl==0 || (l >= sl && filename.substr(l-sl,sl) ==suffix)) {
207 if (GetFullPath) content.push_back(Dir+
"/"+filename);
208 else content.push_back(filename);
212 (void) closedir (dp);
217 bool FileHandling::FileReadable(
const std::string &filename)
219 ifstream ifs(filename.c_str());
221 bool ok = ifs.good();
226 bool FileHandling::CopyFileTo(
const std::string &oldFilename,
227 const std::string &newFilename)
230 std::ifstream fin(oldFilename.c_str(), std::fstream::binary);
232 std::ofstream fout(newFilename.c_str(), std::fstream::trunc | std::fstream::binary);
243 void FileHandling::SplitName(
const std::string &fullname,
244 string &dir,
string &base,
string &suffix )
249 string::size_type tmp1 = fullname.rfind(
'\\');
250 string::size_type tmp2 = fullname.rfind(
'/');
251 string::size_type posL;
252 if (tmp1 == std::string::npos)
254 else if (tmp2 == std::string::npos)
257 posL = (tmp1>tmp2)?tmp1:tmp2;
259 string::size_type posL = fullname.rfind(
'/');
261 string::size_type posR = fullname.rfind(
'.');
264 bool foundDir = (posL!=std::string::npos);
265 bool foundSuf = (posR!=std::string::npos);
271 if (fullname.at(posR-1) ==
'\\' || fullname.at(posR-1) ==
'/') {
279 if (foundDir && foundSuf) {
281 dir = fullname.substr(0, posL+1 );
282 base = fullname.substr(posL+1, (posR-posL-1) );
284 suffix = fullname.substr(posR, (fullname.size()-posR) );
288 }
else if (foundDir && !foundSuf) {
289 dir = fullname.substr(0, posL+1 );
290 base = fullname.substr(posL+1, (fullname.size()-posL) );
293 }
else if (!foundDir && foundSuf) {
295 base = fullname.substr(0, posR);
296 suffix = fullname.substr(posR, (fullname.size()-posR) );
307 std::string FileHandling::Directory(
const std::string &fullname)
309 string dir, base, suf;
310 FileHandling::SplitName(fullname, dir, base, suf);
314 std::string FileHandling::Basename(
const std::string &fullname)
316 string dir, base, suf;
317 FileHandling::SplitName(fullname, dir, base, suf );
321 std::string FileHandling::Suffix(
const std::string &fullname)
323 string dir, base, suf;
324 FileHandling::SplitName(fullname, dir, base, suf );
328 std::string FileHandling::Extension(
const std::string &fullname)
330 string suf = Suffix(fullname);
334 string::size_type pos = suf.rfind(
'.');
335 const bool found = (pos!=std::string::npos);
339 ext = suf.substr(pos+1, (suf.size()-pos-1) );
344 std::string FileHandling::Filename(
const std::string &fullname)
346 string dir, base, suf;
347 FileHandling::SplitName(fullname, dir, base, suf);
351 std::string FileHandling::DirectoryAndBasename(
const std::string &fullname)
353 string dir, base, suf;
354 FileHandling::SplitName(fullname, dir, base, suf);
355 string dir_and_base=
"";
359 dir_and_base += (
'\\');
361 dir_and_base += (
'/');
364 dir_and_base += base;
369 void FileHandling::SplitPath(
const string &fullpath,
370 vector<string> &result)
373 const string delimiters =
"\\/";
375 const string delimiters =
"/";
378 string str = fullpath;
379 string::size_type pos = str.find_first_of(delimiters);
380 while (pos != string::npos) {
382 result.push_back(str.substr(0, pos));
384 str = str.substr(pos+1);
385 pos = str.find_first_of(delimiters);
387 if (str.length() > 0) {
388 result.push_back(str);
392 void FileHandling::LogCommandLine(
int argc,
char** argv, std::ostream &os)
394 for (
int i=0; i<argc; i++){
395 os<<argv[i]<<
" "<<endl;
398 os<<
"argc="<<argc<<endl;
399 for (
int i=0; i<argc; i++){
400 os<<
"argv["<<i<<
"]="<<argv[i]<<endl;
405 void FileHandling::LogCommandLine(
int argc,
char** argv,
406 const std::string &filename)
409 ofs.open(filename.c_str());
411 BIASERR(
"Failed to open "<<filename<<
" for LogCommandLine!")
412 LogCommandLine(argc, argv, std::cout);
414 LogCommandLine(argc, argv, ofs);
419 std::string FileHandling::LeadingZeroString(
const int &n,
420 const unsigned int &digits )
422 return FileHandling::toString(n, digits);
425 char FileHandling::ToLower_c(
char c) {
426 return char(tolower(c));
429 char FileHandling::ToUpper_c(
char c) {
430 return char(toupper(c));
433 std::string FileHandling::LowerCase(
const std::string &input)
437 transform(s.begin(), s.end(), s.begin(), ToLower_c);
441 std::string FileHandling::UpperCase(
const std::string &input)
445 transform(s.begin(), s.end(), s.begin(), ToUpper_c);
449 std::string FileHandling::CurrentDateTimeString()
451 time_t tim = time(NULL);
452 tm *now = localtime(&tim);
458 sstime << setw(4) << setfill(
'0') << now->tm_year+1900
459 << setw(2) << setfill(
'0') << now->tm_mon+1
460 << setw(2) << setfill(
'0') << now->tm_mday <<
"_"
461 << setw(2) << setfill(
'0') << now->tm_hour
462 << setw(2) << setfill(
'0') << now->tm_min
463 << setw(2) << setfill(
'0') << now->tm_sec;
469 int FileHandling::ExtractNumberFromRight(
const std::string &inStr,
470 int &number,
int &posL,
int &posR)
477 bool numericStarted=
false;
480 int current = (int)inStr.size()-1;
481 while (!done && current>=0)
483 const char c=inStr[current];
496 if (!numericStarted){
500 const int act =atoi(&c);
501 const int addAct = int(act*pow(10.0, exp10 ));
520 if (!numericStarted) {
528 int FileHandling::FilenameSequence(
const std::string & refFileName,
529 std::string & resultFilename,
530 const int & offsetFromRef,
531 const unsigned int & stepsize )
534 string dir, base, suffix;
535 SplitName(refFileName, dir, base, suffix );
540 int result=ExtractNumberFromRight(base, actIndex, posL, posR );
542 BIASERR(
"could not extract digit from base="<<base)
547 const int numberOfDigits = posR-posL+1;
548 BIASASSERT(numberOfDigits>0);
551 int newIndex = actIndex +offsetFromRef*stepsize;
554 string basePrefix = base.substr(0, posL);
555 string basePostfix = base.substr(posR+1, (base.size()-numberOfDigits));
556 resultFilename = dir + (basePrefix + FileHandling::LeadingZeroString(newIndex,numberOfDigits) + basePostfix)
561 int FileHandling::FilenameExpansion(
const std::string &in, std::string & out)
568 string tilde = getenv(
"HOME");
572 string homedrive = getenv(
"HOMEDRIVE");
573 string homepath = getenv(
"HOMEPATH");
576 const char backslash=
'\\';
577 const char fwdslash=
'/';
579 for (n=0; n<homedrive.size(); n++)
580 if (homedrive[n]==backslash)
581 homedrive[n]=fwdslash;
582 for (n=0; n<homepath.size(); n++)
583 if (homepath[n]==backslash)
584 homepath[n]=fwdslash;
589 tilde=homedrive+homepath;
592 out.replace(0, 1, tilde);
599 std::string FileHandling::GetCwd()
605 char * buf = _getcwd( NULL, 0 );
607 BIASERR(
"_getcwd error");
608 perror(
"_getcwd error");
617 char currentdir[1024];
619 char *cwd = getcwd(currentdir, 1024);
621 BIASERR(
"error getcwd()")
629 int FileHandling::ChDir(
const std::string &dir)
632 return _chdir(dir.c_str());
634 return chdir(dir.c_str());
638 std::string FileHandling::GetApplicationPath()
642 TCHAR szPathName[_MAX_PATH];
643 ::GetModuleFileName(NULL, szPathName, _MAX_PATH);
645 BIASERR(
"Module: "<<s);
647 BIASERR(
"not implemented")
654 bool FileHandling::IsAbsolute(
const std::string &path)
658 return (path.find(
string(
":\\")) != string::npos);
660 return (path[0] ==
'/');