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); }
void MakeNameUsable(char *Name,bool Extended) { #ifdef _WIN_ALL // In Windows we also need to convert characters not defined in current // code page. This double conversion changes them to '?', which is // catched by code below. size_t NameLength=strlen(Name); wchar NameW[NM]; CharToWide(Name,NameW,ASIZE(NameW)); WideToChar(NameW,Name,NameLength+1); Name[NameLength]=0; #endif for (char *s=Name;*s!=0;s=charnext(s)) { if (strchr(Extended ? "?*<>|\"":"?*",*s)!=NULL || Extended && (byte)*s<32) *s='_'; #ifdef _EMX if (*s=='=') *s='_'; #endif #ifndef _UNIX if (s-Name>1 && *s==':') *s='_'; // Remove ' ' and '.' before path separator, but allow .\ and ..\. if ((*s==' ' || *s=='.' && s>Name && !IsPathDiv(s[-1]) && s[-1]!='.') && IsPathDiv(s[1])) *s='_'; #endif } }
char* PointToName(const char *Path) { //const char *Found=NULL; for (const char *s=&Path[strlen(Path)-1];s>=Path;s--) if (IsPathDiv(*s)) return (char*)(s+1); // if (Found!=NULL) // return((char*)Found); return (char*)((*Path && IsDriveDiv(Path[1]) && charnext(Path)==Path+1) ? Path+2:Path); }
char* DosSlashToUnix(char *SrcName,char *DestName) { if (DestName!=NULL && DestName!=SrcName) strcpy(DestName,SrcName); for (char *s=SrcName;*s!=0;s=charnext(s)) { if (*s=='\\') if (DestName==NULL) *s='/'; else DestName[s-SrcName]='/'; } return(DestName==NULL ? SrcName:DestName); }
bool IsNameUsable(const char *Name) { #ifndef _UNIX if (Name[0] && Name[1] && strchr(Name+2,':')!=NULL) return(false); for (const char *s=Name;*s!=0;s=charnext(s)) { if ((byte)*s<32) return(false); if (*s==' ' && IsPathDiv(s[1])) return(false); } #endif return(*Name!=0 && strpbrk(Name,"?*<>|\"")==NULL); }
void MakeNameUsable(char *Name,bool Extended) { for (char *s=Name;*s!=0;s=charnext(s)) { if (strchr(Extended ? "?*<>|\"":"?*",*s)!=NULL || Extended && *s<32) *s='_'; #ifdef _EMX if (*s=='=') *s='_'; #endif #ifndef _UNIX if (s-Name>1 && *s==':') *s='_'; #endif } }
char* UnixSlashToDos(char *SrcName,char *DestName,uint MaxLength) { if (DestName!=NULL && DestName!=SrcName) strcpy(DestName,SrcName); for (char *s=SrcName;*s!=0;s=charnext(s)) { if (*s=='/') { if (DestName==NULL) *s='\\'; else DestName[s-SrcName]='\\'; } } return(DestName==NULL ? SrcName:DestName); }
/* evaluate .if strcmp (i.e. 'str'str') */ static int if_strcmp(int (*next)(void), void (*back)(int)) { char delim[GNLEN]; struct sbuf s1, s2; int ret; charnext(delim, next, back); sbuf_init(&s1); sbuf_init(&s2); read_until(&s1, delim, next, back); read_until(&s2, delim, next, back); cp_reqbeg(); ret = !strcmp(sbuf_buf(&s1), sbuf_buf(&s2)); sbuf_done(&s1); sbuf_done(&s2); return ret; }
char* DosSlashToUnix(char *SrcName,char *DestName,size_t MaxLength) { if (DestName!=NULL && DestName!=SrcName) if (strlen(SrcName)>=MaxLength) { *DestName=0; return DestName; } else strcpy(DestName,SrcName); for (char *s=SrcName;*s!=0;s=charnext(s)) { if (*s=='\\') if (DestName==NULL) *s='/'; else DestName[s-SrcName]='/'; } return DestName==NULL ? SrcName:DestName; }
bool CreatePath(const char *Path,bool SkipLastName) { if (Path==NULL || *Path==0) return(false); #if defined(_WIN_ALL) || defined(_EMX) uint DirAttr=0; #else uint DirAttr=0777; #endif bool Success=true; for (const char *s=Path;*s!=0;s=charnext(s)) { if (s-Path>=NM) break; if (*s==CPATHDIVIDER) { char DirName[NM]; strncpy(DirName,Path,s-Path); DirName[s-Path]=0; if (MakeDir(DirName,NULL,true,DirAttr)==MKDIR_SUCCESS) { #ifndef GUI mprintf(St(MCreatDir),DirName); mprintf(" %s",St(MOk)); #endif } else Success=false; } } if (!SkipLastName) if (!IsPathDiv(*PointToLastChar(Path))) if (MakeDir(Path,NULL,true,DirAttr)!=MKDIR_SUCCESS) Success=false; return(Success); }
/* read into sbuf until stop; if stop is NULL, stop at whitespace */ static int read_until(struct sbuf *sbuf, char *stop, int (*next)(void), void (*back)(int)) { char cs[GNLEN], cs2[GNLEN]; int c; while ((c = next()) >= 0) { if (c == c_ni) continue; back(c); if (c == '\n') return 1; if (!stop && (c == ' ' || c == '\t')) return 0; charnext(cs, next, back); if (stop && !strcmp(stop, cs)) return 0; charnext_str(cs2, cs); sbuf_append(sbuf, cs2); } return 1; }
void CreatePath(const char *Path,const wchar *PathW,bool SkipLastName) { #ifdef _WIN_32 uint DirAttr=0; #else uint DirAttr=0777; #endif int PosW=0; for (const char *s=Path;*s!=0 && PosW<NM;s=charnext(s),PosW++) { bool Wide=PathW!=NULL && *PathW!=0; if (Wide && PathW[PosW]==CPATHDIVIDER || !Wide && *s==CPATHDIVIDER) { wchar *DirPtrW=NULL; if (Wide) { wchar DirNameW[NM]; strncpyw(DirNameW,PathW,PosW); DirNameW[PosW]=0; DirPtrW=DirNameW; } char DirName[NM]; strncpy(DirName,Path,s-Path); DirName[s-Path]=0; if (MakeDir(DirName,DirPtrW,DirAttr)==MKDIR_SUCCESS) { #ifndef GUI mprintf(St(MCreatDir),DirName); mprintf(" %s",St(MOk)); #endif } } } if (!SkipLastName) MakeDir(Path,PathW,DirAttr); }
char* PointToLastChar(const char *Path) { for (const char *s=Path,*p=Path;;p=s,s=charnext(s)) if (*s==0) return((char *)p); }
void CreatePath(const char *Path,const wchar *PathW,bool SkipLastName) { #ifdef _WIN_32 uint DirAttr=0; #else uint DirAttr=0777; #endif #ifdef UNICODE_SUPPORTED bool Wide=PathW!=NULL && *PathW!=0 && UnicodeEnabled(); #else bool Wide=false; #endif bool IgnoreAscii=false; const char *s=Path; for (int PosW=0;;PosW++) { if (s==NULL || s-Path>=NM || *s==0) IgnoreAscii=true; if (Wide && (PosW>=NM || PathW[PosW]==0) || !Wide && IgnoreAscii) break; if (Wide && PathW[PosW]==CPATHDIVIDER || !Wide && *s==CPATHDIVIDER) { wchar *DirPtrW=NULL,DirNameW[NM]; if (Wide) { strncpyw(DirNameW,PathW,PosW); DirNameW[PosW]=0; DirPtrW=DirNameW; } char DirName[NM]; if (IgnoreAscii) WideToChar(DirPtrW,DirName); else { #ifndef DBCS_SUPPORTED if (*s!=CPATHDIVIDER) for (const char *n=s;*n!=0 && n-Path<NM;n++) if (*n==CPATHDIVIDER) { s=n; break; } #endif strncpy(DirName,Path,s-Path); DirName[s-Path]=0; } if (MakeDir(DirName,DirPtrW,DirAttr)==MKDIR_SUCCESS) { #ifndef GUI mprintf(St(MCreatDir),DirName); mprintf(" %s",St(MOk)); #endif } } if (!IgnoreAscii) s=charnext(s); } if (!SkipLastName) MakeDir(Path,PathW,DirAttr); }