Remake
Functions
Path helpers

Functions

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

Detailed Description

Function Documentation

§ init_prefix_dir()

static void init_prefix_dir ( )
static

Initialize prefix_dir and switch to it.

Definition at line 882 of file remake.cpp.

Referenced by main().

883 {
884  for (;;)
885  {
886  struct stat s;
887  if (stat((prefix_dir + "/Remakefile").c_str(), &s) == 0)
888  {
889  if (!changed_prefix_dir) return;
890  if (chdir(prefix_dir.c_str()))
891  {
892  perror("Failed to change working directory");
893  exit(EXIT_FAILURE);
894  }
895  if (show_targets)
896  {
897  std::cout << "remake: Entering directory `" << prefix_dir << '\'' << std::endl;
898  }
899  return;
900  }
901  size_t pos = prefix_dir.find_last_of('/');
902  if (pos == std::string::npos)
903  {
904  std::cerr << "Failed to locate Remakefile in the current directory or one of its parents" << std::endl;
905  exit(EXIT_FAILURE);
906  }
907  prefix_dir.erase(pos);
908  changed_prefix_dir = true;
909  }
910 }
static std::string prefix_dir
Definition: remake.cpp:726
static bool show_targets
Definition: remake.cpp:706
static bool changed_prefix_dir
Definition: remake.cpp:731

§ init_working_dir()

static void init_working_dir ( )
static

Initialize working_dir.

Definition at line 860 of file remake.cpp.

Referenced by main().

861 {
862  char buf[1024];
863  char *res = getcwd(buf, sizeof(buf));
864  if (!res)
865  {
866  perror("Failed to get working directory");
867  exit(EXIT_FAILURE);
868  }
869  working_dir = buf;
870 #ifdef WINDOWS
871  for (size_t i = 0, l = working_dir.size(); i != l; ++i)
872  {
873  if (working_dir[i] == '\\') working_dir[i] = '/';
874  }
875 #endif
877 }
static std::string working_dir
Definition: remake.cpp:721
static std::string prefix_dir
Definition: remake.cpp:726

§ normalize()

static std::string normalize ( std::string const &  s,
std::string const &  w,
std::string const &  p 
)
static

Normalize path s (possibly relative to w) with respect to p.

  • If both p and w are empty, the function just removes ".", "..", "//".
  • If only p is empty, the function returns an absolute path.

Definition at line 938 of file remake.cpp.

Referenced by main(), and normalize_list().

939 {
940 #ifdef WINDOWS
941  char const *delim = "/\\";
942 #else
943  char delim = '/';
944 #endif
945  size_t pos = s.find_first_of(delim);
946  if (pos == std::string::npos && w == p) return s;
947  bool absolute = pos == 0;
948  if (!absolute && w != p && !w.empty())
949  return normalize(w + '/' + s, w, p);
950  size_t prev = 0, len = s.length();
951  string_list l;
952  for (;;)
953  {
954  if (pos != prev)
955  {
956  std::string n = s.substr(prev, pos - prev);
957  if (n == "..")
958  {
959  if (!l.empty()) l.pop_back();
960  else if (!absolute && !w.empty())
961  return normalize(w + '/' + s, w, p);
962  }
963  else if (n != ".")
964  l.push_back(n);
965  }
966  ++pos;
967  if (pos >= len) break;
968  prev = pos;
969  pos = s.find_first_of(delim, prev);
970  if (pos == std::string::npos) pos = len;
971  }
972  string_list::const_iterator i = l.begin(), i_end = l.end();
973  if (i == i_end) return absolute ? "/" : ".";
974  std::string n;
975  if (absolute) n.push_back('/');
976  n.append(*i);
977  for (++i; i != i_end; ++i)
978  {
979  n.push_back('/');
980  n.append(*i);
981  }
982  if (absolute && !p.empty()) return normalize_abs(n, p);
983  return n;
984 }
static std::string normalize(std::string const &s, std::string const &w, std::string const &p)
Definition: remake.cpp:938
std::list< std::string > string_list
Definition: remake.cpp:456
static std::string normalize_abs(std::string const &s, std::string const &p)
Definition: remake.cpp:916

§ normalize_abs()

static std::string normalize_abs ( std::string const &  s,
std::string const &  p 
)
static

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

Definition at line 916 of file remake.cpp.

Referenced by normalize().

917 {
918  size_t l = p.length();
919  if (s.compare(0, l, p)) return s;
920  size_t ll = s.length();
921  if (ll == l) return ".";
922  if (s[l] != '/')
923  {
924  size_t pos = s.rfind('/', l);
925  assert(pos != std::string::npos);
926  return s.substr(pos + 1);
927  }
928  if (ll == l + 1) return ".";
929  return s.substr(l + 1);
930 }

§ normalize_list()

static void normalize_list ( string_list l,
std::string const &  w,
std::string const &  p 
)
static

Normalize the content of a list of targets.

Definition at line 989 of file remake.cpp.

Referenced by load_rule(), and main().

990 {
991  for (string_list::iterator i = l.begin(),
992  i_end = l.end(); i != i_end; ++i)
993  {
994  *i = normalize(*i, w, p);
995  }
996 }
static std::string normalize(std::string const &s, std::string const &w, std::string const &p)
Definition: remake.cpp:938