migrate substitution keywords to SVN
[reactos.git] / reactos / lib / kjs / ksrc / iostream.c
1 /*
2 * I/O streams.
3 * Copyright (c) 1998 New Generation Software (NGS) Oy
4 *
5 * Author: Markku Rossi <mtr@ngs.fi>
6 */
7
8 /*
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * MA 02111-1307, USA
23 */
24
25 /*
26 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/ksrc/iostream.c,v $
27 * $Id$
28 */
29
30 #include "jsint.h"
31
32 /*
33 * Types and definitions.
34 */
35
36 #define DEFAULT_BUFFER_SIZE 4096
37
38 /*
39 * Global functions.
40 */
41
42 JSIOStream *
43 js_iostream_new ()
44 {
45 JSIOStream *stream;
46
47 stream = js_calloc (NULL, 1, sizeof (*stream));
48 if (stream == NULL)
49 return NULL;
50
51 stream->buflen = DEFAULT_BUFFER_SIZE;
52 stream->buffer = js_malloc (NULL, stream->buflen);
53 if (stream->buffer == NULL)
54 {
55 js_free (stream);
56 return NULL;
57 }
58
59 return stream;
60 }
61
62 size_t
63 js_iostream_read (JSIOStream *stream, void *ptr, size_t size)
64 {
65 size_t total = 0;
66 int got;
67
68 if (stream->writep)
69 {
70 /* We have buffered output data. */
71 if (js_iostream_flush (stream) == EOF)
72 return 0;
73
74 assert (stream->writep == 0);
75 }
76
77 while (size > 0)
78 {
79 /* First, take everything from the buffer. */
80 if (stream->bufpos < stream->data_in_buf)
81 {
82 got = stream->data_in_buf - stream->bufpos;
83
84 if (size < got)
85 got = size;
86
87 memcpy (ptr, stream->buffer + stream->bufpos, got);
88
89 stream->bufpos += got;
90 size -= got;
91 ptr = (void *)((unsigned char *)ptr + got);
92 total += got;
93 }
94 else
95 {
96 if (stream->at_eof)
97 /* EOF seen, can't read more. */
98 break;
99
100 js_iostream_fill_buffer (stream);
101 }
102 }
103
104 return total;
105 }
106
107
108 size_t
109 js_iostream_write (JSIOStream *stream, void *ptr, size_t size)
110 {
111 int i;
112
113 for( i = 0; size > i; i++ )
114 DbgPrint("%c", ((char *)ptr)[i]);
115
116 return i;
117 }
118
119
120 int
121 js_iostream_flush (JSIOStream *stream)
122 {
123 return 0;
124 }
125
126 int
127 js_iostream_unget (JSIOStream *stream, int byte)
128 {
129 return 0;
130 }
131
132 int
133 js_iostream_close (JSIOStream *stream)
134 {
135 int result = 0;
136
137 if (stream == NULL)
138 return result;
139
140 if (js_iostream_flush (stream) == EOF)
141 result = EOF;
142
143 if (stream->close)
144 (*stream->close) (stream->context);
145
146 js_free (stream->buffer);
147 js_free (stream);
148
149 return result;
150 }
151
152
153 int
154 js_iostream_seek (JSIOStream *stream, long offset, int whence)
155 {
156 int result;
157
158 if (js_iostream_flush (stream) == EOF)
159 return -1;
160
161 result = (*stream->seek) (stream->context, offset, whence);
162 if (result == 0)
163 /* Successful. Clear the eof flag. */
164 stream->at_eof = 0;
165
166 return result;
167 }
168
169
170 long
171 js_iostream_get_position (JSIOStream *stream)
172 {
173 long pos;
174
175 /* Flush the possible buffered output. */
176 if (js_iostream_flush (stream) == EOF)
177 return -1;
178
179 pos = (*stream->get_position) (stream->context);
180 if (pos < 0)
181 return pos;
182
183 /*
184 * The logical position if at <bufpos>, the context's idea is at
185 * <data_in_buf>. Adjust.
186 */
187 return pos - (stream->data_in_buf - stream->bufpos);
188 }
189
190
191 long
192 js_iostream_get_length (JSIOStream *stream)
193 {
194 /* Flush the possible buffered output. */
195 if (js_iostream_flush (stream) == EOF)
196 return -1;
197
198 return (*stream->get_length) (stream->context);
199 }
200
201
202 void
203 js_iostream_fill_buffer (JSIOStream *stream)
204 {
205 if (stream->read == NULL)
206 {
207 stream->at_eof = 1;
208 return;
209 }
210
211 stream->data_in_buf = (*stream->read) (stream->context, stream->buffer,
212 stream->buflen, &stream->error);
213 stream->bufpos = 0;
214 if (stream->data_in_buf == 0)
215 stream->at_eof = 1;
216 }