CPN
Computational Process Networks
PathUtils.cc
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #include "common_priv.h"
24 #include <cpn/utils/PathUtils.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 static const char PATH_SEP = '/';
28 
29 std::string RealPath(const std::string &path) {
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 }
38 
39 std::string DirName(const std::string &path) {
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 }
63 
64 std::string BaseName(const std::string &path) {
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 }
84 
85 std::string PathConcat(const std::string &dir, const std::string &file) {
86  return dir + PATH_SEP + file;
87 }
88 
89 bool IsAbsPath(const std::string &path) {
90  bool ret = false;
91  if (!path.empty()) {
92  if (path[0] == PATH_SEP) {
93  ret = true;
94  }
95  }
96  return ret;
97 }
98 
std::string BaseName(const std::string &path)
Definition: PathUtils.cc:64
std::string RealPath(const std::string &path)
Definition: PathUtils.cc:29
static const char PATH_SEP
Definition: PathUtils.cc:27
std::string DirName(const std::string &path)
Definition: PathUtils.cc:39
std::string PathConcat(const std::string &dir, const std::string &file)
Definition: PathUtils.cc:85
bool IsAbsPath(const std::string &path)
Definition: PathUtils.cc:89