diff options
Diffstat (limited to 'src/string.c')
-rw-r--r-- | src/string.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/string.c b/src/string.c new file mode 100644 index 0000000..85a5767 --- /dev/null +++ b/src/string.c @@ -0,0 +1,74 @@ +#include <stdio.h> +#include <string.h> + +rcc_string rccStringInit(rcc_language_id language_id, const char *buf, int len, int *rlen) { + rcc_string_header header = {RCC_STRING_MAGIC, language_id}; + + len = STRNLEN(buf, len); + + res = (char*)malloc(len+sizeof(rcc_string_header)+1); + if (!res) return NULL; + + strncpy(res + sizeof(rcc_string_header), buf, len); + res[sizeof(rcc_string_header) + 1 + len] = 0; + + memcpy(res, &header, sizeof(rcc_string_header)); + + if (rlen) *rlen = len + sizeof(rcc_string_header); + return (rcc_string)res; +} + +void rccStringFree(rcc_string str) { + if (str) free(str); +} + +rcc_language_id rccStringCheck(const rcc_string str) { + int len; + rcc_string_header *header; + + len = strlen(str); + + if ((!str)||(len<=sizeof(unsigned int))||(*((unsigned int*)(str))!=RCC_STRING_MAGIC)) return 0; + + header = (rcc_string_header*)(str); + return header->language_id; +} + +const char *rccStringGet(const rcc_string str) { + if (rccStringCheck(str)) return str + sizeof(rcc_string_header); + return (const char *)str; +} + +char *rccStringExtract(const rcc_string buf, int len, int *rlen) { + char *res; + + len = STRNLEN(buf, len) - sizeof(rcc_string_header); + if (len<0) return NULL; + + res = (char*)malloc(len+1); + if (!res) return NULL; + + strncpy(res, buf + sizeof(rcc_string_header), len); + res[len] = 0; + + if (rlen) *rlen = len; + + return res; +} + +char *rccStringCmp(const rcc_string str1, const rcc_string str2) { + return strcmp(rccStringGet(str1), rccStringGet(str2)); +} + +char *rccStringNCmp(const rcc_string str1, const rcc_string str2, size_t n) { + return strncmp(rccStringGet(str1), rccStringGet(str2), n); +} + +char *rccStringCaseCmp(const rcc_string str1, const rcc_string str2) { + return strcasecmp(rccStringGet(str1), rccStringGet(str2)); +} + +char *rccStringNCaseCmp(const rcc_string str1, const rcc_string str2, size_t n) { + return strncasecmp(rccStringGet(str1), rccStringGet(str2), n); +} + |