string.cpp

Go to the documentation of this file.
00001 /* $Id: string.cpp 15376 2009-02-06 15:53:48Z rubidium $ */
00002 
00005 #include "stdafx.h"
00006 #include "debug.h"
00007 #include "core/alloc_func.hpp"
00008 #include "core/math_func.hpp"
00009 #include "string_func.h"
00010 
00011 #include "table/control_codes.h"
00012 
00013 #include <stdarg.h>
00014 #include <ctype.h> // required for tolower()
00015 
00026 static int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
00027 {
00028   if (str >= last) return 0;
00029   size_t size = last - str;
00030   return min((int)size, vsnprintf(str, size, format, ap));
00031 }
00032 
00033 void ttd_strlcat(char *dst, const char *src, size_t size)
00034 {
00035   assert(size > 0);
00036   while (size > 0 && *dst != '\0') {
00037     size--;
00038     dst++;
00039   }
00040 
00041   ttd_strlcpy(dst, src, size);
00042 }
00043 
00044 
00045 void ttd_strlcpy(char *dst, const char *src, size_t size)
00046 {
00047   assert(size > 0);
00048   while (--size > 0 && *src != '\0') {
00049     *dst++ = *src++;
00050   }
00051   *dst = '\0';
00052 }
00053 
00054 
00055 char *strecat(char *dst, const char *src, const char *last)
00056 {
00057   assert(dst <= last);
00058   while (*dst != '\0') {
00059     if (dst == last) return dst;
00060     dst++;
00061   }
00062 
00063   return strecpy(dst, src, last);
00064 }
00065 
00066 
00067 char *strecpy(char *dst, const char *src, const char *last)
00068 {
00069   assert(dst <= last);
00070   while (dst != last && *src != '\0') {
00071     *dst++ = *src++;
00072   }
00073   *dst = '\0';
00074 
00075   if (dst == last && *src != '\0') {
00076 #ifdef STRGEN
00077     error("String too long for destination buffer");
00078 #else /* STRGEN */
00079     DEBUG(misc, 0, "String too long for destination buffer");
00080 #endif /* STRGEN */
00081   }
00082   return dst;
00083 }
00084 
00085 
00086 char *CDECL str_fmt(const char *str, ...)
00087 {
00088   char buf[4096];
00089   va_list va;
00090 
00091   va_start(va, str);
00092   int len = vseprintf(buf, lastof(buf), str, va);
00093   va_end(va);
00094   char *p = MallocT<char>(len + 1);
00095   memcpy(p, buf, len + 1);
00096   return p;
00097 }
00098 
00099 
00100 void str_validate(char *str, bool allow_newlines, bool ignore)
00101 {
00102   char *dst = str;
00103   WChar c;
00104   size_t len;
00105 
00106   for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
00107     if (IsPrintable(c) && (c < SCC_SPRITE_START || c > SCC_SPRITE_END ||
00108       IsValidChar(c - SCC_SPRITE_START, CS_ALPHANUMERAL))) {
00109       /* Copy the character back. Even if dst is current the same as str
00110        * (i.e. no characters have been changed) this is quicker than
00111        * moving the pointers ahead by len */
00112       do {
00113         *dst++ = *str++;
00114       } while (--len != 0);
00115     } else if (allow_newlines && c == '\n') {
00116       *dst++ = *str++;
00117     } else {
00118       if (allow_newlines && c == '\r' && str[1] == '\n') {
00119         str += len;
00120         continue;
00121       }
00122       assert(c != '\r');
00123       /* Replace the undesirable character with a question mark */
00124       str += len;
00125       if (!ignore) *dst++ = '?';
00126     }
00127   }
00128 
00129   *dst = '\0';
00130 }
00131 
00132 
00133 void str_strip_colours(char *str)
00134 {
00135   char *dst = str;
00136   WChar c;
00137   size_t len;
00138 
00139   for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
00140     if (c < SCC_BLUE || c > SCC_BLACK) {
00141       /* Copy the character back. Even if dst is current the same as str
00142        * (i.e. no characters have been changed) this is quicker than
00143        * moving the pointers ahead by len */
00144       do {
00145         *dst++ = *str++;
00146       } while (--len != 0);
00147     } else {
00148       /* Just skip (strip) the colour codes */
00149       str += len;
00150     }
00151   }
00152   *dst = '\0';
00153 }
00154 
00163 void strtolower(char *str)
00164 {
00165   for (; *str != '\0'; str++) *str = tolower(*str);
00166 }
00167 
00175 bool IsValidChar(WChar key, CharSetFilter afilter)
00176 {
00177   switch (afilter) {
00178     case CS_ALPHANUMERAL: return IsPrintable(key);
00179     case CS_NUMERAL:      return (key >= '0' && key <= '9');
00180     case CS_ALPHA:        return IsPrintable(key) && !(key >= '0' && key <= '9');
00181   }
00182 
00183   return false;
00184 }
00185 
00186 #ifdef WIN32
00187 /* Since version 3.14, MinGW Runtime has snprintf() and vsnprintf() conform to C99 but it's not the case for older versions */
00188 #if (__MINGW32_MAJOR_VERSION < 3) || ((__MINGW32_MAJOR_VERSION == 3) && (__MINGW32_MINOR_VERSION < 14))
00189 int CDECL snprintf(char *str, size_t size, const char *format, ...)
00190 {
00191   va_list ap;
00192   int ret;
00193 
00194   va_start(ap, format);
00195   ret = vsnprintf(str, size, format, ap);
00196   va_end(ap);
00197   return ret;
00198 }
00199 #endif /* MinGW Runtime < 3.14 */
00200 
00201 #ifdef _MSC_VER
00202 /* *nprintf broken, not POSIX compliant, MSDN description
00203  * - If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.
00204  * - If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.
00205  * - If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned
00206  */
00207 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
00208 {
00209   int ret;
00210   ret = _vsnprintf(str, size, format, ap);
00211   if (ret < 0 || ret == size) str[size - 1] = '\0';
00212   return ret;
00213 }
00214 #endif /* _MSC_VER */
00215 
00216 #endif /* WIN32 */
00217 
00227 int CDECL seprintf(char *str, const char *last, const char *format, ...)
00228 {
00229   va_list ap;
00230 
00231   va_start(ap, format);
00232   int ret = vseprintf(str, last, format, ap);
00233   va_end(ap);
00234   return ret;
00235 }
00236 
00237 
00243 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
00244 {
00245   char *p = buf;
00246 
00247   for (uint i = 0; i < 16; i++) {
00248     p += seprintf(p, last, "%02X", md5sum[i]);
00249   }
00250 
00251   return p;
00252 }
00253 
00254 
00255 /* UTF-8 handling routines */
00256 
00257 
00258 /* Decode and consume the next UTF-8 encoded character
00259  * @param c Buffer to place decoded character.
00260  * @param s Character stream to retrieve character from.
00261  * @return Number of characters in the sequence.
00262  */
00263 size_t Utf8Decode(WChar *c, const char *s)
00264 {
00265   assert(c != NULL);
00266 
00267   if (!HasBit(s[0], 7)) {
00268     /* Single byte character: 0xxxxxxx */
00269     *c = s[0];
00270     return 1;
00271   } else if (GB(s[0], 5, 3) == 6) {
00272     if (IsUtf8Part(s[1])) {
00273       /* Double byte character: 110xxxxx 10xxxxxx */
00274       *c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
00275       if (*c >= 0x80) return 2;
00276     }
00277   } else if (GB(s[0], 4, 4) == 14) {
00278     if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
00279       /* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
00280       *c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
00281       if (*c >= 0x800) return 3;
00282     }
00283   } else if (GB(s[0], 3, 5) == 30) {
00284     if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
00285       /* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
00286       *c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
00287       if (*c >= 0x10000 && *c <= 0x10FFFF) return 4;
00288     }
00289   }
00290 
00291   //DEBUG(misc, 1, "[utf8] invalid UTF-8 sequence");
00292   *c = '?';
00293   return 1;
00294 }
00295 
00296 
00297 /* Encode a unicode character and place it in the buffer
00298  * @param buf Buffer to place character.
00299  * @param c   Unicode character to encode.
00300  * @return Number of characters in the encoded sequence.
00301  */
00302 size_t Utf8Encode(char *buf, WChar c)
00303 {
00304   if (c < 0x80) {
00305     *buf = c;
00306     return 1;
00307   } else if (c < 0x800) {
00308     *buf++ = 0xC0 + GB(c,  6, 5);
00309     *buf   = 0x80 + GB(c,  0, 6);
00310     return 2;
00311   } else if (c < 0x10000) {
00312     *buf++ = 0xE0 + GB(c, 12, 4);
00313     *buf++ = 0x80 + GB(c,  6, 6);
00314     *buf   = 0x80 + GB(c,  0, 6);
00315     return 3;
00316   } else if (c < 0x110000) {
00317     *buf++ = 0xF0 + GB(c, 18, 3);
00318     *buf++ = 0x80 + GB(c, 12, 6);
00319     *buf++ = 0x80 + GB(c,  6, 6);
00320     *buf   = 0x80 + GB(c,  0, 6);
00321     return 4;
00322   }
00323 
00324   //DEBUG(misc, 1, "[utf8] can't UTF-8 encode value 0x%X", c);
00325   *buf = '?';
00326   return 1;
00327 }
00328 
00336 size_t Utf8TrimString(char *s, size_t maxlen)
00337 {
00338   size_t length = 0;
00339 
00340   for (const char *ptr = strchr(s, '\0'); *s != '\0';) {
00341     size_t len = Utf8EncodedCharLen(*s);
00342     /* Silently ignore invalid UTF8 sequences, our only concern trimming */
00343     if (len == 0) len = 1;
00344 
00345     /* Take care when a hard cutoff was made for the string and
00346      * the last UTF8 sequence is invalid */
00347     if (length + len >= maxlen || (s + len > ptr)) break;
00348     s += len;
00349     length += len;
00350   }
00351 
00352   *s = '\0';
00353   return length;
00354 }
00355 
00356 #ifndef _GNU_SOURCE
00357 #include "core/math_func.hpp"
00358 char *strndup(const char *s, size_t len)
00359 {
00360   len = min(strlen(s), len);
00361   char *tmp = CallocT<char>(len + 1);
00362   memcpy(tmp, s, len);
00363   return tmp;
00364 }
00365 #endif /* !_GNU_SOURCE */
00366 
00367 #ifdef DEFINE_STRCASESTR
00368 const char *strcasestr(const char *haystack, const char *needle)
00369 {
00370   size_t hay_len = strlen(haystack);
00371   size_t needle_len = strlen(needle);
00372   while (hay_len >= needle_len) {
00373     if (strncasecmp(haystack, needle, needle_len) == 0) return haystack;
00374 
00375     haystack++;
00376     hay_len--;
00377   }
00378 
00379   return NULL;
00380 }
00381 #endif /* DEFINE_STRCASESTR */

Generated on Mon Feb 16 23:12:11 2009 for openttd by  doxygen 1.5.6