Handle TTN_GETDISPINFOW
[cygwin-setup.git] / io_stream_memory.cc
blobcbdaf8372b1163d3e7d680d9fba84a9013fb688c
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 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <errno.h>
22 #include "io_stream.h"
23 #include "io_stream_memory.h"
25 /* memblock helper class */
26 memblock::~memblock ()
28 if (data)
29 delete[] data;
30 if (next)
31 delete next;
34 io_stream_memory::~io_stream_memory ()
36 /* memblocks are self deleting. Nice of 'em eh what */
39 /* virtuals */
42 ssize_t
43 io_stream_memory::read (void *buffer, size_t len)
45 if (len == 0)
46 return 0;
47 unsigned char *to = (unsigned char *) buffer;
48 unsigned char *end = to + len;
49 ssize_t count = 0;
50 while (to < end && pos < length)
52 *to++ = pos_block->data[pos_block_offset++];
53 count++;
54 if (pos_block_offset == pos_block->len)
56 pos_block = pos_block->next;
57 pos_block_offset = 0;
59 pos++;
61 return count;
64 ssize_t
65 io_stream_memory::write (const void *buffer, size_t len)
67 if (len == 0)
68 return 0;
69 /* talk about primitive :} */
70 tail->next = new memblock (len);
71 if (!tail->next->data)
73 delete tail->next;
74 tail->next = 0;
75 lasterr = ENOMEM;
76 return -1;
78 tail = tail->next;
79 memcpy (tail->data, buffer, len);
80 pos += len;
81 pos_block = tail;
82 pos_block_offset = len;
83 length += len;
84 return len;
87 ssize_t
88 io_stream_memory::peek (void *buffer, size_t len)
90 size_t tpos = pos;
91 size_t toff = pos_block_offset;
92 memblock *tblock = pos_block;
93 ssize_t tmp = read (buffer, len);
94 pos = tpos;
95 pos_block_offset = toff;
96 pos_block = tblock;
97 return tmp;
101 io_stream_memory::error ()
103 return lasterr;