-
Notifications
You must be signed in to change notification settings - Fork 7
/
ComGlobal.cpp
2128 lines (1910 loc) · 61.4 KB
/
ComGlobal.cpp
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
#include <string>
#include "ComGlobal.h"
#ifdef LRSTAR
#include "PGGlobal.h"
#endif
#ifdef DFASTAR
#include "LGGlobal.h"
#endif
#ifdef _DEBUG
#include "conio.h"
#endif
#ifdef UNIX
static const char dirSep = '/';
#else
static const char dirSep = '\\';
#endif
// 1 = upper, 2 = lower, 4 = '_', 8 = digit, 16 = quote ("|')
uchar charcode[256] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 16, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 4,
16, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
// numeric[x] gives 1..10 for digits 0..9
uchar numeric[256] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
// 1 = upper, 2 = lower, 4 = '_'
uchar alpha[256] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 4,
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
// upper[x] gives the uppercase of x.
uchar upper[256] =
{
0, 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, 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,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
};
// lower[x] makes lower case of x.
uchar lower[256] =
{
0, 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, 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, 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
};
///////////////////////////////////////////////////////////////////////////////
// //
int itsakeyword (char* terminal)
{
char* p;
if (terminal[0] == '<') return 0;
if (terminal[0] == '{') return 0;
for (p = terminal; *p != 0; p++)
{
if (alpha[*p] & 3) // upper or lower case letter?
{
return (1); // YES
}
}
return (0); // NO
}
///////////////////////////////////////////////////////////////////////////////
// //
char* strchr (char*p, char c)
{
while (*p && (*p++ != c));
if (*p == 0) return (NULL);
return (p-1);
}
///////////////////////////////////////////////////////////////////////////////
// //
char* strrchr (char* str, char c)
{
char *p;
p = str--;
while (*p++); // Find 0 byte.
p--;
while (--p != str && *p != c);
if (p == str) return (NULL);
return (p);
}
///////////////////////////////////////////////////////////////////////////////
// //
char* mystrlwr (char* s)
{
for (char* p = s; *p != 0; p++)
{
*p = lower[*p];
}
return s;
}
///////////////////////////////////////////////////////////////////////////////
// //
char* mystrupr (char* s)
{
for (char* p = s; *p != 0; p++)
{
*p = upper[*p];
}
return s;
}
#ifdef UNIX
///////////////////////////////////////////////////////////////////////////////
// //
// _filelength function supplied by Vasko Mitanov, Feb 2012. //
long _filelength (int fd)
{
struct stat st;
fstat(fd, &st);
return st.st_size;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// //
void PRT_OPTNS (int na, char** arg)
{
int i, opt = 0;
for (i = 2; i < na; i++)
{
#ifdef WINDOWS
if (*arg[i] == '/') // Windows style args?
{
prt_log ("%s ", arg[i]);
}
#endif
#ifdef UNIX
if (*arg[i] == '-') // Unix style args?
{
prt_log ("%s ", arg[i]);
}
#endif
}
prt_log ("\n\n");
}
///////////////////////////////////////////////////////////////////////////////
// //
int SET_OPTNS (int na, char** arg)
{
int i;
for (i = 2; i < na; i++)
{
#ifdef WINDOWS
if (*arg[i] == '/') // Windows style option?
{
if (arg[i][1] != 0) SET_OPTN (arg[i]+1, "", 0);
}
#endif
#ifdef UNIX
if (*arg[i] == '-') // UNIX style option?
{
if (arg[i][1] != 0) SET_OPTN (arg[i]+1, "", 0);
}
#endif
}
if (n_errors > 0)
{
prt_log ("\n");
return (0);
}
return (1);
}
///////////////////////////////////////////////////////////////////////////////
// //
int SET_OPTN (char* opt, char* fid, int linenumb)
{
#ifdef LRSTAR
return (set_optn (PGOption, opt, fid, linenumb));
#endif
#ifdef DFASTAR
return (set_optn (LGOption, opt, fid, linenumb));
#endif
}
///////////////////////////////////////////////////////////////////////////////
// //
int SET_OPTN_MA (char* opt, char* fid, int linenumb)
{
return (set_optn (MAOption, opt, fid, linenumb));
}
///////////////////////////////////////////////////////////////////////////////
// //
int set_optn (OPTION* option, char* opt, char* fid, int linenumb)
{
char *o;
const char* a;
int i, value;
top: value = 1;
if (*opt == '"') opt++;
while (*opt == ' ' || *opt == '\t' || *opt == ',') opt++;
for (i = 0; *option[i].name != 0; i++)
{
a = option[i].name;
for (o = opt;; o++, a++)
{
if (*a == 0) // abbreviation end?
{
if (*o == 0 ) goto set;
if (*o == '=' ) goto val;
if (*o == ' ' ) goto spa;
if (*o == '\t') goto spa;
if (*o == ',' ) goto set;
if (*o == '\"') goto set;
if (!alpha[*o]) goto set;
break;
}
if (*o == 0) break; // opt end?
if (*o != *a) break; // not equal?
}
}
while (alpha[*o]) o++;
*o = 0;
n_errors++;
if (linenumb == 0) prt_log ("Option '%s' is invalid, not one of the following:\n\n", opt);
else prt_log ("%s(%04d) : Option '%s' is invalid, not one of the following:\n\n", fid, linenumb, opt);
prt_log (" OPTION DEFAULT DESCRIPTION\n");
for (i = 0; *option[i].name != 0; i++)
{
prt_log (" %-6s %8d %s.\n", option[i].name, option[i].defvalue, option[i].desc);
}
prt_log ("\n");
return (0);
spa: while (*o == ' ' || *o == '\t') o++; // bypass spaces and tabs
if (*o == '=')
{
val: o++;
while (*o == ' ' || *o == '\t') o++; // bypass spaces and tabs
if (*o < '0' || *o > '9')
{
n_errors++;
if (linenumb == 0) prt_log ("Value for option '%s' is an invalid number.\n", opt);
else prt_log ("%s(%04d) : Value for option '%s' is an invalid number.\n", fid, linenumb, opt);
return (0);
}
value = atoi(o);
while (*o >= '0' && *o <= '9') o++; // bypass digits
}
set: if (*opt == '/')
{
if (value <= 0)
{
n_errors++;
if (linenumb) prt_log ("Option '%s', must have a value greater than zero.\n", opt);
else prt_log ("%s(%04d) : Option '%s', must have a value greater than zero.\n", fid, linenumb, opt);
return (0);
}
}
optn[option[i].numb] = value;
if (*o == ',' || *o == ' ')
{
o++;
opt = o;
goto top;
}
if (*o != '"' && *o != 0)
{
n_errors++;
if (linenumb) prt_log ("Illegal character '%c' in option.\n\n", *o);
else prt_log ("%s(%04d) : Illegal character '%c' in option.\n\n", fid, linenumb, *o);
return (0);
}
return 1;
}
///////////////////////////////////////////////////////////////////////////////
// //
int GetMaxValues (const char* dn, const char* fn)
{
char* p;
int rc; // Return code.
int nb; // Number of bytes read.
int linenumb;
int filedesc;
int filesize;
strcpy (exefid, dn);
strcat (exefid, fn);
rc = 1; // OK
linenumb = 1;
filedesc = open (exefid, 0); // Open the file.
if (filedesc < 0) // File not found.
{
SaveMaxValues (dn, fn); // Create it.
return rc;
}
filesize = _filelength (filedesc) + 2; // Set amount to read.
ALLOC (input_start, filesize);
nb = read (filedesc, input_start, filesize);
if (nb <= 0)
{
SaveMaxValues (dn, fn); // Create it.
goto Ret;
}
input_end = input_start + nb; // Set end-of-buffer pointer.
*(input_end) = EOL_CHAR;
*(input_end+1) = EOF_CHAR;
p = input_start;
do
{
FindOpt: while (*p != '"' && *p != EOF_CHAR && *p != '\n') p++;
if (*p == '\n')
{
p++;
linenumb++;
goto FindOpt;
}
if (*p == EOF_CHAR) goto Ret;
if (*(p+1) != '/')
{
p++; // skip over "
Loop: while (*p != '"' && *p != EOF_CHAR && *p != '\n') p++;
if (*p == '\n')
{
p++;
linenumb++;
goto FindOpt;
}
if (*p == EOF_CHAR) goto Ret;
p++; // skip over ending "
goto FindOpt;
}
char* q = p+1;
Scan: while (*q != '"' && *q != EOF_CHAR && *p != '\n') q++;
if (*q == '\n')
{
p = ++q;
linenumb++;
goto FindOpt;
}
if (*q == EOF_CHAR) goto Ret;
*(++q) = 0; // Mark
if (SET_OPTN_MA (p, exefid, linenumb) == 0)
{
rc = 0;
goto Ret;
}
p = q+1;
}
while (p < input_end);
Ret: FREE (input_start, filesize);
close (filedesc); // Close input file.
return rc;
}
///////////////////////////////////////////////////////////////////////////////
// //
void SaveMaxValues (const char* dn, const char* fn)
{
int i;
FILE* fp;
strcpy (exefid, dn);
strcat (exefid, fn);
fp = fopen (exefid, "w");
if (fp == NULL)
{
printf ("Error: Cannot write file: '%s'.\n", exefid);
Terminate (1);
}
fprintf (fp, "\nMemory Allocation Options (maximum values).\n");
fprintf (fp, "Modify these values to suit your needs.\n\n");
for (i = 0; *MAOption[i].name != 0; i++)
{
fprintf (fp, "\"%-6s = %8d\" %s\n", MAOption[i].name, optn[MAOption[i].numb], MAOption[i].desc);
}
fclose (fp);
}
///////////////////////////////////////////////////////////////////////////////
// //
int GetSkeletonFilename (char* arg, int i, int na)
{
int filedesc;
char string[64];
if (i >= na) return (0);
if (arg == NULL) return (0);
#ifdef WINDOWS
if (*arg == '/') return (0);
#endif
#ifdef UNIX
if (*arg == '-') return (0);
#endif
strcpy (sklfid, arg); // In case of error in get_fid().
if (!get_fid (arg, sdn, sfn, sft)) return 0;
if (sfn[0] == 0 || sft[0] == 0)
{
n_errors++;
prt_log ("Error: Invalid skeleton filename '%s'\n", arg);
return (0);
}
strcpy (string, sft);
mystrlwr (string);
if (strcmp (string, ".skl") != 0)
{
n_errors++;
prt_log ("Error: Invalid skeleton filename '%s%s' (filetype is not '.skl')\n", sfn, sft);
return (0);
}
strcpy (sklfid, sdn);
strcat (sklfid, sfn);
strcat (sklfid, sft);
filedesc = open (sklfid, 0);
if (filedesc < 0)
{
n_errors++;
prt_log ("Error: Cannot find skeleton file '%s'\n", sklfid);
return (0);
}
close (filedesc);
return (1);
}
///////////////////////////////////////////////////////////////////////////////
// //
int GetOutputFilename (char* arg, int i, int na)
{
char string[64];
if (i >= na)
{
n_errors++;
prt_log ("Error: No output filename after '%s'\n", sklfid);
return (0);
}
if (arg == NULL) return (0);
if (*arg == '(') return (0);
if (!get_fid (arg, odn, ofn, oft)) return 0;
if (ofn[0] == 0 || oft[0] == 0)
{
n_errors++;
prt_log ("Error: Invalid output filename '%s'\n", arg);
return (0);
}
strcpy (string, oft);
mystrlwr (string);
if (strcmp (string, ".skl") == 0)
{
n_errors++;
prt_log ("Error: Invalid output filename '%s%s' (filetype cannot be '.skl')\n", sfn, sft);
return (0);
}
strcpy (outfid, odn);
strcat (outfid, ofn);
strcat (outfid, oft);
return (1);
}
///////////////////////////////////////////////////////////////////////////////
// //
int get_fid (char *arg, char *dir, char *fn, char *ft)
{
int len;
char *f, c, *last_slash, *last_dot;
dir[0] = 0;
fn [0] = 0;
ft [0] = 0;
last_slash = strrchr(arg, dirSep);
if (last_slash != NULL)
{
f = last_slash + 1; // Point at filename start.
c = *f; // Save the char.
*f = 0; // Drop null there.
if (f-arg < MAX_DIR) // If length is OK.
{
strcpy (dir, arg); // Copy to 'dir'.
}
else // Directory name is too long.
{
n_errors++;
if (n_errors == 1) printf ("\n");
printf ("Directory name\n\n%s\n\nhas more than %d characters.\n\n", arg, MAX_DIR-1);
return (0);
}
*f = c; // Replace char.
}
else f = arg; // Point at filename start.
last_dot = strrchr (f, '.');
if (last_dot != NULL)
{
*last_dot = 0;
if (last_dot-f < MAX_FILENAME)
{
strcpy (fn, f); // Copy to 'fn'.
}
else // Filename is too long.
{
n_errors++;
if (n_errors == 1) printf ("\n");
printf ("Filename\n\n%s\n\nhas more than %d characters.\n\n", f, MAX_FILENAME-1);
return (0);
}
*last_dot = '.'; // Replace dot.
len = strlen(last_dot);
if (len < MAX_FILETYPE)
{
strcpy (ft, last_dot); // Copy to 'ft'.
}
else // Filetype is too long.
{
n_errors++;
if (n_errors == 1) printf ("\n");
printf ("Filetype\n\n%s\n\nhas more than %d characters.\n\n", last_dot, MAX_FILETYPE-1);
return (0);
}
}
else // No '\' and no '.'
{
len = strlen (f);
if (len < MAX_FILENAME)
{
strcpy (fn, f); // Copy to 'fn'.
}
else // Filename is too long.
{
n_errors++;
if (n_errors == 1) printf ("\n");
printf ("Filename\n\n%s\n\nhas more than %d characters.\n\n", f, MAX_FILENAME-1);
return (0);
}
}
return 1;
}
///////////////////////////////////////////////////////////////////////////////
// //
#ifdef _DEBUG
char* alloc (char *s, char*& x, int size, int n)
{
x = (char*)malloc (n*size);
if (x == NULL)
{
n_errors++;
prt_log ("Allocation error for '%s', %u bytes not available.\n\n", s, size*n);
Terminate (1);
}
memory_usage += n*size;
if (memory_usage > memory_max) memory_max = memory_usage;
return (x);
}
#else
char* alloc (char*& x, int size, int n)
{
x = (char*)malloc (n*size);
if (x == NULL)
{
n_errors++;
prt_log ("Allocation error, %u bytes not available.\n\n", size*n);
exit (1);
}
memory_usage += n*size;
if (memory_usage > memory_max) memory_max = memory_usage;
return (x);
}
#endif
///////////////////////////////////////////////////////////////////////////////
// //
void ralloc (char*& x, int size, int n1, int n2)
{
x = (char*)realloc (x, size*n2);
memory_usage -= (n1-n2)*size;
}
///////////////////////////////////////////////////////////////////////////////
// //
void frea (char*& x, int size, int n)
{
free (x);
memory_usage -= n*size;
x = NULL;
}
///////////////////////////////////////////////////////////////////////////////
// //
int fastcmp (int *a, int *b, int n)
{
int i;
for (i = 0; i < n; i++)
{
if (a[i] != b[i]) return (0);
}
return (1);
}
///////////////////////////////////////////////////////////////////////////////
// //
void fastcpy (int *a, int *b, int n)
{
int i;
for (i = 0; i < n; i++) b[i] = a[i];
}
///////////////////////////////////////////////////////////////////////////////
// //
void fastini (int v, int *b, int n)
{
int i;
for (i = 0; i < n; i++) b[i] = v;
}
///////////////////////////////////////////////////////////////////////////////
// //
int fastmrg (int *a, int *b, int n)
{
int i;
for (i = 0; i < n; i++)
{
if (a[i] == 0) continue;
if (a[i] == b[i]) continue;
if (b[i] != 0) return (0);
b[i] = a[i];
}
return (1);
}
///////////////////////////////////////////////////////////////////////////////
// //
void fastor (int *a, int *b, int n)
{
int i;
for (i = 0; i < n; i++) b[i] |= a[i];
}
////////////////////////////////////////////////////////////////////////////////
// //
void fix_backslash (char *in) // Change \\ to \ in place.
{
char *out = in;
while (*in != 0)
{
if (*in == dirSep && *(in+1) == dirSep) in++;
*out++ = *in++;
}
*out = 0;
}
///////////////////////////////////////////////////////////////////////////////
// //
int symlen (int s, char** term_name, char** head_name)
{
int L;
if (s >= 0) L = strlen (term_name [s]);
else L = strlen (head_name [-s]);
return (L);
}
///////////////////////////////////////////////////////////////////////////////
// //
int sqrt (int n) // Not very efficient.
{
int r;
for (r = 1; r < 65535; r++)
{
if (r*r >= n) return (r);
}
return (65535);
}
///////////////////////////////////////////////////////////////////////////////
// //
// number - put commas into number (12,345,002) //
void number (int x, char* string)
{
int i, j, k;
char buff[16];
sprintf (buff, "%d\0", x);
k = strlen (buff);
i = k + (k-1)/3;
string[i--] = 0;
j = 0;
while (1)
{
string[i--] = buff[--k];
if (k == 0) break;
if (++j % 3 == 0) string[i--] = ',';
}
}
///////////////////////////////////////////////////////////////////////////////
// //
// SORT - Sort one vector.
void SORT (int *start, int *end) /* Integer bubble sort. */
{
// Sort in place, destroys the original order.
int *p, *q, x;
end--;
p = start;
while (p < end)
{
x = *(q = ++p);
do
{
if (x < *--q)
{
*(q+1) = *q;
*q = x;
}
else break;
}
while (q > start);
}
}
///////////////////////////////////////////////////////////////////////////////
// //
// SORT2 - Sort one vector of length n. //
void SORT2 (int* value, int* seq, int n)
{
// Sort in place. Destroys the original order, but seq contains the original order.
int *last;
int *v1, *v2, vt;
int *s1, *s2, st;
if (n <= 1) return;
// Note: when using pointers instead of indexes,
// we must be careful not to decrement below zero.
last = value + n - 1;
v1 = value;
s1 = seq;
while (v1 < last) // from first to last-1
{
v2 = ++v1;
s2 = ++s1;
while (v2 > value) // while next one > first in list.
{
vt = *v2--; // save higher one into temp.
st = *s2--; // save higher one's index into temp.
if (vt < *v2) // if temp less than one above ...
{
*(v2+1) = *v2; // switch these two ...
*(s2+1) = *s2; // .
*v2 = vt; // .
*s2 = st; // done switching.
}
else break;
}
}
/* for (int i = 0; i < n; i++)
{
printf ("%5d %5d\n", value[i], seq[i]);
}
*/
}
///////////////////////////////////////////////////////////////////////////////
// //
// SORT3 - Sort three vectors of length n, see SORT2 above for explanation.
void SORT3 (int* a, int* b, int* c, int n)
{
int *last;
int *a1, *a2, at;
int *b1, *b2, bt;
int *c1, *c2, ct;
if (n <= 1) return;
// Note: when using pointers instead of indexes,
// we must be careful not to decrement below zero.
a1 = a;
b1 = b;
c1 = c;
last = a + n - 1;
while (a1 < last) // from first to last-1
{
a2 = ++a1;
b2 = ++b1;
c2 = ++c1;
while (a2 > a) // while next one > first in list.
{
at = *a2--; // save higher one into temp.
bt = *b2--; // save other ones too.
ct = *c2--; // save other ones too.
if (at < *a2) // if temp less than one above ...
{
*(a2+1) = *a2; // switch these three ...
*(b2+1) = *b2; // .
*(c2+1) = *c2; // .
*a2 = at; // .
*b2 = bt; // done switching.
*c2 = ct; // done switching.
}
else break;
}
}
/* for (int i = 0; i < n; i++)
{
printf ("%5d %5d %5d\n", a[i], b[i], c[i]);
} */
}
////////////////////////////////////////////////////////////////////////////////
// //
void SORTNAMES (char** name, int n, int* seq)
{
/* seq - the sorted sequence:
name[seq[0]] gives the first name in the sorted list.
example:
name[0] = "c", seq[0] = 3
name[1] = "d", seq[1] = 2
name[2] = "b", seq[2] = 0
name[3] = "a", seq[3] = 1
*/
char **P, *P_temp;
int *L, L_temp, seq_temp, i, j, leng, c;
ALLOC (P, n);
ALLOC (L, n);
for (i = 0; i < n; i++)
{
P[i] = name[i];
L[i] = strlen (name[i]);
seq[i] = i;
}
for (i = 1; i < n; i++) // Bubble sort algorithm.
{
P_temp = P[i];
L_temp = L[i];
seq_temp = seq[i];
j = i - 1;
do
{
leng = L[j];
if (L_temp < L[j]) leng = L_temp;
c = strncmp (P_temp, P[j], leng);
if (c < 0 || (c == 0 && L_temp < L[j]))
{
P[j+1] = P[j];
L[j+1] = L[j];
seq[j+1] = seq[j];
P[j] = P_temp;
L[j] = L_temp;
seq[j] = seq_temp;
}
else break;
}
while (--j >= 0);
}
FREE (L, n);
FREE (P, n);
}
////////////////////////////////////////////////////////////////////////////////
// //
void SORTNAMES2 (char** start, int n, int* seq, int* pos)
{
/* seq - the sorted sequence:
start[seq[0]] gives the first name in the sorted list.
pos - the position or rank in the sorted order:
pos[0] gives the position of start[0] in the sorted list.
example:
start[0] = "c", seq[0] = 3, pos[0] = 2
start[1] = "d", seq[1] = 2, pos[1] = 3
start[2] = "b", seq[2] = 0, pos[2] = 1