]>
Commit | Line | Data |
---|---|---|
ca9506cc RC |
1 | /* |
2 | * Copyright (c) 2001, Robert Collins. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins <rbtcollins@hotmail.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | #ifndef _IO_STREAM_MEMORY_H_ | |
17 | #define _IO_STREAM_MEMORY_H_ | |
18 | ||
341988b9 RC |
19 | /* needed to parse */ |
20 | #include <errno.h> | |
21 | ||
ca9506cc RC |
22 | /* this is a stream class that simply abstracts the issue of maintaining |
23 | * amemory buffer. | |
24 | * It's not as efficient as if can be, but that can be fixed without affecting | |
25 | * the API. | |
26 | */ | |
27 | ||
28 | class memblock | |
29 | { | |
30 | public: | |
31 | memblock () : next (0), len (0), data (0) {}; | |
5e0464a1 | 32 | memblock (size_t size) : next (0), len (size) {data = new unsigned char[size]; if (!data) len = 0;}; |
ca9506cc RC |
33 | ~memblock (); |
34 | memblock *next; | |
35 | size_t len; | |
36 | unsigned char *data; | |
37 | }; | |
38 | ||
39 | class io_stream_memory :public io_stream | |
40 | { | |
41 | public: | |
42 | io_stream_memory () : lasterr (0), mtime(0),length (0), head(), tail (&head), pos_block (head.next), pos_block_offset (0), pos (0) {}; | |
43 | /* set the modification time of a file - returns 1 on failure | |
44 | * may distrupt internal state - use after all important io is complete | |
45 | */ | |
46 | virtual int set_mtime (int newmtime) {mtime = newmtime;return 0;}; | |
47 | /* get the mtime for a file TODO make this a stat(0 style call */ | |
48 | virtual int get_mtime () {return mtime;}; | |
341988b9 RC |
49 | /* returns the _current_ size. */ |
50 | virtual size_t get_size () {return length;}; | |
ca9506cc | 51 | /* read data (duh!) */ |
ca9506cc RC |
52 | virtual ssize_t read (void *buffer, size_t len); |
53 | /* provide data to (double duh!) */ | |
54 | virtual ssize_t write (const void *buffer, size_t len); | |
55 | /* read data without removing it from the class's internal buffer */ | |
56 | virtual ssize_t peek (void *buffer, size_t len); | |
57 | /* ever read the f* functions from libc ? */ | |
58 | virtual long tell () {return pos;}; | |
59 | virtual int seek (long where, io_stream_seek_t whence) { if (whence != IO_SEEK_SET) { lasterr = EINVAL; return -1;} ssize_t count=0; pos = 0; pos_block = head.next; pos_block_offset = 0; while (count < where && pos < length) {pos_block_offset++; if (pos_block_offset == pos_block->len) {pos_block = pos_block->next; pos_block_offset = 0;}pos++;count++;}return 0;}; | |
60 | ||
61 | /* try guessing this one */ | |
62 | virtual int error (); | |
63 | // virtual const char* next_file_name() = NULL; | |
64 | /* if you are still needing these hints... give up now! */ | |
65 | virtual ~ io_stream_memory (); | |
66 | private: | |
67 | int lasterr; | |
68 | int mtime; | |
69 | size_t length; | |
70 | memblock head; | |
71 | memblock *tail; | |
72 | memblock *pos_block; | |
73 | size_t pos_block_offset; | |
74 | size_t pos; | |
75 | }; | |
76 | ||
77 | #endif /* _IO_STREAM_MEMORY_H_ */ |