ks_response.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_response.h"
00028 #include "ks_bparse.h"
00029 #include "ks_number.h"
00030 
00031 #ifdef HAVE_STRING_H
00032 #include <string.h>
00033 #endif
00034 
00038 static void
00039 ks_response_bappend(ks_string_t *r, ks_base_t *b)
00040 {
00041         ks_response_t   *e = KS_CAST(ks_response, b);
00042         if (e->response != NULL)
00043                 ks_bappend(r, (ks_base_t *)e->response);
00044         else if (e->error != NULL)
00045                 ks_bappend(r, (ks_base_t *)e->error);
00046         else
00047                 ks_string_append(r, "4:null", 6);
00048 }
00049 
00053 static ks_type_t ks_type_response = {
00054         "ks_response",                                    
00055         (ks_destroy_t)ks_response_free,   
00056         (ks_bappend_t)ks_response_bappend 
00057 };
00058 
00064 ks_type_t *
00065 ks_response_type(void)
00066 {
00067         return &ks_type_response;
00068 }
00069 
00076 static ks_response_t *
00077 ks_response_new()
00078 {
00079         ks_response_t   *e = (ks_response_t *)ks_malloc(sizeof(ks_response_t));
00080         e->base.type = &ks_type_response;
00081         e->response = NULL;
00082         e->error = NULL;
00083         return e;
00084 }
00085 
00089 ks_response_t *
00090 ks_response_bparse(const char *data, int len)
00091 {
00092         ks_base_t               *b = ks_bparse(data, len);
00093         ks_response_t   *e = NULL;
00094 
00095         if (b == NULL) {
00096                 /* Error already reported by parser. */
00097         }
00098         else if (b->type == ks_assoc_type()) {
00099                 e = ks_response_new();
00100                 e->response = KS_CAST(ks_assoc, b);
00101         }
00102         else if (b->type == ks_string_type()) {
00103                 e = ks_response_new();
00104                 e->error = KS_CAST(ks_string, b);
00105         }
00106         else {
00107                 ks_error("Returned packet from server was not ks_assoc: %s",
00108                                                 b->type->name);
00109                 ks_destroy(b);
00110         }
00111 
00112         return e;
00113 }
00114 
00122 void
00123 ks_response_free(ks_response_t *e)
00124 {
00125         if (e->response != NULL)
00126                 ks_assoc_free(e->response);
00127         if (e->error != NULL)
00128                 ks_string_free(e->error);
00129         ks_free(e);
00130 }
00131 
00139 ks_string_t *
00140 ks_response_error(ks_response_t *e)
00141 {
00142         ks_string_t     *s;
00143         ks_base_t       *b;
00144 
00145         if (e->error != NULL)
00146                 return e->error;
00147 
00148         if (e->response != NULL) {
00149                 b = ks_assoc_get(e->response, "error", 5);
00150                 if (b != NULL) {
00151                         s = ks_response_message(e);
00152                         if (s != NULL)
00153                                 return s;
00154                         e->error = ks_string_new("Unknown error", 13);
00155                         return e->error;
00156                 }
00157         }
00158 
00159         return NULL;
00160 }
00161 
00168 ks_string_t *
00169 ks_response_message(ks_response_t *e)
00170 {
00171         ks_base_t       *b = ks_assoc_get(e->response, "message", 7);
00172         if (b != NULL)
00173                 return KS_CAST(ks_string, b);
00174         return NULL;
00175 }
00176 
00183 ks_string_t *
00184 ks_response_id(ks_response_t *e)
00185 {
00186         ks_base_t       *b;
00187         if (e->response == NULL)
00188                 return NULL;
00189         b = ks_assoc_get(e->response, "_", 1);
00190         if (b == NULL)
00191                 return NULL;
00192         return KS_CAST(ks_string, b);
00193 }
00194 
00202 long
00203 ks_response_time(ks_response_t *e)
00204 {
00205         ks_number_t     *n;
00206         ks_base_t       *b;
00207         if (e->response == NULL)
00208                 return -1;
00209         b = ks_assoc_get(e->response, "t", 1);
00210         if (b == NULL)
00211                 b = ks_assoc_get(e->response, "time", 4);
00212         if (b == NULL)
00213                 return -1;
00214         n = KS_CAST(ks_number, b);
00215         return ks_number_get(n);
00216 }
00217 
00225 ks_assoc_t *
00226 ks_response_combinations(ks_response_t *e)
00227 {
00228         ks_base_t       *b;
00229         if (e->response == NULL)
00230                 return NULL;
00231         b = ks_assoc_get(e->response, "c", 1);
00232         if (b == NULL)
00233                 b = ks_assoc_get(e->response, "combiners", 9);
00234         if (b == NULL)
00235                 return NULL;
00236         return KS_CAST(ks_assoc, b);
00237 }
00238 
00248 /* XXX If key is null, then get the first one? */
00249 ks_combination_t *
00250 ks_response_combination(ks_response_t *e, const char *key)
00251 {
00252         ks_assoc_t      *s = ks_response_combinations(e);
00253         ks_base_t       *b;
00254         if (s == NULL)
00255                 return NULL;
00256         b = ks_assoc_get(s, key, strlen(key));
00257         if (b == NULL)
00258                 return NULL;
00259         return KS_CAST(ks_assoc, b);
00260 }
00261 
00270 int
00271 ks_response_combinations_size(ks_response_t *e)
00272 {
00273         ks_assoc_t      *s = ks_response_combinations(e);
00274         if (s == NULL)
00275                 return 0;
00276         return ks_assoc_size(s);
00277 }
00278 
00286 static ks_array_t *
00287 ks_response_facts(ks_response_t *e)
00288 {
00289         ks_base_t       *b;
00290         if (e->response == NULL)
00291                 return NULL;
00292         b = ks_assoc_get(e->response, "f", 5);
00293         if (b == NULL)
00294                 b = ks_assoc_get(e->response, "facts", 5);
00295         if (b == NULL)
00296                 return NULL;
00297         return KS_CAST(ks_array, b);
00298 }
00299 
00307 int
00308 ks_response_facts_length(ks_response_t *e)
00309 {
00310         ks_array_t      *a = ks_response_facts(e);
00311         if (a == NULL)
00312                 return 0;
00313         return ks_array_length(a);
00314 }
00315 
00324 ks_fact_t *
00325 ks_response_fact(ks_response_t *e, int idx)
00326 {
00327         ks_array_t      *a = ks_response_facts(e);
00328         if (a == NULL)
00329                 return NULL;
00330         return (ks_fact_t *)ks_array_get(a, idx);
00331 }

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