string_func.h

Go to the documentation of this file.
00001 /* $Id: string_func.h 15376 2009-02-06 15:53:48Z rubidium $ */
00002 
00018 #ifndef STRING_FUNC_H
00019 #define STRING_FUNC_H
00020 
00021 #include "core/bitmath_func.hpp"
00022 #include "string_type.h"
00023 
00038 void ttd_strlcat(char *dst, const char *src, size_t size);
00039 
00054 void ttd_strlcpy(char *dst, const char *src, size_t size);
00055 
00072 char *strecat(char *dst, const char *src, const char *last);
00073 
00090 char *strecpy(char *dst, const char *src, const char *last);
00091 
00092 int CDECL seprintf(char *str, const char *last, const char *format, ...);
00093 
00094 char *CDECL str_fmt(const char *str, ...);
00095 
00098 void str_validate(char *str, bool allow_newlines = false, bool ignore = false);
00099 
00101 void str_strip_colours(char *str);
00102 
00104 void strtolower(char *str);
00105 
00113 static inline bool StrEmpty(const char *s)
00114 {
00115   return s == NULL || s[0] == '\0';
00116 }
00117 
00125 static inline size_t ttd_strnlen(const char *str, size_t maxlen)
00126 {
00127   const char *t;
00128   for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
00129   return t - str;
00130 }
00131 
00133 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
00134 
00142 bool IsValidChar(WChar key, CharSetFilter afilter);
00143 
00144 size_t Utf8Decode(WChar *c, const char *s);
00145 size_t Utf8Encode(char *buf, WChar c);
00146 size_t Utf8TrimString(char *s, size_t maxlen);
00147 
00148 
00149 static inline WChar Utf8Consume(const char **s)
00150 {
00151   WChar c;
00152   *s += Utf8Decode(&c, *s);
00153   return c;
00154 }
00155 
00156 
00161 static inline int8 Utf8CharLen(WChar c)
00162 {
00163   if (c < 0x80)       return 1;
00164   if (c < 0x800)      return 2;
00165   if (c < 0x10000)    return 3;
00166   if (c < 0x110000)   return 4;
00167 
00168   /* Invalid valid, we encode as a '?' */
00169   return 1;
00170 }
00171 
00172 
00180 static inline int8 Utf8EncodedCharLen(char c)
00181 {
00182   if (GB(c, 3, 5) == 0x1E) return 4;
00183   if (GB(c, 4, 4) == 0x0E) return 3;
00184   if (GB(c, 5, 3) == 0x06) return 2;
00185   if (GB(c, 7, 1) == 0x00) return 1;
00186 
00187   /* Invalid UTF8 start encoding */
00188   return 0;
00189 }
00190 
00191 
00192 /* Check if the given character is part of a UTF8 sequence */
00193 static inline bool IsUtf8Part(char c)
00194 {
00195   return GB(c, 6, 2) == 2;
00196 }
00197 
00205 static inline char *Utf8PrevChar(const char *s)
00206 {
00207   const char *ret = s;
00208   while (IsUtf8Part(*--ret)) {}
00209   return (char*)ret;
00210 }
00211 
00212 
00213 static inline bool IsPrintable(WChar c)
00214 {
00215   if (c < 0x20)   return false;
00216   if (c < 0xE000) return true;
00217   if (c < 0xE200) return false;
00218   return true;
00219 }
00220 
00228 static inline bool IsWhitespace(WChar c)
00229 {
00230   return
00231     c == 0x0020 /* SPACE */ ||
00232     c == 0x3000 /* IDEOGRAPHIC SPACE */
00233   ;
00234 }
00235 
00236 #ifndef _GNU_SOURCE
00237 /* strndup is a GNU extension */
00238 char *strndup(const char *s, size_t len);
00239 #endif /* !_GNU_SOURCE */
00240 
00241 /* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
00242 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)))
00243 # undef DEFINE_STRCASESTR
00244 #else
00245 # define DEFINE_STRCASESTR
00246 const char *strcasestr(const char *haystack, const char *needle);
00247 #endif /* strcasestr is available */
00248 
00249 #endif /* STRING_FUNC_H */

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