1 /* =========================================================================
2 ** Extended Template and Library
3 ** stringf Procedure 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 preferred 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 preferred 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.size() == 1 && str[0] == ETL_DIRECTORY_SEPARATOR)
156 if(str.end()[-1]==ETL_DIRECTORY_SEPARATOR)
161 for(;iter!=str.begin();iter--)
162 if(*iter==ETL_DIRECTORY_SEPARATOR)
165 if (*iter==ETL_DIRECTORY_SEPARATOR)
168 if(str.end()[-1]==ETL_DIRECTORY_SEPARATOR)
169 return std::string(iter,str.end()-1);
171 return std::string(iter,str.end());
175 dirname(const std::string &str)
177 std::string::const_iterator iter;
179 if(str.size() == 1 && str[0] == ETL_DIRECTORY_SEPARATOR)
182 if(str.end()[-1]==ETL_DIRECTORY_SEPARATOR)
187 for(;iter!=str.begin();iter--)
188 if(*iter==ETL_DIRECTORY_SEPARATOR)
191 if(iter==str.begin())
192 if (*iter==ETL_DIRECTORY_SEPARATOR)
197 return std::string(str.begin(),iter);
201 is_absolute_path(const std::string &path)
204 if(path.size()>=3 && path[1]==':' && (path[2]=='\\' || path[2]=='/'))
207 if(!path.empty() && path[0]==ETL_DIRECTORY_SEPARATOR)
213 unix_to_local_path(const std::string &path)
216 std::string::const_iterator iter;
217 for(iter=path.begin();iter!=path.end();iter++)
221 ret+=ETL_DIRECTORY_SEPARATOR;
234 current_working_directory()
237 std::string ret(getcwd(dir,sizeof(dir)));
242 get_root_from_path(std::string path)
245 std::string::const_iterator iter;
247 for(iter=path.begin();iter!=path.end();++iter)
249 if(*iter==ETL_DIRECTORY_SEPARATOR)
253 //if(iter!=path.end())
254 ret+=ETL_DIRECTORY_SEPARATOR;
259 remove_root_from_path(std::string path)
263 if(path[0]==ETL_DIRECTORY_SEPARATOR)
265 path.erase(path.begin());
268 path.erase(path.begin());
274 cleanup_path(std::string path)
278 while(basename(path)=="."&&path.size()!=1)path=dirname(path);
282 std::string dir(get_root_from_path(path));
283 if((dir=="../" || dir=="..\\") && ret.size())
286 if (*(ret.end()-1)!=ETL_DIRECTORY_SEPARATOR)
287 ret+=ETL_DIRECTORY_SEPARATOR;
289 else if((dir!="./" && dir!=".\\") && dir!=".")
291 path=remove_root_from_path(path);
293 if (ret.size()==0)ret+='.';
295 // Remove any trailing directory separators
296 if(ret.size() && ret[ret.size()-1]==ETL_DIRECTORY_SEPARATOR)
297 ret.erase(ret.begin()+ret.size()-1);
302 absolute_path(std::string path)
304 std::string ret(current_working_directory());
307 return cleanup_path(ret);
308 if(is_absolute_path(path))
309 return cleanup_path(path);
310 return cleanup_path(ret+ETL_DIRECTORY_SEPARATOR+path);
314 relative_path(std::string curr_path,std::string dest_path)
316 // If dest_path is already a relative path,
317 // then there is no need to do anything.
318 if(!is_absolute_path(dest_path))
319 dest_path=absolute_path(dest_path);
321 dest_path=cleanup_path(dest_path);
323 if(!is_absolute_path(curr_path))
324 curr_path=absolute_path(curr_path);
326 curr_path=cleanup_path(curr_path);
329 // If we are on windows and the dest path is on a different drive,
330 // then there is no way to make a relative path to it.
331 if(dest_path.size()>=3 && dest_path[1]==':' && dest_path[0]!=curr_path[0])
335 if(curr_path==dirname(dest_path))
336 return basename(dest_path);
338 while(!dest_path.empty() && !curr_path.empty() && get_root_from_path(dest_path)==get_root_from_path(curr_path))
340 dest_path=remove_root_from_path(dest_path);
341 curr_path=remove_root_from_path(curr_path);
344 while(!curr_path.empty())
346 dest_path=std::string("..")+ETL_DIRECTORY_SEPARATOR+dest_path;
347 curr_path=remove_root_from_path(curr_path);
355 /* === E N D =============================================================== */