string_func.h

Go to the documentation of this file.
00001 /* $Id$ */
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 
00026 #ifndef STRING_FUNC_H
00027 #define STRING_FUNC_H
00028 
00029 #include "core/bitmath_func.hpp"
00030 #include "string_type.h"
00031 
00032 void ttd_strlcat(char *dst, const char *src, size_t size);
00033 void ttd_strlcpy(char *dst, const char *src, size_t size);
00034 
00035 char *strecat(char *dst, const char *src, const char *last);
00036 char *strecpy(char *dst, const char *src, const char *last);
00037 
00038 int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4);
00039 
00040 char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
00041 
00042 void str_validate(char *str, const char *last, bool allow_newlines = false, bool ignore = false);
00043 void str_strip_colours(char *str);
00044 void strtolower(char *str);
00045 
00046 bool StrValid(const char *str, const char *last);
00047 
00055 static inline bool StrEmpty(const char *s)
00056 {
00057   return s == NULL || s[0] == '\0';
00058 }
00059 
00067 static inline size_t ttd_strnlen(const char *str, size_t maxlen)
00068 {
00069   const char *t;
00070   for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
00071   return t - str;
00072 }
00073 
00074 char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
00075 
00076 bool IsValidChar(WChar key, CharSetFilter afilter);
00077 
00078 size_t Utf8Decode(WChar *c, const char *s);
00079 size_t Utf8Encode(char *buf, WChar c);
00080 size_t Utf8TrimString(char *s, size_t maxlen);
00081 
00082 
00083 static inline WChar Utf8Consume(const char **s)
00084 {
00085   WChar c;
00086   *s += Utf8Decode(&c, *s);
00087   return c;
00088 }
00089 
00090 
00096 static inline int8 Utf8CharLen(WChar c)
00097 {
00098   if (c < 0x80)       return 1;
00099   if (c < 0x800)      return 2;
00100   if (c < 0x10000)    return 3;
00101   if (c < 0x110000)   return 4;
00102 
00103   /* Invalid valid, we encode as a '?' */
00104   return 1;
00105 }
00106 
00107 
00115 static inline int8 Utf8EncodedCharLen(char c)
00116 {
00117   if (GB(c, 3, 5) == 0x1E) return 4;
00118   if (GB(c, 4, 4) == 0x0E) return 3;
00119   if (GB(c, 5, 3) == 0x06) return 2;
00120   if (GB(c, 7, 1) == 0x00) return 1;
00121 
00122   /* Invalid UTF8 start encoding */
00123   return 0;
00124 }
00125 
00126 
00127 /* Check if the given character is part of a UTF8 sequence */
00128 static inline bool IsUtf8Part(char c)
00129 {
00130   return GB(c, 6, 2) == 2;
00131 }
00132 
00140 static inline char *Utf8PrevChar(char *s)
00141 {
00142   char *ret = s;
00143   while (IsUtf8Part(*--ret)) {}
00144   return ret;
00145 }
00146 
00147 size_t Utf8StringLength(const char *s);
00148 
00155 static inline bool IsTextDirectionChar(WChar c)
00156 {
00157   switch (c) {
00158     case CHAR_TD_LRM:
00159     case CHAR_TD_RLM:
00160     case CHAR_TD_LRE:
00161     case CHAR_TD_RLE:
00162     case CHAR_TD_LRO:
00163     case CHAR_TD_RLO:
00164     case CHAR_TD_PDF:
00165       return true;
00166 
00167     default:
00168       return false;
00169   }
00170 }
00171 
00172 static inline bool IsPrintable(WChar c)
00173 {
00174   if (c < 0x20)   return false;
00175   if (c < 0xE000) return true;
00176   if (c < 0xE200) return false;
00177   return true;
00178 }
00179 
00187 static inline bool IsWhitespace(WChar c)
00188 {
00189   return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
00190 }
00191 
00192 /* Needed for NetBSD version (so feature) testing */
00193 #if defined(__NetBSD__) || defined(__FreeBSD__)
00194 #include <sys/param.h>
00195 #endif
00196 
00197 /* strndup is a GNU extension */
00198 #if defined(_GNU_SOURCE) || (defined(__NetBSD_Version__) && 400000000 <= __NetBSD_Version__) || (defined(__FreeBSD_version) && 701101 <= __FreeBSD_version)
00199 # undef DEFINE_STRNDUP
00200 #else
00201 # define DEFINE_STRNDUP
00202 char *strndup(const char *s, size_t len);
00203 #endif /* strndup is available */
00204 
00205 /* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
00206 #if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
00207 # undef DEFINE_STRCASESTR
00208 #else
00209 # define DEFINE_STRCASESTR
00210 char *strcasestr(const char *haystack, const char *needle);
00211 #endif /* strcasestr is available */
00212 
00213 int strnatcmp(const char *s1, const char *s2);
00214 
00215 #endif /* STRING_FUNC_H */

Generated on Sun Jun 5 04:20:04 2011 for OpenTTD by  doxygen 1.6.1