Example showing how to read a generic GWY file with arbitrary data types. It dumps the hierarchical file structure to the standard output.
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <string.h>
#define INDENT " "
#ifdef _WIN32
#define llformat "I64"
#else
#define llformat "ll"
#endif
static struct {
const char *name;
}
typenames[] = {
};
static void dump_object_recursively(GwyfileObject *object,
int level);
static void dump_item_recursively (const GwyfileItem *item,
void *user_data);
int
main(int argc, char *argv[])
{
GwyfileObject *gwyf;
bool using_stdin = false;
if (argc != 2) {
printf("readgeneric FILE.gwy\n"
"Read a generic GWY file and print its structure.\n"
"Pass \"-\" as the filename to read the standard input.\n");
exit(argc == 1 ? EXIT_SUCCESS : EXIT_FAILURE);
}
if (strcmp(argv[1], "-") == 0) {
using_stdin = true;
}
else
if (!gwyf) {
fprintf(stderr,
"Cannot read %s: %s\n", using_stdin ?
"the standard input" : argv[1], error->
message);
exit(EXIT_FAILURE);
}
dump_object_recursively(gwyf, 0);
return 0;
}
static void
dump_object_recursively(GwyfileObject *object, int level)
{
int i;
for (i = 0; i < level; i++)
printf(INDENT);
level++;
}
static void
dump_item_recursively(const GwyfileItem *item,
void *user_data)
{
int *plevel = (int*)user_data;
int i, level = *plevel;
unsigned int k, array_length;
for (i = 0; i < level; i++)
printf(INDENT);
for (k = 0; k < sizeof(typenames)/sizeof(typenames[0]); k++) {
if (typenames[k].type == type) {
printf(" type=%s", typenames[k].name);
break;
}
}
printf(" length=%u", array_length);
}
printf("\n");
for (k = 0; k < array_length; k++)
dump_object_recursively(objs[k], level+1);
}
}
GwyfileObject * gwyfile_read_file(const char *filename, GwyfileError **error)
Reads a GWY file from a named file and returns its top-level object.
Definition: gwyfile.c:405
GwyfileObject * gwyfile_fread(FILE *stream, size_t max_size, GwyfileError **error)
Reads a GWY file from a stdio stream and returns the top-level object.
Definition: gwyfile.c:565
void gwyfile_error_clear(GwyfileError **error)
Clears a GwyfileError.
Definition: gwyfile.c:8990
bool gwyfile_item_get_bool(const GwyfileItem *item)
Gets the boolean value contained in a GWY file data item.
Definition: gwyfile.c:5440
char gwyfile_item_get_char(const GwyfileItem *item)
Gets the character value contained in a GWY file data item.
Definition: gwyfile.c:5490
double gwyfile_item_get_double(const GwyfileItem *item)
Gets the double value contained in a GWY file data item.
Definition: gwyfile.c:5640
int32_t gwyfile_item_get_int32(const GwyfileItem *item)
Gets the 32bit integer value contained in a GWY file data item.
Definition: gwyfile.c:5540
int64_t gwyfile_item_get_int64(const GwyfileItem *item)
Gets the 64bit integer value contained in a GWY file data item.
Definition: gwyfile.c:5590
GwyfileObject *const * gwyfile_item_get_object_array(const GwyfileItem *item)
Gets the object array value contained in a GWY file data item.
Definition: gwyfile.c:8100
GwyfileObject * gwyfile_item_get_object(const GwyfileItem *item)
Gets the object value contained in a GWY file data item.
Definition: gwyfile.c:5903
uint32_t gwyfile_item_array_length(const GwyfileItem *item)
Obtains the array length of a GWY file data item.
Definition: gwyfile.c:7326
const char * gwyfile_item_name(const GwyfileItem *item)
Obtains the name of a GWY file data item.
Definition: gwyfile.c:7311
GwyfileItemType
Definition: gwyfile.h:87
GwyfileItemType gwyfile_item_type(const GwyfileItem *item)
Obtains the type of a GWY file data item.
Definition: gwyfile.c:7298
@ GWYFILE_ITEM_INT32
32bit integer.
Definition: gwyfile.h:90
@ GWYFILE_ITEM_DOUBLE_ARRAY
Array of IEEE double precision floating point numbers.
Definition: gwyfile.h:98
@ GWYFILE_ITEM_STRING
String of characters.
Definition: gwyfile.h:93
@ GWYFILE_ITEM_OBJECT_ARRAY
Array of objects, i.e. a nested data structures.
Definition: gwyfile.h:100
@ GWYFILE_ITEM_INT64_ARRAY
Array of 64bit integers.
Definition: gwyfile.h:97
@ GWYFILE_ITEM_STRING_ARRAY
Array of character strings.
Definition: gwyfile.h:99
@ GWYFILE_ITEM_CHAR_ARRAY
Array of characters.
Definition: gwyfile.h:95
@ GWYFILE_ITEM_BOOL
Boolean (true or false).
Definition: gwyfile.h:88
@ GWYFILE_ITEM_INT64
64bit integer.
Definition: gwyfile.h:91
@ GWYFILE_ITEM_INT32_ARRAY
Array of 32bit integers.
Definition: gwyfile.h:96
@ GWYFILE_ITEM_OBJECT
Object, i.e. a nested data structure.
Definition: gwyfile.h:94
@ GWYFILE_ITEM_DOUBLE
IEEE double precision floating point number.
Definition: gwyfile.h:92
@ GWYFILE_ITEM_CHAR
Single character.
Definition: gwyfile.h:89
void gwyfile_object_foreach(const GwyfileObject *object, GwyfileObjectForeachFunc function, void *user_data)
Calls a function for each item contained in a GWY file data object.
Definition: gwyfile.c:4894
const char * gwyfile_object_name(const GwyfileObject *object)
Obtains the name of a GWY file data object.
Definition: gwyfile.c:4599
void gwyfile_object_free(GwyfileObject *object)
Frees a GWY file data object.
Definition: gwyfile.c:4577
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