1 /* =========================================================================
2 ** Extended Template and Library
3 ** stringf Procedure Implementation
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 ** Copyright (c) 2007 Chris Moore
9 ** This package is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU General Public License as
11 ** published by the Free Software Foundation; either version 2 of
12 ** the License, or (at your option) any later version.
14 ** This package is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** General Public License for more details.
19 ** === N O T E S ===========================================================
21 ** This is an internal header file, included by other ETL headers.
22 ** You should not attempt to use it directly.
24 ** ========================================================================= */
26 /* === S T A R T =========================================================== */
28 #ifndef __ETL__STRINGF_H
29 #define __ETL__STRINGF_H
31 /* === H E A D E R S ======================================================= */
42 /* === M A C R O S ========================================================= */
44 #ifndef ETL_STRPRINTF_MAX_LENGTH
45 #define ETL_STRPRINTF_MAX_LENGTH (800)
49 #define POPEN_BINARY_READ_TYPE "rb"
50 #define POPEN_BINARY_WRITE_TYPE "wb"
52 #define POPEN_BINARY_READ_TYPE "r"
53 #define POPEN_BINARY_WRITE_TYPE "w"
56 /* === T Y P E D E F S ===================================================== */
60 #if defined(__APPLE__) || defined(__CYGWIN__) || defined(_WIN32)
63 #define ETL_NO_THROW throw()
66 // Prefer prototypes from glibc headers, since defining them ourselves
67 // works around glibc security mechanisms
69 #ifdef HAVE_VASPRINTF // This is the preferred method
71 extern int vasprintf(char **,const char *,va_list)ETL_NO_THROW;
75 # ifdef HAVE_VSNPRINTF // This is the secondary method
77 extern int vsnprintf(char *,size_t,const char*,va_list)ETL_NO_THROW;
85 extern int vsscanf(const char *,const char *,va_list)ETL_NO_THROW;
88 #define ETL_NO_VSTRSCANF
91 extern int sscanf(const char *buf, const char *format, ...)ETL_NO_THROW;
100 /* === C L A S S E S & S T R U C T S ======================================= */
105 vstrprintf(const char *format, va_list args)
107 #ifdef HAVE_VASPRINTF // This is the preferred method (and safest)
110 int i=vasprintf(&buffer,format,args);
118 #ifdef HAVE_VSNPRINTF // This is the secondary method (Safe, but bulky)
119 #warning etl::vstrprintf() has a maximum size of ETL_STRPRINTF_MAX_LENGTH in this configuration.
120 #ifdef ETL_THREAD_SAFE
121 char buffer[ETL_STRPRINTF_MAX_LENGTH];
123 static char buffer[ETL_STRPRINTF_MAX_LENGTH];
125 vsnprintf(buffer,sizeof(buffer),format,args);
127 #else // This is the worst method (UNSAFE, but "works")
128 #warning Potential for Buffer-overflow bug using vsprintf
129 #define ETL_UNSAFE_STRPRINTF (true)
130 // Here, we are doubling the size of the buffer to make this case
131 // slightly more safe.
132 #ifdef ETL_THREAD_SAFE
133 char buffer[ETL_STRPRINTF_MAX_LENGTH*2];
135 static char buffer[ETL_STRPRINTF_MAX_LENGTH*2];
137 vsprintf(buffer,format,args);
144 strprintf(const char *format, ...)
147 va_start(args,format);
148 return vstrprintf(format,args);
151 #ifndef ETL_NO_VSTRSCANF
153 vstrscanf(const std::string &data, const char*format, va_list args)
155 return vsscanf(data.c_str(),format,args);
159 strscanf(const std::string &data, const char*format, ...)
162 va_start(args,format);
163 return vstrscanf(data, format,args);
167 #if defined (HAVE_SSCANF) && defined (__GNUC__)
168 #define strscanf(data,format,...) sscanf(data.c_str(),format,__VA_ARGS__)
173 #define stratof(X) (atof((X).c_str()))
174 #define stratoi(X) (atoi((X).c_str()))
177 basename(const std::string &str)
179 std::string::const_iterator iter;
181 if(str.size() == 1 && str[0] == ETL_DIRECTORY_SEPARATOR)
184 if(str.end()[-1]==ETL_DIRECTORY_SEPARATOR)
189 for(;iter!=str.begin();iter--)
190 if(*iter==ETL_DIRECTORY_SEPARATOR)
193 if (*iter==ETL_DIRECTORY_SEPARATOR)
196 if(str.end()[-1]==ETL_DIRECTORY_SEPARATOR)
197 return std::string(iter,str.end()-1);
199 return std::string(iter,str.end());
203 dirname(const std::string &str)
205 std::string::const_iterator iter;
207 if(str.size() == 1 && str[0] == ETL_DIRECTORY_SEPARATOR)
210 if(str.end()[-1]==ETL_DIRECTORY_SEPARATOR)
215 for(;iter!=str.begin();iter--)
216 if(*iter==ETL_DIRECTORY_SEPARATOR)
219 if(iter==str.begin())
221 if (*iter==ETL_DIRECTORY_SEPARATOR)
227 return std::string(str.begin(),iter);
230 // filename_extension("/f.e/d.c") => ".c"
232 filename_extension(const std::string &str)
234 std::string base = basename(str);
235 std::string::size_type pos = base.find_last_of('.');
236 if (pos == std::string::npos) return std::string();
237 return base.substr(pos);
240 // filename_sans_extension("/f.e/d.c") => "/f.e/d"
242 filename_sans_extension(const std::string &str)
244 std::string base = basename(str);
245 std::string::size_type pos = base.find_last_of('.');
246 if (pos == std::string::npos) return str;
247 std::string dir = dirname(str);
248 if (dir == ".") return base.substr(0,pos);
249 return dir + ETL_DIRECTORY_SEPARATOR + base.substr(0,pos);
253 is_absolute_path(const std::string &path)
256 if(path.size()>=3 && path[1]==':' && (path[2]=='\\' || path[2]=='/'))
259 if(!path.empty() && path[0]==ETL_DIRECTORY_SEPARATOR)
265 unix_to_local_path(const std::string &path)
268 std::string::const_iterator iter;
269 for(iter=path.begin();iter!=path.end();iter++)
273 ret+=ETL_DIRECTORY_SEPARATOR;
286 current_working_directory()
289 std::string ret(getcwd(dir,sizeof(dir)));
294 get_root_from_path(std::string path)
297 std::string::const_iterator iter;
299 for(iter=path.begin();iter!=path.end();++iter)
301 if(*iter==ETL_DIRECTORY_SEPARATOR)
305 //if(iter!=path.end())
306 ret+=ETL_DIRECTORY_SEPARATOR;
311 remove_root_from_path(std::string path)
315 if(path[0]==ETL_DIRECTORY_SEPARATOR)
317 path.erase(path.begin());
320 path.erase(path.begin());
326 cleanup_path(std::string path)
330 while(basename(path)=="."&&path.size()!=1)path=dirname(path);
334 std::string dir(get_root_from_path(path));
335 if((dir=="../" || dir=="..\\") && ret.size())
338 if (*(ret.end()-1)!=ETL_DIRECTORY_SEPARATOR)
339 ret+=ETL_DIRECTORY_SEPARATOR;
341 else if((dir!="./" && dir!=".\\") && dir!=".")
343 path=remove_root_from_path(path);
345 if (ret.size()==0)ret+='.';
347 // Remove any trailing directory separators
348 if(ret.size() && ret[ret.size()-1]==ETL_DIRECTORY_SEPARATOR)
349 ret.erase(ret.begin()+ret.size()-1);
354 absolute_path(std::string path)
356 std::string ret(current_working_directory());
359 return cleanup_path(ret);
360 if(is_absolute_path(path))
361 return cleanup_path(path);
362 return cleanup_path(ret+ETL_DIRECTORY_SEPARATOR+path);
366 relative_path(std::string curr_path,std::string dest_path)
368 // If dest_path is already a relative path,
369 // then there is no need to do anything.
370 if(!is_absolute_path(dest_path))
371 dest_path=absolute_path(dest_path);
373 dest_path=cleanup_path(dest_path);
375 if(!is_absolute_path(curr_path))
376 curr_path=absolute_path(curr_path);
378 curr_path=cleanup_path(curr_path);
381 // If we are on windows and the dest path is on a different drive,
382 // then there is no way to make a relative path to it.
383 if(dest_path.size()>=3 && dest_path[1]==':' && dest_path[0]!=curr_path[0])
387 if(curr_path==dirname(dest_path))
388 return basename(dest_path);
390 while(!dest_path.empty() && !curr_path.empty() && get_root_from_path(dest_path)==get_root_from_path(curr_path))
392 dest_path=remove_root_from_path(dest_path);
393 curr_path=remove_root_from_path(curr_path);
396 while(!curr_path.empty())
398 dest_path=std::string("..")+ETL_DIRECTORY_SEPARATOR+dest_path;
399 curr_path=remove_root_from_path(curr_path);
407 /* === E N D =============================================================== */