Allow for static initialisation.
[libeze] / ez-blob-print.c
1 /* ez-blob.h: Serialising description and serialiser.
2
3    Copyright (C) 2019 Michael Zucchi
4
5    This program is free software: you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public License
7    as published by the Free Software Foundation, either version 3 of
8    the License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this program. If not, see
17    <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23
24 #include <assert.h>
25 #include <stdio.h>
26
27 #include "ez-blob.h"
28 #include "ez-list.h"
29
30 #include "ez-blob-io.h"
31
32 void ez_blob_print(const ez_blob_desc *d, const void *a, int depth) {
33         char x[depth+1];
34         int len = d->bd_length;
35
36         memset(x, ' ', depth);
37         x[depth] = 0;
38
39         for (int i=0;i<=len;i++,d++) {
40                 const void *u = a + d->bd_offset;
41
42                 printf("%s [type=$%04x, offset=$%04x]", x, d->bd_type, d->bd_offset);
43                 
44                 switch (d->bd_type) {
45                 case EZ_BLOB_INT8:
46                         printf(" .%d = %02x\n", d->bd_id, *(uint8_t *)u);
47                         break;
48                 case EZ_BLOB_INT16:
49                         printf(" .%d = %04x\n", d->bd_id, *(uint16_t *)u);
50                         break;
51                 case EZ_BLOB_INT32:
52                         printf(" .%d = %08x\n", d->bd_id, *(uint32_t *)u);
53                         break;
54                 case EZ_BLOB_INT64:
55                         printf(" .%d = %016lx\n", d->bd_id, *(uint64_t *)u);
56                         break;
57                 case EZ_BLOB_FLOAT32:
58                         printf(" .%d = %f\n", d->bd_id, *(float *)u);
59                         break;
60                 case EZ_BLOB_FLOAT64:
61                         printf(" .%d = %f\n", d->bd_id, *(double *)u);
62                         break;
63                         
64                 case EZ_BLOB_STRUCT | EZ_BLOB_SINGLEP | EZ_BLOB_ISNULLABLE:
65                 case EZ_BLOB_STRUCT | EZ_BLOB_SINGLEP:
66                         u = *(void **)u;
67                 case EZ_BLOB_STRUCT | EZ_BLOB_SINGLE:
68                         if (u) {
69                                 printf(" .%d %p = {\n", d->bd_id, u);
70                                 ez_blob_print(d->bd_table, u, depth+4);
71                                 printf("%s  }\n", x);
72                         } else {
73                                 printf(" .%d %p\n", d->bd_id, u);
74                         }
75                         break;
76
77                 case EZ_BLOB_CSTRING | EZ_BLOB_INT8 | EZ_BLOB_ISNULLABLE:
78                 case EZ_BLOB_CSTRING | EZ_BLOB_INT8:
79                         printf(" .%d %p = `%s'\n", d->bd_id, *((char **)u), *((char **)u));
80                         break;
81
82                 case EZ_BLOB_VECTOR | EZ_BLOB_INT8:
83                 case EZ_BLOB_VECTOR | EZ_BLOB_INT16:
84                 case EZ_BLOB_VECTOR | EZ_BLOB_INT32:
85                 case EZ_BLOB_VECTOR | EZ_BLOB_INT64:
86                 case EZ_BLOB_VECTOR | EZ_BLOB_FLOAT32:
87                 case EZ_BLOB_VECTOR | EZ_BLOB_FLOAT64: {
88                         const struct ez_blob *ua = u;
89                         unsigned int elsize = 1 << (d->bd_type & EZ_BLOB_SIZE);
90                         ez_blob blob = { .eb_size = ua->eb_size * elsize, .eb_data = ua->eb_data };
91                         
92                         printf(" .%d %p [%zd x %d] = {\n", d->bd_id, ua->eb_data, ua->eb_size, elsize);
93                         ez_blob_dump(&blob, x);
94                         printf("%s  }\n", x);
95                         break;
96                 }
97
98                 case EZ_BLOB_STRUCT | EZ_BLOB_LISTP | EZ_BLOB_ISNULLABLE:
99                 case EZ_BLOB_STRUCT | EZ_BLOB_LISTP:
100                         u = *(const void **)u;
101                 case EZ_BLOB_STRUCT | EZ_BLOB_LIST:
102                         if (u) {
103                                 printf(" .%d %p [] = {\n", d->bd_id, u);
104                                 for (ez_node *w = ez_list_head((ez_list *)u), *n = ez_node_next(w); n; w = n, n=ez_node_next(n)) {
105                                         printf("%s{\n", x);
106                                         ez_blob_print(d->bd_table, w, depth+4);
107                                         printf("%s},\n", x);
108                                 }
109                                 printf("%s  }\n", x);
110                         } else {
111                                 printf(" .%d %p\n", d->bd_id, u);
112                         }
113                         break;
114
115                 case EZ_BLOB_MAGIC:
116                         printf(" OBJECT id=%d length=%d\n", d->bd_id, d->bd_length);
117                         break;
118                 default:
119                         printf(" .%d ?? UNKNOWN ??\n", d->bd_id);
120                         break;
121                 }
122         }
123 }
124