DEADSOFTWARE

Successful read of StdTextModel (including actual text)
[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 static const unsigned int TYPENOTFOUND = 1;
29 static const unsigned int ALIENVERSION = 2;
31 /*
32 * rider-: Files.Reader
33 * The file rider which links a Reader to a file.
34 */
35 std::istream &d_rider;
37 /*
38 * cancelled-: BOOLEAN valid during a Store.Internalize call
39 * Tells whether the currently executing Internalize has been cancelled by ReadVersion or TurnIntoAlien.
40 */
41 bool d_cancelled;
43 /**
44 * readAlien-: BOOLEAN
45 * Tells whether any alien has been read since the last ConnectTo.
46 */
47 bool d_readAlien;
49 /**
50 * Cause of current read being alien.
51 */
52 unsigned int d_cause;
54 std::vector<TypeEntry*> d_typeList;
56 std::vector<Store*> d_elemList; // FIXME: WTH, why are these different?
57 std::vector<Store*> d_storeList;
59 Store *d_store;
61 struct ReaderState {
62 /**
63 * Position of the next store in the current level
64 */
65 std::streampos next;
66 /**
67 * Position just after the last read store
68 */
69 std::streampos end;
70 };
71 ReaderState *d_state;
73 public:
74 /**
75 * Construct a reader from the istream rider.
76 * @param rider An istream (binary mode).
77 */
78 Reader(std::istream &rider);
80 /**
81 * PROCEDURE (VAR rd: Reader) ConnectTo (f: Files.File)
82 * NEW
83 * 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.
84 * ConnectTo is used internally.
85 *
86 * Pre
87 * 20 (f = NIL) OR (rd.rider = NIL)
88 *
89 * Post
90 * f = NIL
91 * rd.rider = NIL
92 * f # NIL
93 * (rd.rider # NIL) & (rd.rider.Base() = f)
94 * rd.Pos() = 0
95 */
96 // FIXME
98 /**
99 * PROCEDURE (VAR rd: Reader) Pos (): INTEGER
100 * NEW
101 * Returns the reader's current position.
102 *
103 * Post
104 * 0 <= result <= rd.rider.Base().Length()
105 */
106 // FIXME
108 /**
109 * PROCEDURE (VAR rd: Reader) SetPos (pos: INTEGER)
110 * NEW
111 * Sets the reader's current position to pos.
112 *
113 * Pre
114 * 20 pos >= 0
115 * 21 pos <= rd.rider.Base().Length()
116 *
117 * Post
118 * rd.Pos() = pos
119 * ~rd.rider.eof
120 */
121 // FIXME
123 /**
124 * PROCEDURE (VAR rd: Reader) ReadBool (OUT x: BOOLEAN)
125 * NEW
126 * Reads a Boolean value.
127 */
128 /**
129 * PROCEDURE (VAR rd: Reader) ReadSChar (OUT x: SHORTCHAR)
130 * NEW
131 * Reads a short character (00X..0FFX).
132 */
133 SHORTCHAR readSChar();
134 void readSChar(SHORTCHAR *buf, size_t len);
135 /* PROCEDURE (VAR rd: Reader) ReadXChar (OUT x: CHAR)
136 * NEW
137 * Same as ReadSChar, but has a CHAR-type parameter.
138 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
139 */
140 /**
141 * PROCEDURE (VAR rd: Reader) ReadChar (OUT x: CHAR)
142 * NEW
143 * Reads a character (0000X..0FFFFX).
144 */
145 CHAR readLChar();
146 void readLChar(CHAR *buf, size_t len);
147 /**
148 * PROCEDURE (VAR rd: Reader) ReadByte (OUT x: BYTE)
149 * NEW
150 * Reads a very short integer (-128..127).
151 */
152 BYTE readByte();
153 /**
154 * PROCEDURE (VAR rd: Reader) ReadSInt (OUT x: SHORTINT)
155 * NEW
156 * Reads a short integer (-32768..32767).
157 *
158 * PROCEDURE (VAR rd: Reader) ReadXInt (OUT x: INTEGER)
159 * NEW
160 * Same as ReadSInt, but has an INTEGER-type parameter.
161 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
162 */
164 /**
165 * PROCEDURE (VAR rd: Reader) ReadInt (OUT x: INTEGER)
166 * NEW
167 * Reads an integer (-2147483648..2147483647).
168 */
169 INTEGER readInt();
170 /*
171 * PROCEDURE (VAR rd: Reader) ReadLong (OUT x: LONGINT)
172 * NEW
173 * Reads a long integer (-9223372036854775808..9223372036854775807).
174 *
175 * PROCEDURE (VAR rd: Reader) ReadSReal (OUT x: SHORTREAL)
176 * NEW
177 * Reads a short real (32-bit IEEE number).
178 *
179 * PROCEDURE (VAR rd: Reader) ReadXReal (OUT x: REAL)
180 * NEW
181 * Same as ReadSReal, but has a REAL-type parameter.
182 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
183 *
184 * PROCEDURE (VAR rd: Reader) ReadReal (OUT x: REAL)
185 * NEW
186 * Reads a real (64-bit IEEE number).
187 *
188 * PROCEDURE (VAR rd: Reader) ReadSet (OUT x: SET)
189 * NEW
190 * Reads a set (32 elements).
191 */
192 /**
193 * PROCEDURE (VAR rd: Reader) ReadSString (OUT x: ARRAY OF SHORTCHAR)
194 * NEW
195 * Reads a 0X-terminated short string.
196 *
197 * Pre
198 * invalid index LEN(x) > Length(string)
199 */
200 void readSString(SHORTCHAR *out);
201 /**
202 * PROCEDURE (VAR rd: Reader) ReadXString (OUT x: ARRAY OF CHAR)
203 * NEW
204 * Same as ReadSString, but has a string-type parameter.
205 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
206 */
207 //void readXString(CHAR *out);
208 /**
209 * PROCEDURE (VAR rd: Reader) ReadString (OUT x: ARRAY OF CHAR)
210 * NEW
211 * Reads a 0X-terminated string.
212 *
213 * Pre
214 * invalid index LEN(x) > Length(string)
215 *
216 * PROCEDURE (VAR rd: Reader) ReadStore (OUT x: Store)
217 * NEW
218 * 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.
219 * 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.
220 * 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).
221 *
222 * Pre
223 * 20 the reader is at the start position of a new store
224 *
225 * Post
226 * empty store on file
227 * x = NIL
228 * non-empty store on file
229 * x # NIL
230 * x IS Alien
231 * x.cause # 0
232 * x.type # ""
233 * x.file # NIL
234 * x.pos >= 0 beginning of store's data
235 * x.len >= 0 length of store's data
236 * alien store contents are on x.file in the range [x.pos .. x.pos + x.len[.
237 * These data include only the store's contents, not its prefix
238 * ~(x IS Alien)
239 * x was read successfully
240 */
241 Store *readStore();
242 /**
243 * PROCEDURE (VAR rd: Reader) ReadVersion (min, max: INTEGER; OUT version: INTEGER)
244 * NEW
245 * 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.
246 *
247 * Pre
248 * 20 0 <= min <= max
249 *
250 * Post
251 * min <= version <= max
252 * legal version
253 * (version < min) OR (version > max)
254 * illegal version
255 * rd.cause = alienVersion
256 * rd.cancelled
257 * rd.readAlien
258 */
259 INTEGER readVersion(INTEGER min, INTEGER max);
260 /*
261 * PROCEDURE (VAR rd: Reader) TurnIntoAlien (cause: INTEGER)
262 * NEW
263 * 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.
264 *
265 * Pre
266 * 20 cause > 0
267 */
268 void turnIntoAlien(int cause);
270 bool isCancelled();
272 private:
273 Store *readStoreOrElemStore(bool isElem);
274 Store *readNilStore();
275 Store *readLinkStore();
276 Store *readNewLinkStore();
277 void internalizeAlien(Alien *alien, std::streampos down, std::streampos end);
279 std::string &fixTypeName(std::string &name);
280 TypePath readPath();
281 /**
282 * Add another component to the current path. If first==true, start a new path.
283 */
284 void addPathComponent(bool first, const std::string &typeName);
285 };
287 } // namespace odc
289 #endif // _READER_H_