Skip to content

Commit

Permalink
created import json command
Browse files Browse the repository at this point in the history
Merge branch 'enh/import-json' into main
  • Loading branch information
mdhender committed Oct 29, 2023
2 parents b0f04c4 + ae50e25 commit 68b14d9
Show file tree
Hide file tree
Showing 13 changed files with 1,137 additions and 335 deletions.
19 changes: 6 additions & 13 deletions src/cjson/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,20 @@ int jsonGetInt(cJSON *obj, const char *property) {
}

// jsonGetString should copy up to maxLength - 1 bytes.
const char *jsonGetString(cJSON *obj, const char *property, int maxLength) {
static char buffer[1024];
if (maxLength > 1023) {
fprintf(stderr, "jsonGetString: maxLength %d exceeds limit\n", maxLength);
exit(2);
}
void jsonGetString(cJSON *obj, const char *property, char *dst, int size) {
memset(dst, 0, size);
cJSON *item = cJSON_GetObjectItemCaseSensitive(obj, property);
if (item == 0) {
fprintf(stderr, "property: %s: missing\n", property);
fprintf(stderr, "property: %s: must not be null\n", property);
exit(2);
} else if (!cJSON_IsString(item)) {
fprintf(stderr, "property: %s: not a string\n", property);
exit(2);
}
if (strlen(item->valuestring) > maxLength) {
fprintf(stderr, "jsonGetString: strlen %d exceeds limit %d\n", (int) strlen(item->valuestring) + 1, maxLength);
} else if (strlen(item->valuestring)+1 > size) {
fprintf(stderr, "jsonGetString: strlen %d exceeds limit %d\n", (int) strlen(item->valuestring) + 1, size);
exit(2);
}
strcpy(buffer, item->valuestring);
buffer[maxLength - 1] = 0;
return buffer;
strlcpy(dst, item->valuestring, size);
}


Expand Down
4 changes: 3 additions & 1 deletion src/cjson/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ int jsonGetBool(cJSON *obj, const char *property);

int jsonGetInt(cJSON *obj, const char *property);

const char *jsonGetString(cJSON *obj, const char *property, int maxLength);
// jsonGetString uses `strlcpy` to copy the string to the destination.
// size is the size of the destination, including space for the nul byte.
void jsonGetString(cJSON *obj, const char *property, char *dst, int size);

cJSON *jsonParseFile(const char *name);

Expand Down
19 changes: 9 additions & 10 deletions src/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,23 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "export.h"
#include "marshal.h"
#include "galaxyio.h"
#include "namplaio.h"
#include "planetio.h"
#include "shipio.h"
#include "speciesio.h"
#include "stario.h"
#include "namplavars.h"
#include "shipvars.h"
#include "cjson/helpers.h"

int exportToJson(int argc, char *argv[]);

static int exportToJson();

int exportCommand(int argc, char *argv[]) {
const char *cmdName = argv[0];

int exportCommand(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
// fprintf(stderr, "fh: %s: argc %2d argv '%s'\n", cmdName, i, argv[i]);
char *opt = argv[i];
char *val = NULL;
for (val = opt; *val != 0; val++) {
Expand All @@ -55,7 +54,7 @@ int exportCommand(int argc, char *argv[]) {
fprintf(stderr, "usage: export json\n");
return 2;
} else if (strcmp(opt, "json") == 0 && val == NULL) {
return exportToJson(argc - i, argv + i);
return exportToJson();
} else {
fprintf(stderr, "fh: export: unknown option '%s'\n", opt);
return 2;
Expand All @@ -66,21 +65,21 @@ int exportCommand(int argc, char *argv[]) {
}


int exportToJson(int argc, char *argv[]) {
int exportToJson(void) {
get_galaxy_data();
get_star_data();
get_planet_data();
get_species_data();

cJSON *root = marshalGalaxy();
cJSON *root = marshalGalaxyFile();
if (root == 0) {
fprintf(stderr, "error: there was an error converting galaxy data to json\n");
exit(2);
}
jsonWriteFile(root, "galaxy", "galaxy.json");
cJSON_Delete(root);

root = marshalSystems();
root = marshalSystemsFile();
if (root == 0) {
fprintf(stderr, "error: there was an error converting systems data to json\n");
exit(2);
Expand All @@ -90,7 +89,7 @@ int exportToJson(int argc, char *argv[]) {

for (int i = 0; i < MAX_SPECIES; i++) {
if (data_in_memory[i]) {
root = marshalSpecies(&spec_data[i], namp_data[i], ship_data[i]);
root = marshalSpeciesFile(&spec_data[i], namp_data[i], ship_data[i]);
if (root == 0) {
fprintf(stderr, "error: there was an error converting species data to json\n");
exit(2);
Expand Down
75 changes: 66 additions & 9 deletions src/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,25 @@

#include <stdio.h>
#include <string.h>
#include "data.h"
#include "enginevars.h"
#include <sys/stat.h>
#include "import.h"
#include "unmarshal.h"
#include "galaxyio.h"
#include "stario.h"
#include "planetio.h"
#include "speciesio.h"
#include "cjson/helpers.h"
#include "namplavars.h"
#include "shipvars.h"


int importFromJson(int doTest);


int importCommand(int argc, char *argv[]) {
int doImportJson = FALSE;
int doTest = FALSE;

for (int i = 1; i < argc; i++) {
char *opt = argv[i];
char *val = NULL;
Expand All @@ -41,21 +54,65 @@ int importCommand(int argc, char *argv[]) {
}

if (strcmp(opt, "--help") == 0 || strcmp(opt, "-h") == 0 || strcmp(opt, "-?") == 0) {
fprintf(stderr, "usage: import options...\n");
fprintf(stderr, " opt: json import json data\n");
fprintf(stderr, "usage: import json\n");
return 2;
} else if (strcmp(opt, "-v") == 0 && val == NULL) {
verbose_mode = TRUE;
} else if (strcmp(opt, "-t") == 0 && val == NULL) {
doTest = TRUE;
} else if (strcmp(opt, "--test") == 0 && val == NULL) {
test_mode = TRUE;
doTest = TRUE;
} else if (strcmp(opt, "json") == 0 && val == NULL) {
fprintf(stderr, "error: `import json` is not implemented\n");
return 2;
doImportJson = TRUE;
} else {
fprintf(stderr, "import: unknown option '%s%s%s'\n", opt, val ? "=" : "", val);
return 2;
}
}

if (doImportJson) {
return importFromJson(doTest);
}

return 0;
}

int importFromJson(int doTest) {
get_galaxy_data();
get_star_data();
get_planet_data();
get_species_data();

cJSON *root = jsonParseFile("galaxy.json");
unmarshalGalaxyFile(root, &galaxy);
cJSON_Delete(root);

root = jsonParseFile("systems.json");
unmarshalSystemsFile(root, star_base, planet_base);
cJSON_Delete(root);

for (int i = 0; i < MAX_SPECIES; i++) {
if (data_in_memory[i]) {
char filename[128];
sprintf(filename, "species.%03d.json", i + 1);
struct stat sb;
if (stat(filename, &sb) != 0) {
// assume that file is missing
printf(" warn: missing species file '%s'\n", filename);
continue;
}
root = jsonParseFile(filename);
unmarshalSpeciesFile(root, &spec_data[i], namp_data[i], ship_data[i]);
}
}

if (doTest) {
printf(" test: changes not saved\n");
return 0;
}

save_galaxy_data();
save_star_data();
save_planet_data();
save_species_data();

return 2;
}
Loading

0 comments on commit 68b14d9

Please sign in to comment.