string.cpp

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

Generated on Fri Jan 9 19:01:51 2009 for openttd by  doxygen 1.5.6