/* (C) 2003 XDA Developers * Author: Willem Jan Hengeveld * Web: http://www.xda-developers.com/ * * $Header$ */ #include #include #include using namespace std; #include "debug.h" #include "stringutils.h" #include "fileutils.h" #define INVALID_FILE_ATTRIBUTES 0xffffffff FileState GetFileState(const string& filename) { DWORD dwAttr= GetFileAttributes(ToWString(filename).c_str()); if (dwAttr==INVALID_FILE_ATTRIBUTES) { DWORD dwError= GetLastError(); if (dwError==ERROR_FILE_NOT_FOUND || dwError==ERROR_PATH_NOT_FOUND) return FS_NOTFOUND; else return FS_NOACCESS; } else if (dwAttr & FILE_ATTRIBUTE_DIRECTORY) return FS_DIRECTORY; else return FS_FILE; } bool FileExists(const string &filename) { return GetFileState(filename) != FS_NOTFOUND; } bool CopyFile(const string &sourcefile, const string &destinationfile) { if (!CopyFile(ToWString(sourcefile).c_str(), ToWString(destinationfile).c_str(), false)) { error("copyfile"); return false; } return true; } bool ReadFileLines(const string &filename, StringList& lines) { FILE *f= fopen(filename.c_str(), "r"); if (f==NULL) { error("fopen"); return false; } char buf[1024]; while (!feof(f)) { if (NULL==fgets(buf, 1024, f)) { break; } chomp(buf); lines.push_back(string(buf)); } fclose(f); return true; } bool MakeLink(const string &linkname, const string &contents) { debug("makelink not yet implemented\n"); return false; } bool RenameFile(const string &originalname, const string &newname) { if (!MoveFile(ToWString(originalname).c_str(), ToWString(newname).c_str())) { error("movefile"); return false; } return true; } bool DeleteFile(const string &filename) { if (!DeleteFile(ToWString(filename).c_str())) { error("deletefile"); return false; } return true; } bool ParseFilename(const string& fullpath, string& path, string& filename) { string::size_type spos= fullpath.find_last_of("\\/"); if (spos==string::npos) { path.clear(); filename= fullpath; } else { path= fullpath.substr(0, spos-1); filename= fullpath.substr(spos+1); } return true; } string ConcatPaths(const string& path1, const string& path2) { if (path1.find_last_of("\\/")==path1.length()-1) return path1+path2; else return path1+"\\"+path2; }