]> cygwin.com Git - cygwin-apps/setup.git/blob - io_stream_memory.cc
* prereq.cc (PrereqChecker::getUnmetString): Improve dependency list
[cygwin-apps/setup.git] / io_stream_memory.cc
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>
23 #include <string.h>
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)
34 delete[] data;
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 */
42 }
43
44 /* virtuals */
45
46
47 ssize_t
48 io_stream_memory::read (void *buffer, size_t len)
49 {
50 if (len == 0)
51 return 0;
52 unsigned char *to = (unsigned char *) buffer;
53 unsigned char *end = to + len;
54 ssize_t count = 0;
55 while (to < end && pos < length)
56 {
57 *to++ = pos_block->data[pos_block_offset++];
58 count++;
59 if (pos_block_offset == pos_block->len)
60 {
61 pos_block = pos_block->next;
62 pos_block_offset = 0;
63 }
64 pos++;
65 }
66 return count;
67 }
68
69 ssize_t
70 io_stream_memory::write (const void *buffer, size_t len)
71 {
72 if (len == 0)
73 return 0;
74 /* talk about primitive :} */
75 tail->next = new memblock (len);
76 if (!tail->next->data)
77 {
78 delete tail->next;
79 tail->next = 0;
80 lasterr = ENOMEM;
81 return -1;
82 }
83 tail = tail->next;
84 memcpy (tail->data, buffer, len);
85 pos += len;
86 pos_block = tail;
87 pos_block_offset = len;
88 length += len;
89 return len;
90 }
91
92 ssize_t
93 io_stream_memory::peek (void *buffer, size_t len)
94 {
95 size_t tpos = pos;
96 size_t toff = pos_block_offset;
97 memblock *tblock = pos_block;
98 ssize_t tmp = read (buffer, len);
99 pos = tpos;
100 pos_block_offset = toff;
101 pos_block = tblock;
102 return tmp;
103 }
104
105 int
106 io_stream_memory::error ()
107 {
108 return lasterr;
109 }
This page took 0.037176 seconds and 5 git commands to generate.