DefinesString.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <string>
00022 #include <stdlib.h>
00023 #include <stdarg.h>
00024 #include <common/Defines.h>
00025 #include <SDL/SDL.h>
00026 #include <SDL/SDL_thread.h>
00027 
00028 void S3D::trim(std::string &value)
00029 {
00030         int start = value.find_first_not_of(" \t\n");
00031         int end = value.find_last_not_of(" \t\n");
00032         if (start == std::string::npos) value = "";
00033         else if (end == std::string::npos) value = "";
00034         else value = std::string(value, start, end-start+1);
00035 }
00036 
00037 char *S3D::stristr(const char *x, const char *y)
00038 {
00039         std::string newX(x);
00040         std::string newY(y);
00041         _strlwr((char *) newX.c_str());
00042         _strlwr((char *) newY.c_str());
00043 
00044         char *result = (char *) strstr(newX.c_str(), newY.c_str());
00045         if (!result) return 0;
00046 
00047         return (char *)(x + (result - newX.c_str()));
00048 }
00049 
00050 #ifndef va_copy
00051 # define va_copy(d, s)          (d) = (s)
00052 #endif
00053 
00054 std::string S3D::formatStringList(const char *format, va_list ap)
00055 {
00056         int size = 256;
00057         char *p = new char[256];
00058         va_list ap_copy;
00059 
00060         while (1) 
00061         {
00062                 /* Try to print in the allocated space. */
00063                 va_copy(ap_copy, ap);
00064                 int n = vsnprintf (p, size, format, ap_copy);
00065                 va_end(ap_copy);
00066 
00067                 /* If that worked, return the string. */
00068                 if (n > -1 && n < size) break;
00069 
00070                 /* Check size is within limits */
00071                 if (size > 10 * 1024) break;
00072 
00073                 /* Else try again with more space. */
00074                 if (n > -1)    /* glibc 2.1 */
00075                         size = n+1; /* precisely what is needed */
00076                 else           /* glibc 2.0 */
00077                         size *= 2;  /* twice the old size */
00078 
00079                 /* Allocate more space */
00080                 delete [] p;
00081                 p = new char[size];
00082         }
00083 
00084         std::string result(p);
00085         delete [] p;
00086 
00087         return result;
00088 }
00089 
00090 std::string S3D::formatStringBuffer(const char *format, ...)
00091 {
00092         va_list ap; 
00093         va_start(ap, format); 
00094         std::string result = S3D::formatStringList(format, ap);
00095         va_end(ap); 
00096 
00097         return result;
00098 }
00099 
00100 

Generated on Mon Feb 16 15:14:48 2009 for Scorched3D by  doxygen 1.5.3