Debian Codegraph @

terps/tads/tads3/vmpack.h

raw symbols SCIP
1 /* $Header$ */
2
3 /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */
4 /*
5 Name
6 vmpack.h - binary data stream pack/unpack
7 Function
8 Converts between a binary data stream and TADS types, using a list of
9 type codes to drive the conversion. This is a convenient way of reading
10 and writing structured binary files. Our implementation uses generic
11 data streams, so it can work with various underlying data sources; in
12 particular, we provide File and ByteArray interfaces.
13 Notes
14
15 Modified
16 10/01/10 MJRoberts - Creation
17 */
18
19 #ifndef VMPACK_H
20 #define VMPACK_H
21
22 #include "t3std.h"
23 #include "vmtype.h"
24
25
26 class CVmPack
27 {
28 public:
29 /*
30 * Pack data from stack arguments into the given stream. 'arg_index'
31 * is the index in the stack of the format string argument; the data
32 * value arguments come after it on the stack. 'argc' is the total
33 * number of arguments, including the format string.
34 */
35 static void pack(VMG_ int arg_index, int argc,
36 class CVmDataSource *dst);
37
38 /*
39 * Unpack data from the given stream into a list.
40 */
41 static void unpack(VMG_ vm_val_t *retval, const char *fmt, size_t fmtlen,
42 class CVmDataSource *src);
43
44
45 /* write padding bytes */
46 static void write_padding(class CVmDataSource *dst, char ch, long cnt);
47 static void write_padding(class CVmDataSource *dst,
48 char ch1, char ch2, long cnt);
49
50 protected:
51 /*
52 * Pack a group. pack() calls this to start packing the main string,
53 * and we recurse for each parenthesized group we find.
54 */
55 static void pack_group(VMG_ struct CVmPackPos *pos,
56 struct CVmPackArgs *args,
57 struct CVmPackGroup *group,
58 int list_per_iter);
59
60 /* pack a subgroup */
61 static void pack_subgroup(VMG_ struct CVmPackPos *p,
62 struct CVmPackArgs *args,
63 struct CVmPackGroup *parent,
64 struct CVmPackType *prefix_count);
65
66 /* pack an item or iterated list of items */
67 static void pack_item(VMG_ struct CVmPackGroup *group,
68 struct CVmPackArgs *args,
69 struct CVmPackType *typ,
70 struct CVmPackType *prefix_count);
71
72 /* pack a single item */
73 static void pack_one_item(VMG_ struct CVmPackGroup *group,
74 struct CVmPackArgs *args,
75 struct CVmPackType *t,
76 struct CVmPackType *prefix_count);
77
78 /* unpack a group */
79 static void unpack_group(VMG_ struct CVmPackPos *p,
80 class CVmObjList *retlst, int *retcnt,
81 struct CVmPackGroup *group, int list_per_iter);
82
83 /* unpack a subgroup */
84 static void unpack_subgroup(VMG_ CVmPackPos *p,
85 class CVmObjList *retlst, int &retcnt,
86 struct CVmPackGroup *group,
87 const int *prefix_count);
88
89 /* create a sublist for unpacking a "[ ]!" group */
90 static CVmObjList *create_group_sublist(
91 VMG_ CVmObjList *parent_list, int *parent_cnt);
92
93 /* unpack an iterated item */
94 static void unpack_iter_item(VMG_ struct CVmPackPos *p,
95 class CVmObjList *retlst, int &retcnt,
96 struct CVmPackGroup *group,
97 const int *prefix_count);
98
99 /* unpack an item into the list */
100 static void unpack_into_list(VMG_ class CVmObjList *retlst, int &retcnt,
101 class CVmDataSource *src,
102 const struct CVmPackType *t,
103 const struct CVmPackGroup *group);
104
105 /* unpack a single item */
106 static void unpack_item(VMG_ vm_val_t *val,
107 CVmDataSource *src,
108 const struct CVmPackType *t,
109 const struct CVmPackGroup *group);
110
111 /* parse a type code; increments the pointer past the parsed input */
112 static void parse_type(struct CVmPackPos *p, struct CVmPackType *info,
113 const struct CVmPackGroup *group);
114
115 /*
116 * parse the suffix modifiers for a group, given a pointer to the
117 * opening parenthesis of the group
118 */
119 static void parse_group_mods(const struct CVmPackPos *p,
120 struct CVmPackType *gt);
121
122 /* find the close parenthesis/bracket of a group */
123 static void skip_group(struct CVmPackPos *p, wchar_t close_paren);
124
125 /* parse and skip a group and its modifiers */
126 static void skip_group_mods(struct CVmPackPos *p, struct CVmPackType *gt);
127
128 /* parse suffix modifiers */
129 static void parse_mods(struct CVmPackPos *p, struct CVmPackType *t);
130 };
131
132 #endif /* VMPACK_H */