forked from chrchang/plink-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plink_common.c
9849 lines (9422 loc) · 311 KB
/
plink_common.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
#include "plink_common.h"
#include "pigz.h"
// no leading \n since this is used in LOGPRINTFWW expressions
const char errstr_fopen[] = "Error: Failed to open %s.\n";
const char cmdline_format_str[] = "\n " PROG_NAME_STR " [input flag(s)...] {command flag(s)...} {other flag(s)...}\n " PROG_NAME_STR " --help {flag name(s)...}\n\n";
char tbuf[TBUF_SIZE];
// note that \xxx character constants are interpreted in octal.
// technically no need to represent 0-31, but 64 extra bytes of data is
// probably cheaper than the code to subtract 32 everywhere.
const char g_one_char_strs[] = "\0\0\1\0\2\0\3\0\4\0\5\0\6\0\7\0\10\0\11\0\12\0\13\0\14\0\15\0\16\0\17\0\20\0\21\0\22\0\23\0\24\0\25\0\26\0\27\0\30\0\31\0\32\0\33\0\34\0\35\0\36\0\37\0\40\0\41\0\42\0\43\0\44\0\45\0\46\0\47\0\50\0\51\0\52\0\53\0\54\0\55\0\56\0\57\0\60\0\61\0\62\0\63\0\64\0\65\0\66\0\67\0\70\0\71\0\72\0\73\0\74\0\75\0\76\0\77\0\100\0\101\0\102\0\103\0\104\0\105\0\106\0\107\0\110\0\111\0\112\0\113\0\114\0\115\0\116\0\117\0\120\0\121\0\122\0\123\0\124\0\125\0\126\0\127\0\130\0\131\0\132\0\133\0\134\0\135\0\136\0\137\0\140\0\141\0\142\0\143\0\144\0\145\0\146\0\147\0\150\0\151\0\152\0\153\0\154\0\155\0\156\0\157\0\160\0\161\0\162\0\163\0\164\0\165\0\166\0\167\0\170\0\171\0\172\0\173\0\174\0\175\0\176\0\177\0\200\0\201\0\202\0\203\0\204\0\205\0\206\0\207\0\210\0\211\0\212\0\213\0\214\0\215\0\216\0\217\0\220\0\221\0\222\0\223\0\224\0\225\0\226\0\227\0\230\0\231\0\232\0\233\0\234\0\235\0\236\0\237\0\240\0\241\0\242\0\243\0\244\0\245\0\246\0\247\0\250\0\251\0\252\0\253\0\254\0\255\0\256\0\257\0\260\0\261\0\262\0\263\0\264\0\265\0\266\0\267\0\270\0\271\0\272\0\273\0\274\0\275\0\276\0\277\0\300\0\301\0\302\0\303\0\304\0\305\0\306\0\307\0\310\0\311\0\312\0\313\0\314\0\315\0\316\0\317\0\320\0\321\0\322\0\323\0\324\0\325\0\326\0\327\0\330\0\331\0\332\0\333\0\334\0\335\0\336\0\337\0\340\0\341\0\342\0\343\0\344\0\345\0\346\0\347\0\350\0\351\0\352\0\353\0\354\0\355\0\356\0\357\0\360\0\361\0\362\0\363\0\364\0\365\0\366\0\367\0\370\0\371\0\372\0\373\0\374\0\375\0\376\0\377";
const char* g_missing_geno_ptr = &(g_one_char_strs[96]);
const char* g_output_missing_geno_ptr = &(g_one_char_strs[96]);
sfmt_t sfmt;
FILE* logfile = NULL;
// mostly-safe sprintf buffer. warning: do NOT put allele codes or
// arbitrary-length lists in here.
char logbuf[MAXLINELEN * 2];
uint32_t g_debug_on = 0;
uint32_t g_log_failed = 0;
uintptr_t g_sample_ct;
uint32_t g_thread_ct;
uint32_t aligned_malloc(uintptr_t** aligned_pp, uintptr_t size) {
#ifdef __LP64__
// Avoid random segfaults on 64-bit machines which have 8-byte- instead of
// 16-byte-aligned malloc(). (Slightly different code is needed if malloc()
// does not even guarantee 8-byte alignment.)
uintptr_t* malloc_ptr = (uintptr_t*)malloc(size + 16);
if (!malloc_ptr) {
return 1;
}
*aligned_pp = (uintptr_t*)((((uintptr_t)malloc_ptr) + 16) & (~(15 * ONELU)));
(*aligned_pp)[-1] = (uintptr_t)malloc_ptr;
#else
// no SSE2 concerns here
*aligned_pp = (uintptr_t*)malloc(size);
if (!(*aligned_pp)) {
return 1;
}
#endif
return 0;
}
void aligned_free(uintptr_t* aligned_pp) {
#ifdef __LP64__
free((uintptr_t*)(aligned_pp[-1]));
#else
free(aligned_pp);
#endif
}
uint32_t push_ll_str(Ll_str** ll_stack_ptr, const char* ss) {
uint32_t slen = strlen(ss);
Ll_str* new_ll_str = (Ll_str*)malloc(sizeof(Ll_str) + slen + 1);
if (!new_ll_str) {
return 1;
}
new_ll_str->next = *ll_stack_ptr;
memcpy(new_ll_str->ss, ss, slen + 1);
*ll_stack_ptr = new_ll_str;
return 0;
}
void logstr(const char* ss) {
if (!g_debug_on) {
fputs(ss, logfile);
if (ferror(logfile)) {
printf("\nWarning: Logging failure on:\n%s\nFurther logging will not be attempted in this run.\n", ss);
g_log_failed = 1;
}
} else {
if (g_log_failed) {
fputs(ss, stdout);
fflush(stdout);
} else {
fputs(ss, logfile);
if (ferror(logfile)) {
printf("\nError: Debug logging failure. Dumping to standard output:\n%s", ss);
g_log_failed = 1;
} else {
fflush(logfile);
}
}
}
}
void logprint(const char* ss) {
logstr(ss);
fputs(ss, stdout);
}
void logprintb() {
logstr(logbuf);
fputs(logbuf, stdout);
}
void wordwrap(char* ss, uint32_t suffix_len) {
// This should have been written eons ago.
// Input: A null-terminated string with no intermediate newlines. If
// suffix_len is zero, there should be a terminating \n; otherwise,
// the last character should be a space.
// Effect: Spaces are replaced with newlines in a manner that plays well with
// 80 column terminal windows. (Multi-space blocks are never
// collapsed.)
char* token_start = ss;
char* line_end = &(ss[79]);
char* token_end;
while (1) {
while (*token_start == ' ') {
token_start++;
}
if (token_start > line_end) {
do {
*line_end = '\n';
line_end = &(line_end[80]);
} while (token_start > line_end);
}
token_end = strchr(token_start, ' ');
if (!token_end) {
if (&(token_start[79]) == line_end) {
return;
}
token_end = strchr(token_start, '\0');
if (!suffix_len) {
if (token_end <= &(line_end[1])) {
// okay if end-of-string is one past the end, because function
// assumes last character is \n in suffix_len == 0 case (might want
// to add a debug option to enforce that)
return;
}
} else {
if (&(token_end[suffix_len]) <= line_end) {
return;
}
// because of terminal space assumption, token_start actually points
// to the end of the string
}
token_start[-1] = '\n';
return;
}
if (token_end > line_end) {
if (&(token_start[79]) != line_end) {
token_start[-1] = '\n';
line_end = &(token_start[79]);
if (token_end > line_end) {
// single really long token, can't do anything beyond putting it on
// its own line
*token_end = '\n';
line_end = &(token_end[80]);
}
} else {
// single really long token, *and* previous token was either
// nonexistent or long
*token_end = '\n';
line_end = &(token_end[80]);
}
}
token_start = &(token_end[1]);
}
}
int32_t fopen_checked(FILE** target_ptr, const char* fname, const char* mode) {
*target_ptr = fopen(fname, mode);
if (!(*target_ptr)) {
LOGPRINTFWW(errstr_fopen, fname);
return -1;
}
return 0;
}
int32_t fwrite_checked(const void* buf, size_t len, FILE* outfile) {
while (len > 0x7ffe0000) {
// OS X can't perform >2GB writes
fwrite(buf, 1, 0x7ffe0000, outfile);
buf = &(((unsigned char*)buf)[0x7ffe0000]);
len -= 0x7ffe0000;
}
fwrite(buf, 1, len, outfile);
return ferror(outfile);
}
int32_t gzopen_checked(gzFile* target_ptr, const char* fname, const char* mode) {
*target_ptr = gzopen(fname, mode);
if (!(*target_ptr)) {
LOGPRINTFWW(errstr_fopen, fname);
return -1;
}
return 0;
}
// manually managed, very large stack
unsigned char* wkspace_base;
uintptr_t wkspace_left;
unsigned char* wkspace_alloc(uintptr_t size) {
unsigned char* retval;
if (wkspace_left < size) {
return NULL;
}
size = CACHEALIGN(size);
retval = wkspace_base;
wkspace_base += size;
wkspace_left -= size;
return retval;
}
void wkspace_reset(void* new_base) {
uintptr_t freed_bytes = wkspace_base - (unsigned char*)new_base;
wkspace_base = (unsigned char*)new_base;
wkspace_left += freed_bytes;
}
void wkspace_shrink_top(void* rebase, uintptr_t new_size) {
uintptr_t freed_bytes = ((uintptr_t)(wkspace_base - ((unsigned char*)rebase))) - CACHEALIGN(new_size);
wkspace_base -= freed_bytes;
wkspace_left += freed_bytes;
}
uint32_t match_upper(char* ss, const char* fixed_str) {
// Returns whether uppercased ss matches nonempty fixed_str. Assumes
// fixed_str contains nothing but letters and a null terminator.
char cc = *fixed_str++;
do {
if ((((unsigned char)(*ss++)) & 0xdf) != ((unsigned char)cc)) {
return 0;
}
cc = *fixed_str++;
} while (cc);
return !(*ss);
}
uint32_t match_upper_nt(char* ss, const char* fixed_str, uint32_t ct) {
do {
if ((((unsigned char)(*ss++)) & 0xdf) != ((unsigned char)(*fixed_str++))) {
return 0;
}
} while (--ct);
return 1;
}
uint32_t scan_posint_capped(char* ss, uint32_t* valp, uint32_t cap_div_10, uint32_t cap_mod_10) {
// Reads an integer in [1, cap]. Assumes first character is nonspace. Has
// the overflow detection atoi() lacks.
// A funny-looking div_10/mod_10 interface is used since the cap will usually
// be a constant, and we want the integer division/modulus to occur at
// compile time.
// '0' has ascii code 48
uint32_t val = (uint32_t)((unsigned char)*ss) - 48;
uint32_t cur_digit;
if (val < 10) {
while (1) {
scan_posint_capped_main_loop:
cur_digit = (uint32_t)((unsigned char)(*(++ss))) - 48;
if (cur_digit >= 10) {
if (val) {
*valp = val;
return 0;
}
return 1;
}
// avoid integer overflow in middle of computation
if ((val >= cap_div_10) && ((val > cap_div_10) || (cur_digit > cap_mod_10))) {
return 1;
}
val = val * 10 + cur_digit;
}
} else if (val == 0xfffffffbU) {
// permit leading '+' (ascii 43), but not '++' or '+-'
val = (uint32_t)((unsigned char)(*(++ss))) - 48;
if (val < 10) {
goto scan_posint_capped_main_loop;
}
}
return 1;
}
uint32_t scan_uint_capped(char* ss, uint32_t* valp, uint32_t cap_div_10, uint32_t cap_mod_10) {
// Reads an integer in [0, cap]. Assumes first character is nonspace.
uint32_t val = (uint32_t)((unsigned char)*ss) - 48;
uint32_t cur_digit;
if (val < 10) {
while (1) {
scan_uint_capped_main_loop:
cur_digit = (uint32_t)((unsigned char)(*(++ss))) - 48;
if (cur_digit >= 10) {
*valp = val;
return 0;
}
if ((val >= cap_div_10) && ((val > cap_div_10) || (cur_digit > cap_mod_10))) {
return 1;
}
val = val * 10 + cur_digit;
}
}
// '-' has ascii code 45, so unsigned 45 - 48 = 0xfffffffdU
ss++;
if (val != 0xfffffffdU) {
if (val == 0xfffffffbU) {
val = (uint32_t)((unsigned char)(*ss)) - 48;
if (val < 10) {
goto scan_uint_capped_main_loop;
}
}
return 1;
}
// accept "-0", "-00", etc.
if (*ss != '0') {
return 1;
}
while (*(++ss) == '0');
*valp = 0;
return ((uint32_t)((unsigned char)(*ss)) - 48) < 10;
}
uint32_t scan_int_abs_bounded(char* ss, int32_t* valp, uint32_t bound_div_10, uint32_t bound_mod_10) {
// Reads an integer in [-bound, bound]. Assumes first character is nonspace.
uint32_t val = (uint32_t)((unsigned char)*ss) - 48;
int32_t sign = 1;
uint32_t cur_digit;
if (val < 10) {
while (1) {
scan_int_abs_bounded_main_loop:
cur_digit = (uint32_t)((unsigned char)(*(++ss))) - 48;
if (cur_digit >= 10) {
*valp = sign * ((int32_t)val);
return 0;
}
if ((val >= bound_div_10) && ((val > bound_div_10) || (cur_digit > bound_mod_10))) {
return 1;
}
val = val * 10 + cur_digit;
}
}
if (val == 0xfffffffdU) {
sign = -1;
} else if (val != 0xfffffffbU) {
return 1;
}
val = (uint32_t)((unsigned char)(*(++ss))) - 48;
if (val < 10) {
goto scan_int_abs_bounded_main_loop;
}
return 1;
}
uint32_t scan_posintptr(char* ss, uintptr_t* valp) {
// Reads an integer in [1, 2^BITCT - 1]. Assumes first character is
// nonspace.
uintptr_t val = (uint32_t)((unsigned char)*ss) - 48;
uintptr_t cur_digit;
if (val < 10) {
while (1) {
scan_posintptr_main_loop:
cur_digit = (uint32_t)((unsigned char)(*(++ss))) - 48;
if (cur_digit >= 10) {
if (val) {
*valp = val;
return 0;
}
return 1;
}
if ((val >= (~ZEROLU) / 10) && ((val > (~ZEROLU) / 10) || (cur_digit > (~ZEROLU) % 10))) {
return 1;
}
val = val * 10 + cur_digit;
}
} else if (val == 0xfffffffbU) {
val = (uint32_t)((unsigned char)(*(++ss))) - 48;
if (val < 10) {
goto scan_posintptr_main_loop;
}
}
return 1;
}
/*
uint32_t scan_uintptr(char* ss, uintptr_t* valp) {
// [0, 2^BITCT - 1].
uintptr_t val = (uint32_t)((unsigned char)*ss) - 48;
uintptr_t cur_digit;
if (val < 10) {
while (1) {
scan_uintptr_main_loop:
cur_digit = (uint32_t)((unsigned char)(*(++ss))) - 48;
if (cur_digit >= 10) {
*valp = val;
return 0;
}
if ((val >= (~ZEROLU) / 10) && ((val > (~ZEROLU) / 10) || (cur_digit > (~ZEROLU) % 10))) {
return 1;
}
val = val * 10 + cur_digit;
}
}
ss++;
if (val != 0xfffffffdU) {
if (val == 0xfffffffbU) {
val = (uint32_t)((unsigned char)(*ss)) - 48;
if (val < 10) {
goto scan_uintptr_main_loop;
}
}
return 1;
}
if (*ss != '0') {
return 1;
}
while (*(++ss) == '0');
*valp = 0;
return ((uint32_t)((unsigned char)(*ss)) - 48) < 10;
}
*/
uint32_t scan_two_doubles(char* ss, double* val1p, double* val2p) {
char* ss2;
*val1p = strtod(ss, &ss2);
if (ss == ss2) {
return 1;
}
ss = skip_initial_spaces(ss2);
*val2p = strtod(ss, &ss2);
return (ss == ss2)? 1 : 0;
}
int32_t scan_token_ct_len(FILE* infile, char* buf, uintptr_t half_bufsize, uintptr_t* token_ct_ptr, uintptr_t* max_token_len_ptr) {
// buf must be of size >= (2 * half_bufsize + 2)
// max_token_len includes trailing null
uintptr_t full_bufsize = half_bufsize * 2;
uintptr_t curtoklen = 0;
uintptr_t token_ct = *token_ct_ptr;
uintptr_t max_token_len = *max_token_len_ptr;
char* midbuf = &(buf[half_bufsize]);
char* bufptr;
char* bufptr2;
char* buf_end;
uintptr_t bufsize;
while (1) {
if (fread_checked(midbuf, half_bufsize, infile, &bufsize)) {
return RET_READ_FAIL;
}
if (!bufsize) {
if (curtoklen) {
// corner case
if (curtoklen >= max_token_len) {
max_token_len = curtoklen + 1;
}
token_ct++;
}
break;
}
buf_end = &(midbuf[bufsize]);
*buf_end = ' ';
buf_end[1] = '0';
bufptr = &(buf[half_bufsize - curtoklen]);
bufptr2 = midbuf;
if (curtoklen) {
goto scan_token_ct_len_tok_start;
}
while (1) {
while (*bufptr <= ' ') {
bufptr++;
}
if (bufptr >= buf_end) {
curtoklen = 0;
break;
}
bufptr2 = &(bufptr[1]);
scan_token_ct_len_tok_start:
while (*bufptr2 > ' ') {
bufptr2++;
}
curtoklen = (uintptr_t)(bufptr2 - bufptr);
if ((bufptr2 == buf_end) && (buf_end == &(buf[full_bufsize]))) {
if (curtoklen >= half_bufsize) {
return RET_INVALID_FORMAT;
}
break;
}
if (curtoklen >= max_token_len) {
if (curtoklen >= half_bufsize) {
return RET_INVALID_FORMAT;
}
max_token_len = curtoklen + 1;
}
token_ct++;
bufptr = &(bufptr2[1]);
}
}
if (!feof(infile)) {
return RET_READ_FAIL;
}
*max_token_len_ptr = max_token_len;
*token_ct_ptr = token_ct;
return 0;
}
int32_t read_tokens(FILE* infile, char* buf, uintptr_t half_bufsize, uintptr_t token_ct, uintptr_t max_token_len, char* token_name_buf) {
// buf must be of size >= (2 * half_bufsize + 2).
// max_token_len includes trailing null
uintptr_t full_bufsize = half_bufsize * 2;
uintptr_t curtoklen = 0;
uintptr_t token_idx = 0;
char* midbuf = &(buf[half_bufsize]);
char* bufptr = midbuf;
char* bufptr2;
char* bufptr3;
char* buf_end;
uintptr_t bufsize;
while (1) {
if (fread_checked(midbuf, half_bufsize, infile, &bufsize)) {
return RET_READ_FAIL;
}
if (!bufsize) {
if (curtoklen) {
if (token_idx + 1 == token_ct) {
memcpyx(&(token_name_buf[token_idx * max_token_len]), bufptr, curtoklen, '\0');
return 0;
}
}
// something very strange has to happen to get here
return RET_READ_FAIL;
}
buf_end = &(midbuf[bufsize]);
*buf_end = ' ';
buf_end[1] = '0';
bufptr2 = midbuf;
if (curtoklen) {
goto read_tokens_tok_start;
}
while (1) {
while (*bufptr <= ' ') {
bufptr++;
}
if (bufptr >= buf_end) {
curtoklen = 0;
bufptr = midbuf;
break;
}
bufptr2 = &(bufptr[1]);
read_tokens_tok_start:
while (*bufptr2 > ' ') {
bufptr2++;
}
curtoklen = (uintptr_t)(bufptr2 - bufptr);
if ((bufptr2 == buf_end) && (buf_end == &(buf[full_bufsize]))) {
bufptr3 = &(buf[half_bufsize - curtoklen]);
memcpy(bufptr3, bufptr, curtoklen);
bufptr = bufptr3;
break;
}
memcpyx(&(token_name_buf[token_idx * max_token_len]), bufptr, curtoklen, '\0');
if (++token_idx == token_ct) {
return 0;
}
bufptr = &(bufptr2[1]);
}
}
}
int32_t gzputs_w4(gzFile gz_outfile, const char* ss) {
if (!ss[1]) {
if (gzputs(gz_outfile, " ") == -1) {
return -1;
}
return gzputc(gz_outfile, ss[0]);
}
if (!ss[2]) {
if (gzputs(gz_outfile, " ") == -1) {
return -1;
}
} else if (!ss[3]) {
if (gzputc(gz_outfile, ' ') == -1) {
return -1;
}
}
return gzputs(gz_outfile, ss);
}
int32_t get_next_noncomment(FILE* fptr, char** lptr_ptr, uintptr_t* line_idx_ptr) {
char* lptr;
do {
if (!fgets(tbuf, MAXLINELEN, fptr)) {
return -1;
}
*line_idx_ptr += 1;
lptr = skip_initial_spaces(tbuf);
} while (is_eoln_or_comment(*lptr));
*lptr_ptr = lptr;
return 0;
}
int32_t get_next_noncomment_excl(FILE* fptr, char** lptr_ptr, uintptr_t* line_idx_ptr, uintptr_t* marker_exclude, uintptr_t* marker_uidx_ptr) {
while (!get_next_noncomment(fptr, lptr_ptr, line_idx_ptr)) {
if (!is_set_ul(marker_exclude, *marker_uidx_ptr)) {
return 0;
}
*marker_uidx_ptr += 1;
}
return -1;
}
char* token_end(char* sptr) {
char cc;
if (!sptr) {
return NULL;
}
cc = *sptr;
while (!is_space_or_eoln(cc)) {
cc = *(++sptr);
}
return cc? sptr : NULL;
}
char* token_endl(char* sptr) {
if (!sptr) {
return NULL;
}
while (!is_space_or_eoln(*sptr)) {
sptr++;
}
return sptr;
}
void get_top_two(uint32_t* uint_arr, uintptr_t uia_size, uintptr_t* top_idx_ptr, uintptr_t* second_idx_ptr) {
uintptr_t cur_idx = 2;
uintptr_t top_idx;
uint32_t top_val;
uintptr_t second_idx;
uint32_t second_val;
uintptr_t cur_val;
if (uint_arr[1] > uint_arr[0]) {
top_idx = 1;
} else {
top_idx = 0;
}
second_idx = 1 ^ top_idx;
top_val = uint_arr[top_idx];
second_val = uint_arr[second_idx];
do {
cur_val = uint_arr[cur_idx];
if (cur_val > second_val) {
if (cur_val > top_val) {
second_val = top_val;
second_idx = top_idx;
top_val = cur_val;
top_idx = cur_idx;
} else {
second_val = cur_val;
second_idx = cur_idx;
}
}
} while (++cur_idx < uia_size);
*top_idx_ptr = top_idx;
*second_idx_ptr = second_idx;
}
int32_t intlen(int32_t num) {
int32_t retval;
if (num < 0) {
num = -num;
retval = 2;
} else {
retval = 1;
}
while (num > 9) {
num /= 10;
retval++;
}
return retval;
}
int32_t strcmp_se(char* s_read, const char* s_const, uint32_t len) {
return memcmp(s_read, s_const, len) || (!is_space_or_eoln(s_read[len]));
}
char* next_token(char* sptr) {
if (!sptr) {
return NULL;
}
while ((*sptr != ' ') && (*sptr != '\t')) {
if (!(*sptr)) {
return NULL;
}
sptr++;
}
return skip_initial_spaces(sptr);
}
char* next_token_mult(char* sptr, uint32_t ct) {
if (!sptr) {
return NULL;
}
do {
while ((*sptr != ' ') && (*sptr != '\t')) {
if (!(*sptr)) {
return NULL;
}
sptr++;
}
sptr = skip_initial_spaces(sptr);
} while (--ct);
return sptr;
}
uint32_t count_tokens(const char* bufptr) {
uint32_t token_ct = 0;
while ((*bufptr == ' ') || (*bufptr == '\t')) {
bufptr++;
}
while (!is_eoln_kns(*bufptr)) {
token_ct++;
while (!is_space_or_eoln(*(++bufptr)));
while ((*bufptr == ' ') || (*bufptr == '\t')) {
bufptr++;
}
}
return token_ct;
}
uint32_t count_and_measure_multistr(const char* multistr, uintptr_t* max_slen_ptr) {
// max_slen includes null terminator
// assumes multistr is nonempty
uint32_t ct = 0;
uintptr_t max_slen = *max_slen_ptr;
uintptr_t slen;
do {
slen = strlen(multistr) + 1;
if (slen > max_slen) {
max_slen = slen;
}
multistr = &(multistr[slen]);
ct++;
} while (*multistr);
*max_slen_ptr = max_slen;
return ct;
}
// number-to-string encoders
static const char digit2_table[] = {
"0001020304050607080910111213141516171819"
"2021222324252627282930313233343536373839"
"4041424344454647484950515253545556575859"
"6061626364656667686970717273747576777879"
"8081828384858687888990919293949596979899"};
char* uint32_write(char* start, uint32_t uii) {
// Memory-efficient fast integer writer. (You can do a bit better sometimes
// by using a larger lookup table, but on average I doubt that pays off.)
//
// Originally the arguments were in the other order (was trying to follow
// Google's "inputs first, than outputs" coding style guidelines), but then I
// realized that chained invocation of this function is much easier to read
// if I make the target buffer the first argument.
uint32_t quotient;
if (uii < 1000) {
if (uii < 10) {
*start = '0' + uii;
return &(start[1]);
} else if (uii >= 100) {
quotient = uii / 100;
*start++ = '0' + quotient;
uii -= quotient * 100;
}
return memcpya(start, &(digit2_table[uii * 2]), 2);
} else if (uii < 10000000) {
if (uii >= 100000) {
if (uii < 1000000) {
goto uint32_write_6;
}
quotient = uii / 1000000;
*start++ = '0' + quotient;
goto uint32_write_6b;
} else if (uii < 10000) {
goto uint32_write_4;
}
quotient = uii / 10000;
*start++ = '0' + quotient;
} else {
if (uii >= 100000000) {
quotient = uii / 100000000;
if (uii >= 1000000000) {
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
} else {
*start++ = '0' + quotient;
}
uii -= 100000000 * quotient;
}
quotient = uii / 1000000;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
uint32_write_6b:
uii -= 1000000 * quotient;
uint32_write_6:
quotient = uii / 10000;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
}
uii -= 10000 * quotient;
uint32_write_4:
quotient = uii / 100;
uii -= 100 * quotient;
return memcpya(memcpya(start, &(digit2_table[quotient * 2]), 2), &(digit2_table[uii * 2]), 2);
}
char* int32_write(char* start, int32_t ii) {
if (ii < 0) {
if (ii < -2147483647) {
return memcpya(start, "-2147483648", 11);
}
*start++ = '-';
ii = -ii;
}
return uint32_write(start, (uint32_t)ii);
}
void uint32_write4(char* start, uint32_t uii) {
// Write exactly four digits (padding with zeroes if necessary); useful for
// e.g. floating point encoders.
uint32_t quotient = uii / 100;
uii -= 100 * quotient;
memcpy(memcpya(start, &(digit2_table[quotient * 2]), 2), &(digit2_table[uii * 2]), 2);
}
static inline void uint32_write6(char* start, uint32_t uii) {
uint32_t quotient = uii / 10000;
uint32_write4(memcpya(start, &(digit2_table[quotient * 2]), 2), uii - 10000 * quotient);
}
static inline void uint32_write8(char* start, uint32_t uii) {
uint32_t quotient = uii / 1000000;
uint32_write6(memcpya(start, &(digit2_table[quotient * 2]), 2), uii - 1000000 * quotient);
}
char* int64_write(char* start, int64_t llii) {
int64_t top_digits;
uint32_t bottom_eight;
uint32_t middle_eight;
if (llii < 0) {
if (llii < -9223372036854775807LL) {
// special case, can't be represented positive
return memcpya(start, "-9223372036854775808", 20);
}
*start++ = '-';
llii = -llii;
}
if (llii <= 0xffffffffLL) {
return uint32_write(start, (uint32_t)llii);
}
top_digits = llii / 100000000LL;
bottom_eight = (uint32_t)(llii - (top_digits * 100000000));
if (top_digits <= 0xffffffffLL) {
start = uint32_write(start, (uint32_t)top_digits);
uint32_write8(start, bottom_eight);
return &(start[8]);
}
llii = top_digits / 100000000LL;
middle_eight = (uint32_t)(top_digits - (llii * 100000000));
start = uint32_write(start, (uint32_t)llii);
uint32_write8(start, middle_eight);
uint32_write8(&(start[8]), bottom_eight);
return &(start[16]);
}
char* uint32_writew4(char* start, uint32_t uii) {
// Minimum field width 4.
uint32_t quotient;
if (uii < 1000) {
if (uii < 10) {
memset(start, 32, 3);
start[3] = '0' + uii;
return &(start[4]);
} else if (uii < 100) {
memset(start, 32, 2);
} else {
quotient = uii / 100;
*start = ' ';
start[1] = '0' + quotient;
uii -= quotient * 100;
}
return memcpya(&(start[2]), &(digit2_table[uii * 2]), 2);
} else {
return uint32_write(start, uii);
}
}
char* uint32_writew6(char* start, uint32_t uii) {
uint32_t quotient;
if (uii < 1000) {
if (uii < 10) {
memset(start, 32, 5);
start[5] = '0' + uii;
return &(start[6]);
} else if (uii < 100) {
memset(start, 32, 4);
} else {
memset(start, 32, 3);
quotient = uii / 100;
start[3] = '0' + quotient;
uii -= quotient * 100;
}
return memcpya(&(start[4]), &(digit2_table[uii * 2]), 2);
} else if (uii < 10000000) {
if (uii >= 100000) {
if (uii >= 1000000) {
quotient = uii / 1000000;
*start++ = '0' + quotient;
goto uint32_writew6_6b;
}
goto uint32_writew6_6;
} else if (uii >= 10000) {
*start++ = ' ';
quotient = uii / 10000;
*start++ = '0' + quotient;
} else {
start = memseta(start, 32, 2);
goto uint32_writew6_4;
}
} else {
if (uii >= 100000000) {
quotient = uii / 100000000;
if (uii >= 1000000000) {
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
} else {
*start++ = '0' + quotient;
}
uii -= 100000000 * quotient;
}
quotient = uii / 1000000;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
uint32_writew6_6b:
uii -= 1000000 * quotient;
uint32_writew6_6:
quotient = uii / 10000;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
}
uii -= 10000 * quotient;
uint32_writew6_4:
quotient = uii / 100;
uii -= 100 * quotient;
return memcpya(memcpya(start, &(digit2_table[quotient * 2]), 2), &(digit2_table[uii * 2]), 2);
}
char* uint32_writew7(char* start, uint32_t uii) {
uint32_t quotient;
if (uii < 1000) {
if (uii < 10) {
memset(start, 32, 6);
start[6] = '0' + uii;
return &(start[7]);
} else if (uii < 100) {
memset(start, 32, 5);
} else {
memset(start, 32, 4);
quotient = uii / 100;
start[4] = '0' + quotient;
uii -= quotient * 100;
}
return memcpya(&(start[5]), &(digit2_table[uii * 2]), 2);
} else if (uii < 10000000) {
if (uii >= 100000) {
if (uii >= 1000000) {
quotient = uii / 1000000;
*start++ = '0' + quotient;
goto uint32_writew7_6b;
}
*start++ = ' ';
goto uint32_writew7_6;
} else if (uii >= 10000) {
start = memseta(start, 32, 2);
quotient = uii / 10000;
*start++ = '0' + quotient;
} else {
start = memseta(start, 32, 3);
goto uint32_writew7_4;
}
} else {
if (uii >= 100000000) {
quotient = uii / 100000000;
if (uii >= 1000000000) {
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
} else {
*start++ = '0' + quotient;
}
uii -= 100000000 * quotient;
}
quotient = uii / 1000000;
start = memcpya(start, &(digit2_table[quotient * 2]), 2);
uint32_writew7_6b:
uii -= 1000000 * quotient;
uint32_writew7_6: