From 3aa2acb1aa6931d9a5cab87fe9bef94086e25d16 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Thu, 16 Jun 2005 23:14:30 +0000 Subject: Initial Import --- src/string.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/string.c (limited to 'src/string.c') 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 +#include + +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); +} + -- cgit v1.2.3