Skip to content

Commit

Permalink
Refactored duplicate code and removed some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey White committed Apr 10, 2024
1 parent 6c8509a commit 4463bf1
Showing 1 changed file with 46 additions and 51 deletions.
97 changes: 46 additions & 51 deletions general/g.mapsets/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,40 @@
#include "local_proto.h"
#include <grass/parson.h>

// Function to initialize a JSON object with a mapsets array
static JSON_Object *initialize_json_object()
{
JSON_Value *root_value = json_value_init_object();
if (!root_value) {
G_fatal_error(_("Failed to initialize JSON object. Out of memory?"));
}

JSON_Object *root_object = json_value_get_object(root_value);
json_object_set_value(root_object, "mapsets", json_value_init_array());

JSON_Array *mapsets = json_object_get_array(root_object, "mapsets");
if (!mapsets) {
json_value_free(root_value);
G_fatal_error(_("Failed to initialize mapsets array. Out of memory?"));
}

return root_object;
}

// Function to serialize and print JSON object
static void serialize_and_print_json_object(JSON_Value *root_value)
{
char *serialized_string = json_serialize_to_string_pretty(root_value);
if (!serialized_string) {
json_value_free(root_value);
G_fatal_error(_("Failed to serialize JSON to pretty format."));
}

fprintf(stdout, "%s\n", serialized_string);
json_free_serialized_string(serialized_string);
json_value_free(root_value);
}

void list_available_mapsets(const char **mapset_name, int nmapsets,
const char *fs)
{
Expand Down Expand Up @@ -35,70 +69,31 @@ void list_accessible_mapsets(const char *fs)
fprintf(stdout, "\n");
}

// Lists all accessible mapsets in JSON format
void list_accessible_mapsets_json()
{
const char *name;
char *serialized_string = NULL;
JSON_Value *root_value = NULL;
JSON_Object *root_object = NULL;
JSON_Array *mapsets = NULL;
JSON_Object *root_object = initialize_json_object();
JSON_Array *mapsets = json_object_get_array(root_object, "mapsets");

root_value = json_value_init_object();
root_object = json_value_get_object(root_value);

// Create and add mapsets array to root object
json_object_set_value(root_object, "mapsets", json_value_init_array());
mapsets = json_object_get_array(root_object, "mapsets");

// Check that memory was allocated to root json object and array
if (root_value == NULL || mapsets == NULL) {
G_fatal_error(_("Failed to initialize JSON. Out of memory?"));
}

// Add mapsets to mapsets array
for (int n = 0; (name = G_get_mapset_name(n)); n++) {
// Append mapset name to mapsets array
json_array_append_string(mapsets, name);
}

// Serialize root object to string and print it to stdout
serialized_string = json_serialize_to_string_pretty(root_value);
puts(serialized_string);

// Free memory
json_free_serialized_string(serialized_string);
json_value_free(root_value);
serialize_and_print_json_object(
json_object_get_wrapping_value(root_object));
}

void list_avaliable_mapsets_json(const char **mapset_name, int nmapsets)
// Lists available mapsets from a provided array in JSON format
void list_avaliable_mapsets_json(const char **mapset_names, int nmapsets)
{
char *serialized_string = NULL;
JSON_Value *root_value = NULL;
JSON_Object *root_object = NULL;
JSON_Array *mapsets = NULL;
JSON_Object *root_object = initialize_json_object();
JSON_Array *mapsets = json_object_get_array(root_object, "mapsets");

root_value = json_value_init_object();
root_object = json_value_get_object(root_value);

// Create mapsets array
json_object_set_value(root_object, "mapsets", json_value_init_array());
mapsets = json_object_get_array(root_object, "mapsets");

// Check that memory was allocated to root json object and array
if (root_value == NULL || mapsets == NULL) {
G_fatal_error(_("Failed to initialize JSON. Out of memory?"));
}

// Append mapsets to mapsets array
for (int n = 0; n < nmapsets; n++) {
json_array_append_string(mapsets, mapset_name[n]);
json_array_append_string(mapsets, mapset_names[n]);
}

// Serialize root object to string and print it to stdout
serialized_string = json_serialize_to_string_pretty(root_value);
puts(serialized_string);

// Free memory
json_free_serialized_string(serialized_string);
json_value_free(root_value);
serialize_and_print_json_object(
json_object_get_wrapping_value(root_object));
}

0 comments on commit 4463bf1

Please sign in to comment.