Stop a bunch of compile warnings about not used returned value from vasprintf functio...
[synfig.git] / ETL / trunk / ETL / _stringf.h
index 61621ac..f91cfca 100644 (file)
@@ -1,9 +1,10 @@
 /* =========================================================================
 ** Extended Template and Library
-** stringf Prodecure Implementation
-** $Id: _stringf.h,v 1.1.1.1 2005/01/04 01:31:48 darco Exp $
+** stringf Procedure Implementation
+** $Id$
 **
 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
+** Copyright (c) 2007 Chris Moore
 **
 ** This package is free software; you can redistribute it and/or
 ** modify it under the terms of the GNU General Public License as
@@ -31,6 +32,7 @@
 
 #include <string>
 #include <cstdarg>
+#include <cstdlib>
 
 /* === M A C R O S ========================================================= */
 
 #define ETL_STRPRINTF_MAX_LENGTH       (800)
 #endif
 
+#ifdef WIN32
+#define POPEN_BINARY_READ_TYPE "rb"
+#define POPEN_BINARY_WRITE_TYPE "wb"
+#else
+#define POPEN_BINARY_READ_TYPE "r"
+#define POPEN_BINARY_WRITE_TYPE "w"
+#endif
+
 /* === T Y P E D E F S ===================================================== */
 
 _ETL_BEGIN_CDECLS
@@ -48,16 +58,12 @@ _ETL_BEGIN_CDECLS
 #define ETL_NO_THROW throw()
 #endif
 
-#ifdef HAVE_VASPRINTF  // This is the prefered method
+#ifdef HAVE_VASPRINTF  // This is the preferred method
  extern int vasprintf(char **,const char *,va_list)ETL_NO_THROW;
 #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
@@ -82,12 +88,15 @@ _ETL_BEGIN_NAMESPACE
 inline std::string
 vstrprintf(const char *format, va_list args)
 {
-#ifdef HAVE_VASPRINTF  // This is the prefered method (and safest)
+#ifdef HAVE_VASPRINTF  // This is the preferred method (and safest)
        char *buffer;
        std::string ret;
-       vasprintf(&buffer,format,args);
-       ret=buffer;
-       free(buffer);
+       int i=vasprintf(&buffer,format,args);
+       if (i>-1) 
+       {
+               ret=buffer;
+               free(buffer);
+       }
        return ret;
 #else
 #ifdef HAVE_VSNPRINTF  // This is the secondary method (Safe, but bulky)
@@ -153,21 +162,22 @@ basename(const std::string &str)
 {
        std::string::const_iterator iter;
 
-       if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR)
+       if(str.size() == 1 && str[0] == ETL_DIRECTORY_SEPARATOR)
+               return str;
+
+       if(str.end()[-1]==ETL_DIRECTORY_SEPARATOR)
                iter=str.end()-2;
        else
                iter=str.end()-1;
 
        for(;iter!=str.begin();iter--)
-               if(*iter==ETL_DIRECTORY_SEPERATOR)
+               if(*iter==ETL_DIRECTORY_SEPARATOR)
                        break;
 
-       if(iter==str.begin())
-               return str;
-
-       iter++;
+       if (*iter==ETL_DIRECTORY_SEPARATOR)
+               iter++;
 
-       if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR)
+       if(str.end()[-1]==ETL_DIRECTORY_SEPARATOR)
                return std::string(iter,str.end()-1);
 
        return std::string(iter,str.end());
@@ -178,33 +188,61 @@ dirname(const std::string &str)
 {
        std::string::const_iterator iter;
 
-       if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR)
+       if(str.size() == 1 && str[0] == ETL_DIRECTORY_SEPARATOR)
+               return str;
+
+       if(str.end()[-1]==ETL_DIRECTORY_SEPARATOR)
                iter=str.end()-2;
        else
                iter=str.end()-1;
 
        for(;iter!=str.begin();iter--)
