k-dot-p 0.1

key_values.c++

Go to the documentation of this file.
00001 /*
00002  *
00003 
00004 
00005     Copyright (C) 2009 Joseph Pingenot
00006 
00007     This program is free software: you can redistribute it and/or modify
00008     it under the terms of the GNU Affero General Public License as published by
00009     the Free Software Foundation, either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     This program 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
00015     GNU Affero General Public License for more details.
00016 
00017     You should have received a copy of the GNU Affero General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 
00020 */
00021 
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <libmodelxx/shapes.h++>
00025 #include <libmodelxx/key_values.h++>
00026 
00027 namespace kdotp {
00028   namespace libmodelxx {
00029     namespace grid {
00030 
00031 
00032 
00033 
00034 
00035 
00036       void print_vector(GList* l) {
00037   GList* i;
00038   fprintf(stderr, "<");
00039   for(i=l; l!=NULL; l=l->next) {
00040     fprintf(stderr, "%g ", *((double*)(l->data)));
00041   }
00042   fprintf(stderr, ">\n");
00043       }
00044 
00045       void print_vector_list(GList* l) {
00046   GList* i;
00047   fprintf(stderr, "< ");
00048   for(i=l; l!=NULL; l=l->next) {
00049     print_vector((GList*)(l->data));
00050   }
00051   fprintf(stderr, " >\n");
00052       }
00053 
00054       void print_key_value(const struct key_value* kvi) {
00055   if(kvi->t == STRING) {
00056     fprintf(stderr, "(%s=%s) ", kvi->key, kvi->value.s);
00057   } else if(kvi->t == INT) {
00058     fprintf(stderr, "(%s=%d) ", kvi->key, kvi->value.i);
00059   } else if(kvi->t == VECTOR) {
00060     fprintf(stderr, "(%s=", kvi->key);
00061     print_vector(kvi->value.l);
00062   } else if(kvi->t == VECTOR_SET) {
00063     fprintf(stderr, "(%s=", kvi->key);
00064     print_vector_list(kvi->value.l);
00065   } else if(kvi->t == SHAPE) {
00066     fprintf(stderr, "(<shape>)\n");
00067   } else {
00068     /*Double*/
00069     fprintf(stderr, "(%s=%g) ", kvi->key, kvi->value.d);
00070   }
00071       }
00072 
00073       void print_key_value_list(GList* l) {
00074   GList* i;
00075   for(i=l; l!=NULL; l=l->next) {
00076     print_key_value((struct key_value *)(l->data));
00077   }
00078       }
00079       
00080 
00081       void key_value_free(struct key_value *kv) {
00082   GList* l;
00083   GList* ll;
00084   if((kv->key) != NULL) free(kv->key);
00085   switch(kv->t) {
00086   case STRING:
00087     free(kv->value.s);
00088     break;
00089   case VECTOR:
00090     for(ll=kv->value.l; ll != NULL; ll=ll->next) {
00091       free((double*)ll->data);
00092     }
00093     break;
00094   case VECTOR_SET:
00095     for(l=kv->value.l; l!=NULL; l=l->next) {
00096       for(ll=(GList*)(l->data); ll != NULL; ll=ll->next) {
00097         free((double*)ll->data);
00098       }
00099     }
00100     break;
00101   case SHAPE:
00102     shape_tree_free(kv->value.st);
00103     break;
00104   case INT:
00105   case FLOAT:
00106     /*These aren't pointers; nothing to de-allocate*/
00107     break;
00108   default:
00109     fprintf(stderr, "kdotp::libmodelxx::grid::key_value_free: WARNING: didn't recognize the value in key_value (numeric %d)!\n", kv->t);
00110   }
00111       }
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127     }
00128   }
00129 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines