Fawkes API  Fawkes Development Version
pathparser.cpp
1 
2 /***************************************************************************
3  * pathparser.cpp - Header for path parser
4  *
5  * Created: Mon Jul 07 13:25:10 2008
6  * Copyright 2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <utils/system/pathparser.h>
25 
26 #include <cstring>
27 #include <cstdlib>
28 #include <cstdio>
29 
30 using namespace std;
31 
32 namespace fawkes {
33 
34 /** @class PathParser <utils/system/pathparser.h>
35  * Path parser.
36  * Parses a given (Unix) file system path and provides the elements and vector
37  * elements.
38  * @author Tim Niemueller
39  */
40 
41 /** Constructor (C++ string).
42  * @param path path to parse
43  */
44 PathParser::PathParser(std::string &path)
45 {
46  ctor(path);
47 }
48 
49 
50 /** Constructor (C string).
51  * @param path path to parse
52  */
53 PathParser::PathParser(const char *path)
54 {
55  std::string spath = path;
56  ctor(spath);
57 }
58 
59 
60 void
61 PathParser::ctor(const std::string &path)
62 {
63  __abs_path = false;
64 
65  char *p = strdup(path.c_str());
66  char *saveptr;
67  char *r = strtok_r(p, "/", &saveptr);
68 
69  if ( ! r ) {
70  // single string, no slash, does not end with slash
71  push_back(p);
72  } else {
73  __abs_path = ( r != p );
74 
75  while ( r ) {
76  if ( strlen(r) > 0 ) {
77  push_back(r);
78  }
79  r = strtok_r(NULL, "/", &saveptr);
80  }
81  }
82 
83  free(p);
84 }
85 
86 
87 /** Debug print to stdout. */
88 void
89 PathParser::print_debug()
90 {
91  for (size_type i = 0; i < size(); ++i) {
92  printf("Path element: %s\n", ((*this)[i]).c_str());
93  }
94 }
95 
96 /** Get path as string.
97  * Joins the path elements to one path again.
98  * @return path as string
99  */
100 std::string
101 PathParser::path_as_string()
102 {
103  string rv = __abs_path ? "/" : "";
104 
105  size_type sz = size();
106 
107  if ( sz > 0 ) {
108  rv += (*this)[0];
109  }
110 
111  for (size_type i = 1; i < sz; ++i) {
112  rv += "/" + (*this)[i];
113  }
114 
115  return rv;
116 }
117 
118 
119 /** Check if path is absolute.
120  * @return true if path is absolute, false otherwise
121  */
122 bool
123 PathParser::is_absolute() const
124 {
125  return __abs_path;
126 }
127 
128 } // end namespace fawkes
Fawkes library namespace.
STL namespace.