pathparser.cpp

00001 
00002 /***************************************************************************
00003  *  pathparser.cpp - Header for path parser
00004  *
00005  *  Created: Mon Jul 07 13:25:10 2008
00006  *  Copyright  2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
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 /** @class PathParser <utils/system/pathparser.h>
00035  * Path parser.
00036  * Parses a given (Unix) file system path and provides the elements and vector
00037  * elements.
00038  * @author Tim Niemueller
00039  */
00040 
00041 /** Constructor (C++ string).
00042  * @param path path to parse
00043  */
00044 PathParser::PathParser(std::string &path)
00045 {
00046   ctor(path);
00047 }
00048 
00049 
00050 /** Constructor (C string).
00051  * @param path path to parse
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     // single string, no slash, does not end with slash
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 /** Debug print to stdout. */
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 /** Get path as string.
00097  * Joins the path elements to one path again.
00098  * @return path as string
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 /** Check if path is absolute.
00120  * @return true if path is absolute, false otherwise
00121  */
00122 bool
00123 PathParser::is_absolute() const
00124 {
00125   return __abs_path;
00126 }
00127 
00128 } // end namespace fawkes

Generated on 1 Mar 2011 for Fawkes API by  doxygen 1.6.1