/
text.c
2314 lines (2016 loc) · 83.3 KB
/
text.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ------------------------------------------------------------------------- */
/* "text" : Text translation, the abbreviations optimiser, the dictionary */
/* */
/* Part of Inform 6.34 */
/* copyright (c) Graham Nelson 1993 - 2017 */
/* */
/* ------------------------------------------------------------------------- */
#include "header.h"
uchar *low_strings, *low_strings_top; /* Start and next free byte in the low
strings pool */
int32 static_strings_extent; /* Number of bytes of static strings
made so far */
memory_block static_strings_area; /* Used if (!temporary_files_switch) to
hold the static strings area so far */
static uchar *strings_holding_area; /* Area holding translated strings
until they are moved into either
a temporary file, or the
static_strings_area below */
char *all_text, *all_text_top; /* Start and next byte free in (large)
text buffer holding the entire text
of the game, when it is being
recorded */
int put_strings_in_low_memory, /* When TRUE, put static strings in
the low strings pool at 0x100 rather
than in the static strings area */
is_abbreviation, /* When TRUE, the string being trans
is itself an abbreviation string
so can't make use of abbreviations */
abbrevs_lookup_table_made, /* The abbreviations lookup table is
constructed when the first non-
abbreviation string is translated:
this flag is TRUE after that */
abbrevs_lookup[256]; /* Once this has been constructed,
abbrevs_lookup[n] = the smallest
number of any abbreviation beginning
with ASCII character n, or -1
if none of the abbreviations do */
int no_abbreviations; /* No of abbreviations defined so far */
uchar *abbreviations_at; /* Memory to hold the text of any
abbreviation strings declared */
/* ------------------------------------------------------------------------- */
/* Glulx string compression storage */
/* ------------------------------------------------------------------------- */
int no_strings; /* No of strings in static strings
area. */
int no_dynamic_strings; /* No. of @.. string escapes used
(actually, the highest value used
plus one) */
int no_unicode_chars; /* Number of distinct Unicode chars
used. (Beyond 0xFF.) */
static int MAX_CHARACTER_SET; /* Number of possible entities */
huffentity_t *huff_entities; /* The list of entities (characters,
abbreviations, @.. escapes, and
the terminator) */
static huffentity_t **hufflist; /* Copy of the list, for sorting */
int no_huff_entities; /* The number of entities in the list */
int huff_unicode_start; /* Position in the list where Unicode
chars begin. */
int huff_abbrev_start; /* Position in the list where string
abbreviations begin. */
int huff_dynam_start; /* Position in the list where @..
entities begin. */
int huff_entity_root; /* The position in the list of the root
entry (when considering the table
as a tree). */
int done_compression; /* Has the game text been compressed? */
int32 compression_table_size; /* Length of the Huffman table, in
bytes */
int32 compression_string_size; /* Length of the compressed string
data, in bytes */
int32 *compressed_offsets; /* The beginning of every string in
the game, relative to the beginning
of the Huffman table. (So entry 0
is equal to compression_table_size)*/
#define UNICODE_HASH_BUCKETS (64)
unicode_usage_t *unicode_usage_entries;
static unicode_usage_t *unicode_usage_hash[UNICODE_HASH_BUCKETS];
static int unicode_entity_index(int32 unicode);
/* ------------------------------------------------------------------------- */
/* Abbreviation arrays */
/* ------------------------------------------------------------------------- */
int *abbrev_values;
int *abbrev_quality;
int *abbrev_freqs;
/* ------------------------------------------------------------------------- */
int32 total_chars_trans, /* Number of ASCII chars of text in */
total_bytes_trans, /* Number of bytes of Z-code text out */
zchars_trans_in_last_string; /* Number of Z-chars in last string:
needed only for abbrev efficiency
calculation in "directs.c" */
static int32 total_zchars_trans, /* Number of Z-chars of text out
(only used to calculate the above) */
no_chars_transcribed; /* Number of ASCII chars written to
the text transcription area (used
for the -r and -u switches) */
static int zchars_out_buffer[3], /* During text translation, a buffer of
3 Z-chars at a time: when it's full
these are written as a 2-byte word */
zob_index; /* Index (0 to 2) into it */
static unsigned char *text_out_pc; /* The "program counter" during text
translation: the next address to
write Z-coded text output to */
static unsigned char *text_out_limit; /* The upper limit of text_out_pc
during text translation */
static int text_out_overflow; /* During text translation, becomes
true if text_out_pc tries to pass
text_out_limit */
/* ------------------------------------------------------------------------- */
/* For variables/arrays used by the dictionary manager, see below */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* Prepare the abbreviations lookup table (used to speed up abbreviation */
/* detection in text translation). We first bubble-sort the abbrevs into */
/* alphabetical order (this is necessary for the detection algorithm to */
/* to work). Since the table is only prepared once, and for a table */
/* of size at most 96, there's no point using an efficient sort algorithm. */
/* ------------------------------------------------------------------------- */
static void make_abbrevs_lookup(void)
{ int bubble_sort, j, k, l; char p[MAX_ABBREV_LENGTH]; char *p1, *p2;
do
{ bubble_sort = FALSE;
for (j=0; j<no_abbreviations; j++)
for (k=j+1; k<no_abbreviations; k++)
{ p1=(char *)abbreviations_at+j*MAX_ABBREV_LENGTH;
p2=(char *)abbreviations_at+k*MAX_ABBREV_LENGTH;
if (strcmp(p1,p2)<0)
{ strcpy(p,p1); strcpy(p1,p2); strcpy(p2,p);
l=abbrev_values[j]; abbrev_values[j]=abbrev_values[k];
abbrev_values[k]=l;
l=abbrev_quality[j]; abbrev_quality[j]=abbrev_quality[k];
abbrev_quality[k]=l;
bubble_sort = TRUE;
}
}
} while (bubble_sort);
for (j=no_abbreviations-1; j>=0; j--)
{ p1=(char *)abbreviations_at+j*MAX_ABBREV_LENGTH;
abbrevs_lookup[(uchar)p1[0]]=j;
abbrev_freqs[j]=0;
}
abbrevs_lookup_table_made = TRUE;
}
/* ------------------------------------------------------------------------- */
/* Search the abbreviations lookup table (a routine which must be fast). */
/* The source text to compare is text[i], text[i+1], ... and this routine */
/* is only called if text[i] is indeed the first character of at least one */
/* abbreviation, "from" begin the least index into the abbreviations table */
/* of an abbreviation for which text[i] is the first character. Recall */
/* that the abbrevs table is in alphabetical order. */
/* */
/* The return value is -1 if there is no match. If there is a match, the */
/* text to be abbreviated out is over-written by a string of null chars */
/* with "ASCII" value 1, and the abbreviation number is returned. */
/* */
/* In Glulx, we *do not* do this overwriting with 1's. */
/* ------------------------------------------------------------------------- */
static int try_abbreviations_from(unsigned char *text, int i, int from)
{ int j, k; uchar *p, c;
c=text[i];
for (j=from, p=(uchar *)abbreviations_at+from*MAX_ABBREV_LENGTH;
(j<no_abbreviations)&&(c==p[0]); j++, p+=MAX_ABBREV_LENGTH)
{ if (text[i+1]==p[1])
{ for (k=2; p[k]!=0; k++)
if (text[i+k]!=p[k]) goto NotMatched;
if (!glulx_mode) {
for (k=0; p[k]!=0; k++) text[i+k]=1;
}
abbrev_freqs[j]++;
return(j);
NotMatched: ;
}
}
return(-1);
}
extern void make_abbreviation(char *text)
{
strcpy((char *)abbreviations_at
+ no_abbreviations*MAX_ABBREV_LENGTH, text);
is_abbreviation = TRUE;
abbrev_values[no_abbreviations] = compile_string(text, TRUE, TRUE);
is_abbreviation = FALSE;
/* The quality is the number of Z-chars saved by using this */
/* abbreviation: note that it takes 2 Z-chars to print it. */
abbrev_quality[no_abbreviations++] = zchars_trans_in_last_string - 2;
}
/* ------------------------------------------------------------------------- */
/* The front end routine for text translation */
/* ------------------------------------------------------------------------- */
extern int32 compile_string(char *b, int in_low_memory, int is_abbrev)
{ int i, j; uchar *c;
is_abbreviation = is_abbrev;
/* Put into the low memory pool (at 0x100 in the Z-machine) of strings */
/* which may be wanted as possible entries in the abbreviations table */
if (!glulx_mode && in_low_memory)
{ j=subtract_pointers(low_strings_top,low_strings);
low_strings_top=translate_text(low_strings_top, low_strings+MAX_LOW_STRINGS, b);
if (!low_strings_top)
memoryerror("MAX_LOW_STRINGS", MAX_LOW_STRINGS);
is_abbreviation = FALSE;
return(0x21+(j/2));
}
if (glulx_mode && done_compression)
compiler_error("Tried to add a string after compression was done.");
c = translate_text(strings_holding_area, strings_holding_area+MAX_STATIC_STRINGS, b);
if (!c)
memoryerror("MAX_STATIC_STRINGS",MAX_STATIC_STRINGS);
i = subtract_pointers(c, strings_holding_area);
/* Insert null bytes as needed to ensure that the next static string */
/* also occurs at an address expressible as a packed address */
if (!glulx_mode) {
int textalign;
if (oddeven_packing_switch)
textalign = scale_factor*2;
else
textalign = scale_factor;
while ((i%textalign)!=0)
{
if (i+2 > MAX_STATIC_STRINGS)
memoryerror("MAX_STATIC_STRINGS",MAX_STATIC_STRINGS);
i+=2; *c++ = 0; *c++ = 0;
}
}
j = static_strings_extent;
if (temporary_files_switch)
for (c=strings_holding_area; c<strings_holding_area+i;
c++, static_strings_extent++)
fputc(*c,Temp1_fp);
else
for (c=strings_holding_area; c<strings_holding_area+i;
c++, static_strings_extent++)
write_byte_to_memory_block(&static_strings_area,
static_strings_extent, *c);
is_abbreviation = FALSE;
if (!glulx_mode) {
return(j/scale_factor);
}
else {
/* The marker value is a one-based string number. (We reserve zero
to mean "not a string at all". */
return (++no_strings);
}
}
/* ------------------------------------------------------------------------- */
/* Output a single Z-character into the buffer, and flush it if full */
/* ------------------------------------------------------------------------- */
static void write_z_char_z(int i)
{ uint32 j;
ASSERT_ZCODE();
total_zchars_trans++;
zchars_out_buffer[zob_index++]=(i%32);
if (zob_index!=3) return;
zob_index=0;
j= zchars_out_buffer[0]*0x0400 + zchars_out_buffer[1]*0x0020
+ zchars_out_buffer[2];
if (text_out_pc+2 > text_out_limit) {
text_out_overflow = TRUE;
return;
}
text_out_pc[0] = j/256; text_out_pc[1] = j%256; text_out_pc+=2;
total_bytes_trans+=2;
}
static void write_zscii(int zsc)
{
int lookup_value, in_alphabet;
if (zsc==' ')
{ write_z_char_z(0);
return;
}
if (zsc < 0x100) lookup_value = zscii_to_alphabet_grid[zsc];
else lookup_value = -1;
if (lookup_value >= 0)
{ alphabet_used[lookup_value] = 'Y';
in_alphabet = lookup_value/26;
if (in_alphabet==1) write_z_char_z(4); /* SHIFT to A1 */
if (in_alphabet==2) write_z_char_z(5); /* SHIFT to A2 */
write_z_char_z(lookup_value%26 + 6);
}
else
{ write_z_char_z(5); write_z_char_z(6);
write_z_char_z(zsc/32); write_z_char_z(zsc%32);
}
}
/* ------------------------------------------------------------------------- */
/* Finish a Z-coded string, padding out with Z-char 5s if necessary and */
/* setting the "end" bit on the final 2-byte word */
/* ------------------------------------------------------------------------- */
static void end_z_chars(void)
{ unsigned char *p;
zchars_trans_in_last_string=total_zchars_trans-zchars_trans_in_last_string;
while (zob_index!=0) write_z_char_z(5);
p=(unsigned char *) text_out_pc;
*(p-2)= *(p-2)+128;
}
/* Glulx handles this much more simply -- compression is done elsewhere. */
static void write_z_char_g(int i)
{
ASSERT_GLULX();
if (text_out_pc+1 > text_out_limit) {
text_out_overflow = TRUE;
return;
}
total_zchars_trans++;
text_out_pc[0] = i;
text_out_pc++;
total_bytes_trans++;
}
/* ------------------------------------------------------------------------- */
/* The main routine "text.c" provides to the rest of Inform: the text */
/* translator. p is the address to write output to, s_text the source text */
/* and the return value is the next free address to write output to. */
/* The return value will not exceed p_limit. If the translation tries to */
/* overflow this boundary, the return value will be NULL (and you should */
/* display an error). */
/* Note that the source text may be corrupted by this routine. */
/* ------------------------------------------------------------------------- */
extern uchar *translate_text(uchar *p, uchar *p_limit, char *s_text)
{ int i, j, k, in_alphabet, lookup_value;
int32 unicode; int zscii;
unsigned char *text_in;
/* Cast the input and output streams to unsigned char: text_out_pc will
advance as bytes of Z-coded text are written, but text_in doesn't */
text_in = (unsigned char *) s_text;
text_out_pc = (unsigned char *) p;
text_out_limit = (unsigned char *) p_limit;
text_out_overflow = FALSE;
/* Remember the Z-chars total so that later we can subtract to find the
number of Z-chars translated on this string */
zchars_trans_in_last_string = total_zchars_trans;
/* Start with the Z-characters output buffer empty */
zob_index=0;
/* If this is the first text translated since the abbreviations were
declared, and if some were declared, then it's time to make the
lookup table for abbreviations
(Except: we don't if the text being translated is itself
the text of an abbreviation currently being defined) */
if ((!abbrevs_lookup_table_made) && (no_abbreviations > 0)
&& (!is_abbreviation))
make_abbrevs_lookup();
/* If we're storing the whole game text to memory, then add this text */
if ((!is_abbreviation) && (store_the_text))
{ no_chars_transcribed += strlen(s_text)+2;
if (no_chars_transcribed >= MAX_TRANSCRIPT_SIZE)
memoryerror("MAX_TRANSCRIPT_SIZE", MAX_TRANSCRIPT_SIZE);
sprintf(all_text_top, "%s\n\n", s_text);
all_text_top += strlen(all_text_top);
}
if (transcript_switch && (!veneer_mode))
write_to_transcript_file(s_text);
if (!glulx_mode) {
/* The empty string of Z-text is illegal, since it can't carry an end
bit: so we translate an empty string of ASCII text to just the
pad character 5. Printing this causes nothing to appear on screen. */
if (text_in[0]==0) write_z_char_z(5);
/* Loop through the characters of the null-terminated input text: note
that if 1 is written over a character in the input text, it is
afterwards ignored */
for (i=0; text_in[i]!=0; i++)
{ total_chars_trans++;
/* Contract ". " into ". " if double-space-removing switch set:
likewise "? " and "! " if the setting is high enough */
if ((double_space_setting >= 1)
&& (text_in[i+1]==' ') && (text_in[i+2]==' '))
{ if (text_in[i]=='.') text_in[i+2]=1;
if (double_space_setting >= 2)
{ if (text_in[i]=='?') text_in[i+2]=1;
if (text_in[i]=='!') text_in[i+2]=1;
}
}
/* Try abbreviations if the economy switch set */
if ((economy_switch) && (!is_abbreviation)
&& ((k=abbrevs_lookup[text_in[i]])!=-1))
{ if ((j=try_abbreviations_from(text_in, i, k))!=-1)
{ if (j<32) { write_z_char_z(2); write_z_char_z(j); }
else { write_z_char_z(3); write_z_char_z(j-32); }
}
}
/* If Unicode switch set, use text_to_unicode to perform UTF-8
decoding */
if (character_set_unicode && (text_in[i] & 0x80))
{ unicode = text_to_unicode((char *) (text_in+i));
zscii = unicode_to_zscii(unicode);
if (zscii != 5) write_zscii(zscii);
else
{ unicode_char_error(
"Character can only be used if declared in \
advance as part of 'Zcharacter table':", unicode);
}
i += textual_form_length - 1;
continue;
}
/* '@' is the escape character in Inform string notation: the various
possibilities are:
(printing only)
@@decimalnumber : write this ZSCII char (0 to 1023)
@twodigits : write the abbreviation string with this
decimal number
(any string context)
@accentcode : this accented character: e.g.,
for @'e write an E-acute
@{...} : this Unicode char (in hex) */
if (text_in[i]=='@')
{ if (text_in[i+1]=='@')
{
/* @@... */
i+=2; j=atoi((char *) (text_in+i));
switch(j)
{ /* Prevent ~ and ^ from being translated to double-quote
and new-line, as they ordinarily would be */
case 94: write_z_char_z(5); write_z_char_z(6);
write_z_char_z(94/32); write_z_char_z(94%32);
break;
case 126: write_z_char_z(5); write_z_char_z(6);
write_z_char_z(126/32); write_z_char_z(126%32);
break;
default: write_zscii(j); break;
}
while (isdigit(text_in[i])) i++; i--;
}
else if (isdigit(text_in[i+1])!=0)
{ int d1, d2;
/* @.. */
d1 = character_digit_value[text_in[i+1]];
d2 = character_digit_value[text_in[i+2]];
if ((d1 == 127) || (d1 >= 10) || (d2 == 127) || (d2 >= 10))
error("'@..' must have two decimal digits");
else
{ i+=2;
write_z_char_z(1); write_z_char_z(d1*10 + d2);
}
}
else
{
/* A string escape specifying an unusual character */
unicode = text_to_unicode((char *) (text_in+i));
zscii = unicode_to_zscii(unicode);
if (zscii != 5) write_zscii(zscii);
else
{ unicode_char_error(
"Character can only be used if declared in \
advance as part of 'Zcharacter table':", unicode);
}
i += textual_form_length - 1;
}
}
else
{ /* Skip a character which has been over-written with the null
value 1 earlier on */
if (text_in[i]!=1)
{ if (text_in[i]==' ') write_z_char_z(0);
else
{ j = (int) text_in[i];
lookup_value = iso_to_alphabet_grid[j];
if (lookup_value < 0)
{ /* The character isn't in the standard alphabets, so
we have to use the ZSCII 4-Z-char sequence */
if (lookup_value == -5)
{ /* Character isn't in the ZSCII set at all */
unicode = iso_to_unicode(j);
unicode_char_error(
"Character can only be used if declared in \
advance as part of 'Zcharacter table':", unicode);
write_zscii(0x200 + unicode/0x100);
write_zscii(0x300 + unicode%0x100);
}
else write_zscii(-lookup_value);
}
else
{ /* The character is in one of the standard alphabets:
write a SHIFT to temporarily change alphabet if
it isn't in alphabet 0, then write the Z-char */
alphabet_used[lookup_value] = 'Y';
in_alphabet = lookup_value/26;
if (in_alphabet==1) write_z_char_z(4); /* SHIFT to A1 */
if (in_alphabet==2) write_z_char_z(5); /* SHIFT to A2 */
write_z_char_z(lookup_value%26 + 6);
}
}
}
}
}
/* Flush the Z-characters output buffer and set the "end" bit */
end_z_chars();
}
else {
/* The text storage here is, of course, temporary. Compression
will occur when we're finished compiling, so that all the
clever Huffman stuff will work.
In the stored text, we use "@@" to indicate @,
"@0" to indicate a zero byte,
"@ANNNN" to indicate an abbreviation,
"@DNNNN" to indicate a dynamic string thing.
"@UNNNN" to indicate a four-byte Unicode value (0x100 or higher).
(NNNN is a four-digit hex number using the letters A-P... an
ugly representation but a convenient one.)
*/
for (i=0; text_in[i]!=0; i++) {
/* Contract ". " into ". " if double-space-removing switch set:
likewise "? " and "! " if the setting is high enough. */
if ((double_space_setting >= 1)
&& (text_in[i+1]==' ') && (text_in[i+2]==' ')) {
if (text_in[i]=='.'
|| (double_space_setting >= 2
&& (text_in[i]=='?' || text_in[i]=='!'))) {
text_in[i+1] = text_in[i];
i++;
}
}
total_chars_trans++;
/* Try abbreviations if the economy switch set. We have to be in
compression mode too, since the abbreviation mechanism is part
of string decompression. */
if ((economy_switch) && (compression_switch) && (!is_abbreviation)
&& ((k=abbrevs_lookup[text_in[i]])!=-1)
&& ((j=try_abbreviations_from(text_in, i, k)) != -1)) {
char *cx = (char *)abbreviations_at+j*MAX_ABBREV_LENGTH;
i += (strlen(cx)-1);
write_z_char_g('@');
write_z_char_g('A');
write_z_char_g('A' + ((j >>12) & 0x0F));
write_z_char_g('A' + ((j >> 8) & 0x0F));
write_z_char_g('A' + ((j >> 4) & 0x0F));
write_z_char_g('A' + ((j ) & 0x0F));
}
else if (text_in[i] == '@') {
if (text_in[i+1]=='@') {
/* An ASCII code */
i+=2; j=atoi((char *) (text_in+i));
if (j == '@' || j == '\0') {
write_z_char_g('@');
if (j == 0) {
j = '0';
if (!compression_switch)
warning("Ascii @@0 will prematurely terminate non-compressed \
string.");
}
}
write_z_char_g(j);
while (isdigit(text_in[i])) i++; i--;
}
else if (isdigit(text_in[i+1])) {
int d1, d2;
d1 = character_digit_value[text_in[i+1]];
d2 = character_digit_value[text_in[i+2]];
if ((d1 == 127) || (d1 >= 10) || (d2 == 127) || (d2 >= 10)) {
error("'@..' must have two decimal digits");
}
else {
if (!compression_switch)
warning("'@..' print variable will not work in non-compressed \
string; substituting ' '.");
i += 2;
j = d1*10 + d2;
if (j >= MAX_DYNAMIC_STRINGS) {
memoryerror("MAX_DYNAMIC_STRINGS", MAX_DYNAMIC_STRINGS);
j = 0;
}
if (j+1 >= no_dynamic_strings)
no_dynamic_strings = j+1;
write_z_char_g('@');
write_z_char_g('D');
write_z_char_g('A' + ((j >>12) & 0x0F));
write_z_char_g('A' + ((j >> 8) & 0x0F));
write_z_char_g('A' + ((j >> 4) & 0x0F));
write_z_char_g('A' + ((j ) & 0x0F));
}
}
else {
unicode = text_to_unicode((char *) (text_in+i));
i += textual_form_length - 1;
if (unicode == '@' || unicode == '\0') {
write_z_char_g('@');
write_z_char_g(unicode ? '@' : '0');
}
else if (unicode >= 0 && unicode < 256) {
write_z_char_g(unicode);
}
else {
if (!compression_switch) {
warning("Unicode characters will not work in non-compressed \
string; substituting '?'.");
write_z_char_g('?');
}
else {
j = unicode_entity_index(unicode);
write_z_char_g('@');
write_z_char_g('U');
write_z_char_g('A' + ((j >>12) & 0x0F));
write_z_char_g('A' + ((j >> 8) & 0x0F));
write_z_char_g('A' + ((j >> 4) & 0x0F));
write_z_char_g('A' + ((j ) & 0x0F));
}
}
}
}
else if (text_in[i] == '^')
write_z_char_g(0x0A);
else if (text_in[i] == '~')
write_z_char_g('"');
else if (character_set_unicode) {
if (text_in[i] & 0x80) {
unicode = text_to_unicode((char *) (text_in+i));
i += textual_form_length - 1;
if (unicode >= 0 && unicode < 256) {
write_z_char_g(unicode);
}
else {
if (!compression_switch) {
warning("Unicode characters will not work in non-compressed \
string; substituting '?'.");
write_z_char_g('?');
}
else {
j = unicode_entity_index(unicode);
write_z_char_g('@');
write_z_char_g('U');
write_z_char_g('A' + ((j >>12) & 0x0F));
write_z_char_g('A' + ((j >> 8) & 0x0F));
write_z_char_g('A' + ((j >> 4) & 0x0F));
write_z_char_g('A' + ((j ) & 0x0F));
}
}
}
else {
write_z_char_g(text_in[i]);
}
}
else {
unicode = iso_to_unicode_grid[text_in[i]];
if (unicode >= 0 && unicode < 256) {
write_z_char_g(unicode);
}
else {
if (!compression_switch) {
warning("Unicode characters will not work in non-compressed \
string; substituting '?'.");
write_z_char_g('?');
}
else {
j = unicode_entity_index(unicode);
write_z_char_g('@');
write_z_char_g('U');
write_z_char_g('A' + ((j >>12) & 0x0F));
write_z_char_g('A' + ((j >> 8) & 0x0F));
write_z_char_g('A' + ((j >> 4) & 0x0F));
write_z_char_g('A' + ((j ) & 0x0F));
}
}
}
}
write_z_char_g(0);
}
if (text_out_overflow)
return NULL;
else
return((uchar *) text_out_pc);
}
static int unicode_entity_index(int32 unicode)
{
unicode_usage_t *uptr;
int j;
int buck = unicode % UNICODE_HASH_BUCKETS;
for (uptr = unicode_usage_hash[buck]; uptr; uptr=uptr->next) {
if (uptr->ch == unicode)
break;
}
if (uptr) {
j = (uptr - unicode_usage_entries);
}
else {
if (no_unicode_chars >= MAX_UNICODE_CHARS) {
memoryerror("MAX_UNICODE_CHARS", MAX_UNICODE_CHARS);
j = 0;
}
else {
j = no_unicode_chars;
no_unicode_chars++;
uptr = unicode_usage_entries + j;
uptr->ch = unicode;
uptr->next = unicode_usage_hash[buck];
unicode_usage_hash[buck] = uptr;
}
}
return j;
}
/* ------------------------------------------------------------------------- */
/* Glulx compression code */
/* ------------------------------------------------------------------------- */
static void compress_makebits(int entnum, int depth, int prevbit,
huffbitlist_t *bits);
/* The compressor. This uses the usual Huffman compression algorithm. */
void compress_game_text()
{
int entities=0, branchstart, branches;
int numlive;
int32 lx;
int jx;
int ch;
int32 ix;
huffbitlist_t bits;
if (compression_switch) {
/* How many entities have we currently got? Well, 256 plus the
string-terminator plus Unicode chars plus abbrevations plus
dynamic strings. */
entities = 256+1;
huff_unicode_start = entities;
entities += no_unicode_chars;
huff_abbrev_start = entities;
if (economy_switch)
entities += no_abbreviations;
huff_dynam_start = entities;
entities += no_dynamic_strings;
if (entities > MAX_CHARACTER_SET)
memoryerror("MAX_CHARACTER_SET",MAX_CHARACTER_SET);
/* Characters */
for (jx=0; jx<256; jx++) {
huff_entities[jx].type = 2;
huff_entities[jx].count = 0;
huff_entities[jx].u.ch = jx;
}
/* Terminator */
huff_entities[256].type = 1;
huff_entities[256].count = 0;
for (jx=0; jx<no_unicode_chars; jx++) {
huff_entities[huff_unicode_start+jx].type = 4;
huff_entities[huff_unicode_start+jx].count = 0;
huff_entities[huff_unicode_start+jx].u.val = jx;
}
if (economy_switch) {
for (jx=0; jx<no_abbreviations; jx++) {
huff_entities[huff_abbrev_start+jx].type = 3;
huff_entities[huff_abbrev_start+jx].count = 0;
huff_entities[huff_abbrev_start+jx].u.val = jx;
}
}
for (jx=0; jx<no_dynamic_strings; jx++) {
huff_entities[huff_dynam_start+jx].type = 9;
huff_entities[huff_dynam_start+jx].count = 0;
huff_entities[huff_dynam_start+jx].u.val = jx;
}
}
else {
/* No compression; use defaults that will make it easy to check
for errors. */
no_huff_entities = 257;
huff_unicode_start = 257;
huff_abbrev_start = 257;
huff_dynam_start = 257+MAX_ABBREVS;
compression_table_size = 0;
}
if (temporary_files_switch) {
fclose(Temp1_fp);
Temp1_fp=fopen(Temp1_Name,"rb");
if (Temp1_fp==NULL)
fatalerror("I/O failure: couldn't reopen temporary file 1");
}
if (compression_switch) {
for (lx=0, ix=0; lx<no_strings; lx++) {
int escapelen=0, escapetype=0;
int done=FALSE;
int32 escapeval=0;
while (!done) {
if (temporary_files_switch)
ch = fgetc(Temp1_fp);
else
ch = read_byte_from_memory_block(&static_strings_area, ix);
ix++;
if (ix > static_strings_extent || ch < 0)
compiler_error("Read too much not-yet-compressed text.");
if (escapelen == -1) {
escapelen = 0;
if (ch == '@') {
ch = '@';
}
else if (ch == '0') {
ch = '\0';
}
else if (ch == 'A' || ch == 'D' || ch == 'U') {
escapelen = 4;
escapetype = ch;
escapeval = 0;
continue;
}
else {
compiler_error("Strange @ escape in processed text.");
}
}
else if (escapelen) {
escapeval = (escapeval << 4) | ((ch-'A') & 0x0F);
escapelen--;
if (escapelen == 0) {
if (escapetype == 'A') {
ch = huff_abbrev_start+escapeval;
}
else if (escapetype == 'D') {
ch = huff_dynam_start+escapeval;
}
else if (escapetype == 'U') {
ch = huff_unicode_start+escapeval;
}
else {
compiler_error("Strange @ escape in processed text.");
}
}
else
continue;
}
else {
if (ch == '@') {
escapelen = -1;
continue;
}
if (ch == 0) {
ch = 256;
done = TRUE;
}
}
huff_entities[ch].count++;
}
}
numlive = 0;
for (jx=0; jx<entities; jx++) {
if (huff_entities[jx].count) {
hufflist[numlive] = &(huff_entities[jx]);
numlive++;
}
}
branchstart = entities;
branches = 0;
while (numlive > 1) {
int best1, best2;
int best1num, best2num;
huffentity_t *bran;
if (hufflist[0]->count < hufflist[1]->count) {
best1 = 0;
best2 = 1;
}
else {
best2 = 0;
best1 = 1;
}
best1num = hufflist[best1]->count;
best2num = hufflist[best2]->count;
for (jx=2; jx<numlive; jx++) {
if (hufflist[jx]->count < best1num) {
best2 = best1;
best2num = best1num;
best1 = jx;
best1num = hufflist[best1]->count;
}
else if (hufflist[jx]->count < best2num) {
best2 = jx;
best2num = hufflist[best2]->count;
}
}
bran = &(huff_entities[branchstart+branches]);
branches++;
bran->type = 0;
bran->count = hufflist[best1]->count + hufflist[best2]->count;
bran->u.branch[0] = (hufflist[best1] - huff_entities);
bran->u.branch[1] = (hufflist[best2] - huff_entities);
hufflist[best1] = bran;
if (best2 < numlive-1) {
memmove(&(hufflist[best2]), &(hufflist[best2+1]),
((numlive-1) - best2) * sizeof(huffentity_t *));
}
numlive--;
}
huff_entity_root = (hufflist[0] - huff_entities);
for (ix=0; ix<MAXHUFFBYTES; ix++)
bits.b[ix] = 0;
compression_table_size = 12;
no_huff_entities = 0; /* compress_makebits will total this up */
compress_makebits(huff_entity_root, 0, -1, &bits);
}