X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=ETL%2Ftrunk%2FETL%2F_stringf.h;h=49e970511eae8fe9e14e7908f737d76c46936a91;hb=2fd0cb406ed7d422c64c6293ac29336605fc3b29;hp=21a7190bba9fb8f98ef7dad97e04af3a5336e897;hpb=b3016b249333ac0ab0008d8c6c4d9029b2ff30c9;p=synfig.git diff --git a/ETL/trunk/ETL/_stringf.h b/ETL/trunk/ETL/_stringf.h index 21a7190..49e9705 100644 --- a/ETL/trunk/ETL/_stringf.h +++ b/ETL/trunk/ETL/_stringf.h @@ -1,7 +1,7 @@ /* ========================================================================= ** Extended Template and Library ** stringf Prodecure Implementation -** $Id: _stringf.h,v 1.1.1.1 2005/01/04 01:31:48 darco Exp $ +** $Id$ ** ** Copyright (c) 2002 Robert B. Quattlebaum Jr. ** @@ -31,6 +31,7 @@ #include #include +#include /* === M A C R O S ========================================================= */ @@ -53,11 +54,7 @@ _ETL_BEGIN_CDECLS #else # ifdef HAVE_VSNPRINTF // This is the secondary method -# if defined(__CYGWIN__) || defined(_WIN32) -extern int vsnprintf(char *,unsigned int,const char*,va_list)ETL_NO_THROW; -# else -extern int vsnprintf(char *,int,const char*,va_list)ETL_NO_THROW; -# endif + extern int vsnprintf(char *,size_t,const char*,va_list)ETL_NO_THROW; # endif #endif @@ -153,6 +150,9 @@ basename(const std::string &str) { std::string::const_iterator iter; + if(str.size() == 1 && str[0] == ETL_DIRECTORY_SEPERATOR) + return str; + if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR) iter=str.end()-2; else @@ -162,10 +162,8 @@ basename(const std::string &str) if(*iter==ETL_DIRECTORY_SEPERATOR) break; - if(iter==str.begin()) - return str; - - iter++; + if (*iter==ETL_DIRECTORY_SEPERATOR) + iter++; if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR) return std::string(iter,str.end()-1); @@ -178,6 +176,9 @@ dirname(const std::string &str) { std::string::const_iterator iter; + if(str.size() == 1 && str[0] == ETL_DIRECTORY_SEPERATOR) + return str; + if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR) iter=str.end()-2; else @@ -188,7 +189,10 @@ dirname(const std::string &str) break; if(iter==str.begin()) - return "."; + if (*iter==ETL_DIRECTORY_SEPERATOR) + return "/"; + else + return "."; return std::string(str.begin(),iter); } @@ -197,14 +201,12 @@ inline bool is_absolute_path(const std::string &path) { #ifdef WIN32 - if(path.size()>=3 && path[1]==':' && path[2]=='\\') + if(path.size()>=3 && path[1]==':' && (path[2]=='\\' || path[2]=='/')) return true; - return false; -#else +#endif if(!path.empty() && path[0]==ETL_DIRECTORY_SEPERATOR) return true; return false; -#endif } inline std::string @@ -241,14 +243,14 @@ get_root_from_path(std::string path) { std::string ret; std::string::const_iterator iter; - + for(iter=path.begin();iter!=path.end();++iter) { if(*iter==ETL_DIRECTORY_SEPERATOR) break; ret+=*iter; } - if(iter!=path.end()) + //if(iter!=path.end()) ret+=ETL_DIRECTORY_SEPERATOR; return ret; } @@ -273,26 +275,26 @@ cleanup_path(std::string path) { std::string ret; - while(basename(path)==".")path=dirname(path); - + while(basename(path)=="."&&path.size()!=1)path=dirname(path); + while(!path.empty()) { std::string dir(get_root_from_path(path)); - if(dir=="../" && ret.size()) + if((dir=="../" || dir=="..\\") && ret.size()) { - ret=dirname(ret)+ETL_DIRECTORY_SEPERATOR; + ret=dirname(ret); + if (*(ret.end()-1)!=ETL_DIRECTORY_SEPERATOR) + ret+=ETL_DIRECTORY_SEPERATOR; } - else if(dir!="./" && dir!=".") - { + else if((dir!="./" && dir!=".\\") && dir!=".") ret+=dir; - } path=remove_root_from_path(path); } - + if (ret.size()==0)ret+='.'; + + // Remove any trailing directory seperators if(ret.size() && ret[ret.size()-1]==ETL_DIRECTORY_SEPERATOR) - { ret.erase(ret.begin()+ret.size()-1); - } return ret; } @@ -300,30 +302,39 @@ inline std::string absolute_path(std::string path) { std::string ret(current_working_directory()); - + if(path.empty()) return cleanup_path(ret); if(is_absolute_path(path)) return cleanup_path(path); - // TODO: This needs to be written return cleanup_path(ret+ETL_DIRECTORY_SEPERATOR+path); } inline std::string relative_path(std::string curr_path,std::string dest_path) -{ +{ // If dest_path is already a relative path, // then there is no need to do anything. if(!is_absolute_path(dest_path)) dest_path=absolute_path(dest_path); else dest_path=cleanup_path(dest_path); - + if(!is_absolute_path(curr_path)) curr_path=absolute_path(curr_path); else curr_path=cleanup_path(curr_path); - + +#ifdef WIN32 + // If we are on windows and the dest path is on a different drive, + // then there is no way to make a relative path to it. + if(dest_path.size()>=3 && dest_path[1]==':' && dest_path[0]!=curr_path[0]) + return dest_path; +#endif + + if(curr_path==dirname(dest_path)) + return basename(dest_path); + while(!dest_path.empty() && !curr_path.empty() && get_root_from_path(dest_path)==get_root_from_path(curr_path)) { dest_path=remove_root_from_path(dest_path); @@ -332,10 +343,10 @@ relative_path(std::string curr_path,std::string dest_path) while(!curr_path.empty()) { - dest_path="../"+dest_path; + dest_path=std::string("..")+ETL_DIRECTORY_SEPERATOR+dest_path; curr_path=remove_root_from_path(curr_path); } - + return dest_path; }