tron@2186: /* $Id$ */ tron@2186: tron@1317: #ifndef STRING_H tron@1317: #define STRING_H tron@1317: peter1138@5108: #include "macros.h" peter1138@5108: tron@1317: /* tron@1317: * dst: destination buffer tron@1317: * src: string to copy/concatenate tron@1317: * size: size of the destination buffer tron@1317: * usage: ttd_strlcpy(dst, src, lengthof(dst)); tron@1317: */ tron@1317: void ttd_strlcat(char *dst, const char *src, size_t size); tron@1317: void ttd_strlcpy(char *dst, const char *src, size_t size); tron@1317: tron@1317: /* tron@1317: * dst: destination buffer tron@1317: * src: string to copy tron@1317: * last: pointer to the last element in the dst array tron@1317: * if NULL no boundary check is performed tron@1317: * returns a pointer to the terminating \0 in the destination buffer tron@1317: * usage: strecpy(dst, src, lastof(dst)); tron@1317: */ tron@1317: char* strecat(char* dst, const char* src, const char* last); tron@1317: char* strecpy(char* dst, const char* src, const char* last); tron@1317: tron@2234: char* CDECL str_fmt(const char* str, ...); tron@2234: Darkvater@2775: /** Scans the string for valid characters and if it finds invalid ones, Darkvater@2775: * replaces them with a question mark '?' */ Darkvater@2775: void str_validate(char *str); Darkvater@2775: Darkvater@5101: /** Scans the string for colour codes and strips them */ Darkvater@5101: void str_strip_colours(char *str); Darkvater@5101: truelight@4300: /** peter1138@5108: * Valid filter types for IsValidChar. truelight@4300: */ truelight@4300: typedef enum CharSetFilter { truelight@4300: CS_ALPHANUMERAL, //! Both numeric and alphabetic and spaces and stuff truelight@4300: CS_NUMERAL, //! Only numeric ones truelight@4300: CS_ALPHA, //! Only alphabetic values truelight@4300: } CharSetFilter; truelight@4300: Darkvater@5164: /** Convert the given string to lowercase, only works with ASCII! */ peter1138@5108: void strtolower(char *str); peter1138@5108: peter1138@5317: peter1138@5317: /** Get the length of a string, within a limited buffer */ peter1138@5317: static inline int ttd_strnlen(const char *str, int maxlen) peter1138@5317: { peter1138@5317: const char *t; peter1138@5317: for (t = str; *t != '\0' && t - str < maxlen; t++); peter1138@5317: return t - str; peter1138@5317: } peter1138@5317: peter1138@5317: peter1138@5108: typedef uint32 WChar; peter1138@5108: truelight@4300: /** truelight@4300: * Only allow certain keys. You can define the filter to be used. This makes truelight@4300: * sure no invalid keys can get into an editbox, like BELL. truelight@4300: * @param key character to be checked truelight@4300: * @param afilter the filter to use truelight@4300: * @return true or false depending if the character is printable/valid or not truelight@4300: */ peter1138@5108: bool IsValidChar(WChar key, CharSetFilter afilter); truelight@4300: peter1138@5108: size_t Utf8Decode(WChar *c, const char *s); peter1138@5108: size_t Utf8Encode(char *buf, WChar c); peter1138@5108: peter1138@5108: peter1138@5108: static inline WChar Utf8Consume(const char **s) peter1138@5108: { peter1138@5108: WChar c; peter1138@5108: *s += Utf8Decode(&c, *s); peter1138@5108: return c; peter1138@5108: } peter1138@5108: peter1138@5108: peter1138@5108: /** Return the length of a UTF-8 encoded character. peter1138@5108: * @param c Unicode character. peter1138@5108: * @return Length of UTF-8 encoding for character. peter1138@5108: */ peter1138@5108: static inline size_t Utf8CharLen(WChar c) peter1138@5108: { peter1138@5108: if (c < 0x80) return 1; peter1138@5108: if (c < 0x800) return 2; peter1138@5108: if (c < 0x10000) return 3; peter1138@5108: if (c < 0x110000) return 4; peter1138@5108: peter1138@5108: /* Invalid valid, we encode as a '?' */ peter1138@5108: return 1; peter1138@5108: } peter1138@5108: peter1138@5108: peter1138@5108: /* Check if the given character is part of a UTF8 sequence */ peter1138@5108: static inline bool IsUtf8Part(char c) peter1138@5108: { peter1138@5108: return GB(c, 6, 2) == 2; peter1138@5108: } peter1138@5108: peter1138@5108: peter1138@5108: static inline bool IsPrintable(WChar c) peter1138@5108: { peter1138@5108: if (c < 0x20) return false; peter1138@5108: if (c < 0xE000) return true; peter1138@5108: if (c < 0xE200) return false; peter1138@5108: return true; peter1138@5108: } peter1138@5108: Darkvater@4200: Darkvater@2436: #endif /* STRING_H */