CPN
Computational Process Networks
Functions | Variables
PathUtils.cc File Reference
#include "common_priv.h"
#include <cpn/utils/PathUtils.h>
#include <limits.h>
#include <stdlib.h>
+ Include dependency graph for PathUtils.cc:

Go to the source code of this file.

Functions

std::string RealPath (const std::string &path)
 
std::string DirName (const std::string &path)
 
std::string BaseName (const std::string &path)
 
std::string PathConcat (const std::string &dir, const std::string &file)
 
bool IsAbsPath (const std::string &path)
 

Variables

static const char PATH_SEP = '/'
 

Detailed Description

Author
John Bridgman

Definition in file PathUtils.cc.

Function Documentation

std::string BaseName ( const std::string &  path)

Essentially the same as basename from libgen but this one is reentrant.

Definition at line 64 of file PathUtils.cc.

References PATH_SEP.

64  {
65  std::string ret;
66  if (!path.empty()) {
67  std::string::const_iterator end = path.end();
68  std::string::const_iterator start;
69  --end;
70  while (end != path.begin() && *end == PATH_SEP) { --end; }
71  if (end == path.begin()) {
72  if (*end == PATH_SEP) {
73  ret = std::string(end, end + 1);
74  }
75  } else {
76  start = end;
77  while (start != path.begin() && *start != PATH_SEP) { --start; }
78  if (*start == PATH_SEP) { ++start; }
79  ret = std::string(start, end + 1);
80  }
81  }
82  return ret;
83 }
static const char PATH_SEP
Definition: PathUtils.cc:27
std::string DirName ( const std::string &  path)
Returns
the directory name of path. Trailing / are not considered part of the path. If no / is contained inside path then . is returned.

This was written to provide a reentrant version of dirname. Should work the same as dirname in the POSIX 1003.1-2008 specification. Note that giving '..' returns '.' which is what the POSIX standard says but man basename does '..' gives '..' on most systems.

Definition at line 39 of file PathUtils.cc.

References PATH_SEP.

Referenced by Loader().

39  {
40  std::string ret = ".";
41  if (!path.empty()) {
42  std::string::const_iterator cur = path.end();
43  --cur;
44  while (cur != path.begin() && *cur == PATH_SEP) { --cur; }
45  if (cur == path.begin()) {
46  if (*cur == PATH_SEP) {
47  ret = std::string(cur, cur + 1);
48  }
49  } else {
50  while (cur != path.begin() && *cur != PATH_SEP) { --cur; }
51  if (cur == path.begin()) {
52  if (*cur == PATH_SEP) {
53  ret = std::string(cur, cur + 1);
54  }
55  } else {
56  while (cur != path.begin() && *cur == PATH_SEP) { --cur; }
57  ret = std::string(path.begin(), cur + 1);
58  }
59  }
60  }
61  return ret;
62 }
static const char PATH_SEP
Definition: PathUtils.cc:27

+ Here is the caller graph for this function:

bool IsAbsPath ( const std::string &  path)
Returns
true if path is an absolute path.

Definition at line 89 of file PathUtils.cc.

References PATH_SEP.

Referenced by ProcessLine().

89  {
90  bool ret = false;
91  if (!path.empty()) {
92  if (path[0] == PATH_SEP) {
93  ret = true;
94  }
95  }
96  return ret;
97 }
static const char PATH_SEP
Definition: PathUtils.cc:27

+ Here is the caller graph for this function:

std::string PathConcat ( const std::string &  dir,
const std::string &  file 
)

Convinience function to concatinate a filename to a directory in a platform dependent way.

Definition at line 85 of file PathUtils.cc.

References PATH_SEP.

Referenced by ProcessLine().

85  {
86  return dir + PATH_SEP + file;
87 }
static const char PATH_SEP
Definition: PathUtils.cc:27

+ Here is the caller graph for this function:

std::string RealPath ( const std::string &  path)

Fully resolve path to its absolute path. path must exist. Follows symlinks.

Definition at line 29 of file PathUtils.cc.

Referenced by Loader(), and ProcessLine().

29  {
30  std::string ret;
31  char *str = realpath(path.c_str(), 0);
32  if (str != 0) {
33  ret = str;
34  free(str);
35  }
36  return ret;
37 }

+ Here is the caller graph for this function:

Variable Documentation

const char PATH_SEP = '/'
static

Definition at line 27 of file PathUtils.cc.

Referenced by BaseName(), DirName(), IsAbsPath(), and PathConcat().