timesynk/test/conf.c

62 lines
1.7 KiB
C

#include <stdlib.h>
#include "../common/data.h"
int dumpTable(struct Table *table, int depth) {
int i = 0;
char i_depth[31]; // just cap it for now
i_depth[0] = '\0';
while(i<depth) {
i_depth[i] = '\t';
i_depth[i+1] = '\0';
i++;
}
i = 0;
while (i < table->size) {
struct TablePair *entry = table->pair[i];
// use a loop here, as multiple TablePairs can occupy the same index
while (entry != NULL) {
switch(entry->type) {
case T_INT:
printf("%s%s=>%d(int)\n", i_depth, entry->key, *(int*)entry->value);
break;
case T_FLOAT:
printf("%s%s=>%f(float)\n", i_depth, entry->key, *(float*)entry->value);
break;
case T_CHAR:
printf("%s%s=>%c(char)\n", i_depth, entry->key, *(char *)entry->value);
break;
case T_STRING:
printf("%s%s=>%s(string)\n", i_depth, entry->key, (char *)entry->value);
break;
case T_TABLE:
printf("%s%s=>\n", i_depth, entry->key);
dumpTable(entry->value, depth+1);
break;
}
entry = entry->next;
}
i++;
}
return 0;
}
int main(int argc, char **argv) {
if (argc <= 1) {
printf("Usage: %s config_file\n", argv[0]);
return;
}
int buffer_size = 0;
char *memory;
if ((buffer_size = fileToMemory(&memory, argv[1])) >= 0) {
struct Table *config_table = newTable(32);
int *offset;
offset = 0;
printf("loading in config file...\n");
loadConfig_r(config_table, memory, buffer_size, &offset);
printf("loaded, spitting out contents:\n\n");
dumpTable(config_table, 0);
} else {
printf("ERR: couldn't open file for reading\n");
}
}