static GhtErr l2g_save_tree(const Las2GhtConfig *config, Las2GhtState *state, const GhtTreePtr tree) { char ght_filename[STRSIZE]; char xml_filename[STRSIZE]; GhtWriterPtr writer; GhtSchemaPtr schema; GhtHash *hash = NULL; assert(config); assert(state); assert(tree); ght_tree_get_hash(tree, &hash); l2g_ght_file(config, state, hash, ght_filename); l2g_xml_file(config, state, hash, xml_filename); ght_info("writing tree to file %s", ght_filename); if ( ! l2g_writable(ght_filename) ) { ght_error("unable to write to '%s'", ght_filename); return GHT_ERROR; } if ( ! l2g_writable(xml_filename) ) { ght_error("unable to write to '%s'", xml_filename); return GHT_ERROR; } GHT_TRY(ght_tree_get_schema(tree, &schema)); GHT_TRY(ght_schema_to_xml_file(schema, xml_filename)); GHT_TRY(ght_writer_new_file(ght_filename, &writer)); GHT_TRY(ght_tree_write(tree, writer)); GHT_TRY(ght_writer_free(writer)); /* Increment file counter */ state->fileno++; return GHT_OK; }
static void test_ght_node_file_serialization(void) { GhtCoordinate coord; GhtNode *node, *root, *noderead; GhtErr err; GhtWriter *writer; GhtReader *reader; stringbuffer_t *sb1; GhtAttribute *attr; const char* testfile = "test.ght"; if ( fexists(testfile) ) remove(testfile); coord.x = -127.4123; coord.y = 49.23141; err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node); CU_ASSERT_EQUAL(err, GHT_OK); root = node; coord.x = -127.4122; coord.y = 49.23142; err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node); err = ght_attribute_new_from_double(schema->dims[2], 88.88, &attr); err = ght_node_add_attribute(node, attr); err = ght_node_insert_node(root, node, GHT_DUPES_YES); CU_ASSERT_EQUAL(err, GHT_OK); coord.x = -127.4122001; coord.y = 49.23142001; err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node); err = ght_attribute_new_from_double(schema->dims[2], 15.23, &attr); err = ght_node_add_attribute(node, attr); err = ght_node_insert_node(root, node, GHT_DUPES_YES); CU_ASSERT_EQUAL(err, GHT_OK); coord.x = -127.4122002; coord.y = 49.23142002; err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node); err = ght_attribute_new_from_double(schema->dims[2], 19.23, &attr); err = ght_node_add_attribute(node, attr); err = ght_node_insert_node(root, node, GHT_DUPES_YES); CU_ASSERT_EQUAL(err, GHT_OK); // sb1 = stringbuffer_create(); // err = ght_node_to_string(root, sb1, 0); // printf("\n%s\n", stringbuffer_getstring(sb1)); // stringbuffer_destroy(sb1); err = ght_writer_new_file(testfile, &writer); CU_ASSERT_EQUAL(err, GHT_OK); err = ght_node_write(root, writer); CU_ASSERT_EQUAL(err, GHT_OK); ght_writer_free(writer); err = ght_reader_new_file(testfile, schema, &reader); CU_ASSERT_EQUAL(err, GHT_OK); err = ght_node_read(reader, &noderead); CU_ASSERT_EQUAL(err, GHT_OK); ght_reader_free(reader); remove(testfile); ght_node_free(root); ght_node_free(noderead); }