Add support for reading and writing SPV files.
[pspp] / src / output / spv / spvbin-helpers.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2018 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef SPVBIN_HELPERS_H
18 #define SPVBIN_HELPERS_H 1
19
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <stdint.h>
23
24 struct spvbin_input
25   {
26     const uint8_t *data;
27     size_t ofs;
28     size_t size;
29     int version;
30
31 #define SPVBIN_MAX_ERRORS 16
32     struct
33       {
34         const char *name;
35         size_t start;
36       }
37     errors[SPVBIN_MAX_ERRORS];
38     size_t n_errors;
39     size_t error_ofs;
40   };
41
42 void spvbin_input_init (struct spvbin_input *, const void *, size_t);
43 bool spvbin_input_at_end (const struct spvbin_input *);
44
45 char *spvbin_input_to_error (const struct spvbin_input *, const char *name);
46
47 bool spvbin_match_bytes (struct spvbin_input *, const void *, size_t);
48 bool spvbin_match_byte (struct spvbin_input *, uint8_t);
49
50 bool spvbin_parse_bool (struct spvbin_input *, bool *);
51 bool spvbin_parse_byte (struct spvbin_input *, uint8_t *);
52 bool spvbin_parse_int16 (struct spvbin_input *, uint16_t *);
53 bool spvbin_parse_int32 (struct spvbin_input *, uint32_t *);
54 bool spvbin_parse_int64 (struct spvbin_input *, uint64_t *);
55 bool spvbin_parse_be16 (struct spvbin_input *, uint16_t *);
56 bool spvbin_parse_be32 (struct spvbin_input *, uint32_t *);
57 bool spvbin_parse_be64 (struct spvbin_input *, uint64_t *);
58 bool spvbin_parse_double (struct spvbin_input *, double *);
59 bool spvbin_parse_float (struct spvbin_input *, double *);
60 bool spvbin_parse_string (struct spvbin_input *, char **);
61 bool spvbin_parse_bestring (struct spvbin_input *, char **);
62
63 void spvbin_error (struct spvbin_input *, const char *name, size_t start);
64
65 void spvbin_print_header (const char *title, size_t start, size_t len,
66                           int indent);
67 void spvbin_print_presence (const char *title, int indent, bool);
68 void spvbin_print_bool (const char *title, int indent, bool);
69 void spvbin_print_byte (const char *title, int indent, uint8_t);
70 void spvbin_print_int16 (const char *title, int indent, uint16_t);
71 void spvbin_print_int32 (const char *title, int indent, uint32_t);
72 void spvbin_print_int64 (const char *title, int indent, uint64_t);
73 void spvbin_print_double (const char *title, int indent, double);
74 void spvbin_print_string (const char *title, int indent, const char *);
75 void spvbin_print_case (const char *title, int indent, int);
76
77 struct spvbin_position
78   {
79     size_t ofs;
80   };
81
82 struct spvbin_position spvbin_position_save (const struct spvbin_input *);
83 void spvbin_position_restore (struct spvbin_position *, struct spvbin_input *);
84
85 struct spvbin_limit
86   {
87     size_t size;
88   };
89
90 bool spvbin_limit_parse (struct spvbin_limit *, struct spvbin_input *);
91 bool spvbin_limit_parse_be (struct spvbin_limit *, struct spvbin_input *);
92 void spvbin_limit_pop (struct spvbin_limit *, struct spvbin_input *);
93
94 #endif /* output/spv/spvbin-helpers.h */