Planeshift

strutil.h

Go to the documentation of this file.
00001 /*
00002  * strutil.h by Matze Braun <[email protected]>
00003  *
00004  * Copyright (C) 2001 Atomic Blue ([email protected], http://www.atomicblue.org)
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation (version 2 of the License)
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 #ifndef __STRUTIL_H__
00020 #define __STRUTIL_H__
00021 
00022 //=============================================================================
00023 // Crystal Space Includes
00024 //=============================================================================
00025 #include <csutil/array.h>
00026 #include <csutil/stringarray.h>
00027 #include <csutil/csstring.h>
00028 #include <csgeom/vector3.h>
00029 #include <iengine/sector.h>
00030 #include <csgeom/matrix3.h>
00031 #include <csgeom/transfrm.h>
00032 #include <csutil/stringconv.h>
00033 
00034 class psString;
00035 
00043 void Split(const csString& str, csArray<csString> & arr);
00044 
00056 csString& GetWordNumber(const csString& str, int number, size_t * startpos = NULL);
00057 
00058 
00062 class WordArray : protected csStringArray
00063 {
00064 public:
00065     WordArray(const csString& cmd, bool ignoreQuotes = true);
00066 
00067     size_t GetCount() const
00068     {
00069         return GetSize();
00070     }
00071 
00075     csString Get(size_t wordNum) const
00076     {
00077         if(wordNum < GetSize())
00078             return csStringArray::Get(wordNum);
00079         else
00080             return "";
00081     }
00082     csString operator[](size_t wordNum) const
00083     {
00084         return Get(wordNum);
00085     }
00086 
00090     bool DeleteIndex(size_t n)
00091     {
00092         return csStringArray::DeleteIndex(n);
00093     }
00094 
00095     bool GetString(size_t wordNum, csString &retValue) const
00096     {
00097         if(wordNum >= GetSize())
00098         {
00099             return false;
00100         }
00101 
00102         retValue = csStringArray::Get(wordNum);
00103         return true;
00104     }
00105 
00109     bool IsInt(size_t wordNum) const
00110     {
00111         const char* toConvert = Get(wordNum).GetDataSafe();
00112         char* endPtr = const_cast<char*>(toConvert);
00113 
00114         // TOFIX: gives the following error on windows:
00115         //        error C2106: '=' : left operand must be l-value
00116         //errno = 0; /* To distinguish success/failure after call */
00117         strtol(toConvert, &endPtr, 10);  // 10 base
00118 
00119         // Check for error situations
00120         if (errno != 0) return false;
00121         if (toConvert == endPtr) return false;
00122         if (*endPtr != '\0') return false;  // Further characters after number
00123         
00124         return true;
00125     }
00126 
00127     int GetInt(size_t wordNum) const 
00128     {
00129         return atoi(Get(wordNum).GetData());
00130     }
00131 
00132     bool GetInt(size_t wordNum, int &retValue ) const
00133     {
00134         char *end;
00135 
00136         const char *data = Get(wordNum).GetData();
00137    
00138         // The string has to contain some chars, otherwise the end pointer will not
00139         // be able to tell if all data where an integer.
00140         if (!data || *data == '\0')
00141         {
00142             return false;
00143         }
00144 
00145         int value = (int)strtol(data, &end, 10);
00146 
00147         // If end point to a \0 then everything where read.
00148         if (*end == '\0')
00149         {
00150             retValue = value;
00151             return true;
00152         }
00153 
00154         return false;
00155     }
00156 
00160     bool IsFloat(size_t wordNum) const
00161     {
00162         const char* toConvert = Get(wordNum).GetDataSafe();
00163         const char* endPtr = const_cast<char*>(toConvert);
00164 
00165         // TOFIX: gives the following error on windows:
00166         //        error C2106: '=' : left operand must be l-value
00167         // errno = 0; /* To distinguish success/failure after call */
00168         CS::Utility::strtof(toConvert, &endPtr);
00169 
00170         // Check for error situations
00171         if (errno != 0) return false;
00172         if (toConvert == endPtr) return false;
00173         if (*endPtr != '\0') return false;  // Further characters after number
00174         
00175         return true;
00176     }
00177 
00178     float GetFloat(size_t wordNum) const
00179     {
00180         return atof(Get(wordNum).GetData());
00181     }
00182 
00183     bool GetFloat(size_t wordNum, float &retValue ) const
00184     {
00185         const char *end;
00186 
00187         const char *data = Get(wordNum).GetData();
00188    
00189         // The string has to contain some chars, otherwise the end pointer will not
00190         // be able to tell if all data where an integer.
00191         if (!data || *data == '\0')
00192         {
00193             return false;
00194         }
00195 
00196         float value = (float)CS::Utility::strtof(data, &end);
00197 
00198         // If end point to a \0 then everything where read.
00199         if (*end == '\0')
00200         {
00201             retValue = value;
00202             return true;
00203         }
00204 
00205         return false;
00206     }
00207 
00208     /* Returns all words, starting at given word */
00209     csString GetTail(size_t wordNum) const;
00210     void GetTail(size_t wordNum, csString& dest) const;
00211 
00212     /* Gets all the words from startWord to endWord */
00213     csString GetWords( size_t startWord, size_t endWord) const;
00214 
00215     size_t FindStr(const csString& str,int start=0) const;
00216 
00217 protected:
00218     size_t AddWord(const csString& cmd, size_t pos);
00219     size_t AddQuotedWord(const csString& cmd, size_t pos);
00220     void SkipSpaces(const csString& cmd, size_t & pos);
00221 };
00222 
00223 
00224 bool psContain(const csString& str, const csArray<csString>& strs);
00225 bool psSentenceContain(const csString& sentence,const csString& word);
00226 const char* PS_GetFileName(const char* path);
00227 csArray<csString> psSplit(csString& str, char delimer);
00228 csArray<csString> psSplit(const char* str, char delimer);
00229 bool isFlagSet(const psString & flagstr, const char * flag);
00230 csString toString(const csVector2& pos);
00231 csString toString(const csVector3& pos);
00232 csString toString(const csVector4& pos);
00233 csString toString(const csVector3& pos, iSector* sector);
00234 csString toString(const csMatrix3& mat);
00235 csString toString(const csTransform& trans);
00236 
00243 csArray<csString> splitTextInLines(csString inText, size_t maxLineLength, int& maxRowLen);
00244 
00247 #endif
00248