1 // THIS FILE IS MODIFIED TO WORK WITH XBMC
4 #include "utils/URIUtils.h"
6 char* PointToName(const char *Path)
8 //const char *Found=NULL;
9 for (const char *s=&Path[strlen(Path)-1];s>=Path;s--)
13 // return((char*)Found);
14 return (char*)((*Path && IsDriveDiv(Path[1]) && charnext(Path)==Path+1) ? Path+2:Path);
18 Dumbass broken routine!!
19 Not searching in reverse to strip off filename portion of path!
21 const char *Found=NULL;
22 for (const char *s=Path;*s!=0;s=charnext(s))
27 return (char*)((*Path && IsDriveDiv(Path[1]) && charnext(Path)==Path+1) ? Path+2:Path);
31 wchar* PointToName(const wchar *Path)
33 for (int I=strlenw(Path)-1;I>=0;I--)
34 if (IsPathDiv(Path[I]))
35 return (wchar*)&Path[I+1];
36 return (wchar*)((*Path && IsDriveDiv(Path[1])) ? Path+2:Path);
40 char* PointToLastChar(const char *Path)
42 for (const char *s=Path,*p=Path;;p=s,s=charnext(s))
48 char* ConvertPath(const char *SrcPath,char *DestPath)
50 const char *DestPtr=SrcPath;
51 for (const char *s=DestPtr;*s!=0;s++)
52 if (IsPathDiv(s[0]) && s[1]=='.' && s[2]=='.' && IsPathDiv(s[3]))
56 const char *s=DestPtr;
57 if (s[0] && IsDriveDiv(s[1]))
59 if (s[0]=='\\' && s[1]=='\\')
61 const char *Slash=strchr(s+2,'\\');
62 if (Slash!=NULL && (Slash=strchr(Slash+1,'\\'))!=NULL)
65 for (const char *t=s;*t!=0;t++)
78 strncpy(TmpStr,DestPtr,sizeof(TmpStr)-1);
79 strcpy(DestPath,TmpStr);
81 return((char *)DestPtr);
85 wchar* ConvertPath(const wchar *SrcPath,wchar *DestPath)
87 const wchar *DestPtr=SrcPath;
88 for (const wchar *s=DestPtr;*s!=0;s++)
89 if (IsPathDiv(s[0]) && s[1]=='.' && s[2]=='.' && IsPathDiv(s[3]))
93 const wchar *s=DestPtr;
94 if (s[0] && IsDriveDiv(s[1]))
96 if (s[0]=='\\' && s[1]=='\\')
98 const wchar *Slash=strchrw(s+2,'\\');
99 if (Slash!=NULL && (Slash=strchrw(Slash+1,'\\'))!=NULL)
102 for (const wchar *t=s;*t!=0;t++)
115 strncpyw(TmpStr,DestPtr,sizeof(TmpStr)/sizeof(TmpStr[0])-1);
116 strcpyw(DestPath,TmpStr);
118 return((wchar *)DestPtr);
122 void SetExt(char *Name,const char *NewExt)
124 char *Dot=GetExt(Name);
137 strcpy(Dot+1,NewExt);
142 void SetExt(wchar *Name,const wchar *NewExt)
144 if (Name==NULL || *Name==0)
146 wchar *Dot=GetExt(Name);
156 strcatw(Name,NewExt);
159 strcpyw(Dot+1,NewExt);
165 void SetSFXExt(char *SFXName)
168 SetExt(SFXName,"sfx");
171 #if defined(_WIN_32) || defined(_EMX)
172 SetExt(SFXName,"exe");
179 void SetSFXExt(wchar *SFXName)
181 if (SFXName==NULL || *SFXName==0)
185 SetExt(SFXName,L"sfx");
188 #if defined(_WIN_32) || defined(_EMX)
189 SetExt(SFXName,L"exe");
195 char *GetExt(const char *Name)
197 CStdString strExtension = URIUtils::GetExtension(Name);
198 return((char *)strstr((char *)Name,strExtension.c_str()));
201 wchar *GetExt(const wchar *Name)
203 CStdString strExtension = URIUtils::GetExtension(Name);
204 return((wchar *)wcsstr((wchar_t *)Name,CStdStringW(strExtension).c_str()));
208 bool CmpExt(const char *Name,const char *Ext)
210 char *NameExt=GetExt(Name);
211 return(NameExt!=NULL && stricomp(NameExt+1,Ext)==0);
215 bool IsWildcard(const char *Str,const wchar *StrW)
217 if (StrW!=NULL && *StrW!=0)
218 return(strpbrkw(StrW,L"*?")!=NULL);
219 return(Str==NULL ? false:strpbrk(Str,"*?")!=NULL);
223 bool IsPathDiv(int Ch)
225 #if defined(_WIN_32) || defined(_EMX)
226 return(Ch=='\\' || Ch=='/');
228 return(Ch==CPATHDIVIDER);
233 bool IsDriveDiv(int Ch)
243 int GetPathDisk(const char *Path)
245 if (IsDiskLetter(Path))
246 return(toupper(*Path)-'A');
252 void AddEndSlash(char *Path)
254 char *LastChar=PointToLastChar(Path);
255 if (*LastChar!=0 && *LastChar!=CPATHDIVIDER)
256 strcat(LastChar,PATHDIVIDER);
260 void AddEndSlash(wchar *Path)
262 int Length=strlenw(Path);
263 if (Length>0 && Path[Length-1]!=CPATHDIVIDER)
264 strcatw(Path,PATHDIVIDERW);
268 void GetFilePath(const char *FullName,char *Path)
270 int PathLength=PointToName(FullName)-FullName;
271 strncpy(Path,FullName,PathLength);
276 void GetFilePath(const wchar *FullName,wchar *Path)
278 const wchar *PathPtr=/*(*FullName && IsDriveDiv(FullName[1])) ? FullName+2:*/FullName;
279 int PathLength=PointToName(FullName)-FullName;
280 strncpyw(Path,PathPtr,PathLength);
285 void RemoveNameFromPath(char *Path)
287 char *Name=PointToName(Path);
288 if (Name>=Path+2 && (!IsDriveDiv(Path[1]) || Name>=Path+4))
295 void RemoveNameFromPath(wchar *Path)
297 wchar *Name=PointToName(Path);
298 if (Name>=Path+2 && (!IsDriveDiv(Path[1]) || Name>=Path+4))
306 bool EnumConfigPaths(char *Path,int Number)
309 static char RARFileName[NM];
311 strcpy(RARFileName,Path);
314 if (_osmode==OS2_MODE)
318 DosGetInfoBlocks(&ptib, &ppib);
319 DosQueryModuleName(ppib->pib_hmte,NM,Path);
322 strcpy(Path,RARFileName);
323 RemoveNameFromPath(Path);
328 char *EnvStr=getenv("HOME");
331 strncpy(Path,EnvStr,NM);
335 static const char *AltPath[]={
336 "/etc","/usr/lib","/usr/local/lib","/usr/local/etc"
339 if (Number<0 || Number>=(int)(sizeof(AltPath)/sizeof(AltPath[0])))
341 strcpy(Path,AltPath[Number]);
343 #elif defined(_WIN_32)
346 #if !defined(TARGET_POSIX)
347 GetModuleFileName(NULL,Path,NM);
348 RemoveNameFromPath(Path);
359 void GetConfigName(const char *Name,char *FullName, bool CheckExist)
361 for (int I=0;EnumConfigPaths(FullName,I);I++)
363 AddEndSlash(FullName);
364 strcat(FullName,Name);
365 if (!CheckExist || WildFileExist(FullName))
372 char* GetVolNumPart(char *ArcName)
374 char *ChPtr=ArcName+strlen(ArcName)-1;
375 while (!isdigit(*ChPtr) && ChPtr>ArcName)
378 while (isdigit(*NumPtr) && NumPtr>ArcName)
380 while (NumPtr>ArcName && *NumPtr!='.')
382 if (isdigit(*NumPtr))
384 char *Dot=strchrd(PointToName(ArcName),'.');
385 if (Dot!=NULL && Dot<NumPtr)
395 void NextVolumeName(char *ArcName,bool OldNumbering)
398 if ((ChPtr=GetExt(ArcName))==NULL)
400 strcat(ArcName,".rar");
401 ChPtr=GetExt(ArcName);
404 if (ChPtr[1]==0 || stricomp(ChPtr+1,"exe")==0 || stricomp(ChPtr+1,"sfx")==0)
405 strcpy(ChPtr+1,"rar");
408 ChPtr=GetVolNumPart(ArcName);
410 while ((++(*ChPtr))=='9'+1)
414 if (ChPtr<ArcName || !isdigit(*ChPtr))
416 for (char *EndPtr=ArcName+strlen(ArcName);EndPtr!=ChPtr;EndPtr--)
424 if (!isdigit(*(ChPtr+2)) || !isdigit(*(ChPtr+3)))
425 strcpy(ChPtr+2,"00");
429 while ((++(*ChPtr))=='9'+1)
444 bool IsNameUsable(const char *Name)
447 if ( Name == NULL) return false;
448 char cIllegalChars[] = "<>=?;\"*+,/|";
449 unsigned int iIllegalCharSize = strlen(cIllegalChars);
451 unsigned int iSize = strlen(Name);
452 if(iSize > 42) return false;
454 for (unsigned int i = 0; i < iSize; i++)
456 isIllegalChar = false;
457 // check for illegal chars
458 for (unsigned j = 0; j < iIllegalCharSize; j++)
459 if (Name[i] == cIllegalChars[j]) isIllegalChar = true;
460 // FATX only allows chars from 32 till 127
461 if (isIllegalChar == true || Name[i] < 32 || Name[i] > 126) return false;
466 if (Name[0] && Name[1] && strchr(Name+2,':')!=NULL)
469 if (strlen(Name) < 43) return 1;
471 return(*Name!=0 && strpbrk(Name,"?*<>|")==NULL);
476 void MakeNameUsable(char *Name, bool bKeepExtension, bool IsFATX)
478 // Changed to be compatible with xbmc's MakeLegalFileName function
481 if ( Name == NULL) return;
482 char cIllegalChars[] = "<>=?;\"*+,/|";
483 unsigned int iIllegalCharSize = strlen(cIllegalChars);
485 unsigned int iSize = strlen(Name);
486 unsigned int iNewStringSize = 0;
487 char* strNewString = new char[iSize + 1];
489 // only copy the legal characters to the new filename
490 for (unsigned int i = 0; i < iSize; i++)
492 isIllegalChar = false;
493 // check for illigal chars
494 for (unsigned j = 0; j < iIllegalCharSize; j++)
495 if (Name[i] == cIllegalChars[j]) isIllegalChar = true;
496 // FATX only allows chars from 32 till 127
497 if (isIllegalChar == false &&
498 Name[i] > 31 && Name[i] < 127) strNewString[iNewStringSize++] = Name[i];
500 strNewString[iNewStringSize] = '\0';
504 // since we can only write to samba shares and hd, we assume this has to be a fatx filename
505 // thus we have to strip it down to 42 chars (samba doesn't have this limitation)
507 char* FileName = PointToName(strNewString);
508 int iFileNameSize = strlen(FileName);
509 // no need to keep the extension, just strip it down to 42 characters
510 if (iFileNameSize > 42 && bKeepExtension == false) FileName[42] = '\0';
512 // we want to keep the extension
513 else if (iFileNameSize > 42 && bKeepExtension == true)
515 char strExtension[42];
516 unsigned int iExtensionLength = iFileNameSize - (strrchr(FileName, '.') - FileName);
517 strcpy(strExtension, (FileName + iFileNameSize - iExtensionLength));
519 strcpy(FileName + (42 - iExtensionLength), strExtension);
523 strcpy(Name, strNewString);
524 delete[] strNewString;
527 char* UnixSlashToDos(char *SrcName,char *DestName,uint MaxLength)
529 if (DestName!=NULL && DestName!=SrcName)
530 strcpy(DestName,SrcName);
531 for (char *s=SrcName;*s!=0;s=charnext(s))
538 DestName[s-SrcName]='\\';
541 return(DestName==NULL ? SrcName:DestName);
545 char* DosSlashToUnix(char *SrcName,char *DestName,uint MaxLength)
547 if (DestName!=NULL && DestName!=SrcName)
549 if (strlen(SrcName)>=MaxLength)
555 strcpy(DestName,SrcName);
557 for (char *s=SrcName;*s!=0;s=charnext(s))
564 DestName[s-SrcName]='/';
567 return(DestName==NULL ? SrcName:DestName);
571 bool IsFullPath(const char *Path)
574 GetFilePath(Path,PathOnly);
575 if (IsWildcard(PathOnly))
577 #if defined(_WIN_32) || defined(_EMX)
578 return(Path[0]=='\\' && Path[1]=='\\' ||
579 IsDiskLetter(Path) && IsPathDiv(Path[2]));
581 return(IsPathDiv(Path[0]));
586 bool IsDiskLetter(const char *Path)
588 char Letter=toupper(Path[0]);
589 return(Letter>='A' && Letter<='Z' && IsDriveDiv(Path[1]));
593 void GetPathRoot(const char *Path,char *Root)
596 if (IsDiskLetter(Path))
597 sprintf(Root,"%c:\\",*Path);
599 if (Path[0]=='\\' && Path[1]=='\\')
601 const char *Slash=strchr(Path+2,'\\');
605 if ((Slash=strchr(Slash+1,'\\'))!=NULL)
609 strncpy(Root,Path,Length);
616 int ParseVersionFileName(char *Name,wchar *NameW,bool Truncate)
619 char *VerText=strrchrd(Name,';');
622 Version=atoi(VerText+1);
628 wchar *VerTextW=strrchrw(NameW,';');
632 Version=atoiw(VerTextW+1);
642 char* VolNameToFirstName(const char *VolName,char *FirstName,bool NewNumbering)
644 if (FirstName!=VolName)
645 strcpy(FirstName,VolName);
646 char *VolNumStart=FirstName;
650 for (char *ChPtr=GetVolNumPart(FirstName);ChPtr>FirstName;ChPtr--)
665 SetExt(FirstName,"rar");
666 VolNumStart=GetExt(FirstName);
668 if (!FileExist(FirstName))
671 strcpy(Mask,FirstName);
676 while (Find.Next(&FD))
679 if (Arc.Open(FD.Name,FD.NameW) && Arc.IsArchive(true) && !Arc.NotFirstVolume)
681 strcpy(FirstName,FD.Name);
693 wchar* GetWideName(const char *Name,const wchar *NameW,wchar *DestW)
695 if (NameW!=NULL && *NameW!=0)
698 strcpyw(DestW,NameW);
701 CharToWide(Name,DestW);