//--------------------------------------------------------------------------- bool File::Exists(const Ztring &File_Name) { #ifdef ZENLIB_USEWX wxFileName FN(File_Name.c_str()); return FN.FileExists(); #else //ZENLIB_USEWX #ifdef ZENLIB_STANDARD struct stat buffer; int status; #ifdef UNICODE status=stat(File_Name.To_Local().c_str(), &buffer); #else status=stat(File_Name.c_str(), &buffer); #endif //UNICODE return status==0 && S_ISREG(buffer.st_mode); #elif defined WINDOWS #ifdef UNICODE DWORD FileAttributes; if (IsWin9X()) FileAttributes=GetFileAttributesA(File_Name.To_Local().c_str()); else FileAttributes=GetFileAttributesW(File_Name.c_str()); #else DWORD FileAttributes=GetFileAttributes(File_Name.c_str()); #endif //UNICODE return ((FileAttributes!=INVALID_FILE_ATTRIBUTES) && !(FileAttributes&FILE_ATTRIBUTE_DIRECTORY)); #endif #endif //ZENLIB_USEWX }
Ztring OpenFolder_Show(void* Handle, const Ztring &Title, const Ztring &Caption) { //Caption Directory_Select_Caption=Caption; if (IsWin9X()) { return Ztring(); //Not supported in Win9X } else { //Values LPMALLOC Malloc; LPSHELLFOLDER ShellFolder; BROWSEINFO BrowseInfo; LPITEMIDLIST ItemIdList; //Initializing the SHBrowseForFolder function if (SHGetMalloc(&Malloc)!=NOERROR) return Ztring(); if (SHGetDesktopFolder(&ShellFolder)!=NOERROR) return Ztring(); ZeroMemory(&BrowseInfo, sizeof(BROWSEINFOW)); BrowseInfo.ulFlags+=BIF_RETURNONLYFSDIRS; BrowseInfo.hwndOwner=(HWND)Handle; BrowseInfo.pszDisplayName=InitDir; BrowseInfo.lpszTitle=Title.c_str(); BrowseInfo.lpfn=ShowOpenFolder_CallbackProc; //Displaying ItemIdList=SHBrowseForFolder(&BrowseInfo); //Releasing ShellFolder->Release(); if (ItemIdList!=NULL) { SHGetPathFromIDList(ItemIdList, InitDir); Malloc->Free(ItemIdList); Malloc->Release(); //The value return InitDir; } else return Ztring(); } }
void Shell_Execute(const Ztring &ToExecute) { #ifdef ZENLIB_USEWX #else //ZENLIB_USEWX #ifdef WINDOWS #ifdef UNICODE if (IsWin9X()) ShellExecuteA(NULL, "open", ToExecute.To_Local().c_str(), NULL, NULL, 0); else ShellExecute (NULL, _T("open"), ToExecute.c_str(), NULL, NULL, 0); #else ShellExecute(NULL, _T("open"), ToExecute.c_str(), NULL, NULL, 0); #endif #else //Not supported #endif #endif //ZENLIB_USEWX }
//--------------------------------------------------------------------------- bool File::Copy(const Ztring &Source, const Ztring &Destination, bool OverWrite) { #ifdef ZENLIB_USEWX return wxCopyFile(Source.c_str(), Destination.c_str(), OverWrite); #else //ZENLIB_USEWX #ifdef ZENLIB_STANDARD return false; #elif defined WINDOWS #ifdef UNICODE if (IsWin9X()) return CopyFileA(Source.To_Local().c_str(), Destination.To_Local().c_str(), !OverWrite)!=0; else return CopyFileW(Source.c_str(), Destination.c_str(), !OverWrite)!=0; #else return CopyFile(Source.c_str(), Destination.c_str(), !OverWrite)!=0; #endif //UNICODE #endif #endif //ZENLIB_USEWX }
//--------------------------------------------------------------------------- bool Dir::Create(const Ztring &File_Name) { #ifdef ZENLIB_USEWX return wxFileName::Mkdir(File_Name.c_str()); #else //ZENLIB_USEWX #ifdef WINDOWS #ifdef UNICODE if (IsWin9X()) return CreateDirectoryA(File_Name.To_Local().c_str(), NULL)!=0; else return CreateDirectoryW(File_Name.c_str(), NULL)!=0; #else return CreateDirectory(File_Name.c_str(), NULL)!=0; #endif //UNICODE #else //WINDOWS return mkdir(File_Name.To_Local().c_str(), 0700)==0; #endif //WINDOWS #endif //ZENLIB_USEWX }
int __stdcall ShowOpenFolder_CallbackProc (HWND hwnd, UINT uMsg, LPARAM, LPARAM) { if (uMsg==BFFM_INITIALIZED) { if (IsWin9X()) { SetWindowTextA (hwnd, Directory_Select_Caption.To_Local().c_str()); // Caption SendMessageA (hwnd, BFFM_ENABLEOK, 0, TRUE); SendMessageA (hwnd, BFFM_SETSELECTION, true, (LPARAM)&InitDirA); } else { SetWindowText (hwnd, Directory_Select_Caption.c_str()); // Caption SendMessage (hwnd, BFFM_ENABLEOK, 0, TRUE); SendMessage (hwnd, BFFM_SETSELECTION, true, (LPARAM)&InitDir); } } return 0; }
//--------------------------------------------------------------------------- bool File::Delete(const Ztring &File_Name) { #ifdef ZENLIB_USEWX return wxRemoveFile(File_Name.c_str()); #else //ZENLIB_USEWX #ifdef ZENLIB_STANDARD #ifdef UNICODE return remove(File_Name.To_Local().c_str())==0; #else return remove(File_Name.c_str())==0; #endif //UNICODE #elif defined WINDOWS #ifdef UNICODE if (IsWin9X()) return DeleteFileA(File_Name.To_Local().c_str())!=0; else return DeleteFileW(File_Name.c_str())!=0; #else return DeleteFile(File_Name.c_str())!=0; #endif //UNICODE #endif #endif //ZENLIB_USEWX }
//--------------------------------------------------------------------------- bool File::Move(const Ztring &Source, const Ztring &Destination, bool OverWrite) { if (OverWrite && Exists(Source)) Delete(Destination); #ifdef ZENLIB_USEWX if (OverWrite && Exists(Destination)) wxRemoveFile(Destination.c_str()); return wxRenameFile(Source.c_str(), Destination.c_str()); #else //ZENLIB_USEWX #ifdef ZENLIB_STANDARD return !std::rename(Source.To_Local().c_str(), Destination.To_Local().c_str()); #elif defined WINDOWS #ifdef UNICODE if (IsWin9X()) return MoveFileA(Source.To_Local().c_str(), Destination.To_Local().c_str())!=0; else return MoveFileW(Source.c_str(), Destination.c_str())!=0; #else return MoveFile(Source.c_str(), Destination.c_str())!=0; #endif //UNICODE #endif #endif //ZENLIB_USEWX }
//--------------------------------------------------------------------------- bool File::Create (const Ztring &File_Name, bool OverWrite) { #ifdef ZENLIB_USEWX File_Handle=(void*)new wxFile(); if (((wxFile*)File_Handle)->Create(File_Name.c_str(), OverWrite)==0) { //Sometime the file is locked for few milliseconds, we try again later wxMilliSleep(3000); if (((wxFile*)File_Handle)->Create(File_Name.c_str(), OverWrite)==0) //File is not openable return false; } return true; #else //ZENLIB_USEWX #ifdef ZENLIB_STANDARD /* int access; switch (OverWrite) { case false : access=O_BINARY|O_CREAT|O_WRONLY|O_EXCL ; break; default : access=O_BINARY|O_CREAT|O_WRONLY|O_TRUNC; break; } #ifdef UNICODE File_Handle=open(File_Name.To_Local().c_str(), access); #else File_Handle=open(File_Name.c_str(), access); #endif //UNICODE return File_Handle!=-1; */ /*ios_base::openmode mode; switch (OverWrite) { //case false : mode= ; break; default : mode=0 ; break; }*/ ios_base::openmode access; switch (OverWrite) { case false : if (Exists(File_Name)) return false; default : access=ios_base::binary|ios_base::in|ios_base::out|ios_base::trunc; break; } #ifdef UNICODE File_Handle=new fstream(File_Name.To_Local().c_str(), access); #else File_Handle=new fstream(File_Name.c_str(), access); #endif //UNICODE return ((fstream*)File_Handle)->is_open(); #elif defined WINDOWS DWORD dwDesiredAccess, dwShareMode, dwCreationDisposition; switch (OverWrite) { case false : dwDesiredAccess=GENERIC_WRITE; dwShareMode=0; dwCreationDisposition=CREATE_NEW; break; default : dwDesiredAccess=GENERIC_WRITE; dwShareMode=0; dwCreationDisposition=CREATE_ALWAYS; break; } #ifdef UNICODE if (IsWin9X()) File_Handle=CreateFileA(File_Name.To_Local().c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); else File_Handle=CreateFileW(File_Name.c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); #else File_Handle=CreateFile(File_Name.c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); #endif //UNICODE if (File_Handle==INVALID_HANDLE_VALUE) { //Sometime the file is locked for few milliseconds, we try again later Sleep(3000); #ifdef UNICODE if (IsWin9X()) File_Handle=CreateFileA(File_Name.To_Local().c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); else File_Handle=CreateFileW(File_Name.c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); #else File_Handle=CreateFile(File_Name.c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); #endif //UNICODE } if (File_Handle==INVALID_HANDLE_VALUE) //File is not openable return false; return true; #endif #endif //ZENLIB_USEWX }
//--------------------------------------------------------------------------- bool File::Open (const tstring &File_Name_, access_t Access) { File_Name=File_Name_; #ifdef ZENLIB_USEWX File_Handle=(void*)new wxFile(); if (((wxFile*)File_Handle)->Open(File_Name.c_str(), (wxFile::OpenMode)Access)==0) { //Sometime the file is locked for few milliseconds, we try again later wxMilliSleep(1000); if (((wxFile*)File_Handle)->Open(File_Name.c_str(), (wxFile::OpenMode)Access)==0) //File is not openable return false; } return true; #else //ZENLIB_USEWX #ifdef ZENLIB_STANDARD /* int access; switch (Access) { case Access_Read : access=O_BINARY|O_RDONLY ; break; case Access_Write : access=O_BINARY|O_WRONLY|O_CREAT|O_TRUNC ; break; case Access_Read_Write : access=O_BINARY|O_RDWR |O_CREAT ; break; case Access_Write_Append : access=O_BINARY|O_WRONLY|O_CREAT|O_APPEND ; break; default : access=0 ; break; } #ifdef UNICODE File_Handle=open(File_Name.To_Local().c_str(), access); #else File_Handle=open(File_Name.c_str(), access); #endif //UNICODE return File_Handle!=-1; */ ios_base::openmode mode; switch (Access) { case Access_Read : mode=ios_base::binary|ios_base::in; break; case Access_Write : mode=ios_base::binary|ios_base::out; break; case Access_Read_Write : mode=ios_base::binary|ios_base::in|ios_base::out; break; case Access_Write_Append : if (!Exists(File_Name)) mode=ios_base::binary|ios_base::out; else mode=ios_base::binary|ios_base::out|ios_base::app; break; default : ; } #ifdef UNICODE File_Handle=new fstream(File_Name.To_Local().c_str(), mode); #else File_Handle=new fstream(File_Name.c_str(), mode); #endif //UNICODE if (!((fstream*)File_Handle)->is_open()) { delete (fstream*)File_Handle; File_Handle=NULL; return false; } return true; #elif defined WINDOWS DWORD dwDesiredAccess, dwShareMode, dwCreationDisposition; switch (Access) { case Access_Read : dwDesiredAccess=IsWin9X()?GENERIC_READ:FILE_READ_DATA; dwShareMode=FILE_SHARE_READ|FILE_SHARE_WRITE; dwCreationDisposition=OPEN_EXISTING; break; case Access_Write : dwDesiredAccess=GENERIC_WRITE; dwShareMode=0; dwCreationDisposition=OPEN_ALWAYS; break; case Access_Read_Write : dwDesiredAccess=(IsWin9X()?GENERIC_READ:FILE_READ_DATA)|GENERIC_WRITE; dwShareMode=0; dwCreationDisposition=OPEN_ALWAYS; break; case Access_Write_Append : dwDesiredAccess=GENERIC_WRITE; dwShareMode=0; dwCreationDisposition=OPEN_ALWAYS; break; default : dwDesiredAccess=0; dwShareMode=0; dwCreationDisposition=0; break; } #ifdef UNICODE if (IsWin9X()) File_Handle=CreateFileA(File_Name.To_Local().c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); else File_Handle=CreateFileW(File_Name.c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); #else File_Handle=CreateFile(File_Name.c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); #endif //UNICODE if (File_Handle==INVALID_HANDLE_VALUE) { /* //Sometime the file is locked for few milliseconds, we try again later char lpMsgBuf[1000]; DWORD dw = GetLastError(); FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), lpMsgBuf, 1000, NULL ); */ Sleep(1000); #ifdef UNICODE if (IsWin9X()) File_Handle=CreateFileA(Ztring(File_Name).To_Local().c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); else File_Handle=CreateFileW(File_Name.c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); #else File_Handle=CreateFile(File_Name.c_str(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL); #endif //UNICODE } if (File_Handle==INVALID_HANDLE_VALUE) //File is not openable return false; //Append if (Access==Access_Write_Append) GoTo(0, FromEnd); return true; #endif #endif //ZENLIB_USEWX }
ZtringList Dir::GetAllFileNames(const Ztring &Dir_Name_, dirlist_t Options) { ZtringList ToReturn; Ztring Dir_Name=Dir_Name_; #ifdef ZENLIB_USEWX int Flags=wxDIR_FILES | wxDIR_DIRS; //Search for files wxArrayString Liste; wxFileName FullPath; FullPath=Dir_Name.c_str(); //-File if (FullPath.FileExists()) { FullPath.Normalize(); Liste.Add(FullPath.GetFullPath()); } //-Directory else if (FullPath.DirExists()) { FullPath.Normalize(); wxDir::GetAllFiles(FullPath.GetFullPath(), &Liste, Ztring(), Flags); } //-WildCards else { wxString FileName=FullPath.GetFullName(); FullPath.SetFullName(Ztring()); //Supress filename FullPath.Normalize(); if (FullPath.DirExists()) wxDir::GetAllFiles(FullPath.GetPath(), &Liste, FileName, Flags); } //Compatible array ToReturn.reserve(Liste.GetCount()); for (size_t Pos=0; Pos<Liste.GetCount(); Pos++) ToReturn.push_back(Liste[Pos].c_str()); #else //ZENLIB_USEWX #ifdef WINDOWS //Is a dir? if (Exists(Dir_Name)) Dir_Name+=_T("\\*"); //Path Ztring Path=FileName::Path_Get(Dir_Name); if (Path.empty()) { #ifdef UNICODE if (IsWin9X()) { DWORD Path_Size=GetFullPathNameA(Dir_Name.To_Local().c_str(), 0, NULL, NULL); char* PathTemp=new char[Path_Size+1]; if (GetFullPathNameA(Dir_Name.To_Local().c_str(), Path_Size+1, PathTemp, NULL)) Path=FileName::Path_Get(PathTemp); delete [] PathTemp; //PathTemp=NULL; } else { DWORD Path_Size=GetFullPathName(Dir_Name.c_str(), 0, NULL, NULL); Char* PathTemp=new Char[Path_Size+1]; if (GetFullPathNameW(Dir_Name.c_str(), Path_Size+1, PathTemp, NULL)) Path=FileName::Path_Get(PathTemp); delete [] PathTemp; //PathTemp=NULL; } #else DWORD Path_Size=GetFullPathName(Dir_Name.c_str(), 0, NULL, NULL); Char* PathTemp=new Char[Path_Size+1]; if (GetFullPathName(Dir_Name.c_str(), Path_Size+1, PathTemp, NULL)) Path=FileName::Path_Get(PathTemp); delete [] PathTemp; //PathTemp=NULL; #endif //UNICODE } #ifdef UNICODE WIN32_FIND_DATAA FindFileDataA; WIN32_FIND_DATAW FindFileDataW; HANDLE hFind; if (IsWin9X()) hFind=FindFirstFileA(Dir_Name.To_Local().c_str(), &FindFileDataA); else hFind=FindFirstFileW(Dir_Name.c_str(), &FindFileDataW); #else WIN32_FIND_DATA FindFileData; HANDLE hFind=FindFirstFile(Dir_Name.c_str(), &FindFileData); #endif //UNICODE if (hFind==INVALID_HANDLE_VALUE) return ZtringList(); BOOL ReturnValue; do { #ifdef UNICODE Ztring File_Name; if (IsWin9X()) File_Name=FindFileDataA.cFileName; else File_Name=FindFileDataW.cFileName; #else Ztring File_Name(FindFileData.cFileName); #endif //UNICODE if (File_Name!=_T(".") && File_Name!=_T("..")) //Avoid . an .. { Ztring File_Name_Complete=Path+_T("\\")+File_Name; if (Exists(File_Name_Complete)) { if (Options&Parse_SubDirs) ToReturn+=GetAllFileNames(File_Name_Complete, Options); //A SubDir } else if ((Options&Include_Hidden) || (!File_Name.empty() && File_Name[0]!=_T('.'))) ToReturn.push_back(File_Name_Complete); //A file } #ifdef UNICODE if (IsWin9X()) ReturnValue=FindNextFileA(hFind, &FindFileDataA); else ReturnValue=FindNextFileW(hFind, &FindFileDataW); #else ReturnValue=FindNextFile(hFind, &FindFileData); #endif //UNICODE } while (ReturnValue); FindClose(hFind); #else //WINDOWS //A file? if (File::Exists(Dir_Name)) { ToReturn.push_back(Dir_Name); //TODO return ToReturn; } //A dir? if (!Dir::Exists(Dir_Name)) return ToReturn; //Does not exist //open #ifdef UNICODE DIR* Dir=opendir(Dir_Name.To_Local().c_str()); #else DIR* Dir=opendir(Dir_Name.c_str()); #endif //UNICODE if (Dir) { //This is a dir //Normalizing dir (the / at the end) size_t Dir_Pos=Dir_Name.rfind(FileName_PathSeparator); if (Dir_Pos==std::string::npos) Dir_Name+=FileName_PathSeparator; else if (Dir_Pos+Ztring(FileName_PathSeparator).size()!=Dir_Name.size()) Dir_Name+=FileName_PathSeparator; struct dirent *DirEnt; while((DirEnt=readdir(Dir))!=NULL) { //A file Ztring File_Name(DirEnt->d_name); if (File_Name!=_T(".") && File_Name!=_T("..")) //Avoid . an .. { Ztring File_Name_Complete=Dir_Name+File_Name; if (Exists(File_Name_Complete)) { if (Options&Parse_SubDirs) ToReturn+=GetAllFileNames(File_Name_Complete, Options); //A SubDir } else if ((Options&Include_Hidden) || (!File_Name.empty() && File_Name[0]!=_T('.'))) ToReturn.push_back(File_Name_Complete); //A file } } //Close it closedir(Dir); } else { glob_t globbuf; if (glob(Dir_Name.To_Local().c_str(), GLOB_NOSORT, NULL, &globbuf)==0) { for (int Pos=0; Pos<globbuf.gl_pathc; Pos++) ToReturn.push_back(Ztring().From_Local(globbuf.gl_pathv[Pos])); } } #endif #endif //ZENLIB_USEWX return ToReturn; }