The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utilstr.cpp File Reference
#include <utilstr.h>
#include <ctype.h>
#include <string.h>
#include <sysdata.h>
#include <swlog.h>
#include <swbuf.h>
+ Include dependency graph for utilstr.cpp:

Go to the source code of this file.

Functions

SWBuf assureValidUTF8 (const char *buf)
 
int stricmp (const char *s1, const char *s2)
 
const char * stristr (const char *s1, const char *s2)
 
int strnicmp (const char *s1, const char *s2, int len)
 
char * strstrip (char *istr)
 
SWBuf utf8ToWChar (const char *buf)
 
SWBuf wcharToUTF8 (const wchar_t *buf)
 

Variables

const unsigned char SW_tolower_array [256]
 
SWORD_NAMESPACE_START const
unsigned char 
SW_toupper_array [256]
 

Function Documentation

SWBuf assureValidUTF8 ( const char *  buf)

Definition at line 207 of file utilstr.cpp.

207  {
208 
209  SWBuf myCopy = buf;
210  const unsigned char *b = (const unsigned char *)myCopy.c_str();
211  const unsigned char *q = 0;
212  bool invalidChar = false;
213  while (*b) {
214  q = b;
215  if (!getUniCharFromUTF8(&b)) {
216  long len = b - q;
217  if (len) {
218  invalidChar = true;
219  for (long start = q - (const unsigned char *)myCopy.c_str(); len; len--) {
220  myCopy[start+len-1] = 0x1a; // unicode replacement character
221  }
222 
223  }
224  }
225  }
226  if (invalidChar) {
227 // SWLog::getSystemLog()->logWarning("Changing invalid UTF-8 string (%s) to (%s)\n", buf, myCopy.c_str());
228  }
229  return myCopy;
230 }
Definition: swbuf.h:47
const char * c_str() const
Definition: swbuf.h:158
SW_u32 getUniCharFromUTF8(const unsigned char **buf, bool skipValidation=false)
Definition: utilstr.h:88
int stricmp ( const char *  s1,
const char *  s2 
)

Definition at line 194 of file utilstr.cpp.

194  {
195 #if defined(__GNUC__)
196  return ::strcasecmp(s1, s2);
197 #else
198  #if defined(_WIN32_WCE)
199  return ::_stricmp(s1, s2);
200  #else
201  return ::stricmp(s1, s2);
202  #endif
203 #endif
204 }
int stricmp(const char *s1, const char *s2)
Definition: utilstr.cpp:194
const char* stristr ( const char *  s1,
const char *  s2 
)

Definition at line 145 of file utilstr.cpp.

145  {
146  int tLen = (int)strlen(s2);
147  int cLen = (int)strlen(s1);
148  char *target = new char [ tLen + 1 ];
149  int i, j;
150  const char *retVal = 0;
151 
152  strcpy(target, s2);
153  for (i = 0; i < tLen; i++)
154  target[i] = SW_toupper(target[i]);
155 
156  for (i = 0; i < (cLen - tLen)+1; i++) {
157  if (SW_toupper(s1[i]) == (unsigned char)*target) {
158  for (j = 1; j < tLen; j++) {
159  if (SW_toupper(s1[i+j]) != (unsigned char)target[j])
160  break;
161  }
162  if (j == tLen) {
163  retVal = s1+i;
164  break;
165  }
166  }
167  }
168  delete [] target;
169  return retVal;
170 }
#define SW_toupper(c)
Definition: utilstr.h:67
int strnicmp ( const char *  s1,
const char *  s2,
int  len 
)

Definition at line 180 of file utilstr.cpp.

180  {
181  int tLen = (int)strlen(s2);
182  int cLen = (int)strlen(s1);
183  char diff;
184  int i;
185  for (i = 0; ((i < len) && (i < tLen) && (i < cLen)); i++) {
186  if ((diff = SW_toupper(*s1) - SW_toupper(*s2)))
187  return diff;
188  s1++;
189  s2++;
190  }
191  return (i < len) ? cLen - tLen : 0;
192 }
#define SW_toupper(c)
Definition: utilstr.h:67
char* strstrip ( char *  istr)

Definition at line 118 of file utilstr.cpp.

118  {
119  char *tmp = istr;
120  char *rtmp;
121 
122  int len = (int)strlen(istr);
123  if (len < 1)
124  return istr;
125  rtmp = istr + (len - 1);
126 
127  while ((rtmp > istr)&&((*rtmp == ' ')||(*rtmp == '\t')||(*rtmp == 10)||(*rtmp == 13))) *(rtmp--) = 0;
128  while ((*tmp == ' ')||(*tmp == '\t')||(*tmp == 10)||(*tmp == 13)) tmp++;
129  memmove(istr, tmp, (rtmp - tmp) + 1);
130  istr[(rtmp - tmp) + 1] = 0;
131 
132  return istr;
133 }
SWBuf utf8ToWChar ( const char *  buf)

Definition at line 239 of file utilstr.cpp.

239  {
240 
241  const char *q = 0;
242  SWBuf wcharBuf;
243  while (*buf) {
244  q = buf;
245  wchar_t wc = getUniCharFromUTF8((const unsigned char **)&buf);
246  if (!wc) {
247  // if my buffer was advanced but nothing was converted, I had invalid data
248  if (buf - q) {
249  // invalid bytes in UTF8 stream
250  wcharBuf.append((wchar_t)0x1a); // unicode replacement character
251  }
252  }
253  else wcharBuf.append(wc);
254  }
255  return wcharBuf;
256 }
Definition: swbuf.h:47
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
SW_u32 getUniCharFromUTF8(const unsigned char **buf, bool skipValidation=false)
Definition: utilstr.h:88
SWBuf wcharToUTF8 ( const wchar_t *  buf)

Definition at line 263 of file utilstr.cpp.

263  {
264 
265  SWBuf utf8Buf;
266  if (buf) {
267  while (*buf) {
268  getUTF8FromUniChar(*buf++, &utf8Buf);
269  }
270  }
271  return utf8Buf;
272 }
Definition: swbuf.h:47
SWBuf * getUTF8FromUniChar(SW_u32 uchar, SWBuf *appendTo)
Definition: utilstr.h:165

Variable Documentation

const unsigned char SW_tolower_array[256]

Definition at line 73 of file utilstr.cpp.

SWORD_NAMESPACE_START const unsigned char SW_toupper_array[256]

Definition at line 34 of file utilstr.cpp.