00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "path.h"
00022
00023 #include <boost/tokenizer.hpp>
00024
00025 using namespace std;
00026 using namespace salt;
00027
00028 Path::Path(const std::string &path)
00029 {
00030 Set(path);
00031 }
00032
00033 void Path::Set(const std::string &path)
00034 {
00035 if (path[0] == '/' || path[0] == '\\')
00036 {
00037 mIsAbsolute = true;
00038 }
00039 else
00040 {
00041 mIsAbsolute = false;
00042 }
00043
00044 Tokenize(path);
00045 }
00046
00047 std::string Path::GetCleanPath(const std::string &sep) const
00048 {
00049 string path;
00050
00051 if (mIsAbsolute)
00052 {
00053 path += sep;
00054 }
00055
00056 unsigned int count = 0;
00057 for (TStringList::const_iterator i = mPathComponents.begin(); i != mPathComponents.end(); ++i)
00058 {
00059 path += (*i);
00060 ++count;
00061 if ( count != mPathComponents.size() )
00062 {
00063 path += sep;
00064 }
00065 }
00066
00067 return path;
00068 }
00069
00070 bool Path::IsAbsolute() const
00071 {
00072 return mIsAbsolute;
00073 }
00074
00075 bool Path::IsEmpty() const
00076 {
00077 return mPathComponents.empty();
00078 }
00079
00080 const std::string& Path::Front() const
00081 {
00082 return mPathComponents.front();
00083 }
00084
00085 void Path::PopFront()
00086 {
00087 mIsAbsolute = false;
00088 mPathComponents.pop_front();
00089 }
00090
00091 const std::string& Path::Back() const
00092 {
00093 return mPathComponents.back();
00094 }
00095
00096 void Path::PopBack()
00097 {
00098 mPathComponents.pop_back();
00099 }
00100
00101 void Path::Tokenize(const std::string &path)
00102 {
00103 typedef boost::tokenizer<boost::char_separator<char> > TTokenizer;
00104 boost::char_separator<char> sep("/\\");
00105 TTokenizer tokens(path, sep);
00106
00107 for (TTokenizer::iterator i = tokens.begin(); i != tokens.end(); ++i)
00108 {
00109 mPathComponents.push_back(*i);
00110 }
00111 }