pathparser.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <utils/system/pathparser.h>
00025
00026 #include <cstring>
00027 #include <cstdlib>
00028 #include <cstdio>
00029
00030 using namespace std;
00031
00032 namespace fawkes {
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 PathParser::PathParser(std::string &path)
00045 {
00046 ctor(path);
00047 }
00048
00049
00050
00051
00052
00053 PathParser::PathParser(const char *path)
00054 {
00055 std::string spath = path;
00056 ctor(spath);
00057 }
00058
00059
00060 void
00061 PathParser::ctor(const std::string &path)
00062 {
00063 __abs_path = false;
00064
00065 char *p = strdup(path.c_str());
00066 char *saveptr;
00067 char *r = strtok_r(p, "/", &saveptr);
00068
00069 if ( ! r ) {
00070
00071 push_back(p);
00072 } else {
00073 __abs_path = ( r != p );
00074
00075 while ( r ) {
00076 if ( strlen(r) > 0 ) {
00077 push_back(r);
00078 }
00079 r = strtok_r(NULL, "/", &saveptr);
00080 }
00081 }
00082
00083 free(p);
00084 }
00085
00086
00087
00088 void
00089 PathParser::print_debug()
00090 {
00091 for (size_type i = 0; i < size(); ++i) {
00092 printf("Path element: %s\n", ((*this)[i]).c_str());
00093 }
00094 }
00095
00096
00097
00098
00099
00100 std::string
00101 PathParser::path_as_string()
00102 {
00103 string rv = __abs_path ? "/" : "";
00104
00105 size_type sz = size();
00106
00107 if ( sz > 0 ) {
00108 rv += (*this)[0];
00109 }
00110
00111 for (size_type i = 1; i < sz; ++i) {
00112 rv += "/" + (*this)[i];
00113 }
00114
00115 return rv;
00116 }
00117
00118
00119
00120
00121
00122 bool
00123 PathParser::is_absolute() const
00124 {
00125 return __abs_path;
00126 }
00127
00128 }