timesynk/test/t2d.c

115 lines
3.2 KiB
C

/****** t2d.c
This test program converts a text file containing ts tile set declarations into various Data structures.
******/
#include <stdio.h>
#include <stdlib.h>
#include "../data.h"
int fileToMemory(char **buffer, char *file_name) {
int i, n, pos = 0;
char t_buf[16];
FILE *file = fopen(file_name, "r");
fseek(file, 0, SEEK_END);
int size = ftell(file);
fseek(file, 0, SEEK_SET);
char *new_buffer = malloc(size);
while ((n = fread(t_buf, 1, 16, file))) {
for (i = 0; i < n; i++) {
new_buffer[pos++] = t_buf[i];
}
}
fclose(file);
*buffer = new_buffer;
return size;
}
int main() {
char *buffer;
int size = fileToMemory(&buffer, "xibalba.tsd");
struct Data *data = loadDataFromMemory(buffer, size);
free(buffer);
printf("---- total tile sets: %d\n", data->set_count);
int bytes = 0;
bytes += sizeof(struct Data) + (sizeof(int) * data->size) + (sizeof(struct TileSetData)*data->size);
int count = 0;
while (count < data->size) {
int count_2 = 0;
if (data->set[count]) {
bytes += sizeof(struct TileSetData) + (sizeof(struct TileData*)*data->set[count]->size);
bytes += sizeof(struct Table);
while (count_2 < data->set[count]->size) {
if (data->set[count]->tile[count_2]) {
bytes += sizeof(struct TileData);
printf("- TILE %d:%d\n", count, count_2);
struct Table *table = data->set[count]->tile[count_2]->table;
int i = 0;
while (i < table->size) {
struct TablePair *table_pair = table->pair[i];
while (table_pair != NULL) {
bytes += sizeof(struct TablePair);
bytes += sizeof(*table_pair->value);
bytes += sizeof(*table_pair->key);
if (table_pair->type == T_STRING)
printf("%s=>%s!\n", table_pair->key, table_pair->value);
table_pair = table_pair->next;
}
i++;
}
}
count_2++;
}
}
count++;
}
printf("estimated bytes: %d\n", bytes);
printf("getTileDataByKey 'big key'\n");
struct TileData *tile = getTileDataByKey(data, "big key");
if (tile != NULL) {
printf("found %d:%d, dumping key=>value pairs\n", tile->tid, tile->id);
int i = 0;
printf("size is %d\n", tile->size);
struct Table *table = tile->table;
while (i < table->size) {
struct TablePair *table_pair = table->pair[i];
while (table_pair != NULL) {
if (table_pair->type == T_STRING)
printf("%s=>%s!\n", table_pair->key, table_pair->value);
table_pair = table_pair->next;
}
i++;
}
} else {
printf("nope!\n");
}
printf("getTileDataById '0, 1'\n");
tile = getTileDataById(data, 0, 1);
if (tile != NULL) {
printf("found %d:%d, dumping key=>value pairs\n", tile->tid, tile->id);
int i = 0;
printf("size is %d\n", tile->size);
struct Table *table = tile->table;
while (i < table->size) {
struct TablePair *table_pair = table->pair[i];
while (table_pair != NULL) {
if (table_pair->type == T_STRING)
printf("%s=>%s!\n", table_pair->key, table_pair->value);
table_pair = table_pair->next;
}
i++;
}
} else {
printf("nope!\n");
}
return 0;
}