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
12 * Written by Robert Collins <rbtcollins@hotmail.com>
22 #include "io_stream.h"
23 #include "io_stream_memory.h"
25 /* memblock helper class */
26 memblock::~memblock ()
34 io_stream_memory::~io_stream_memory ()
36 /* memblocks are self deleting. Nice of 'em eh what */
43 io_stream_memory::read (void *buffer
, size_t len
)
47 unsigned char *to
= (unsigned char *) buffer
;
48 unsigned char *end
= to
+ len
;
50 while (to
< end
&& pos
< length
)
52 *to
++ = pos_block
->data
[pos_block_offset
++];
54 if (pos_block_offset
== pos_block
->len
)
56 pos_block
= pos_block
->next
;
65 io_stream_memory::write (const void *buffer
, size_t len
)
69 /* talk about primitive :} */
70 tail
->next
= new memblock (len
);
71 if (!tail
->next
->data
)
79 memcpy (tail
->data
, buffer
, len
);
82 pos_block_offset
= len
;
88 io_stream_memory::peek (void *buffer
, size_t len
)
91 size_t toff
= pos_block_offset
;
92 memblock
*tblock
= pos_block
;
93 ssize_t tmp
= read (buffer
, len
);
95 pos_block_offset
= toff
;
101 io_stream_memory::error ()