1 /* =========================================================================
2 ** Extended Template and Library
3 ** stringf Prodecure Implementation
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
23 ** ========================================================================= */
25 /* === S T A R T =========================================================== */
27 #ifndef __ETL__STRINGF_H
28 #define __ETL__STRINGF_H
30 /* === H E A D E R S ======================================================= */
36 /* === M A C R O S ========================================================= */
38 #ifndef ETL_STRPRINTF_MAX_LENGTH
39 #define ETL_STRPRINTF_MAX_LENGTH (800)
42 /* === T Y P E D E F S ===================================================== */
46 #if defined(__APPLE__) || defined(__CYGWIN__) || defined(_WIN32)
49 #define ETL_NO_THROW throw()
52 #ifdef HAVE_VASPRINTF // This is the prefered method
53 extern int vasprintf(char **,const char *,va_list)ETL_NO_THROW;
56 # ifdef HAVE_VSNPRINTF // This is the secondary method
57 extern int vsnprintf(char *,size_t,const char*,va_list)ETL_NO_THROW;
63 extern int vsscanf(const char *,const char *,va_list)ETL_NO_THROW;
65 #define ETL_NO_VSTRSCANF
67 extern int sscanf(const char *buf, const char *format, ...)ETL_NO_THROW;
75 /* === C L A S S E S & S T R U C T S ======================================= */
80 vstrprintf(const char *format, va_list args)
82 #ifdef HAVE_VASPRINTF // This is the prefered method (and safest)
85 vasprintf(&buffer,format,args);
90 #ifdef HAVE_VSNPRINTF // This is the secondary method (Safe, but bulky)
91 #warning etl::vstrprintf() has a maximum size of ETL_STRPRINTF_MAX_LENGTH in this configuration.
92 #ifdef ETL_THREAD_SAFE
93 char buffer[ETL_STRPRINTF_MAX_LENGTH];
95 static char buffer[ETL_STRPRINTF_MAX_LENGTH];
97 vsnprintf(buffer,sizeof(buffer),format,args);
99 #else // This is the worst method (UNSAFE, but "works")
100 #warning Potential for Buffer-overflow bug using vsprintf
101 #define ETL_UNSAFE_STRPRINTF (true)
102 // Here, we are doubling the size of the buffer to make this case
103 // slightly more safe.
104 #ifdef ETL_THREAD_SAFE
105 char buffer[ETL_STRPRINTF_MAX_LENGTH*2];
107 static char buffer[ETL_STRPRINTF_MAX_LENGTH*2];
109 vsprintf(buffer,format,args);
116 strprintf(const char *format, ...)
119 va_start(args,format);
120 return vstrprintf(format,args);
123 #ifndef ETL_NO_VSTRSCANF
125 vstrscanf(const std::string &data, const char*format, va_list args)
127 return vsscanf(data.c_str(),format,args);
131 strscanf(const std::string &data, const char*format, ...)
134 va_start(args,format);
135 return vstrscanf(data, format,args);
139 #if defined (HAVE_SSCANF) && defined (__GNUC__)
140 #define strscanf(data,format,...) sscanf(data.c_str(),format,__VA_ARGS__)
145 #define stratof(X) (atof((X).c_str()))
146 #define stratoi(X) (atoi((X).c_str()))
149 basename(const std::string &str)
151 std::string::const_iterator iter;
153 if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR)
158 for(;iter!=str.begin();iter--)
159 if(*iter==ETL_DIRECTORY_SEPERATOR)
162 if(iter==str.begin())
167 if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR)
168 return std::string(iter,str.end()-1);
170 return std::string(iter,str.end());
174 dirname(const std::string &str)
176 std::string::const_iterator iter;
178 if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR)
183 for(;iter!=str.begin();iter--)
184 if(*iter==ETL_DIRECTORY_SEPERATOR)
187 if(iter==str.begin())
190 return std::string(str.begin(),iter);
194 is_absolute_path(const std::string &path)
197 if(path.size()>=3 && path[1]==':' && (path[2]=='\\' || path[2]=='/'))
200 if(!path.empty() && path[0]==ETL_DIRECTORY_SEPERATOR)
206 unix_to_local_path(const std::string &path)
209 std::string::const_iterator iter;
210 for(iter=path.begin();iter!=path.end();iter++)
214 ret+=ETL_DIRECTORY_SEPERATOR;
227 current_working_directory()
230 std::string ret(getcwd(dir,sizeof(dir)));
235 get_root_from_path(std::string path)
238 std::string::const_iterator iter;
240 for(iter=path.begin();iter!=path.end();++iter)
242 if(*iter==ETL_DIRECTORY_SEPERATOR)
246 //if(iter!=path.end())
247 ret+=ETL_DIRECTORY_SEPERATOR;
252 remove_root_from_path(std::string path)
256 if(path[0]==ETL_DIRECTORY_SEPERATOR)
258 path.erase(path.begin());
261 path.erase(path.begin());
267 cleanup_path(std::string path)
271 while(basename(path)==".")path=dirname(path);
275 std::string dir(get_root_from_path(path));
276 if((dir=="../" || dir=="..\\") && ret.size())
278 ret=dirname(ret)+ETL_DIRECTORY_SEPERATOR;
280 else if((dir!="./" && dir!=".\\") && dir!=".")
284 path=remove_root_from_path(path);
287 // Remove any trailing directory seperators
288 if(ret.size() && ret[ret.size()-1]==ETL_DIRECTORY_SEPERATOR)
290 ret.erase(ret.begin()+ret.size()-1);
296 absolute_path(std::string path)
298 std::string ret(current_working_directory());
301 return cleanup_path(ret);
302 if(is_absolute_path(path))
303 return cleanup_path(path);
304 // TODO: This needs to be written
305 return cleanup_path(ret+ETL_DIRECTORY_SEPERATOR+path);
309 relative_path(std::string curr_path,std::string dest_path)
311 // If dest_path is already a relative path,
312 // then there is no need to do anything.
313 if(!is_absolute_path(dest_path))
314 dest_path=absolute_path(dest_path);
316 dest_path=cleanup_path(dest_path);
318 if(!is_absolute_path(curr_path))
319 curr_path=absolute_path(curr_path);
321 curr_path=cleanup_path(curr_path);
324 // If we are on windows and the dest path is on a different drive,
325 // then there is no way to make a relative path to it.
326 if(dest_path.size()>=3 && dest_path[1]==':' && dest_path[0]!=curr_path[0])
332 if(curr_path==dirname(dest_path))
334 return basename(dest_path);
337 while(!dest_path.empty() && !curr_path.empty() && get_root_from_path(dest_path)==get_root_from_path(curr_path))
339 dest_path=remove_root_from_path(dest_path);
340 curr_path=remove_root_from_path(curr_path);
343 while(!curr_path.empty())
345 dest_path=std::string("..")+ETL_DIRECTORY_SEPERATOR+dest_path;
346 curr_path=remove_root_from_path(curr_path);
354 /* === E N D =============================================================== */