-
Notifications
You must be signed in to change notification settings - Fork 8
/
zstring.c
870 lines (758 loc) · 22.7 KB
/
zstring.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
/* zstring.c String operations
1988 Zinn Computer Company, for Infocom
Contained in this file:
addfstr Add string to frequent-string table
zstr Translate ASCII to Z-string
zstrlen Return # of words in a Z-string
zword Translate ASCII to Zword-string
*/
#include <stdio.h>
#include <fcntl.h>
#include "zap.h"
/* Local definitions */
/* External routines */
extern char *CALLOC();
extern char *REALLOC();
extern char *MALLOC();
/* External data */
extern BOOL Permshift;
extern int Max_buf_len;
extern int character_count;
extern long character_counts[];
extern int build_freq;
extern char language_char;
extern unsigned char *language_table;
/* Local routines (and forward references) */
/* Local data */
/* Private data */
static int FreqC = 0; /* Number of frequent strings */
static UBYTE *Zstrbuf = NULL;
static int Zstrbuf_size = 0;
static struct freq_node { /* Frequent string table */
int fs_len; /* Length of string */
char *fs_strP; /* Ptr to string */
int fs_num;
struct freq_node *next;
} *Freqstr[256]; /* We assume this is initialized to 0 */
static struct { /* Character set codes */
UBYTE zc_set; /* Set number */
UBYTE zc_char; /* Character value */
} Zctbl[256]; /* Indexed by character */
/*
*//* addfstr( strP )
Add a string to the list of frequent strings
Accepts :
strP Address of the Z-string
Returns :
<value> status code
Notes :
No check is made to ensure that the string to be
added is not already in the frequent string list. We assume
that the compiler knew what it was doing; anyway, doing so would
probably throw the application's assumptions out of sync.
*/
int
addfstr( strP )
AREG1 char *strP; /* Addr of zstring */
{
DREG1 int len; /* Length of string */
AREG2 struct freq_node *new;
if (build_freq) return(SCOK);
if ( FreqC == MAXFREQSTR )
zerror( E_PASS1, "Too many frequent strings at \"%s\"", strP );
else {
new = (struct freq_node *)MALLOC(sizeof(struct freq_node));
new->next = Freqstr[(unsigned char)(*strP)];
Freqstr[(unsigned char)(*strP)] = new;
len = strlen( strP ); /* Number of bytes */
if ( ( new->fs_strP = CALLOC( 1, len+1 ) ) == NULL )
zerror( E_PASS1, "addfstr: can not allocate string space!" );
else {
strcpy( new->fs_strP, strP );
new->fs_len = len;
new->fs_num = FreqC;
++FreqC;
}
}
return( SCOK );
}
/*
*//* zstr( strP, freqF, reqlen )
Translate ASCII to Z-string
Accepts :
strP Address of the ASCII string
freqF Whether to use frequent-words in the translation
reqlen Number of bytes of required string length. If
non-zero, resulting Z string must contain exactly
this many bytes. It will be truncated if too long,
or padded if too short.
Returns :
<value> The address of the translated string
Notes :
The returned value is a pointer to a static reusable buffer.
*/
UBYTE *translate_char(strP, chrp)
UBYTE *strP;
UBYTE *chrp;
{
UBYTE *tbl = language_table;
UBYTE tch, ch;
ch = strP[1];
if (ch != NUL) {
/* Ignore escape at end of string */
strP++;
if (ch != language_char) {
/* %% turns into % */
while (tch = *tbl++) {
if (tch == ch) {
*chrp = *tbl;
break; }
tbl++; } } }
return(strP); }
UBYTE *
zstr( strP, freqF, reqlen )
AREG1 UBYTE *strP; /* Ptr to input string */
int freqF; /* Whether to use frequent words */
int reqlen; /* Required length, if any */
{
UBYTE ch; /* Character */
UBYTE curcset; /* Currently selected cs */
UBYTE ccode; /* Character code */
UBYTE cset; /* Character set */
AREG2 struct freq_node *fnode; /* Frequent string node */
char *fsP; /* Frequent string ptr */
int fsL; /* Frequent string length */
int matchX; /* Match index */
int matchL; /* Match length */
if (Zstrbuf_size < Max_buf_len) {
Zstrbuf_size = Max_buf_len;
Zstrbuf = (UBYTE *)REALLOC(Zstrbuf,Max_buf_len);
}
startzstr( &Zstrbuf[0], reqlen );
if (freqF && build_freq) {
/* If freqF is not set, it's for a vocabulary word, which doesn't
get included in the frequent words stuff. */
fw_string(strP); }
else {
curcset = 0;
for( ; ( ch = *strP ) != NUL; ++strP ) {
/* Check for frequent word encoding */
if (ch == language_char) {
strP = translate_char(strP, &ch); }
if ( freqF ) {
/* Look through the frequent strings */
matchL = 0; /* No match */
for( fnode = Freqstr[(unsigned char)ch]; fnode != NULL;
fnode = fnode->next ) {
fsP = fnode->fs_strP;
if ( ch == *fsP ) {
fsL = fnode->fs_len;
if ( strncmp( strP, fsP, fsL ) == 0 )
if ( fsL > matchL ) { /* Bigger than last match? */
matchL = fsL; /* Remember match length */
matchX = fnode->fs_num; /* Remember match num */
}
}
}
/* If we had a match, use it... */
if ( matchL > 0 ) {
addzchar( ( matchX/32 ) +1 ); /* String set */
addzchar( matchX % 32 ); /* String number in set */
strP += matchL-1; /* Skip the string */
continue; /* Go process next guy */
}
}
if (character_count)
character_counts[ch]++; /* Maintain count of characters encoded */
/* Encode the character into 5-bit charset */
if ( ch == ' ' )
addzchar( '\0' ); /* Space is always zero */
else if ( ( ccode = Zctbl[ch].zc_char ) == 0 ) {
/* Doesn't map -- use full ASCII sequence */
if ( curcset == 1 ) {
addzchar( '\005' ); /* Shift to set 0 */
curcset = 0;
}
if ( curcset == 0 )
addzchar( '\005' ); /* Temp shift to set 2 */
addzchar( '\006' ); /* Shift to ASCII */
addzchar( ( ch >> 5 ) & 0x1f );
addzchar( ch & 0x1f );
}
else {
cset = Zctbl[ch].zc_set;
/* If need to switch character set, make sure we are in set
zero -- it's the only way we can get to the others. */
if ( ( cset != curcset ) && ( curcset != 0 ) ) {
addzchar( '\004' + (2-curcset) );
curcset = 0;
}
/* See if we still need to change sets */
if ( cset != curcset ) {
/* Note that if we get in here, curcset==0 */
/* Start with temporary shift */
addzchar( '\003' + cset );
/* Make a guess as to whether we should make it permanent */
if ( Permshift && ( ( ch = strP[1] ) != NUL ) &&
( Zctbl[ch].zc_char != 0 ) &&
( Zctbl[ch].zc_set == cset ) ) {
addzchar( '\003' + cset );
curcset = cset;
}
}
/* Put out the character (phew) */
addzchar( ccode );
}
}
endzstr(); /* Finish zstring */ }
return( &Zstrbuf[0] );
}
/*
*//* zstrlen( strP )
Count words in a Zstring
Accepts :
strP Pointer to string in Z{word} format
Returns :
<value> Number of words in the string
*/
int
zstrlen( strP )
AREG1 UBYTE *strP; /* Ptr to string */
{
DREG1 int len; /* Length of string */
for( len = 1; ( *strP & 0x80 ) == 0; ++len, strP += 2 )
;
return( len );
}
/*
*//* zcset( csP )
Identify a character set
Accepts :
csP Pointer to the new character set; an array of 78
characters that make up values 6-31 in each of
the 5-bit encoded character subranges.
Returns :
*/
zcset( csP )
AREG1 UBYTE *csP; /* New character set */
{
DREG1 int i; /* Scratch */
DREG2 int ch; /* Char val */
/* Clear out current character set */
for( i = 0; i < 256; ++i )
Zctbl[i].zc_char = 0; /* Clear it */
/* Process the new set */
for( i = 0; i < 78; ++i ) {
ch = *csP++;
if ( ch != 0 ) { /* Zero means not used */
Zctbl[ch].zc_set = i/26; /* Charset # */
Zctbl[ch].zc_char = (i%26) +6; /* Encoding value */
}
}
}
/*
*//* startzstr, addzchar, endzstr - 5bit string stuff
These routine help construct 5-bit-byte strings
*/
static UBYTE *ZstrP; /* String being made */
static UWORD Zword; /* Element of string */
static int ZwordC; /* Byte count */
static int ZwordX; /* Zword index */
static int Zwordrlen; /* Required length, if any */
/* Start making a string */
startzstr( strP, reqlen )
UBYTE *strP; /* Where to put the string */
int reqlen; /* Required length, if any */
{
ZstrP = strP; /* Remember the ptr */
Zwordrlen = reqlen; /* Required length */
Zword = ZwordC = ZwordX = 0;
}
/* Add a 5-bit byte to a string */
addzchar( ch )
UBYTE ch; /* byte value to add */
{
/* Proceed only if within the required length */
if ( ( Zwordrlen == 0 ) || ( ZwordC < Zwordrlen ) ) {
if ( ZwordX == 3 ) { /* Last word full ? */
*ZstrP++ = (Zword>>8) & 0xff;
*ZstrP++ = Zword & 0xff;
Zword = ZwordX = 0;
}
/* Add in new byte */
if (ZwordC * 2 / 3 >= Max_buf_len) {
my_fprintf(stderr,"Error in addzchar from zstring.c: zstring too long.\n");
my_exit(1);
}
Zword = ( Zword << 5 ) | ch;
++ZwordX;
++ZwordC;
}
}
/* End the Z-string */
endzstr()
{
/* Take care of any minimum length requirement first by adding pad bytes */
if ( Zwordrlen != 0 ) {
while( ZwordC < Zwordrlen )
addzchar( '\005' );
}
/* Finish off the last word by adding pad (5) chars. */
while( ZwordX++ != 3 )
Zword = ( Zword << 5 ) | '\005';
/* Set the high bit to mean last word of string */
Zword |= 0x8000;
/* Place the word in the string */
*ZstrP++ = (Zword>>8) & 0xff;
*ZstrP++ = Zword & 0xff;
/* Add a zero byte just for luck. */
*ZstrP = NUL;
}
int zchar_size(ch, cset_ptr)
UBYTE ch;
int *cset_ptr;
{
int val, cset;
if (cset_ptr) *cset_ptr = 2;
if (ch == ' ')
return(1);
else if (!ch) return(0);
else if (Zctbl[ch].zc_char == 0) {
cset = 2;
val = 4; }
else {
cset = Zctbl[ch].zc_set;
if (cset != 0) {
val = 2; }
else
val = 1; }
if (cset_ptr)
*cset_ptr = cset;
return(val); }
typedef struct fw_obj {
struct fw_obj *fw_next; /* Next element in hash bucket */
UBYTE *fw_str;
int fw_strlen; /* Length of fw_str */
int fw_original_zlen; /* Original encoded length */
int fw_zlen; /* Used to compute rating' */
int fw_freq; /* Number of uses */
int fw_original_freq; /* Safety first */
int fw_goodness;
int fw_id;
struct fw_obj *fw_parent; /* If this is 'The ', points to 'The' */
struct fw_obj *fw_child; /* If this is 'The', points to 'The ' */
struct fw_obj *fw_sibling; /* If this is 'The ', points to 'The,' */
} *fw_node;
#define fw_hash_len 997
fw_node fw_hash[fw_hash_len];
fw_get_hash(str, len)
UBYTE *str;
int len;
{
int hash = 0;
int i, woffs = 0, shft = 0;
UBYTE ch;
for (i = 0; i < len; i++) {
ch = str[i];
hash ^= ch << shft;
woffs++;
if (woffs >= sizeof(int)) {
woffs = 0;
shft = 0; }
else
shft += 8; }
hash = hash % fw_hash_len;
if (hash < 0) return(-hash);
else return(hash); }
fw_node fw_lookup(str, len, hash)
UBYTE *str;
int len;
int hash;
{
fw_node buck;
if (buck = fw_hash[hash]) {
while (buck) {
if ((buck->fw_strlen == len) &&
(strncmp(str, buck->fw_str, len) == 0)) {
buck->fw_freq++;
return(buck); }
buck = buck->fw_next; } }
return(0); }
int fw_node_ct = 0;
fw_node fw_add(str, len, zlen, parent, hash)
UBYTE *str;
int len, zlen;
fw_node parent;
int hash;
{
fw_node buck;
UBYTE *nstr;
fw_node_ct++;
buck = (fw_node)MALLOC(sizeof(struct fw_obj));
buck->fw_id = 0;
buck->fw_next = fw_hash[hash];
fw_hash[hash] = buck;
buck->fw_strlen = len;
buck->fw_zlen = zlen;
buck->fw_original_zlen = zlen;
buck->fw_freq = 1;
buck->fw_parent = parent;
buck->fw_child = 0;
if (parent) {
buck->fw_sibling = parent->fw_child;
parent->fw_child = buck; }
else
buck->fw_sibling = 0;
nstr = (UBYTE *)MALLOC(len);
strncpy(nstr, str, len);
buck->fw_str = nstr;
return(buck); }
/* Call this with a string pointer. Update all the frequent-words stuff. */
/* As currently constituted, this includes all prefixes that might be interesting
as well. Thus "theory of" will cause "the", "theo", "theor", "theory", and
"theory " to be added to the database. The process of assigning the actual
frequent words becomes something of a relaxation process: if "the" becomes
a frequent word, then the goodness of "theory" must be adjusted downward.
If "theory" becomes a frequent word anyway, then the goodness of
"the" must be adjusted downward, leading to a corresponding upward adjustment
in the goodness of "theory". This could be fun. */
fw_string(strP)
UBYTE *strP;
{
UBYTE ch; /* Character */
UBYTE curcset; /* Current character set */
UBYTE ccode; /* Character */
int cset;
int cchar_size;
int ccount = 0; /* Number of input characters */
int cstrlen = 0; /* Number of output characters */
UBYTE *strP_save = strP;
fw_node frob, last_frob = 0;
int last_frob_len;
if (!strP) return;
for (; (ch = *strP) != NUL; ++strP) {
if (ch == language_char)
strP = translate_char(strP, &ch);
if (character_count)
character_counts[ch]++;
cchar_size = zchar_size(ch, &cset); /* This puts space into set 2... */
if ((cset == 0) || (cset == 1) || (ch == '.') || (ch == '?') ||
(ch == '!') || (ch == ',')) {
/* Normally character sets 0 and 1 are upper and lower case letters.
If .CHRSETs have happened, they might be something else. */
cstrlen += cchar_size;
ccount++;
last_frob = 0;
last_frob_len = 0;
strP_save = strP; /* Save pointer to first interesting char */
while (1) {
int hash;
ch = *++strP;
cchar_size = zchar_size(ch, &cset);
if (cset != 2) {
/* Still in a word */
cstrlen += cchar_size;
ccount++;
if (cstrlen > 2) {
hash = fw_get_hash(strP_save, ccount);
if (!(frob = fw_lookup(strP_save, ccount, hash)))
frob = fw_add(strP_save, ccount, cstrlen, last_frob, hash);
last_frob_len = ccount;
last_frob = frob; }
continue; }
/* Hit end of word: end of string or non-"alpha" */
if (ch == '\'') {
/* Look for "'s" and friends */
zchar_size(*(strP + 1), &cset); /* Get the next character's set */
if (cset != 2) {
/* it's alpha, so add in the ' and continue */
cstrlen += cchar_size;
ccount++;
continue; } }
if (ch == ' ') {
ccount++;
cstrlen++; }
else if (cset == 2) {
/* We already did this one */
strP--;
cstrlen = 0;
ccount = 0;
break; }
/* strP_save contains pointer to beginning of string with
ccount characters, and encoded size of cstrlen. */
if (cstrlen > 2) {
if (last_frob_len == ccount)
printf("losing\n");
hash = fw_get_hash(strP_save, ccount);
/* Don't bother if the encoding is too short to be helpful */
if (!fw_lookup(strP_save, ccount, hash))
fw_add(strP_save, ccount, cstrlen, last_frob, hash); }
strP--;
cstrlen = 0;
ccount = 0;
break; } } } }
fw_node frequent_words[96];
int fw_worst_allocated = 0x3fffffff;
int fw_worst_index = -1;
prune_parents(thing)
fw_node thing;
{
fw_node parent = thing->fw_parent;
/* For a given node, find out if its parent has no other descendants.
If that's true, then there's no reason to consider it. Wander up
the tree until an interesting parent is found, or until we run out
of them. */
while (parent) {
fw_node new_parent;
if (thing->fw_freq == parent->fw_freq) {
parent->fw_id = -1; /* This will keep the parent from being considered */
parent->fw_child = 0; /* The parent has no child any more */
if (parent->fw_sibling) {
/* If the parent has siblings, then we can stop anyway */
fw_node new_thing = parent->fw_parent;
fw_node kid = new_thing->fw_child;
/* The parent doesn't have a parent either */
parent->fw_parent = 0;
thing->fw_parent = new_thing;
/* Now place thing in its ancestor's tree in place of the parent. */
if (kid == parent) {
thing->fw_sibling = parent->fw_sibling;
new_thing->fw_child = thing;
thing->fw_parent = new_thing;
break; }
while (1) {
if (kid->fw_sibling == parent) {
kid->fw_sibling = thing;
thing->fw_sibling = parent->fw_sibling;
break; }
kid = kid->fw_sibling; }
break; }
/* At this point we know that the parent has no siblings */
new_parent = parent->fw_parent;
parent->fw_parent = 0;
parent = new_parent;
thing->fw_parent = parent;
if (!parent) {
/* No more parents, so stop */
thing->fw_sibling = 0;
break; }
thing->fw_sibling = parent->fw_child;
parent->fw_child = thing; }
else
break; } }
print_name(thing)
fw_node thing;
{
UBYTE *str = thing->fw_str;
int len = thing->fw_strlen, i;
for (i = 0; i < len; i++)
printf("%c", str[i]); }
fw_node any_frequent_words(thing)
fw_node thing;
{
for (; thing; thing = thing->fw_parent)
if (thing->fw_id > 0) return(thing);
return(0); }
dump_frequent_words(game_name)
char *game_name;
{
int i, j;
fw_node buck;
int fw_allocated = 0;
int fw_best = 0;
int goodness;
int changed = 1;
int passes = 0;
printf("%d nodes allocated.\n", fw_node_ct);
for (i = 0; i < fw_hash_len; i++) {
buck = fw_hash[i];
while (buck) {
if (buck->fw_parent)
prune_parents(buck);
buck->fw_goodness = (buck->fw_freq - 1) * (buck->fw_zlen - 2);
/* Subtract one because the string will still occur in the frequent
words table; two because that's the assumed length of the fw
reference. */
buck->fw_original_freq = buck->fw_freq;
buck = buck->fw_next; } }
while (1) {
if (!changed) break;
changed = 0;
passes++;
if (passes > 100) {
printf("Giving up after 100 passes\n");
break; }
for (i = 0; i < fw_hash_len; i++) {
buck = fw_hash[i];
while (buck) {
fw_node parent;
goodness = buck->fw_goodness;
if ((goodness > 0) && (buck->fw_id == 0) &&
((fw_allocated < 96) || (goodness > fw_worst_allocated))) {
changed++;
/* Try this word in the frequent words table */
if (fw_allocated == 96) {
/* Table is full, so have to kill something that was already
there */
fw_insert(buck, fw_worst_index, 1); }
else {
fw_insert(buck, fw_allocated, 0);
fw_allocated++; }
fw_set_worst(fw_allocated); }
else if ((buck->fw_id == 0) && (parent = buck->fw_parent) &&
(parent = any_frequent_words(parent))) {
/* We're looking at the descendant of a word that's already in the
table. Decide whether it would be better to have the descendant
in the table with the ancestor removed. */
int new_goodness, new_parent_goodness;
/* Get the rating for the child with the parent removed. We already
know that the child isn't good enough to be added to the table
with the parent present. */
new_goodness = buck->fw_goodness +
(buck->fw_freq - 1) * (parent->fw_zlen - 2);
if (new_goodness > parent->fw_goodness) {
print_name(buck);
printf(" in for ");
print_name(parent);
printf(".\n");
fw_insert(buck, parent->fw_id - 1, 1);
changed++; } }
buck = buck->fw_next; } }
printf("End of pass %d. %d changes, worst entry is %d.\n",
passes, changed, fw_worst_allocated); }
sort_stuff(fw_allocated);
print_stuff(game_name, fw_allocated); }
sort_stuff(cnt)
int cnt;
{
int i, j;
fw_node temp;
for (i = (cnt - 2); i >= 0; i--) {
for (j = 0; j <= i; j++) {
if (frequent_words[j]->fw_goodness <
frequent_words[j + 1]->fw_goodness) {
temp = frequent_words[j];
frequent_words[j] = frequent_words[j + 1];
frequent_words[j + 1] = temp; } } } }
print_stuff(game_name, cnt)
char *game_name;
int cnt;
{
int i, j;
fw_node temp;
FILE *fw_file;
int tdesc;
char real_name[255];
char new_name[255];
char c;
int original_zbytes = 0, final_zbytes = 0;
strcpy(&real_name[0], game_name);
strcat(&real_name[0], "freq.zap");
tdesc = open(&real_name[0], O_RDONLY, 0);
if (tdesc >= 0) {
strcpy(&new_name[0], &real_name[0]);
strcat(&new_name[0], "~");
close(tdesc);
rename(&real_name[0], &new_name[0]); }
fw_file = fopen(&real_name[0], "w");
for (i = 0; i < cnt; i++) {
temp = frequent_words[i];
original_zbytes += temp->fw_freq * temp->fw_zlen;
final_zbytes += temp->fw_zlen + temp->fw_freq * 2;
fprintf(fw_file, " .FSTR FSTR?%d,\"", i + 1);
for (j = 0; j < temp->fw_strlen; j++) {
c = temp->fw_str[j];
if (c == '\"')
fprintf(fw_file, "\"\"");
else
fprintf(fw_file, "%c", c); }
fprintf(fw_file, "\" ;%d %d %d\n",
temp->fw_goodness, temp->fw_freq, temp->fw_original_freq); }
fprintf(fw_file, "\n\nWORDS:: .TABLE\n");
for (i = 0; i < cnt; i++)
fprintf(fw_file, " FSTR?%d\n", i + 1);
fprintf(fw_file, " .ENDT\n\n .ENDI\n");
printf("%d to %d = %d.\n", original_zbytes, final_zbytes,
original_zbytes - final_zbytes);
fclose(fw_file); }
check_name(thing, str)
fw_node thing;
char *str;
{
char *ostr = (char *)(thing->fw_str);
if (strlen(str) != thing->fw_strlen)
return(0);
if (strncmp(str, ostr, thing->fw_strlen) != 0) return(0);
return(1); }
update_parents(thing, ct)
fw_node thing;
int ct;
{
thing = thing->fw_parent;
while (thing) {
thing->fw_freq += ct;
thing->fw_goodness += ct * (thing->fw_zlen - 2);
/* The parent's goodness decreases only by the lost frequency from
its child */
if (thing->fw_id) {
break; }
thing = thing->fw_parent; } }
update_kids(thing, fudge)
fw_node thing;
int fudge;
{
thing = thing->fw_child;
while (thing) {
/* If the parent is becoming a frequent word, the child's goodness decreases:
its effective length is now only (child_zlen - parent_zlen + 2), because
it can be encoded in that. */
/* Fudge is the encoded length of the parent */
if (fudge > 0)
thing->fw_zlen = thing->fw_original_zlen - fudge + 2;
else
thing->fw_zlen = thing->fw_zlen - fudge -2;
thing->fw_goodness = (thing->fw_freq - 1) * (thing->fw_zlen - 2);
if ((thing->fw_child) && !thing->fw_id)
update_kids(thing, fudge);
thing = thing->fw_sibling; } }
/* Find the lowest-quality frequent word */
fw_set_worst(ct)
int ct;
{
int i;
fw_worst_allocated = 0x3fffffff;
fw_worst_index = -1;
for (i = 0; i < ct; i++) {
if (frequent_words[i]->fw_goodness < fw_worst_allocated) {
fw_worst_allocated = frequent_words[i]->fw_goodness;
fw_worst_index = i; } } }
/* We think that 'thing' wants to be a frequent word. 'at' is either
fw_worst_index (if kill is true) or the next empty slot (if kill is
false; happens when fewer than 96 words have been allocated). */
fw_insert(thing, at, kill)
fw_node thing;
int at;
int kill;
{
fw_node other_thing;
if (kill)
fw_dealloc(at);
thing->fw_id = at + 1;
frequent_words[at] = thing;
if (thing->fw_parent)
update_parents(thing, -thing->fw_freq);
if (thing->fw_child)
update_kids(thing, thing->fw_zlen); }
/* Kill the last entry in the frequent words table. Update goodness of any
of its relatives. */
fw_dealloc(at)
int at;
{
fw_node thing = frequent_words[at], other_thing;
thing->fw_id = 0;
if (thing->fw_parent)
/* If this has a parent, the parent's goodness miraculously increases
(along with the goodness of all ancestors) */
update_parents(thing, thing->fw_freq);
if (thing->fw_child)
update_kids(thing, -thing->fw_zlen); }