-               if(*iter==ETL_DIRECTORY_SEPERATOR)
+               if(*iter==ETL_DIRECTORY_SEPARATOR)
                        break;
 
        if(iter==str.begin())
-               return ".";
+       {
+          if (*iter==ETL_DIRECTORY_SEPARATOR)
+                  return "/";
+          else
+                  return ".";
+       }
 
        return std::string(str.begin(),iter);
 }
 
+// filename_extension("/f.e/d.c") => ".c"
+inline std::string
+filename_extension(const std::string &str)
+{
+       std::string base = basename(str);
+       std::string::size_type pos = base.find_last_of('.');
+       if (pos == std::string::npos) return std::string();
+       return base.substr(pos);
+}
+
+// filename_sans_extension("/f.e/d.c") => "/f.e/d"
+inline std::string
+filename_sans_extension(const std::string &str)
+{
+       std::string base = basename(str);
+       std::string::size_type pos = base.find_last_of('.');
+       if (pos == std::string::npos) return str;
+       std::string dir = dirname(str);
+       if (dir == ".") return base.substr(0,pos);
+       return dir + ETL_DIRECTORY_SEPARATOR + base.substr(0,pos);
+}
+
 inline bool
 is_absolute_path(const std::string &path)
 {
 #ifdef WIN32
        if(path.size()>=3 && path[1]==':' && (path[2]=='\\' || path[2]=='/'))
                return true;
-       return false;
-#else
-       if(!path.empty() && path[0]==ETL_DIRECTORY_SEPERATOR)
+#endif
+       if(!path.empty() && path[0]==ETL_DIRECTORY_SEPARATOR)
                return true;
        return false;
-#endif 
 }
 
 inline std::string
@@ -216,7 +254,7 @@ unix_to_local_path(const std::string &path)
                switch(*iter)
                {
                case '/':
-                       ret+=ETL_DIRECTORY_SEPERATOR;
+                       ret+=ETL_DIRECTORY_SEPARATOR;
                        break;
                case '~':
                        ret+='~';
@@ -241,15 +279,15 @@ 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)
+               if(*iter==ETL_DIRECTORY_SEPARATOR)
                        break;
                ret+=*iter;
        }
        //if(iter!=path.end())
-               ret+=ETL_DIRECTORY_SEPERATOR;
+               ret+=ETL_DIRECTORY_SEPARATOR;
        return ret;
 }
 
@@ -258,7 +296,7 @@ remove_root_from_path(std::string path)
 {
        while(!path.empty())
        {
-               if(path[0]==ETL_DIRECTORY_SEPERATOR)
+               if(path[0]==ETL_DIRECTORY_SEPARATOR)
                {
                        path.erase(path.begin());
                        return path;
@@ -273,27 +311,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_SEPARATOR)
+                               ret+=ETL_DIRECTORY_SEPARATOR;
                }
-               else if(dir!="./" && dir!=".")
-               {
+               else if((dir!="./" && dir!=".\\") && dir!=".")
                        ret+=dir;
-               }
                path=remove_root_from_path(path);
        }
-       
-       // Remove any trailing directory seperators
-       if(ret.size() && ret[ret.size()-1]==ETL_DIRECTORY_SEPERATOR)
-       {
+       if (ret.size()==0)ret+='.';
+
+       // Remove any trailing directory separators
+       if(ret.size() && ret[ret.size()-1]==ETL_DIRECTORY_SEPARATOR)
                ret.erase(ret.begin()+ret.size()-1);
-       }
        return ret;
 }
 
@@ -301,25 +338,24 @@ 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);
+       return cleanup_path(ret+ETL_DIRECTORY_SEPARATOR+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
@@ -329,16 +365,12 @@ relative_path(std::string curr_path,std::string dest_path)
        // 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);
@@ -347,10 +379,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_SEPARATOR+dest_path;
                curr_path=remove_root_from_path(curr_path);
        }
-       
+
        return dest_path;
 }