00001 #include "PathParser.h"
00002 #include <vector>
00003
00004 Anaphe::AIDA_Tree_native::PathParser::PathParser()
00005 {}
00006
00007
00008 Anaphe::AIDA_Tree_native::PathParser::~PathParser()
00009 {}
00010
00011
00012 std::list< std::string >
00013 Anaphe::AIDA_Tree_native::PathParser::formNames( const std::string& path ) const
00014 {
00015 std::vector<std::string> output;
00016 std::string currentString;
00017 for ( unsigned int iChar = 0; iChar < path.size(); ++iChar ) {
00018 const char& c = path[iChar];
00019 if ( c != '/' ) {
00020 currentString += c;
00021 }
00022 else {
00023 if ( currentString.size() > 0 ) {
00024 output.push_back( currentString );
00025 currentString = "";
00026 }
00027 }
00028 }
00029 if ( currentString.size() > 0 ) output.push_back( currentString );
00030
00031
00032 std::list<std::string> loutput;
00033 if ( output.empty() ) return loutput;
00034 loutput.push_back( output[0] );
00035 for( unsigned int i = 1; i < output.size(); ++i ) {
00036 const std::string& name = output[i];
00037 if ( name == "." ) continue;
00038 if ( name == ".." ) {
00039 if ( loutput.empty() ) return loutput;
00040 else loutput.pop_back();
00041 }
00042 else loutput.push_back( name );
00043 }
00044
00045 return loutput;
00046 }
00047
00048
00049 std::string
00050 Anaphe::AIDA_Tree_native::PathParser::formPath( const std::list< std::string >& names ) const
00051 {
00052 std::string output = "";
00053 for ( std::list< std::string >::const_iterator iName = names.begin(); iName != names.end(); ++iName ) {
00054 if ( output.size() > 0 ) output += "/";
00055 output += *iName;
00056 }
00057 return output;
00058 }
00059
00060 std::string
00061 Anaphe::AIDA_Tree_native::PathParser::parent( const std::string& path ) const
00062 {
00063 std::list< std::string > names = formNames( path );
00064 if ( names.size() > 0 ) names.pop_back();
00065 std::string par = formPath( names );
00066 if ( path.size() > 0 && path[0] == '/' ) par = "/" + par;
00067 return par;
00068 }