Main Page | Data Structures | Directories | File List | Data Fields | Globals

ks_string.c

Go to the documentation of this file.
00001 /*
00002  * libkarmaclient - A C Library to the Karmasphere Reputation Server
00003  * Copyright (C) 2006 Karmasphere <http://www.karmasphere.com/>
00004  *  - Shevek <shevek@karmasphere.com>
00005  *  - Dave Stewart <dave.stewart@karmasphere.com>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020  */
00021 
00027 #include "ks_config.h"
00028 #include "ks_string.h"
00029 
00030 #ifdef HAVE_STRING_H
00031 #include <string.h>
00032 #endif
00033 
00034 #ifdef HAVE_STDIO_H
00035 #include <stdio.h>
00036 #endif
00037 
00047 static void
00048 ks_string_bappend(ks_string_t *r, ks_base_t *b)
00049 {
00050         ks_string_t     *u = KS_CAST(ks_string, b);
00051         char             buf[64];
00052         snprintf(buf, 63, "%d", ks_string_length(u));
00053         buf[63] = '\0';
00054         ks_string_append(r, buf, strlen(buf));
00055         ks_string_append(r, ":", 1);
00056         ks_string_append(r, ks_string_get(u), ks_string_length(u));
00057 }
00058 
00062 static ks_type_t ks_type_string = {
00063         "ks_string",                                    
00064         (ks_destroy_t)ks_string_free,   
00065         (ks_bappend_t)ks_string_bappend 
00066 };
00067 
00073 ks_type_t *
00074 ks_string_type(void)
00075 {
00076         return &ks_type_string;
00077 }
00078 
00085 ks_string_t *
00086 ks_string_new(const char *data, int len)
00087 {
00088         ks_string_t     *r = (ks_string_t *)ks_malloc(sizeof(ks_string_t));
00089         r->base.type = &ks_type_string;
00090         r->length = 0;
00091         r->size = -1;   /* Watch carefully. */
00092         r->data = NULL;
00093         ks_string_append(r, data, len);
00094         return r;
00095 }
00096 
00104 void
00105 ks_string_free(ks_string_t *r)
00106 {
00107         ks_free(r->data);
00108         ks_free(r);
00109 }
00110 
00117 int
00118 ks_string_length(ks_string_t *r)
00119 {
00120         return r->length;
00121 }
00122 
00131 void
00132 ks_string_append(ks_string_t *r, const char *data, int len)
00133 {
00134         if (r->length + len > r->size) {
00135                 /* If this is changed, the -1 in ks_string_new will be wrong. */
00136                 r->size = r->size + r->size;
00137                 if (r->length + len > r->size)
00138                         r->size = r->length + len;
00139                 r->data = (char *) ks_realloc(r->data, r->size + 1);
00140         }
00141         memcpy(&(r->data[r->length]), data, len);
00142         r->length += len;
00143         r->data[r->length] = '\0';
00144 }
00145 
00155 void
00156 ks_string_set(ks_string_t *r, const char *data, int len)
00157 {
00158         r->length = 0;
00159         ks_string_append(r, data, len);
00160 }
00161 
00168 const char *
00169 ks_string_get(ks_string_t *r)
00170 {
00171         return r->data;
00172 }
00173 
00182 int
00183 ks_string_equals(ks_string_t *r, ks_string_t *s)
00184 {
00185         int     len = ks_string_length(r);
00186         if (ks_string_length(s) != len)
00187                 return 0;
00188         if (strcmp(ks_string_get(r), ks_string_get(s)) != 0)
00189                 return 0;
00190         return 1;
00191 }

Generated on Tue Aug 7 21:11:30 2007 for libkarmaclient by  doxygen 1.4.4