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 780 of file remake.cpp.

Referenced by main().

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

Normalize a target name.

Definition at line 815 of file remake.cpp.

Referenced by main(), and normalize_list().

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

Referenced by normalize().

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

Normalize the content of a list of targets.

Definition at line 864 of file remake.cpp.

Referenced by load_rule().

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