Changeset aa4d31 in git for ppcc/adlib


Ignore:
Timestamp:
Feb 7, 2019, 1:18:27 PM (5 years ago)
Author:
Reimer Behrends <behrends@…>
Branches:
(u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', 'c5facdfddea2addfd91babd8b9019161dea4b695')
Children:
098784114c00a08a4d18cf392c0f2411aa0d007ed669f7b103e531e130535f2141dd1d77cfa31a81
Parents:
54b24c79febc0e79ee3f9c6a86b7c4d505c69a43
Message:
parallel preprocessor-related fixes.
Location:
ppcc/adlib
Files:
5 added
5 edited

Legend:

Unmodified
Added
Removed
  • ppcc/adlib/os.cc

    r54b24c raa4d31  
    1111  if (fp == NULL)
    1212    return NULL;
    13   Str *result = new Str(1024);
     13  StrArr *result = new StrArr();
    1414  for (;;) {
    1515    size_t nbytes = fread(buffer, 1, sizeof(buffer), fp);
    1616    if (nbytes == 0)
    1717      break;
    18     result->add(buffer, nbytes);
    19   }
    20   return result;
     18    result->add(new Str(buffer, nbytes));
     19  }
     20  return StrJoin(result, "");
    2121}
    2222
     
    197197}
    198198
    199 Str *Pwd() {
     199Str *CurrentDir() {
    200200#ifdef PATH_MAX
    201201  char *path = getcwd(NULL, PATH_MAX);
     
    389389}
    390390
     391Str *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
     399Str *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
     422Str *GetEnv(const char *name) {
     423  char *result = getenv(name);
     424  if (result)
     425    return new Str(result);
     426  else
     427    return NULL;
     428}
     429
     430Str *GetEnv(Str *name) {
     431  return GetEnv(name->c_str());
     432}
     433
     434Str *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
    391458bool MakeDir(const char *path, bool recursive) {
    392459  if (!recursive)
  • ppcc/adlib/os.h

    r54b24c raa4d31  
    3232};
    3333
    34 Str *Pwd();
     34Str *CurrentDir();
    3535bool ChDir(Str *path);
    3636bool ChDir(const char *path);
     
    6565Str *BaseName(Str *path);
    6666Str *FileExtension(Str *path);
     67Str *AbsolutePath(Str *path);
     68Str *NormalizePath(Str *path);
     69Str *GetEnv(Str *name);
     70Str *GetEnv(const char *name);
     71
     72Str *ProgramPath();
     73
    6774bool MakeDir(Str *path, bool recursive = false);
    6875bool MakeDir(const char *path, bool recursive = false);
  • ppcc/adlib/str.cc

    r54b24c raa4d31  
    1111
    1212StrArr *Str::split(const char *s, Int n) {
     13  if (n == 1)
     14    return split(s[0]);
    1315  Arr<Int> *parts = new Arr<Int>();
    1416  parts->add(-n);
     
    9698  if (arr->len() == 0)
    9799    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);
    99105  result->add(arr->first());
    100106  for (Int i = 1; i < arr->len(); i++) {
    101     result->add(sep, n);
     107    if (n >= 0)
     108      result->add(sep, n);
    102109    result->add(arr->at(i));
    103110  }
  • ppcc/adlib/test1.cc

    r54b24c raa4d31  
    5858  Check(sum_tree(tree) * 2 == counter * (counter - 1),
    5959      "stress test memory allocation");
     60  Check(NormalizePath(S("/foo/x/../bar/./"))->eq("/foo/bar"), "normalize path");
     61  Check(BaseName(ProgramPath())->eq("test1"), "program path");
    6062}
  • ppcc/adlib/test2.cc

    r54b24c raa4d31  
    2222  Check(ListFiles(".")->len() > 0, "reading directories");
    2323  StrSet *files = new StrSet(ListFiles("adlib"));
    24   files = new StrSet(ListFileTree(Pwd(), ListFilesRelative));
     24  files = new StrSet(ListFileTree(CurrentDir(), ListFilesRelative));
    2525  Check(files->contains(S(__FILE__)), "reading directories recursively");
    2626}
Note: See TracChangeset for help on using the changeset viewer.