Debian Codegraph @

terps/tads/tads3/tcsrc.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/tads3/TCSRC.CPP,v 1.3 1999/07/11 00:46:55 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 tcsrc.cpp - source file reader
15 Function
16
17 Notes
18
19 Modified
20 04/13/99 MJRoberts - Creation
21 */
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "os.h"
27 #include "t3std.h"
28 #include "tcsrc.h"
29 #include "tcglob.h"
30 #include "charmap.h"
31 #include "vmdatasrc.h"
32
33
34 /* ------------------------------------------------------------------------ */
35 /*
36 * Source files
37 */
38
39 /*
40 * Construction
41 */
42 CTcSrcFile::CTcSrcFile(osfildef *fp, class CCharmapToUni *mapper)
43 {
44 /* remember my source file */
45 fp_ = new CVmFileSource(fp);
46
47 /* net yet at end of file */
48 at_eof_ = FALSE;
49
50 /* there's no data in the buffer yet */
51 rem_ = 0;
52
53 /* remember my character mapper */
54 mapper_ = mapper;
55 }
56
57 /*
58 * Deletion
59 */
60 CTcSrcFile::~CTcSrcFile()
61 {
62 /* close my source file */
63 if (fp_ != 0)
64 delete fp_;
65
66 /* release my character mapper */
67 if (mapper_ != 0)
68 mapper_->release_ref();
69 }
70
71
72 #if 0
73 // we don't currently need this, but keep the source in case it
74 // becomes interesting later
75 //
76 /* ------------------------------------------------------------------------ */
77 /*
78 * Open a plain ASCII file, with no #charset marker.
79 */
80 CTcSrcFile *CTcSrcFile::open_plain(const char *filename)
81 {
82 osfildef *fp;
83 char buf[5];
84 size_t siz;
85
86 /*
87 * open the file in binary mode, since we do all of the newline
88 * interpretation explicitly
89 */
90 if ((fp = osfoprb(filename, OSFTTEXT)) == 0)
91 return 0;
92
93 /* read the first few bytes of the file */
94 siz = osfrbc(fp, buf, sizeof(buf));
95
96 /* check for a 3-byte UTF-8 marker */
97 if (siz >= 3
98 && (uchar)buf[0] == 0xEF
99 && (uchar)buf[1] == 0xBB
100 && (uchar)buf[2] == 0xBF)
101 {
102 /*
103 * seek to the byte after the marker, so that our caller won't see
104 * the marker
105 */
106 osfseek(fp, 3, OSFSK_SET);
107
108 /* return a source file reader with a utf-8 mapper */
109 return new CTcSrcFile(fp, new CCharmapToUniUTF8());
110 }
111
112 /* if we read at least two bytes, try auto-detecting UCS-2 */
113 if (siz >= 2)
114 {
115 /* if the first bytes are 0xFF 0xFE, it's UCS-2 low-byte first */
116 if ((unsigned char)buf[0] == 0xFF && (unsigned char)buf[1] == 0xFE)
117 {
118 /* seek to the byte after the marker */
119 osfseek(fp, 2, OSFSK_SET);
120
121 /* return a reader with a little-endian mapper */
122 return new CTcSrcFile(fp, new CCharmapToUniUcs2Little());
123 }
124
125 /* if the first bytes are 0xFE 0xFF, it's UCS-2 high-byte first */
126 if ((unsigned char)buf[0] == 0xFE && (unsigned char)buf[1] == 0xFF)
127 {
128 /* seek to the byte after the marker */
129 osfseek(fp, 2, OSFSK_SET);
130
131 /* return a reader with a little-endian mapper */
132 return new CTcSrcFile(fp, new CCharmapToUniUcs2Big());
133 }
134 }
135
136 /*
137 * there are no Unicode markers, so our only remaining option is plain
138 * ASCII - return a source file object with a plain ASCII mapper
139 */
140 return new CTcSrcFile(fp, new CCharmapToUniASCII());
141 }
142 #endif
143
144 /* ------------------------------------------------------------------------ */
145 /*
146 * Open a plain ASCII source file.
147 */
148 CTcSrcFile *CTcSrcFile::open_ascii(const char *filename)
149 {
150 osfildef *fp;
151
152 /*
153 * open the file in binary mode, since we do all of the newline
154 * interpretation explicitly
155 */
156 if ((fp = osfoprb(filename, OSFTTEXT)) == 0)
157 return 0;
158
159 /* return a source reader with a plain ASCII mapper */
160 return new CTcSrcFile(fp, new CCharmapToUniASCII());
161 }
162
163
164 /* ------------------------------------------------------------------------ */
165 /*
166 * Open a source file
167 */
168 CTcSrcFile *CTcSrcFile::open_source(const char *filename,
169 class CResLoader *res_loader,
170 const char *default_charset,
171 int *charset_error,
172 int *default_charset_error)
173 {
174 char buf[275];
175 size_t siz;
176 osfildef *fp;
177 long startofs;
178 CCharmapToUni *mapper;
179
180 /* presume we won't find an invalid #charset directive */
181 *charset_error = FALSE;
182
183 /* presume we'll have no problem with the default character set */
184 *default_charset_error = FALSE;
185
186 /*
187 * open the file in binary mode, so that we can scan the first few
188 * bytes to see if we can detect the character set from information
189 * at the beginning of the file
190 */
191 fp = osfoprb(filename, OSFTTEXT);
192
193 /* if we couldn't open the file, return failure */
194 if (fp == 0)
195 return 0;
196
197 /* note the starting offset in the file */
198 startofs = osfpos(fp);
199
200 /* read the first few bytes of the file */
201 siz = osfrbc(fp, buf, sizeof(buf));
202
203 /* check for a 3-byte UTF-8 byte-order marker */
204 if (siz >= 3 && (uchar)buf[0] == 0xEF && (uchar)buf[1] == 0xBB
205 && (uchar)buf[2] == 0xBF)
206 {
207 char *p;
208 size_t rem;
209 uint skip;
210
211 /* skip at least the three-byte marker sequence */
212 skip = 3;
213
214 /*
215 * check for a #charset marker for utf-8 - this would be redundant,
216 * but we'll allow it
217 */
218 p = buf + 3;
219 rem = siz - 3;
220 if (rem > 9 && memcmp(p, "#charset ", 9) == 0)
221 {
222 /* skip spaces */
223 for (p += 9, rem -= 9 ; rem != 0 && (*p == ' ' || *p == '\t') ;
224 ++p, --rem);
225
226 /* check for valid character set markers */
227 if (rem >= 7 && memicmp(p, "\"utf-8\"", 7) == 0)
228 {
229 /* skip the whole sequence */
230 skip = (p + 7) - buf;
231 }
232 else if (rem >= 6 && memicmp(p, "\"utf8\"", 6) == 0)
233 {
234 /* skip the whole sequence */
235 skip = (p + 6) - buf;
236 }
237 }
238
239 /* seek past the character set markers */
240 osfseek(fp, startofs + skip, OSFSK_SET);
241
242 /* return a new utf-8 decoder */
243 return new CTcSrcFile(fp, new CCharmapToUniUTF8());
244 }
245
246 /* if we read at least two bytes, try auto-detecting unicode */
247 if (siz >= 2)
248 {
249 CTcSrcFile *srcf;
250 const char *const *cs_names;
251 int bige;
252
253 /* presume we won't find a byte-order marker */
254 srcf = 0;
255
256 /* if the first bytes are 0xFF 0xFE, it's UCS-2 low-byte first */
257 if ((unsigned char)buf[0] == 0xFF && (unsigned char)buf[1] == 0xFE)
258 {
259 static const char *names[] = { "unicodel", "utf-16le", 0 };
260
261 /* create a UCS-2 little-endian reader */
262 srcf = new CTcSrcFile(fp, new CCharmapToUniUcs2Little());
263 bige = FALSE;
264 cs_names = names;
265 }
266
267 /* if the first bytes are 0xFE 0xFF, it's UCS-2 high-byte first */
268 if ((unsigned char)buf[0] == 0xFE && (unsigned char)buf[1] == 0xFF)
269 {
270 static const char *names[] = { "unicodeb", "utf-16be", 0 };
271
272 /* create a UCS-2 little-endian reader */
273 srcf = new CTcSrcFile(fp, new CCharmapToUniUcs2Big());
274 bige = TRUE;
275 cs_names = names;
276 }
277
278 /* if we found the byte-order marker, we know the character set */
279 if (srcf != 0)
280 {
281 uint skip;
282
283 /* we at least want to skip the byte-order marker */
284 skip = 2;
285
286 /* check to see if we have a '#charset' directive */
287 if (ucs_str_starts_with(buf + 2, siz - 2, "#charset ",
288 bige, FALSE))
289 {
290 char *p;
291 size_t rem;
292
293 /* scan past following spaces */
294 for (p = buf + 2 + 18, rem = siz - 2 - 18 ;
295 rem >= 2 && (ucs_char_eq(p, ' ', bige, FALSE)
296 || ucs_char_eq(p, '\t', bige, FALSE)) ;
297 p += 2, rem -= 2) ;
298
299 /* check for a '"' */
300 if (rem >= 2 && ucs_char_eq(p, '"', bige, FALSE))
301 {
302 const char *const *n;
303
304 /* skip the '"' */
305 p += 2;
306 rem -= 2;
307
308 /*
309 * check for a match to any of the valid names for this
310 * character set
311 */
312 for (n = cs_names ; *n != 0 ; ++n)
313 {
314 /* if it's a match, stop scanning */
315 if (ucs_str_starts_with(p, rem, *n, bige, TRUE))
316 {
317 size_t l;
318
319 /* get the length of the name */
320 l = strlen(*n) * 2;
321
322 /* check for a close quote */
323 if (rem >= l + 2
324 && ucs_char_eq(p + l, '"', bige, FALSE))
325 {
326 /* skip the name and the quote */
327 p += l + 2;
328 rem -= l + 2;
329
330 /* skip the source text to this point */
331 skip = p - buf;
332
333 /* stop scanning */
334 break;
335 }
336 }
337 }
338 }
339 }
340
341 /* seek just past the character set indicators */
342 osfseek(fp, startofs + skip, OSFSK_SET);
343
344 /* return the file */
345 return srcf;
346 }
347 }
348
349 /*
350 * It doesn't appear to use UCS-2 encoding (at least, the file
351 * doesn't start with a byte-order sensing sequence). Check to see
352 * if the file starts with "#charset " in ASCII single-byte
353 * characters.
354 */
355 if (siz >= 9 && memcmp(buf, "#charset ", 9) == 0)
356 {
357 char *p;
358 size_t rem;
359
360 /* skip the #charset string and any following spaces */
361 for (p = buf + 9, rem = siz - 9 ;
362 rem > 0 && (*p == ' ' || *p == '\t') ; ++p, --rem) ;
363
364 /* make sure we're looking at a '"' */
365 if (rem != 0 && *p == '"')
366 {
367 char *charset_name;
368
369 /* skip the open quote */
370 ++p;
371 --rem;
372
373 /* remember where the character set name starts */
374 charset_name = p;
375
376 /*
377 * find the closing quote, which must occur before a CR or
378 * LF character
379 */
380 for ( ; rem > 0 && *p != '"' && *p != 10 && *p != 13 ;
381 ++p, --rem) ;
382
383 /* make sure we found a matching quote */
384 if (rem != 0 && *p == '"')
385 {
386 /* seek just past the #charset string */
387 osfseek(fp, startofs + (p - buf) + 1, OSFSK_SET);
388
389 /*
390 * put a null terminator at the end of the character set
391 * name
392 */
393 *p = '\0';
394
395 /* create a mapper */
396 mapper = CCharmapToUni::load(res_loader, charset_name);
397
398 /*
399 * if that succeeded, return a reader for the mapper;
400 * otherwise, simply proceed as though no #charset had
401 * been present, so that we create a default mapper
402 */
403 if (mapper != 0)
404 {
405 /* success - return a reader */
406 return new CTcSrcFile(fp, mapper);
407 }
408 else
409 {
410 /* tell the caller the #charset was invalid */
411 *charset_error = TRUE;
412 }
413 }
414 }
415 }
416
417 /*
418 * we didn't find any sensing codes, so seek back to the start of
419 * the file
420 */
421 osfseek(fp, startofs, OSFSK_SET);
422
423 /*
424 * We couldn't identify the file's character set based on anything
425 * in the file, so create a mapper for the given default character
426 * set. If there's not even a default character set defined, create
427 * a plain ASCII mapper.
428 */
429 if (default_charset != 0)
430 mapper = CCharmapToUni::load(res_loader, default_charset);
431 else
432 mapper = new CCharmapToUniASCII();
433
434 /* check to see if we created a mapper */
435 if (mapper != 0)
436 {
437 /* return a source file reader based on the mapper */
438 return new CTcSrcFile(fp, mapper);
439 }
440 else
441 {
442 /*
443 * we failed to create a mapper for the default character set -
444 * flag the problem
445 */
446 *default_charset_error = TRUE;
447
448 /* close the input file */
449 osfcls(fp);
450
451 /* return failure */
452 return 0;
453 }
454 }
455
456 /* ------------------------------------------------------------------------ */
457 /*
458 * Read a line of text from the file.
459 */
460 size_t CTcSrcFile::read_line(char *buf, size_t bufl)
461 {
462 char *dst;
463
464 /* start out writing to the start of the caller's buffer */
465 dst = buf;
466
467 /*
468 * Keep going until we run out of input file, fill up the buffer, or
469 * reach the end of a line
470 */
471 for (;;)
472 {
473 char *src;
474
475 /* read some more data if our buffer is empty */
476 if (rem_ == 0)
477 {
478 /* load another buffer-full */
479 rem_ = mapper_->read_file(fp_, buf_, sizeof(buf_));
480
481 /*
482 * If we didn't read anything, we've reached the end of the
483 * file. If we've already copied anything into the caller's
484 * buffer, null-terminate their buffer and return success;
485 * otherwise, return failure, since the caller has already
486 * read everything available from the file.
487 */
488 if (rem_ == 0)
489 {
490 /*
491 * Remember that we've reached the end of the file.
492 * We're about to return the last of the data, so the
493 * caller will not need to call us again (although it's
494 * legal if they do - we'll just return a zero length on
495 * the next call).
496 */
497 at_eof_ = TRUE;
498
499 /* check if we've copied anything to the caller's buffer */
500 if (buf == dst)
501 {
502 /* the caller's buffer is empty - return end of file */
503 return 0;
504 }
505 else
506 {
507 /* null-terminate the caller's buffer */
508 *dst++ = '\0';
509
510 /*
511 * return the number of bytes copied, including the null
512 * terminator
513 */
514 return (dst - buf);
515 }
516 }
517
518 /* start over at the beginning of the buffer */
519 p_ = buf_;
520 }
521
522 /*
523 * Scan the input buffer one character (not byte) at a time.
524 * Keep track of how much many bytes we've skipped. Stop when
525 * we reach a CR or LF character, or when skipping another
526 * character would exceed the remaining capacity of the caller's
527 * buffer, or when we run out of data in our input buffer.
528 */
529 for (src = p_ ; rem_ > 0 ; )
530 {
531 size_t csiz;
532
533 /* get the length of the current character */
534 csiz = utf8_ptr::s_charsize(*src);
535
536 /*
537 * if this character plus a null terminator wouldn't fit in
538 * the output buffer, stop scanning
539 */
540 if (csiz >= bufl)
541 {
542 /*
543 * There's no more room in the caller's buffer. Copy
544 * what we've scanned so far to the output buffer and
545 * null-terminate the buffer.
546 */
547 memcpy(dst, p_, src - p_);
548
549 /* advance past the copied bytes and write the null byte */
550 dst += (src - p_);
551 *dst++ = '\0';
552
553 /* advance the buffer read pointer over the copied bytes */
554 p_ = src;
555
556 /* return success - indicate the number of bytes copied */
557 return (dst - buf);
558 }
559
560 /*
561 * If it's a newline character of some kind, we're done with
562 * this line. Note that we can just check the byte directly,
563 * since if it's a multi-byte character, we'll never mistake
564 * the first byte for a single-byte newline or carriage return
565 * character, since a UTF-8 lead byte always has the high bit
566 * set.
567 *
568 * Also treat the Unicode character 0x2028 (line separator) as
569 * a newline.
570 */
571 if (*src == '\n' || *src == '\r'
572 || utf8_ptr::s_getch(src) == 0x2028)
573 {
574 char nl;
575
576 /* copy what we've scanned so far to the caller's buffer */
577 memcpy(dst, p_, src - p_);
578
579 /* advance past the copied bytes */
580 dst += src - p_;
581
582 /*
583 * add a newline to the caller's buffer -- always add a
584 * '\n' newline, regardless of what kind of newline
585 * sequence we found in the input; also add a null
586 * terminator
587 */
588 *dst++ = '\n';
589 *dst++ = '\0';
590
591 /* remember which type of newline we found */
592 nl = *src;
593
594 /* advance past the newline */
595 p_ = src + csiz;
596 rem_ -= csiz;
597
598 /*
599 * If the input buffer is empty, read more, so that we
600 * can check the next character after the newline
601 * character.
602 */
603 if (rem_ == 0)
604 {
605 /* read more data */
606 rem_ = mapper_->read_file(fp_, buf_, sizeof(buf_));
607
608 /* start over at the start of the buffer */
609 p_ = buf_;
610 }
611
612 /*
613 * Check for a paired newline character. If we found a
614 * CR, check for an LF; if we found an LF, check for a
615 * CR. This will ensure that we will recognize
616 * essentially any newline character sequence for any
617 * platform - this will accept CR, LF, CR-LF, or LF-CR
618 * sequences.
619 */
620 if (rem_ != 0
621 && ((nl == '\n' && *p_ == '\r')
622 || (nl == '\r' && *p_ == '\n')))
623 {
624 /* it's a paired newline - skip the second character */
625 ++p_;
626 --rem_;
627 }
628
629 /* we've finished this line - return success */
630 return dst - buf;
631 }
632
633 /* skip this character in the input and proceed */
634 src += csiz;
635 rem_ -= csiz;
636
637 /* consider this character consumed in the caller's buffer */
638 bufl -= csiz;
639 }
640
641 /*
642 * We've exhausted the current input buffer, without filling the
643 * caller's buffer. Copy what we've skipped so far into the
644 * caller's buffer.
645 */
646 memcpy(dst, p_, src - p_);
647
648 /*
649 * Advance the output pointer past the data we just copied, then
650 * continue looping to read more data from the input file.
651 */
652 dst += src - p_;
653 }
654 }
655
656 /* ------------------------------------------------------------------------ */
657 /*
658 * Buffer reader source object
659 */
660
661 /*
662 * allocate
663 */
664 CTcSrcMemory::CTcSrcMemory(const char *buf, size_t len, CCharmapToUni *mapper)
665 {
666 /*
667 * Allocate a buffer for a UTF8-encoded copy of the buffer -
668 * allocate three bytes per byte of the original, since this is the
669 * worst case for expansion of the encoding. Allocate one extra
670 * byte to ensure we have space for a null terminator.
671 */
672 size_t alo_len = len*3;
673 buf_alo_ = (char *)t3malloc(alo_len + 1);
674
675 /* map the buffer */
676 char *p = buf_alo_;
677 mapper->map(&p, &alo_len, buf, len);
678
679 /* null-terminate the translated buffer */
680 *p = '\0';
681
682 /* start reading at the start of the translated buffer */
683 buf_ = buf_alo_;
684 }
685
686 /*
687 * delete
688 */
689 CTcSrcMemory::~CTcSrcMemory()
690 {
691 /* free our buffer */
692 t3free(buf_alo_);
693 }
694
695 /*
696 * read next line
697 */
698 size_t CTcSrcMemory::read_line(char *buf, size_t bufl)
699 {
700 char *dst;
701 const char *src;
702
703 /* if there's nothing left in our buffer, return EOF */
704 if (*buf_ == '\0')
705 return 0;
706
707 /* start out writing to the start of the caller's buffer */
708 dst = buf;
709
710 /*
711 * Scan the input buffer one character (not byte) at a time. Keep
712 * track of how much many bytes we've skipped. Stop when we reach a
713 * CR or LF character, or when skipping another character would
714 * exceed the remaining capacity of the caller's buffer, or when we
715 * run out of data in our input buffer.
716 */
717 for (src = buf_ ; *src != '\0' ; )
718 {
719 size_t csiz;
720
721 /* get the length of the current character */
722 csiz = utf8_ptr::s_charsize(*src);
723
724 /*
725 * if this character plus a null terminator wouldn't fit in the
726 * output buffer, stop scanning
727 */
728 if (csiz >= bufl)
729 {
730 /*
731 * There's no more room in the caller's buffer. Copy what
732 * we've scanned so far to the output buffer and
733 * null-terminate the buffer.
734 */
735 memcpy(dst, buf_, src - buf_);
736
737 /* advance past the copied bytes and write the null byte */
738 dst += (src - buf_);
739 *dst++ = '\0';
740
741 /* advance the buffer read pointer over the copied bytes */
742 buf_ = src;
743
744 /* return success - indicate the number of bytes copied */
745 return (dst - buf);
746 }
747
748 /*
749 * If it's a newline character of some kind, we're done with this
750 * line. Note that we can just check the byte directly, since if
751 * it's a multi-byte character, we'll never mistake the first byte
752 * for a single-byte newline or carriage return character, since a
753 * UTF-8 lead byte always has the high bit set. Allow Unicode
754 * character 0x2028 (line separator) as a newline as well.
755 */
756 if (*src == '\n' || *src == '\r' || utf8_ptr::s_getch(src) == 0x2028)
757 {
758 char nl;
759
760 /* copy what we've scanned so far to the caller's buffer */
761 memcpy(dst, buf_, src - buf_);
762
763 /* advance past the copied bytes */
764 dst += src - buf_;
765
766 /*
767 * add a newline to the caller's buffer -- always add a '\n'
768 * newline, regardless of what kind of newline sequence we
769 * found in the input; also add a null terminator
770 */
771 *dst++ = '\n';
772 *dst++ = '\0';
773
774 /* remember which type of newline we found */
775 nl = *src;
776
777 /* advance past the newline */
778 buf_ = src + csiz;
779
780 /*
781 * Check for a paired newline character. If we found a CR,
782 * check for an LF; if we found an LF, check for a CR. This
783 * will ensure that we will recognize essentially any
784 * newline character sequence for any platform - this will
785 * accept CR, LF, CR-LF, or LF-CR sequences.
786 */
787 if ((nl == '\n' && *buf_ == '\r')
788 || (nl == '\r' && *buf_ == '\n'))
789 {
790 /* it's a paired newline - skip the second character */
791 ++buf_;
792 }
793
794 /* we've finished this line - return its length */
795 return dst - buf;
796 }
797
798 /* skip this character in the input and proceed */
799 src += csiz;
800
801 /* consider this space consumed in the caller's buffer */
802 bufl -= csiz;
803 }
804
805 /*
806 * We've exhausted the input buffer, without filling the caller's
807 * buffer. Copy what we've skipped so far into the caller's buffer.
808 */
809 memcpy(dst, buf_, src - buf_);
810 dst += src - buf_;
811
812 /* null-terminate the result buffer */
813 *dst++ = '\0';
814
815 /* advance our input pointer to the new (EOF) position */
816 buf_ = src;
817
818 /* return the buffer length */
819 return dst - buf;
820 }
821