| 1 | /****************************************************************************** |
| 2 | * * |
| 3 | * Copyright (C) 2006-2009 by Tor Andersson, Jesse McGrew. * |
| 4 | * Copyright (C) 2010 by Ben Cressey, Andrew Plotkin, Jörg Walter. * |
| 5 | * * |
| 6 | * This file is part of Gargoyle. * |
| 7 | * * |
| 8 | * Gargoyle is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * Gargoyle is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with Gargoyle; if not, write to the Free Software * |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
| 21 | * * |
| 22 | *****************************************************************************/ |
| 23 | |
| 24 | /* cgstream.c: Stream functions for Glk API, version 0.7.5. |
| 25 | Designed by Andrew Plotkin <[email protected]> |
| 26 | http://www.eblong.com/zarf/glk/index.html |
| 27 | |
| 28 | Portions of this file are copyright 1998-2016 by Andrew Plotkin. |
| 29 | It is distributed under the MIT license; see the "LICENSE" file. |
| 30 | */ |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include "glk.h" |
| 36 | #include "garglk.h" |
| 37 | #include "gi_blorb.h" |
| 38 | |
| 39 | #define TRUE true |
| 40 | #define FALSE false |
| 41 | |
| 42 | /* This implements pretty much what any Glk implementation needs for |
| 43 | stream stuff. Memory streams, file streams (using stdio functions), |
| 44 | and window streams (which just print to stdout.) A fancier |
| 45 | implementation would need to change the window stream stuff, but |
| 46 | memory and file streams would stay the same. (Unless you're on a |
| 47 | wacky platform like the Mac and want to change stdio to native file |
| 48 | functions.) |
| 49 | */ |
| 50 | |
| 51 | static stream_t *gli_streamlist = NULL; /* linked list of all streams */ |
| 52 | static stream_t *gli_currentstr = NULL; /* the current output stream */ |
| 53 | |
| 54 | #ifdef GARGLK |
| 55 | static void gli_set_style(stream_t *str, glui32 val); |
| 56 | #endif |
| 57 | |
| 58 | stream_t *gli_new_stream(int type, int readable, int writable, |
| 59 | glui32 rock) |
| 60 | { |
| 61 | stream_t *str = (stream_t *)malloc(sizeof(stream_t)); |
| 62 | if (!str) |
| 63 | return NULL; |
| 64 | |
| 65 | str->magicnum = MAGIC_STREAM_NUM; |
| 66 | str->type = type; |
| 67 | str->rock = rock; |
| 68 | |
| 69 | str->unicode = FALSE; |
| 70 | str->isbinary = FALSE; |
| 71 | |
| 72 | str->win = NULL; |
| 73 | str->file = NULL; |
| 74 | str->lastop = 0; |
| 75 | str->buf = NULL; |
| 76 | str->bufptr = NULL; |
| 77 | str->bufend = NULL; |
| 78 | str->bufeof = NULL; |
| 79 | str->ubuf = NULL; |
| 80 | str->ubufptr = NULL; |
| 81 | str->ubufend = NULL; |
| 82 | str->ubufeof = NULL; |
| 83 | str->buflen = 0; |
| 84 | |
| 85 | str->readcount = 0; |
| 86 | str->writecount = 0; |
| 87 | str->readable = readable; |
| 88 | str->writable = writable; |
| 89 | |
| 90 | str->prev = NULL; |
| 91 | str->next = gli_streamlist; |
| 92 | gli_streamlist = str; |
| 93 | if (str->next) { |
| 94 | str->next->prev = str; |
| 95 | } |
| 96 | |
| 97 | if (gli_register_obj) |
| 98 | str->disprock = (*gli_register_obj)(str, gidisp_Class_Stream); |
| 99 | else |
| 100 | str->disprock.ptr = NULL; |
| 101 | |
| 102 | return str; |
| 103 | } |
| 104 | |
| 105 | void gli_delete_stream(stream_t *str) |
| 106 | { |
| 107 | window_t *win; |
| 108 | stream_t *prev, *next; |
| 109 | |
| 110 | if (str == gli_currentstr) { |
| 111 | gli_currentstr = NULL; |
| 112 | } |
| 113 | |
| 114 | #ifdef GARGLK |
| 115 | for (win = gli_window_iterate_treeorder(nullptr); win != nullptr; win = gli_window_iterate_treeorder(win)) { |
| 116 | if (win->echostr == str) { |
| 117 | win->echostr = nullptr; |
| 118 | } |
| 119 | } |
| 120 | #else |
| 121 | win = gli_window_get(); |
| 122 | if (win && win->echostr == str) { |
| 123 | win->echostr = NULL; |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | str->magicnum = 0; |
| 128 | |
| 129 | switch (str->type) { |
| 130 | case strtype_Window: |
| 131 | /* nothing necessary; the window is already being closed */ |
| 132 | break; |
| 133 | case strtype_Memory: |
| 134 | if (gli_unregister_arr) { |
| 135 | /* This could be a char array or a glui32 array. */ |
| 136 | const char *typedesc = (str->unicode ? "&+#!Iu" : "&+#!Cn"); |
| 137 | void *buf = (str->unicode ? (void*)str->ubuf : (void*)str->buf); |
| 138 | (*gli_unregister_arr)(buf, str->buflen, const_cast<char *>(typedesc), |
| 139 | str->arrayrock); |
| 140 | } |
| 141 | break; |
| 142 | case strtype_Resource: |
| 143 | /* nothing necessary; the array belongs to gi_blorb.c. */ |
| 144 | break; |
| 145 | case strtype_File: |
| 146 | /* close the FILE */ |
| 147 | fclose(str->file); |
| 148 | str->file = NULL; |
| 149 | str->lastop = 0; |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | if (gli_unregister_obj) { |
| 154 | (*gli_unregister_obj)(str, gidisp_Class_Stream, str->disprock); |
| 155 | str->disprock.ptr = NULL; |
| 156 | } |
| 157 | |
| 158 | prev = str->prev; |
| 159 | next = str->next; |
| 160 | str->prev = NULL; |
| 161 | str->next = NULL; |
| 162 | |
| 163 | if (prev) |
| 164 | prev->next = next; |
| 165 | else |
| 166 | gli_streamlist = next; |
| 167 | if (next) |
| 168 | next->prev = prev; |
| 169 | |
| 170 | free(str); |
| 171 | } |
| 172 | |
| 173 | void gli_stream_fill_result(stream_t *str, stream_result_t *result) |
| 174 | { |
| 175 | if (!result) |
| 176 | return; |
| 177 | |
| 178 | result->readcount = str->readcount; |
| 179 | result->writecount = str->writecount; |
| 180 | } |
| 181 | |
| 182 | void glk_stream_close(stream_t *str, stream_result_t *result) |
| 183 | { |
| 184 | if (!str) { |
| 185 | gli_strict_warning("stream_close: invalid ref."); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | if (str->type == strtype_Window) { |
| 190 | gli_strict_warning("stream_close: cannot close window stream"); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | gli_stream_fill_result(str, result); |
| 195 | gli_delete_stream(str); |
| 196 | } |
| 197 | |
| 198 | strid_t glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, |
| 199 | glui32 rock) |
| 200 | { |
| 201 | stream_t *str; |
| 202 | |
| 203 | if (fmode != filemode_Read |
| 204 | && fmode != filemode_Write |
| 205 | && fmode != filemode_ReadWrite) { |
| 206 | gli_strict_warning("stream_open_memory: illegal filemode"); |
| 207 | return NULL; |
| 208 | } |
| 209 | |
| 210 | str = gli_new_stream(strtype_Memory, |
| 211 | (fmode != filemode_Write), |
| 212 | (fmode != filemode_Read), |
| 213 | rock); |
| 214 | if (!str) { |
| 215 | gli_strict_warning("stream_open_memory: unable to create stream."); |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | if (buf && buflen) { |
| 220 | str->buf = (unsigned char *)buf; |
| 221 | str->bufptr = (unsigned char *)buf; |
| 222 | str->buflen = buflen; |
| 223 | str->bufend = str->buf + str->buflen; |
| 224 | if (fmode == filemode_Write) |
| 225 | str->bufeof = str->buf; |
| 226 | else |
| 227 | str->bufeof = str->bufend; |
| 228 | if (gli_register_arr) { |
| 229 | str->arrayrock = (*gli_register_arr)(buf, buflen, const_cast<char *>("&+#!Cn")); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return str; |
| 234 | } |
| 235 | |
| 236 | strid_t glk_stream_open_file(fileref_t *fref, glui32 fmode, |
| 237 | glui32 rock) |
| 238 | { |
| 239 | stream_t *str; |
| 240 | char modestr[16]; |
| 241 | FILE *fl; |
| 242 | |
| 243 | if (!fref) { |
| 244 | gli_strict_warning("stream_open_file: invalid fileref id."); |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | /* The spec says that Write, ReadWrite, and WriteAppend create the |
| 249 | file if necessary. However, fopen(filename, "r+") doesn't create |
| 250 | a file. So we have to pre-create it in the ReadWrite and |
| 251 | WriteAppend cases. (We use "a" so as not to truncate, and "b" |
| 252 | because we're going to close it immediately, so it doesn't matter.) */ |
| 253 | |
| 254 | /* Another Unix quirk: in r+ mode, you're not supposed to flip from |
| 255 | reading to writing or vice versa without doing an fseek. We will |
| 256 | track the most recent operation (as lastop) -- Write, Read, or |
| 257 | 0 if either is legal next. */ |
| 258 | |
| 259 | if (fmode == filemode_ReadWrite || fmode == filemode_WriteAppend) { |
| 260 | fl = fopen(fref->filename, "ab"); |
| 261 | if (!fl) { |
| 262 | gli_strict_warning("stream_open_file: unable to open file."); |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | fclose(fl); |
| 267 | } |
| 268 | |
| 269 | switch (fmode) { |
| 270 | case filemode_Write: |
| 271 | strcpy(modestr, "w"); |
| 272 | break; |
| 273 | case filemode_Read: |
| 274 | strcpy(modestr, "r"); |
| 275 | break; |
| 276 | case filemode_ReadWrite: |
| 277 | strcpy(modestr, "r+"); |
| 278 | break; |
| 279 | case filemode_WriteAppend: |
| 280 | /* Can't use "a" here, because then fseek wouldn't work. |
| 281 | Instead we use "r+" and then fseek to the end. */ |
| 282 | strcpy(modestr, "r+"); |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | if (!fref->textmode) |
| 287 | strcat(modestr, "b"); |
| 288 | |
| 289 | fl = fopen(fref->filename, modestr); |
| 290 | if (!fl) { |
| 291 | /* According to recent spec discussion, we only display an error on missing files for write/append mode. In read mode, we return silently. */ |
| 292 | if (fmode != filemode_Read) { |
| 293 | gli_strict_warning("stream_open_file: unable to open file."); |
| 294 | } |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | if (fmode == filemode_WriteAppend) { |
| 299 | fseek(fl, 0, SEEK_END); /* ...to the end. */ |
| 300 | } |
| 301 | |
| 302 | str = gli_new_stream(strtype_File, |
| 303 | (fmode == filemode_Read || fmode == filemode_ReadWrite), |
| 304 | !(fmode == filemode_Read), |
| 305 | rock); |
| 306 | if (!str) { |
| 307 | gli_strict_warning("stream_open_file: unable to create stream."); |
| 308 | fclose(fl); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | str->isbinary = !fref->textmode; |
| 313 | str->file = fl; |
| 314 | str->lastop = 0; |
| 315 | |
| 316 | return str; |
| 317 | } |
| 318 | |
| 319 | #ifdef GLK_MODULE_UNICODE |
| 320 | |
| 321 | strid_t glk_stream_open_memory_uni(glui32 *ubuf, glui32 buflen, glui32 fmode, |
| 322 | glui32 rock) |
| 323 | { |
| 324 | stream_t *str; |
| 325 | |
| 326 | if (fmode != filemode_Read |
| 327 | && fmode != filemode_Write |
| 328 | && fmode != filemode_ReadWrite) { |
| 329 | gli_strict_warning("stream_open_memory_uni: illegal filemode"); |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | str = gli_new_stream(strtype_Memory, |
| 334 | (fmode != filemode_Write), |
| 335 | (fmode != filemode_Read), |
| 336 | rock); |
| 337 | if (!str) { |
| 338 | gli_strict_warning("stream_open_memory_uni: unable to create stream."); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | str->unicode = TRUE; |
| 343 | |
| 344 | if (ubuf && buflen) { |
| 345 | str->ubuf = ubuf; |
| 346 | str->ubufptr = ubuf; |
| 347 | str->buflen = buflen; |
| 348 | str->ubufend = str->ubuf + str->buflen; |
| 349 | if (fmode == filemode_Write) |
| 350 | str->ubufeof = ubuf; |
| 351 | else |
| 352 | str->ubufeof = str->ubufend; |
| 353 | if (gli_register_arr) { |
| 354 | str->arrayrock = (*gli_register_arr)(ubuf, buflen, const_cast<char *>("&+#!Iu")); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return str; |
| 359 | } |
| 360 | |
| 361 | strid_t glk_stream_open_file_uni(fileref_t *fref, glui32 fmode, |
| 362 | glui32 rock) |
| 363 | { |
| 364 | strid_t str = glk_stream_open_file(fref, fmode, rock); |
| 365 | if (!str) |
| 366 | return NULL; |
| 367 | /* Unlovely, but it works in this library */ |
| 368 | str->unicode = TRUE; |
| 369 | return str; |
| 370 | } |
| 371 | |
| 372 | #endif /* GLK_MODULE_UNICODE */ |
| 373 | |
| 374 | #ifdef GLK_MODULE_RESOURCE_STREAM |
| 375 | |
| 376 | #define gli_get_dataresource_info(num, ptr, len, isbinary) (false) |
| 377 | |
| 378 | strid_t glk_stream_open_resource(glui32 filenum, glui32 rock) |
| 379 | { |
| 380 | strid_t str; |
| 381 | int isbinary; |
| 382 | giblorb_err_t err; |
| 383 | giblorb_result_t res; |
| 384 | |
| 385 | if (gli_get_dataresource_info(filenum, &res.data.ptr, &res.length, &isbinary)) { |
| 386 | /* Success! */ |
| 387 | } |
| 388 | else { |
| 389 | /* No command-line pathname; check blorb. */ |
| 390 | |
| 391 | giblorb_map_t *map = giblorb_get_resource_map(); |
| 392 | if (!map) |
| 393 | return 0; /* Not running from a blorb file */ |
| 394 | |
| 395 | err = giblorb_load_resource(map, giblorb_method_Memory, &res, giblorb_ID_Data, filenum); |
| 396 | if (err) |
| 397 | return 0; /* Not found, or some other error */ |
| 398 | |
| 399 | /* Note that binary chunks are normally type BINA, but FORM |
| 400 | chunks also count as binary. (This allows us to embed AIFF |
| 401 | files as readable resources, for example.) */ |
| 402 | |
| 403 | if (res.chunktype == giblorb_ID_TEXT) |
| 404 | isbinary = FALSE; |
| 405 | else if (res.chunktype == giblorb_ID_BINA |
| 406 | || res.chunktype == giblorb_make_id('F', 'O', 'R', 'M')) |
| 407 | isbinary = TRUE; |
| 408 | else |
| 409 | return 0; /* Unknown chunk type */ |
| 410 | } |
| 411 | |
| 412 | /* We'll use the in-memory copy of the chunk data as the basis for |
| 413 | our new stream. It's important to not call chunk_unload() until |
| 414 | the stream is closed (and we won't). |
| 415 | |
| 416 | This will be memory-hoggish for giant data chunks, but I don't |
| 417 | expect giant data chunks at this point. A more efficient model |
| 418 | would be to use the file on disk, but this requires some hacking |
| 419 | into the file stream code (we'd need to open a new FILE*) and |
| 420 | I don't feel like doing that. |
| 421 | */ |
| 422 | |
| 423 | str = gli_new_stream(strtype_Resource, |
| 424 | TRUE, FALSE, rock); |
| 425 | if (!str) { |
| 426 | gli_strict_warning("stream_open_resource: unable to create stream."); |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | str->isbinary = isbinary; |
| 431 | |
| 432 | if (res.data.ptr && res.length) { |
| 433 | str->buf = (unsigned char *)res.data.ptr; |
| 434 | str->bufptr = (unsigned char *)res.data.ptr; |
| 435 | str->buflen = res.length; |
| 436 | str->bufend = str->buf + str->buflen; |
| 437 | str->bufeof = str->bufend; |
| 438 | } |
| 439 | |
| 440 | return str; |
| 441 | } |
| 442 | |
| 443 | strid_t glk_stream_open_resource_uni(glui32 filenum, glui32 rock) |
| 444 | { |
| 445 | strid_t str; |
| 446 | int isbinary; |
| 447 | giblorb_err_t err; |
| 448 | giblorb_result_t res; |
| 449 | |
| 450 | if (gli_get_dataresource_info(filenum, &res.data.ptr, &res.length, &isbinary)) { |
| 451 | /* Success! */ |
| 452 | } |
| 453 | else { |
| 454 | /* No explicit path; check blorb. */ |
| 455 | |
| 456 | giblorb_map_t *map = giblorb_get_resource_map(); |
| 457 | if (!map) |
| 458 | return 0; /* Not running from a blorb file */ |
| 459 | |
| 460 | err = giblorb_load_resource(map, giblorb_method_Memory, &res, giblorb_ID_Data, filenum); |
| 461 | if (err) |
| 462 | return 0; /* Not found, or some other error */ |
| 463 | |
| 464 | if (res.chunktype == giblorb_ID_TEXT) |
| 465 | isbinary = FALSE; |
| 466 | else if (res.chunktype == giblorb_ID_BINA |
| 467 | || res.chunktype == giblorb_make_id('F', 'O', 'R', 'M')) |
| 468 | isbinary = TRUE; |
| 469 | else |
| 470 | return 0; /* Unknown chunk type */ |
| 471 | } |
| 472 | |
| 473 | str = gli_new_stream(strtype_Resource, |
| 474 | TRUE, FALSE, rock); |
| 475 | if (!str) { |
| 476 | gli_strict_warning("stream_open_resource_uni: unable to create stream."); |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | str->unicode = TRUE; |
| 481 | str->isbinary = isbinary; |
| 482 | |
| 483 | /* We have been handed an array of bytes. (They're big-endian |
| 484 | four-byte chunks, or perhaps a UTF-8 byte sequence, rather than |
| 485 | native-endian four-byte integers). So we drop it into buf, |
| 486 | rather than ubuf -- we'll have to do the translation in the |
| 487 | get() functions. */ |
| 488 | |
| 489 | if (res.data.ptr && res.length) { |
| 490 | str->buf = (unsigned char *)res.data.ptr; |
| 491 | str->bufptr = (unsigned char *)res.data.ptr; |
| 492 | str->buflen = res.length; |
| 493 | str->bufend = str->buf + str->buflen; |
| 494 | str->bufeof = str->bufend; |
| 495 | } |
| 496 | |
| 497 | return str; |
| 498 | } |
| 499 | |
| 500 | #endif /* GLK_MODULE_RESOURCE_STREAM */ |
| 501 | |
| 502 | strid_t gli_stream_open_pathname(char *pathname, int writemode, |
| 503 | int textmode, glui32 rock) |
| 504 | { |
| 505 | char modestr[16]; |
| 506 | stream_t *str; |
| 507 | FILE *fl; |
| 508 | |
| 509 | if (!writemode) |
| 510 | strcpy(modestr, "r"); |
| 511 | else |
| 512 | strcpy(modestr, "w"); |
| 513 | if (!textmode) |
| 514 | strcat(modestr, "b"); |
| 515 | |
| 516 | fl = fopen(pathname, modestr); |
| 517 | if (!fl) { |
| 518 | return NULL; |
| 519 | } |
| 520 | |
| 521 | str = gli_new_stream(strtype_File, |
| 522 | !writemode, writemode, rock); |
| 523 | if (!str) { |
| 524 | fclose(fl); |
| 525 | return NULL; |
| 526 | } |
| 527 | |
| 528 | str->isbinary = !textmode; |
| 529 | str->file = fl; |
| 530 | str->lastop = 0; |
| 531 | |
| 532 | return str; |
| 533 | } |
| 534 | |
| 535 | strid_t glk_stream_iterate(strid_t str, glui32 *rock) |
| 536 | { |
| 537 | if (!str) { |
| 538 | str = gli_streamlist; |
| 539 | } |
| 540 | else { |
| 541 | str = str->next; |
| 542 | } |
| 543 | |
| 544 | if (str) { |
| 545 | if (rock) |
| 546 | *rock = str->rock; |
| 547 | return str; |
| 548 | } |
| 549 | |
| 550 | if (rock) |
| 551 | *rock = 0; |
| 552 | return NULL; |
| 553 | } |
| 554 | |
| 555 | glui32 glk_stream_get_rock(stream_t *str) |
| 556 | { |
| 557 | if (!str) { |
| 558 | gli_strict_warning("stream_get_rock: invalid ref."); |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | return str->rock; |
| 563 | } |
| 564 | |
| 565 | void gli_stream_set_current(stream_t *str) |
| 566 | { |
| 567 | gli_currentstr = str; |
| 568 | } |
| 569 | |
| 570 | void glk_stream_set_current(stream_t *str) |
| 571 | { |
| 572 | gli_stream_set_current(str); |
| 573 | } |
| 574 | |
| 575 | strid_t glk_stream_get_current() |
| 576 | { |
| 577 | if (gli_currentstr) |
| 578 | return gli_currentstr; |
| 579 | else |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | void glk_stream_set_position(stream_t *str, glsi32 pos, glui32 seekmode) |
| 584 | { |
| 585 | if (!str) { |
| 586 | gli_strict_warning("stream_set_position: invalid ref"); |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | switch (str->type) { |
| 591 | case strtype_Memory: |
| 592 | case strtype_Resource: |
| 593 | if (!str->unicode || str->type == strtype_Resource) { |
| 594 | if (seekmode == seekmode_Current) { |
| 595 | pos = (str->bufptr - str->buf) + pos; |
| 596 | } |
| 597 | else if (seekmode == seekmode_End) { |
| 598 | pos = (str->bufeof - str->buf) + pos; |
| 599 | } |
| 600 | else { |
| 601 | /* pos = pos */ |
| 602 | } |
| 603 | if (pos < 0) |
| 604 | pos = 0; |
| 605 | if (pos > (str->bufeof - str->buf)) |
| 606 | pos = (str->bufeof - str->buf); |
| 607 | str->bufptr = str->buf + pos; |
| 608 | } |
| 609 | else { |
| 610 | if (seekmode == seekmode_Current) { |
| 611 | pos = (str->ubufptr - str->ubuf) + pos; |
| 612 | } |
| 613 | else if (seekmode == seekmode_End) { |
| 614 | pos = (str->ubufeof - str->ubuf) + pos; |
| 615 | } |
| 616 | else { |
| 617 | /* pos = pos */ |
| 618 | } |
| 619 | if (pos < 0) |
| 620 | pos = 0; |
| 621 | if (pos > (str->ubufeof - str->ubuf)) |
| 622 | pos = (str->ubufeof - str->ubuf); |
| 623 | str->ubufptr = str->ubuf + pos; |
| 624 | } |
| 625 | break; |
| 626 | case strtype_Window: |
| 627 | /* do nothing; don't pass to echo stream */ |
| 628 | break; |
| 629 | case strtype_File: |
| 630 | /* Either reading or writing is legal after an fseek. */ |
| 631 | str->lastop = 0; |
| 632 | if (str->unicode) { |
| 633 | /* Use 4 here, rather than sizeof(glui32). */ |
| 634 | pos *= 4; |
| 635 | } |
| 636 | fseek(str->file, pos, |
| 637 | ((seekmode == seekmode_Current) ? 1 : |
| 638 | ((seekmode == seekmode_End) ? 2 : 0))); |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | glui32 glk_stream_get_position(stream_t *str) |
| 644 | { |
| 645 | if (!str) { |
| 646 | gli_strict_warning("stream_get_position: invalid ref"); |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | switch (str->type) { |
| 651 | case strtype_Memory: |
| 652 | case strtype_Resource: |
| 653 | if (!str->unicode || str->type == strtype_Resource) { |
| 654 | return (str->bufptr - str->buf); |
| 655 | } |
| 656 | else { |
| 657 | return (str->ubufptr - str->ubuf); |
| 658 | } |
| 659 | case strtype_File: |
| 660 | if (!str->unicode) { |
| 661 | return ftell(str->file); |
| 662 | } |
| 663 | else { |
| 664 | /* Use 4 here, rather than sizeof(glui32). */ |
| 665 | return ftell(str->file) / 4; |
| 666 | } |
| 667 | case strtype_Window: |
| 668 | default: |
| 669 | return 0; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | static void gli_stream_ensure_op(stream_t *str, glui32 op) |
| 674 | { |
| 675 | /* We have to do an fseek() between reading and writing. This will |
| 676 | only come up for ReadWrite or WriteAppend files. */ |
| 677 | if (str->lastop != 0 && str->lastop != op) { |
| 678 | long pos = ftell(str->file); |
| 679 | fseek(str->file, pos, SEEK_SET); |
| 680 | } |
| 681 | str->lastop = op; |
| 682 | } |
| 683 | |
| 684 | static void gli_put_char(stream_t *str, unsigned char ch) |
| 685 | { |
| 686 | if (!str || !str->writable) |
| 687 | return; |
| 688 | |
| 689 | str->writecount++; |
| 690 | |
| 691 | switch (str->type) { |
| 692 | case strtype_Memory: |
| 693 | if (!str->unicode) { |
| 694 | if (str->bufptr < str->bufend) { |
| 695 | *(str->bufptr) = ch; |
| 696 | str->bufptr++; |
| 697 | if (str->bufptr > str->bufeof) |
| 698 | str->bufeof = str->bufptr; |
| 699 | } |
| 700 | } |
| 701 | else { |
| 702 | if (str->ubufptr < str->ubufend) { |
| 703 | *(str->ubufptr) = (glui32)ch; |
| 704 | str->ubufptr++; |
| 705 | if (str->ubufptr > str->ubufeof) |
| 706 | str->ubufeof = str->ubufptr; |
| 707 | } |
| 708 | } |
| 709 | break; |
| 710 | case strtype_Window: |
| 711 | #ifdef GARGLK |
| 712 | if (str->win->line_request || str->win->line_request_uni) { |
| 713 | if (gli_conf_safeclicks && gli_forceclick) { |
| 714 | glk_cancel_line_event(str->win, nullptr); |
| 715 | gli_forceclick = false; |
| 716 | } else { |
| 717 | gli_strict_warning("put_char: window has pending line request"); |
| 718 | break; |
| 719 | } |
| 720 | } |
| 721 | gli_window_put_char_uni(str->win, ch); |
| 722 | #else |
| 723 | if (str->win->line_request) { |
| 724 | gli_strict_warning("put_char: window has pending line request"); |
| 725 | break; |
| 726 | } |
| 727 | /* If you're going to convert Latin-1 to a different |
| 728 | character set, this is (a) place to do it. Only on the |
| 729 | putc(); not on the gli_put_char to echostr. */ |
| 730 | if (!gli_utf8output) |
| 731 | putc(ch, stdout); |
| 732 | else |
| 733 | gli_putchar_utf8(ch, stdout); |
| 734 | #endif |
| 735 | if (str->win->echostr) |
| 736 | gli_put_char(str->win->echostr, ch); |
| 737 | break; |
| 738 | case strtype_File: |
| 739 | gli_stream_ensure_op(str, filemode_Write); |
| 740 | if (!str->unicode) { |
| 741 | putc(ch, str->file); |
| 742 | } |
| 743 | else { |
| 744 | if (!str->isbinary) { |
| 745 | /* cheap UTF-8 stream */ |
| 746 | gli_putchar_utf8(ch, str->file); |
| 747 | } |
| 748 | else { |
| 749 | /* cheap big-endian stream */ |
| 750 | putc(0, str->file); |
| 751 | putc(0, str->file); |
| 752 | putc(0, str->file); |
| 753 | putc(ch, str->file); |
| 754 | } |
| 755 | } |
| 756 | fflush(str->file); |
| 757 | break; |
| 758 | case strtype_Resource: |
| 759 | /* resource streams are never writable */ |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | #ifdef GLK_MODULE_UNICODE |
| 765 | |
| 766 | static void gli_put_char_uni(stream_t *str, glui32 ch) |
| 767 | { |
| 768 | if (!str || !str->writable) |
| 769 | return; |
| 770 | |
| 771 | str->writecount++; |
| 772 | |
| 773 | switch (str->type) { |
| 774 | case strtype_Memory: |
| 775 | if (!str->unicode) { |
| 776 | if (ch >= 0x100) |
| 777 | ch = '?'; |
| 778 | if (str->bufptr < str->bufend) { |
| 779 | *(str->bufptr) = ch; |
| 780 | str->bufptr++; |
| 781 | if (str->bufptr > str->bufeof) |
| 782 | str->bufeof = str->bufptr; |
| 783 | } |
| 784 | } |
| 785 | else { |
| 786 | if (str->ubufptr < str->ubufend) { |
| 787 | *(str->ubufptr) = ch; |
| 788 | str->ubufptr++; |
| 789 | if (str->ubufptr > str->ubufeof) |
| 790 | str->ubufeof = str->ubufptr; |
| 791 | } |
| 792 | } |
| 793 | break; |
| 794 | case strtype_Window: |
| 795 | #ifdef GARGLK |
| 796 | if (str->win->line_request || str->win->line_request_uni) { |
| 797 | if (gli_conf_safeclicks && gli_forceclick) { |
| 798 | glk_cancel_line_event(str->win, nullptr); |
| 799 | gli_forceclick = false; |
| 800 | } else { |
| 801 | gli_strict_warning("put_char: window has pending line request"); |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | gli_window_put_char_uni(str->win, ch); |
| 806 | #else |
| 807 | if (str->win->line_request) { |
| 808 | gli_strict_warning("put_char_uni: window has pending line request"); |
| 809 | break; |
| 810 | } |
| 811 | /* If you're going to convert Latin-1 to a different |
| 812 | character set, this is (a) place to do it. Only on the |
| 813 | putc(); not on the gli_put_char to echostr. */ |
| 814 | if (!gli_utf8output) |
| 815 | putc((ch & 0xFF), stdout); |
| 816 | else |
| 817 | gli_putchar_utf8(ch, stdout); |
| 818 | #endif |
| 819 | if (str->win->echostr) |
| 820 | gli_put_char_uni(str->win->echostr, ch); |
| 821 | break; |
| 822 | case strtype_File: |
| 823 | gli_stream_ensure_op(str, filemode_Write); |
| 824 | if (!str->unicode) { |
| 825 | if (ch >= 0x100) |
| 826 | ch = '?'; |
| 827 | putc(ch, str->file); |
| 828 | } |
| 829 | else { |
| 830 | if (!str->isbinary) { |
| 831 | /* cheap UTF-8 stream */ |
| 832 | gli_putchar_utf8(ch, str->file); |
| 833 | } |
| 834 | else { |
| 835 | /* cheap big-endian stream */ |
| 836 | putc(((ch >> 24) & 0xFF), str->file); |
| 837 | putc(((ch >> 16) & 0xFF), str->file); |
| 838 | putc(((ch >> 8) & 0xFF), str->file); |
| 839 | putc( (ch & 0xFF), str->file); |
| 840 | } |
| 841 | } |
| 842 | fflush(str->file); |
| 843 | break; |
| 844 | case strtype_Resource: |
| 845 | /* resource streams are never writable */ |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | #endif /* GLK_MODULE_UNICODE */ |
| 851 | |
| 852 | static void gli_put_buffer(stream_t *str, char *buf, glui32 len) |
| 853 | { |
| 854 | glui32 lx; |
| 855 | |
| 856 | if (!str || !str->writable) |
| 857 | return; |
| 858 | |
| 859 | str->writecount += len; |
| 860 | |
| 861 | switch (str->type) { |
| 862 | case strtype_Memory: |
| 863 | if (!str->unicode) { |
| 864 | if (str->bufptr >= str->bufend) { |
| 865 | len = 0; |
| 866 | } |
| 867 | else { |
| 868 | if (str->bufptr + len > str->bufend) { |
| 869 | lx = (str->bufptr + len) - str->bufend; |
| 870 | if (lx < len) |
| 871 | len -= lx; |
| 872 | else |
| 873 | len = 0; |
| 874 | } |
| 875 | } |
| 876 | if (len) { |
| 877 | memcpy(str->bufptr, buf, len); |
| 878 | str->bufptr += len; |
| 879 | if (str->bufptr > str->bufeof) |
| 880 | str->bufeof = str->bufptr; |
| 881 | } |
| 882 | } |
| 883 | else { |
| 884 | if (str->ubufptr >= str->ubufend) { |
| 885 | len = 0; |
| 886 | } |
| 887 | else { |
| 888 | if (str->ubufptr + len > str->ubufend) { |
| 889 | lx = (str->ubufptr + len) - str->ubufend; |
| 890 | if (lx < len) |
| 891 | len -= lx; |
| 892 | else |
| 893 | len = 0; |
| 894 | } |
| 895 | } |
| 896 | if (len) { |
| 897 | for (lx=0; lx<len; lx++) { |
| 898 | *str->ubufptr = (unsigned char)(buf[lx]); |
| 899 | str->ubufptr++; |
| 900 | } |
| 901 | if (str->ubufptr > str->ubufeof) |
| 902 | str->ubufeof = str->ubufptr; |
| 903 | } |
| 904 | } |
| 905 | break; |
| 906 | case strtype_Window: |
| 907 | #ifdef GARGLK |
| 908 | if (str->win->line_request || str->win->line_request_uni) { |
| 909 | if (gli_conf_safeclicks && gli_forceclick) { |
| 910 | glk_cancel_line_event(str->win, nullptr); |
| 911 | gli_forceclick = false; |
| 912 | } else { |
| 913 | gli_strict_warning("put_buffer: window has pending line request"); |
| 914 | break; |
| 915 | } |
| 916 | } |
| 917 | for (lx = 0; lx < len; lx++) { |
| 918 | gli_window_put_char_uni(str->win, static_cast<unsigned char>(buf[lx])); |
| 919 | } |
| 920 | #else |
| 921 | if (str->win->line_request) { |
| 922 | gli_strict_warning("put_buffer: window has pending line request"); |
| 923 | break; |
| 924 | } |
| 925 | /* If you're going to convert Latin-1 to a different |
| 926 | character set, this is (a) place to do it. Only on the |
| 927 | fwrite(); not on the gli_put_buffer to echostr. */ |
| 928 | if (!gli_utf8output) { |
| 929 | fwrite((unsigned char *)buf, 1, len, stdout); |
| 930 | } |
| 931 | else { |
| 932 | for (lx=0; lx<len; lx++) |
| 933 | gli_putchar_utf8(((unsigned char *)buf)[lx], stdout); |
| 934 | } |
| 935 | #endif |
| 936 | if (str->win->echostr) |
| 937 | gli_put_buffer(str->win->echostr, buf, len); |
| 938 | break; |
| 939 | case strtype_File: |
| 940 | gli_stream_ensure_op(str, filemode_Write); |
| 941 | if (!str->unicode) { |
| 942 | fwrite(buf, 1, len, str->file); |
| 943 | } |
| 944 | else { |
| 945 | if (!str->isbinary) { |
| 946 | /* cheap UTF-8 stream */ |
| 947 | for (lx=0; lx<len; lx++) |
| 948 | gli_putchar_utf8(((unsigned char *)buf)[lx], str->file); |
| 949 | } |
| 950 | else { |
| 951 | /* cheap big-endian stream */ |
| 952 | for (lx=0; lx<len; lx++) { |
| 953 | unsigned char ch = ((unsigned char *)buf)[lx]; |
| 954 | putc(((ch >> 24) & 0xFF), str->file); |
| 955 | putc(((ch >> 16) & 0xFF), str->file); |
| 956 | putc(((ch >> 8) & 0xFF), str->file); |
| 957 | putc( (ch & 0xFF), str->file); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | fflush(str->file); |
| 962 | break; |
| 963 | case strtype_Resource: |
| 964 | /* resource streams are never writable */ |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | void gli_stream_echo_line(stream_t *str, char *buf, glui32 len) |
| 970 | { |
| 971 | /* This is only used to echo line input to an echo stream. See |
| 972 | glk_select(). */ |
| 973 | gli_put_buffer(str, buf, len); |
| 974 | gli_put_char(str, '\n'); |
| 975 | } |
| 976 | |
| 977 | #ifdef GLK_MODULE_UNICODE |
| 978 | |
| 979 | void gli_stream_echo_line_uni(stream_t *str, glui32 *buf, glui32 len) |
| 980 | { |
| 981 | glui32 ix; |
| 982 | /* This is only used to echo line input to an echo stream. See |
| 983 | glk_select(). */ |
| 984 | for (ix=0; ix<len; ix++) { |
| 985 | gli_put_char_uni(str, buf[ix]); |
| 986 | } |
| 987 | gli_put_char(str, '\n'); |
| 988 | } |
| 989 | |
| 990 | #else |
| 991 | |
| 992 | void gli_stream_echo_line_uni(stream_t *str, glui32 *buf, glui32 len) |
| 993 | { |
| 994 | gli_strict_warning("stream_echo_line_uni: called with no Unicode line request"); |
| 995 | } |
| 996 | |
| 997 | #endif /* GLK_MODULE_UNICODE */ |
| 998 | |
| 999 | static glsi32 gli_get_char(stream_t *str, int want_unicode) |
| 1000 | { |
| 1001 | if (!str || !str->readable) |
| 1002 | return -1; |
| 1003 | |
| 1004 | switch (str->type) { |
| 1005 | case strtype_Resource: |
| 1006 | if (str->unicode) { |
| 1007 | glui32 ch; |
| 1008 | if (str->isbinary) { |
| 1009 | /* cheap big-endian stream */ |
| 1010 | if (str->bufptr >= str->bufend) |
| 1011 | return -1; |
| 1012 | ch = *(str->bufptr); |
| 1013 | str->bufptr++; |
| 1014 | if (str->bufptr >= str->bufend) |
| 1015 | return -1; |
| 1016 | ch = (ch << 8) | (*(str->bufptr) & 0xFF); |
| 1017 | str->bufptr++; |
| 1018 | if (str->bufptr >= str->bufend) |
| 1019 | return -1; |
| 1020 | ch = (ch << 8) | (*(str->bufptr) & 0xFF); |
| 1021 | str->bufptr++; |
| 1022 | if (str->bufptr >= str->bufend) |
| 1023 | return -1; |
| 1024 | ch = (ch << 8) | (*(str->bufptr) & 0xFF); |
| 1025 | str->bufptr++; |
| 1026 | } |
| 1027 | else { |
| 1028 | /* slightly less cheap UTF8 stream */ |
| 1029 | glui32 val0, val1, val2, val3; |
| 1030 | int flag = UTF8_DECODE_INLINE(&ch, (str->bufptr >= str->bufend), (*(str->bufptr++)), val0, val1, val2, val3); |
| 1031 | if (!flag) |
| 1032 | return -1; |
| 1033 | } |
| 1034 | str->readcount++; |
| 1035 | if (!want_unicode && ch >= 0x100) |
| 1036 | return '?'; |
| 1037 | return (glsi32)ch; |
| 1038 | } |
| 1039 | /* for text streams, fall through to memory case */ |
| 1040 | case strtype_Memory: |
| 1041 | if (!str->unicode) { |
| 1042 | if (str->bufptr < str->bufend) { |
| 1043 | unsigned char ch; |
| 1044 | ch = *(str->bufptr); |
| 1045 | str->bufptr++; |
| 1046 | str->readcount++; |
| 1047 | return ch; |
| 1048 | } |
| 1049 | else { |
| 1050 | return -1; |
| 1051 | } |
| 1052 | } |
| 1053 | else { |
| 1054 | if (str->ubufptr < str->ubufend) { |
| 1055 | glui32 ch; |
| 1056 | ch = *(str->ubufptr); |
| 1057 | str->ubufptr++; |
| 1058 | str->readcount++; |
| 1059 | if (!want_unicode && ch >= 0x100) |
| 1060 | return '?'; |
| 1061 | return ch; |
| 1062 | } |
| 1063 | else { |
| 1064 | return -1; |
| 1065 | } |
| 1066 | } |
| 1067 | case strtype_File: |
| 1068 | gli_stream_ensure_op(str, filemode_Read); |
| 1069 | if (!str->unicode) { |
| 1070 | int res; |
| 1071 | res = getc(str->file); |
| 1072 | if (res != -1) { |
| 1073 | str->readcount++; |
| 1074 | return (glsi32)res; |
| 1075 | } |
| 1076 | else { |
| 1077 | return -1; |
| 1078 | } |
| 1079 | } |
| 1080 | else if (str->isbinary) { |
| 1081 | /* cheap big-endian stream */ |
| 1082 | int res; |
| 1083 | glui32 ch; |
| 1084 | res = getc(str->file); |
| 1085 | if (res == -1) |
| 1086 | return -1; |
| 1087 | ch = (res & 0xFF); |
| 1088 | res = getc(str->file); |
| 1089 | if (res == -1) |
| 1090 | return -1; |
| 1091 | ch = (ch << 8) | (res & 0xFF); |
| 1092 | res = getc(str->file); |
| 1093 | if (res == -1) |
| 1094 | return -1; |
| 1095 | ch = (ch << 8) | (res & 0xFF); |
| 1096 | res = getc(str->file); |
| 1097 | if (res == -1) |
| 1098 | return -1; |
| 1099 | ch = (ch << 8) | (res & 0xFF); |
| 1100 | str->readcount++; |
| 1101 | if (!want_unicode && ch >= 0x100) |
| 1102 | return '?'; |
| 1103 | return (glsi32)ch; |
| 1104 | } |
| 1105 | else { |
| 1106 | /* slightly less cheap UTF-8 stream */ |
| 1107 | glui32 val0, val1, val2, val3; |
| 1108 | int res; |
| 1109 | glui32 ch; |
| 1110 | int flag = UTF8_DECODE_INLINE(&ch, (res=getc(str->file), res == -1), (res & 0xFF), val0, val1, val2, val3); |
| 1111 | if (!flag) |
| 1112 | return -1; |
| 1113 | str->readcount++; |
| 1114 | if (!want_unicode && ch >= 0x100) |
| 1115 | return '?'; |
| 1116 | return (glsi32)ch; |
| 1117 | } |
| 1118 | case strtype_Window: |
| 1119 | default: |
| 1120 | return -1; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | static glui32 gli_get_buffer(stream_t *str, char *cbuf, glui32 *ubuf, |
| 1125 | glui32 len) |
| 1126 | { |
| 1127 | if (!str || !str->readable) |
| 1128 | return 0; |
| 1129 | |
| 1130 | switch (str->type) { |
| 1131 | case strtype_Resource: |
| 1132 | if (str->unicode) { |
| 1133 | glui32 count = 0; |
| 1134 | while (count < len) { |
| 1135 | glui32 ch; |
| 1136 | if (str->isbinary) { |
| 1137 | /* cheap big-endian stream */ |
| 1138 | if (str->bufptr >= str->bufend) |
| 1139 | break; |
| 1140 | ch = *(str->bufptr); |
| 1141 | str->bufptr++; |
| 1142 | if (str->bufptr >= str->bufend) |
| 1143 | break; |
| 1144 | ch = (ch << 8) | (*(str->bufptr) & 0xFF); |
| 1145 | str->bufptr++; |
| 1146 | if (str->bufptr >= str->bufend) |
| 1147 | break; |
| 1148 | ch = (ch << 8) | (*(str->bufptr) & 0xFF); |
| 1149 | str->bufptr++; |
| 1150 | if (str->bufptr >= str->bufend) |
| 1151 | break; |
| 1152 | ch = (ch << 8) | (*(str->bufptr) & 0xFF); |
| 1153 | str->bufptr++; |
| 1154 | } |
| 1155 | else { |
| 1156 | /* slightly less cheap UTF8 stream */ |
| 1157 | glui32 val0, val1, val2, val3; |
| 1158 | int flag = UTF8_DECODE_INLINE(&ch, (str->bufptr >= str->bufend), (*(str->bufptr++)), val0, val1, val2, val3); |
| 1159 | if (!flag) |
| 1160 | break; |
| 1161 | } |
| 1162 | if (cbuf) { |
| 1163 | if (ch >= 0x100) |
| 1164 | cbuf[count] = '?'; |
| 1165 | else |
| 1166 | cbuf[count] = ch; |
| 1167 | } |
| 1168 | else { |
| 1169 | ubuf[count] = ch; |
| 1170 | } |
| 1171 | count++; |
| 1172 | } |
| 1173 | str->readcount += count; |
| 1174 | return count; |
| 1175 | } |
| 1176 | /* for text streams, fall through to memory case */ |
| 1177 | case strtype_Memory: |
| 1178 | if (!str->unicode) { |
| 1179 | if (str->bufptr >= str->bufend) { |
| 1180 | len = 0; |
| 1181 | } |
| 1182 | else { |
| 1183 | if (str->bufptr + len > str->bufend) { |
| 1184 | glui32 lx; |
| 1185 | lx = (str->bufptr + len) - str->bufend; |
| 1186 | if (lx < len) |
| 1187 | len -= lx; |
| 1188 | else |
| 1189 | len = 0; |
| 1190 | } |
| 1191 | } |
| 1192 | if (len) { |
| 1193 | if (cbuf) { |
| 1194 | memcpy(cbuf, str->bufptr, len); |
| 1195 | } |
| 1196 | else { |
| 1197 | glui32 lx; |
| 1198 | for (lx=0; lx<len; lx++) { |
| 1199 | ubuf[lx] = (unsigned char)str->bufptr[lx]; |
| 1200 | } |
| 1201 | } |
| 1202 | str->bufptr += len; |
| 1203 | if (str->bufptr > str->bufeof) |
| 1204 | str->bufeof = str->bufptr; |
| 1205 | } |
| 1206 | } |
| 1207 | else { |
| 1208 | if (str->ubufptr >= str->ubufend) { |
| 1209 | len = 0; |
| 1210 | } |
| 1211 | else { |
| 1212 | if (str->ubufptr + len > str->ubufend) { |
| 1213 | glui32 lx; |
| 1214 | lx = (str->ubufptr + len) - str->ubufend; |
| 1215 | if (lx < len) |
| 1216 | len -= lx; |
| 1217 | else |
| 1218 | len = 0; |
| 1219 | } |
| 1220 | } |
| 1221 | if (len) { |
| 1222 | glui32 lx, ch; |
| 1223 | if (cbuf) { |
| 1224 | for (lx=0; lx<len; lx++) { |
| 1225 | ch = str->ubufptr[lx]; |
| 1226 | if (ch >= 0x100) |
| 1227 | ch = '?'; |
| 1228 | cbuf[lx] = ch; |
| 1229 | } |
| 1230 | } |
| 1231 | else { |
| 1232 | for (lx=0; lx<len; lx++) { |
| 1233 | ubuf[lx] = str->ubufptr[lx]; |
| 1234 | } |
| 1235 | } |
| 1236 | str->ubufptr += len; |
| 1237 | if (str->ubufptr > str->ubufeof) |
| 1238 | str->ubufeof = str->ubufptr; |
| 1239 | } |
| 1240 | } |
| 1241 | str->readcount += len; |
| 1242 | return len; |
| 1243 | case strtype_File: |
| 1244 | gli_stream_ensure_op(str, filemode_Read); |
| 1245 | if (!str->unicode) { |
| 1246 | if (cbuf) { |
| 1247 | glui32 res; |
| 1248 | res = fread(cbuf, 1, len, str->file); |
| 1249 | str->readcount += res; |
| 1250 | return res; |
| 1251 | } |
| 1252 | else { |
| 1253 | glui32 lx; |
| 1254 | for (lx=0; lx<len; lx++) { |
| 1255 | int res; |
| 1256 | glui32 ch; |
| 1257 | res = getc(str->file); |
| 1258 | if (res == -1) |
| 1259 | break; |
| 1260 | ch = (res & 0xFF); |
| 1261 | str->readcount++; |
| 1262 | ubuf[lx] = ch; |
| 1263 | } |
| 1264 | return lx; |
| 1265 | } |
| 1266 | } |
| 1267 | else if (str->isbinary) { |
| 1268 | /* cheap big-endian stream */ |
| 1269 | glui32 lx; |
| 1270 | for (lx=0; lx<len; lx++) { |
| 1271 | int res; |
| 1272 | glui32 ch; |
| 1273 | res = getc(str->file); |
| 1274 | if (res == -1) |
| 1275 | break; |
| 1276 | ch = (res & 0xFF); |
| 1277 | res = getc(str->file); |
| 1278 | if (res == -1) |
| 1279 | break; |
| 1280 | ch = (ch << 8) | (res & 0xFF); |
| 1281 | res = getc(str->file); |
| 1282 | if (res == -1) |
| 1283 | break; |
| 1284 | ch = (ch << 8) | (res & 0xFF); |
| 1285 | res = getc(str->file); |
| 1286 | if (res == -1) |
| 1287 | break; |
| 1288 | ch = (ch << 8) | (res & 0xFF); |
| 1289 | str->readcount++; |
| 1290 | if (cbuf) { |
| 1291 | if (ch >= 0x100) |
| 1292 | ch = '?'; |
| 1293 | cbuf[lx] = ch; |
| 1294 | } |
| 1295 | else { |
| 1296 | ubuf[lx] = ch; |
| 1297 | } |
| 1298 | } |
| 1299 | return lx; |
| 1300 | } |
| 1301 | else { |
| 1302 | /* slightly less cheap UTF-8 stream */ |
| 1303 | glui32 lx; |
| 1304 | for (lx=0; lx<len; lx++) { |
| 1305 | glui32 val0, val1, val2, val3; |
| 1306 | int res; |
| 1307 | glui32 ch; |
| 1308 | int flag = UTF8_DECODE_INLINE(&ch, (res=getc(str->file), res == -1), (res & 0xFF), val0, val1, val2, val3); |
| 1309 | if (!flag) |
| 1310 | break; |
| 1311 | str->readcount++; |
| 1312 | if (cbuf) { |
| 1313 | if (ch >= 0x100) |
| 1314 | ch = '?'; |
| 1315 | cbuf[lx] = ch; |
| 1316 | } |
| 1317 | else { |
| 1318 | ubuf[lx] = ch; |
| 1319 | } |
| 1320 | } |
| 1321 | return lx; |
| 1322 | } |
| 1323 | case strtype_Window: |
| 1324 | default: |
| 1325 | return 0; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | static glui32 gli_get_line(stream_t *str, char *cbuf, glui32 *ubuf, |
| 1330 | glui32 len) |
| 1331 | { |
| 1332 | glui32 lx; |
| 1333 | int gotnewline; |
| 1334 | |
| 1335 | if (!str || !str->readable) |
| 1336 | return 0; |
| 1337 | |
| 1338 | switch (str->type) { |
| 1339 | case strtype_Resource: |
| 1340 | if (len == 0) |
| 1341 | return 0; |
| 1342 | len -= 1; /* for the terminal null */ |
| 1343 | if (str->unicode) { |
| 1344 | glui32 count = 0; |
| 1345 | while (count < len) { |
| 1346 | glui32 ch; |
| 1347 | if (str->isbinary) { |
| 1348 | /* cheap big-endian stream */ |
| 1349 | if (str->bufptr >= str->bufend) |
| 1350 | break; |
| 1351 | ch = *(str->bufptr); |
| 1352 | str->bufptr++; |
| 1353 | if (str->bufptr >= str->bufend) |
| 1354 | break; |
| 1355 | ch = (ch << 8) | (*(str->bufptr) & 0xFF); |
| 1356 | str->bufptr++; |
| 1357 | if (str->bufptr >= str->bufend) |
| 1358 | break; |
| 1359 | ch = (ch << 8) | (*(str->bufptr) & 0xFF); |
| 1360 | str->bufptr++; |
| 1361 | if (str->bufptr >= str->bufend) |
| 1362 | break; |
| 1363 | ch = (ch << 8) | (*(str->bufptr) & 0xFF); |
| 1364 | str->bufptr++; |
| 1365 | } |
| 1366 | else { |
| 1367 | /* slightly less cheap UTF8 stream */ |
| 1368 | glui32 val0, val1, val2, val3; |
| 1369 | int flag = UTF8_DECODE_INLINE(&ch, (str->bufptr >= str->bufend), (*(str->bufptr++)), val0, val1, val2, val3); |
| 1370 | if (!flag) |
| 1371 | break; |
| 1372 | } |
| 1373 | if (cbuf) { |
| 1374 | if (ch >= 0x100) |
| 1375 | cbuf[count] = '?'; |
| 1376 | else |
| 1377 | cbuf[count] = ch; |
| 1378 | } |
| 1379 | else { |
| 1380 | ubuf[count] = ch; |
| 1381 | } |
| 1382 | count++; |
| 1383 | if (ch == '\n') |
| 1384 | break; |
| 1385 | } |
| 1386 | if (cbuf) |
| 1387 | cbuf[count] = '\0'; |
| 1388 | else |
| 1389 | ubuf[count] = '\0'; |
| 1390 | str->readcount += count; |
| 1391 | return count; |
| 1392 | } |
| 1393 | /* for text streams, fall through to memory case */ |
| 1394 | case strtype_Memory: |
| 1395 | if (len == 0) |
| 1396 | return 0; |
| 1397 | len -= 1; /* for the terminal null */ |
| 1398 | if (!str->unicode) { |
| 1399 | if (str->bufptr >= str->bufend) { |
| 1400 | len = 0; |
| 1401 | } |
| 1402 | else { |
| 1403 | if (str->bufptr + len > str->bufend) { |
| 1404 | lx = (str->bufptr + len) - str->bufend; |
| 1405 | if (lx < len) |
| 1406 | len -= lx; |
| 1407 | else |
| 1408 | len = 0; |
| 1409 | } |
| 1410 | } |
| 1411 | gotnewline = FALSE; |
| 1412 | if (cbuf) { |
| 1413 | for (lx=0; lx<len && !gotnewline; lx++) { |
| 1414 | cbuf[lx] = str->bufptr[lx]; |
| 1415 | gotnewline = (cbuf[lx] == '\n'); |
| 1416 | } |
| 1417 | cbuf[lx] = '\0'; |
| 1418 | } |
| 1419 | else { |
| 1420 | for (lx=0; lx<len && !gotnewline; lx++) { |
| 1421 | ubuf[lx] = (unsigned char)str->bufptr[lx]; |
| 1422 | gotnewline = (ubuf[lx] == '\n'); |
| 1423 | } |
| 1424 | ubuf[lx] = '\0'; |
| 1425 | } |
| 1426 | str->bufptr += lx; |
| 1427 | } |
| 1428 | else { |
| 1429 | if (str->ubufptr >= str->ubufend) { |
| 1430 | len = 0; |
| 1431 | } |
| 1432 | else { |
| 1433 | if (str->ubufptr + len > str->ubufend) { |
| 1434 | lx = (str->ubufptr + len) - str->ubufend; |
| 1435 | if (lx < len) |
| 1436 | len -= lx; |
| 1437 | else |
| 1438 | len = 0; |
| 1439 | } |
| 1440 | } |
| 1441 | gotnewline = FALSE; |
| 1442 | if (cbuf) { |
| 1443 | for (lx=0; lx<len && !gotnewline; lx++) { |
| 1444 | glui32 ch; |
| 1445 | ch = str->ubufptr[lx]; |
| 1446 | if (ch >= 0x100) |
| 1447 | ch = '?'; |
| 1448 | cbuf[lx] = ch; |
| 1449 | gotnewline = (ch == '\n'); |
| 1450 | } |
| 1451 | cbuf[lx] = '\0'; |
| 1452 | } |
| 1453 | else { |
| 1454 | for (lx=0; lx<len && !gotnewline; lx++) { |
| 1455 | glui32 ch; |
| 1456 | ch = str->ubufptr[lx]; |
| 1457 | ubuf[lx] = ch; |
| 1458 | gotnewline = (ch == '\n'); |
| 1459 | } |
| 1460 | ubuf[lx] = '\0'; |
| 1461 | } |
| 1462 | str->ubufptr += lx; |
| 1463 | } |
| 1464 | str->readcount += lx; |
| 1465 | return lx; |
| 1466 | case strtype_File: |
| 1467 | gli_stream_ensure_op(str, filemode_Read); |
| 1468 | if (!str->unicode) { |
| 1469 | if (cbuf) { |
| 1470 | char *res; |
| 1471 | res = fgets(cbuf, len, str->file); |
| 1472 | if (!res) { |
| 1473 | return 0; |
| 1474 | } |
| 1475 | else { |
| 1476 | glui32 lx = strlen(cbuf); |
| 1477 | str->readcount += lx; |
| 1478 | return lx; |
| 1479 | } |
| 1480 | } |
| 1481 | else { |
| 1482 | glui32 lx; |
| 1483 | if (len == 0) |
| 1484 | return 0; |
| 1485 | len -= 1; /* for the terminal null */ |
| 1486 | gotnewline = FALSE; |
| 1487 | for (lx=0; lx<len && !gotnewline; lx++) { |
| 1488 | int res; |
| 1489 | glui32 ch; |
| 1490 | res = getc(str->file); |
| 1491 | if (res == -1) |
| 1492 | break; |
| 1493 | ch = (res & 0xFF); |
| 1494 | str->readcount++; |
| 1495 | ubuf[lx] = ch; |
| 1496 | gotnewline = (ch == '\n'); |
| 1497 | } |
| 1498 | ubuf[lx] = '\0'; |
| 1499 | return lx; |
| 1500 | } |
| 1501 | } |
| 1502 | else if (str->isbinary) { |
| 1503 | /* cheap big-endian stream */ |
| 1504 | glui32 lx; |
| 1505 | if (len == 0) |
| 1506 | return 0; |
| 1507 | len -= 1; /* for the terminal null */ |
| 1508 | gotnewline = FALSE; |
| 1509 | for (lx=0; lx<len && !gotnewline; lx++) { |
| 1510 | int res; |
| 1511 | glui32 ch; |
| 1512 | res = getc(str->file); |
| 1513 | if (res == -1) |
| 1514 | break; |
| 1515 | ch = (res & 0xFF); |
| 1516 | res = getc(str->file); |
| 1517 | if (res == -1) |
| 1518 | break; |
| 1519 | ch = (ch << 8) | (res & 0xFF); |
| 1520 | res = getc(str->file); |
| 1521 | if (res == -1) |
| 1522 | break; |
| 1523 | ch = (ch << 8) | (res & 0xFF); |
| 1524 | res = getc(str->file); |
| 1525 | if (res == -1) |
| 1526 | break; |
| 1527 | ch = (ch << 8) | (res & 0xFF); |
| 1528 | str->readcount++; |
| 1529 | if (cbuf) { |
| 1530 | if (ch >= 0x100) |
| 1531 | ch = '?'; |
| 1532 | cbuf[lx] = ch; |
| 1533 | } |
| 1534 | else { |
| 1535 | ubuf[lx] = ch; |
| 1536 | } |
| 1537 | gotnewline = (ch == '\n'); |
| 1538 | } |
| 1539 | if (cbuf) |
| 1540 | cbuf[lx] = '\0'; |
| 1541 | else |
| 1542 | ubuf[lx] = '\0'; |
| 1543 | return lx; |
| 1544 | } |
| 1545 | else { |
| 1546 | /* slightly less cheap UTF-8 stream */ |
| 1547 | glui32 lx; |
| 1548 | if (len == 0) |
| 1549 | return 0; |
| 1550 | len -= 1; /* for the terminal null */ |
| 1551 | gotnewline = FALSE; |
| 1552 | for (lx=0; lx<len && !gotnewline; lx++) { |
| 1553 | glui32 val0, val1, val2, val3; |
| 1554 | int res; |
| 1555 | glui32 ch; |
| 1556 | int flag = UTF8_DECODE_INLINE(&ch, (res=getc(str->file), res == -1), (res & 0xFF), val0, val1, val2, val3); |
| 1557 | if (!flag) |
| 1558 | break; |
| 1559 | str->readcount++; |
| 1560 | if (cbuf) { |
| 1561 | if (ch >= 0x100) |
| 1562 | ch = '?'; |
| 1563 | cbuf[lx] = ch; |
| 1564 | } |
| 1565 | else { |
| 1566 | ubuf[lx] = ch; |
| 1567 | } |
| 1568 | gotnewline = (ch == '\n'); |
| 1569 | } |
| 1570 | if (cbuf) |
| 1571 | cbuf[lx] = '\0'; |
| 1572 | else |
| 1573 | ubuf[lx] = '\0'; |
| 1574 | return lx; |
| 1575 | } |
| 1576 | case strtype_Window: |
| 1577 | default: |
| 1578 | return 0; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | void glk_put_char(unsigned char ch) |
| 1583 | { |
| 1584 | gli_put_char(gli_currentstr, ch); |
| 1585 | } |
| 1586 | |
| 1587 | void glk_put_char_stream(stream_t *str, unsigned char ch) |
| 1588 | { |
| 1589 | if (!str) { |
| 1590 | gli_strict_warning("put_char_stream: invalid ref"); |
| 1591 | return; |
| 1592 | } |
| 1593 | gli_put_char(str, ch); |
| 1594 | } |
| 1595 | |
| 1596 | void glk_put_string(char *s) |
| 1597 | { |
| 1598 | gli_put_buffer(gli_currentstr, s, strlen(s)); |
| 1599 | } |
| 1600 | |
| 1601 | void glk_put_string_stream(stream_t *str, char *s) |
| 1602 | { |
| 1603 | if (!str) { |
| 1604 | gli_strict_warning("put_string_stream: invalid ref"); |
| 1605 | return; |
| 1606 | } |
| 1607 | gli_put_buffer(str, s, strlen(s)); |
| 1608 | } |
| 1609 | |
| 1610 | void glk_put_buffer(char *buf, glui32 len) |
| 1611 | { |
| 1612 | gli_put_buffer(gli_currentstr, buf, len); |
| 1613 | } |
| 1614 | |
| 1615 | void glk_put_buffer_stream(stream_t *str, char *buf, glui32 len) |
| 1616 | { |
| 1617 | if (!str) { |
| 1618 | gli_strict_warning("put_string_stream: invalid ref"); |
| 1619 | return; |
| 1620 | } |
| 1621 | gli_put_buffer(str, buf, len); |
| 1622 | } |
| 1623 | |
| 1624 | #ifdef GLK_MODULE_UNICODE |
| 1625 | |
| 1626 | void glk_put_char_uni(glui32 ch) |
| 1627 | { |
| 1628 | gli_put_char_uni(gli_currentstr, ch); |
| 1629 | } |
| 1630 | |
| 1631 | void glk_put_char_stream_uni(stream_t *str, glui32 ch) |
| 1632 | { |
| 1633 | if (!str) { |
| 1634 | gli_strict_warning("put_char_stream: invalid ref"); |
| 1635 | return; |
| 1636 | } |
| 1637 | gli_put_char_uni(str, ch); |
| 1638 | } |
| 1639 | |
| 1640 | void glk_put_string_uni(glui32 *us) |
| 1641 | { |
| 1642 | int len = 0; |
| 1643 | glui32 val; |
| 1644 | |
| 1645 | while (1) { |
| 1646 | val = us[len]; |
| 1647 | if (!val) |
| 1648 | break; |
| 1649 | gli_put_char_uni(gli_currentstr, val); |
| 1650 | len++; |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | void glk_put_string_stream_uni(stream_t *str, glui32 *us) |
| 1655 | { |
| 1656 | int len = 0; |
| 1657 | glui32 val; |
| 1658 | |
| 1659 | if (!str) { |
| 1660 | gli_strict_warning("put_string_stream: invalid ref"); |
| 1661 | return; |
| 1662 | } |
| 1663 | |
| 1664 | while (1) { |
| 1665 | val = us[len]; |
| 1666 | if (!val) |
| 1667 | break; |
| 1668 | gli_put_char_uni(str, val); |
| 1669 | len++; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | void glk_put_buffer_uni(glui32 *buf, glui32 len) |
| 1674 | { |
| 1675 | glui32 ix; |
| 1676 | for (ix=0; ix<len; ix++) { |
| 1677 | gli_put_char_uni(gli_currentstr, buf[ix]); |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | void glk_put_buffer_stream_uni(stream_t *str, glui32 *buf, glui32 len) |
| 1682 | { |
| 1683 | glui32 ix; |
| 1684 | if (!str) { |
| 1685 | gli_strict_warning("put_string_stream: invalid ref"); |
| 1686 | return; |
| 1687 | } |
| 1688 | for (ix=0; ix<len; ix++) { |
| 1689 | gli_put_char_uni(str, buf[ix]); |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | glsi32 glk_get_char_stream_uni(strid_t str) |
| 1694 | { |
| 1695 | if (!str) { |
| 1696 | gli_strict_warning("get_char_stream_uni: invalid ref"); |
| 1697 | return -1; |
| 1698 | } |
| 1699 | return gli_get_char(str, 1); |
| 1700 | } |
| 1701 | |
| 1702 | glui32 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len) |
| 1703 | { |
| 1704 | if (!str) { |
| 1705 | gli_strict_warning("get_buffer_stream_uni: invalid ref"); |
| 1706 | return -1; |
| 1707 | } |
| 1708 | return gli_get_buffer(str, NULL, buf, len); |
| 1709 | } |
| 1710 | |
| 1711 | glui32 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len) |
| 1712 | { |
| 1713 | if (!str) { |
| 1714 | gli_strict_warning("get_line_stream_uni: invalid ref"); |
| 1715 | return -1; |
| 1716 | } |
| 1717 | return gli_get_line(str, NULL, buf, len); |
| 1718 | } |
| 1719 | |
| 1720 | #endif /* GLK_MODULE_UNICODE */ |
| 1721 | |
| 1722 | void glk_set_style(glui32 val) |
| 1723 | { |
| 1724 | /* This cheap library doesn't handle styles */ |
| 1725 | #ifdef GARGLK |
| 1726 | gli_set_style(gli_currentstr, val); |
| 1727 | #endif |
| 1728 | } |
| 1729 | |
| 1730 | void glk_set_style_stream(stream_t *str, glui32 val) |
| 1731 | { |
| 1732 | if (!str) { |
| 1733 | gli_strict_warning("set_style_stream: invalid ref"); |
| 1734 | return; |
| 1735 | } |
| 1736 | /* This cheap library doesn't handle styles */ |
| 1737 | #ifdef GARGLK |
| 1738 | gli_set_style(str, val); |
| 1739 | #endif |
| 1740 | } |
| 1741 | |
| 1742 | glsi32 glk_get_char_stream(stream_t *str) |
| 1743 | { |
| 1744 | if (!str) { |
| 1745 | gli_strict_warning("get_char_stream: invalid ref"); |
| 1746 | return -1; |
| 1747 | } |
| 1748 | return gli_get_char(str, 0); |
| 1749 | } |
| 1750 | |
| 1751 | glui32 glk_get_line_stream(stream_t *str, char *buf, glui32 len) |
| 1752 | { |
| 1753 | if (!str) { |
| 1754 | gli_strict_warning("get_line_stream: invalid ref"); |
| 1755 | return -1; |
| 1756 | } |
| 1757 | return gli_get_line(str, buf, NULL, len); |
| 1758 | } |
| 1759 | |
| 1760 | glui32 glk_get_buffer_stream(stream_t *str, char *buf, glui32 len) |
| 1761 | { |
| 1762 | if (!str) { |
| 1763 | gli_strict_warning("get_buffer_stream: invalid ref"); |
| 1764 | return -1; |
| 1765 | } |
| 1766 | return gli_get_buffer(str, buf, NULL, len); |
| 1767 | } |
| 1768 | |
| 1769 | #ifdef GARGLK |
| 1770 | glui32 gli_strlen_uni(const glui32 *s) |
| 1771 | { |
| 1772 | glui32 length = 0; |
| 1773 | while (*s++ != 0) { |
| 1774 | length++; |
| 1775 | } |
| 1776 | return length; |
| 1777 | } |
| 1778 | |
| 1779 | stream_t *gli_stream_open_window(window_t *win) |
| 1780 | { |
| 1781 | stream_t *str; |
| 1782 | |
| 1783 | str = gli_new_stream(strtype_Window, false, true, 0); |
| 1784 | if (str == nullptr) { |
| 1785 | return nullptr; |
| 1786 | } |
| 1787 | |
| 1788 | str->win = win; |
| 1789 | str->unicode = true; |
| 1790 | |
| 1791 | return str; |
| 1792 | } |
| 1793 | |
| 1794 | static void gli_set_hyperlink(stream_t *str, glui32 linkval) |
| 1795 | { |
| 1796 | if (str == nullptr || !str->writable) { |
| 1797 | return; |
| 1798 | } |
| 1799 | |
| 1800 | switch (str->type) { |
| 1801 | case strtype_Window: |
| 1802 | str->win->attr.hyper = linkval; |
| 1803 | break; |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | void glk_set_hyperlink(glui32 linkval) |
| 1808 | { |
| 1809 | gli_set_hyperlink(gli_currentstr, linkval); |
| 1810 | } |
| 1811 | |
| 1812 | void glk_set_hyperlink_stream(stream_t *str, glui32 linkval) |
| 1813 | { |
| 1814 | if (str == nullptr) { |
| 1815 | gli_strict_warning("set_hyperlink_stream: invalid ref"); |
| 1816 | return; |
| 1817 | } |
| 1818 | gli_set_hyperlink(str, linkval); |
| 1819 | } |
| 1820 | |
| 1821 | static void gli_set_style(stream_t *str, glui32 val) |
| 1822 | { |
| 1823 | if (str == nullptr || !str->writable) { |
| 1824 | return; |
| 1825 | } |
| 1826 | |
| 1827 | if (val < 0 || val >= style_NUMSTYLES) { |
| 1828 | val = 0; |
| 1829 | } |
| 1830 | |
| 1831 | switch (str->type) { |
| 1832 | case strtype_Window: |
| 1833 | str->win->attr.style = val; |
| 1834 | if (str->win->echostr != nullptr) { |
| 1835 | gli_set_style(str->win->echostr, val); |
| 1836 | } |
| 1837 | break; |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | static void gli_set_zcolors(stream_t *str, glui32 fg, glui32 bg) |
| 1842 | { |
| 1843 | if (str == nullptr || !str->writable) { |
| 1844 | return; |
| 1845 | } |
| 1846 | |
| 1847 | if (!gli_conf_stylehint) { |
| 1848 | return; |
| 1849 | } |
| 1850 | |
| 1851 | Color fore((fg >> 16) & 0xff, |
| 1852 | (fg >> 8) & 0xff, |
| 1853 | (fg) & 0xff); |
| 1854 | |
| 1855 | Color back((bg >> 16) & 0xff, |
| 1856 | (bg >> 8) & 0xff, |
| 1857 | (bg) & 0xff); |
| 1858 | |
| 1859 | switch (str->type) { |
| 1860 | case strtype_Window: |
| 1861 | if (fg != zcolor_Transparent && fg != zcolor_Cursor) { |
| 1862 | if (fg == zcolor_Default) { |
| 1863 | str->win->attr.fgcolor.reset(); |
| 1864 | gli_override_fg.reset(); |
| 1865 | gli_more_color = gli_more_save; |
| 1866 | gli_caret_color = gli_caret_save; |
| 1867 | gli_link_color = gli_link_save; |
| 1868 | } else if (fg != zcolor_Current) { |
| 1869 | str->win->attr.fgcolor = fore; |
| 1870 | gli_override_fg = fore; |
| 1871 | gli_more_color = fore; |
| 1872 | gli_caret_color = fore; |
| 1873 | gli_link_color = fore; |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | if (bg != zcolor_Transparent && bg != zcolor_Cursor) { |
| 1878 | if (bg == zcolor_Default) { |
| 1879 | str->win->attr.bgcolor.reset(); |
| 1880 | gli_override_bg.reset(); |
| 1881 | gli_window_color = gli_window_save; |
| 1882 | gli_border_color = gli_border_save; |
| 1883 | } else if (bg != zcolor_Current) { |
| 1884 | str->win->attr.bgcolor = back; |
| 1885 | gli_override_bg = back; |
| 1886 | gli_window_color = back; |
| 1887 | gli_border_color = back; |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | gli_override_reverse = fg != zcolor_Default || bg != zcolor_Default; |
| 1892 | |
| 1893 | if (str->win->echostr != nullptr) { |
| 1894 | gli_set_zcolors(str->win->echostr, fg, bg); |
| 1895 | } |
| 1896 | break; |
| 1897 | } |
| 1898 | |
| 1899 | gli_force_redraw = true; |
| 1900 | } |
| 1901 | |
| 1902 | void garglk_set_zcolors(glui32 fg, glui32 bg) |
| 1903 | { |
| 1904 | gli_set_zcolors(gli_currentstr, fg, bg); |
| 1905 | } |
| 1906 | |
| 1907 | void garglk_set_zcolors_stream(stream_t *str, glui32 fg, glui32 bg) |
| 1908 | { |
| 1909 | if (str == nullptr) { |
| 1910 | gli_strict_warning("set_style_stream: invalid ref"); |
| 1911 | return; |
| 1912 | } |
| 1913 | gli_set_zcolors(str, fg, bg); |
| 1914 | } |
| 1915 | |
| 1916 | static void gli_set_reversevideo(stream_t *str, glui32 reverse) |
| 1917 | { |
| 1918 | if (str == nullptr || !str->writable) { |
| 1919 | return; |
| 1920 | } |
| 1921 | |
| 1922 | if (!gli_conf_stylehint) { |
| 1923 | return; |
| 1924 | } |
| 1925 | |
| 1926 | switch (str->type) { |
| 1927 | case strtype_Window: |
| 1928 | str->win->attr.reverse = (reverse != 0); |
| 1929 | if (str->win->echostr != nullptr) { |
| 1930 | gli_set_reversevideo(str->win->echostr, reverse); |
| 1931 | } |
| 1932 | break; |
| 1933 | } |
| 1934 | gli_force_redraw = true; |
| 1935 | } |
| 1936 | |
| 1937 | void garglk_set_reversevideo(glui32 reverse) |
| 1938 | { |
| 1939 | gli_set_reversevideo(gli_currentstr, reverse); |
| 1940 | } |
| 1941 | |
| 1942 | void garglk_set_reversevideo_stream(stream_t *str, glui32 reverse) |
| 1943 | { |
| 1944 | if (str == nullptr) { |
| 1945 | gli_strict_warning("set_style_stream: invalid ref"); |
| 1946 | return; |
| 1947 | } |
| 1948 | gli_set_reversevideo(str, reverse); |
| 1949 | } |
| 1950 | |
| 1951 | static glui32 gli_unput_buffer_uni(stream_t *str, const glui32 *buf, glui32 len) |
| 1952 | { |
| 1953 | glui32 lx; |
| 1954 | const glui32 *cx; |
| 1955 | |
| 1956 | if (str == nullptr || !str->writable) { |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
| 1960 | if (str->win->line_request || str->win->line_request_uni) { |
| 1961 | gli_strict_warning("unput_buffer: window has pending line request"); |
| 1962 | return 0; |
| 1963 | } |
| 1964 | |
| 1965 | if (str->type == strtype_Window) { |
| 1966 | if (str->win->line_request || str->win->line_request_uni) { |
| 1967 | if (gli_conf_safeclicks && gli_forceclick) { |
| 1968 | glk_cancel_line_event(str->win, nullptr); |
| 1969 | gli_forceclick = false; |
| 1970 | } else { |
| 1971 | gli_strict_warning("unput_buffer: window has pending line request"); |
| 1972 | return 0; |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | for (lx = 0, cx = buf + len - 1; lx < len; lx++, cx--) { |
| 1977 | if (!gli_window_unput_char_uni(str->win, *cx)) { |
| 1978 | break; |
| 1979 | } |
| 1980 | str->writecount--; |
| 1981 | } |
| 1982 | |
| 1983 | if (str->win->echostr != nullptr) { |
| 1984 | gli_unput_buffer_uni(str->win->echostr, buf, len); |
| 1985 | } |
| 1986 | |
| 1987 | return lx; |
| 1988 | } |
| 1989 | |
| 1990 | return 0; |
| 1991 | } |
| 1992 | |
| 1993 | static glui32 gli_unput_buffer(stream_t *str, const char *buf, glui32 len) |
| 1994 | { |
| 1995 | std::vector<glui32> unicode; |
| 1996 | try { |
| 1997 | unicode.reserve(len); |
| 1998 | } catch (const std::bad_alloc &) { |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
| 2002 | for (glui32 i = 0; i < len; i++) { |
| 2003 | unicode.push_back(static_cast<unsigned char>(buf[i])); |
| 2004 | } |
| 2005 | |
| 2006 | glui32 n = gli_unput_buffer_uni(str, unicode.data(), len); |
| 2007 | |
| 2008 | return n; |
| 2009 | } |
| 2010 | |
| 2011 | void garglk_unput_string(const char *s) |
| 2012 | { |
| 2013 | gli_unput_buffer(gli_currentstr, s, strlen(s)); |
| 2014 | } |
| 2015 | |
| 2016 | void garglk_unput_string_uni(const glui32 *s) |
| 2017 | { |
| 2018 | gli_unput_buffer_uni(gli_currentstr, s, gli_strlen_uni(s)); |
| 2019 | } |
| 2020 | |
| 2021 | glui32 garglk_unput_string_count(const char *s) |
| 2022 | { |
| 2023 | return gli_unput_buffer(gli_currentstr, s, strlen(s)); |
| 2024 | } |
| 2025 | |
| 2026 | glui32 garglk_unput_string_count_uni(const glui32 *s) |
| 2027 | { |
| 2028 | return gli_unput_buffer_uni(gli_currentstr, s, gli_strlen_uni(s)); |
| 2029 | } |
| 2030 | #endif |