This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetio.c
3138 lines (2602 loc) · 93.7 KB
/
netio.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
// netio.c - networks to and from file
/*
This file is part of Tripover, a broad-search journey planner.
Copyright (C) 2014-2017 Joris van der Geer.
*/
/*
Functions to read and write tripover internal networks, utility functions to write diagnostics
*/
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "base.h"
#include "cfg.h"
#include "mem.h"
#include "math.h"
#include "util.h"
#include "time.h"
static ub4 msgfile;
#include "msg.h"
#include "iadr.h"
#include "netbase.h"
#include "netio.h"
static ub4 pdfscale_lat = 1200;
static ub4 pdfscale_lon = 1200;
static ub4 lat2pdf(ub4 lat,ub4 lolat,ub4 dlat) { return (lat - lolat) * pdfscale_lat / dlat; }
static ub4 lon2pdf(ub4 lon,ub4 lolon,ub4 dlon) { return (lon - lolon) * pdfscale_lon / dlon; }
static const ub4 pdfmaxports = 1000;
static const ub4 pdfmaxhops = 1000;
static const ub4 daymin = 60 * 24;
static const ub4 maxtripdur = 10; // in days
static ub4 timecntlimit = hi32;
// static const ub4 hop2watch = hi32;
// static const ub4 rtid2watch = hi32;
#include "watch.h"
/* tripover external format: easy manual editing, typical from single gtfs
files contain tab-separated columns
file ports.txt:
# starts comment
letters introduce commands:
s <latlon scale,ofs> scale and offset for lat and lon
numbers start a regular 'port' line:
id name lat lon
numbers default to hexadecimal. prefix . for decimal.
file hops.txt
patterned after ports.txt
commands:
# starts comment
tab introduces commands:
t from to # overal time validity of following block
any char except tab and newline introduce a regular hop line:
name id dport aport route.seq dow.hhmm.rep.t0.t1.dur dow.hhmm.rep ... # mm implies rep=60min
dport,aport refers to port.id above
route refers to route.id below
seq = hops from route start
dow = days of week
hhmm = hour:min
rep = repetition interval
t0,t1 is start and end of repetition
dur = duration
routes.txt todo
id name hopcnt
...
tripover internal format todo: meant for larger nets. typ combine sets of external
*/
enum stopopts { Stopopt_child = 1, Stopopt_parent = 2, Stopopt_geo = 4 };
static const char *kindnames[Kindcntb] = { "unknown","air int","air dom","rail","bus","ferry","taxi","walk" };
enum extresult { Next, Newitem, Newcmd, Eof, Parserr };
static int memeq(const char *s,const char *q,ub4 n) { return !memcmp(s,q,n); }
// count non-empty and non-comment lines
static ub4 linecnt(const char *name,const char *buf, ub8 len)
{
ub8 pos = 0;
ub4 cnt = 0;
const char nl = '\n';
while (pos < len) {
if (buf[pos] == '#' || (pos + 1 < len && buf[pos] == '.' && buf[pos+1] != '.') ) { while (pos < len && buf[pos] != nl) pos++; }
else if (buf[pos] == nl) pos++;
else {
cnt++;
while (pos < len && buf[pos] != nl) pos++;
}
}
if (len && buf[len-1] != nl) warning(0,"%s has unterminated last line",name);
info(0,"%s: %u data lines", name,cnt);
return cnt;
}
static ub1 hexmap[256];
static void mkhexmap(void)
{
char c;
memset(hexmap,0xff,256);
for (c = '0'; c <= '9'; c++) hexmap[(ub4)c] = (ub1)(c - '0');
for (c = 'a'; c <= 'f'; c++) hexmap[(ub4)c] = (ub1)(c + 10 - 'a');
for (c = 'A'; c <= 'F'; c++) hexmap[(ub4)c] = (ub1)(c + 10 - 'A');
hexmap[9] = 0x20;
hexmap[0x0a] = 0xfe;
hexmap[0x20] = 0x20;
}
#define Maxval (64 * 1024)
#define Maxsval (16 * 1024)
#define Maxname 256
enum extstates { Init,Out,Val0,Hex1,Dec0,Dec1,Val1,Name,Cmd0,Fls};
struct extfmt {
struct myfile mf;
ub8 pos;
enum extstates state;
ub4 linno,colno;
int iscmd;
char name[Maxname];
char prvname[Maxname];
ub4 namelen;
ub4 radix;
ub4 val,xval,valndx,vals[Maxval];
ub4 svalndx,svalpos;
char svals[Maxval * Maxname];
ub4 svallens[Maxval];
};
static enum extresult __attribute__ ((format (printf,6,7))) parserr(ub4 fln,const char *fname,ub4 linno,ub4 colno,struct extfmt *ef,const char *fmt, ...)
{
va_list ap;
char buf[1024];
ub4 pos,len = sizeof(buf);
if (ef) info(0,"after '%s'",ef->prvname);
pos = fmtstring(buf,"%s.%u.%u: parse error: ",fname,linno,colno);
if (fmt) {
va_start(ap,fmt);
myvsnprintf(buf,pos,len,fmt,ap);
va_end(ap);
}
errorfln(fln,0,FLN,"%s",buf);
nomsgpfx();
return Parserr;
}
static int __attribute__ ((format (printf,5,6))) inerr(ub4 fln,const char *fname,ub4 linno,ub4 colno,const char *fmt, ...)
{
va_list ap;
char buf[1024];
ub4 pos,len = sizeof(buf);
pos = fmtstring(buf,"%s.%u.%u: ",fname,linno,colno);
if (fmt) {
va_start(ap,fmt);
myvsnprintf(buf,pos,len,fmt,ap);
va_end(ap);
}
errorfln(fln,0,FLN,"%s",buf);
return msgprefix(1,NULL);
}
static int __attribute__ ((format (printf,5,6))) parsewarn(ub4 fln,const char *fname,ub4 linno,ub4 colno,const char *fmt, ...)
{
va_list ap;
char buf[1024];
ub4 pos,len = sizeof(buf);
pos = fmtstring(buf,"%s.%u.%u: ",fname,linno,colno);
if (fmt) {
va_start(ap,fmt);
myvsnprintf(buf,pos,len,fmt,ap);
va_end(ap);
}
return warnfln(fln,Iter,"%s",buf);
}
/* basic tab-separated ints parser.
first item is name, rest are integers
string starting with . is a command, double dot to escape a dot
# is comment
. prefix for decimal
no prefix to use default radix
.. switch radix to dec
x switch radix to hex
*/
static enum extresult nextchar(struct extfmt *ef)
{
char *fname,*name,c;
ub8 pos,len;
ub4 linno,colno,x,valndx,svalndx,svalpos,namelen;
ub4 val,xval,*vals;
ub4 namemax = Maxname - 2;
ub4 maxval = Maxval - 1;
int newitem,iscmd;
ub4 radix;
enum extstates state;
char *sp;
if (globs.sigint) return Parserr;
len = ef->mf.len;
pos = ef->pos;
if (pos >= len) return Eof;
// state
state = ef->state;
valndx = ef->valndx;
svalndx = ef->svalndx;
svalpos = ef->svalpos;
val = ef->val;
xval = ef->xval;
linno = ef->linno + 1;
colno = ef->colno;
namelen = ef->namelen;
iscmd = ef->iscmd;
radix = ef->radix;
// convenience
name = ef->name;
fname = ef->mf.name;
vals = ef->vals;
c = ef->mf.buf[pos];
ef->pos = pos + 1;
newitem = 0;
// infocc(linno == 135,Noiter,"state %u c %c",state,c);
switch(state) {
case Init:
linno = 1;
radix = 16; Fallthrough
case Out:
valndx = svalndx = svalpos = 0;
iscmd = 0;
switch (c) {
case '#': state = Fls; break;
case '\t': valndx = namelen = 0; vals[0] = hi32; state = Val0; break;
case '\n': break;
case ' ': break;
case '.': iscmd = 1; state = Cmd0; break;
default: name[0] = c; namelen = 1; state = Name;
}
break;
case Cmd0:
switch (c) {
case '#': state = Fls; break;
case '\t': valndx = namelen = 0; vals[0] = hi32; state = Val0; break;
case '\n': return parserr(FLN,fname,linno,colno,ef,"unexpected newline");
case '.': iscmd = 0; name[0] = '.'; namelen = 1; state = Name; break;
case '\\': state = Name; namelen = 0; break;
default: name[0] = c; namelen = 1; state = Name;
}
break;
case Name:
if (c == '\t') {
if (namelen + 2 < namemax) name[namelen] = 0;
valndx = svalndx = svalpos = 0; state = Val0;
vals[0] = hi32;
}
else if (c == '\n') return parserr(FLN,fname,linno,colno,ef,"missing args");
else if (namelen + 2 < namemax) name[namelen++] = c;
else if (name[namemax] != '!') {
parsewarn(FLN,fname,linno,colno,"name exceeds %u",namemax);
namelen = truncutf8(name,namelen);
name[namemax] = '!';
}
break;
case Val0:
switch(c) {
case '#': newitem = 1; state = Fls; break;
case '\n': newitem = 1; state = Out; break;
case '\t': case ' ': break;
case '.': state = Dec0; break;
case 'x': radix = 16; break;
case '\'':
if (svalndx + 1 >= Maxsval) return parserr(FLN,fname,linno,colno,ef,"exceeded %u strings",Maxsval);
ef->svallens[svalndx] = 0;
sp = ef->svals + svalndx * Maxname;
svalpos = 0;
*sp = 0;
state = Val1;
break;
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
if (radix == 16) {
val = hexmap[(ub4)c];
state = Hex1;
} else {
val = c - '0';
state = Dec1;
}
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
val = hexmap[(ub4)c];
state = Hex1;
break;
default: return parserr(FLN,fname,linno,colno,ef,"expected digit, found '%c'",c);
}
break;
case Val1: // generic string
switch(c) {
// case '#': newitem = 1; state = Fls; break;
case '\n':
newitem = 1; state = Out;
ef->svallens[svalndx] = svalpos;
svalndx++; svalpos = 0;
break;
case '\t':
ef->svallens[svalndx] = svalpos;
svalndx++; svalpos = 0;
state = Val0;
break;
default:
if (svalpos + 1 >= Maxname) return parserr(FLN,fname,linno,colno,ef,"exceeding %u string len",svalpos);
sp = ef->svals + svalndx * Maxname;
sp[svalpos++] = c;
sp[svalpos] = 0;
break;
}
break;
case Hex1: // hex number
if (valndx + 1 >= maxval) return parserr(FLN,fname,linno,colno,ef,"exceeding %u values",valndx);
x = hexmap[(ub4)c];
if (x < 16) val = (val << 4) | x;
else if (x == 0x20) {
vals[valndx++] = val;
vals[valndx] = hi32;
state = Val0;
} else if (x == 0xfe) { // newline
vals[valndx++] = val;
vals[valndx] = hi32;
newitem = 1;
state = Out;
} else return parserr(FLN,fname,linno,colno,ef,"expected whitespace after number, found %c",c);
break;
case Dec0: // dec number
if (c == '\t') {
if (valndx >= maxval) return parserr(FLN,fname,linno,colno,ef,"exceeding %u values",valndx);
vals[valndx++] = 0;
vals[valndx] = hi32;
state = Val0;
} else if (c == '.') radix = 10;
// else if (c >= 'A' && c <= 'Z') { val = 0; xval = (ub4)c << 24; state = Dec1; }
else if (c < '0' || c > '9') return parserr(FLN,fname,linno,colno,ef,"expected decimal digit or letter, found '%c'",c);
else { val = c - '0'; xval = 0; state = Dec1; }
break;
case Dec1: // dec number
if (c == '\t' || c == ' ') {
if (valndx >= maxval) return parserr(FLN,fname,linno,colno,ef,"exceeding %u values",valndx);
vals[valndx++] = val | xval;
vals[valndx] = hi32;
state = Val0;
}
// else if (c >= 'A' && c <= 'Z') xval |= (ub4)c << 16;
else if (c >= '0' && c <= '9') {
if (val > 400 * 1000 * 1000) parsewarn(FLN,fname,linno,colno,"decimal digit overflow at %u",val);
else val = (val * 10) + (c - '0');
} else if (c == '\n') {
vals[valndx++] = val | xval;
vals[valndx] = hi32;
newitem = 1;
state = Out;
} else return parserr(FLN,fname,linno,colno,ef,"col %u: expected decimal digit or letter, found '%c' radix %u",valndx,c,radix);
break;
case Fls:
if (c == '\n') { state = Out; } break;
}
if (c == '\n') { linno++; colno = 1; }
else colno++;
ef->state = state;
ef->valndx = valndx;
ef->svalndx = svalndx;
ef->svalpos = svalpos;
ef->val = val;
ef->xval = xval;
ef->linno = linno - 1;
ef->colno = colno;
ef->namelen = namelen;
ef->iscmd = iscmd;
ef->radix = radix;
if (namelen) strcpy(ef->prvname,ef->name);
else *ef->prvname = 0;
if (newitem) return iscmd ? Newcmd : Newitem;
else return Next;
}
struct cmdvars {
const char *name;
ub4 namelen;
ub4 nval;
ub4 *pval;
char *sval;
ub4 linno;
};
static ub4 sumtimes;
static ub4 rawtripcnt;
static ub4 hitripid;
static ub4 feedstamp,feedlostamp;
static struct cmdvars hopvars[] = {
{"sumtimes",8,1,&sumtimes,NULL,0},
{"trips",5,1,&rawtripcnt,NULL,0},
{"hitrip",6,1,&hitripid,NULL,0},
{"",0,0,NULL,NULL,0}
};
static ub4 timebox[2]; // coded decimal local yyyymmdd inclusive todo timezone
static ub4 dummy;
static ub4 sidrange[2];
static struct cmdvars timevars[] = {
{"timebox",7,2,timebox,NULL,0},
{"dowstart",8,0,&dummy,NULL,0},
{"feedstamp",9,1,&feedstamp,NULL,hi32},
{"feedlostamp",11,1,&feedlostamp,NULL,hi32},
{"sidrange",8,2,sidrange,NULL,0},
{"",0,0,NULL,NULL,0}
};
static ub4 ridrange[2];
static struct cmdvars routevars[] = {
{"ridrange",8,2,ridrange,NULL,0}, // ridcnt, hirrid
{"",0,0,NULL,NULL,0}
};
static ub4 tidrange[2];
static struct cmdvars ridevars[] = {
{"tidrange",8,2,tidrange,NULL,0}, // tidcnt, hirtid
{"",0,0,NULL,NULL,0}
};
static char portcmt[Maxname];
static ub4 pc_max = Maxname;
static ub4 latscale,lonscale;
static struct cmdvars portvars[] = {
{"comment",7,1,&pc_max,portcmt,hi32},
{"latscale",8,1,&latscale,NULL,0},
{"lonscale",8,1,&lonscale,NULL,0},
{"",0,0,NULL,NULL,0}
};
static int docmd(struct cmdvars *cvs,struct extfmt *ef,const char *fname)
{
struct cmdvars *cv = cvs;
ub4 namelen = ef->namelen;
ub4 valcnt = ef->valndx;
ub4 svalcnt = ef->svalndx;
ub4 linno = ef->linno;
ub4 *vals = ef->vals;
char *svals = ef->svals;
char *name = ef->name;
ub4 nval,n,maxlen;
while (cv->namelen) {
if (namelen == cv->namelen && memeq(name,cv->name,namelen)) {
if (cv->linno && cv->linno != hi32) return parsewarn(FLN,fname,linno,0,"ignore %s previously defined at %u",name,cv->linno);
nval = cv->nval;
if (valcnt + svalcnt < nval) return parserr(FLN,fname,linno,0,ef,"%s needs %u arg\as, has %u",name,nval,valcnt);
cv->linno = linno;
if (cv->sval) {
if (svalcnt == 0 || ef->svallens[0] == 0) return parserr(FLN,fname,linno,0,ef,"missing value for cmd %s",name);
maxlen = min(*cv->pval,ef->svallens[0]);
maxlen = min(maxlen,Maxname);
info(Emp,"%s '%.*s'",name,Maxname,svals);
strncpy(cv->sval,svals,maxlen);
return 0;
}
if (valcnt < nval) return parserr(FLN,fname,linno,0,ef,"%s needs %u arg\as, has %u",name,nval,valcnt);
for (n = 0; n < nval; n++) {
error_eq(vals[n],hi32);
cv->pval[n] = vals[n];
}
if (nval == 1) info(0,"%s : %u",name,vals[0]);
else if (nval > 1) info(0,"%s : %u %u",name,vals[0],vals[1]);
return 0;
}
cv++;
}
return parsewarn(FLN,fname,linno,0,"unknown cmd %s ignored",name);
}
static void chkcmd(struct cmdvars *cvs,const char *desc)
{
struct cmdvars *cv = cvs;
while (cv->namelen) {
if (cv->linno == 0) error(Exit,"missing required var %s for %s",cv->name,desc);
cv++;
}
}
#define Conshift 6
#define Conmax 1024
// manage constats for this step only. netbase needs to reconnect
static int doconstats(struct portbase *ports,ub4 portcnt)
{
struct portbase *pp;
ub4 nodep,noarr,nodeparr,udeparr1;
ub4 hidep,hiarr,hidport,hiaport;
ub4 port,ndep,narr,i,cnt,pos,pos1;
char *name;
char buf[256];
char buf1[256];
ub4 buflen = sizeof(buf);
ub4 maxcon = (1 << Conshift) - 1;
static ub4 constats[1 << (Conshift * 2)];
static ub4 depstats[Conmax];
static ub4 arrstats[Conmax];
static ub4 dastats[Conmax];
ub4 depivs = Elemcnt(depstats) - 1;
ub4 arrivs = Elemcnt(arrstats) - 1;
nodep = noarr = nodeparr = udeparr1 = 0;
hidep = hiarr = hidport = hiaport = 0;
for (port = 0; port < portcnt; port++) {
pp = ports + port;
name = pp->iname;
ndep = pp->ndep; narr = pp->narr;
if (ndep == 0 && narr == 0) {
if (pp->geo == 0) {
warn(Iter,"port %u %u has no connections - %s",port,pp->id,name);
nodeparr++;
}
}
else if (ndep == 0) { info(Iter,"port %u has no deps, %u arr\as %s",port,narr,name); nodep++; }
else if (narr == 0) { info(Iter,"port %u has no arrs, %u dep\as %s",port,ndep,name); noarr++; }
if (ndep > hidep) { hidep = ndep; hidport = port; }
if (narr > hiarr) { hiarr = narr; hiaport = port; }
depstats[min(ndep,depivs)]++;
arrstats[min(narr,arrivs)]++;
dastats[min(ndep + narr,arrivs)]++;
ndep = min(ndep,maxcon);
narr = min(narr,maxcon);
constats[(ndep << Conshift) | narr]++;
}
if (nodeparr) warn(0,"%u of %u ports without connection",nodeparr,portcnt);
if (nodep) info(0,"%u of %u ports without departures",nodep,portcnt);
if (noarr) info(0,"%u of %u ports without arrivals",noarr,portcnt);
ub4 showda = dyncfg("netio.showda",1,0);
if (showda) {
info0(User,"");
info0(0,"ports with h deps and v arrs ");
info0(0," 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15");
for (i = 0; i < 16; i++) {
ndep = i << Conshift;
info(0,"%2u %5u %5u %5u %5u %5u %5u %5u %5u %5u %5u %5u %5u %5u %5u %5u %5u",i,
constats[ndep],constats[ndep+1],constats[ndep+2],constats[ndep+3],constats[ndep+4],constats[ndep+5],constats[ndep+6],constats[ndep+7],
constats[ndep+8],constats[ndep+9],constats[ndep+10],constats[ndep+11],constats[ndep+12],constats[ndep+13],constats[ndep+14],constats[ndep+15]);
}
info0(User,"");
info0(0,"ports with n deps");
pos = pos1 = 0;
for (ndep = 0; ndep <= min(depivs,127); ndep++) {
pos1 += mysnprintf(buf1,pos1,buflen," %5u",ndep);
pos += mysnprintf(buf,pos,buflen," %5u",depstats[ndep]);
if ( (ndep % 16) == 15) {
info0(User,"");
info(0,"%s",buf1);
info(0,"%s",buf);
pos = pos1 = 0;
}
}
if (pos) {
info0(User,"");
info(0,"%s",buf1);
info(0,"%s",buf);
}
info0(User,"");
info0(0,"ports with n arrs");
pos = pos1 = 0;
for (narr = 0; narr <= min(arrivs,127); narr++) {
pos1 += mysnprintf(buf1,pos1,buflen," %5u",narr);
pos += mysnprintf(buf,pos,buflen," %5u",arrstats[narr]);
if ( (narr % 16) == 15) {
info0(User,"");
info(0,"%s",buf1);
info(0,"%s",buf);
pos = pos1 = 0;
}
}
if (pos) {
info0(User,"");
info(0,"%s",buf1);
info(0,"%s",buf);
}
info0(User,"");
}
pp = ports + hidport;
info(0,"port %u deps %u arrs %u %s",hidport,hidep,pp->narr,pp->iname);
pp = ports + hiaport;
info(0,"port %u deps %u arrs %u %s",hiaport,pp->ndep,hiarr,pp->iname);
for (port = 0; port < portcnt; port++) {
pp = ports + port;
ndep = pp->ndep; narr = pp->narr;
if (ndep > hidep / 2 || narr > hiarr / 2) info(Iter,"port %u deps %u arrs %u %s", port,ndep,narr,pp->iname);
pp->ndep = pp->narr = 0;
}
ub8 sumcnt = 0;
for (narr = 0; narr <= arrivs; narr++) {
cnt = dastats[narr];
sumcnt += cnt;
msgopt("dastats"); infocc(cnt,0,"%u deps + arrs : %u = \ap%lu%lu",narr,cnt,sumcnt,(ub8)portcnt);
}
return 0;
}
static ub4 portmodes(struct portbase *pp)
{
ub4 m = 0;
if (pp->air) m |= 1;
if (pp->rail) m |= 2;
if (pp->bus) m |= 4;
if (pp->ferry) m |= 8;
return m;
}
static ub4 sportmodes(struct subportbase *pp)
{
ub4 m = 0;
if (pp->air) m |= 1;
if (pp->rail) m |= 2;
if (pp->bus) m |= 4;
if (pp->ferry) m |= 8;
return m;
}
// name iname pfx id subid lat lon [opts stopid utcofs dstonof]
// opts: parent, child, geo
static int rdextports(netbase *net,const char *dir)
{
enum extresult res;
struct extfmt *eft = talloc0(1,struct extfmt,0);
const char *fname;
ub4 rawportcnt,extportcnt,portcnt,port;
struct portbase *ports,*pp;
int rv;
char *buf;
ub4 len,linno,colno,namelen,inamelen,prefixlen,idlo,idhi,subidhi,mapidlen,maxid;
ub4 lat,lon,id,subid,stopid,opts;
ub4 utcofs12,dstonof;
ub4 dup2;
ub4 lolon,lolat,hilon,hilat;
char *name,*iname;
ub4 valndx,svalndx,*vals;
ub8 x8;
struct extport {
char name[Portnamelen];
char iname[Portnamelen];
char prefix[64];
ub4 namelen,inamelen;
ub4 prefixlen;
ub4 id,subid,stopid,linno;
ub4 opts;
bool parent,child;
ub4 subcnt,subofs,seq;
ub4 lat,lon;
ub4 utcofs12,dstonof;
};
struct extport *extports,*ep,*pep,*ep2;
ub4 namemax = sizeof(extports->name) - 1;
fmtstring(eft->mf.name,"%s/ports.txt",dir);
fname = eft->mf.name;
info0(User,"");
info(0,"reading %s",fname);
rv = readfile(&eft->mf,fname,1,0);
if (rv) return 1;
buf = eft->mf.buf;
len = (ub4)eft->mf.len;
rawportcnt = linecnt(fname,buf, len);
if (rawportcnt == 0) return error(0,"%s is empty",fname);
extportcnt = 0;
extports = ep = talloc(rawportcnt,struct extport,0,"ext ports",rawportcnt);
// vg_set_undef(ep,rawportcnt * sizeof(struct extport));
// vg_chk_def(ep,sizeof(struct extport));
idhi = subidhi = maxid = 0;
idlo = hi32;
lolat = lolon = hi32;
hilat = hilon = 0;
vals = eft->vals;
name = eft->name;
do {
res = nextchar(eft);
switch(res) {
case Newcmd:
if (docmd(portvars,eft,fname)) return 1;
break;
case Newitem:
if (extportcnt == 0) {
chkcmd(portvars,"ports");
error_zz(latscale,lonscale);
error_eq(latscale,hi32);
error_eq(lonscale,hi32);
x8 = (ub8)latscale / 180;
error_gt(x8,1<<30,0);
x8 = (ub8)lonscale / 360;
error_gt(x8,1<<30,0);
}
namelen = eft->namelen;
valndx = eft->valndx;
svalndx = eft->svalndx;
linno = eft->linno;
colno = eft->colno;
error_ge(extportcnt,rawportcnt);
if (valndx < 4) return parserr(FLN,fname,linno,colno,eft,"missing id,subid,lat,lon args, only %u",valndx);
id = vals[0];
subid = vals[1];
msgprefix(0,"r.port %u.%u",id,extportcnt);
lat = vals[2];
lon = vals[3];
if (valndx > 3) opts = vals[4];
else opts = 0;
if (valndx > 4) stopid = vals[5];
else stopid = 0;
utcofs12 = (valndx > 5 ? vals[6] : 2600);
dstonof = (valndx > 6 ? vals[7] : 0);
error_eq(id,hi32);
error_eq(subid,hi32);
error_eq(lat,hi32);
error_eq(lon,hi32);
error_eq(opts,hi32);
error_eq(stopid,hi32);
vrb(0,"port %u id %u sub %u opts %x",extportcnt,id,subid,opts);
for (dup2 = 0; dup2 < extportcnt; dup2++) {
ep2 = extports + dup2;
if (ep2->lat != lat || ep2->lon != lon || ep2->namelen != namelen) break;
if (memcmp(ep2->name,name,namelen)) break;
if (ep2->id == id && ep2->subid == subid) parsewarn(FLN,fname,linno,colno,"duplicate port id %u subid %u %s",id,subid,name);
else parsewarn(FLN,fname,linno,colno,"port name %s lat %u lon %u for both id %u subid %u and %u %u",name,lat,lon,id,subid,ep2->id,ep2->subid);
}
if (id > idhi) idhi = id;
if (id < idlo) idlo = id;
if (subid > subidhi) subidhi = subid;
ep->id = id;
ep->stopid = stopid;
ep->subid = subid;
ep->linno = linno;
// infocc(id > 300000,Iter,"port %u id %u %x hi %u",extportcnt,id,id,idhi);
ep->opts = opts;
ep->parent = (opts & Stopopt_parent);
ep->child = (opts & Stopopt_child);
if (id != subid && ep->parent) parsewarn(FLN,fname,linno,colno,"parent port %u has parent %u",subid,id);
if (id == subid && ep->child) parsewarn(FLN,fname,linno,colno,"child port %u %s has no parent",id,name);
if (lat >= 180 * latscale) { parsewarn(FLN,fname,linno,colno,"port %u lat %u out of range",id,lat); lat = 0; }
if (lon >= 360 * lonscale) { parsewarn(FLN,fname,linno,colno,"port %u lon %u out of range",id,lon); lon = 0; }
ep->lat = lat;
ep->lon = lon;
lolat = min(lolat,lat);
hilat = max(hilat,lat);
lolon = min(lolon,lon);
hilon = max(hilon,lon);
ep->utcofs12 = utc12ofs(utcofs12);
ep->dstonof = dstonof;
if (namelen > namemax) parsewarn(FLN,fname,linno,colno,"name length %u exceeds max %u",namelen,namemax);
else if (namelen == 0) return parserr(FLN,fname,linno,colno,"port %u has no name",id);
else {
ep->namelen = copystr(ep->name,name);
memset(ep->iname,0,sizeof(ep->iname));
}
if (svalndx > 1) {
prefixlen = eft->svallens[1];
prefixlen = min(prefixlen,sizeof(ep->prefix)-1);
if (prefixlen) memcpy(ep->prefix,eft->svals + Maxname,prefixlen);
ep->prefixlen = prefixlen;
ep->prefix[prefixlen] = 0;
} else ep->prefixlen = 0;
if (svalndx) {
inamelen = eft->svallens[0];
iname = eft->svals;
if (inamelen > namemax) {
parsewarn(FLN,fname,linno,colno,"name length %u exceeds max %u",inamelen,namemax);
}
info(V1,"port %u name '%s'",extportcnt,iname);
} else { inamelen = 0; iname = name; }
ep->inamelen = copystr(ep->Liname,iname);
extportcnt++;
ep++;
msgprefix(0,NULL);
break; // newitem
case Next: break;
case Eof: break;
case Parserr: return 1;
}
} while (res < Eof); // each input char
info(0,"read %u ports, id in %u..%u",extportcnt,idlo,idhi);
freefile(&eft->mf);
afree0(eft);
if (extportcnt == 0) return error(0,"no ports from %u",rawportcnt);
ub4 pid,subofs,seq,pos;
ub4 cnt,extport,extport2,subport,subportcnt;
ub8 vgadr = 0;
struct subportbase *subports,*sp;
// create mappings
if (idhi > 100 * 1000 * 1000) warning(0,"max port id %u",idhi);
if (subidhi > 100 * 1000 * 1000) warning(0,"max subport id %u",subidhi);
mapidlen = max(idhi,subidhi) + 1;
ub4 *id2ports = talloc(mapidlen,ub4,0xff,"ext id2port",idhi);
ub4 *subid2ports = talloc(mapidlen,ub4,0xff,"ext subid2port",subidhi);
port = 0;
for (extport = 0; extport < extportcnt; extport++) {
ep = extports + extport;
id = ep->id;
subid = ep->subid;
error_gt_cc(id,idhi,"port %u",extport);
error_gt(subid,subidhi,extport);
ep->subcnt = 0;
if (id == subid) { // parent or plain port
if (id2ports[id] != hi32) warning(0,"port ID %u doubly defined as %x", id,id2ports[id]);
else id2ports[id] = extport;
infocc(id == 1696,0,"port %u ext %u %s",id,extport,ep->name);
if (subid2ports[id] != hi32) warning(0,"port subID %u doubly defined as %x", subid,subid2ports[subid]);
else { subid2ports[id] = extport; if (extport == 0) info(0,"id %u port 0 %s",id,ep->namelen ? ep->name : "(no name)"); port++; }
} else {
if (subid2ports[subid] != hi32) warning(0,"port subID %u doubly defined as %x", subid,subid2ports[subid]);
else { subid2ports[subid] = extport; if (extport == 0) info(0,"subid %u",subid); }
}
}
portcnt = port;
for (extport = 0; extport < extportcnt; extport++) {
ep = extports + extport;
id = ep->id;
if (subid2ports[id] == 0) info(0,"port %u id %u map 0 %s",extport,id,ep->name);
}
// check station assignment
for (extport = 0; extport < extportcnt; extport++) {
ep = extports + extport;
id = ep->id;
subid = ep->subid;
if (id == subid) continue;
extport2 = subid2ports[id];
if (extport2 == hi32) return parserr(FLN,fname,ep->linno,0,eft,"port ID %u has nonexisting parent %u %s",subid,id,ep->name);
ep2 = extports + extport2;
if (ep2->parent == 0) warning(0,"line %u: port ID %u has plain %u as parent %s",ep->linno,subid,id,ep->name);
}
// count members
subportcnt = 0;
for (extport = 0; extport < extportcnt; extport++) {
ep = extports + extport;
id = ep->id;
subid = ep->subid;
if (id == subid) continue;
pid = id2ports[id];
error_ge_cc(pid,extportcnt,"id %u",id);
error_eq(pid,extport);
pep = extports + pid;
cnt = pep->subcnt;
if (cnt > 253) {
warning(0,"drop subport %u for %u %s %s",subid,pid,ep->name,pep->name);
ep->id = hi32;
continue;
}
pep->subcnt = cnt + 1;
ep->seq = cnt;
vrb(0,"port %u has parent %u %s %s",subid,id,ep->name,pep->name);
subportcnt++;
}
info(Emp,"%u ports, %u subports", portcnt, subportcnt);
error_gt(portcnt,extportcnt,0);
error_ge(subportcnt,extportcnt);
if (portcnt == 0) return 0;
ports = mkblock(&net->portmem,portcnt,struct portbase,Init0,"ext ports");
net->ports = ports;
if (subportcnt) {
subports = mkblock(&net->subportmem,subportcnt,struct subportbase,Init0,"ext subports");
net->subports = subports;
} else subports = NULL;
// assign subport ofs
subofs = 0;
for (extport = 0; extport < extportcnt; extport++) {
ep = extports + extport;
id = ep->id;
if (id == hi32) continue;
subid = ep->subid;
if (id != subid) continue;
cnt = ep->subcnt;
ep->subofs = subofs;
if (cnt == 0) continue;
vrb(0,"port %u subcnt %u %s",extport,cnt,ep->name);
subofs += cnt;
}
error_ne(subofs,subportcnt);