tron@2186: /* $Id$ */ tron@2186: tron@1317: #include "stdafx.h" tron@1317: #include "string.h" tron@1317: tron@2234: #include Darkvater@4200: #include // required for tolower() tron@2234: tron@1317: void ttd_strlcat(char *dst, const char *src, size_t size) tron@1317: { tron@1317: assert(size > 0); tron@1317: for (; size > 0 && *dst != '\0'; --size, ++dst) {} tron@1317: assert(size > 0); tron@1317: while (--size > 0 && *src != '\0') *dst++ = *src++; tron@1317: *dst = '\0'; tron@1317: } tron@1317: tron@1317: tron@1317: void ttd_strlcpy(char *dst, const char *src, size_t size) tron@1317: { tron@1317: assert(size > 0); tron@1317: while (--size > 0 && *src != '\0') *dst++ = *src++; tron@1317: *dst = '\0'; tron@1317: } tron@1317: tron@1317: tron@1317: char* strecat(char* dst, const char* src, const char* last) tron@1317: { tron@1317: assert(last == NULL || dst <= last); tron@1317: for (; *dst != '\0'; ++dst) tron@1317: if (dst == last) return dst; tron@1317: for (; *src != '\0' && dst != last; ++dst, ++src) *dst = *src; tron@1317: *dst = '\0'; tron@1317: return strecpy(dst, src, last); tron@1317: } tron@1317: tron@1317: tron@1317: char* strecpy(char* dst, const char* src, const char* last) tron@1317: { tron@1317: assert(last == NULL || dst <= last); tron@1317: for (; *src != '\0' && dst != last; ++dst, ++src) *dst = *src; tron@1317: *dst = '\0'; tron@1317: return dst; tron@1317: } tron@2234: tron@2234: tron@2234: char* CDECL str_fmt(const char* str, ...) tron@2234: { tron@2234: char buf[4096]; tron@2234: va_list va; tron@2234: int len; tron@2234: char* p; tron@2234: tron@2234: va_start(va, str); truelight@4370: len = vsnprintf(buf, lengthof(buf), str, va); tron@2234: va_end(va); tron@2234: p = malloc(len + 1); tron@2234: if (p != NULL) memcpy(p, buf, len + 1); tron@2234: return p; tron@2234: } Darkvater@2775: Darkvater@2775: void str_validate(char *str) Darkvater@2775: { Darkvater@2775: for (; *str != '\0'; str++) truelight@4299: if (!IsValidAsciiChar(*str, CS_ALPHANUMERAL)) *str = '?'; Darkvater@2775: } Darkvater@4200: 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@4299: * @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: */ truelight@4299: bool IsValidAsciiChar(byte key, CharSetFilter afilter) truelight@4299: { truelight@4299: bool firsttest = false; truelight@4299: truelight@4299: switch (afilter) { truelight@4299: case CS_ALPHANUMERAL: truelight@4299: firsttest = (key >= ' ' && key < 127); truelight@4299: break; truelight@4299: truelight@4300: /* We are very strict here */ truelight@4300: case CS_NUMERAL: truelight@4300: return (key >= '0' && key <= '9'); truelight@4299: truelight@4299: case CS_ALPHA: truelight@4299: default: truelight@4299: firsttest = ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z')); truelight@4299: break; truelight@4299: } truelight@4299: truelight@4300: /* Allow some special chars too that are non-ASCII but still valid (like '^' above 'a') */ truelight@4299: return (firsttest || (key >= 160 && truelight@4299: key != 0xAA && key != 0xAC && key != 0xAD && key != 0xAF && truelight@4299: key != 0xB5 && key != 0xB6 && key != 0xB7 && key != 0xB9)); truelight@4299: } truelight@4300: truelight@4300: void strtolower(char *str) truelight@4300: { truelight@4300: for (; *str != '\0'; str++) *str = tolower(*str); truelight@4300: } truelight@4370: truelight@4370: #ifdef WIN32 truelight@4370: int CDECL snprintf(char *str, size_t size, const char *format, ...) truelight@4370: { truelight@4370: va_list ap; truelight@4370: int ret; truelight@4370: truelight@4370: va_start(ap, format); truelight@4370: ret = vsnprintf(str, size, format, ap); truelight@4370: va_end(ap); truelight@4370: return ret; truelight@4370: } truelight@4370: truelight@4370: #ifdef _MSC_VER truelight@4370: int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap) truelight@4370: { truelight@4370: int ret; truelight@4370: ret = _vsnprintf(str, size, format, ap); truelight@4370: if (ret < 0) str[size - 1] = '\0'; truelight@4370: return ret; truelight@4370: } truelight@4370: #endif /* _MSC_VER */ truelight@4370: truelight@4370: #endif /* WIN32 */