Changeset aa4d31 in git for ppcc/adlib
- Timestamp:
- Feb 7, 2019, 1:18:27 PM (5 years ago)
- Branches:
- (u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', 'c5facdfddea2addfd91babd8b9019161dea4b695')
- Children:
- 098784114c00a08a4d18cf392c0f2411aa0d007ed669f7b103e531e130535f2141dd1d77cfa31a81
- Parents:
- 54b24c79febc0e79ee3f9c6a86b7c4d505c69a43
- Location:
- ppcc/adlib
- Files:
-
- 5 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
ppcc/adlib/os.cc
r54b24c raa4d31 11 11 if (fp == NULL) 12 12 return NULL; 13 Str *result = new Str(1024);13 StrArr *result = new StrArr(); 14 14 for (;;) { 15 15 size_t nbytes = fread(buffer, 1, sizeof(buffer), fp); 16 16 if (nbytes == 0) 17 17 break; 18 result->add( buffer, nbytes);19 } 20 return result;18 result->add(new Str(buffer, nbytes)); 19 } 20 return StrJoin(result, ""); 21 21 } 22 22 … … 197 197 } 198 198 199 Str * Pwd() {199 Str *CurrentDir() { 200 200 #ifdef PATH_MAX 201 201 char *path = getcwd(NULL, PATH_MAX); … … 389 389 } 390 390 391 Str *AbsolutePath(Str *path) { 392 if (path->starts_with(PathSeparator)) { 393 return path->clone(); 394 } 395 Str *result = CurrentDir(); 396 return result->add(PathSeparator)->add(path); 397 } 398 399 Str *NormalizePath(Str *path) { 400 bool abs = path->starts_with(PathSeparator); 401 StrArr *parts = path->split(PathSeparator); 402 StrArr *result = new StrArr(); 403 for (Int i = 0; i < parts->len(); i++) { 404 Str *part = parts->at(i); 405 if (part->eq("") || part->eq(".")) { 406 // do nothing 407 } else if (part->eq("..")) { 408 if (result->len() > 0) 409 result->pop(); 410 else if (!abs) 411 result->add(part); 412 } else { 413 result->add(part); 414 } 415 } 416 if (abs) 417 return S(PathSeparator)->add(StrJoin(result, PathSeparator)); 418 else 419 return StrJoin(result, PathSeparator); 420 } 421 422 Str *GetEnv(const char *name) { 423 char *result = getenv(name); 424 if (result) 425 return new Str(result); 426 else 427 return NULL; 428 } 429 430 Str *GetEnv(Str *name) { 431 return GetEnv(name->c_str()); 432 } 433 434 Str *ProgramPath() { 435 Str *arg0 = new Str(ArgV[0]); 436 Str *result = arg0; 437 if (arg0) { 438 if (FileStat(result, true)) { 439 return NormalizePath(AbsolutePath(result)); 440 } 441 if (!arg0->starts_with(PathSeparator)) { 442 Str *path = GetEnv("PATH"); 443 StrArr *paths = path ? path->split(':') : A(); 444 for (Int i = 0; i < paths->len(); i++) { 445 Str *p = paths->at(i)->clone(); 446 p->add(PathSeparator); 447 p->add(arg0); 448 if (FileStat(p, true)) { 449 result = p; 450 break; 451 } 452 } 453 } 454 } 455 return result ? NormalizePath(AbsolutePath(result)) : result; 456 } 457 391 458 bool MakeDir(const char *path, bool recursive) { 392 459 if (!recursive) -
ppcc/adlib/os.h
r54b24c raa4d31 32 32 }; 33 33 34 Str * Pwd();34 Str *CurrentDir(); 35 35 bool ChDir(Str *path); 36 36 bool ChDir(const char *path); … … 65 65 Str *BaseName(Str *path); 66 66 Str *FileExtension(Str *path); 67 Str *AbsolutePath(Str *path); 68 Str *NormalizePath(Str *path); 69 Str *GetEnv(Str *name); 70 Str *GetEnv(const char *name); 71 72 Str *ProgramPath(); 73 67 74 bool MakeDir(Str *path, bool recursive = false); 68 75 bool MakeDir(const char *path, bool recursive = false); -
ppcc/adlib/str.cc
r54b24c raa4d31 11 11 12 12 StrArr *Str::split(const char *s, Int n) { 13 if (n == 1) 14 return split(s[0]); 13 15 Arr<Int> *parts = new Arr<Int>(); 14 16 parts->add(-n); … … 96 98 if (arr->len() == 0) 97 99 return new Str(); 98 Str *result = new Str(arr->len() * (n + 1)); 100 Int len = (arr->len() - 1) * n; 101 for (Int i = 0; i < arr->len(); i++) { 102 len += arr->at(i)->len(); 103 } 104 Str *result = new Str(len); 99 105 result->add(arr->first()); 100 106 for (Int i = 1; i < arr->len(); i++) { 101 result->add(sep, n); 107 if (n >= 0) 108 result->add(sep, n); 102 109 result->add(arr->at(i)); 103 110 } -
ppcc/adlib/test1.cc
r54b24c raa4d31 58 58 Check(sum_tree(tree) * 2 == counter * (counter - 1), 59 59 "stress test memory allocation"); 60 Check(NormalizePath(S("/foo/x/../bar/./"))->eq("/foo/bar"), "normalize path"); 61 Check(BaseName(ProgramPath())->eq("test1"), "program path"); 60 62 } -
ppcc/adlib/test2.cc
r54b24c raa4d31 22 22 Check(ListFiles(".")->len() > 0, "reading directories"); 23 23 StrSet *files = new StrSet(ListFiles("adlib")); 24 files = new StrSet(ListFileTree( Pwd(), ListFilesRelative));24 files = new StrSet(ListFileTree(CurrentDir(), ListFilesRelative)); 25 25 Check(files->contains(S(__FILE__)), "reading directories recursively"); 26 26 }
Note: See TracChangeset
for help on using the changeset viewer.