-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyocton_print.c
82 lines (73 loc) · 2.03 KB
/
yocton_print.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Copyright (c) 2022, Simon Howard
//
// Permission to use, copy, modify, and/or distribute this software
// for any purpose with or without fee is hereby granted, provided
// that the above copyright notice and this permission notice appear
// in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Basic example program that reads a .yocton file and prints the contents.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "yocton.h"
struct yocton_object;
void print_indent(int indent)
{
int i;
for (i = 0; i < indent; ++i) {
putchar(' ');
}
}
void print_object(struct yocton_object *obj, int indent)
{
struct yocton_prop *property;
for (;;) {
property = yocton_next_prop(obj);
if (property == NULL) {
break;
}
print_indent(indent);
printf("%s", yocton_prop_name(property));
if (yocton_prop_type(property) == YOCTON_PROP_OBJECT) {
printf(":\n");
print_object(yocton_prop_inner(property), indent + 4);
} else {
printf(" = \"%s\"\n", yocton_prop_value(property));
}
}
}
int main(int argc, char *argv[])
{
FILE *fstream;
struct yocton_object *obj;
const char *error;
int error_lineno;
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
exit(1);
}
fstream = fopen(argv[1], "r");
if (fstream == NULL) {
fprintf(stderr, "Error opening %s: %s\n",
argv[1], strerror(errno));
exit(1);
}
obj = yocton_read_from(fstream);
print_object(obj, 0);
if (yocton_have_error(obj, &error_lineno, &error)) {
fprintf(stderr, "%d: %s\n", error_lineno, error);
}
yocton_free(obj);
fclose(fstream);
}