3 * Copyright (c) 1998 New Generation Software (NGS) Oy
5 * Author: Markku Rossi <mtr@ngs.fi>
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.
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.
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,
26 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/ksrc/iostream.c,v $
33 * Types and definitions.
36 #define DEFAULT_BUFFER_SIZE 4096
47 stream
= js_calloc (NULL
, 1, sizeof (*stream
));
51 stream
->buflen
= DEFAULT_BUFFER_SIZE
;
52 stream
->buffer
= js_malloc (NULL
, stream
->buflen
);
53 if (stream
->buffer
== NULL
)
63 js_iostream_read (JSIOStream
*stream
, void *ptr
, size_t size
)
70 /* We have buffered output data. */
71 if (js_iostream_flush (stream
) == EOF
)
74 assert (stream
->writep
== 0);
79 /* First, take everything from the buffer. */
80 if (stream
->bufpos
< stream
->data_in_buf
)
82 got
= stream
->data_in_buf
- stream
->bufpos
;
87 memcpy (ptr
, stream
->buffer
+ stream
->bufpos
, got
);
89 stream
->bufpos
+= got
;
91 ptr
= (void *)((unsigned char *)ptr
+ got
);
97 /* EOF seen, can't read more. */
100 js_iostream_fill_buffer (stream
);
109 js_iostream_write (JSIOStream
*stream
, void *ptr
, size_t size
)
113 for( i
= 0; size
> i
; i
++ )
114 DbgPrint("%c", ((char *)ptr
)[i
]);
121 js_iostream_flush (JSIOStream
*stream
)
127 js_iostream_unget (JSIOStream
*stream
, int byte
)
133 js_iostream_close (JSIOStream
*stream
)
140 if (js_iostream_flush (stream
) == EOF
)
144 (*stream
->close
) (stream
->context
);
146 js_free (stream
->buffer
);
154 js_iostream_seek (JSIOStream
*stream
, long offset
, int whence
)
158 if (js_iostream_flush (stream
) == EOF
)
161 result
= (*stream
->seek
) (stream
->context
, offset
, whence
);
163 /* Successful. Clear the eof flag. */
171 js_iostream_get_position (JSIOStream
*stream
)
175 /* Flush the possible buffered output. */
176 if (js_iostream_flush (stream
) == EOF
)
179 pos
= (*stream
->get_position
) (stream
->context
);
184 * The logical position if at <bufpos>, the context's idea is at
185 * <data_in_buf>. Adjust.
187 return pos
- (stream
->data_in_buf
- stream
->bufpos
);
192 js_iostream_get_length (JSIOStream
*stream
)
194 /* Flush the possible buffered output. */
195 if (js_iostream_flush (stream
) == EOF
)
198 return (*stream
->get_length
) (stream
->context
);
203 js_iostream_fill_buffer (JSIOStream
*stream
)
205 if (stream
->read
== NULL
)
211 stream
->data_in_buf
= (*stream
->read
) (stream
->context
, stream
->buffer
,
212 stream
->buflen
, &stream
->error
);
214 if (stream
->data_in_buf
== 0)