Planeshift
strutil.h
Go to the documentation of this file.
1 /*
2  * strutil.h by Matze Braun <MatzeBraun@gmx.de>
3  *
4  * Copyright (C) 2001 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 #ifndef __STRUTIL_H__
20 #define __STRUTIL_H__
21 
22 //=============================================================================
23 // Crystal Space Includes
24 //=============================================================================
25 #include <csutil/array.h>
26 #include <csutil/stringarray.h>
27 #include <csutil/csstring.h>
28 #include <csgeom/vector3.h>
29 #include <iengine/sector.h>
30 #include <csgeom/matrix3.h>
31 #include <csgeom/transfrm.h>
32 #include <csutil/stringconv.h>
33 
34 class psString;
35 
43 void Split(const csString& str, csArray<csString> & arr);
44 
56 csString& GetWordNumber(const csString& str, int number, size_t * startpos = NULL);
57 
58 
62 class WordArray : protected csStringArray
63 {
64 public:
65  WordArray(const csString& cmd, bool ignoreQuotes = true);
66 
67  size_t GetCount() const
68  {
69  return GetSize();
70  }
71 
75  csString Get(size_t wordNum) const
76  {
77  if(wordNum < GetSize())
78  return csStringArray::Get(wordNum);
79  else
80  return "";
81  }
82  csString operator[](size_t wordNum) const
83  {
84  return Get(wordNum);
85  }
86 
90  bool DeleteIndex(size_t n)
91  {
92  return csStringArray::DeleteIndex(n);
93  }
94 
95  bool GetString(size_t wordNum, csString &retValue) const
96  {
97  if(wordNum >= GetSize())
98  {
99  return false;
100  }
101 
102  retValue = csStringArray::Get(wordNum);
103  return true;
104  }
105 
109  bool IsInt(size_t wordNum) const
110  {
111  const char* toConvert = Get(wordNum).GetDataSafe();
112  char* endPtr = const_cast<char*>(toConvert);
113 
114  // TOFIX: gives the following error on windows:
115  // error C2106: '=' : left operand must be l-value
116  //errno = 0; /* To distinguish success/failure after call */
117  strtol(toConvert, &endPtr, 10); // 10 base
118 
119  // Check for error situations
120  if (errno != 0) return false;
121  if (toConvert == endPtr) return false;
122  if (*endPtr != '\0') return false; // Further characters after number
123 
124  return true;
125  }
126 
127  int GetInt(size_t wordNum) const
128  {
129  return atoi(Get(wordNum).GetData());
130  }
131 
132  bool GetInt(size_t wordNum, int &retValue ) const
133  {
134  char *end;
135 
136  const char *data = Get(wordNum).GetData();
137 
138  // The string has to contain some chars, otherwise the end pointer will not
139  // be able to tell if all data where an integer.
140  if (!data || *data == '\0')
141  {
142  return false;
143  }
144 
145  int value = (int)strtol(data, &end, 10);
146 
147  // If end point to a \0 then everything where read.
148  if (*end == '\0')
149  {
150  retValue = value;
151  return true;
152  }
153 
154  return false;
155  }
156 
160  bool IsFloat(size_t wordNum) const
161  {
162  const char* toConvert = Get(wordNum).GetDataSafe();
163  const char* endPtr = const_cast<char*>(toConvert);
164 
165  // TOFIX: gives the following error on windows:
166  // error C2106: '=' : left operand must be l-value
167  // errno = 0; /* To distinguish success/failure after call */
168  CS::Utility::strtof(toConvert, &endPtr);
169 
170  // Check for error situations
171  if (errno != 0) return false;
172  if (toConvert == endPtr) return false;
173  if (*endPtr != '\0') return false; // Further characters after number
174 
175  return true;
176  }
177 
178  float GetFloat(size_t wordNum) const
179  {
180  return atof(Get(wordNum).GetData());
181  }
182 
183  bool GetFloat(size_t wordNum, float &retValue ) const
184  {
185  const char *end;
186 
187  const char *data = Get(wordNum).GetData();
188 
189  // The string has to contain some chars, otherwise the end pointer will not
190  // be able to tell if all data where an integer.
191  if (!data || *data == '\0')
192  {
193  return false;
194  }
195 
196  float value = (float)CS::Utility::strtof(data, &end);
197 
198  // If end point to a \0 then everything where read.
199  if (*end == '\0')
200  {
201  retValue = value;
202  return true;
203  }
204 
205  return false;
206  }
207 
208  /* Returns all words, starting at given word */
209  csString GetTail(size_t wordNum) const;
210  void GetTail(size_t wordNum, csString& dest) const;
211 
212  /* Gets all the words from startWord to endWord */
213  csString GetWords( size_t startWord, size_t endWord) const;
214 
215  size_t FindStr(const csString& str,int start=0) const;
216 
217 protected:
218  size_t AddWord(const csString& cmd, size_t pos);
219  size_t AddQuotedWord(const csString& cmd, size_t pos);
220  void SkipSpaces(const csString& cmd, size_t & pos);
221 };
222 
223 
224 bool psContain(const csString& str, const csArray<csString>& strs);
225 bool psSentenceContain(const csString& sentence,const csString& word);
226 const char* PS_GetFileName(const char* path);
227 csArray<csString> psSplit(csString& str, char delimer);
228 csArray<csString> psSplit(const char* str, char delimer);
229 bool isFlagSet(const psString & flagstr, const char * flag);
230 csString toString(const csVector2& pos);
231 csString toString(const csVector3& pos);
232 csString toString(const csVector4& pos);
233 csString toString(const csVector3& pos, iSector* sector);
234 csString toString(const csMatrix3& mat);
235 csString toString(const csTransform& trans);
236 
243 csArray<csString> splitTextInLines(csString inText, size_t maxLineLength, int& maxRowLen);
244 
247 #endif
248 
float GetFloat(size_t wordNum) const
Definition: strutil.h:178
size_t GetCount() const
Definition: strutil.h:67
csString GetTail(size_t wordNum) const
int GetInt(size_t wordNum) const
Definition: strutil.h:127
bool DeleteIndex(size_t n)
Delete the n&#39;th element from the word array.
Definition: strutil.h:90
csString & GetWordNumber(const csString &str, int number, size_t *startpos=NULL)
Return the given word number.
bool GetFloat(size_t wordNum, float &retValue) const
Definition: strutil.h:183
bool IsInt(size_t wordNum) const
Check if the word is an interger.
Definition: strutil.h:109
bool IsFloat(size_t wordNum) const
Check if the word is a float.
Definition: strutil.h:160
csArray< csString > splitTextInLines(csString inText, size_t maxLineLength, int &maxRowLen)
Split text into multiple lines.
csString GetWords(size_t startWord, size_t endWord) const
bool GetInt(size_t wordNum, int &retValue) const
Definition: strutil.h:132
bool psSentenceContain(const csString &sentence, const csString &word)
WordArray is class that parses text command (e.g.
Definition: strutil.h:62
size_t FindStr(const csString &str, int start=0) const
bool psContain(const csString &str, const csArray< csString > &strs)
const char * PS_GetFileName(const char *path)
size_t AddWord(const csString &cmd, size_t pos)
bool GetString(size_t wordNum, csString &retValue) const
Definition: strutil.h:95
csArray< csString > psSplit(csString &str, char delimer)
csString toString(const csVector2 &pos)
csString operator[](size_t wordNum) const
Definition: strutil.h:82
void Split(const csString &str, csArray< csString > &arr)
Split a csString into an array of sctrings.
size_t AddQuotedWord(const csString &cmd, size_t pos)
WordArray(const csString &cmd, bool ignoreQuotes=true)
void SkipSpaces(const csString &cmd, size_t &pos)
bool isFlagSet(const psString &flagstr, const char *flag)
csString Get(size_t wordNum) const
Returns given word, or empty string if it does not exist.
Definition: strutil.h:75