bool Pathname::FullToRelative(const Pathname &relativeto) { if (relativeto.IsNull() || IsNull()) return false; bool h1= HasDrive(); bool h2= relativeto.HasDrive(); if (h1 != h2) return false; //rozdilny zpusob adresace - nelze vytvorit relatvni cestu if (h1== true && h2== true && toupper(GetDrive()) != toupper(relativeto.GetDrive())) return false; //ruzne disky, nelze vytvorit relativni cestu if (strncmp(_path,"\\\\",2) == 0) //sitova cesta { int slsh = 0; //citac lomitek const char *a = _path; const char *b = relativeto._path; while (toupper(*a) == toupper(*b) && *a && slsh<3) //zacatek sitove cesty musi byt stejny { if (*a =='\\') slsh++; a++;b++; } if (slsh != 3) return false; //pokud neni stejny, nelze vytvorit relativni cestu } int sublevel = 0; const char *ps1= _path; const char *ps2= relativeto._path; if (h1) {ps1 += 2;ps2 += 2;} const char *sls = ps2; while (toupper(*ps1) == toupper(*ps2) && *ps1) { if (*ps2=='\\') sls = ps2+1; ps1++;ps2++; } ps1 -= ps2-sls; if (sls) { while (sls = strchr(sls,'\\')) { sls++; sublevel++; } } char *buff = (char *)alloca((sublevel*3+strlen(ps1)+1)*sizeof(*buff)); char *pos = buff; for (int i = 0;i<sublevel;i++) {strcpy(pos,"..\\");pos += 3;} strcpy(pos,ps1); SetDrive(0); SetDirectory(buff); return true; }
bool Pathname::RelativeToFull(const Pathname &ref) { if (ref.IsNull() || IsNull()) return false; const char *beg; if (HasDrive()) if (toupper(GetDrive()) != toupper(ref.GetDrive())) return false; else beg = _path+2; else beg = _path; const char *end = strchr(ref._path,0); if (beg[0] =='\\') { int np; if (ref.HasDrive()) end = ref._path+2; else if (np = ref.IsNetworkPath()) end = ref._path+np; else end = ref._path; } else while (strncmp(beg,"..\\",3) == 0 || strncmp(beg,".\\",2) == 0) { if (beg[1] =='.') { if (end>ref._path) { end--; while (end>ref._path && end[-1]!='\\') end--; } beg += 3; } else beg += 2; } int partln = end-ref._path; char *buff = (char *)alloca((partln+strlen(beg)+1)*sizeof(*buff)); memcpy(buff,ref._path,partln); strcpy(buff+partln,beg); SetDrive(0); SetDirectory(buff); return true; }