DEADSOFTWARE

764a27618138fa5d4d7f098b04510fff6151964a
[odcread.git] / reader.h
1 #ifndef _READER_H_
2 #define _READER_H_
4 #include <iostream>
5 #include <vector>
7 #include <oberon.h>
8 #include <store.h>
9 #include <alien.h>
11 namespace odc {
13 struct TypeEntry {
14 const std::string name;
15 INTEGER baseId;
17 TypeEntry(const std::string &typeName) : name(typeName), baseId(-1) {}
18 };
20 /**
21 * TYPE Reader
22 * Reader for Component Pascal values like integers, reals, or sets. A reader contains a Files.Reader, to which it forwards most operations.
23 * Readers are used in the Store.Internalize procedure.
24 * Readers are not extensible.
25 */
26 class Reader {
27 private:
28 /*
29 * rider-: Files.Reader
30 * The file rider which links a Reader to a file.
31 */
32 std::istream &d_rider;
34 /*
35 * cancelled-: BOOLEAN valid during a Store.Internalize call
36 * Tells whether the currently executing Internalize has been called by ReadVersion or TurnIntoAlien.
37 */
38 bool d_cancelled;
40 /**
41 * readAlien-: BOOLEAN
42 * Tells whether any alien has been read since the last ConnectTo.
43 */
44 bool d_readAlien;
46 std::vector<TypeEntry*> d_typeList;
48 std::vector<Store*> d_elemList; // FIXME: WTH, why are these different?
49 std::vector<Store*> d_storeList;
51 Store *d_store;
53 struct ReaderState {
54 /**
55 * Position of the next store in the current level
56 */
57 std::streampos next;
58 /**
59 * Position just after the last read store
60 */
61 std::streampos end;
62 };
63 ReaderState *d_state;
65 INTEGER d_cause;
67 public:
68 /**
69 * Construct a reader from the istream rider.
70 * @param rider An istream (binary mode).
71 */
72 Reader(std::istream &rider);
74 /**
75 * PROCEDURE (VAR rd: Reader) ConnectTo (f: Files.File)
76 * NEW
77 * Connect the reader to a file. All the following operations require connected readers, i.e., rd.rider # NIL. This precondition is not checked explicitly, however. After connecting, the reader's position is at the beginning of the file. If the same reader should be reused on another file, it must first be closed, by connecting it to NIL.
78 * ConnectTo is used internally.
79 *
80 * Pre
81 * 20 (f = NIL) OR (rd.rider = NIL)
82 *
83 * Post
84 * f = NIL
85 * rd.rider = NIL
86 * f # NIL
87 * (rd.rider # NIL) & (rd.rider.Base() = f)
88 * rd.Pos() = 0
89 */
90 // FIXME
92 /**
93 * PROCEDURE (VAR rd: Reader) Pos (): INTEGER
94 * NEW
95 * Returns the reader's current position.
96 *
97 * Post
98 * 0 <= result <= rd.rider.Base().Length()
99 */
100 // FIXME
102 /**
103 * PROCEDURE (VAR rd: Reader) SetPos (pos: INTEGER)
104 * NEW
105 * Sets the reader's current position to pos.
106 *
107 * Pre
108 * 20 pos >= 0
109 * 21 pos <= rd.rider.Base().Length()
110 *
111 * Post
112 * rd.Pos() = pos
113 * ~rd.rider.eof
114 */
115 // FIXME
117 /**
118 * PROCEDURE (VAR rd: Reader) ReadBool (OUT x: BOOLEAN)
119 * NEW
120 * Reads a Boolean value.
121 */
122 /**
123 * PROCEDURE (VAR rd: Reader) ReadSChar (OUT x: SHORTCHAR)
124 * NEW
125 * Reads a short character (00X..0FFX).
126 */
127 SHORTCHAR readSChar();
128 /* PROCEDURE (VAR rd: Reader) ReadXChar (OUT x: CHAR)
129 * NEW
130 * Same as ReadSChar, but has a CHAR-type parameter.
131 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
132 */
133 CHAR readXChar();
134 /**
135 * PROCEDURE (VAR rd: Reader) ReadChar (OUT x: CHAR)
136 * NEW
137 * Reads a character (0000X..0FFFFX).
138 *
139 * PROCEDURE (VAR rd: Reader) ReadByte (OUT x: BYTE)
140 * NEW
141 * Reads a very short integer (-128..127).
142 *
143 * PROCEDURE (VAR rd: Reader) ReadSInt (OUT x: SHORTINT)
144 * NEW
145 * Reads a short integer (-32768..32767).
146 *
147 * PROCEDURE (VAR rd: Reader) ReadXInt (OUT x: INTEGER)
148 * NEW
149 * Same as ReadSInt, but has an INTEGER-type parameter.
150 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
151 */
153 /**
154 * PROCEDURE (VAR rd: Reader) ReadInt (OUT x: INTEGER)
155 * NEW
156 * Reads an integer (-2147483648..2147483647).
157 */
158 INTEGER readInt();
159 /*
160 * PROCEDURE (VAR rd: Reader) ReadLong (OUT x: LONGINT)
161 * NEW
162 * Reads a long integer (-9223372036854775808..9223372036854775807).
163 *
164 * PROCEDURE (VAR rd: Reader) ReadSReal (OUT x: SHORTREAL)
165 * NEW
166 * Reads a short real (32-bit IEEE number).
167 *
168 * PROCEDURE (VAR rd: Reader) ReadXReal (OUT x: REAL)
169 * NEW
170 * Same as ReadSReal, but has a REAL-type parameter.
171 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
172 *
173 * PROCEDURE (VAR rd: Reader) ReadReal (OUT x: REAL)
174 * NEW
175 * Reads a real (64-bit IEEE number).
176 *
177 * PROCEDURE (VAR rd: Reader) ReadSet (OUT x: SET)
178 * NEW
179 * Reads a set (32 elements).
180 */
181 /**
182 * PROCEDURE (VAR rd: Reader) ReadSString (OUT x: ARRAY OF SHORTCHAR)
183 * NEW
184 * Reads a 0X-terminated short string.
185 *
186 * Pre
187 * invalid index LEN(x) > Length(string)
188 */
189 void readSString(SHORTCHAR *out);
190 /**
191 * PROCEDURE (VAR rd: Reader) ReadXString (OUT x: ARRAY OF CHAR)
192 * NEW
193 * Same as ReadSString, but has a string-type parameter.
194 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
195 */
196 //void readXString(CHAR *out);
197 /**
198 * PROCEDURE (VAR rd: Reader) ReadString (OUT x: ARRAY OF CHAR)
199 * NEW
200 * Reads a 0X-terminated string.
201 *
202 * Pre
203 * invalid index LEN(x) > Length(string)
204 *
205 * PROCEDURE (VAR rd: Reader) ReadStore (OUT x: Store)
206 * NEW
207 * Reads a store's type, allocates it, and then reads its contents, by calling the store's Internalize procedure. x may also be NIL, or an alien if the store's module cannot be loaded, or if internalization has been cancelled by the Internalize procedure.
208 * If the store has already been read in, a pointer to the same store is returned instead of allocating a new one. This means that arbitrary graphs that have been written with WriteStore are reconstructed correctly, including alias pointers to the same store, cycles, etc.
209 * If the file on which the reader operates does not contain correct input, then an assertion trap will be caused (traps 101 to trap 106).
210 *
211 * Pre
212 * 20 the reader is at the start position of a new store
213 *
214 * Post
215 * empty store on file
216 * x = NIL
217 * non-empty store on file
218 * x # NIL
219 * x IS Alien
220 * x.cause # 0
221 * x.type # ""
222 * x.file # NIL
223 * x.pos >= 0 beginning of store's data
224 * x.len >= 0 length of store's data
225 * alien store contents are on x.file in the range [x.pos .. x.pos + x.len[.
226 * These data include only the store's contents, not its prefix
227 * ~(x IS Alien)
228 * x was read successfully
229 */
230 Store *readStore();
231 /**
232 * PROCEDURE (VAR rd: Reader) ReadVersion (min, max: INTEGER; OUT version: INTEGER)
233 * NEW
234 * Read a version byte and return it in version. If version is not in the specified range [min .. max], the store currently being read is turned into an alien, with cause = alienVersion.
235 *
236 * Pre
237 * 20 0 <= min <= max
238 *
239 * Post
240 * min <= version <= max
241 * legal version
242 * (version < min) OR (version > max)
243 * illegal version
244 * rd.cause = alienVersion
245 * rd.cancelled
246 * rd.readAlien
247 *
248 * PROCEDURE (VAR rd: Reader) TurnIntoAlien (cause: INTEGER)
249 * NEW
250 * A store which is currently being internalized can turn itself into an alien, e.g., if it has read a component store which is an alien.
251 *
252 * Pre
253 * 20 cause > 0
254 */
256 private:
257 Store *readStoreOrElemStore(bool isElem);
258 Store *readNilStore();
259 Store *readLinkStore();
260 Store *readNewLinkStore();
261 void internalizeAlien(Alien *alien, std::streampos down, std::streampos end);
263 TypePath readPath();
264 /**
265 * Add another component to the current path. If first==true, start a new path.
266 */
267 void addPathComponent(bool first, const std::string &typeName);
268 };
270 } // namespace odc
272 #endif // _READER_H_