-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheatshow.cpp
More file actions
845 lines (672 loc) · 22.7 KB
/
eatshow.cpp
File metadata and controls
845 lines (672 loc) · 22.7 KB
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
/*
* Find words in the RAL version of the Edinburgh Associative Thesaurus.
* *** EATSHOW.C ***
*
* M.D. Wilson Informatics Department, Rutherford Appleton Laboratory,
* Chilton, Didcot, Oxon, OQX OX11, U.K.
*
* June 1988
*
* compile with:
*
* cc eatshow.c
*
* The file names in the define statements should be changed for the local
* installation.
*
* The number of words in the files may change if they are altered and
* these values should also be altered in the define statements.
* ========================================================================
* ************************************************************************
* Altered June 2005 by peetm (peet.morris@comlab.ox.ac.uk/peet.morris@cslab.com).
* Added some extra switches: (run the app with -h for full list)
*
* Also added the ability for the program to be passed a series of words at the
* commandline.
*
* compile with: gcc -Wall, cl -W4, icl -Wall etc - gcc eatshow.cpp
* ************************************************************************
* ========================================================================
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#ifndef bool
#define bool int
#define true 1
#define false 0
#endif
// Forward declarations.
//
static void doFileCloseOpen(char, FILE **, FILE **, int *);
static bool toggleDataBaseCheck(char *);
static void out(char *, int, float);
static bool nothingEntered(char *);
static void checkForWord(char *);
static void checkInitRun(void);
static void fromFile(char *);
static bool dumpWords(char *);
static void actOnFlag(char *);
static void resetFlags(void);
static void trimLF(char *);
static void trim(char *);
static void about(void);
static void usage(void);
static const char * const SRFILE = "./sr_concise"; /* file containing s-r data */
static const char * const RSFILE = "./rs_concise"; /* file containing r-s data */
static const char * const SRINDEX = "./sr_index"; /* s-r index file */
static const char * const RSINDEX = "./rs_index"; /* r-s index file */
static const int SRLENGTH = 8211; /* number of headwords in s-r data */
static const int RSLENGTH = 22776; /* number of headwords in r-s data */
static const int MAXBUF = 10000; /* Maximum buffer size for words or association lists */
static const int WORD_FOUND = 0; /* 'cue' found in the database */
static char sourcef = 's'; /* default source file is srfile */
static bool bTabPad = false; /* use tabs defaults to false */
static bool bNumber = false; /* number the ouputs */
static bool binitRun = true; /* first run of this application? */
static bool bDemark = true; /* demark (~~~~) results? */
static bool bLimit = false; /* limit output to nLimit entries? */
static int nLimit = 0;
static char * echoFile = NULL; /* default file to echo to is NULL */
static char * inptFile = NULL; /* file to read instructions from */
static FILE * fp = NULL; /* main db file */
static FILE * fp1 = NULL; /* index file for whatever fp points to */
static int index_length = 0;
// Entry point.
//
int main(int argc, char * argv[])
{
int i;
// Is this the first time this app's been run from this location? Sets the global 'binitRun' to the result.
//
checkInitRun();
// Check for 'flags' and act accordingly if found.
//
for(i = 1; i < argc; i++)
{
if(*argv[i] == '-')
{
// actOnFlag won't return if -i used.
//
actOnFlag(argv[i]);
}
else
{
// ** Valid 'initial' switches come before non-switches (words) and runtime
// switches like -sw (which, for -sw, should come after a word really).
// We break out of the for loop when/if we hit the first non-switch.
//
// e.g., consider: eatshow -ftest.txt -n -r man -sw man
//
// Here, -f -n and -r will be processed, but -sw won't [at this time] -
// the break below will happen when 'man' is seen.
//
break;
}
}
// actOnFlag may have altered sourcef from the default 's' (use the stimulus db)
// AND, we need to make sure we've a db open - this routine does that.
//
doFileCloseOpen(sourcef, &fp, &fp1, &index_length);
// The leading switches are done - but are there are other args? They
// should be words or the -switch dbs' flag if there are.
//
{
char cue[MAXBUF];
// i was set up in the for loop above - that loop may have exited before it
// reached argc's value - see ** for why.
//
// NOTE: If i < argv, we're in commandline mode.
//
if(i < argc)
{
while(i < argc)
{
// Also checks for runtime switches.
//
checkForWord(argv[i++]);
}
// Exit as we're all done in commandline mode.
//
return 0;
}
else
{
// As printf returns the number of chars output, it works quite well
// when used in an && like this.
//
while(printf("Enter a word>") && fgets(cue, MAXBUF, stdin) != NULL && !nothingEntered(cue))
{
checkForWord(cue);
}
return 0;
}
}
}
// Opens appropriate data and index files - closes any that are already open first.
//
static void doFileCloseOpen(char sourcef, FILE ** fp, FILE ** fp1, int * index_length)
{
if(*fp != NULL)
{
fclose(*fp);
}
if(*fp1 != NULL)
{
fclose(*fp1);
}
if(sourcef == 's')
{
if((*fp = fopen(SRFILE, "r")) == NULL)
{
fprintf(stderr, "cannot access the file: %s\n", SRFILE);
exit(1);
}
if((*fp1 = fopen(SRINDEX, "r")) == NULL)
{
fprintf(stderr, "cannot access the file: %s\n", SRINDEX);
exit(1);
}
*index_length = SRLENGTH;
}
if(sourcef == 'r')
{
if((*fp = fopen(RSFILE, "r")) == NULL)
{
fprintf(stderr, "cannot access the file: %s\n", RSFILE);
exit(1);
}
if((*fp1 = fopen(RSINDEX, "r")) == NULL)
{
fprintf(stderr, "cannot access the file: %s\n", RSINDEX);
exit(1);
}
*index_length = RSLENGTH;
}
return;
}
// Used to output stuff to the screen and, optionally, to a file.
//
static void out(char * out1, int count, float prop)
{
const char * format = NULL;
// Determine whether to use spaces or tabs in the output.
//
format = bTabPad ? "%s\t%d\t%.2f\n" : "%-25s %3d %5.2f\n";
printf(format, out1, count, prop);
// Been given a file name to echo output to?
//
if(echoFile != NULL)
{
// YES.
FILE * ech = NULL;
if((ech = fopen(echoFile, "a")) != NULL)
{
fprintf(ech, format, out1, count, prop);
}
fclose(ech);
}
return;
}
// The functionally 'main' routine - checks in the index/data files for relevant data and outputs it.
//
static void checkForWord(char * cue)
{
int count;
int fail = 3; // 3 = 'cue' word not found.
int tot_rec = 0;
int tot_freq = 0;
float prop = 0;
long int tail_address = 0;
long int head_address = 0;
char * out1;
char * out2;
char response_word[MAXBUF];
char index_word [MAXBUF];
doFileCloseOpen(sourcef, &fp, &fp1, &index_length);
// Check that cue isn't either the 'toggle db' or 'dump index words' flags.
//
if(toggleDataBaseCheck(cue) || dumpWords(cue))
{
return;
}
trimLF(cue);
strupr(cue);
printf("\nLooking for: %s in %s MODE\n\n", cue, sourcef == 'r' ? "RESPONSE" : "STIMULUS");
while(!feof(fp1))
{
if(fgets(index_word, 21, fp1) == NULL)
{
fail = 1;
break;
}
if(fscanf(fp1, "%d %d %ld %ld\n", &tot_rec, &tot_freq, &head_address, &tail_address) != 4)
{
fail = 2;
break;
}
trim(index_word);
if(strcmp(index_word, cue) == WORD_FOUND)
{
fail = 0;
break;
}
}
if(fail != 0)
{
printf("eatshow: %s: not found\n", cue);
}
else // We found 'cue' - w00t!
{
if(fseek(fp, tail_address, 0) != -1)
{
if(fgets(response_word, MAXBUF, fp) == NULL)
{
printf("eatshow: %ld: bad address index file\n", tail_address);
}
else
{
int nCount = 0;
out1 = NULL;
if(bDemark)
{
puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
while(out1 = strtok(out1 == NULL ? response_word : (char *) 0 , "|"))
{
out2 = strtok((char *) 0, "|");
count = atoi(out2);
// Limit the output to nLimit entries?
//
if(bLimit)
{
if(nCount >= nLimit)
{
break;
}
}
{
char buffer[100];
// Number the outputs?
//
if(bNumber)
{
++nCount;
sprintf(buffer, "%4d: %s", nCount, out1);
prop = ((float) count / (float) tot_freq);
out(buffer, count, prop);
}
else
{
sprintf(buffer, " %s", out1);
out(buffer, count, prop);
}
}
}
if(bDemark)
{
puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
printf("\n\t%s %s\n\n", cue, sourcef == 'r' ? "was [one of] the 'response(s)' to the stimulli above" : "was [one of] the 'stimuli' to the responses above");
printf("\tNumber of different answers: %d\n", tot_rec);
printf("\t Total count of all answers: %d\n\n", tot_freq);
}
}
else
{
printf("eatshow: %ld: bad address index file\n", tail_address);
}
rewind(fp1);
}
return;
}
// Toggles the database being used (Response/Stimulus) if passed '-sw'.
//
// Returns: true if the database was toggled, else false.
//
static bool toggleDataBaseCheck(char * cue)
{
if(!strcmp(cue, "-sw"))
{
if(sourcef == 's')
{
puts("\t>>Mode switched from Stimulus to Response");
sourcef = 'r';
}
else
{
puts("\t>>Mode switched from Response to Stimulus");
sourcef = 's';
}
return true;
}
else
{
return false;
}
}
// Checks for s being either NULL (a null pointer) or *s being NULL (obviously, it does this latter
// check first!
//
static bool nothingEntered(char * s)
{
if( s == NULL || *s == '\0')
{
return true;
}
else
{
return false;
}
}
// Checks to see if the file es.log is in the same folder as the app. If 'no' we're
// basically assuming that this is the first time this user has ever run eatshow. The
// idea is that we could give some extra guidance on startup you see.
//
// Also, if fpCheck is made global, and not closed until we exit the while loop in main(),
// it could be used to log debug or other runtime info.
//
static void checkInitRun(void)
{
FILE * fpCheck;
// Is this the first time this app has been run (from this folder)?
//
binitRun = (fpCheck = fopen("es.log", "r")) == NULL ? true : false;
// Flag the fact that we've been run by writing out a log file.
// binitRun used later to give some 'first time' guidance.
//
if(binitRun)
{
if((fpCheck = fopen("es.log", "a")) != NULL)
{
fprintf(fpCheck, "eatshow run at %s %s\n", __DATE__, __TIME__);
// Could leave this open at only close it as main() is about to exit.
// Could be useful for various logging thingmys?
//
fclose(fpCheck);
}
fprintf(stderr, "============================================\n");
fprintf(stderr, "Use eatshow -? for help on this application.\n");
fprintf(stderr, "============================================\n");
}
else
{
fclose(fpCheck);
}
return;
}
// Produces a raw dump of the words in the current db's index file - numbers the output.
//
static bool dumpWords(char * cue)
{
if(!strcmp(cue, "-x"))
{
char index_word[MAXBUF];
int ndummy = 0;
long lcount = 0;
doFileCloseOpen(sourcef, &fp, &fp1, &index_length);
while(!feof(fp1))
{
if(fgets(index_word, 21, fp1) == NULL)
{
break;
}
if(fscanf(fp1, "%d %d %d %d\n", &ndummy, &ndummy, &ndummy, &ndummy) != 4)
{
break;
}
trim(index_word);
printf("%5ld: %s\n", ++lcount, index_word);
}
printf("\nAlthough %ld entries were listed, as this is a raw dump of the index,\n", lcount);
puts("the output will contain a number of duplicates.");
rewind(fp1);
return true;
}
return false;
}
static void about(void)
{
char timeBuff[sizeof(__TIME__) + 1];
// Get rid of the seconds info.
//
sprintf(timeBuff, "%s", __TIME__);
//
// Put a null terminator at the second colon position.
//
timeBuff[5] = '\0';
puts("");
puts(" ============================================================================");
puts(" See http://www.eat.rl.ac.uk/");
puts("");
puts(" Modified the original source at the URL - mainly to add the -sw -t -f -n");
puts(" options for my own use.\n\n\tpeetm - peet.morris@comlab.ox.ac.uk");
puts("");
printf("\t\t\tLast built: %s at %s", __DATE__, timeBuff);
puts("");
puts(" ============================================================================");
puts("");
puts(" Some 'option' examples:");
puts("");
puts(" eatshow -n ... numbers the results");
puts("");
puts(" eatshow -ftest.txt ... echo output to the file test.txt: file is appended");
puts(" to if it exists, or created anew if it does not");
puts("");
puts(" eatshow man -sw man ... starts eatshow in non-interactive mode: first");
puts(" outputs the responses when 'man' was used as a");
puts(" stimuli, then toggles (-sw) eatshow into response");
puts(" mode, in which the output lists words which were");
puts(" used as a stimulus, and where 'man' was a response");
puts("");
puts(" eatshow -r -t ... starts eatshow in response mode, and instructs");
puts(" it to use tabs instead of spaces in its layouts");
puts("");
exit(1);
}
// print proper usage and exit
//
static void usage(void)
{
puts("Usage: eatshow [-a -d -f -i -n -r -s -sw -t -x -?] [word_list]");
puts("Find associates to words in the Edinburgh Associative Thesaurus");
puts("");
puts("Outputs:");
puts("\tThe total number of different answers, the count of");
puts("\tall answers, and the list of triads of associated");
puts("\ttypes, their individual frequencies, and proportion");
puts("\tof occurrence. The proportion of occurrence for a");
puts("\tgiven type is its individual frequency divided by");
puts("\tthe total count of all answers.");
puts("");
puts("Switches:");
puts("\t-a \t further info about this application");
puts("\t-d \t turn off results demarcation");
puts("\t-f<file> echo screen output to a file");
puts("\t-i<file> reads/processes input from a file a line at a time");
puts("\t-l<n>\t limits the number of outputs to <n>");
puts("\t-n \t number outputs");
puts("\t-r \t use cue as response");
puts("\t-s \t use cue as stimulus(default)");
puts("\t-sw\t toggles the -r/-s mode [without restart] (runtime switch)");
puts("\t-t \t tab-delimit output [default is to use spaces]");
puts("\t-x \t dumps the index wordlist for the current mode (runtime switch)");
puts("\t-? \t display these options");
puts("");
puts("NOTE: If -i or a word_list is used, eatshow does not enter interactive mode");
puts("");
puts("In interactive mode, to return to the command prompt, simply hit return");
puts("(do not enter a word). Alternately, enter Ctrl + Z");
exit(1);
}
// Trims a LF off a string by inserting a '\0' in the location
// of the first LF found.
//
static void trimLF(char * string)
{
char * p;
if((p = strchr(string, (char)10)) != NULL)
{
*p = '\0';
}
return;
}
// Trims spaces off (nominally -the end-) of a string by inserting a '\0' in the location
// of the first space char found.
//
static void trim(char * string)
{
char * p;
if((p = strchr(string, (char)' ')) != NULL)
{
*p = '\0';
}
return;
}
// Parses command line to set options.
//
static void actOnFlag(char * f)
{
f++;
switch(*f++)
{
case 's': // read s-r file.
sourcef = 's';
break;
case 'r':
sourcef = 'r'; // read r-s file.
break;
case 'f': // echo to file.
if(*f != '\0')
{
echoFile = f;
}
break;
case 't': // tab output.
bTabPad = true;
break;
case 'a': // show 'about'.
about();
break;
case 'd': // add demarcation lines.
bDemark = false;
break;
case 'n': // number outputs.
bNumber = true;
break;
case 'l': // limit the number of outputs.
if(*f != '\0')
{
bLimit = true;
nLimit = atoi(f);
bNumber = true;
}
break;
case '?': // show usage.
usage();
break;
case 'i': // read input from file.
if(*f != '\0')
{
fromFile(f);
exit(0); // close here.
}
break;
case '\0': // error.
usage();
exit(1);
break;
default:
usage();
exit(1);
break;
}
return;
}
// Function to support -i<filename> usage.
//
// s is a pointer to the file to read.
//
// Basically, this function generates an argc/argv pair from each line in the
// input file. It then calls main() to have the commands processed.
//
void fromFile(char * s)
{
FILE * fp = NULL;
if((fp = fopen(s, "r")) != NULL)
{
char * argv[10];
int argc = 0;
char buffer[255];
char * p = NULL;
int n = 0;
// argv[0] is always the appname.
//
argv[0] = (char *)malloc(strlen("eatshow") + 1);
//
strcpy(argv[0], "eatshow");
while(!feof(fp))
{
// Get a line from the file.
//
if(fgets(&buffer[0], 255, fp))
{
// is there a lf or a cr on a seperate line?
//
if(strlen(buffer) == 1)
{
// Yes, ignore.
//
continue;
}
// +++ This bit now parses the line, and creates the argv array.
//
p = strtok(&buffer[0], " ");
while(p && argc != 10)
{
argv[++argc] = (char *)malloc(strlen(p) + 1);
strcpy(argv[argc], p);
p = strtok(NULL, " ");
}
// +++
// Put back defaults ... number output, use tabs, demarcation etc.
//
resetFlags();
// Call main for each suitably parsed line from the file.
//
main(argc + 1, argv);
// Tidy alloc'ed memory away.
//
for(n = 1; n < argc; n++)
{
free(argv[n]);
}
}
}
// Free 'eatshow' alloc'ed memory.
//
free(argv[0]);
fclose(fp);
}
else
{
printf("Error opening %s\n", s);
}
return;
}
// Put app into default state. Mostly used when we're reading directives from a file
// using the -i option.
//
static void resetFlags(void)
{
echoFile = NULL;
inptFile = NULL;
bTabPad = false;
bDemark = true;
bNumber = false;
bLimit = false;
nLimit = 0;
sourcef = 's';
return;
}