1 /* =========================================================================
2 ** Extended Template and Library
3 ** stringf Prodecure Implementation
4 ** $Id: _stringf.h,v 1.1.1.1 2005/01/04 01:31:48 darco Exp $
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 ======================================================= */
35 /* === M A C R O S ========================================================= */
37 #ifndef ETL_STRPRINTF_MAX_LENGTH
38 #define ETL_STRPRINTF_MAX_LENGTH (800)
41 /* === T Y P E D E F S ===================================================== */
45 #if defined(__APPLE__) || defined(__CYGWIN__) || defined(_WIN32)
48 #define ETL_NO_THROW throw()
51 #ifdef HAVE_VASPRINTF // This is the prefered method
52 extern int vasprintf(char **,const char *,va_list)ETL_NO_THROW;
55 # ifdef HAVE_VSNPRINTF // This is the secondary method
56 extern int vsnprintf(char *,size_t,const char*,va_list)ETL_NO_THROW;
62 extern int vsscanf(const char *,const char *,va_list)ETL_NO_THROW;
64 #define ETL_NO_VSTRSCANF
66 extern int sscanf(const char *buf, const char *format, ...)ETL_NO_THROW;
74 /* === C L A S S E S & S T R U C T S ======================================= */
79 vstrprintf(const char *format, va_list args)
81 #ifdef HAVE_VASPRINTF // This is the prefered method (and safest)
84 vasprintf(&buffer,format,args);
89 #ifdef HAVE_VSNPRINTF // This is the secondary method (Safe, but bulky)
90 #warning etl::vstrprintf() has a maximum size of ETL_STRPRINTF_MAX_LENGTH in this configuration.
91 #ifdef ETL_THREAD_SAFE
92 char buffer[ETL_STRPRINTF_MAX_LENGTH];
94 static char buffer[ETL_STRPRINTF_MAX_LENGTH];
96 vsnprintf(buffer,sizeof(buffer),format,args);
98 #else // This is the worst method (UNSAFE, but "works")
99 #warning Potential for Buffer-overflow bug using vsprintf
100 #define ETL_UNSAFE_STRPRINTF (true)
101 // Here, we are doubling the size of the buffer to make this case
102 // slightly more safe.
103 #ifdef ETL_THREAD_SAFE
104 char buffer[ETL_STRPRINTF_MAX_LENGTH*2];
106 static char buffer[ETL_STRPRINTF_MAX_LENGTH*2];
108 vsprintf(buffer,format,args);
115 strprintf(const char *format, ...)
118 va_start(args,format);
119 return vstrprintf(format,args);
122 #ifndef ETL_NO_VSTRSCANF
124 vstrscanf(const std::string &data, const char*format, va_list args)
126 return vsscanf(data.c_str(),format,args);
130 strscanf(const std::string &data, const char*format, ...)
133 va_start(args,format);
134 return vstrscanf(data, format,args);
138 #if defined (HAVE_SSCANF) && defined (__GNUC__)
139 #define strscanf(data,format,...) sscanf(data.c_str(),format,__VA_ARGS__)
144 #define stratof(X) (atof((X).c_str()))
145 #define stratoi(X) (atoi((X).c_str()))
148 basename(const std::string &str)
150 std::string::const_iterator iter;
152 if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR)
157 for(;iter!=str.begin();iter--)
158 if(*iter==ETL_DIRECTORY_SEPERATOR)
161 if(iter==str.begin())
166 if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR)
167 return std::string(iter,str.end()-1);
169 return std::string(iter,str.end());
173 dirname(const std::string &str)
175 std::string::const_iterator iter;
177 if(str.end()[-1]==ETL_DIRECTORY_SEPERATOR)
182 for(;iter!=str.begin();iter--)
183 if(*iter==ETL_DIRECTORY_SEPERATOR)
186 if(iter==str.begin())
189 return std::string(str.begin(),iter);
193 is_absolute_path(const std::string &path)
196 if(path.size()>=3 && path[1]==':' && (path[2]=='\\' || path[2]=='/'))
199 if(!path.empty() && path[0]==ETL_DIRECTORY_SEPERATOR)
205 unix_to_local_path(const std::string &path)
208 std::string::const_iterator iter;
209 for(iter=path.begin();iter!=path.end();iter++)
213 ret+=ETL_DIRECTORY_SEPERATOR;
226 current_working_directory()
229 std::string ret(getcwd(dir,sizeof(dir)));
234 get_root_from_path(std::string path)
237 std::string::const_iterator iter;
239 for(iter=path.begin();iter!=path.end();++iter)
241 if(*iter==ETL_DIRECTORY_SEPERATOR)
245 //if(iter!=path.end())
246 ret+=ETL_DIRECTORY_SEPERATOR;
251 remove_root_from_path(std::string path)
255 if(path[0]==ETL_DIRECTORY_SEPERATOR)
257 path.erase(path.begin());
260 path.erase(path.begin());
266 cleanup_path(std::string path)
270 while(basename(path)==".")path=dirname(path);
274 std::string dir(get_root_from_path(path));
275 if((dir=="../" || dir=="..\\") && ret.size())
277 ret=dirname(ret)+ETL_DIRECTORY_SEPERATOR;
279 else if((dir!="./" && dir!=".\\") && dir!=".")
283 path=remove_root_from_path(path);
286 // Remove any trailing directory seperators
287 if(ret.size() && ret[ret.size()-1]==ETL_DIRECTORY_SEPERATOR)
289 ret.erase(ret.begin()+ret.size()-1);
295 absolute_path(std::string path)
297 std::string ret(current_working_directory());
300 return cleanup_path(ret);
301 if(is_absolute_path(path))
302 return cleanup_path(path);
303 // TODO: This needs to be written
304 return cleanup_path(ret+ETL_DIRECTORY_SEPERATOR+path);
308 relative_path(std::string curr_path,std::string dest_path)
310 // If dest_path is already a relative path,
311 // then there is no need to do anything.
312 if(!is_absolute_path(dest_path))
313 dest_path=absolute_path(dest_path);
315 dest_path=cleanup_path(dest_path);
317 if(!is_absolute_path(curr_path))
318 curr_path=absolute_path(curr_path);
320 curr_path=cleanup_path(curr_path);
323 // If we are on windows and the dest path is on a different drive,
324 // then there is no way to make a relative path to it.
325 if(dest_path.size()>=3 && dest_path[1]==':' && dest_path[0]!=curr_path[0])
331 if(curr_path==dirname(dest_path))
333 return basename(dest_path);
336 while(!dest_path.empty() && !curr_path.empty() && get_root_from_path(dest_path)==get_root_from_path(curr_path))
338 dest_path=remove_root_from_path(dest_path);
339 curr_path=remove_root_from_path(curr_path);
342 while(!curr_path.empty())
344 dest_path=std::string("..")+ETL_DIRECTORY_SEPERATOR+dest_path;
345 curr_path=remove_root_from_path(curr_path);
353 /* === E N D =============================================================== */