From: Koichi KAMICHI Date: Tue, 20 Apr 2004 05:27:30 +0000 (+0000) Subject: Add original string functions. X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1623ac415f4906e038d27b5c84d72ce1a168c54b;p=chise%2Fkage.git Add original string functions. --- diff --git a/kagecgi/kagestr.c b/kagecgi/kagestr.c new file mode 100755 index 0000000..4d96b1e --- /dev/null +++ b/kagecgi/kagestr.c @@ -0,0 +1,68 @@ +//kagestr.c +// + +#include "sysdep.h" +#include "kagestr.h" + +KGString* kg_string_new(char *initString){ + KGString *tmp; + tmp = (KGString *)malloc(sizeof(KGString)); + if(initString == NULL){ + tmp->len = 0; + tmp->str = (char *)malloc(0); + } + else{ + tmp->len = strlen(initString); + tmp->str = (char *)malloc(tmp->len + 1); + strcpy(tmp->str, initString); + } + return tmp; +} + +KGString* kg_string_set_size(KGString *target, int newSize){ + target->len = newSize; + target->str = (char *)realloc((char *)target->str, target->len + 1); + target->str[target->len] = '\0'; + return target; +} + +KGString* kg_string_append_len(KGString *target, char *appendString, int length){ + target->len = target->len + length; + target->str = (char *)realloc((char *)target->str, target->len + 1); + strncat(target->str, appendString, length); + return target; +} + +KGString* kg_string_append(KGString *target, char *appendString){ + target->len = target->len + strlen(appendString); + target->str = (char *)realloc((char *)target->str, target->len + 1); + strcat(target->str, appendString); + return target; +} + +KGString* kg_string_erase(KGString *target, int startPoint, int length){ + char *tmp; + int oldLength; + oldLength = target->len; + target->len = target->len - length; + tmp = (char *)malloc(target->len + 1); + strncpy(tmp, target->str, startPoint); + tmp[startPoint] = '\0'; + if(oldLength > startPoint + length) strcat(tmp, target->str + startPoint + length); + target->str = (char *)realloc((char *)target->str, target->len + 1); + strcpy(target->str, tmp); + free(tmp); + return target; +} + +KGString* kg_string_assign(KGString *target, char *assignString){ + target->len = strlen(assignString); + target->str = (char *)realloc((char *)target->str, target->len + 1); + strcpy(target->str, assignString); + return target; +} + + + + + diff --git a/kagecgi/kagestr.h b/kagecgi/kagestr.h new file mode 100755 index 0000000..5547b60 --- /dev/null +++ b/kagecgi/kagestr.h @@ -0,0 +1,22 @@ +//kagestr.h +// + +#ifndef _KAGESTR_H_ +#define _KAGESTR_H_ + +typedef char kgchar; + +typedef struct _KGString { + char *str; + int len; +} KGString; + +KGString* kg_string_new(char*); +KGString* kg_string_set_size(KGString*, int); +KGString* kg_string_append_len(KGString*, char*, int); +KGString* kg_string_append(KGString*, char*); +KGString* kg_string_erase(KGString*, int, int); +KGString* kg_string_assign(KGString*, char*); + +#endif +