| 1 |
/* |
| 2 |
* vport.c - 'virtual port' |
| 3 |
* |
| 4 |
* Copyright (c) 2004-2024 Shiro Kawai <[email protected]> |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <gauche.h> |
| 35 |
#include <gauche/extend.h> |
| 36 |
#include <gauche/priv/portP.h> |
| 37 |
|
| 38 |
#include "vport.h" |
| 39 |
|
| 40 |
#undef close /* windows black magic */ |
| 41 |
|
| 42 |
static ScmObj key_bufsize = SCM_FALSE; /* :buffer-size */ |
| 43 |
static ScmObj key_name = SCM_FALSE; /* :name */ |
| 44 |
|
| 45 |
/*================================================================ |
| 46 |
* <virtual-port> |
| 47 |
*/ |
| 48 |
|
| 49 |
static ScmObj vport_allocate(ScmClass *klass, ScmObj initargs); |
| 50 |
static void vport_print(ScmObj obj, ScmPort *port, ScmWriteContext *ctx); |
| 51 |
|
| 52 |
static ScmClass *vport_cpa[] = { |
| 53 |
SCM_CLASS_STATIC_PTR(Scm_PortClass), |
| 54 |
SCM_CLASS_STATIC_PTR(Scm_TopClass), |
| 55 |
NULL |
| 56 |
}; |
| 57 |
|
| 58 |
SCM_DEFINE_BASE_CLASS(Scm_VirtualInputPortClass, ScmPort, |
| 59 |
vport_print, NULL, NULL, |
| 60 |
vport_allocate, vport_cpa); |
| 61 |
|
| 62 |
SCM_DEFINE_BASE_CLASS(Scm_VirtualOutputPortClass, ScmPort, |
| 63 |
vport_print, NULL, NULL, |
| 64 |
vport_allocate, vport_cpa); |
| 65 |
|
| 66 |
SCM_DEFINE_BASE_CLASS(Scm_VirtualIOPortClass, ScmPort, |
| 67 |
vport_print, NULL, NULL, |
| 68 |
vport_allocate, vport_cpa); |
| 69 |
|
| 70 |
/* |
| 71 |
* Scheme handlers. They are visible from Scheme as instance slots. |
| 72 |
* Any of these slots can be #f - if possible, the vport tries to fulfill |
| 73 |
* the feature by using alternative methods. If not possible, it raises |
| 74 |
* 'not supported' error. |
| 75 |
*/ |
| 76 |
typedef struct vport_rec { |
| 77 |
ScmObj getb_proc; /* () -> Maybe Byte */ |
| 78 |
ScmObj getc_proc; /* () -> Maybe Char */ |
| 79 |
ScmObj gets_proc; /* (Size) -> Maybe String */ |
| 80 |
ScmObj ready_proc; /* (Bool) -> Bool */ |
| 81 |
ScmObj putb_proc; /* (Byte) -> () */ |
| 82 |
ScmObj putc_proc; /* (Char) -> () */ |
| 83 |
ScmObj puts_proc; /* (String) -> () */ |
| 84 |
ScmObj flush_proc; /* () -> () */ |
| 85 |
ScmObj close_proc; /* () -> () */ |
| 86 |
ScmObj seek_proc; /* (Offset, Whence) -> Offset */ |
| 87 |
ScmObj getpos_proc; /* () -> Pos */ |
| 88 |
ScmObj setpos_proc; /* (Pos) -> () */ |
| 89 |
} vport; |
| 90 |
|
| 91 |
#define VPORT(port) ((vport*)PORT_VT(port)->data) |
| 92 |
|
| 93 |
/*------------------------------------------------------------ |
| 94 |
* Vport Getb |
| 95 |
*/ |
| 96 |
static int vport_getb(ScmPort *p) |
| 97 |
{ |
| 98 |
vport *data = VPORT(p); |
| 99 |
SCM_ASSERT(data != NULL); |
| 100 |
|
| 101 |
if (SCM_FALSEP(data->getb_proc)) { |
| 102 |
/* If the port doesn't have get-byte method, use get-char |
| 103 |
if possible. */ |
| 104 |
char buf[SCM_CHAR_MAX_BYTES]; |
| 105 |
|
| 106 |
if (SCM_FALSEP(data->getc_proc)) return EOF; |
| 107 |
ScmObj ch = Scm_ApplyRec(data->getc_proc, SCM_NIL); |
| 108 |
if (!SCM_CHARP(ch)) return EOF; |
| 109 |
|
| 110 |
ScmChar c = SCM_CHAR_VALUE(ch); |
| 111 |
int nb = SCM_CHAR_NBYTES(c); |
| 112 |
SCM_CHAR_PUT(buf, c); |
| 113 |
|
| 114 |
for (int i=1; i<nb; i++) { |
| 115 |
/* pushback for later use. this isn't very efficient; |
| 116 |
if efficiency becomes a problem, we need another API |
| 117 |
to pushback multiple bytes. */ |
| 118 |
Scm_UngetbUnsafe(buf[i], p); |
| 119 |
} |
| 120 |
return (unsigned char)buf[0]; |
| 121 |
} else { |
| 122 |
ScmObj b = Scm_ApplyRec(data->getb_proc, SCM_NIL); |
| 123 |
if (!SCM_INTP(b)) return EOF; |
| 124 |
return (SCM_INT_VALUE(b) & 0xff); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/*------------------------------------------------------------ |
| 129 |
* Vport Getc |
| 130 |
*/ |
| 131 |
static int vport_getc(ScmPort *p) |
| 132 |
{ |
| 133 |
vport *data = VPORT(p); |
| 134 |
SCM_ASSERT(data != NULL); |
| 135 |
|
| 136 |
if (SCM_FALSEP(data->getc_proc)) { |
| 137 |
/* If the port doesn't have get-char method, try get-byte */ |
| 138 |
char buf[SCM_CHAR_MAX_BYTES]; |
| 139 |
|
| 140 |
if (SCM_FALSEP(data->getb_proc)) return EOF; |
| 141 |
ScmObj b = Scm_ApplyRec(data->getb_proc, SCM_NIL); |
| 142 |
if (!SCM_INTP(b)) return EOF; |
| 143 |
buf[0] = (char)SCM_INT_VALUE(b); |
| 144 |
int n = SCM_CHAR_NFOLLOWS(P_(p)->scratch[0]); |
| 145 |
for (int i=0; i<n; i++) { |
| 146 |
b = Scm_ApplyRec(data->getb_proc, SCM_NIL); |
| 147 |
if (!SCM_INTP(b)) { |
| 148 |
/* TODO: should raise an exception? */ |
| 149 |
return EOF; |
| 150 |
} |
| 151 |
buf[i+1] = (char)SCM_INT_VALUE(b); |
| 152 |
} |
| 153 |
ScmChar ch; |
| 154 |
SCM_CHAR_GET(buf, ch); |
| 155 |
return ch; |
| 156 |
} else { |
| 157 |
ScmObj ch = Scm_ApplyRec(data->getc_proc, SCM_NIL); |
| 158 |
if (!SCM_CHARP(ch)) return EOF; |
| 159 |
return SCM_CHAR_VALUE(ch); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/*------------------------------------------------------------ |
| 164 |
* Vport Gets |
| 165 |
*/ |
| 166 |
static ScmSize vport_getz(char *buf, ScmSize buflen, ScmPort *p) |
| 167 |
{ |
| 168 |
vport *data = VPORT(p); |
| 169 |
SCM_ASSERT(data != NULL); |
| 170 |
|
| 171 |
if (!SCM_FALSEP(data->gets_proc)) { |
| 172 |
ScmSmallInt size; |
| 173 |
ScmObj s = Scm_ApplyRec(data->gets_proc, |
| 174 |
SCM_LIST1(SCM_MAKE_INT(buflen))); |
| 175 |
if (!SCM_STRINGP(s)) return EOF; |
| 176 |
const char *start = Scm_GetStringContent(SCM_STRING(s), &size, |
| 177 |
NULL, NULL); |
| 178 |
if (size > buflen) { |
| 179 |
/* NB: should raise an exception? */ |
| 180 |
memcpy(buf, start, buflen); |
| 181 |
return buflen; |
| 182 |
} else { |
| 183 |
memcpy(buf, start, size); |
| 184 |
return size; |
| 185 |
} |
| 186 |
} else { |
| 187 |
ScmSize i = 0; |
| 188 |
for (; i<buflen; i++) { |
| 189 |
int byte = vport_getb(p); |
| 190 |
if (byte == EOF) break; |
| 191 |
buf[i] = byte; |
| 192 |
} |
| 193 |
if (i==0) return EOF; |
| 194 |
else return i; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/*------------------------------------------------------------ |
| 199 |
* Vport Ready |
| 200 |
*/ |
| 201 |
static int vport_ready(ScmPort *p, int charp) |
| 202 |
{ |
| 203 |
vport *data = VPORT(p); |
| 204 |
SCM_ASSERT(data != NULL); |
| 205 |
|
| 206 |
if (!SCM_FALSEP(data->ready_proc)) { |
| 207 |
ScmObj s = Scm_ApplyRec(data->ready_proc, |
| 208 |
SCM_LIST1(SCM_MAKE_BOOL(charp))); |
| 209 |
return !SCM_FALSEP(s); |
| 210 |
} else { |
| 211 |
/* if no method is given, always return #t */ |
| 212 |
return TRUE; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/*------------------------------------------------------------ |
| 217 |
* Vport putb |
| 218 |
*/ |
| 219 |
static void vport_putb(ScmByte b, ScmPort *p) |
| 220 |
{ |
| 221 |
vport *data = VPORT(p); |
| 222 |
SCM_ASSERT(data != NULL); |
| 223 |
|
| 224 |
if (SCM_FALSEP(data->putb_proc)) { |
| 225 |
if (!SCM_FALSEP(data->putc_proc) |
| 226 |
&& SCM_CHAR_NFOLLOWS(b) == 0) { |
| 227 |
/* This byte is a single-byte character, so we can use putc. */ |
| 228 |
Scm_ApplyRec(data->putc_proc, SCM_LIST1(SCM_MAKE_CHAR(b))); |
| 229 |
} else { |
| 230 |
/* Given byte is a part of multibyte sequence. We don't |
| 231 |
handle it for the time being. */ |
| 232 |
Scm_PortError(p, SCM_PORT_ERROR_UNIT, |
| 233 |
"cannot perform binary output to the port %S", p); |
| 234 |
} |
| 235 |
} else { |
| 236 |
Scm_ApplyRec(data->putb_proc, SCM_LIST1(SCM_MAKE_INT(b))); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/*------------------------------------------------------------ |
| 241 |
* Vport putc |
| 242 |
*/ |
| 243 |
static void vport_putc(ScmChar c, ScmPort *p) |
| 244 |
{ |
| 245 |
vport *data = VPORT(p); |
| 246 |
SCM_ASSERT(data != NULL); |
| 247 |
|
| 248 |
if (SCM_FALSEP(data->putc_proc)) { |
| 249 |
if (SCM_FALSEP(data->putb_proc)) { |
| 250 |
Scm_PortError(p, SCM_PORT_ERROR_OTHER, |
| 251 |
"cannot perform output to the port %S", p); |
| 252 |
} else { |
| 253 |
unsigned char buf[SCM_CHAR_MAX_BYTES]; |
| 254 |
int n = SCM_CHAR_NBYTES(c); |
| 255 |
SCM_CHAR_PUT(buf, c); |
| 256 |
for (int i=0; i<n; i++) { |
| 257 |
Scm_ApplyRec(data->putb_proc, SCM_LIST1(SCM_MAKE_INT(buf[i]))); |
| 258 |
} |
| 259 |
} |
| 260 |
} else { |
| 261 |
Scm_ApplyRec(data->putc_proc, SCM_LIST1(SCM_MAKE_CHAR(c))); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/*------------------------------------------------------------ |
| 266 |
* Vport putz |
| 267 |
*/ |
| 268 |
static void vport_putz(const char *buf, ScmSize size, ScmPort *p) |
| 269 |
{ |
| 270 |
vport *data = VPORT(p); |
| 271 |
SCM_ASSERT(data != NULL); |
| 272 |
|
| 273 |
if (!SCM_FALSEP(data->puts_proc)) { |
| 274 |
Scm_ApplyRec(data->puts_proc, |
| 275 |
SCM_LIST1(Scm_MakeString(buf, size, -1, |
| 276 |
SCM_STRING_COPYING))); |
| 277 |
} else if (!SCM_FALSEP(data->putb_proc)) { |
| 278 |
for (ScmSize i=0; i<size; i++) { |
| 279 |
unsigned char b = buf[i]; |
| 280 |
Scm_ApplyRec(data->putb_proc, SCM_LIST1(SCM_MAKE_INT(b))); |
| 281 |
} |
| 282 |
} else { |
| 283 |
Scm_PortError(p, SCM_PORT_ERROR_UNIT, |
| 284 |
"cannot perform binary output to the port %S", p); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/*------------------------------------------------------------ |
| 289 |
* Vport puts |
| 290 |
*/ |
| 291 |
static void vport_puts(ScmString *s, ScmPort *p) |
| 292 |
{ |
| 293 |
vport *data = VPORT(p); |
| 294 |
const ScmStringBody *b = SCM_STRING_BODY(s); |
| 295 |
SCM_ASSERT(data != NULL); |
| 296 |
|
| 297 |
if (!SCM_FALSEP(data->puts_proc)) { |
| 298 |
Scm_ApplyRec(data->puts_proc, SCM_LIST1(SCM_OBJ(s))); |
| 299 |
} else if (SCM_STRING_BODY_INCOMPLETE_P(b) |
| 300 |
|| (SCM_FALSEP(data->putc_proc) |
| 301 |
&& !SCM_FALSEP(data->putb_proc))) { |
| 302 |
/* we perform binary output */ |
| 303 |
vport_putz(SCM_STRING_BODY_START(b), SCM_STRING_BODY_SIZE(b), p); |
| 304 |
} else if (!SCM_FALSEP(data->putc_proc)) { |
| 305 |
const char *cp = SCM_STRING_BODY_START(b); |
| 306 |
for (int i=0; i < (int)SCM_STRING_BODY_LENGTH(b); i++) { |
| 307 |
ScmChar c; |
| 308 |
SCM_CHAR_GET(cp, c); |
| 309 |
cp += SCM_CHAR_NFOLLOWS(*cp)+1; |
| 310 |
Scm_ApplyRec(data->putc_proc, SCM_LIST1(SCM_MAKE_CHAR(c))); |
| 311 |
} |
| 312 |
} else { |
| 313 |
Scm_PortError(p, SCM_PORT_ERROR_OTHER, |
| 314 |
"cannot perform output to the port %S", p); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
/*------------------------------------------------------------ |
| 319 |
* Vport flush |
| 320 |
*/ |
| 321 |
static void vport_flush(ScmPort *p) |
| 322 |
{ |
| 323 |
vport *data = VPORT(p); |
| 324 |
SCM_ASSERT(data != NULL); |
| 325 |
if (!SCM_FALSEP(data->flush_proc)) { |
| 326 |
Scm_ApplyRec(data->flush_proc, SCM_NIL); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/*------------------------------------------------------------ |
| 331 |
* Vport close |
| 332 |
*/ |
| 333 |
static void vport_close(ScmPort *p) |
| 334 |
{ |
| 335 |
vport *data = VPORT(p); |
| 336 |
SCM_ASSERT(data != NULL); |
| 337 |
if (!SCM_FALSEP(data->close_proc)) { |
| 338 |
Scm_ApplyRec(data->close_proc, SCM_NIL); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
/*------------------------------------------------------------ |
| 343 |
* Vport seek |
| 344 |
*/ |
| 345 |
static off_t vport_seek(ScmPort *p, off_t off, int whence) |
| 346 |
{ |
| 347 |
vport *data = VPORT(p); |
| 348 |
SCM_ASSERT(data != NULL); |
| 349 |
if (!SCM_FALSEP(data->seek_proc)) { |
| 350 |
ScmObj r = Scm_ApplyRec(data->seek_proc, |
| 351 |
SCM_LIST2(Scm_OffsetToInteger(off), |
| 352 |
Scm_MakeInteger(whence))); |
| 353 |
if (SCM_INTEGERP(r)) { |
| 354 |
return Scm_IntegerToOffset(r); |
| 355 |
} |
| 356 |
} |
| 357 |
return (off_t)-1; |
| 358 |
} |
| 359 |
|
| 360 |
static ScmObj vport_getpos(ScmPort *p) |
| 361 |
{ |
| 362 |
vport *data = VPORT(p); |
| 363 |
SCM_ASSERT(data != NULL); |
| 364 |
if (!SCM_FALSEP(data->getpos_proc)) { |
| 365 |
return Scm_ApplyRec(data->getpos_proc, SCM_NIL); |
| 366 |
} else if (!SCM_FALSEP(data->seek_proc)) { |
| 367 |
return Scm_ApplyRec(data->seek_proc, |
| 368 |
SCM_LIST2(SCM_MAKE_INT(0), |
| 369 |
SCM_MAKE_INT(SEEK_CUR))); |
| 370 |
} |
| 371 |
return SCM_UNDEFINED; |
| 372 |
} |
| 373 |
|
| 374 |
static void vport_setpos(ScmPort *p, ScmObj pos) |
| 375 |
{ |
| 376 |
vport *data = VPORT(p); |
| 377 |
SCM_ASSERT(data != NULL); |
| 378 |
if (!SCM_FALSEP(data->setpos_proc)) { |
| 379 |
Scm_ApplyRec(data->setpos_proc, SCM_LIST1(pos)); |
| 380 |
} else if (!SCM_FALSEP(data->seek_proc)) { |
| 381 |
Scm_ApplyRec(data->seek_proc, |
| 382 |
SCM_LIST2(pos, SCM_MAKE_INT(SEEK_SET))); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
/*------------------------------------------------------------ |
| 388 |
* Allocation & wiring |
| 389 |
*/ |
| 390 |
|
| 391 |
static ScmObj vport_allocate(ScmClass *klass, ScmObj initargs) |
| 392 |
{ |
| 393 |
vport *data = SCM_NEW(vport); |
| 394 |
|
| 395 |
data->getb_proc = SCM_FALSE; |
| 396 |
data->getc_proc = SCM_FALSE; |
| 397 |
data->gets_proc = SCM_FALSE; |
| 398 |
data->ready_proc = SCM_FALSE; |
| 399 |
data->putb_proc = SCM_FALSE; |
| 400 |
data->putc_proc = SCM_FALSE; |
| 401 |
data->puts_proc = SCM_FALSE; |
| 402 |
data->flush_proc = SCM_FALSE; |
| 403 |
data->close_proc = SCM_FALSE; |
| 404 |
data->seek_proc = SCM_FALSE; |
| 405 |
data->getpos_proc = SCM_FALSE; |
| 406 |
data->setpos_proc = SCM_FALSE; |
| 407 |
|
| 408 |
ScmPortVTable vtab; |
| 409 |
vtab.Getb = vport_getb; |
| 410 |
vtab.Getc = vport_getc; |
| 411 |
vtab.Getz = vport_getz; |
| 412 |
vtab.Ready = vport_ready; |
| 413 |
vtab.Putb = vport_putb; |
| 414 |
vtab.Putc = vport_putc; |
| 415 |
vtab.Putz = vport_putz; |
| 416 |
vtab.Puts = vport_puts; |
| 417 |
vtab.Flush = vport_flush; |
| 418 |
vtab.Close = vport_close; |
| 419 |
/* these procedures will be set according to the slot values */ |
| 420 |
vtab.Seek = NULL; |
| 421 |
vtab.GetPos = NULL; |
| 422 |
vtab.SetPos = NULL; |
| 423 |
vtab.flags = 0; |
| 424 |
|
| 425 |
int dir = 0; |
| 426 |
if (Scm_SubtypeP(klass, SCM_CLASS_VIRTUAL_INPUT_PORT)) { |
| 427 |
dir = SCM_PORT_INPUT; |
| 428 |
} else if (Scm_SubtypeP(klass, SCM_CLASS_VIRTUAL_OUTPUT_PORT)) { |
| 429 |
dir = SCM_PORT_OUTPUT; |
| 430 |
} else if (Scm_SubtypeP(klass, SCM_CLASS_VIRTUAL_IO_PORT)) { |
| 431 |
dir = SCM_PORT_INPUT | SCM_PORT_OUTPUT; |
| 432 |
} else { |
| 433 |
Scm_Panic("vport_allocate: implementation error (class wiring screwed?)"); |
| 434 |
} |
| 435 |
ScmObj name = Scm_GetKeyword(key_name, initargs, SCM_FALSE); |
| 436 |
ScmObj port = Scm_MakeVirtualPortFull(klass, name, dir, &vtab, |
| 437 |
SCM_PORT_WITH_POSITION); |
| 438 |
PORT_VT(port)->data = data; |
| 439 |
return port; |
| 440 |
} |
| 441 |
|
| 442 |
static void vport_print(ScmObj obj, ScmPort *port, |
| 443 |
ScmWriteContext *ctx SCM_UNUSED) |
| 444 |
{ |
| 445 |
Scm_Printf(port, "#<%A%s %A %p>", |
| 446 |
Scm_ShortClassName(Scm_ClassOf(obj)), |
| 447 |
SCM_PORT_CLOSED_P(obj)? "(closed)" : "", |
| 448 |
Scm_PortName(SCM_PORT(obj)), |
| 449 |
obj); |
| 450 |
} |
| 451 |
|
| 452 |
/* Accessors */ |
| 453 |
#define VPORT_GET(name) \ |
| 454 |
static ScmObj SCM_CPP_CAT3(vport_,name,_get) (ScmObj p) \ |
| 455 |
{ \ |
| 456 |
vport *data = VPORT(p); \ |
| 457 |
SCM_ASSERT(data != NULL); \ |
| 458 |
return data->SCM_CPP_CAT(name,_proc); \ |
| 459 |
} |
| 460 |
|
| 461 |
#define VPORT_SET(name) \ |
| 462 |
static void SCM_CPP_CAT3(vport_,name,_set) (ScmObj p, ScmObj v) \ |
| 463 |
{ \ |
| 464 |
vport *data = VPORT(p); \ |
| 465 |
SCM_ASSERT(data != NULL); \ |
| 466 |
data->SCM_CPP_CAT(name,_proc) = v; \ |
| 467 |
} |
| 468 |
|
| 469 |
#define VPORT_ACC(name) VPORT_GET(name) VPORT_SET(name) |
| 470 |
|
| 471 |
VPORT_ACC(getb) |
| 472 |
VPORT_ACC(getc) |
| 473 |
VPORT_ACC(gets) |
| 474 |
VPORT_ACC(ready) |
| 475 |
VPORT_ACC(putb) |
| 476 |
VPORT_ACC(putc) |
| 477 |
VPORT_ACC(puts) |
| 478 |
VPORT_ACC(flush) |
| 479 |
VPORT_ACC(close) |
| 480 |
|
| 481 |
VPORT_GET(seek) |
| 482 |
VPORT_GET(getpos) |
| 483 |
VPORT_GET(setpos) |
| 484 |
|
| 485 |
static void vport_seek_set(ScmObj p, ScmObj v) |
| 486 |
{ |
| 487 |
vport *data = VPORT(p); |
| 488 |
SCM_ASSERT(data != NULL); |
| 489 |
data->seek_proc = v; |
| 490 |
ScmPortVTable *vt = Scm_PortVTableStruct(SCM_PORT(p)); |
| 491 |
if (SCM_FALSEP(v)) { |
| 492 |
vt->Seek = NULL; |
| 493 |
} else { |
| 494 |
vt->Seek = vport_seek; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
static void vport_getpos_set(ScmObj p, ScmObj v) |
| 499 |
{ |
| 500 |
vport *data = VPORT(p); |
| 501 |
SCM_ASSERT(data != NULL); |
| 502 |
data->getpos_proc = v; |
| 503 |
ScmPortVTable *vt = Scm_PortVTableStruct(SCM_PORT(p)); |
| 504 |
if (SCM_FALSEP(v)) { |
| 505 |
vt->GetPos = NULL; |
| 506 |
} else { |
| 507 |
vt->GetPos = vport_getpos; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
static void vport_setpos_set(ScmObj p, ScmObj v) |
| 512 |
{ |
| 513 |
vport *data = VPORT(p); |
| 514 |
SCM_ASSERT(data != NULL); |
| 515 |
data->setpos_proc = v; |
| 516 |
ScmPortVTable *vt = Scm_PortVTableStruct(SCM_PORT(p)); |
| 517 |
if (SCM_FALSEP(v)) { |
| 518 |
vt->SetPos = NULL; |
| 519 |
} else { |
| 520 |
vt->SetPos = vport_setpos; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
#define VPORT_SLOT(name) \ |
| 525 |
SCM_CLASS_SLOT_SPEC(#name, \ |
| 526 |
SCM_CPP_CAT3(vport_,name,_get), \ |
| 527 |
SCM_CPP_CAT3(vport_,name,_set)) |
| 528 |
|
| 529 |
static ScmClassStaticSlotSpec viport_slots[] = { |
| 530 |
VPORT_SLOT(getb), |
| 531 |
VPORT_SLOT(getc), |
| 532 |
VPORT_SLOT(gets), |
| 533 |
VPORT_SLOT(ready), |
| 534 |
VPORT_SLOT(close), |
| 535 |
VPORT_SLOT(seek), |
| 536 |
VPORT_SLOT(getpos), |
| 537 |
VPORT_SLOT(setpos), |
| 538 |
SCM_CLASS_SLOT_SPEC_END() |
| 539 |
}; |
| 540 |
|
| 541 |
static ScmClassStaticSlotSpec voport_slots[] = { |
| 542 |
VPORT_SLOT(putb), |
| 543 |
VPORT_SLOT(putc), |
| 544 |
VPORT_SLOT(puts), |
| 545 |
VPORT_SLOT(flush), |
| 546 |
VPORT_SLOT(close), |
| 547 |
VPORT_SLOT(seek), |
| 548 |
VPORT_SLOT(getpos), |
| 549 |
VPORT_SLOT(setpos), |
| 550 |
SCM_CLASS_SLOT_SPEC_END() |
| 551 |
}; |
| 552 |
|
| 553 |
static ScmClassStaticSlotSpec vioport_slots[] = { |
| 554 |
VPORT_SLOT(getb), |
| 555 |
VPORT_SLOT(getc), |
| 556 |
VPORT_SLOT(gets), |
| 557 |
VPORT_SLOT(ready), |
| 558 |
VPORT_SLOT(putb), |
| 559 |
VPORT_SLOT(putc), |
| 560 |
VPORT_SLOT(puts), |
| 561 |
VPORT_SLOT(flush), |
| 562 |
VPORT_SLOT(close), |
| 563 |
VPORT_SLOT(seek), |
| 564 |
VPORT_SLOT(getpos), |
| 565 |
VPORT_SLOT(setpos), |
| 566 |
SCM_CLASS_SLOT_SPEC_END() |
| 567 |
}; |
| 568 |
|
| 569 |
/*================================================================ |
| 570 |
* <buffered-port> |
| 571 |
*/ |
| 572 |
|
| 573 |
static ScmObj bport_allocate(ScmClass *klass, ScmObj initargs); |
| 574 |
|
| 575 |
SCM_DEFINE_BASE_CLASS(Scm_BufferedInputPortClass, ScmPort, |
| 576 |
vport_print, NULL, NULL, |
| 577 |
bport_allocate, vport_cpa); |
| 578 |
|
| 579 |
SCM_DEFINE_BASE_CLASS(Scm_BufferedOutputPortClass, ScmPort, |
| 580 |
vport_print, NULL, NULL, |
| 581 |
bport_allocate, vport_cpa); |
| 582 |
|
| 583 |
|
| 584 |
/* |
| 585 |
* Scheme handlers. They are visible from Scheme as instance slots. |
| 586 |
*/ |
| 587 |
|
| 588 |
typedef struct bport_rec { |
| 589 |
ScmObj fill_proc; /* (U8vector) -> Maybe Int*/ |
| 590 |
ScmObj flush_proc; /* (U8vector, Bool) -> Maybe Int */ |
| 591 |
ScmObj close_proc; /* () -> () */ |
| 592 |
ScmObj ready_proc; /* () -> Bool */ |
| 593 |
ScmObj filenum_proc; /* () -> Maybe Int */ |
| 594 |
ScmObj getpos_proc; /* () -> Maybe Int */ |
| 595 |
ScmObj setpos_proc; /* (Int) -> () */ |
| 596 |
ScmObj seek_proc; /* (Offset, Whence) -> Offset */ |
| 597 |
} bport; |
| 598 |
|
| 599 |
#define BPORT(port) ((bport*)PORT_BUF(port)->data) |
| 600 |
|
| 601 |
/*------------------------------------------------------------ |
| 602 |
* Bport fill |
| 603 |
*/ |
| 604 |
static ScmSize bport_fill(ScmPort *p, ScmSize cnt) |
| 605 |
{ |
| 606 |
bport *data = BPORT(p); |
| 607 |
SCM_ASSERT(data != NULL); |
| 608 |
if (SCM_FALSEP(data->fill_proc)) { |
| 609 |
return 0; /* indicates EOF */ |
| 610 |
} |
| 611 |
ScmObj vec = |
| 612 |
Scm_MakeU8VectorFromArrayShared(cnt, (u_char*)PORT_BUF(p)->buffer); |
| 613 |
ScmObj r = Scm_ApplyRec(data->fill_proc, SCM_LIST1(vec)); |
| 614 |
if (SCM_INTP(r)) return SCM_INT_VALUE(r); |
| 615 |
else if (SCM_EOFP(r)) return 0; |
| 616 |
else return -1; |
| 617 |
} |
| 618 |
|
| 619 |
/*------------------------------------------------------------ |
| 620 |
* Bport flush |
| 621 |
*/ |
| 622 |
static ScmSize bport_flush(ScmPort *p, ScmSize cnt, int forcep) |
| 623 |
{ |
| 624 |
bport *data = BPORT(p); |
| 625 |
SCM_ASSERT(data != NULL); |
| 626 |
if (SCM_FALSEP(data->flush_proc)) { |
| 627 |
return cnt; /* blackhole */ |
| 628 |
} |
| 629 |
ScmObj vec = |
| 630 |
Scm_MakeU8VectorFromArrayShared(cnt, (u_char*)PORT_BUF(p)->buffer); |
| 631 |
ScmObj r = Scm_ApplyRec(data->flush_proc, |
| 632 |
SCM_LIST2(vec, SCM_MAKE_BOOL(forcep))); |
| 633 |
if (SCM_INTP(r)) return SCM_INT_VALUE(r); |
| 634 |
else if (SCM_EOFP(r)) return 0; |
| 635 |
else return -1; |
| 636 |
} |
| 637 |
|
| 638 |
/*------------------------------------------------------------ |
| 639 |
* Bport close |
| 640 |
*/ |
| 641 |
static void bport_close(ScmPort *p) |
| 642 |
{ |
| 643 |
bport *data = BPORT(p); |
| 644 |
SCM_ASSERT(data != NULL); |
| 645 |
if (!SCM_FALSEP(data->close_proc)) { |
| 646 |
Scm_ApplyRec(data->close_proc, SCM_NIL); |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
/*------------------------------------------------------------ |
| 651 |
* Bport Ready |
| 652 |
*/ |
| 653 |
static int bport_ready(ScmPort *p) |
| 654 |
{ |
| 655 |
bport *data = BPORT(p); |
| 656 |
SCM_ASSERT(data != NULL); |
| 657 |
|
| 658 |
if (!SCM_FALSEP(data->ready_proc)) { |
| 659 |
ScmObj s = Scm_ApplyRec(data->ready_proc, SCM_NIL); |
| 660 |
return SCM_FALSEP(s)? SCM_FD_WOULDBLOCK:SCM_FD_READY; |
| 661 |
} else { |
| 662 |
/* if no method is given, always return #t */ |
| 663 |
return SCM_FD_READY; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
/*------------------------------------------------------------ |
| 668 |
* Bport filenum |
| 669 |
*/ |
| 670 |
static int bport_filenum(ScmPort *p) |
| 671 |
{ |
| 672 |
bport *data = BPORT(p); |
| 673 |
SCM_ASSERT(data != NULL); |
| 674 |
|
| 675 |
if (SCM_FALSEP(data->filenum_proc)) { |
| 676 |
return -1; |
| 677 |
} else { |
| 678 |
ScmObj s = Scm_ApplyRec(data->filenum_proc, SCM_NIL); |
| 679 |
if (SCM_INTP(s)) return SCM_INT_VALUE(s); |
| 680 |
else return -1; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
/*------------------------------------------------------------ |
| 685 |
* Bport positioning |
| 686 |
*/ |
| 687 |
static ScmObj bport_getpos(ScmPort *p) |
| 688 |
{ |
| 689 |
bport *data = BPORT(p); |
| 690 |
SCM_ASSERT(data != NULL); |
| 691 |
if (!SCM_FALSEP(data->getpos_proc)) { |
| 692 |
return Scm_ApplyRec(data->getpos_proc, SCM_NIL); |
| 693 |
} else { |
| 694 |
return SCM_FALSE; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
static void bport_setpos(ScmPort *p, ScmObj pos) |
| 699 |
{ |
| 700 |
bport *data = BPORT(p); |
| 701 |
SCM_ASSERT(data != NULL); |
| 702 |
if (!SCM_FALSEP(data->setpos_proc)) { |
| 703 |
Scm_ApplyRec(data->setpos_proc, SCM_LIST1(pos)); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
static off_t bport_seek(ScmPort *p, off_t off, int whence) |
| 708 |
{ |
| 709 |
bport *data = BPORT(p); |
| 710 |
SCM_ASSERT(data != NULL); |
| 711 |
if (!SCM_FALSEP(data->seek_proc)) { |
| 712 |
ScmObj r = Scm_ApplyRec(data->seek_proc, |
| 713 |
SCM_LIST2(Scm_OffsetToInteger(off), |
| 714 |
Scm_MakeInteger(whence))); |
| 715 |
if (SCM_INTEGERP(r)) { |
| 716 |
return Scm_IntegerToOffset(r); |
| 717 |
} |
| 718 |
} |
| 719 |
return (off_t)-1; |
| 720 |
} |
| 721 |
|
| 722 |
/*------------------------------------------------------------ |
| 723 |
* Allocation & wiring |
| 724 |
*/ |
| 725 |
|
| 726 |
static ScmObj bport_allocate(ScmClass *klass, ScmObj initargs) |
| 727 |
{ |
| 728 |
bport *data = SCM_NEW(bport); |
| 729 |
int bufsize = Scm_GetInteger(Scm_GetKeyword(key_bufsize, initargs, |
| 730 |
SCM_MAKE_INT(0))); |
| 731 |
|
| 732 |
data->fill_proc = SCM_FALSE; |
| 733 |
data->flush_proc = SCM_FALSE; |
| 734 |
data->close_proc = SCM_FALSE; |
| 735 |
data->ready_proc = SCM_FALSE; |
| 736 |
data->filenum_proc = SCM_FALSE; |
| 737 |
data->getpos_proc = SCM_FALSE; |
| 738 |
data->setpos_proc = SCM_FALSE; |
| 739 |
data->seek_proc = SCM_FALSE; |
| 740 |
|
| 741 |
ScmPortBuffer buf; |
| 742 |
if (bufsize > 0) { |
| 743 |
buf.buffer = SCM_NEW_ATOMIC2(char*, bufsize); |
| 744 |
buf.size = bufsize; |
| 745 |
} else { |
| 746 |
buf.buffer = NULL; |
| 747 |
buf.size = 0; |
| 748 |
} |
| 749 |
|
| 750 |
buf.current = NULL; |
| 751 |
buf.end = NULL; |
| 752 |
buf.mode = SCM_PORT_BUFFER_FULL; |
| 753 |
buf.filler = bport_fill; |
| 754 |
buf.flusher = bport_flush; |
| 755 |
buf.closer = bport_close; |
| 756 |
buf.ready = bport_ready; |
| 757 |
buf.filenum = bport_filenum; |
| 758 |
buf.data = data; |
| 759 |
/* these procedures will be set according to the slot values */ |
| 760 |
buf.seeker = NULL; |
| 761 |
buf.getpos = NULL; |
| 762 |
buf.setpos = NULL; |
| 763 |
buf.flags = 0; |
| 764 |
|
| 765 |
int dir = 0; |
| 766 |
if (Scm_SubtypeP(klass, SCM_CLASS_BUFFERED_INPUT_PORT)) { |
| 767 |
dir = SCM_PORT_INPUT; |
| 768 |
} else if (Scm_SubtypeP(klass, SCM_CLASS_BUFFERED_OUTPUT_PORT)) { |
| 769 |
dir = SCM_PORT_OUTPUT; |
| 770 |
} else { |
| 771 |
Scm_Panic("bport_allocate: implementation error (class wiring screwed?)"); |
| 772 |
} |
| 773 |
ScmObj name = Scm_GetKeyword(key_name, initargs, SCM_FALSE); |
| 774 |
ScmObj port = Scm_MakeBufferedPortFull(klass, name, dir, &buf, |
| 775 |
(SCM_PORT_OWNER|SCM_PORT_WITH_POSITION)); |
| 776 |
return port; |
| 777 |
} |
| 778 |
|
| 779 |
/* Accessors */ |
| 780 |
#define BPORT_GET(name) \ |
| 781 |
static ScmObj SCM_CPP_CAT3(bport_,name,_get) (ScmObj p) \ |
| 782 |
{ \ |
| 783 |
bport *data = BPORT(p); \ |
| 784 |
SCM_ASSERT(data != NULL); \ |
| 785 |
return data->SCM_CPP_CAT(name,_proc); \ |
| 786 |
} |
| 787 |
|
| 788 |
#define BPORT_SET(name) \ |
| 789 |
static void SCM_CPP_CAT3(bport_,name,_set) (ScmObj p, ScmObj v) \ |
| 790 |
{ \ |
| 791 |
bport *data = BPORT(p); \ |
| 792 |
SCM_ASSERT(data != NULL); \ |
| 793 |
data->SCM_CPP_CAT(name,_proc) = v; \ |
| 794 |
} |
| 795 |
|
| 796 |
#define BPORT_ACC(name) BPORT_GET(name) BPORT_SET(name) |
| 797 |
|
| 798 |
BPORT_ACC(fill) |
| 799 |
BPORT_ACC(ready) |
| 800 |
BPORT_ACC(flush) |
| 801 |
BPORT_ACC(close) |
| 802 |
BPORT_ACC(filenum) |
| 803 |
|
| 804 |
BPORT_GET(seek) |
| 805 |
BPORT_GET(getpos) |
| 806 |
BPORT_GET(setpos) |
| 807 |
|
| 808 |
static void bport_seek_set(ScmObj p, ScmObj v) |
| 809 |
{ |
| 810 |
bport *data = BPORT(p); |
| 811 |
SCM_ASSERT(data != NULL); |
| 812 |
data->seek_proc = v; |
| 813 |
ScmPortBuffer *b = Scm_PortBufferStruct(SCM_PORT(p)); |
| 814 |
if (SCM_FALSEP(v)) { |
| 815 |
b->seeker = NULL; |
| 816 |
} else { |
| 817 |
b->seeker = bport_seek; |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
static void bport_getpos_set(ScmObj p, ScmObj v) |
| 822 |
{ |
| 823 |
bport *data = BPORT(p); |
| 824 |
SCM_ASSERT(data != NULL); |
| 825 |
data->getpos_proc = v; |
| 826 |
ScmPortBuffer *b = Scm_PortBufferStruct(SCM_PORT(p)); |
| 827 |
if (SCM_FALSEP(v)) { |
| 828 |
b->getpos = NULL; |
| 829 |
} else { |
| 830 |
b->getpos = bport_getpos; |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
static void bport_setpos_set(ScmObj p, ScmObj v) |
| 835 |
{ |
| 836 |
bport *data = BPORT(p); |
| 837 |
SCM_ASSERT(data != NULL); |
| 838 |
data->setpos_proc = v; |
| 839 |
ScmPortBuffer *b = Scm_PortBufferStruct(SCM_PORT(p)); |
| 840 |
if (SCM_FALSEP(v)) { |
| 841 |
b->setpos = NULL; |
| 842 |
} else { |
| 843 |
b->setpos = bport_setpos; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
#define BPORT_SLOT(name) \ |
| 848 |
SCM_CLASS_SLOT_SPEC(#name, \ |
| 849 |
SCM_CPP_CAT3(bport_,name,_get), \ |
| 850 |
SCM_CPP_CAT3(bport_,name,_set)) |
| 851 |
|
| 852 |
static ScmClassStaticSlotSpec biport_slots[] = { |
| 853 |
BPORT_SLOT(fill), |
| 854 |
BPORT_SLOT(ready), |
| 855 |
BPORT_SLOT(close), |
| 856 |
BPORT_SLOT(filenum), |
| 857 |
BPORT_SLOT(seek), |
| 858 |
BPORT_SLOT(getpos), |
| 859 |
BPORT_SLOT(setpos), |
| 860 |
SCM_CLASS_SLOT_SPEC_END() |
| 861 |
}; |
| 862 |
|
| 863 |
static ScmClassStaticSlotSpec boport_slots[] = { |
| 864 |
BPORT_SLOT(flush), |
| 865 |
BPORT_SLOT(close), |
| 866 |
BPORT_SLOT(filenum), |
| 867 |
BPORT_SLOT(seek), |
| 868 |
BPORT_SLOT(getpos), |
| 869 |
BPORT_SLOT(setpos), |
| 870 |
SCM_CLASS_SLOT_SPEC_END() |
| 871 |
}; |
| 872 |
|
| 873 |
/*================================================================ |
| 874 |
* Initialization |
| 875 |
*/ |
| 876 |
|
| 877 |
SCM_EXTENSION_ENTRY void Scm_Init_gauche__vport(void) |
| 878 |
{ |
| 879 |
SCM_INIT_EXTENSION(gauche__vport); |
| 880 |
ScmModule *mod = SCM_FIND_MODULE("gauche.vport", SCM_FIND_MODULE_CREATE); |
| 881 |
|
| 882 |
Scm_InitStaticClass(&Scm_VirtualInputPortClass, |
| 883 |
"<virtual-input-port>", mod, viport_slots, 0); |
| 884 |
Scm_InitStaticClass(&Scm_VirtualOutputPortClass, |
| 885 |
"<virtual-output-port>", mod, voport_slots, 0); |
| 886 |
Scm_InitStaticClass(&Scm_VirtualIOPortClass, |
| 887 |
"<virtual-io-port>", mod, vioport_slots, 0); |
| 888 |
Scm_InitStaticClass(&Scm_BufferedInputPortClass, |
| 889 |
"<buffered-input-port>", mod, biport_slots, 0); |
| 890 |
Scm_InitStaticClass(&Scm_BufferedOutputPortClass, |
| 891 |
"<buffered-output-port>", mod, boport_slots, 0); |
| 892 |
|
| 893 |
key_bufsize = SCM_MAKE_KEYWORD("buffer-size"); |
| 894 |
key_name = SCM_MAKE_KEYWORD("name"); |
| 895 |
} |