# string_func.h

Go to the documentation of this file.

    00001 /* $Id: string_func.h 21047 2010-10-27 20:15:18Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00025 #ifndef STRING_FUNC_H
    00026 #define STRING_FUNC_H
    00027
    00028 #include "core/bitmath_func.hpp"
    00029 #include "string_type.h"
    00030
    00045 void ttd_strlcat(char *dst, const char *src, size_t size);
    00046
    00061 void ttd_strlcpy(char *dst, const char *src, size_t size);
    00062
    00079 char *strecat(char *dst, const char *src, const char *last);
    00080
    00097 char *strecpy(char *dst, const char *src, const char *last);
    00098
    00099 int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4);
    00100
    00101 char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
    00102
    00111 void str_validate(char *str, const char *last, bool allow_newlines = false, bool ignore = false);
    00112
    00114 void str_strip_colours(char *str);
    00115
    00117 void strtolower(char *str);
    00118
    00126 static inline bool StrEmpty(const char *s)
    00127 {
    00128   return s == NULL || s[0] == '\0';
    00129 }
    00130
    00138 static inline size_t ttd_strnlen(const char *str, size_t maxlen)
    00139 {
    00140   const char *t;
    00141   for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
    00142   return t - str;
    00143 }
    00144
    00146 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
    00147
    00155 bool IsValidChar(WChar key, CharSetFilter afilter);
    00156
    00157 size_t Utf8Decode(WChar *c, const char *s);
    00158 size_t Utf8Encode(char *buf, WChar c);
    00159 size_t Utf8TrimString(char *s, size_t maxlen);
    00160
    00161
    00162 static inline WChar Utf8Consume(const char **s)
    00163 {
    00164   WChar c;
    00165   *s += Utf8Decode(&c, *s);
    00166   return c;
    00167 }
    00168
    00169
    00174 static inline int8 Utf8CharLen(WChar c)
    00175 {
    00176   if (c < 0x80)       return 1;
    00177   if (c < 0x800)      return 2;
    00178   if (c < 0x10000)    return 3;
    00179   if (c < 0x110000)   return 4;
    00180
    00181   /* Invalid valid, we encode as a '?' */
    00182   return 1;
    00183 }
    00184
    00185
    00193 static inline int8 Utf8EncodedCharLen(char c)
    00194 {
    00195   if (GB(c, 3, 5) == 0x1E) return 4;
    00196   if (GB(c, 4, 4) == 0x0E) return 3;
    00197   if (GB(c, 5, 3) == 0x06) return 2;
    00198   if (GB(c, 7, 1) == 0x00) return 1;
    00199
    00200   /* Invalid UTF8 start encoding */
    00201   return 0;
    00202 }
    00203
    00204
    00205 /* Check if the given character is part of a UTF8 sequence */
    00206 static inline bool IsUtf8Part(char c)
    00207 {
    00208   return GB(c, 6, 2) == 2;
    00209 }
    00210
    00218 static inline char *Utf8PrevChar(char *s)
    00219 {
    00220   char *ret = s;
    00221   while (IsUtf8Part(*--ret)) {}
    00222   return ret;
    00223 }
    00224
    00231 static inline bool IsTextDirectionChar(WChar c)
    00232 {
    00233   switch (c) {
    00234     case CHAR_TD_LRM:
    00235     case CHAR_TD_RLM:
    00236     case CHAR_TD_LRE:
    00237     case CHAR_TD_RLE:
    00238     case CHAR_TD_LRO:
    00239     case CHAR_TD_RLO:
    00240     case CHAR_TD_PDF:
    00241       return true;
    00242
    00243     default:
    00244       return false;
    00245   }
    00246 }
    00247
    00248 static inline bool IsPrintable(WChar c)
    00249 {
    00250   if (c < 0x20)   return false;
    00251   if (c < 0xE000) return true;
    00252   if (c < 0xE200) return false;
    00253   return true;
    00254 }
    00255
    00263 static inline bool IsWhitespace(WChar c)
    00264 {
    00265   return
    00266     c == 0x0020 /* SPACE */ ||
    00267     c == 0x3000 /* IDEOGRAPHIC SPACE */
    00268   ;
    00269 }
    00270
    00271 /* Needed for NetBSD version (so feature) testing */
    00272 #ifdef __NetBSD__
    00273 #include <sys/param.h>
    00274 #endif
    00275
    00276 /* strndup is a GNU extension */
    00277 #if defined(_GNU_SOURCE) || (defined(__NetBSD_Version__) && 400000000 <= __NetBSD_Version__)
    00278 # undef DEFINE_STRNDUP
    00279 #else
    00280 # define DEFINE_STRNDUP
    00281 char *strndup(const char *s, size_t len);
    00282 #endif /* strndup is available */
    00283
    00284 /* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
    00285 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
    00286 # undef DEFINE_STRCASESTR
    00287 #else
    00288 # define DEFINE_STRCASESTR
    00289 char *strcasestr(const char *haystack, const char *needle);
    00290 #endif /* strcasestr is available */
    00291
    00292 #endif /* STRING_FUNC_H */