Gwyfile Library
checkgeneric.c

Example demonstrating the functions for checking GWY files beyond the physical consistency enforced by libgwyfile.

/*
* $Id: checkgeneric.c 318 2020-06-05 17:04:48Z yeti-dn $
*
* This example reads a generic GWY file and checks it for some violations of
* the GWY file format specification.
*
* 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"
int
main(int argc, char *argv[])
{
GwyfileObject *gwyf;
GwyfileError *error = NULL;
bool using_stdin = false;
unsigned int i;
if (argc != 2) {
printf("checkgeneric FILE.gwy\n"
"Read a generic GWY file and check it for problems.\n"
"Pass \"-\" as the filename to read the standard input.\n");
exit(argc == 1 ? EXIT_SUCCESS : EXIT_FAILURE);
}
/* Read a Gwyddion GWY file. */
if (strcmp(argv[1], "-") == 0) {
using_stdin = true;
gwyf = gwyfile_fread(stdin, SIZE_MAX, &error);
}
else
gwyf = gwyfile_read_file(argv[1], &error);
if (!gwyf) {
fprintf(stderr, "Cannot read %s: %s\n",
using_stdin ? "the standard input" : argv[1], error->message);
exit(EXIT_FAILURE);
}
/* Check validity. */
printf("No validity problems found.\n");
}
else {
printf("Validity problems:\n");
for (i = 0; i < errlist.n; i++) {
error = errlist.errors[i];
printf("%s (%u)\n", error->message, error->code);
}
}
/* Check warnings. */
printf("No warnings found.\n");
}
else {
printf("Warnings:\n");
for (i = 0; i < errlist.n; i++) {
error = errlist.errors[i];
printf("%s (%u)\n", error->message, error->code);
}
}
/* Clean-up. This call recursively frees all the objects and items. */
return 0;
}
/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
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
bool gwyfile_check_object(const GwyfileObject *object, unsigned int flags, GwyfileErrorList *errlist)
Checks an object for specifications violations.
Definition: gwyfile.c:9065
@ GWYFILE_CHECK_FLAG_WARNING
Enables checking for errors from the GWYFILE_ERROR_DOMAIN_WARNING category in gwyfile_check_object().
Definition: gwyfile.h:152
@ GWYFILE_CHECK_FLAG_VALIDITY
Enables checking for errors from the GWYFILE_ERROR_DOMAIN_VALIDITY category in gwyfile_check_object()...
Definition: gwyfile.h:151
void gwyfile_error_list_init(GwyfileErrorList *errlist)
Initializes a GwyfileErrorList.
Definition: gwyfile.c:9009
void gwyfile_error_clear(GwyfileError **error)
Clears a GwyfileError.
Definition: gwyfile.c:8990
void gwyfile_error_list_clear(GwyfileErrorList *errlist)
Frees all errors in a GwyfileErrorList.
Definition: gwyfile.c:9026
void gwyfile_object_free(GwyfileObject *object)
Frees a GWY file data object.
Definition: gwyfile.c:4577
Gwyfile Library.
List of errors.
Definition: gwyfile.h:137
GwyfileError ** errors
Array holding the errors.
Definition: gwyfile.h:138
size_t n
Number of errors in the list.
Definition: gwyfile.h:139
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
int code
Particular error code. For errors from the system domain it is equal to errno while for libgwyfile-sp...
Definition: gwyfile.h:133