Debian Codegraph @

terps/tads/tads3/vmdatasrc.h

raw symbols SCIP
1 /* $Header$ */
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_H
20
21 #include <string.h>
22 #include "t3std.h"
23 #include "osifcnet.h"
24 #include "vmerr.h"
25 #include "vmfile.h"
26 #include "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 CVmDataSource
35 {
36 public:
37 virtual ~CVmDataSource() { }
38
39 /* read bytes - returns 0 on success, non-zero on EOF or error */
40 virtual int read(void *buf, size_t len) = 0;
41
42 /* read bytes - returns the number of bytes read; 0 means EOF or error */
43 virtual int readc(void *buf, size_t len) = 0;
44
45 /* write bytes - returns 0 on success, non-zero on error */
46 virtual int write(const void *buf, size_t len) = 0;
47
48 /* write a null-terminated string */
49 virtual int writez(const char *str) { return write(str, strlen(str)); }
50
51 /* get the length of the file in bytes */
52 virtual long get_size() = 0;
53
54 /* get the current seek location */
55 virtual long get_pos() = 0;
56
57 /* set the current seek location - 'mode' is an OSFSK_xxx mode */
58 virtual int seek(long ofs, int mode) = 0;
59
60 /* flush - returns 0 on success, non-zero on error */
61 virtual int flush() = 0;
62
63 /* close the underlying system resource */
64 virtual void close() = 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_line(char *buf, size_t len)
73 {
74 /* if we have no bytes left, return null for EOF */
75 if (get_pos() >= get_size())
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_pos() < get_size())
81 {
82 /* read the next character */
83 char ch;
84 if (read(&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_pos() < get_size()
105 && !read(&ch2, 1)
106 && ch2 != 10)
107 {
108 /* it's not a CR-LF - un-get the second character */
109 seek(get_pos() - 1, OSFSK_SET);
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_alo()
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_line(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 *)t3malloc(len + 1);
151 else
152 ret = (char *)t3realloc(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 CVmDataSource *clone(VMG_ const char *mode) = 0;
182 };
183
184
185 /* ------------------------------------------------------------------------ */
186 /*
187 * Basic OS file data source.
188 */
189 class CVmFileSource: public CVmDataSource
190 {
191 public:
192 CVmFileSource(osfildef *fp) { this->fp = fp; }
193 ~CVmFileSource()
194 {
195 if (fp != 0)
196 close();
197 }
198
199 /* read bytes - returns 0 on success, non-zero on EOF or error */
200 virtual int read(void *buf, size_t len) { return osfrb(fp, buf, len); }
201
202 /* read bytes - returns 0 on success, non-zero on EOF or error */
203 virtual int readc(void *buf, size_t len) { return osfrbc(fp, buf, len); }
204
205 /* read a line of text (fgets() semantics) */
206 virtual char *read_line(char *buf, size_t len)
207 { return osfgets(buf, len, fp); }
208
209 /* write bytes - returns 0 on success, non-zero on error*/
210 virtual int write(const void *buf, size_t len)
211 { return osfwb(fp, buf, len); }
212
213 /* get the length of the file in bytes */
214 virtual long get_size()
215 {
216 /* remember the current seek location */
217 long oldpos = osfpos(fp);
218
219 /* seek to the end */
220 osfseek(fp, 0, OSFSK_END);
221
222 /* the current position is the file size */
223 long siz = osfpos(fp);
224
225 /* seek back to where we started */
226 osfseek(fp, oldpos, OSFSK_SET);
227
228 /* return the size */
229 return siz;
230 }
231
232 /* get the current seek location */
233 virtual long get_pos() { return osfpos(fp); }
234
235 /* set the current seek location - 'mode' is an OSFSK_xxx mode */
236 virtual int seek(long ofs, int mode) { return osfseek(fp, ofs, mode); }
237
238 /* flush - returns 0 on success, non-zero on error */
239 virtual int flush() { return osfflush(fp); }
240
241 /* close the underlying system resource */
242 virtual void close()
243 {
244 osfcls(fp);
245 fp = 0;
246 }
247
248 /* clone the source */
249 virtual CVmDataSource *clone(VMG_ const char *mode)
250 {
251 /* duplicate our underlying file handle */
252 osfildef *fpdup = osfdup(fp, mode);
253 if (fpdup == 0)
254 return 0;
255
256 /* return a new source wrapping the duplicated file handle */
257 return new CVmFileSource(fpdup);
258 }
259
260 protected:
261 /* the underlying file */
262 osfildef *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 CVmResFileSource: public CVmDataSource
272 {
273 public:
274 CVmResFileSource(osfildef *fp, unsigned long start, long end)
275 {
276 this->fp = fp;
277 this->start = start;
278 this->end = end;
279 }
280 ~CVmResFileSource()
281 {
282 if (fp != 0)
283 close();
284 }
285
286 /* clone the source */
287 virtual CVmDataSource *clone(VMG_ const char *mode)
288 {
289 /* duplicate our underlying file handle */
290 osfildef *fpdup = osfdup(fp, mode);
291 if (fpdup == 0)
292 return 0;
293
294 /* return a new source wrapping the duplicated file handle */
295 return new CVmResFileSource(fpdup, start, end);
296 }
297
298 /* read bytes - returns 0 on success, non-zero on EOF or error */
299 virtual int read(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(fp);
303 if (pos + len > end)
304 return 1;
305
306 /* do the read */
307 return osfrb(fp, buf, len);
308 }
309
310 /* read a line of text (fgets() semantics) */
311 virtual char *read_line(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(fp);
315 if (pos >= end)
316 return 0;
317
318 /* read a line */
319 char *ret = osfgets(buf, len, 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(fp) > end)
328 {
329 /* seek back to where we started */
330 osfseek(fp, pos, OSFSK_SET);
331
332 /* read the remaining bytes in the resource (up to buflen) */
333 size_t r = end - pos > len ? (long)len : (size_t)(end - pos);
334 if (osfrb(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 readc(void *buf, size_t len)
348 {
349 /* if we're already past the end, we can't read anything */
350 unsigned long pos = osfpos(fp);
351 if (pos >= end)
352 return 0;
353
354 /* limit the read to the available file size */
355 unsigned long limit = end - pos;
356 if (len > limit)
357 len = (size_t)limit;
358
359 /* do the read */
360 return osfrbc(fp, buf, len);
361 }
362
363 /* write bytes - resource files are read-only */
364 virtual int write(const void *buf, size_t len) { return 1; }
365
366 /* get the length of the file in bytes */
367 virtual long get_size() { return end - start; }
368
369 /* get the current seek location */
370 virtual long get_pos() { return osfpos(fp) - start; }
371
372 /* set the current seek location - 'mode' is an OSFSK_xxx mode */
373 virtual int seek(long ofs, int mode)
374 {
375 switch (mode)
376 {
377 case OSFSK_SET:
378 do_set:
379 /* check that 'ofs' is in range */
380 if (ofs < 0 || (unsigned long)ofs > end - start)
381 return 1;
382
383 /* seek to the offset */
384 return osfseek(fp, start + ofs, OSFSK_SET);
385
386 case OSFSK_CUR:
387 /* set the offset relative to the current position */
388 ofs += get_pos();
389 goto do_set;
390
391 case OSFSK_END:
392 /* set the offset relative to the end of the file */
393 ofs += (end - 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 flush() { return 0; }
404
405 /* close the underlying system resource */
406 virtual void close()
407 {
408 osfcls(fp);
409 fp = 0;
410 }
411
412 protected:
413 /* the underlying file */
414 osfildef *fp;
415
416 /* the byte range within the resource file */
417 unsigned long start, 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 CVmStringSource: public CVmDataSource
427 {
428 public:
429 CVmStringSource(const char *mem, long len)
430 {
431 this->mem = mem;
432 this->memlen = len;
433 this->pos = 0;
434 }
435
436 virtual ~CVmStringSource() { }
437
438 virtual CVmDataSource *clone(VMG_ const char * /*mode*/)
439 { return new CVmStringSource(mem, memlen); }
440
441 /* read bytes - returns 0 on success, non-zero on EOF or error */
442 virtual int read(void *buf, size_t len)
443 {
444 /* make sure there's enough data to satisfy the request */
445 if (pos + (long)len > memlen)
446 return 1;
447
448 /* copy the data */
449 memcpy(buf, mem + pos, len);
450
451 /* advance the read pointer */
452 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 readc(void *buf, size_t len)
460 {
461 /* limit the read to the available length */
462 long rem = memlen - pos;
463 if ((long)len > rem)
464 len = rem;
465
466 /* copy the bytes */
467 memcpy(buf, mem + pos, len);
468
469 /* advance the read pointer */
470 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_line(char *buf, size_t len)
478 {
479 /* if we have no bytes left, return null for EOF */
480 if (pos == memlen)
481 return 0;
482
483 /* read bytes until we encounter a CR, LF, or CR-LF */
484 char *dst = buf;
485 while (pos < memlen && len > 0)
486 {
487 /* check for a line terminator */
488 if (mem[pos] == 10 || mem[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 ++pos;
503 if (pos < memlen && mem[pos-1] == 13 && mem[pos] == 10)
504 ++pos;
505
506 /* we're done */
507 break;
508 }
509
510 /* copy this byte */
511 *dst++ = mem[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 write(const void *buf, size_t len) { return 1; }
527
528 /* get the length of the file in bytes */
529 virtual long get_size() { return memlen; }
530
531 /* get the current seek location */
532 virtual long get_pos() { return pos; }
533
534 /* set the current seek location - 'mode' is an OSFSK_xxx mode */
535 virtual int seek(long ofs, int mode)
536 {
537 switch (mode)
538 {
539 case OSFSK_SET:
540 do_set:
541 /* make sure it's in range */
542 if (ofs < 0 || ofs > memlen)
543 return 1;
544
545 /* set the new position */
546 pos = ofs;
547
548 /* success */
549 return 0;
550
551 case OSFSK_CUR:
552 /* calculate the absolute position and go do the set */
553 ofs += pos;
554 goto do_set;
555
556 case OSFSK_END:
557 /* calculate the absolute position and go do the set */
558 ofs += 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 flush() { return 0; }
569
570 /* close the underlying system resource; there's nothing for us to do */
571 virtual void close() { }
572
573 protected:
574 /* our memory buffer, and the number of bytes in the buffer */
575 const char *mem;
576 long memlen;
577
578 /* current read position */
579 long pos;
580 };
581
582 /* ------------------------------------------------------------------------ */
583 /*
584 * Private string source - makes a private copy of a string buffer
585 */
586
587 class CVmPrivateStringSource: public CVmStringSource
588 {
589 public:
590 CVmPrivateStringSource(const char *mem, long len)
591 : CVmStringSource(0, len)
592 {
593 this->bufobj = new CVmRefCntBuf(mem, len);
594 this->mem = bufobj->buf_;
595 }
596
597 CVmPrivateStringSource(CVmRefCntBuf *bufobj, long len)
598 : CVmStringSource(bufobj->buf_, len)
599 {
600 this->bufobj = bufobj;
601 }
602
603 CVmDataSource *clone(VMG_ const char * /*mode*/)
604 { return new CVmPrivateStringSource(bufobj, memlen); }
605
606 ~CVmPrivateStringSource()
607 {
608 bufobj->release_ref();
609 }
610
611 protected:
612 CVmRefCntBuf *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 CVmMemorySourceBlock
624 {
625 CVmMemorySourceBlock(long ofs)
626 {
627 this->ofs = ofs;
628 nxt = 0;
629 }
630
631 /* block length */
632 static const int BlockLen = 4096;
633
634 /* next block in the list */
635 CVmMemorySourceBlock *nxt;
636
637 /* byte offset in the overall stream of the start of this block */
638 long ofs;
639
640 /* block buffer */
641 char buf[BlockLen];
642 };
643
644 /* page list for a memory source */
645 class CVmMemorySourceList: public CVmRefCntObj
646 {
647 public:
648 static const int BlockLen = CVmMemorySourceBlock::BlockLen;
649
650 CVmMemorySourceList(long init_len)
651 {
652 /* no blocks yet */
653 first_block_ = last_block_ = 0;
654
655 /* always allocate at least the initial block */
656 add_block();
657 init_len -= BlockLen;
658
659 /* set the seek position to the start of the first block */
660 cur_block_ = first_block_;
661 cur_block_ofs_ = 0;
662
663 /* add more blocks until we've satisfied the length request */
664 for ( ; init_len > 0 ; init_len -= BlockLen)
665 add_block();
666
667 /* the file is currently empty */
668 len_ = 0;
669 }
670
671 /* read bytes */
672 int read(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_->ofs + cur_block_ofs_ + (long)len > 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)readc(buf, len) != len;
686 }
687
688 /* read bytes */
689 int readc(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_ - get_pos();
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 = BlockLen - 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_->buf + cur_block_ofs_, cur);
714
715 /* move past the bytes in the current block */
716 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_->nxt == 0)
732 return bytes_read;
733
734 /* advance to the start of the next block */
735 cur_block_ = cur_block_->nxt;
736 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 write(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 = BlockLen - 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_->buf + cur_block_ofs_, buf, cur);
760 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_->nxt == 0)
771 {
772 /* add the block */
773 add_block();
774
775 /* if that didn't work, fail */
776 if (cur_block_->nxt == 0)
777 return 1;
778 }
779
780 /* advance to the start of the next block */
781 cur_block_ = cur_block_->nxt;
782 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_->ofs + cur_block_ofs_ > len_)
791 len_ = cur_block_->ofs + cur_block_ofs_;
792
793 /* success */
794 return 0;
795 }
796
797 /* get the length of the stream's contents */
798 long get_size() { return len_; }
799
800 /* get the current seek offset */
801 long get_pos()
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_->ofs + cur_block_ofs_;
808 }
809
810 /* set the seek offset */
811 int seek(long pos, int mode)
812 {
813 /* figure the absolute position based on the mode */
814 switch (mode)
815 {
816 case OSFSK_SET:
817 /* from the beginning - use pos as given */
818 break;
819
820 case OSFSK_CUR:
821 /* from the current position */
822 pos += get_pos();
823 break;
824
825 case OSFSK_END:
826 /* from the end */
827 pos += get_size();
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_)
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_;
850 cur_block_ = last_block_;
851 cur_block_ofs_ = pos - last_block_->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 CVmMemorySourceBlock *b;
859 for (b = first_block_ ; b != 0 ; b = b->nxt)
860 {
861 /* if the offset is within this block, we're done */
862 if (pos >= b->ofs && pos < b->ofs + BlockLen)
863 {
864 /* set the current pointers */
865 cur_block_ = b;
866 cur_block_ofs_ = pos - b->ofs;
867
868 /* stop scanning */
869 break;
870 }
871 }
872
873 /* success */
874 return 0;
875 }
876
877 protected:
878 ~CVmMemorySourceList()
879 {
880 /* delete our block list */
881 CVmMemorySourceBlock *cur, *nxt;
882 for (cur = first_block_ ; cur != 0 ; cur = nxt)
883 {
884 nxt = cur->nxt;
885 delete cur;
886 }
887 }
888
889 /* add a block to the end of the list */
890 void add_block()
891 {
892 long ofs = last_block_ == 0 ? 0 : last_block_->ofs + BlockLen;
893 CVmMemorySourceBlock *b = new CVmMemorySourceBlock(ofs);
894
895 if (last_block_ != 0)
896 last_block_->nxt = b;
897 else
898 first_block_ = b;
899 last_block_ = b;
900 }
901
902 /* current seek block and offset */
903 CVmMemorySourceBlock *cur_block_;
904 long cur_block_ofs_;
905
906 /* total file length */
907 long len_;
908
909 /* head/tail of block list */
910 CVmMemorySourceBlock *first_block_;
911 CVmMemorySourceBlock *last_block_;
912 };
913
914 /*
915 * Paged memory source
916 */
917 class CVmMemorySource: public CVmDataSource
918 {
919 public:
920 CVmMemorySource(long init_len)
921 {
922 /* create our underlying page list */
923 pl = new CVmMemorySourceList(init_len);
924 }
925
926 /* clone the data source */
927 CVmDataSource *clone(VMG_ const char * /*mode*/)
928 {
929 /* return a new data source wrapper for our page list */
930 return new CVmMemorySource(pl);
931 }
932
933 ~CVmMemorySource()
934 {
935 /* release our underlying page list */
936 pl->release_ref();
937 }
938
939 /* read bytes */
940 virtual int read(void *buf, size_t len) { return pl->read(buf, len); }
941
942 /* read bytes */
943 virtual int readc(void *buf, size_t len) { return pl->readc(buf, len); }
944
945 /* write bytes */
946 virtual int write(const void *buf, size_t len)
947 { return pl->write(buf, len); }
948
949 /* get the length of the stream's contents */
950 virtual long get_size() { return pl->get_size(); }
951
952 /* get the current seek offset */
953 virtual long get_pos() { return pl->get_pos(); }
954
955 /* set the seek offset */
956 virtual int seek(long pos, int mode) { return pl->seek(pos, mode); }
957
958 /* flush - we don't buffer, so this does nothing */
959 virtual int flush() { 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 close() { }
966
967 protected:
968 CVmMemorySource(CVmMemorySourceList *pl)
969 {
970 pl->add_ref();
971 this->pl = pl;
972 }
973
974 /* our underlying page list */
975 CVmMemorySourceList *pl;
976 };
977
978
979 #endif /* VMDATASRC_H */