Debian Codegraph @

tads3/vmdatasrc.h

raw symbols SCIP
1 /* $Header$ */cxx . . $ `<file>/tads3/vmdatasrc.h`/
2
3 /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */
4 /*
5 Name
6 vmdatasrc.h - Data Source object
7 Function
8 CVmDataSource is an abstract class representing a read/write data source,
9 such as a file, block of memory, etc. The data source object has an
10 interface essentially identical to the osifc file interface, but can be
11 implemented with a variety of underlying data sources.
12 Notes
13
14 Modified
15 05/05/10 MJRoberts - Creation
16 */
17
18 #ifndef VMDATASRC_H
19 #define VMDATASRC_Hcxx . . $ `tads3/vmdatasrc.h:19:9`!
20
21 #include <string.h>
22 #include "t3std.h"cxx . . $ `<file>/tads3/t3std.h`/
23 #include "osifcnet.h"cxx . . $ `<file>/tads3/osifcnet.h`/
24 #include "vmerr.h"cxx . . $ `<file>/tads3/vmerr.h`/
25 #include "vmfile.h"cxx . . $ `<file>/tads3/vmfile.h`/
26 #include "vmerrnum.h"cxx . . $ `<file>/tads3/vmerrnum.h`/
27
28
29 /* ------------------------------------------------------------------------ */
30 /*
31 * Abstract file interface. This allows the File intrinsic class to
32 * present a common interface on multiple underlying data sources.
33 */
34 class CVmDataSourcecxx . . $ CVmDataSource#
35 {
36 public:
37 virtual ~cxx . . $ CVmDataSource#`~CVmDataSource`(ced63f7c635d850d).CVmDataSourcecxx . . $ CVmDataSource#() { }
38
39 /* read bytes - returns 0 on success, non-zero on EOF or error */
40 virtual int readcxx . . $ CVmDataSource#read(c0a5f96701d3f89).(void *buf, size_t len) = 0;
41
42 /* read bytes - returns the number of bytes read; 0 means EOF or error */
43 virtual int readccxx . . $ CVmDataSource#readc(c0a5f96701d3f89).(void *buf, size_t len) = 0;
44
45 /* write bytes - returns 0 on success, non-zero on error */
46 virtual int writecxx . . $ CVmDataSource#write(a10e65277395b633).(const void *buf, size_t len) = 0;
47
48 /* write a null-terminated string */
49 virtual int writezcxx . . $ CVmDataSource#writez(5c4306d8553b3696).(const char *str) { return writecxx . . $ CVmDataSource#write(a10e65277395b633).(str, strlen(str)); }
50
51 /* get the length of the file in bytes */
52 virtual long get_sizecxx . . $ CVmDataSource#get_size(da6335ded91d79c2).() = 0;
53
54 /* get the current seek location */
55 virtual long get_poscxx . . $ CVmDataSource#get_pos(da6335ded91d79c2).() = 0;
56
57 /* set the current seek location - 'mode' is an OSFSK_xxx mode */
58 virtual int seekcxx . . $ CVmDataSource#seek(4a3ed3c01b538bac).(long ofs, int mode) = 0;
59
60 /* flush - returns 0 on success, non-zero on error */
61 virtual int flushcxx . . $ CVmDataSource#flush(b126dc7c1de90089).() = 0;
62
63 /* close the underlying system resource */
64 virtual void closecxx . . $ CVmDataSource#close(49f6e7a06ebc5aa8).() = 0;
65
66 /*
67 * Read a line of text (fgets() semantics). This default
68 * implementation reads a character at a time from the underlying
69 * source; subclasses should override this if they can implement it
70 * more efficiently.
71 */
72 virtual char *read_linecxx . . $ CVmDataSource#read_line(dff97f9eb74e1544).(char *buf, size_t len)
73 {
74 /* if we have no bytes left, return null for EOF */
75 if (get_poscxx . . $ CVmDataSource#get_pos(da6335ded91d79c2).() >= get_sizecxx . . $ CVmDataSource#get_size(da6335ded91d79c2).())
76 return 0;
77
78 /* read bytes until we encounter a CR, LF, or CR-LF */
79 char *dst = buf;
80 while (len > 0 && get_poscxx . . $ CVmDataSource#get_pos(da6335ded91d79c2).() < get_sizecxx . . $ CVmDataSource#get_size(da6335ded91d79c2).())
81 {
82 /* read the next character */
83 char ch;
84 if (readcxx . . $ CVmDataSource#read(c0a5f96701d3f89).(&ch, 1))
85 return 0;
86
87 /* check for a line terminator */
88 if (ch == 10 || ch == 13)
89 {
90 /*
91 * add a newline to the result (it's always '\n' in the
92 * result buffer regardless of the type of newline in the
93 * source)
94 */
95 *dst++ = '\n';
96 --len;
97
98 /*
99 * if this is a CR-LF sequence, skip both characters;
100 * otherwise just skip the single CR or LF
101 */
102 char ch2;
103 if (ch == 13
104 && get_poscxx . . $ CVmDataSource#get_pos(da6335ded91d79c2).() < get_sizecxx . . $ CVmDataSource#get_size(da6335ded91d79c2).()
105 && !readcxx . . $ CVmDataSource#read(c0a5f96701d3f89).(&ch2, 1)
106 && ch2 != 10)
107 {
108 /* it's not a CR-LF - un-get the second character */
109 seekcxx . . $ CVmDataSource#seek(4a3ed3c01b538bac).(get_poscxx . . $ CVmDataSource#get_pos(da6335ded91d79c2).() - 1, OSFSK_SETcxx . . $ `src/osqt.h:168:9`!);
110 }
111
112 /* we're done */
113 break;
114 }
115
116 /* copy this byte */
117 *dst++ = ch;
118
119 /* count it against the remaining buffer length */
120 --len;
121 }
122
123 /* add a null terminator if possible */
124 if (len != 0)
125 *dst = '\0';
126
127 /* return the original buffer pointer */
128 return buf;
129 }
130
131 /* read a line of text into an allocated buffer */
132 char *read_line_alocxx . . $ CVmDataSource#read_line_alo(41208ce6ca65dd58).()
133 {
134 char *ret = 0;
135 size_t retlen = 0;
136
137 /* keep going until we satisfy the request */
138 for (;;)
139 {
140 /* read a chunk; if at EOF, we're done */
141 char buf[1024];
142 if (read_linecxx . . $ CVmDataSource#read_line(dff97f9eb74e1544).(buf, sizeof(buf)) == 0)
143 break;
144
145 /* get the length of this chunk */
146 size_t len = strlen(buf);
147
148 /* allocate or expand the buffer to accommodate the new text */
149 if (ret == 0)
150 ret = (char *)t3malloccxx . . $ `tads3/t3std.h:773:9`!(len + 1);
151 else
152 ret = (char *)t3realloccxx . . $ `tads3/t3std.h:775:9`!(ret, retlen + len + 1);
153
154 /* fail if out of memory */
155 if (ret == 0)
156 return 0;
157
158 /* add the new chunk to the result (including the null) */
159 memcpy(ret + retlen, buf, len + 1);
160 retlen += len;
161
162 /* if the buffer ends with a newline, we're done */
163 if (retlen != 0 && ret[retlen-1] == '\n')
164 break;
165 }
166
167 /* return our buffer */
168 return ret;
169 }
170
171 /*
172 * Create a duplicate data source.
173 *
174 * 'mode' is a simplified version of the stdio fopen() mode string.
175 * The first letter is 'r' for read access, 'w' for write access. This
176 * can be followed by '+' for both read and write access; 'r+' and 'w+'
177 * are identical, both indicating read/write access. This can
178 * optionally be followed by 't' for text mode or 'b' for binary mode;
179 * text mode is assumed if not specified.
180 */
181 virtual CVmDataSourcecxx . . $ CVmDataSource# *clonecxx . . $ CVmDataSource#clone(80c95016c20e3f25).(VMG_cxx . . $ `tads3/vmglob.h:349:9`! const char *mode) = 0;
182 };
183
184
185 /* ------------------------------------------------------------------------ */
186 /*
187 * Basic OS file data source.
188 */
189 class CVmFileSourcecxx . . $ CVmFileSource#: public CVmDataSourcecxx . . $ CVmDataSource#
190 {
191 public:
192 CVmFileSourcecxx . . $ CVmFileSource#CVmFileSource(350f9e409240e83b).(osfildef *fp) { this->fpcxx . . $ CVmFileSource#fp. = fp; }
193 ~cxx . . $ CVmFileSource#`~CVmFileSource`(ced63f7c635d850d).CVmFileSourcecxx . . $ CVmFileSource#()
194 {
195 if (fpcxx . . $ CVmFileSource#fp. != 0)
196 closecxx . . $ CVmFileSource#close(49f6e7a06ebc5aa8).();
197 }
198
199 /* read bytes - returns 0 on success, non-zero on EOF or error */
200 virtual int readcxx . . $ CVmFileSource#read(c0a5f96701d3f89).(void *buf, size_t len) { return osfrb(fpcxx . . $ CVmFileSource#fp., buf, len); }
201
202 /* read bytes - returns 0 on success, non-zero on EOF or error */
203 virtual int readccxx . . $ CVmFileSource#readc(c0a5f96701d3f89).(void *buf, size_t len) { return osfrbc(fpcxx . . $ CVmFileSource#fp., buf, len); }
204
205 /* read a line of text (fgets() semantics) */
206 virtual char *read_linecxx . . $ CVmFileSource#read_line(dff97f9eb74e1544).(char *buf, size_t len)
207 { return osfgets(buf, len, fpcxx . . $ CVmFileSource#fp.); }
208
209 /* write bytes - returns 0 on success, non-zero on error*/
210 virtual int writecxx . . $ CVmFileSource#write(a10e65277395b633).(const void *buf, size_t len)
211 { return osfwb(fpcxx . . $ CVmFileSource#fp., buf, len); }
212
213 /* get the length of the file in bytes */
214 virtual long get_sizecxx . . $ CVmFileSource#get_size(da6335ded91d79c2).()
215 {
216 /* remember the current seek location */
217 long oldpos = osfpos(fpcxx . . $ CVmFileSource#fp.);
218
219 /* seek to the end */
220 osfseek(fpcxx . . $ CVmFileSource#fp., 0, OSFSK_ENDcxx . . $ `src/osqt.h:170:9`!);
221
222 /* the current position is the file size */
223 long siz = osfpos(fpcxx . . $ CVmFileSource#fp.);
224
225 /* seek back to where we started */
226 osfseek(fpcxx . . $ CVmFileSource#fp., oldpos, OSFSK_SETcxx . . $ `src/osqt.h:168:9`!);
227
228 /* return the size */
229 return siz;
230 }
231
232 /* get the current seek location */
233 virtual long get_poscxx . . $ CVmFileSource#get_pos(da6335ded91d79c2).() { return osfpos(fpcxx . . $ CVmFileSource#fp.); }
234
235 /* set the current seek location - 'mode' is an OSFSK_xxx mode */
236 virtual int seekcxx . . $ CVmFileSource#seek(4a3ed3c01b538bac).(long ofs, int mode) { return osfseek(fpcxx . . $ CVmFileSource#fp., ofs, mode); }
237
238 /* flush - returns 0 on success, non-zero on error */
239 virtual int flushcxx . . $ CVmFileSource#flush(b126dc7c1de90089).() { return osfflush(fpcxx . . $ CVmFileSource#fp.); }
240
241 /* close the underlying system resource */
242 virtual void closecxx . . $ CVmFileSource#close(49f6e7a06ebc5aa8).()
243 {
244 osfcls(fpcxx . . $ CVmFileSource#fp.);
245 fpcxx . . $ CVmFileSource#fp. = 0;
246 }
247
248 /* clone the source */
249 virtual CVmDataSourcecxx . . $ CVmDataSource# *clonecxx . . $ CVmFileSource#clone(80c95016c20e3f25).(VMG_cxx . . $ `tads3/vmglob.h:349:9`! const char *mode)
250 {
251 /* duplicate our underlying file handle */
252 osfildef *fpdup = osfdup(fpcxx . . $ CVmFileSource#fp., mode);
253 if (fpdup == 0)
254 return 0;
255
256 /* return a new source wrapping the duplicated file handle */
257 return new CVmFileSourcecxx . . $ CVmFileSource#CVmFileSource(350f9e409240e83b).(fpdup);
258 }
259
260 protected:
261 /* the underlying file */
262 osfildef *fpcxx . . $ CVmFileSource#fp.;
263 };
264
265
266 /* ------------------------------------------------------------------------ */
267 /*
268 * Resource file data source. A resource file is a contiguous byte range
269 * within a larger file.
270 */
271 class CVmResFileSourcecxx . . $ CVmResFileSource#: public CVmDataSourcecxx . . $ CVmDataSource#
272 {
273 public:
274 CVmResFileSourcecxx . . $ CVmResFileSource#CVmResFileSource(50eefc89278d9580).(osfildef *fp, unsigned long start, long end)
275 {
276 this->fpcxx . . $ CVmResFileSource#fp. = fp;
277 this->startcxx . . $ CVmResFileSource#start. = start;
278 this->endcxx . . $ CVmResFileSource#end. = end;
279 }
280 ~cxx . . $ CVmResFileSource#`~CVmResFileSource`(ced63f7c635d850d).CVmResFileSourcecxx . . $ CVmResFileSource#()
281 {
282 if (fpcxx . . $ CVmResFileSource#fp. != 0)
283 closecxx . . $ CVmResFileSource#close(49f6e7a06ebc5aa8).();
284 }
285
286 /* clone the source */
287 virtual CVmDataSourcecxx . . $ CVmDataSource# *clonecxx . . $ CVmResFileSource#clone(80c95016c20e3f25).(VMG_cxx . . $ `tads3/vmglob.h:349:9`! const char *mode)
288 {
289 /* duplicate our underlying file handle */
290 osfildef *fpdup = osfdup(fpcxx . . $ CVmResFileSource#fp., mode);
291 if (fpdup == 0)
292 return 0;
293
294 /* return a new source wrapping the duplicated file handle */
295 return new CVmResFileSourcecxx . . $ CVmResFileSource#(fpdup, startcxx . . $ CVmResFileSource#start., endcxx . . $ CVmResFileSource#end.);
296 }
297
298 /* read bytes - returns 0 on success, non-zero on EOF or error */
299 virtual int readcxx . . $ CVmResFileSource#read(c0a5f96701d3f89).(void *buf, size_t len)
300 {
301 /* if this would take us past the end, we can't fulfill the request */
302 unsigned long pos = osfpos(fpcxx . . $ CVmResFileSource#fp.);
303 if (pos + len > endcxx . . $ CVmResFileSource#end.)
304 return 1;
305
306 /* do the read */
307 return osfrb(fpcxx . . $ CVmResFileSource#fp., buf, len);
308 }
309
310 /* read a line of text (fgets() semantics) */
311 virtual char *read_linecxx . . $ CVmResFileSource#read_line(dff97f9eb74e1544).(char *buf, size_t len)
312 {
313 /* if we're already past the end, we can't fulfill the request */
314 unsigned long pos = osfpos(fpcxx . . $ CVmResFileSource#fp.);
315 if (pos >= endcxx . . $ CVmResFileSource#end.)
316 return 0;
317
318 /* read a line */
319 char *ret = osfgets(buf, len, fpcxx . . $ CVmResFileSource#fp.);
320 if (ret == 0)
321 return 0;
322
323 /*
324 * if that moved us past the end of our segment, go back and
325 * re-read the exact number of bytes remaining
326 */
327 if ((unsigned long)osfpos(fpcxx . . $ CVmResFileSource#fp.) > endcxx . . $ CVmResFileSource#end.)
328 {
329 /* seek back to where we started */
330 osfseek(fpcxx . . $ CVmResFileSource#fp., pos, OSFSK_SETcxx . . $ `src/osqt.h:168:9`!);
331
332 /* read the remaining bytes in the resource (up to buflen) */
333 size_t r = endcxx . . $ CVmResFileSource#end. - pos > len ? (long)len : (size_t)(endcxx . . $ CVmResFileSource#end. - pos);
334 if (osfrb(fpcxx . . $ CVmResFileSource#fp., buf, r))
335 return 0;
336
337 /* if that didn't fill the buffer, null-terminate it */
338 if (r < len)
339 buf[r] = '\0';
340 }
341
342 /* return the result */
343 return buf;
344 }
345
346 /* read bytes - returns 0 on success, non-zero on EOF or error */
347 virtual int readccxx . . $ CVmResFileSource#readc(c0a5f96701d3f89).(void *buf, size_t len)
348 {
349 /* if we're already past the end, we can't read anything */
350 unsigned long pos = osfpos(fpcxx . . $ CVmResFileSource#fp.);
351 if (pos >= endcxx . . $ CVmResFileSource#end.)
352 return 0;
353
354 /* limit the read to the available file size */
355 unsigned long limit = endcxx . . $ CVmResFileSource#end. - pos;
356 if (len > limit)
357 len = (size_t)limit;
358
359 /* do the read */
360 return osfrbc(fpcxx . . $ CVmResFileSource#fp., buf, len);
361 }
362
363 /* write bytes - resource files are read-only */
364 virtual int writecxx . . $ CVmResFileSource#write(a10e65277395b633).(const void *buf, size_t len) { return 1; }
365
366 /* get the length of the file in bytes */
367 virtual long get_sizecxx . . $ CVmResFileSource#get_size(da6335ded91d79c2).() { return endcxx . . $ CVmResFileSource#end. - startcxx . . $ CVmResFileSource#start.; }
368
369 /* get the current seek location */
370 virtual long get_poscxx . . $ CVmResFileSource#get_pos(da6335ded91d79c2).() { return osfpos(fpcxx . . $ CVmResFileSource#fp.) - startcxx . . $ CVmResFileSource#start.; }
371
372 /* set the current seek location - 'mode' is an OSFSK_xxx mode */
373 virtual int seekcxx . . $ CVmResFileSource#seek(4a3ed3c01b538bac).(long ofs, int mode)
374 {
375 switch (mode)
376 {
377 case OSFSK_SETcxx . . $ `src/osqt.h:168:9`!:
378 do_set:
379 /* check that 'ofs' is in range */
380 if (ofs < 0 || (unsigned long)ofs > endcxx . . $ CVmResFileSource#end. - startcxx . . $ CVmResFileSource#start.)
381 return 1;
382
383 /* seek to the offset */
384 return osfseek(fpcxx . . $ CVmResFileSource#fp., startcxx . . $ CVmResFileSource#start. + ofs, OSFSK_SETcxx . . $ `src/osqt.h:168:9`!);
385
386 case OSFSK_CURcxx . . $ `src/osqt.h:169:9`!:
387 /* set the offset relative to the current position */
388 ofs += get_poscxx . . $ CVmResFileSource#get_pos(da6335ded91d79c2).();
389 goto do_set;
390
391 case OSFSK_ENDcxx . . $ `src/osqt.h:170:9`!:
392 /* set the offset relative to the end of the file */
393 ofs += (endcxx . . $ CVmResFileSource#end. - startcxx . . $ CVmResFileSource#start.);
394 goto do_set;
395
396 default:
397 /* invalid mode */
398 return 1;
399 }
400 }
401
402 /* flush - we're read-only, so this just returns success */
403 virtual int flushcxx . . $ CVmResFileSource#flush(b126dc7c1de90089).() { return 0; }
404
405 /* close the underlying system resource */
406 virtual void closecxx . . $ CVmResFileSource#close(49f6e7a06ebc5aa8).()
407 {
408 osfcls(fpcxx . . $ CVmResFileSource#fp.);
409 fpcxx . . $ CVmResFileSource#fp. = 0;
410 }
411
412 protected:
413 /* the underlying file */
414 osfildef *fpcxx . . $ CVmResFileSource#fp.;
415
416 /* the byte range within the resource file */
417 unsigned long startcxx . . $ CVmResFileSource#start., endcxx . . $ CVmResFileSource#end.;
418 };
419
420
421 /* ------------------------------------------------------------------------ */
422 /*
423 * Read-only string data source. This takes a buffer in memory and
424 * presents a read-only file interface to it.
425 */
426 class CVmStringSourcecxx . . $ CVmStringSource#: public CVmDataSourcecxx . . $ CVmDataSource#
427 {
428 public:
429 CVmStringSourcecxx . . $ CVmStringSource#CVmStringSource(ea9ecd48262ed8f2).(const char *mem, long len)
430 {
431 this->memcxx . . $ CVmStringSource#mem. = mem;
432 this->memlencxx . . $ CVmStringSource#memlen. = len;
433 this->poscxx . . $ CVmStringSource#pos. = 0;
434 }
435
436 virtual ~cxx . . $ CVmStringSource#`~CVmStringSource`(ced63f7c635d850d).CVmStringSourcecxx . . $ CVmStringSource#() { }
437
438 virtual CVmDataSourcecxx . . $ CVmDataSource# *clonecxx . . $ CVmStringSource#clone(80c95016c20e3f25).(VMG_cxx . . $ `tads3/vmglob.h:349:9`! const char * /*mode*/)
439 { return new CVmStringSourcecxx . . $ CVmStringSource#(memcxx . . $ CVmStringSource#mem., memlencxx . . $ CVmStringSource#memlen.); }
440
441 /* read bytes - returns 0 on success, non-zero on EOF or error */
442 virtual int readcxx . . $ CVmStringSource#read(c0a5f96701d3f89).(void *buf, size_t len)
443 {
444 /* make sure there's enough data to satisfy the request */
445 if (poscxx . . $ CVmStringSource#pos. + (long)len > memlencxx . . $ CVmStringSource#memlen.)
446 return 1;
447
448 /* copy the data */
449 memcpy(buf, memcxx . . $ CVmStringSource#mem. + poscxx . . $ CVmStringSource#pos., len);
450
451 /* advance the read pointer */
452 poscxx . . $ CVmStringSource#pos. += len;
453
454 /* success */
455 return 0;
456 }
457
458 /* read bytes - returns the number of bytes read; 0 means EOF or error */
459 virtual int readccxx . . $ CVmStringSource#readc(c0a5f96701d3f89).(void *buf, size_t len)
460 {
461 /* limit the read to the available length */
462 long rem = memlencxx . . $ CVmStringSource#memlen. - poscxx . . $ CVmStringSource#pos.;
463 if ((long)len > rem)
464 len = rem;
465
466 /* copy the bytes */
467 memcpy(buf, memcxx . . $ CVmStringSource#mem. + poscxx . . $ CVmStringSource#pos., len);
468
469 /* advance the read pointer */
470 poscxx . . $ CVmStringSource#pos. += len;
471
472 /* return the amount read */
473 return (int)len;
474 }
475
476 /* read a line of text (fgets() semantics) */
477 virtual char *read_linecxx . . $ CVmStringSource#read_line(dff97f9eb74e1544).(char *buf, size_t len)
478 {
479 /* if we have no bytes left, return null for EOF */
480 if (poscxx . . $ CVmStringSource#pos. == memlencxx . . $ CVmStringSource#memlen.)
481 return 0;
482
483 /* read bytes until we encounter a CR, LF, or CR-LF */
484 char *dst = buf;
485 while (poscxx . . $ CVmStringSource#pos. < memlencxx . . $ CVmStringSource#memlen. && len > 0)
486 {
487 /* check for a line terminator */
488 if (memcxx . . $ CVmStringSource#mem.[poscxx . . $ CVmStringSource#pos.] == 10 || memcxx . . $ CVmStringSource#mem.[poscxx . . $ CVmStringSource#pos.] == 13)
489 {
490 /*
491 * add a newline to the result (it's always '\n' in the
492 * result buffer regardless of the type of newline in the
493 * source)
494 */
495 *dst++ = '\n';
496 --len;
497
498 /*
499 * if this is a CR-LF sequence, skip both characters;
500 * otherwise just skip the single CR or LF
501 */
502 ++poscxx . . $ CVmStringSource#pos.;
503 if (poscxx . . $ CVmStringSource#pos. < memlencxx . . $ CVmStringSource#memlen. && memcxx . . $ CVmStringSource#mem.[poscxx . . $ CVmStringSource#pos.-1] == 13 && memcxx . . $ CVmStringSource#mem.[poscxx . . $ CVmStringSource#pos.] == 10)
504 ++poscxx . . $ CVmStringSource#pos.;
505
506 /* we're done */
507 break;
508 }
509
510 /* copy this byte */
511 *dst++ = memcxx . . $ CVmStringSource#mem.[poscxx . . $ CVmStringSource#pos.++];
512
513 /* count it against the remaining buffer length */
514 --len;
515 }
516
517 /* add a null terminator if possible */
518 if (len != 0)
519 *dst = '\0';
520
521 /* return the original buffer pointer */
522 return buf;
523 }
524
525 /* write bytes - we're read-only, so this is an error */
526 virtual int writecxx . . $ CVmStringSource#write(a10e65277395b633).(const void *buf, size_t len) { return 1; }
527
528 /* get the length of the file in bytes */
529 virtual long get_sizecxx . . $ CVmStringSource#get_size(da6335ded91d79c2).() { return memlencxx . . $ CVmStringSource#memlen.; }
530
531 /* get the current seek location */
532 virtual long get_poscxx . . $ CVmStringSource#get_pos(da6335ded91d79c2).() { return poscxx . . $ CVmStringSource#pos.; }
533
534 /* set the current seek location - 'mode' is an OSFSK_xxx mode */
535 virtual int seekcxx . . $ CVmStringSource#seek(4a3ed3c01b538bac).(long ofs, int mode)
536 {
537 switch (mode)
538 {
539 case OSFSK_SETcxx . . $ `src/osqt.h:168:9`!:
540 do_set:
541 /* make sure it's in range */
542 if (ofs < 0 || ofs > memlencxx . . $ CVmStringSource#memlen.)
543 return 1;
544
545 /* set the new position */
546 poscxx . . $ CVmStringSource#pos. = ofs;
547
548 /* success */
549 return 0;
550
551 case OSFSK_CURcxx . . $ `src/osqt.h:169:9`!:
552 /* calculate the absolute position and go do the set */
553 ofs += poscxx . . $ CVmStringSource#pos.;
554 goto do_set;
555
556 case OSFSK_ENDcxx . . $ `src/osqt.h:170:9`!:
557 /* calculate the absolute position and go do the set */
558 ofs += memlencxx . . $ CVmStringSource#memlen.;
559 goto do_set;
560
561 default:
562 /* invalid mode */
563 return 1;
564 }
565 }
566
567 /* flush - we're read-only, so this is a no-op; just return success */
568 virtual int flushcxx . . $ CVmStringSource#flush(b126dc7c1de90089).() { return 0; }
569
570 /* close the underlying system resource; there's nothing for us to do */
571 virtual void closecxx . . $ CVmStringSource#close(49f6e7a06ebc5aa8).() { }
572
573 protected:
574 /* our memory buffer, and the number of bytes in the buffer */
575 const char *memcxx . . $ CVmStringSource#mem.;
576 long memlencxx . . $ CVmStringSource#memlen.;
577
578 /* current read position */
579 long poscxx . . $ CVmStringSource#pos.;
580 };
581
582 /* ------------------------------------------------------------------------ */
583 /*
584 * Private string source - makes a private copy of a string buffer
585 */
586
587 class CVmPrivateStringSourcecxx . . $ CVmPrivateStringSource#: public CVmStringSourcecxx . . $ CVmStringSource#
588 {
589 public:
590 CVmPrivateStringSourcecxx . . $ CVmPrivateStringSource#CVmPrivateStringSource(ea9ecd48262ed8f2).(const char *mem, long len)
591 : CVmStringSourcecxx . . $ CVmStringSource#CVmStringSource(ea9ecd48262ed8f2).(0, len)
592 {
593 this->bufobjcxx . . $ CVmPrivateStringSource#bufobj. = new CVmRefCntBufcxx . . $ CVmRefCntBuf#(mem, len);
594 this->memcxx . . $ CVmStringSource#mem. = bufobjcxx . . $ CVmPrivateStringSource#bufobj.->buf_cxx . . $ CVmRefCntBuf#buf_.;
595 }
596
597 CVmPrivateStringSourcecxx . . $ CVmPrivateStringSource#CVmPrivateStringSource(af481febac14d5c7).(CVmRefCntBufcxx . . $ CVmRefCntBuf# *bufobj, long len)
598 : CVmStringSourcecxx . . $ CVmStringSource#(bufobj->buf_cxx . . $ CVmRefCntBuf#buf_., len)
599 {
600 this->bufobjcxx . . $ CVmPrivateStringSource#bufobj. = bufobj;
601 }
602
603 CVmDataSourcecxx . . $ CVmDataSource# *clonecxx . . $ CVmPrivateStringSource#clone(80c95016c20e3f25).(VMG_cxx . . $ `tads3/vmglob.h:349:9`! const char * /*mode*/)
604 { return new CVmPrivateStringSourcecxx . . $ CVmPrivateStringSource#CVmPrivateStringSource(af481febac14d5c7).(bufobjcxx . . $ CVmPrivateStringSource#bufobj., memlencxx . . $ CVmStringSource#memlen.); }
605
606 ~cxx . . $ CVmPrivateStringSource#`~CVmPrivateStringSource`(ced63f7c635d850d).CVmPrivateStringSourcecxx . . $ CVmPrivateStringSource#()
607 {
608 bufobjcxx . . $ CVmPrivateStringSource#bufobj.->release_refcxx . . $ CVmRefCntObj#release_ref(49f6e7a06ebc5aa8).();
609 }
610
611 protected:
612 CVmRefCntBufcxx . . $ CVmRefCntBuf# *bufobjcxx . . $ CVmPrivateStringSource#bufobj.;
613 };
614
615
616 /* ------------------------------------------------------------------------ */
617 /*
618 * Memory data source. This automatically allocates additional buffer
619 * space as needed.
620 */
621
622 /* page block for a memory source */
623 struct CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock#
624 {
625 CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock#CVmMemorySourceBlock(1624ee8275b10bda).(long ofs)
626 {
627 this->ofscxx . . $ CVmMemorySourceBlock#ofs. = ofs;
628 nxtcxx . . $ CVmMemorySourceBlock#nxt. = 0;
629 }
630
631 /* block length */
632 static const int BlockLencxx . . $ CVmMemorySourceBlock#BlockLen. = 4096;
633
634 /* next block in the list */
635 CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock# *nxtcxx . . $ CVmMemorySourceBlock#nxt.;
636
637 /* byte offset in the overall stream of the start of this block */
638 long ofscxx . . $ CVmMemorySourceBlock#ofs.;
639
640 /* block buffer */
641 char bufcxx . . $ CVmMemorySourceBlock#buf.[BlockLencxx . . $ CVmMemorySourceBlock#BlockLen.];
642 };
643
644 /* page list for a memory source */
645 class CVmMemorySourceListcxx . . $ CVmMemorySourceList#: public CVmRefCntObjcxx . . $ CVmRefCntObj#
646 {
647 public:
648 static const int BlockLencxx . . $ CVmMemorySourceList#BlockLen. = CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock#::BlockLencxx . . $ CVmMemorySourceBlock#BlockLen.;
649
650 CVmMemorySourceListcxx . . $ CVmMemorySourceList#CVmMemorySourceList(1624ee8275b10bda).(long init_len)
651 {
652 /* no blocks yet */
653 first_block_cxx . . $ CVmMemorySourceList#first_block_. = last_block_cxx . . $ CVmMemorySourceList#last_block_. = 0;
654
655 /* always allocate at least the initial block */
656 add_blockcxx . . $ CVmMemorySourceList#add_block(49f6e7a06ebc5aa8).();
657 init_len -= BlockLencxx . . $ CVmMemorySourceList#BlockLen.;
658
659 /* set the seek position to the start of the first block */
660 cur_block_cxx . . $ CVmMemorySourceList#cur_block_. = first_block_cxx . . $ CVmMemorySourceList#first_block_.;
661 cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_. = 0;
662
663 /* add more blocks until we've satisfied the length request */
664 for ( ; init_len > 0 ; init_len -= BlockLencxx . . $ CVmMemorySourceList#BlockLen.)
665 add_blockcxx . . $ CVmMemorySourceList#add_block(49f6e7a06ebc5aa8).();
666
667 /* the file is currently empty */
668 len_cxx . . $ CVmMemorySourceList#len_. = 0;
669 }
670
671 /* read bytes */
672 int readcxx . . $ CVmMemorySourceList#read(c0a5f96701d3f89).(void *buf, size_t len)
673 {
674 /*
675 * if the request would take us past the current content length,
676 * fail immediately, since we know we can't satisfy the request
677 */
678 if (cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->ofscxx . . $ CVmMemorySourceBlock#ofs. + cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_. + (long)len > len_cxx . . $ CVmMemorySourceList#len_.)
679 return 1;
680
681 /*
682 * do the read; return success == 0 if the amount read exactly
683 * matches the requested length
684 */
685 return (size_t)readccxx . . $ CVmMemorySourceList#readc(c0a5f96701d3f89).(buf, len) != len;
686 }
687
688 /* read bytes */
689 int readccxx . . $ CVmMemorySourceList#readc(c0a5f96701d3f89).(void *buf0, size_t len)
690 {
691 /* get the buffer pointer as a character pointer */
692 char *buf = (char *)buf0;
693
694 /* we haven't ready anything yet */
695 size_t bytes_read = 0;
696
697 /* limit the length to the remaining bytes in the object */
698 long rem = len_cxx . . $ CVmMemorySourceList#len_. - get_poscxx . . $ CVmMemorySourceList#get_pos(da6335ded91d79c2).();
699 if ((long)len > rem)
700 len = (size_t)rem;
701
702 /* keep going until we satisfy the request */
703 while (len != 0)
704 {
705 /* figure how much we can read from the current block */
706 size_t rem = BlockLencxx . . $ CVmMemorySourceList#BlockLen. - cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_.;
707 size_t cur = (len < rem ? len : rem);
708
709 /* read that much */
710 if (cur != 0)
711 {
712 /* copy bytes to the output buffer */
713 memcpy(buf, cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->bufcxx . . $ CVmMemorySourceBlock#buf. + cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_., cur);
714
715 /* move past the bytes in the current block */
716 cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_. += cur;
717
718 /* adjust our request counters past this chunk */
719 len -= cur;
720 buf += cur;
721
722 /* count it in the total read so far */
723 bytes_read += cur;
724 }
725
726 /* if that satisfies the request, we're done */
727 if (len == 0)
728 break;
729
730 /* if this is the last block, we're done */
731 if (cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->nxtcxx . . $ CVmMemorySourceBlock#nxt. == 0)
732 return bytes_read;
733
734 /* advance to the start of the next block */
735 cur_block_cxx . . $ CVmMemorySourceList#cur_block_. = cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->nxtcxx . . $ CVmMemorySourceBlock#nxt.;
736 cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_. = 0;
737 }
738
739 /* return the amount we managed to read */
740 return bytes_read;
741 }
742
743 /* write bytes */
744 int writecxx . . $ CVmMemorySourceList#write(a10e65277395b633).(const void *buf0, size_t len)
745 {
746 /* get the buffer pointer as a character pointer */
747 const char *buf = (const char *)buf0;
748
749 /* keep going until we satisfy the request */
750 for (;;)
751 {
752 /* figure how much we can write to the current block */
753 size_t rem = BlockLencxx . . $ CVmMemorySourceList#BlockLen. - cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_.;
754 size_t cur = (len < rem ? len : rem);
755
756 /* write that much */
757 if (cur != 0)
758 {
759 memcpy(cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->bufcxx . . $ CVmMemorySourceBlock#buf. + cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_., buf, cur);
760 cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_. += cur;
761 len -= cur;
762 buf += cur;
763 }
764
765 /* if that satisfied the request, we're done */
766 if (len == 0)
767 break;
768
769 /* if this is the last block, add another */
770 if (cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->nxtcxx . . $ CVmMemorySourceBlock#nxt. == 0)
771 {
772 /* add the block */
773 add_blockcxx . . $ CVmMemorySourceList#add_block(49f6e7a06ebc5aa8).();
774
775 /* if that didn't work, fail */
776 if (cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->nxtcxx . . $ CVmMemorySourceBlock#nxt. == 0)
777 return 1;
778 }
779
780 /* advance to the start of the next block */
781 cur_block_cxx . . $ CVmMemorySourceList#cur_block_. = cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->nxtcxx . . $ CVmMemorySourceBlock#nxt.;
782 cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_. = 0;
783 }
784
785 /*
786 * if the seek pointer is past the current content length, we've
787 * written past the old end of the file and thus expanded the file
788 * - note the new length
789 */
790 if (cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->ofscxx . . $ CVmMemorySourceBlock#ofs. + cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_. > len_cxx . . $ CVmMemorySourceList#len_.)
791 len_cxx . . $ CVmMemorySourceList#len_. = cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->ofscxx . . $ CVmMemorySourceBlock#ofs. + cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_.;
792
793 /* success */
794 return 0;
795 }
796
797 /* get the length of the stream's contents */
798 long get_sizecxx . . $ CVmMemorySourceList#get_size(da6335ded91d79c2).() { return len_cxx . . $ CVmMemorySourceList#len_.; }
799
800 /* get the current seek offset */
801 long get_poscxx . . $ CVmMemorySourceList#get_pos(da6335ded91d79c2).()
802 {
803 /*
804 * figure the seek position as the current block's base offset plus
805 * the current offset within that block
806 */
807 return cur_block_cxx . . $ CVmMemorySourceList#cur_block_.->ofscxx . . $ CVmMemorySourceBlock#ofs. + cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_.;
808 }
809
810 /* set the seek offset */
811 int seekcxx . . $ CVmMemorySourceList#seek(4a3ed3c01b538bac).(long pos, int mode)
812 {
813 /* figure the absolute position based on the mode */
814 switch (mode)
815 {
816 case OSFSK_SETcxx . . $ `src/osqt.h:168:9`!:
817 /* from the beginning - use pos as given */
818 break;
819
820 case OSFSK_CURcxx . . $ `src/osqt.h:169:9`!:
821 /* from the current position */
822 pos += get_poscxx . . $ CVmMemorySourceList#get_pos(da6335ded91d79c2).();
823 break;
824
825 case OSFSK_ENDcxx . . $ `src/osqt.h:170:9`!:
826 /* from the end */
827 pos += get_sizecxx . . $ CVmMemorySourceList#get_size(da6335ded91d79c2).();
828 break;
829
830 default:
831 /* other modes are invalid */
832 return 1;
833 }
834
835 /* limit the position to the file's bounds */
836 if (pos < 0)
837 {
838 /* at start of file */
839 pos = 0;
840 }
841 else if (pos >= len_cxx . . $ CVmMemorySourceList#len_.)
842 {
843 /*
844 * At end of file. This is a special case for setting the
845 * block pointer, because we could be positioned at the last
846 * byte of the last block, which we won't find in our scan.
847 * Explicitly set the position here.
848 */
849 pos = len_cxx . . $ CVmMemorySourceList#len_.;
850 cur_block_cxx . . $ CVmMemorySourceList#cur_block_. = last_block_cxx . . $ CVmMemorySourceList#last_block_.;
851 cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_. = pos - last_block_cxx . . $ CVmMemorySourceList#last_block_.->ofscxx . . $ CVmMemorySourceBlock#ofs.;
852
853 /* we've set the block position, so skip the usual search */
854 return 0;
855 }
856
857 /* find the block containing the seek offset */
858 CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock# *b;
859 for (b = first_block_cxx . . $ CVmMemorySourceList#first_block_. ; b != 0 ; b = b->nxtcxx . . $ CVmMemorySourceBlock#nxt.)
860 {
861 /* if the offset is within this block, we're done */
862 if (pos >= b->ofscxx . . $ CVmMemorySourceBlock#ofs. && pos < b->ofscxx . . $ CVmMemorySourceBlock#ofs. + BlockLencxx . . $ CVmMemorySourceList#BlockLen.)
863 {
864 /* set the current pointers */
865 cur_block_cxx . . $ CVmMemorySourceList#cur_block_. = b;
866 cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_. = pos - b->ofscxx . . $ CVmMemorySourceBlock#ofs.;
867
868 /* stop scanning */
869 break;
870 }
871 }
872
873 /* success */
874 return 0;
875 }
876
877 protected:
878 ~cxx . . $ CVmMemorySourceList#`~CVmMemorySourceList`(ced63f7c635d850d).CVmMemorySourceListcxx . . $ CVmMemorySourceList#()
879 {
880 /* delete our block list */
881 CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock# *cur, *nxt;
882 for (cur = first_block_cxx . . $ CVmMemorySourceList#first_block_. ; cur != 0 ; cur = nxt)
883 {
884 nxt = cur->nxtcxx . . $ CVmMemorySourceBlock#nxt.;
885 delete cur;
886 }
887 }
888
889 /* add a block to the end of the list */
890 void add_blockcxx . . $ CVmMemorySourceList#add_block(49f6e7a06ebc5aa8).()
891 {
892 long ofs = last_block_cxx . . $ CVmMemorySourceList#last_block_. == 0 ? 0 : last_block_cxx . . $ CVmMemorySourceList#last_block_.->ofscxx . . $ CVmMemorySourceBlock#ofs. + BlockLencxx . . $ CVmMemorySourceList#BlockLen.;
893 CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock# *b = new CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock#(ofs);
894
895 if (last_block_cxx . . $ CVmMemorySourceList#last_block_. != 0)
896 last_block_cxx . . $ CVmMemorySourceList#last_block_.->nxtcxx . . $ CVmMemorySourceBlock#nxt. = b;
897 else
898 first_block_cxx . . $ CVmMemorySourceList#first_block_. = b;
899 last_block_cxx . . $ CVmMemorySourceList#last_block_. = b;
900 }
901
902 /* current seek block and offset */
903 CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock# *cur_block_cxx . . $ CVmMemorySourceList#cur_block_.;
904 long cur_block_ofs_cxx . . $ CVmMemorySourceList#cur_block_ofs_.;
905
906 /* total file length */
907 long len_cxx . . $ CVmMemorySourceList#len_.;
908
909 /* head/tail of block list */
910 CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock# *first_block_cxx . . $ CVmMemorySourceList#first_block_.;
911 CVmMemorySourceBlockcxx . . $ CVmMemorySourceBlock# *last_block_cxx . . $ CVmMemorySourceList#last_block_.;
912 };
913
914 /*
915 * Paged memory source
916 */
917 class CVmMemorySourcecxx . . $ CVmMemorySource#: public CVmDataSourcecxx . . $ CVmDataSource#
918 {
919 public:
920 CVmMemorySourcecxx . . $ CVmMemorySource#CVmMemorySource(1624ee8275b10bda).(long init_len)
921 {
922 /* create our underlying page list */
923 plcxx . . $ CVmMemorySource#pl. = new CVmMemorySourceListcxx . . $ CVmMemorySourceList#CVmMemorySourceList(1624ee8275b10bda).(init_len);
924 }
925
926 /* clone the data source */
927 CVmDataSourcecxx . . $ CVmDataSource# *clonecxx . . $ CVmMemorySource#clone(80c95016c20e3f25).(VMG_cxx . . $ `tads3/vmglob.h:349:9`! const char * /*mode*/)
928 {
929 /* return a new data source wrapper for our page list */
930 return new CVmMemorySourcecxx . . $ CVmMemorySource#(plcxx . . $ CVmMemorySource#pl.);
931 }
932
933 ~cxx . . $ CVmMemorySource#`~CVmMemorySource`(ced63f7c635d850d).CVmMemorySourcecxx . . $ CVmMemorySource#()
934 {
935 /* release our underlying page list */
936 plcxx . . $ CVmMemorySource#pl.->release_refcxx . . $ CVmRefCntObj#release_ref(49f6e7a06ebc5aa8).();
937 }
938
939 /* read bytes */
940 virtual int readcxx . . $ CVmMemorySource#read(c0a5f96701d3f89).(void *buf, size_t len) { return plcxx . . $ CVmMemorySource#pl.->readcxx . . $ CVmMemorySourceList#read(c0a5f96701d3f89).(buf, len); }
941
942 /* read bytes */
943 virtual int readccxx . . $ CVmMemorySource#readc(c0a5f96701d3f89).(void *buf, size_t len) { return plcxx . . $ CVmMemorySource#pl.->readccxx . . $ CVmMemorySourceList#readc(c0a5f96701d3f89).(buf, len); }
944
945 /* write bytes */
946 virtual int writecxx . . $ CVmMemorySource#write(a10e65277395b633).(const void *buf, size_t len)
947 { return plcxx . . $ CVmMemorySource#pl.->writecxx . . $ CVmMemorySourceList#write(a10e65277395b633).(buf, len); }
948
949 /* get the length of the stream's contents */
950 virtual long get_sizecxx . . $ CVmMemorySource#get_size(da6335ded91d79c2).() { return plcxx . . $ CVmMemorySource#pl.->get_sizecxx . . $ CVmMemorySourceList#get_size(da6335ded91d79c2).(); }
951
952 /* get the current seek offset */
953 virtual long get_poscxx . . $ CVmMemorySource#get_pos(da6335ded91d79c2).() { return plcxx . . $ CVmMemorySource#pl.->get_poscxx . . $ CVmMemorySourceList#get_pos(da6335ded91d79c2).(); }
954
955 /* set the seek offset */
956 virtual int seekcxx . . $ CVmMemorySource#seek(4a3ed3c01b538bac).(long pos, int mode) { return plcxx . . $ CVmMemorySource#pl.->seekcxx . . $ CVmMemorySourceList#seek(4a3ed3c01b538bac).(pos, mode); }
957
958 /* flush - we don't buffer, so this does nothing */
959 virtual int flushcxx . . $ CVmMemorySource#flush(b126dc7c1de90089).() { return 0; }
960
961 /*
962 * close - we don't have underlying system resources (other than
963 * our memory buffers), so there's nothing to do here
964 */
965 virtual void closecxx . . $ CVmMemorySource#close(49f6e7a06ebc5aa8).() { }
966
967 protected:
968 CVmMemorySourcecxx . . $ CVmMemorySource#CVmMemorySource(4bdd9cb1344fc1b4).(CVmMemorySourceListcxx . . $ CVmMemorySourceList# *pl)
969 {
970 pl->add_refcxx . . $ CVmRefCntObj#add_ref(49f6e7a06ebc5aa8).();
971 this->plcxx . . $ CVmMemorySource#pl. = pl;
972 }
973
974 /* our underlying page list */
975 CVmMemorySourceListcxx . . $ CVmMemorySourceList# *plcxx . . $ CVmMemorySource#pl.;
976 };
977
978
979 #endif /* VMDATASRC_H */