Remake
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Functions
Path helpers

Functions

void init_working_dir ()
 
static std::string normalize_abs (std::string const &s)
 
static std::string normalize (std::string const &s)
 
static void normalize_list (string_list &l)
 

Detailed Description

Function Documentation

void init_working_dir ( )

Initialize working_dir.

Definition at line 779 of file remake.cpp.

Referenced by main().

780 {
781  char buf[1024];
782  char *res = getcwd(buf, sizeof(buf));
783  if (!res)
784  {
785  perror("Failed to get working directory");
786  exit(EXIT_FAILURE);
787  }
788  working_dir = buf;
789 }
static std::string normalize ( std::string const &  s)
static

Normalize a target name.

Definition at line 814 of file remake.cpp.

Referenced by main(), and normalize_list().

815 {
816 #ifdef WINDOWS
817  char const *delim = "/\\";
818 #else
819  char delim = '/';
820 #endif
821  size_t prev = 0, len = s.length();
822  size_t pos = s.find_first_of(delim);
823  if (pos == std::string::npos) return s;
824  bool absolute = pos == 0;
825  string_list l;
826  for (;;)
827  {
828  if (pos != prev)
829  {
830  std::string n = s.substr(prev, pos - prev);
831  if (n == "..")
832  {
833  if (!l.empty()) l.pop_back();
834  else if (!absolute)
835  return normalize(working_dir + '/' + s);
836  }
837  else if (n != ".")
838  l.push_back(n);
839  }
840  ++pos;
841  if (pos >= len) break;
842  prev = pos;
843  pos = s.find_first_of(delim, prev);
844  if (pos == std::string::npos) pos = len;
845  }
846  string_list::const_iterator i = l.begin(), i_end = l.end();
847  if (i == i_end) return absolute ? "/" : ".";
848  std::string n;
849  if (absolute) n.push_back('/');
850  n.append(*i);
851  for (++i; i != i_end; ++i)
852  {
853  n.push_back('/');
854  n.append(*i);
855  }
856  if (absolute) return normalize_abs(n);
857  return n;
858 }
static std::string normalize_abs ( std::string const &  s)
static

Normalize an absolute path with respect to the working directory. Paths outside the working subtree are left unchanged.

Definition at line 795 of file remake.cpp.

Referenced by normalize().

796 {
797  size_t l = working_dir.length();
798  if (s.compare(0, l, working_dir)) return s;
799  size_t ll = s.length();
800  if (ll == l) return ".";
801  if (s[l] != '/')
802  {
803  size_t pos = s.rfind('/', l);
804  assert(pos != std::string::npos);
805  return s.substr(pos + 1);
806  }
807  if (ll == l + 1) return ".";
808  return s.substr(l + 1);
809 }
static void normalize_list ( string_list l)
static

Normalize the content of a list of targets.

Definition at line 863 of file remake.cpp.

Referenced by load_rule().

864 {
865  for (string_list::iterator i = l.begin(),
866  i_end = l.end(); i != i_end; ++i)
867  {
868  *i = normalize(*i);
869  }
870 }