]>
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 | #if 0 | |
17 | static const char *cvsid = | |
18 | "\n%%% $Id$\n"; | |
19 | #endif | |
20 | ||
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
f71e9756 | 23 | #include <string.h> |
ca9506cc RC |
24 | #include <unistd.h> |
25 | #include <errno.h> | |
26 | ||
27 | #include "io_stream.h" | |
28 | #include "io_stream_memory.h" | |
29 | ||
30 | /* memblock helper class */ | |
31 | memblock::~memblock () | |
32 | { | |
33 | if (data) | |
5e0464a1 | 34 | delete[] data; |
ca9506cc RC |
35 | if (next) |
36 | delete next; | |
37 | } | |
38 | ||
39 | io_stream_memory::~io_stream_memory () | |
40 | { | |
41 | /* memblocks are self deleting. Nice of 'em eh what */ | |
e9440f0f | 42 | destroyed = 1; |
ca9506cc RC |
43 | } |
44 | ||
45 | /* virtuals */ | |
46 | ||
47 | ||
48 | ssize_t | |
49 | io_stream_memory::read (void *buffer, size_t len) | |
50 | { | |
51 | if (len == 0) | |
52 | return 0; | |
53 | unsigned char *to = (unsigned char *) buffer; | |
54 | unsigned char *end = to + len; | |
55 | ssize_t count = 0; | |
56 | while (to < end && pos < length) | |
57 | { | |
58 | *to++ = pos_block->data[pos_block_offset++]; | |
59 | count++; | |
60 | if (pos_block_offset == pos_block->len) | |
61 | { | |
62 | pos_block = pos_block->next; | |
63 | pos_block_offset = 0; | |
64 | } | |
65 | pos++; | |
66 | } | |
67 | return count; | |
68 | } | |
69 | ||
70 | ssize_t | |
71 | io_stream_memory::write (const void *buffer, size_t len) | |
72 | { | |
73 | if (len == 0) | |
74 | return 0; | |
75 | /* talk about primitive :} */ | |
76 | tail->next = new memblock (len); | |
77 | if (!tail->next->data) | |
78 | { | |
79 | delete tail->next; | |
80 | tail->next = 0; | |
81 | lasterr = ENOMEM; | |
82 | return -1; | |
83 | } | |
84 | tail = tail->next; | |
85 | memcpy (tail->data, buffer, len); | |
86 | pos += len; | |
87 | pos_block = tail; | |
88 | pos_block_offset = len; | |
89 | length += len; | |
90 | return len; | |
91 | } | |
92 | ||
93 | ssize_t | |
94 | io_stream_memory::peek (void *buffer, size_t len) | |
95 | { | |
96 | size_t tpos = pos; | |
97 | size_t toff = pos_block_offset; | |
98 | memblock *tblock = pos_block; | |
99 | ssize_t tmp = read (buffer, len); | |
100 | pos = tpos; | |
101 | pos_block_offset = toff; | |
102 | pos_block = tblock; | |
103 | return tmp; | |
104 | } | |
105 | ||
106 | int | |
107 | io_stream_memory::error () | |
108 | { | |
109 | return lasterr; | |
110 | } |