Translated using Weblate (Chinese (Simplified))
[cygwin-setup.git] / io_stream_memory.h
blob21d3e14fbf602398afba7f1912ba5e8bb6799d4a
1 /*
2 * Copyright (c) 2001, Robert Collins.
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.
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
12 * Written by Robert Collins <rbtcollins@hotmail.com>
16 #ifndef SETUP_IO_STREAM_MEMORY_H
17 #define SETUP_IO_STREAM_MEMORY_H
19 /* needed to parse */
20 #include <errno.h>
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.
28 class memblock
30 public:
31 memblock ():next (0), len (0), data (0)
33 memblock (size_t size):next (0), len (size)
35 data = new unsigned char[size];
36 if (!data)
37 len = 0;
39 ~memblock ();
40 memblock *next;
41 size_t len;
42 unsigned char *data;
45 class io_stream_memory:public io_stream
47 public:
48 io_stream_memory (): lasterr (0), mtime (0), length (0), head (),
49 tail (&head), pos_block (head.next), pos_block_offset (0), pos (0)
51 /* set the modification time of a file - returns 1 on failure
52 * may distrupt internal state - use after all important io is complete
54 virtual int set_mtime (time_t newmtime)
56 mtime = newmtime;
57 return 0;
59 /* get the mtime for a file TODO make this a stat(0 style call */
60 virtual time_t get_mtime () { return mtime; }
61 virtual mode_t get_mode () { return 0; }
62 /* returns the _current_ size. */
63 virtual size_t get_size () { return length; }
64 /* read data (duh!) */
65 virtual ssize_t read (void *buffer, size_t len);
66 /* provide data to (double duh!) */
67 virtual ssize_t write (const void *buffer, size_t len);
68 /* read data without removing it from the class's internal buffer */
69 virtual ssize_t peek (void *buffer, size_t len);
70 /* ever read the f* functions from libc ? */
71 virtual off_t tell ()
73 return pos;
75 virtual off_t seek (off_t where, io_stream_seek_t whence)
77 if (whence != IO_SEEK_SET)
79 lasterr = EINVAL;
80 return -1;
82 ssize_t count = 0;
83 pos = 0;
84 pos_block = head.next;
85 pos_block_offset = 0;
86 while (count < where && pos < (off_t) length)
88 pos_block_offset++;
89 if (pos_block_offset == pos_block->len)
91 pos_block = pos_block->next;
92 pos_block_offset = 0;
94 pos++;
95 count++;
97 return 0;
100 /* try guessing this one */
101 virtual int error ();
102 // virtual const char* next_file_name() = NULL;
103 /* if you are still needing these hints... give up now! */
104 virtual ~io_stream_memory ();
105 private:
106 int lasterr;
107 time_t mtime;
108 size_t length;
109 memblock head;
110 memblock *tail;
111 memblock *pos_block;
112 size_t pos_block_offset;
113 off_t pos;
116 #endif /* SETUP_IO_STREAM_MEMORY_H */