]> cygwin.com Git - cygwin-apps/setup.git/blob - io_stream_memory.cc
Suggest URLs for updated setup based on build architecture
[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 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <errno.h>
21
22 #include "io_stream.h"
23 #include "io_stream_memory.h"
24
25 /* memblock helper class */
26 memblock::~memblock ()
27 {
28 if (data)
29 delete[] data;
30 if (next)
31 delete next;
32 }
33
34 io_stream_memory::~io_stream_memory ()
35 {
36 /* memblocks are self deleting. Nice of 'em eh what */
37 }
38
39 /* virtuals */
40
41
42 ssize_t
43 io_stream_memory::read (void *buffer, size_t len)
44 {
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 < (off_t) length)
51 {
52 *to++ = pos_block->data[pos_block_offset++];
53 count++;
54 if (pos_block_offset == pos_block->len)
55 {
56 pos_block = pos_block->next;
57 pos_block_offset = 0;
58 }
59 pos++;
60 }
61 return count;
62 }
63
64 ssize_t
65 io_stream_memory::write (const void *buffer, size_t len)
66 {
67 if (len == 0)
68 return 0;
69 /* talk about primitive :} */
70 tail->next = new memblock (len);
71 if (!tail->next->data)
72 {
73 delete tail->next;
74 tail->next = 0;
75 lasterr = ENOMEM;
76 return -1;
77 }
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;
85 }
86
87 ssize_t
88 io_stream_memory::peek (void *buffer, size_t len)
89 {
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;
98 }
99
100 int
101 io_stream_memory::error ()
102 {
103 return lasterr;
104 }
This page took 0.045299 seconds and 6 git commands to generate.