DEADSOFTWARE

b4f475ca725bceff119d4f2bd790171995d4bd56
[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 /* PROCEDURE (VAR rd: Reader) ReadXChar (OUT x: CHAR)
135 * NEW
136 * Same as ReadSChar, but has a CHAR-type parameter.
137 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
138 */
139 CHAR readXChar();
140 /**
141 * PROCEDURE (VAR rd: Reader) ReadChar (OUT x: CHAR)
142 * NEW
143 * Reads a character (0000X..0FFFFX).
144 */
145 /**
146 * PROCEDURE (VAR rd: Reader) ReadByte (OUT x: BYTE)
147 * NEW
148 * Reads a very short integer (-128..127).
149 */
150 BYTE readByte();
151 /**
152 * PROCEDURE (VAR rd: Reader) ReadSInt (OUT x: SHORTINT)
153 * NEW
154 * Reads a short integer (-32768..32767).
155 *
156 * PROCEDURE (VAR rd: Reader) ReadXInt (OUT x: INTEGER)
157 * NEW
158 * Same as ReadSInt, but has an INTEGER-type parameter.
159 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
160 */
162 /**
163 * PROCEDURE (VAR rd: Reader) ReadInt (OUT x: INTEGER)
164 * NEW
165 * Reads an integer (-2147483648..2147483647).
166 */
167 INTEGER readInt();
168 /*
169 * PROCEDURE (VAR rd: Reader) ReadLong (OUT x: LONGINT)
170 * NEW
171 * Reads a long integer (-9223372036854775808..9223372036854775807).
172 *
173 * PROCEDURE (VAR rd: Reader) ReadSReal (OUT x: SHORTREAL)
174 * NEW
175 * Reads a short real (32-bit IEEE number).
176 *
177 * PROCEDURE (VAR rd: Reader) ReadXReal (OUT x: REAL)
178 * NEW
179 * Same as ReadSReal, but has a REAL-type parameter.
180 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
181 *
182 * PROCEDURE (VAR rd: Reader) ReadReal (OUT x: REAL)
183 * NEW
184 * Reads a real (64-bit IEEE number).
185 *
186 * PROCEDURE (VAR rd: Reader) ReadSet (OUT x: SET)
187 * NEW
188 * Reads a set (32 elements).
189 */
190 /**
191 * PROCEDURE (VAR rd: Reader) ReadSString (OUT x: ARRAY OF SHORTCHAR)
192 * NEW
193 * Reads a 0X-terminated short string.
194 *
195 * Pre
196 * invalid index LEN(x) > Length(string)
197 */
198 void readSString(SHORTCHAR *out);
199 /**
200 * PROCEDURE (VAR rd: Reader) ReadXString (OUT x: ARRAY OF CHAR)
201 * NEW
202 * Same as ReadSString, but has a string-type parameter.
203 * This procedure is provided to simplify migration from Release 1.2 to 1.3.
204 */
205 //void readXString(CHAR *out);
206 /**
207 * PROCEDURE (VAR rd: Reader) ReadString (OUT x: ARRAY OF CHAR)
208 * NEW
209 * Reads a 0X-terminated string.
210 *
211 * Pre
212 * invalid index LEN(x) > Length(string)
213 *
214 * PROCEDURE (VAR rd: Reader) ReadStore (OUT x: Store)
215 * NEW
216 * 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.
217 * 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.
218 * 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).
219 *
220 * Pre
221 * 20 the reader is at the start position of a new store
222 *
223 * Post
224 * empty store on file
225 * x = NIL
226 * non-empty store on file
227 * x # NIL
228 * x IS Alien
229 * x.cause # 0
230 * x.type # ""
231 * x.file # NIL
232 * x.pos >= 0 beginning of store's data
233 * x.len >= 0 length of store's data
234 * alien store contents are on x.file in the range [x.pos .. x.pos + x.len[.
235 * These data include only the store's contents, not its prefix
236 * ~(x IS Alien)
237 * x was read successfully
238 */
239 Store *readStore();
240 /**
241 * PROCEDURE (VAR rd: Reader) ReadVersion (min, max: INTEGER; OUT version: INTEGER)
242 * NEW
243 * 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.
244 *
245 * Pre
246 * 20 0 <= min <= max
247 *
248 * Post
249 * min <= version <= max
250 * legal version
251 * (version < min) OR (version > max)
252 * illegal version
253 * rd.cause = alienVersion
254 * rd.cancelled
255 * rd.readAlien
256 */
257 INTEGER readVersion(INTEGER min, INTEGER max);
258 /*
259 * PROCEDURE (VAR rd: Reader) TurnIntoAlien (cause: INTEGER)
260 * NEW
261 * 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.
262 *
263 * Pre
264 * 20 cause > 0
265 */
266 void turnIntoAlien(int cause);
268 bool isCancelled();
270 private:
271 Store *readStoreOrElemStore(bool isElem);
272 Store *readNilStore();
273 Store *readLinkStore();
274 Store *readNewLinkStore();
275 void internalizeAlien(Alien *alien, std::streampos down, std::streampos end);
277 std::string &fixTypeName(std::string &name);
278 TypePath readPath();
279 /**
280 * Add another component to the current path. If first==true, start a new path.
281 */
282 void addPathComponent(bool first, const std::string &typeName);
283 };
285 } // namespace odc
287 #endif // _READER_H_