export to a stream

can write a dict of simple regions now.
This commit is contained in:
Enno Rehling 2014-03-06 08:05:26 -08:00
parent 18203cfdbf
commit 40d7d236ac
1 changed files with 15 additions and 2 deletions

View File

@ -2,9 +2,10 @@
#include <kernel/types.h>
#include <kernel/region.h>
#include <kernel/terrain.h>
#include <stream.h>
#include "cJSON.h"
void export_json(const char *filename) {
void export_json(stream * out) {
region * r;
cJSON *json, *root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject());
@ -15,6 +16,18 @@ void export_json(const char *filename) {
cJSON_AddItemToObject(json, id, data = cJSON_CreateObject());
cJSON_AddNumberToObject(data, "x", r->x);
cJSON_AddNumberToObject(data, "y", r->y);
cJSON_AddStringToObject(data, "terrain", r->terrain->_name);
cJSON_AddStringToObject(data, "type", r->terrain->_name);
}
if (out) {
char *tok, *output;
output = cJSON_Print(json);
tok = strtok(output, "\n\r");
while (tok) {
if (tok[0]) {
out->api->writeln(out->handle, tok);
}
tok = strtok(NULL, "\n\r");
}
free(output);
}
}