ks_number.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 
00026 #include "ks_config.h"
00027 #include "ks_number.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 
00045 static void
00046 ks_number_bappend(ks_string_t *r, ks_base_t *b)
00047 {
00048         ks_number_t     *n = KS_CAST(ks_number, b);
00049         char             buf[64];
00050         snprintf(buf, 63, "%ld", n->number);
00051         buf[63] = '\0';
00052         ks_string_append(r, "i", 1);
00053         ks_string_append(r, buf, strlen(buf));
00054         ks_string_append(r, "e", 1);
00055 }
00056 
00060 static ks_type_t ks_type_number = {
00061         "ks_number",                    
00062         (ks_destroy_t)ks_number_free,   
00063         (ks_bappend_t)ks_number_bappend 
00064 };
00065 
00072 ks_number_t *
00073 ks_number_new(long int num)
00074 {
00075         ks_number_t *n = (ks_number_t *) ks_malloc(sizeof *n);
00076         ks_number_init(n, num);
00077         return n;
00078 }
00079 
00087 void
00088 ks_number_init(ks_number_t *n, long int num)
00089 {
00090         n->base.type = &ks_type_number;
00091         ks_number_set(n, num);
00092 }
00093 
00101 void
00102 ks_number_free(ks_number_t *n)
00103 {
00104         ks_number_fini(n);
00105         ks_free(n);
00106 }
00107 
00114 void
00115 ks_number_fini(ks_number_t *n)
00116 {
00117         n->number = 0L;
00118 }
00119 
00125 ks_type_t *
00126 ks_number_type(void)
00127 {
00128         return &ks_type_number;
00129 }
00130 
00138 void
00139 ks_number_set(ks_number_t *n, long int num)
00140 {
00141         n->number = num;
00142 }
00143 
00150 long int
00151 ks_number_get(ks_number_t *n)
00152 {
00153         return n->number;
00154 }

Generated on Wed May 9 01:01:56 2007 for libkarmaclient by  doxygen 1.5.1