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 <cstdio>
27#include <cstdlib>
28#include <cstring>
29
30using namespace std;
31
32namespace 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 */
44PathParser::PathParser(std::string &path)
45{
46 ctor(path);
47}
48
49/** Constructor (C string).
50 * @param path path to parse
51 */
52PathParser::PathParser(const char *path)
53{
54 std::string spath = path;
55 ctor(spath);
56}
57
58void
59PathParser::ctor(const std::string &path)
60{
61 abs_path_ = false;
62
63 char *p = strdup(path.c_str());
64 char *saveptr;
65 char *r = strtok_r(p, "/", &saveptr);
66
67 if (!r) {
68 // single string, no slash, does not end with slash
69 push_back(p);
70 } else {
71 abs_path_ = (r != p);
72
73 while (r) {
74 if (strlen(r) > 0) {
75 push_back(r);
76 }
77 r = strtok_r(NULL, "/", &saveptr);
78 }
79 }
80
81 free(p);
82}
83
84/** Debug print to stdout. */
85void
87{
88 for (size_type i = 0; i < size(); ++i) {
89 printf("Path element: %s\n", ((*this)[i]).c_str());
90 }
91}
92
93/** Get path as string.
94 * Joins the path elements to one path again.
95 * @return path as string
96 */
97std::string
99{
100 string rv = abs_path_ ? "/" : "";
101
102 size_type sz = size();
103
104 if (sz > 0) {
105 rv += (*this)[0];
106 }
107
108 for (size_type i = 1; i < sz; ++i) {
109 rv += "/" + (*this)[i];
110 }
111
112 return rv;
113}
114
115/** Check if path is absolute.
116 * @return true if path is absolute, false otherwise
117 */
118bool
120{
121 return abs_path_;
122}
123
124} // end namespace fawkes
bool is_absolute() const
Check if path is absolute.
Definition: pathparser.cpp:119
PathParser(std::string &path)
Constructor (C++ string).
Definition: pathparser.cpp:44
void print_debug()
Debug print to stdout.
Definition: pathparser.cpp:86
std::string path_as_string()
Get path as string.
Definition: pathparser.cpp:98
Fawkes library namespace.