📄 pathfn.cpp
字号:
#include "rar.hpp"char* PointToName(const char *Path){ const char *Found=NULL; for (const char *s=Path;*s!=0;s=charnext(s)) if (IsPathDiv(*s)) Found=(char*)(s+1); if (Found!=NULL) return((char*)Found); return (char*)((*Path && IsDriveDiv(Path[1]) && charnext(Path)==Path+1) ? Path+2:Path);}wchar* PointToName(const wchar *Path){ for (int I=strlenw(Path)-1;I>=0;I--) if (IsPathDiv(Path[I])) return (wchar*)&Path[I+1]; return (wchar*)((*Path && IsDriveDiv(Path[1])) ? Path+2:Path);}char* PointToLastChar(const char *Path){ for (const char *s=Path,*p=Path;;p=s,s=charnext(s)) if (*s==0) return((char *)p);}char* ConvertPath(const char *SrcPath,char *DestPath){ const char *DestPtr=SrcPath; /* prevents \..\ in any part of path string */ for (const char *s=DestPtr;*s!=0;s++) if (IsPathDiv(s[0]) && s[1]=='.' && s[2]=='.' && IsPathDiv(s[3])) DestPtr=s+4; /* removes any sequence of . and \ in the beginning of path string */ while (*DestPtr) { const char *s=DestPtr; if (s[0] && IsDriveDiv(s[1])) s+=2; else if (s[0]=='\\' && s[1]=='\\') { const char *Slash=strchr(s+2,'\\'); if (Slash!=NULL && (Slash=strchr(Slash+1,'\\'))!=NULL) s=Slash+1; } for (const char *t=s;*t!=0;t++) if (IsPathDiv(*t)) s=t+1; else if (*t!='.') break; if (s==DestPtr) break; DestPtr=s; } /* code above does not remove last "..", doing here */ if (DestPtr[0]=='.' && DestPtr[1]=='.' && DestPtr[2]==0) DestPtr+=2; if (DestPath!=NULL) { char TmpStr[NM]; strncpyz(TmpStr,DestPtr,ASIZE(TmpStr)); strcpy(DestPath,TmpStr); } return((char *)DestPtr);}wchar* ConvertPath(const wchar *SrcPath,wchar *DestPath){ const wchar *DestPtr=SrcPath; for (const wchar *s=DestPtr;*s!=0;s++) if (IsPathDiv(s[0]) && s[1]=='.' && s[2]=='.' && IsPathDiv(s[3])) DestPtr=s+4; while (*DestPtr) { const wchar *s=DestPtr; if (s[0] && IsDriveDiv(s[1])) s+=2; if (s[0]=='\\' && s[1]=='\\') { const wchar *Slash=strchrw(s+2,'\\'); if (Slash!=NULL && (Slash=strchrw(Slash+1,'\\'))!=NULL) s=Slash+1; } for (const wchar *t=s;*t!=0;t++) if (IsPathDiv(*t)) s=t+1; else if (*t!='.') break; if (s==DestPtr) break; DestPtr=s; } if (DestPath!=NULL) { wchar TmpStr[NM]; strncpyw(TmpStr,DestPtr,sizeof(TmpStr)/sizeof(TmpStr[0])-1); strcpyw(DestPath,TmpStr); } return((wchar *)DestPtr);}void SetExt(char *Name,const char *NewExt){ char *Dot=GetExt(Name); if (NewExt==NULL) { if (Dot!=NULL) *Dot=0; } else if (Dot==NULL) { strcat(Name,"."); strcat(Name,NewExt); } else strcpy(Dot+1,NewExt);}#ifndef SFX_MODULEvoid SetExt(wchar *Name,const wchar *NewExt){ if (Name==NULL || *Name==0) return; wchar *Dot=GetExt(Name); if (NewExt==NULL) { if (Dot!=NULL) *Dot=0; } else if (Dot==NULL) { strcatw(Name,L"."); strcatw(Name,NewExt); } else strcpyw(Dot+1,NewExt);}#endif#ifndef SFX_MODULEvoid SetSFXExt(char *SFXName){#ifdef _UNIX SetExt(SFXName,"sfx");#endif#if defined(_WIN_32) || defined(_EMX) SetExt(SFXName,"exe");#endif}#endif#ifndef SFX_MODULEvoid SetSFXExt(wchar *SFXName){ if (SFXName==NULL || *SFXName==0) return;#ifdef _UNIX SetExt(SFXName,L"sfx");#endif#if defined(_WIN_32) || defined(_EMX) SetExt(SFXName,L"exe");#endif}#endifchar *GetExt(const char *Name){ return(strrchrd(PointToName(Name),'.'));}wchar *GetExt(const wchar *Name){ return(Name==NULL ? (wchar *)L"":strrchrw(PointToName(Name),'.'));}bool CmpExt(const char *Name,const char *Ext){ char *NameExt=GetExt(Name); return(NameExt!=NULL && stricomp(NameExt+1,Ext)==0);}bool IsWildcard(const char *Str,const wchar *StrW){ if (StrW!=NULL && *StrW!=0) return(strpbrkw(StrW,L"*?")!=NULL); return(Str==NULL ? false:strpbrk(Str,"*?")!=NULL);}bool IsPathDiv(int Ch){#if defined(_WIN_32) || defined(_EMX) return(Ch=='\\' || Ch=='/');#else return(Ch==CPATHDIVIDER);#endif}bool IsDriveDiv(int Ch){#ifdef _UNIX return(false);#else return(Ch==':');#endif}int GetPathDisk(const char *Path){ if (IsDiskLetter(Path)) return(etoupper(*Path)-'A'); else return(-1);}void AddEndSlash(char *Path){ char *LastChar=PointToLastChar(Path); if (*LastChar!=0 && *LastChar!=CPATHDIVIDER) strcat(LastChar,PATHDIVIDER);}void AddEndSlash(wchar *Path){ int Length=strlenw(Path); if (Length>0 && Path[Length-1]!=CPATHDIVIDER) strcatw(Path,PATHDIVIDERW);}// returns file path including the trailing path separator symbolvoid GetFilePath(const char *FullName,char *Path,int MaxLength){ int PathLength=Min(MaxLength-1,PointToName(FullName)-FullName); strncpy(Path,FullName,PathLength); Path[PathLength]=0;}// returns file path including the trailing path separator symbolvoid GetFilePath(const wchar *FullName,wchar *Path,int MaxLength){ int PathLength=Min(MaxLength-1,PointToName(FullName)-FullName); strncpyw(Path,FullName,PathLength); Path[PathLength]=0;}// removes name and returns file path without the trailing// path separator symbolvoid RemoveNameFromPath(char *Path){ char *Name=PointToName(Path); if (Name>=Path+2 && (!IsDriveDiv(Path[1]) || Name>=Path+4)) Name--; *Name=0;}#ifndef SFX_MODULE// removes name and returns file path without the trailing// path separator symbolvoid RemoveNameFromPath(wchar *Path){ wchar *Name=PointToName(Path); if (Name>=Path+2 && (!IsDriveDiv(Path[1]) || Name>=Path+4)) Name--; *Name=0;}#endif#if defined(_WIN_32) && !defined(_WIN_CE) && !defined(SFX_MODULE)void GetAppDataPath(char *Path){ LPMALLOC g_pMalloc; SHGetMalloc(&g_pMalloc); LPITEMIDLIST ppidl; *Path=0; bool Success=false; if (SHGetSpecialFolderLocation(NULL,CSIDL_APPDATA,&ppidl)==NOERROR && SHGetPathFromIDList(ppidl,Path) && *Path!=0) { AddEndSlash(Path); strcat(Path,"WinRAR"); Success=FileExist(Path) || MakeDir(Path,NULL,0)==MKDIR_SUCCESS; } if (!Success) { GetModuleFileName(NULL,Path,NM); RemoveNameFromPath(Path); } g_pMalloc->Free(ppidl);}#endif#if defined(_WIN_32) && !defined(_WIN_CE) && !defined(SFX_MODULE)void GetRarDataPath(char *Path){ *Path=0; HKEY hKey; if (RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\WinRAR\\Paths",0, KEY_QUERY_VALUE,&hKey)==ERROR_SUCCESS) { DWORD DataSize=NM,Type; RegQueryValueEx(hKey,"AppData",0,&Type,(BYTE *)Path,&DataSize); RegCloseKey(hKey); } if (*Path==0 || !FileExist(Path)) GetAppDataPath(Path);}#endif#ifndef SFX_MODULEbool EnumConfigPaths(char *Path,int Number){#ifdef _EMX static char RARFileName[NM]; if (Number==-1) strcpy(RARFileName,Path); if (Number!=0) return(false);#ifndef _DJGPP if (_osmode==OS2_MODE) { PTIB ptib; PPIB ppib; DosGetInfoBlocks(&ptib, &ppib); DosQueryModuleName(ppib->pib_hmte,NM,Path); } else#endif strcpy(Path,RARFileName); RemoveNameFromPath(Path); return(true);#elif defined(_UNIX) static const char *AltPath[]={ "/etc","/etc/rar","/usr/lib","/usr/local/lib","/usr/local/etc" }; if (Number==0) { char *EnvStr=getenv("HOME"); strncpy(Path, (EnvStr==NULL) ? AltPath[0] : EnvStr, NM-1); Path[NM-1]=0; return(true); } Number--; if (Number<0 || Number>=sizeof(AltPath)/sizeof(AltPath[0])) return(false); strcpy(Path,AltPath[Number]); return(true);#elif defined(_WIN_32) if (Number<0 || Number>1) return(false); if (Number==0) GetRarDataPath(Path); else { GetModuleFileName(NULL,Path,NM);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -