-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.c
2819 lines (2341 loc) · 57.3 KB
/
lib.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 "lib.h"
#include <cstring>
/*
new --> calloc and delete --> free:
May 20, 2004:
int** get_cols
free --> delete:
May 20, 2004:
zfree
ttd:
1. in fgets, check for overrun.
--------------------------------------------------------------------------------
cat2 - cat together two strings.
choose(k,n) - n choose k.
drand - random number generator.
entropy - naive estimate of entropy.
entropy2 - naive estimate of second moment of log(p).
get_chars - parse file and return list of character
strings pointing to each element on each line.
get_cols - get columns from file (including stdin).
header_info - get info from headers in ".dat" files.
isnumber - turns string into a number with lots of checking.
kl_divergence - Kullback-Leibler distance between two distributions.
length_string - returns length of \0 terminated string, including \0.
max - finds maximum of array.
min - finds minimum of array.
mmult - multiply matrix times matrix or matrix times vector.
cint - 2-D integer array, using calloc.
cfloat - 2-D float array, using calloc.
cdouble - 2-D double array, using calloc.
newchar - 2-D character array.
newdouble - 2-D double array, using new.
newfloat - 2-D float array.
newint - 2-D integer array.
init_constant - initizlize 1 and 2 D arrays.
mi - computes mutual information
rabs - absolute value.
read_binary - read binary file
read_n - read n lines of a file and return data in kth column.
read_plexon - read plexon file.
readline - read line of file.
parse_c - parse data file -- return pointer to string.
parse_comline - extract info from command line.
s_parse - simple string parse.
unit_no - return cell number in unit format given
input in _either_ plexon or unit format.
write_err - write an error message and exit(1).
*/
char* cat2(char* prefix, char* suffix)
{
/*
cat together two strings and return the result
*/
char* dum = new char[80];
strcpy(dum, prefix);
strcat(dum, suffix);
return dum;
}
int choose(int k, int n)
{
/*
compute n!/(k! (n-k)!)
will get overflow if n and k are too big.
*/
if (k > n) return 0;
int c = 1;
k = k > n/2 ? n-k: k;
for (int i=0; i < k; i++) c *= (n-i);
for (int i=0; i < k; i++) c /= (i+1);
return c;
}
double drand()
{
// return a random number between 0 and 1
// use srand(seed) to start seed.
// 4/4/00: RAND_MAX --> RAND_MAX-1, so 1 cannot be returned.
// 8/4/04: RAND_MAX --> RAND_MAX. why I thought RAND_MAX-1
// would fix this is a mystery. would
// like to go to RAND_MAX+1, but this
// causes overflow and returns negative
// numbers.
return (double) rand()/RAND_MAX;
}
double drand(float x)
{
// return a random number between 0 and x
return x*rand()/RAND_MAX;
}
double drand(float xmin, float xmax)
{
// return a random number between xmin and xmax
return xmin + (xmax-xmin)*rand()/RAND_MAX;
}
double entropy(int n, int* p)
{
/*
overloaded function (int* p --> double s)
compute entropy of a probability distribution, p.
*/
// ---find ntot
float ntot=0;
for (int i=0; i < n; i++) ntot += p[i] > 0 ? p[i] : 0;
double s=0;
for (int i=0; i < n; i++)
s -= p[i] > 0 ? (p[i]/ntot)*log(p[i]/ntot) : 0;
return s/log(2);
}
double entropy(int n, float* p, int norm)
{
/*
overloaded function (float* p --> double s)
compute entropy of a probability distribution, p.
*/
double s=0;
for (int i=0; i < n; i++) s -= p[i] > 0 ? p[i]*log(p[i]) : 0;
if (norm)
{
// normalize probability distribution
double z=0;
for (int i=0; i < n; i++) z += p[i] > 0 ? p[i] : 0;
if (z > 0) s = s/z + log(z);
}
return s/log(2);
}
double entropy(int n, double* p, int norm)
{
/*
overloaded function (double* p --> double s)
compute entropy of a probability distribution, p.
*/
double s=0;
for (int i=0; i < n; i++) s -= p[i] > 0 ? p[i]*log(p[i]) : 0;
if (norm)
{
// normalize probability distribution
double z=0;
for (int i=0; i < n; i++) z += p[i] > 0 ? p[i] : 0;
if (z > 0) s = s/z + log(z);
}
return s/log(2);
}
double entropy2(int n, int* p)
{
/*
overloaded function (int* p --> double s)
compute second moment of log p.
*/
// ---find ntot
float ntot=0;
for (int i=0; i < n; i++) ntot += p[i] > 0 ? p[i] : 0;
double s=0;
for (int i=0; i < n; i++)
s += p[i] > 0 ? (p[i]/ntot)*pow(log(p[i]/ntot), 2) : 0;
return s/(log(2)*log(2));
}
double entropy2(int n, float* p, int norm=0)
{
/*
overloaded function (float* p --> double s)
compute second moment of log p.
*/
double z=1;
if (norm)
{
// normalize probability distribution
double z=0;
for (int i=0; i < n; i++) z += p[i] > 0 ? p[i] : 0;
}
double s=0;
for (int i=0; i < n; i++)
s += p[i] > 0 ? (p[i]/z)*pow(log(p[i]/z), 2) : 0;
return s/(log(2)*log(2));
}
double entropy2(int n, double* p, int norm=0)
{
/*
overloaded function (double* p --> double s)
compute second moment of log p.
*/
double z=1;
if (norm)
{
// normalize probability distribution
double z=0;
for (int i=0; i < n; i++) z += p[i] > 0 ? p[i] : 0;
}
double s=0;
for (int i=0; i < n; i++)
s += p[i] > 0 ? (p[i]/z)*pow(log(p[i]/z), 2) : 0;
return s/(log(2)*log(2));
}
charplus get_chars(FILE* f)
{
/*
read from file f, except skips lines whose first element is #.
delimiter is white space or tab.
RETURNS:
charplus z:
z.x[i][j] - jth element in line i, in char* format
z.m[i] - number of elements on line i
z.n - number of lines. returns 0 if the file
does not exist.
*/
// ---declarations
struct charplus z;
// ---check that file exists. if not, return with z.n = 0.
// reserve space just in case.
if (!f)
{
z.n=0;
z.m = new int[1];
z.x = new char**[1];
z.x[0] = new char*[1];
z.x[0][0] = new char[1];
return z;
}
// reserve space and make a few declarations
char* line = new char[MLL_lib];
int n_elements;
ostrstream m_ptr;
strstream inout;
int line_no=0;
while (fgets(line, MLL_lib, f))
{
inout << line;
line_no++;
}
// ---reserve slightly too much memory, since some lines are
// comments.
z.x = new char**[line_no];
z.n = 0;
for (int i=0; i < line_no; i++)
{
inout.getline(line, MLL_lib);
// parse
int* nw = parse_c(line, n_elements);
// check that there is at least one element on the line
if (n_elements == 0)
{
z.x[z.n] = new char*[1];
z.x[z.n][0] = new char[1];
z.n++;
m_ptr.write((char*) &n_elements, sizeof(n_elements));
}
// check for comment
else if (line[nw[0]] != '#')
{
// ---data exists
// reserve space
z.x[z.n] = new char*[n_elements];
for (int i=0; i < n_elements; i++)
{
z.x[z.n][i] =
new char[length_string(&line[nw[i]])];
strcpy(z.x[z.n][i], &line[nw[i]]);
}
m_ptr.write((char*) &n_elements, sizeof(n_elements));
z.n++;
}
delete [] nw;
}
delete [] line;
z.m = (int*) m_ptr.str();
return z;
}
float** get_cols(int n_col, int* col, int& n, FILE* f, int silent)
{
/*
read from file f. skips lines whose first element is #
INPUT:
n_col - see col below.
col - reads columns col[0], col[1], ..., col[n_col-1].
skips lines with fewer than max(col) elements.
f - file. in calling program:
FILE *f;
f = fopen("filename", "r");
or
f = stdin;
silent - if nonzero, do not announce empty lines
OUTPUT:
n - number of columns
RETURNS:
x - x[n][j] = jth element in column col[n]
*/
// some checking
int err=0;
if (n_col < 1)
{
cerr << "function get_cols: first argument is "
<< "less than 1. code will probably bomb." << endl;
err=1;
}
for (int i=0; i < n_col; i++)
{
if (col[i] < 0)
{
cerr << "function get_cols: col[" << i << "]"
<< " is less than 0. code will probably bomb."
<< endl;
err=1;
}
}
if (err)
{
float** x = new float*[1];
x[0] = new float[1];
return x;
}
// reserve space
float** x = new float*[n_col];
// find maximum column
int colmax = col[0];
for (int i=1; i < n_col; i++)
colmax = col[i] > colmax ? col[i] : colmax;
colmax += 1;
// reserve space and make a few declarations
char* line = new char[MLL_lib];
int line_no=0, n_elements;
ostrstream* x_ptr = new ostrstream[n_col];
n=0;
while (fgets(line, MLL_lib, f))
{
line_no++;
// parse
int* nw = parse_c(line, n_elements);
// check for empty line
if (n_elements == 0) {}
// check for comment
else if (line[nw[0]] == '#') {}
// check for enough elements.
else if (n_elements < colmax)
{
if (!silent)
cerr << "line " << line_no << ": fewer than "
<< colmax << " elements; skipping"
<< endl;
}
else
{
n++;
for (int i=0; i < n_col; i++)
{
float y = atof(&line[nw[col[i]]]);
x_ptr[i].write((char*) &y, sizeof(y));
}
}
delete [] nw;
}
for (int i=0; i < n_col; i++) x[i] = (float*) x_ptr[i].str();
delete [] line;
fclose(f);
return x;
}
int** get_cols(
FILE* f, int& lines, int& n_elements, int flag, int min_elements)
{
/*
read from file f, except skips lines whose first element is #.
INPUT:
f - file to be read from.
flag - determines orientation of array x; see below.
OPTIONAL INPUT:
min_elements - only lines with at least min_elements will
be saved. default=0
OUTPUT:
lines - number of lines
n_elements - number of elements on each line.
RETURNS:
x[i][j] - flag=1: element i, line j.
flag=0: line i, element j.
*/
int** x;
// ---check that file exists. if not, return with lines =
// n_elements=0;
if (!f)
{
lines=n_elements=0;
return x;
}
// ---dummy variable; not used.
char** dat;
ostrstream x_ptr;
ostrstream m_ptr;
parse_file(f, x_ptr, m_ptr, lines, 0, dat, -1.0, ' ', min_elements);
// ---check
if (lines == 0)
{
n_elements=0;
return x;
}
// ---number of elements on each line
int* m = (int*) m_ptr.str();
// ---check that m[i] is the same for all lines. if it isn't,
// return with lines=-1.
n_elements = m[0];
for (int i=1; i < lines; i++) if (m[i] != n_elements)
{
lines=-1;
return x;
}
// ---temporarily store the whole file
double* xtmp = (double*) x_ptr.str();
// ---transfer to x
int cnt=0;
if (flag)
{
// ---x[i][j] is element i, line j
x = cint(n_elements, lines);
for (int i=0; i < lines; i++) for (int j=0; j < n_elements; j++)
x[j][i] = (int) floor(xtmp[cnt++] + 0.5);
}
else
{
// ---x[i][j] is line i, element j
x = cint(lines, n_elements);
for (int i=0; i < lines; i++) for (int j=0; j < n_elements; j++)
x[i][j] = (int) floor(xtmp[cnt++] + 0.5);
}
delete [] xtmp;
fclose(f);
return x;
}
float** get_cols(
FILE* f, int& lines, int& n_elements, float flag, int min_elements)
{
/*
read from file f, except skips lines whose first element is #.
INPUT:
f - file to be read from.
flag - determines orientation of array x; see below.
OPTIONAL INPUT:
min_elements - only lines with at least min_elements will
be saved. default=0
OUTPUT:
lines - number of lines
n_elements - number of elements on each line.
RETURNS:
x[i][j] - flag=1: element i, line j.
flag=0: line i, element j.
*/
float** x;
// ---check that file exists. if not, return with lines =
// n_elements=0;
if (!f)
{
lines=n_elements=0;
return x;
}
// ---dummy variable; not used.
char** dat;
ostrstream x_ptr;
ostrstream m_ptr;
parse_file(f, x_ptr, m_ptr, lines, 0, dat, -1.0, ' ', min_elements);
// ---check
if (lines == 0)
{
n_elements=0;
return x;
}
// ---number of elements on each line
int* m = (int*) m_ptr.str();
// ---check that m[i] is the same for all lines. if it isn't,
// return with lines=-1.
n_elements = m[0];
for (int i=1; i < lines; i++) if (m[i] != n_elements)
{
lines=-1;
return x;
}
// ---temporarily store the whole file
double* xtmp = (double*) x_ptr.str();
// ---transfer to x
int cnt=0;
if (flag)
{
// ---x[i][j] is element i, line j
x = newfloat(n_elements, lines);
for (int i=0; i < lines; i++) for (int j=0; j < n_elements; j++)
x[j][i] = xtmp[cnt++];
}
else
{
// ---x[i][j] is line i, element j
x = newfloat(lines, n_elements);
for (int i=0; i < lines; i++) for (int j=0; j < n_elements; j++)
x[i][j] = xtmp[cnt++];
}
delete [] xtmp;
fclose(f);
return x;
}
double** get_cols(
FILE* f, int& lines, int& n_elements, double flag, int min_elements)
{
/*
read from file f, except skips lines whose first element is #.
INPUT:
f - file to be read from.
flag - determines orientation of array x; see below.
OPTIONAL INPUT:
min_elements - only lines with at least min_elements will
be saved. default=0
OUTPUT:
lines - number of lines
n_elements - number of elements on each line.
RETURNS:
x[i][j] - flag=1: element i, line j.
flag=0: line i, element j.
*/
double** x;
// ---check that file exists. if not, return with lines =
// n_elements=0;
if (!f)
{
lines=n_elements=0;
x = (double**) calloc(1, sizeof(double*));
x[0] = (double*) calloc(1, sizeof(double));
return x;
}
// ---dummy variable; not used.
char** dat;
ostrstream x_ptr;
ostrstream m_ptr;
parse_file(f, x_ptr, m_ptr, lines, 0, dat, -1.0, ' ', min_elements);
// ---check
if (lines == 0)
{
n_elements=0;
return x;
}
// ---number of elements on each line
int* m = (int*) m_ptr.str();
// ---check that m[i] is the same for all lines. if it isn't,
// return with lines=-1.
n_elements = m[0];
for (int i=1; i < lines; i++) if (m[i] != n_elements)
{
lines=-1;
return x;
}
// ---temporarily store the whole file
double* xtmp = (double*) x_ptr.str();
// ---transfer to x
int cnt=0;
if (flag)
{
// ---x[i][j] is element i, line j
x = newdouble(n_elements, lines);
for (int i=0; i < lines; i++) for (int j=0; j < n_elements; j++)
x[j][i] = xtmp[cnt++];
}
else
{
// ---x[i][j] is line i, element j
x = newdouble(lines, n_elements);
for (int i=0; i < lines; i++) for (int j=0; j < n_elements; j++)
x[i][j] = xtmp[cnt++];
}
delete [] xtmp;
fclose(f);
return x;
}
floatplus get_cols(FILE* f)
{
/*
read from file f, except skips lines whose first element is #.
call:
floatplus z = get_cols(stdin);
or (I believe)
floatplus z = get_cols(fopen("filename", "r"));
INPUT:
f - input file.
RETURNS:
floatplus z:
z.x[i][j] - jth element in line i
z.m[i] - number of elements on line i
z.n - number of lines. returns 0 if the file
does not exist.
*/
// ---declarations
struct floatplus z;
// ---check that file exists. if not, return with z.n = 0.
// reserve space just in case.
if (!f)
{
z.n=0;
z.m = new int[1];
z.x = new double*[1];
z.x[0] = new double[1];
z.ndat=1;
z.ddat = new double[1];
return z;
}
// reserve space and make a few declarations
ostrstream x_ptr;
ostrstream m_ptr;
char** dat;
z.ddat = parse_file(f, x_ptr, m_ptr, z.n, 0, dat, 0.0, ' ', 0);
z.m = (int*) m_ptr.str();
// ---temporarily store the whole file
double* x = (double*) x_ptr.str();
// ---transfer to z.x
z.x = newdouble(z.n, z.m);
int cnt=0;
for (int i=0; i < z.n; i++) for (int j=0; j < z.m[i]; j++)
z.x[i][j] = x[cnt++];
delete [] x;
fclose(f);
return z;
}
floatplus get_cols(
FILE* f, int ndat, char** dat, double dflt=-1.0, char sep=' ',
int min_elements)
{
/*
read from file f, except skips lines whose first element is #.
call:
floatplus z = get_cols(stdin);
or (I believe)
floatplus z = get_cols(fopen("filename", "r"));
INPUT:
f - input file.
ndat - number of elements in dat.
dat[i] - element to be matched to comments; see parse_file
and xdat below.
dflt - value returned in z.ddat[i] (see below) if
there is no match with dat[i].
sep - separator; see xdat below.
basically, if, say, the input file contains
the line "# noise_sheaf_length=17",
dat[3]="noise_sheaf_length" and sep = '=',
then z.ddat[3] will equal 17. if the input
file contains the line "# noise_sheaf_length
17", again dat[3]="noise_sheaf_length", but
this time sep = ' ' (the default), then
again z.ddat[3] will equal 17.
min_elements - only lines with at least min_elements will
be saved.
RETURNS:
floatplus z:
z.x[i][j] - jth element in line i
z.m[i] - number of elements on line i
z.n - number of lines. returns 0 if the file
does not exist.
z.ddat - data from comment; see above. if dat[i] is
not found, z.ddat[i] comes back equal to dflt.
ndat - number of elements in z.dat.
*/
// ---declarations
struct floatplus z;
// ---check that file exists. if not, return with z.n = 0.
// reserve space just in case.
if (!f)
{
z.n=0;
z.m = new int[1];
z.x = new double*[1];
z.x[0] = new double[1];
z.ndat=1;
z.ddat = new double[1];
return z;
}
// reserve space and make a few declarations
ostrstream x_ptr;
ostrstream m_ptr;
z.ddat = parse_file(f, x_ptr, m_ptr, z.n, ndat, dat, dflt,
sep, min_elements);
z.m = (int*) m_ptr.str();
// ---temporarily store the whole file
double* x = (double*) x_ptr.str();
// ---transfer to z.x
z.x = newdouble(z.n, z.m);
int cnt=0;
for (int i=0; i < z.n; i++) for (int j=0; j < z.m[i]; j++)
z.x[i][j] = x[cnt++];
delete [] x;
fclose(f);
return z;
}
int* header_info(FILE* f, int n, char** s, int dflt=-1)
{
/*
get info from header in ".dat" files. assume header has the
form
# ...
...
# string=n
...
where string is a string and n is an integer. stops searching
after all strings are found, or if at the first line _not_
beginning with #.
INPUT:
n - number of strings.
s[i] - a list of strings, i = 0, n-1.
dflt - if string is not found, then dflt is returned.
RETURN:
info[i] - the info pointed to by string. e.g., if
s[3]="noise_random_seed" and the .dat file had the
line "noise_random_seed=5", then info[3] would come
back equal to 5. if noise_random_seed was _not_ in
the .dat file, then info[3] would come back set to
dflt.
*/
int* info = init_constant(n, dflt);
char* line = new char[MLL_lib];
while (fgets(line, MLL_lib, f))
{
// parse
int n_elements;
int* nw = parse_c(line, n_elements);
// ---if there is at least 1 element on the line, check
// that first element is '#'. if not, return.
if (n_elements > 0) if (line[nw[0]] != '#') return info;
// only consider lines with 2 or more elements.
if (n_elements > 1)
{
// ---search for equal sign in second element.
for (int i=0; ; i++)
{
if (line[nw[1]+i] == '\0') break;
else if (line[nw[1]+i] == '=')
{
line[nw[1]+i] = '\0';
// ---look for match
for (int j=0; j < n; j++)
if (!strcmp(s[j], &line[nw[1]]))
info[j]=atoi(&line[nw[1]+i+1]);
break;
}
}
}
delete [] nw;
// ---see if we are all done
int k;
for (k=0; k < n; k++) if (info[k] == dflt) break;
if (k == n) break;
}
delete [] line;
return info;
}
double* parse_file(
FILE* f, ostrstream& x_ptr, ostrstream& m_ptr, int& lines,
int ndat, char** dat, double dflt, char sep=' ', int min_elements=0)
{
/*
parse file
INPUT:
f - input file.
ndat - number of elements in dat.
dat[i] - element to be matched to comments; see parse_file
and xdat below.
dflt - value returned in z.ddat[i] (see below) if
there is no match with dat[i].
sep - separator; see xdat below.
basically, if, say, the input file contains
the line "# noise_sheaf_length=17",
dat[3]="noise_sheaf_length" and sep = '=',
then z.ddat[3] will equal 17. if the input
file contains the line "# noise_sheaf_length
17", again dat[3]="noise_sheaf_length", but
this time sep = ' ' (the default), then
again z.ddat[3] will equal 17.
min_elements - only lines with at least min_elements will
be saved.
OUTPUT:
x_ptr - pointer to data.
m_ptr - point to number of elements on each line.
RETURNS:
ddat - see comments in get_cols above.
*/
char* line = new char[MLL_lib];
int line_no=0, n_elements;
lines=0;
int cnt=0;
int* cdat = init_constant((ndat < 1 ? 1 : ndat), 0);
double* ddat = init_constant((ndat < 1 ? 1 : ndat), dflt);
while (fgets(line, MLL_lib, f))
{
line_no++;
// parse
int* nw = parse_c(line, n_elements);
// check that there is at least one element on the line
if (n_elements == 0)
{
if (n_elements >= min_elements)
{
lines++;
m_ptr.write((char*) &n_elements,
sizeof(n_elements));
}
}
else if (line[nw[0]] != '#')
{
// data exists and is not a comment
for (int i=0; i < n_elements; i++)
{
double y = atof(&line[nw[i]]);
x_ptr.write((char*) &y, sizeof(y));
}
lines++;
m_ptr.write((char*) &n_elements, sizeof(n_elements));
}
else if (line[nw[0]] == '#' && cnt < ndat)
{
cnt += xdat(ndat, cdat, dat, sep, n_elements, nw, line,
ddat);
}
delete [] nw;
}
delete [] line;
return ddat;
}
int xdat(int ndat, int* cdat, char** dat, char sep,
int n_line, int* nw, char* line, double* ddat)
{
/*
see if any of the elements in line appear in dat.
INPUT:
ndat - number of elements in dat.
cdat[i] - if 1, i^th element has been found; if 1, it has not.
once an element is found, it is not searched for again.
dat[i] - i^th elements.
sep - separater. two cases:
sep = ' ': compare each element in line to each