Gwyfile Library
writegeneric.c

Example showing how to write a generic GWY file with various kinds of data types.

/*
* $Id: writegeneric.c 343 2020-07-10 09:36:51Z yeti-dn $
*
* This example shows how to write a simple generic GWY file "test.gwy"
* containing all kinds of basic datatypes.
*
* I, the copyright holder of this work, release this work into the public
* domain. This applies worldwide. In some countries this may not be legally
* possible; if so: I grant anyone the right to use this work for any purpose,
* without any conditions, unless such conditions are required by law.
*/
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <string.h>
#include "gwyfile.h"
/* Array length. */
#define AL(x) (sizeof(x)/sizeof((x)[0]))
enum {
OBJECTS_DATA_LEN = 2,
STRINGS_DATA_LEN = 3,
};
static char char_data[21];
static int32_t int32_data[6];
static int64_t int64_data[15];
static double double_data[13];
int
main(void)
{
GwyfileObject *gwyf, **objects_data;
GwyfileError *error = NULL;
char **strings_data;
unsigned int i;
/* Build the file structure. We use the _copy functions here so the
* arrays will not be consumed. */
gwyf = gwyfile_object_new("MyStruct",
gwyfile_item_new_bool("bool", true),
gwyfile_item_new_char("char", 'X'),
gwyfile_item_new_int32("int32", 42),
gwyfile_item_new_int64("int64", 1234567890ULL),
gwyfile_item_new_double("double", 3.14),
gwyfile_item_new_string_copy("string", "Shark"),
gwyfile_item_new_char_array_copy("chars", char_data, AL(char_data)),
gwyfile_item_new_int32_array_copy("int32s", int32_data, AL(int32_data)),
gwyfile_item_new_int64_array_copy("int64s", int64_data, AL(int64_data)),
gwyfile_item_new_double_array_copy("doubles", double_data, AL(double_data)),
NULL);
/* Create a string array. This is a bit more involved. Note the array
* strings_data[] will be also consumed. */
strings_data = (char**)malloc(sizeof(char*)*STRINGS_DATA_LEN);
for (i = 0; i < STRINGS_DATA_LEN; i++) {
strings_data[i] = (char*)malloc(8);
snprintf(strings_data[i], 8, "string%u", i);
}
gwyfile_object_add(gwyf, gwyfile_item_new_string_array("strings", strings_data, STRINGS_DATA_LEN));
/* Create an object array. This is a bit more involved. Note the array
* objects_data[] will be also consumed. */
objects_data = (GwyfileObject**)malloc(sizeof(GwyfileObject*)*OBJECTS_DATA_LEN);
for (i = 0; i < OBJECTS_DATA_LEN; i++)
objects_data[i] = gwyfile_object_new("MySubStruct", gwyfile_item_new_int32("i", i), NULL);
gwyfile_object_add(gwyf, gwyfile_item_new_object_array("objects", objects_data, OBJECTS_DATA_LEN));
/* Write the top-level object to a file. */
if (!gwyfile_write_file(gwyf, "test.gwy", &error)) {
fprintf(stderr, "Cannot write test.gwy: %s\n", error->message);
exit(EXIT_FAILURE);
}
/* Clean-up. With the default ownership transfer rules, this call nicely
* frees all the created objects and items. */
return 0;
}
/* vim: set cin et ts=4 sw=4 columns=120 tw=119 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
bool gwyfile_write_file(GwyfileObject *object, const char *filename, GwyfileError **error)
Writes a GWY file to a named file.
Definition: gwyfile.c:331
void gwyfile_error_clear(GwyfileError **error)
Clears a GwyfileError.
Definition: gwyfile.c:8990
GwyfileItem * gwyfile_item_new_bool(const char *name, bool value)
Creates a new boolean GWY file item.
Definition: gwyfile.c:5406
GwyfileItem * gwyfile_item_new_char_array_copy(const char *name, const char *value, uint32_t array_length)
Creates a new character array GWY file item.
Definition: gwyfile.c:6008
GwyfileItem * gwyfile_item_new_char(const char *name, char value)
Creates a new character GWY file item.
Definition: gwyfile.c:5456
GwyfileItem * gwyfile_item_new_double_array_copy(const char *name, const double *value, uint32_t array_length)
Creates a new double array GWY file item.
Definition: gwyfile.c:6697
GwyfileItem * gwyfile_item_new_double(const char *name, double value)
Creates a new double GWY file item.
Definition: gwyfile.c:5606
GwyfileItem * gwyfile_item_new_int32_array_copy(const char *name, const int32_t *value, uint32_t array_length)
Creates a new 32bit integer array GWY file item.
Definition: gwyfile.c:6238
GwyfileItem * gwyfile_item_new_int32(const char *name, int32_t value)
Creates a new 32bit integer GWY file item.
Definition: gwyfile.c:5506
GwyfileItem * gwyfile_item_new_int64_array_copy(const char *name, const int64_t *value, uint32_t array_length)
Creates a new 64bit integer array GWY file item.
Definition: gwyfile.c:6466
GwyfileItem * gwyfile_item_new_int64(const char *name, int64_t value)
Creates a new 64bit integer GWY file item.
Definition: gwyfile.c:5556
GwyfileItem * gwyfile_item_new_object_array(const char *name, GwyfileObject **value, uint32_t array_length)
Creates a new object array GWY file item.
Definition: gwyfile.c:7155
GwyfileItem * gwyfile_item_new_string_array(const char *name, char **value, uint32_t array_length)
Creates a new string array GWY file item.
Definition: gwyfile.c:6867
GwyfileItem * gwyfile_item_new_string_copy(const char *name, const char *value)
Creates a new string GWY file item.
Definition: gwyfile.c:5712
GwyfileObject * gwyfile_object_new(const char *name,...)
Creates a new GWY file object.
Definition: gwyfile.c:607
bool gwyfile_object_add(GwyfileObject *object, GwyfileItem *item)
Adds an data item to a GWY file data object.
Definition: gwyfile.c:4652
void gwyfile_object_free(GwyfileObject *object)
Frees a GWY file data object.
Definition: gwyfile.c:4577
Gwyfile Library.
Detailed information about an error.
Definition: gwyfile.h:131
char * message
Text message describing the error. It is allocated when the error struct is created and freed with gw...
Definition: gwyfile.h:134