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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
|
/***************************************************************************
* *
* _____ ____ *
* | __ \ / __ \ _ _ _____ *
* | | \ \ / / \_\ | | | | _ \ *
* | | \ \| | | | | | |_| | *
* | | | || | | | | | ___/ *
* | | / /| | __ | | | | _ \ *
* | |__/ / \ \__/ / | |___| | |_| | *
* |_____/ \____/ |_____|_|_____/ *
* *
* Wiimms source code library *
* *
***************************************************************************
* *
* Copyright (c) 2012-2021 by Dirk Clemens <wiimm@wiimm.de> *
* *
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
* *
***************************************************************************/
#ifndef DCLIB_BASICS_H
#define DCLIB_BASICS_H 1
#define _GNU_SOURCE 1
#include <sys/types.h>
#include <arpa/inet.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#ifndef WIN_DCLIB
#include <stdint.h>
#endif
#include "dclib-types.h"
#include "dclib-debug.h"
#include "dclib-color.h"
#include "dclib-numeric.h"
//
///////////////////////////////////////////////////////////////////////////////
/////////////// helpers ///////////////
///////////////////////////////////////////////////////////////////////////////
#define SIZEOFMEMBER(type,a) sizeof(((type*)0)->a)
#define SIZEOFRANGE(type,a,b) \
( offsetof(type,b) + sizeof(((type*)0)->b) - offsetof(type,a) )
#define PTR_DISTANCE(a,b) ( (u8*)(a) - (u8*)(b) )
#define PTR_DISTANCE_INT(a,b) ( (int)( (u8*)(a) - (u8*)(b) ))
//
///////////////////////////////////////////////////////////////////////////////
/////////////// HAVE_* ///////////////
///////////////////////////////////////////////////////////////////////////////
#undef HAVE_CLOCK_GETTIME
#if _POSIX_C_SOURCE >= 199309L
#define HAVE_CLOCK_GETTIME 1
#else
#define HAVE_CLOCK_GETTIME 0
#endif
///////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
/////////////// bits ///////////////
///////////////////////////////////////////////////////////////////////////////
extern const uchar TableBitCount[0x100];
extern const char TableLowest0Bit[0x100];
extern const char TableLowest1Bit[0x100];
extern const char TableHighest0Bit[0x100];
extern const char TableHighest1Bit[0x100];
uint Count1Bits ( cvp addr, uint size );
static inline uint Count0Bits ( cvp addr, uint size )
{ return CHAR_BIT * size - Count1Bits(addr,size); }
#define Count1BitsObj(obj) Count1Bits(&(obj),sizeof(obj))
#define Count0BitsObj(obj) Count1Bits(&(obj),sizeof(obj))
static inline uint Count1Bits8 ( u8 data )
{
return TableBitCount[data];
}
static inline uint Count1Bits16 ( u16 data )
{
const u8 * d = (u8*)&data;
return TableBitCount[d[0]]
+ TableBitCount[d[1]];
}
static inline uint Count1Bits32 ( u32 data )
{
// __builtin_popcount(unsigned int) is slower than table based summary
const u8 * d = (u8*)&data;
return TableBitCount[d[0]]
+ TableBitCount[d[1]]
+ TableBitCount[d[2]]
+ TableBitCount[d[3]];
}
static inline uint Count1Bits64 ( u64 data )
{
#if __GNUC__ > 3
return __builtin_popcountll(data);
#else
const u8 * d = (u8*)&data;
return TableBitCount[d[0]]
+ TableBitCount[d[1]]
+ TableBitCount[d[2]]
+ TableBitCount[d[3]]
+ TableBitCount[d[4]]
+ TableBitCount[d[5]]
+ TableBitCount[d[6]]
+ TableBitCount[d[7]];
#endif
}
///////////////////////////////////////////////////////////////////////////////
int FindLowest0BitBE ( cvp addr, uint size );
int FindLowest1BitBE ( cvp addr, uint size );
int FindHighest0BitBE ( cvp addr, uint size );
int FindHighest1BitBE ( cvp addr, uint size );
int FindLowest0BitLE ( cvp addr, uint size );
int FindLowest1BitLE ( cvp addr, uint size );
int FindHighest0BitLE ( cvp addr, uint size );
int FindHighest1BitLE ( cvp addr, uint size );
#if __BYTE_ORDER == 1234
#define FindLowest0Bit FindLowest0BitLE
#define FindLowest1Bit FindLowest1BitLE
#define FindHighest0Bit FindHighest0BitLE
#define FindHighest1Bit FindHighest1BitLE
#else
#define FindLowest0Bit FindLowest0BitBE
#define FindLowest1Bit FindLowest1BitBE
#define FindHighest0Bit FindHighest0BitBE
#define FindHighest1Bit FindHighest1BitBE
#endif
u32 GetAlign32 ( u32 data );
u64 GetAlign64 ( u64 data );
///////////////////////////////////////////////////////////////////////////////
static inline void SetBit ( void *addr, uint bit_num )
{ ((u8*)addr)[ bit_num >> 3 ] |= 1 << ( bit_num & 7 ); }
static inline void ClearBit ( void *addr, uint bit_num )
{ ((u8*)addr)[ bit_num >> 3 ] &= ~(1 << ( bit_num & 7 )); }
static inline void InvertBit ( void *addr, uint bit_num )
{ ((u8*)addr)[ bit_num >> 3 ] ^= 1 << ( bit_num & 7 ); }
static inline bool TestBit ( const void *addr, uint bit_num )
{ return ( ((u8*)addr)[ bit_num >> 3 ] & 1 << ( bit_num & 7 ) ) != 0; }
// change a single bit and return the previous state
bool TestSetBit ( void *addr, uint bit_num );
bool TestClearBit ( void *addr, uint bit_num );
bool TestInvertBit ( void *addr, uint bit_num );
// change bits inclusive 'beg' to exclusive 'end' if beg_bitnum<end_bitnum
void SetBits ( void *addr, uint beg_bitnum, uint end_bitnum );
void ClearBits ( void *addr, uint beg_bitnum, uint end_bitnum );
void InvertBits ( void *addr, uint beg_bitnum, uint end_bitnum );
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int PrintBitfieldEx
(
// returns the number of printed characters, or -1 if buffer is to small
char *buf, // buffer
uint buf_size, // size of 'buf'
cvp bit_field, // address of bit field
uint min_idx, // minimal index (inclusive) to check
uint max_idx, // maximal index (inclusive) to check
bool use_ranges, // true: create ranges 'a:b'
int mult, // each printed index is first multiplyed by 'mult'
int add // ... and then added to 'add'.
);
static inline int PrintBitfield
(
// returns the number of printed characters, or -1 if buffer is to small
char *buf, // buffer
uint buf_size, // size of 'buf'
cvp bit_field, // address of bit field
uint bit_field_size, // size of 'bit_field' in bytes
bool use_ranges // true: create ranges 'a:b'
)
{
return PrintBitfieldEx ( buf, buf_size, bit_field,
0, 8*bit_field_size-1, use_ranges, 1, 0 );
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// low level endian conversions ///////////////
///////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------
// swapping bytes = inverse byte order
// void dswap16 ( void * data_ptr );
// void dswap24 ( void * data_ptr );
// void dswap32 ( void * data_ptr );
// void dswap48 ( void * data_ptr );
// void dswap64 ( void * data_ptr );
//----------------------------------------------------
static inline void dswap16 ( void * data_ptr )
{
u8 *d = data_ptr;
u8 temp = d[0];
d[0] = d[1];
d[1] = temp;
}
static inline void dswap24 ( void * data_ptr )
{
u8 *d = data_ptr;
const u8 temp = d[0];
d[0] = d[2];
d[2] = temp;
}
static inline void dswap32 ( void * data_ptr )
{
u8 temp, *d = data_ptr;
temp = d[0]; d[0] = d[3]; d[3] = temp;
temp = d[1]; d[1] = d[2]; d[2] = temp;
}
static inline void dswap48 ( void * data_ptr )
{
u8 temp, *d = data_ptr;
temp = d[0]; d[0] = d[5]; d[5] = temp;
temp = d[1]; d[1] = d[4]; d[4] = temp;
temp = d[2]; d[2] = d[3]; d[3] = temp;
}
static inline void dswap64 ( void * data_ptr )
{
u8 temp, *d = data_ptr;
temp = d[0]; d[0] = d[7]; d[7] = temp;
temp = d[1]; d[1] = d[6]; d[6] = temp;
temp = d[2]; d[2] = d[5]; d[5] = temp;
temp = d[3]; d[3] = d[4]; d[4] = temp;
}
///////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------
// read data and swap bytes (inverse byte order)
// u16 swap16 ( const void * data_ptr );
// u32 swap24 ( const void * data_ptr );
// u32 swap32 ( const void * data_ptr );
// u64 swap48 ( const void * data_ptr );
// u64 swap64 ( const void * data_ptr );
// float swapf4 ( const void * data_ptr );
// double swapf8 ( const void * data_ptr );
//----------------------------------------------------
static inline u16 swap16 ( const void * data_ptr )
{
u8 r[2];
const u8 *d = data_ptr;
r[1] = *d++;
r[0] = *d;
return *(u16*)r;
}
static inline u32 swap24 ( const void * data_ptr )
{
u8 r[4];
const u8 *d = data_ptr;
r[3] = 0;
r[2] = *d++;
r[1] = *d++;
r[0] = *d;
return *(u32*)r;
}
static inline u32 swap32 ( const void * data_ptr )
{
u8 r[4];
const u8 *d = data_ptr;
r[3] = *d++;
r[2] = *d++;
r[1] = *d++;
r[0] = *d;
return *(u32*)r;
}
static inline u64 swap48 ( const void * data_ptr )
{
u8 r[8];
const u8 *d = data_ptr;
r[7] = 0;
r[6] = 0;
r[5] = *d++;
r[4] = *d++;
r[3] = *d++;
r[2] = *d++;
r[1] = *d++;
r[0] = *d;
return *(u64*)r;
}
static inline u64 swap64 ( const void * data_ptr )
{
u8 r[8];
const u8 *d = data_ptr;
r[7] = *d++;
r[6] = *d++;
r[5] = *d++;
r[4] = *d++;
r[3] = *d++;
r[2] = *d++;
r[1] = *d++;
r[0] = *d;
return *(u64*)r;
}
static inline float swapf4 ( const void * data_ptr )
{
// assume: local system supports IEEE 754
union { float f; u32 i; } u;
u.i = swap32(data_ptr);
return u.f;
}
static inline double swapf8 ( const void * data_ptr )
{
// assume: local system supports IEEE 754
union { double f; u64 i; } u;
u.i = swap64(data_ptr);
return u.f;
}
///////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------
// convert big endian data to a number in host format
// u16 be16 ( const void * be_data_ptr );
// u32 be24 ( const void * be_data_ptr );
// u32 be32 ( const void * be_data_ptr );
// u64 be40 ( const void * be_data_ptr );
// u64 be48 ( const void * be_data_ptr );
// u64 be56 ( const void * be_data_ptr );
// u64 be64 ( const void * be_data_ptr );
// float bef4 ( const void * be_data_ptr );
// double bef8 ( const void * be_data_ptr );
//----------------------------------------------------
static inline u16 be16 ( const void * be_data_ptr )
{
const u8 * d = be_data_ptr;
return d[0] << 8 | d[1];
}
static inline u32 be24 ( const void * be_data_ptr )
{
const u8 * d = be_data_ptr;
return ( d[0] << 8 | d[1] ) << 8 | d[2];
}
static inline u32 be32 ( const void * be_data_ptr )
{
const u8 * d = be_data_ptr;
return (( d[0] << 8 | d[1] ) << 8 | d[2] ) << 8 | d[3];
}
static inline u64 be40 ( const void * be_data_ptr )
{
const u8 * d = be_data_ptr;
return (u64)be32(d) << 8 | ((u8*)d)[4];
}
static inline u64 be48 ( const void * be_data_ptr )
{
const u8 * d = be_data_ptr;
return (u64)be32(d) << 16 | be16(d+4);
}
static inline u64 be56 ( const void * be_data_ptr )
{
const u8 * d = be_data_ptr;
return (u64)be32(d) << 24 | be24(d+4);
}
static inline u64 be64 ( const void * be_data_ptr )
{
const u8 * d = be_data_ptr;
return (u64)be32(d) << 32 | be32(d+4);
}
static inline float bef4 ( const void * be_data_ptr )
{
// assume: local system supports IEEE 754
union { float f; u32 i; } u;
u.i = be32(be_data_ptr);
return u.f;
}
static inline double bef8 ( const void * be_data_ptr )
{
// assume: local system supports IEEE 754
union { double f; u64 i; } u;
u.i = be64(be_data_ptr);
return u.f;
}
///////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------
// convert little endian data to a number in host format
// u16 le16 ( const void * le_data_ptr );
// u32 le24 ( const void * le_data_ptr );
// u32 le32 ( const void * le_data_ptr );
// u64 le40 ( const void * le_data_ptr );
// u64 le48 ( const void * le_data_ptr );
// u64 le56 ( const void * le_data_ptr );
// u64 le64 ( const void * le_data_ptr );
// float lef4 ( const void * le_data_ptr );
// double lef8 ( const void * le_data_ptr );
//-------------------------------------------------------
static inline u16 le16 ( const void * le_data_ptr )
{
const u8 * d = le_data_ptr;
return d[1] << 8 | d[0];
}
static inline u32 le24 ( const void * le_data_ptr )
{
const u8 * d = le_data_ptr;
return ( d[2] << 8 | d[1] ) << 8 | d[0];
}
static inline u32 le32 ( const void * le_data_ptr )
{
const u8 * d = le_data_ptr;
return (( d[3] << 8 | d[2] ) << 8 | d[1] ) << 8 | d[0];
}
static inline u64 le40 ( const void * le_data_ptr )
{
const u8 * d = le_data_ptr;
return (u64)le32(d+1) << 8 | ((u8*)d)[0];
}
static inline u64 le48 ( const void * le_data_ptr )
{
const u8 * d = le_data_ptr;
return (u64)le16(d+4) << 32 | le32(d);
}
static inline u64 le56 ( const void * le_data_ptr )
{
const u8 * d = le_data_ptr;
return (u64)le24(d+4) << 32 | le32(d);
}
static inline u64 le64 ( const void * le_data_ptr )
{
const u8 * d = le_data_ptr;
return (u64)le32(d+4) << 32 | le32(d);
}
static inline float lef4 ( const void * le_data_ptr )
{
// assume: local system supports IEEE 754
union { float f; u32 i; } u;
u.i = le32(le_data_ptr);
return u.f;
}
static inline double lef8 ( const void * le_data_ptr )
{
// assume: local system supports IEEE 754
union { double f; u64 i; } u;
u.i = le64(le_data_ptr);
return u.f;
}
///////////////////////////////////////////////////////////////////////////////
//-------------------------------------------
// convert u64 from/to network byte order
// be64_t hton64 ( u64 data );
// u64 ntoh64 ( be64_t data );
//-------------------------------------------
static inline be64_t hton64 ( u64 data )
{
be64_t result;
((u32*)&result)[0] = htonl( (u32)(data >> 32) );
((u32*)&result)[1] = htonl( (u32)data );
return result;
}
static inline u64 ntoh64 ( be64_t data )
{
return (u64)ntohl(((u32*)&data)[0]) << 32 | ntohl(((u32*)&data)[1]);
}
///////////////////////////////////////////////////////////////////////////////
// convert a number in host format into big endian data
void write_be16 ( void * be_data_ptr, u16 data );
void write_be24 ( void * be_data_ptr, u32 data );
void write_be32 ( void * be_data_ptr, u32 data );
void write_be40 ( void * be_data_ptr, u64 data );
void write_be48 ( void * be_data_ptr, u64 data );
void write_be56 ( void * be_data_ptr, u64 data );
void write_be64 ( void * be_data_ptr, u64 data );
void write_bef4 ( void * be_data_ptr, float data );
void write_bef8 ( void * be_data_ptr, double data );
// convert a number in host format into little endian data
void write_le16 ( void * le_data_ptr, u16 data );
void write_le24 ( void * le_data_ptr, u32 data );
void write_le32 ( void * le_data_ptr, u32 data );
void write_le40 ( void * le_data_ptr, u64 data );
void write_le48 ( void * le_data_ptr, u64 data );
void write_le56 ( void * le_data_ptr, u64 data );
void write_le64 ( void * le_data_ptr, u64 data );
void write_lef4 ( void * le_data_ptr, float data );
void write_lef8 ( void * le_data_ptr, double data );
// convert lists
void be16n ( u16 * dest, const u16 * src, int n );
void be32n ( u32 * dest, const u32 * src, int n );
void bef4n ( float32 * dest, const float32 * src, int n );
void le16n ( u16 * dest, const u16 * src, int n );
void lef4n ( float32 * dest, const float32 * src, int n );
void write_be16n ( u16 * dest, const u16 * src, int n );
void write_be32n ( u32 * dest, const u32 * src, int n );
void write_bef4n ( float32 * dest, const float32 * src, int n );
void write_le16n ( u16 * dest, const u16 * src, int n );
void write_lef4n ( float32 * dest, const float32 * src, int n );
//-----------------------------------------------------------------------------
// [[endian_func_t]]
typedef struct endian_func_t
{
//--- info section
u8 bom[2]; // byte order mark: BE=0xfe,0xff, LE=0xff,0xfe
bool is_be; // true: is big endian
bool is_le; // true: is little endian
dcEndian_t endian; // DC_BIG_ENDIAN or DC_LITTLE_ENDIAN
//--- read functions
u16 (*rd16) ( const void * data_ptr );
u32 (*rd24) ( const void * data_ptr );
u32 (*rd32) ( const void * data_ptr );
u64 (*rd40) ( const void * data_ptr );
u64 (*rd48) ( const void * data_ptr );
u64 (*rd56) ( const void * data_ptr );
u64 (*rd64) ( const void * data_ptr );
float (*rdf4) ( const void * data_ptr );
double (*rdf8) ( const void * data_ptr );
//--- write functions
void (*wr16) ( void * data_ptr, u16 data );
void (*wr24) ( void * data_ptr, u32 data );
void (*wr32) ( void * data_ptr, u32 data );
void (*wr40) ( void * data_ptr, u64 data );
void (*wr48) ( void * data_ptr, u64 data );
void (*wr56) ( void * data_ptr, u64 data );
void (*wr64) ( void * data_ptr, u64 data );
void (*wrf4) ( void * data_ptr, float data );
void (*wrf8) ( void * data_ptr, double data );
//--- convert functions
u16 (*n2hs) ( u16 data );
u32 (*n2hl) ( u32 data );
u64 (*n2h64) ( u64 data );
u16 (*h2ns) ( u16 data );
u32 (*h2nl) ( u32 data );
u64 (*h2n64) ( u64 data );
} endian_func_t;
extern const endian_func_t be_func;
extern const endian_func_t le_func;
const endian_func_t * GetEndianFunc ( const void * byte_order_mark );
uint GetTextBOMLen ( const void * data, uint data_size );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// struct mem_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[mem_t]]
typedef struct mem_t
{
ccp ptr; // NULL or a pointer to the data
int len; // length in bytes of the data
}
mem_t;
extern mem_t EmptyMem; // mem_t(EmptyString,0)
extern mem_t NullMem; // mem_t(0,0)
//-----------------------------------------------------------------------------
static inline mem_t MemByChar ( char ch )
{
mem_t mem = { &ch, 1 };
return mem;
}
static inline mem_t MemByString0 ( ccp str )
{
if (!str)
return NullMem;
mem_t mem = { str, strlen(str) };
return mem;
}
static inline mem_t MemByString ( ccp str )
{
mem_t mem = { str, strlen(str) };
return mem;
}
static inline mem_t MemByStringS ( ccp str, int len )
{
mem_t mem = { str, len < 0 ? strlen(str) : len };
return mem;
}
static inline mem_t MemByStringE ( ccp str, ccp end )
{
mem_t mem = { str, end ? end-str : strlen(str) };
return mem;
}
static inline mem_t MemByS ( ccp str, int len )
{
mem_t mem = { str, len };
return mem;
}
static inline mem_t MemByE ( ccp str, ccp end )
{
mem_t mem = { str, end-str };
return mem;
}
//-----------------------------------------------------------------------------
// Index helpers
// For the 'End' variants, index 0 is end_of_source
int CheckIndex1 ( int max, int index );
int CheckIndex1End ( int max, int index );
int CheckIndex2 ( int max, int * p_begin, int * p_end );
int CheckIndex2End ( int max, int * p_begin, int * p_end );
int CheckIndexC ( int max, int * p_begin, int count );
int CheckIndexCEnd ( int max, int * p_begin, int count );
//-----------------------------------------------------------------------------
// All functions are robust.
// A negative index is relative to the end of source.
// For RightMem() and ExtractEndMem(), index 0 means: end of source.
// count<0 means: copy left from 'begin' (only MidMem()) or skip bytes.
mem_t MidMem ( const mem_t src, int begin, int count );
static inline mem_t LeftMem ( const mem_t src, int count )
{
mem_t res;
res.ptr = src.ptr;
res.len = CheckIndex1(src.len,count);
return res;
}
static inline mem_t RightMem ( const mem_t src, int count )
{
mem_t res;
res.len = src.len - CheckIndex1End(src.len,-count);
res.ptr = src.ptr + src.len - res.len;
return res;
}
static inline mem_t ExtractMem ( const mem_t src, int begin, int end )
{
mem_t res;
res.len = CheckIndex2(src.len,&begin,&end);
res.ptr = src.ptr + begin;
return res;
}
static inline mem_t ExtractEndMem ( const mem_t src, int begin, int end )
{
mem_t res;
res.len = CheckIndex2End(src.len,&begin,&end);
res.ptr = src.ptr + begin;
return res;
}
mem_t BeforeMem ( const mem_t src, ccp ref );
mem_t BehindMem ( const mem_t src, ccp ref );
//-----------------------------------------------------------------------------
// Alloc dest buffer and terminate with 0.
// If m*.len < 0, then use strlen().
mem_t MemCat2A ( const mem_t m1, const mem_t m2 );
mem_t MemCat3A ( const mem_t m1, const mem_t m2, const mem_t m3 );
//-----------------------------------------------------------------------------
int CmpMem ( const mem_t s1, const mem_t s2 );
int StrCmpMem ( const mem_t mem, ccp str );
bool LeftStrCmpMemEQ ( const mem_t mem, ccp str );
// free a string if str is not NULL|EmptyString|MinusString
void FreeString ( ccp str );
static inline void FreeMem ( mem_t *mem )
{
DASSERT(mem);
FreeString(mem->ptr);
mem->ptr = 0;
mem->len = 0;
}
static inline void AssignDupMem ( mem_t *dest, mem_t src )
{
DASSERT(dest);
FreeString(dest->ptr);
dest->ptr = MEMDUP(src.ptr,src.len);
dest->len = src.len;
}
static inline mem_t DupMem ( const mem_t src )
{
mem_t res = { MEMDUP(src.ptr,src.len), src.len };
return res;
}
static inline char * DupStrMem ( const mem_t src )
{
return MEMDUP(src.ptr,src.len);
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// struct mem_list_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[mem_list_t]]
typedef struct mem_list_t
{
mem_t *list; // List of mem_t objects.
// Elements are always terminated with NULL.
// Content is NULL or EmptyString or stored in 'buf'
uint used; // Number of active 'list' elements
uint size; // Number of alloced 'list' elements
char *buf; // Alloced temp buffer
uint buf_used; // Usage of 'buf'
uint buf_size; // Size of 'buf'
}
mem_list_t;
//-----------------------------------------------------------------------------
// [[mem_list_mode_t]]
typedef enum mem_list_mode_t
{
MEMLM_ALL, // insert all
MEMLM_IGN_NULL, // ignore NULL
MEMLM_IGN_EMPTY, // ignore NULL and empty
MEMLM_REPL_NULL, // replace NULL by EmptyString
}
mem_list_mode_t;
//-----------------------------------------------------------------------------
void InitializeMemList ( mem_list_t *ml );
void ResetMemList ( mem_list_t *ml );
void MoveMemList ( mem_list_t *dest, mem_list_t *src );
void NeedBufMemList ( mem_list_t *ml, uint need_size, uint extra_size );
void NeedElemMemList ( mem_list_t *ml, uint n_elem, uint need_size );
//-----------------------------------------------------------------------------
void InsertMemListN
(
mem_list_t *ml, // valid destination mem_list
int pos, // insert position => CheckIndex1()
const mem_t *src, // source list
uint src_len, // number of elements in source list
mem_list_mode_t src_ignore // how to manage NULL and empty strings
);
//-----------------------------------------------------------------------------
static inline void InsertMemList2
(
mem_list_t *ml, // valid destination mem_list
int pos, // insert position => CheckIndex1()
const mem_list_t *src, // source mem_list
mem_list_mode_t src_ignore // how to manage NULL and empty strings
)
{
DASSERT(src);
InsertMemListN(ml,pos,src->list,src->used,src_ignore);
}
//-----------------------------------------------------------------------------
static inline void AppendMemListN
(
mem_list_t *ml, // valid destination mem_list
const mem_t *src, // source list
uint src_len, // number of elements in source list
mem_list_mode_t src_ignore // how to manage NULL and empty strings
)
{
DASSERT(ml);
InsertMemListN(ml,ml->used,src,src_len,src_ignore);
}
//-----------------------------------------------------------------------------
static inline void AppendMemList2
(
mem_list_t *ml, // valid destination mem_list
const mem_list_t *src, // source mem_list
mem_list_mode_t src_ignore // how to manage NULL and empty strings
)
{
DASSERT(ml);
DASSERT(src);
InsertMemListN(ml,ml->used,src->list,src->used,src_ignore);
}
//-----------------------------------------------------------------------------
static inline void AssignMemListN
(
mem_list_t *ml, // valid destination mem_list
const mem_t *src, // source list
uint src_len, // number of elements in source list
mem_list_mode_t src_ignore // how to manage NULL and empty strings
)
{
DASSERT(ml);
ml->used = 0;
InsertMemListN(ml,0,src,src_len,src_ignore);
}
//-----------------------------------------------------------------------------
static inline void AssignMemList2
(
mem_list_t *ml, // valid destination mem_list
const mem_list_t *src, // source mem_list
mem_list_mode_t src_ignore // how to manage NULL and empty strings
)
{
DASSERT(ml);
DASSERT(src);
ml->used = 0;
InsertMemListN(ml,0,src->list,src->used,src_ignore);
}
///////////////////////////////////////////////////////////////////////////////
void CatMemListN
(
mem_list_t *dest, // valid destination mem_list
const mem_list_t **src_list, // list with mem lists, element may be NULL
uint n_src_list, // number of elements in 'src'
mem_list_mode_t src_ignore // how to manage NULL and empty strings
);
//-----------------------------------------------------------------------------
static inline void CatMemList2
(
mem_list_t *dest, // valid destination mem_list
const mem_list_t *src1, // source mem_list
const mem_list_t *src2, // source mem_list
mem_list_mode_t src_ignore // how to manage NULL and empty strings
)
{
const mem_list_t *ml[2] = { src1, src2 };
CatMemListN(dest,ml,2,src_ignore);
}
//-----------------------------------------------------------------------------
static inline void CatMemList3
(
mem_list_t *dest, // valid destination mem_list
const mem_list_t *src1, // source mem_list
const mem_list_t *src2, // source mem_list
const mem_list_t *src3, // source mem_list
mem_list_mode_t src_ignore // how to manage NULL and empty strings
)
{
const mem_list_t *ml[3] = { src1, src2, src3 };
CatMemListN(dest,ml,3,src_ignore);
}
//-----------------------------------------------------------------------------
// All functions are robust and modify the current list.
// A negative index is relative to the end of source.
// For RightMemList() and *EndMemList(), index 0 means: end of source.
// count<0 means: copy left from 'begin' (only MidMemList()) or skip bytes.
// All functions return the number of elements aufer operation.
uint LeftMemList ( mem_list_t *ml, int count );
uint RightMemList ( mem_list_t *ml, int count );
uint MidMemList ( mem_list_t *ml, int begin, int count );
uint ExtractMemList ( mem_list_t *ml, int begin, int end );
uint ExtractEndMemList ( mem_list_t *ml, int begin, int end );
uint RemoveMemList ( mem_list_t *ml, int begin, int end );
uint RemoveEndMemList ( mem_list_t *ml, int begin, int end );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// MemPool_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[MemPoolChunk_t]]
typedef struct MemPoolChunk_t
{
struct MemPoolChunk_t *next;
char data[];
}
MemPoolChunk_t;
//-----------------------------------------------------------------------------
// [[MemPool_t]]
typedef struct MemPool_t
{
MemPoolChunk_t *chunk; // pointer to first (=current) chunk
uint space; // space left in current chunk
uint chunk_size; // wanted chunk size
}
MemPool_t;
//-----------------------------------------------------------------------------
static inline void InitializeMemPool ( MemPool_t *mp, uint chunk_size )
{ DASSERT(mp); memset(mp,0,sizeof(*mp)); mp->chunk_size = chunk_size; }
void ResetMemPool ( MemPool_t *mp );
void * MallocMemPool ( MemPool_t *mp, uint size );
void * CallocMemPool ( MemPool_t *mp, uint size );
void * MallocMemPoolA ( MemPool_t *mp, uint size, uint align );
void * CallocMemPoolA ( MemPool_t *mp, uint size, uint align );
void * MemDupMemPool ( MemPool_t *mp, cvp source, uint size );
static inline void * StrDupMemPool ( MemPool_t *mp, ccp source )
{ DASSERT(mp); return source ? MemDupMemPool(mp,source,strlen(source)) : 0; }
//
///////////////////////////////////////////////////////////////////////////////
/////////////// CircBuf_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[CircBuf_t]]
typedef struct CircBuf_t
{
char *buf; // NULL or alloced buffer
uint pos; // position of first valid byte
uint used; // used bytes in buffer
uint size; // size of 'buf'
}
CircBuf_t;
///////////////////////////////////////////////////////////////////////////////
void InitializeCircBuf ( CircBuf_t *cb, uint size );
void ResetCircBuf ( CircBuf_t *cb );
uint WriteCircBuf ( CircBuf_t *cb, cvp data, uint size );
uint PeakCircBuf ( CircBuf_t *cb, char *buf, uint buf_size );
uint ReadCircBuf ( CircBuf_t *cb, char *buf, uint buf_size );
uint DropCircBuf ( CircBuf_t *cb, uint size );
void ClearCircBuf ( CircBuf_t *cb );
uint PurgeCircBuf ( CircBuf_t *cb );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// CopyMode_t ///////////////
///////////////////////////////////////////////////////////////////////////////
typedef enum CopyMode_t
{
CPM_COPY, // make a copy of the data, alloced
CPM_MOVE, // move the alloced data
CPM_LINK, // link the data
} CopyMode_t;
///////////////////////////////////////////////////////////////////////////////
void * CopyData
(
const void *data, // data to copy/move/link
uint size, // size of 'data'
CopyMode_t mode, // copy mode
bool *res_alloced // not NULL:
// store true, if data must be freed
// otherwise store false
);
///////////////////////////////////////////////////////////////////////////////
void FreeData
(
const void *data, // data to free
CopyMode_t mode // copy mode
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// split strings ///////////////
///////////////////////////////////////////////////////////////////////////////
uint SplitByCharMem
(
// 'mem_list' will be padded with {EmptyString,0}
mem_t *mem_list, // array of mem_t
uint n_mem, // number of elements of 'mem_list'
mem_t source, // source string
char sep // separator character
);
//-----------------------------------------------------------------------------
uint SplitByTextMem
(
// 'mem_list' will be padded with {EmptyString,0}
mem_t *mem_list, // array of mem_t
uint n_mem, // number of elements of 'mem_list'
mem_t source, // source string
mem_t sep // separator text
);
//-----------------------------------------------------------------------------
uint SplitByCharMemList
(
mem_list_t *ml, // valid destination mem_list
bool init_ml, // true: initialize 'ml' first
mem_t source, // source string
char sep, // separator character
int max_fields // >0: max number of result fields
);
//-----------------------------------------------------------------------------
uint SplitByTextMemList
(
mem_list_t *ml, // valid destination mem_list
bool init_ml, // true: initialize 'ml' first
mem_t source, // source string
mem_t sep, // separator text
int max_fields // >0: max number of result fields
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// alloc info ///////////////
///////////////////////////////////////////////////////////////////////////////
extern bool alloc_info_calced;
extern uint alloc_info_overhead;
extern uint alloc_info_block_size;
extern uint alloc_info_add1;
extern uint alloc_info_add2;
extern uint alloc_info_mask;
///////////////////////////////////////////////////////////////////////////////
uint SetupAllocInfo(void);
static inline uint GetGoodAllocSize ( uint need )
{
return ( need + alloc_info_add1 & alloc_info_mask )
+ alloc_info_add2;
}
static inline uint GetGoodAllocSize2 ( uint n_elem, uint itemsize )
{
return (( n_elem*itemsize + alloc_info_add1 & alloc_info_mask )
+ alloc_info_add2 ) / itemsize;
}
static inline uint GetGoodAllocSizeA ( uint need, uint align )
{
need = ( need + align - 1) / align * align;
return (( need + alloc_info_add1 & alloc_info_mask )
+ alloc_info_add2 ) / align * align;
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// some basic string functions ///////////////
///////////////////////////////////////////////////////////////////////////////
extern const char EmptyQuote[]; // »""« Ignored by FreeString()
extern const char EmptyString[4]; // »« + 4*NULL, Ignored by FreeString()
extern const char MinusString[4]; // »-« + 3*NULL, Ignored by FreeString()
extern const char LoDigits[0x41]; // 0-9 a-z A-Z .+ NULL
extern const char HiDigits[0x41]; // 0-9 A-Z a-z .+ NULL
extern const char Tabs20[21]; // 20 * TAB + NULL
extern const char LF20[21]; // 20 * LF + NULL
extern const char Space200[201]; // 200 * ' ' + NULL
extern const char Minus300[301]; // 300 * '-' + NULL
extern const char Underline200[201]; // 200 * '_' + NULL
extern const char Hash200[201]; // 200 * '#' + NULL
extern const char Slash200[201]; // 200 * '/' + NULL
// UTF-8 => 3 bytes per char
extern const char ThinLine300_3[901]; // 300 * '─' (U+2500) + NULL
//-----------------------------------------------------------------------------
void SetupMultiProcessing(void);
void PrintSettingsDCLIB ( FILE *f, int indent );
void SetupProgname ( int argc, char ** argv,
ccp tool_name, ccp tool_vers, ccp tool_title );
// return NULL or 'ProgInfo.progpath', calc GetProgramPath() once if needed
ccp ProgramPath(void);
// return NULL or 'ProgInfo.progdir', calc ProgramPath() once if needed
ccp ProgramDirectory(void);
// path0 can be a directory or a filename (->dir extracted)
void DefineLogDirectory ( ccp path0, bool force );
// get 'ProgInfo.logdir' without tailing '/' ("." as fall back)
ccp GetLogDirectory();
#ifdef __CYGWIN__
ccp ProgramPathNoExt(void);
#else
static inline ccp ProgramPathNoExt(void) { return ProgramPath(); }
#endif
int GetProgramPath
(
// returns strlen(buf) on success, -1 on failure
char *buf, // buffer for the result; empty string on failure.
uint buf_size, // size of buffer
bool check_proc, // true: read files of /proc/... to find path
ccp argv0 // not NULL: analyse this argument (argv[0])
);
//-----
// StringCopy(), StringCopyE(), StringCat*()
// RESULT: end of copied string pointing to a NULL character.
// 'src*' may be a NULL pointer.
char * StringCopyS ( char * buf, size_t bufsize, ccp src );
char * StringCopySM ( char * buf, size_t bufsize, ccp src, size_t max_copy );
char * StringCat2S ( char * buf, size_t bufsize, ccp src1, ccp src2 );
char * StringCat3S ( char * buf, size_t bufsize, ccp src1, ccp src2, ccp src3 );
char * StringCopyE ( char * buf, ccp buf_end, ccp src );
char * StringCopyEM ( char * buf, ccp buf_end, ccp src, size_t max_copy );
char * StringCat2E ( char * buf, ccp buf_end, ccp src1, ccp src2 );
char * StringCat3E ( char * buf, ccp buf_end, ccp src1, ccp src2, ccp src3 );
// alloc space and return
char * StringCat2A ( ccp src1, ccp src2 );
char * StringCat3A ( ccp src1, ccp src2, ccp src3 );
static inline char * StringCopySMem ( char * buf, size_t bufsize, mem_t mem )
{ return StringCopySM(buf,bufsize,mem.ptr,mem.len); }
static inline char * StringCopyEMem ( char * buf, ccp buf_end, mem_t mem )
{ return StringCopyEM(buf,buf_end,mem.ptr,mem.len); }
char * StringLowerS ( char * buf, size_t bufsize, ccp src );
char * StringLowerE ( char * buf, ccp buf_end, ccp src );
char * StringUpperS ( char * buf, size_t bufsize, ccp src );
char * StringUpperE ( char * buf, ccp buf_end, ccp src );
char * MemLowerE ( char * buf, ccp buf_end, mem_t src );
char * MemLowerS ( char * buf, size_t bufsize, mem_t src );
char * MemUpperE ( char * buf, ccp buf_end, mem_t src );
char * MemUpperS ( char * buf, size_t bufsize, mem_t src );
// special handling for unsigned decimals
int StrNumCmp ( ccp a, ccp b );
// count the number of equal bytes and return a value of 0 to SIZE
uint CountEqual ( cvp m1, cvp m2, uint size );
// Concatenate path + path, return pointer to buf
// Return pointer to buf or path1|path2|buf or alloced string
char *PathCatBufPP ( char *buf, size_t bufsize, ccp path1, ccp path2 );
char *PathCatBufPPE ( char *buf, size_t bufsize, ccp path1, ccp path2, ccp ext );
ccp PathCatPP ( char *buf, size_t bufsize, ccp path1, ccp path2 );
//ccp PathCatPPE ( char *buf, size_t bufsize, ccp path1, ccp path2, ccp ext );
ccp PathAllocPP ( ccp path1, ccp path2 );
ccp PathAllocPPE ( ccp path1, ccp path2, ccp ext );
// inline wrapper
static inline ccp PathCatPPE ( char *buf, size_t bufsize, ccp path1, ccp path2, ccp ext )
{ return PathCatBufPPE(buf,bufsize,path1,path2,ext); }
// Same as PathCatPP*(), but use 'base' as prefix for relative pathes.
// If 'base' is NULL (but not empty), use getcwd() instead.
// PathCatBufBP() and PathCatBP() are special: path can be part of buf
char *PathCatBufBP ( char *buf, size_t bufsize, ccp base, ccp path );
char *PathCatBufBPP ( char *buf, size_t bufsize, ccp base, ccp path1, ccp path2 );
char *PathCatBufBPPE ( char *buf, size_t bufsize, ccp base, ccp path1, ccp path2, ccp ext );
ccp PathCatBP ( char *buf, size_t bufsize, ccp base, ccp path );
ccp PathCatBPP ( char *buf, size_t bufsize, ccp base, ccp path1, ccp path2 );
ccp PathCatBPPE ( char *buf, size_t bufsize, ccp base, ccp path1, ccp path2, ccp ext );
ccp PathAllocBP ( ccp base, ccp path );
ccp PathAllocBPP ( ccp base, ccp path1, ccp path2 );
ccp PathAllocBPPE ( ccp base, ccp path1, ccp path2, ccp ext );
char * PathCombine ( char *temp_buf, uint buf_size, ccp path, ccp base_path );
char * PathCombineFast ( char *dest_buf, uint buf_size, ccp path, ccp base_path );
// Remove last extension and add a new one.
// If ext==NULL or emtpy: remove only
// 'ext' may start with '.'. If not, a additional '.' is included
// If 'path' is NULL or path==buf: Inplace job using 'buf' as source.
char * NewFileExtE ( char * buf, ccp buf_end, ccp path, ccp ext );
char * NewFileExtS ( char * buf, size_t bufsize, ccp path, ccp ext );
// eliminate leading './' before comparing
int StrPathCmp ( ccp path1, ccp path2 );
int PathCmp
(
ccp path1, // NULL or first path
ccp path2, // NULL or second path
uint mode // bit field:
// 1: skip (multiple) leading './'
// 2: compare case insensitive
// 4: sort with respect for unsigned decimal numbers
);
int ReplaceToBuf
(
// returns -1 on error (buf to small) or the length of the resulting string.
// In the later case, a 0 byte is appended, but not counting for the length.
char *buf, // pointer to buf
uint buf_size, // size of 'buf'
ccp src1, // NULL or source, maybe part of 'buf'
int src1_len, // length of 'src1', if -1: determine by strlen()
int pos, // robust replace position, <0: relative to end
int rm_len, // robust num of chars to remove, <0: rm before 'pos'
ccp src2, // NULL or source to insert, must not be part of 'buf'
int src2_len // lengh of 'src2', if -1: determine by strlen()
);
int NormalizeIndent ( int indent );
//-----------------------------------------------------
// skips all character 1..SPACE; 'src' can be NULL
char * SkipControls ( ccp src );
// skips all character 0..SPACE; if end==NULL: Use SkipControls(); 'src' can be NULL
char * SkipControlsE ( ccp src, ccp end );
// same as above, but skip also 'ch1'; 'src' can be NULL
char * SkipControls1 ( ccp src, char ch1 );
char * SkipControlsE1 ( ccp src, ccp end, char ch1 );
// same as above, but skip also 'ch1' and 'ch2'; 'src' can be NULL
char * SkipControls2 ( ccp src, char ch1, char ch2 );
char * SkipControlsE2 ( ccp src, ccp end, char ch1, char ch2 );
//-----------------------------------------------------
// return the number of chars until first EOL ( \0 | \r | \n );
// pointer are NULL save
uint LineLen ( ccp ptr );
uint LineLenE ( ccp ptr, ccp end );
//-----------------------------------------------------
uint TrimBlanks
(
char *str, // pointer to string, modified
ccp end // NULL or end of string
);
char * ScanName
(
// return a pointer to the first unused character
char *buf, // destination buffer, terminated with NULL
uint buf_size, // size of 'buf', must be >0
ccp str, // source string
ccp end, // NULL or end of 'str'
bool allow_signs // true: allow '-' and '+'
);
//-----------------------------------------------------
// Format of version number: AABBCCDD = A.BB | A.BB.CC
// If D != 0x00 && D != 0xff => append: 'beta' D
//-----------------------------------------------------
char * PrintVersion
(
char * buf, // result buffer
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u32 version // version number to print
);
char * PrintID
(
const void * id, // ID to convert in printable format
size_t id_len, // len of 'id'
void * buf // Pointer to buffer, size >= id_len + 1
// If NULL, a local circulary static buffer is used
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// circ buf ///////////////
///////////////////////////////////////////////////////////////////////////////
#define CIRC_BUF_MAX_ALLOC 0x0400 // request limit
#define CIRC_BUF_SIZE 0x4000 // internal buffer size
char * GetCircBuf
(
// never returns NULL, but always ALIGN(4)
u32 buf_size // wanted buffer size, caller must add 1 for NULL-term
// if buf_size > CIRC_BUF_MAX_ALLOC:
// ==> ERROR0(ERR_OUT_OF_MEMORY)
);
char * CopyCircBuf
(
// never returns NULL, but always ALIGN(4)
cvp data, // source to copy
u32 data_size // see GetCircBuf()
);
char * CopyCircBuf0
(
// never returns NULL, but always ALIGN(4)
// an additional char is alloced and set to NULL
cvp data, // source to copy
u32 data_size // see GetCircBuf()
);
char * PrintCircBuf
(
ccp format, // format string for vsnprintf()
... // arguments for 'format'
)
__attribute__ ((__format__(__printf__,1,2)));
void ReleaseCircBuf
(
ccp end_buf, // pointer to end of previous alloced buffer
uint release_size // number of bytes to give back from end
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// struct IntMode_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[IntMode_t]]
typedef enum IntMode_t
{
IMD_UNSET = 0, // default, always 0
//--- big endian modes
IMD_BE0, // default big endian
IMD_BE1,
IMD_BE2,
IMD_BE3,
IMD_BE4,
IMD_BE5,
IMD_BE6,
IMD_BE7,
IMD_BE8,
//--- little endian modes
IMD_LE0, // default little endian
IMD_LE1,
IMD_LE2,
IMD_LE3,
IMD_LE4,
IMD_LE5,
IMD_LE6,
IMD_LE7,
IMD_LE8,
//--- local endian modes
#ifdef LITTLE_ENDIAN
IMD_0 = IMD_LE0,
IMD_1 = IMD_LE1,
IMD_2 = IMD_LE2,
IMD_3 = IMD_LE3,
IMD_4 = IMD_LE4,
IMD_5 = IMD_LE5,
IMD_6 = IMD_LE6,
IMD_7 = IMD_LE7,
IMD_8 = IMD_LE8,
#else
IMD_0 = IMD_BE0,
IMD_1 = IMD_BE1,
IMD_2 = IMD_BE2,
IMD_3 = IMD_BE3,
IMD_4 = IMD_BE4,
IMD_5 = IMD_BE5,
IMD_6 = IMD_BE6,
IMD_7 = IMD_BE7,
IMD_8 = IMD_BE8,
#endif
}
__attribute__ ((packed)) IntMode_t;
//-----------------------------------------------------------------------------
ccp GetIntModeName ( IntMode_t mode );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// struct FastBuf_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[FastBuf_t]]
typedef struct FastBuf_t
{
char *buf; // pointer to buffer
char *ptr; // pointer to first unused char (always space for 0-term)
char *end; // pointer to end of buffer := buf+size-1
uint fast_buf_size; // size of 'fast_buf+space'
char fast_buf[4]; // first use buffer with minimal space
char space[]; // additional space
}
FastBuf_t;
///////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------
//
// 4 examples for initialization:
//
// FastBuf_t fb1;
// InitializeFastBufAlloc(&fb1,1000);
// printf("fb1: %s: %s\n", GetFastBufStatus(&fb1), fb1.buf );
//
// FastBuf_t fb2;
// InitializeFastBuf(&fb2,sizeof(fb2));
// printf("fb2: %s: %s\n", GetFastBufStatus(&fb2), fb2.buf );
//
// struct { FastBuf_t b; char space[2000]; } fb3;
// InitializeFastBuf(&fb3,sizeof(fb3));
// printf("fb3: %s: %s\n", GetFastBufStatus(&fb.b), fb3.b.buf );
//
// char fb4x[2000];
// FastBuf_t *fb4 = InitializeFastBuf(fb4x,sizeof(fb4x));
// printf("fb4: %s: %s\n", GetFastBufStatus(fb4), fb4->buf );
//
//------------------------------------------------------------------
FastBuf_t * InitializeFastBuf ( cvp mem, uint size );
FastBuf_t * InitializeFastBufAlloc ( FastBuf_t *fb, uint size );
void ResetFastBuf ( FastBuf_t *fb );
static inline void ClearFastBuf ( FastBuf_t *fb )
{ DASSERT(fb); fb->ptr = fb->buf; }
ccp GetFastBufStatus ( const FastBuf_t *fb );
int ReserveSpaceFastBuf ( FastBuf_t *fb, uint size );
char * GetSpaceFastBuf ( FastBuf_t *fb, uint size );
uint InsertFastBuf ( FastBuf_t *fb, int index, cvp source, int size );
char * AppendFastBuf ( FastBuf_t *fb, cvp source, int size );
char * WriteFastBuf ( FastBuf_t *fb, uint offset, cvp source, int size );
uint AssignFastBuf ( FastBuf_t *fb, cvp source, int size );
uint AlignFastBuf ( FastBuf_t *fb, uint align, u8 fill );
uint DropFastBuf ( FastBuf_t *fb, int index, int size );
void CopyFastBuf ( FastBuf_t *dest, const FastBuf_t *src );
void MoveFastBuf ( FastBuf_t *dest, FastBuf_t *src );
static inline void AppendMemFastBuf ( FastBuf_t *fb, const mem_t mem )
{ AppendFastBuf(fb,mem.ptr,mem.len); }
static inline void AppendCharFastBuf ( FastBuf_t *fb, char ch )
{
DASSERT(fb);
GetSpaceFastBuf(fb,1)[0] = ch;
}
void AppendUTF8CharFastBuf ( FastBuf_t *fb, u32 code );
void AppendBE16FastBuf ( FastBuf_t *fb, u16 num );
void AppendBE32FastBuf ( FastBuf_t *fb, u32 num );
void AppendInt64FastBuf ( FastBuf_t *fb, u64 val, IntMode_t mode );
static inline void AppendU16FastBuf ( FastBuf_t *fb, u16 num )
{ AppendFastBuf(fb,&num,sizeof(num)); }
static inline void AppendU32FastBuf ( FastBuf_t *fb, u32 num )
{ AppendFastBuf(fb,&num,sizeof(num)); }
static inline void AppendU64FastBuf ( FastBuf_t *fb, u64 num )
{ AppendFastBuf(fb,&num,sizeof(num)); }
static inline int GetFastBufLen ( const FastBuf_t *fb )
{ DASSERT(fb); return fb->ptr - fb->buf; }
//-----------------------------------------------------------------------------
// The resulting strings of GetFastBufString() and GetFastBufMem0() are always
// terminated by an additional NULL byte.
// The resulting strings of GetFastBufMem() is not terminated by a NULL byte.
//-----------------------------------------------------------------------------
static inline char * GetFastBufString ( const FastBuf_t *fb )
{
DASSERT(fb);
*fb->ptr = 0;
return fb->buf;
}
static inline mem_t GetFastBufMem0 ( const FastBuf_t *fb )
{
DASSERT(fb);
*fb->ptr = 0;
mem_t mem = { fb->buf, fb->ptr - fb->buf };
return mem;
}
static inline mem_t GetFastBufMem ( const FastBuf_t *fb )
{
DASSERT(fb);
mem_t mem = { fb->buf, fb->ptr - fb->buf };
return mem;
}
//-----------------------------------------------------------------------------
// The allocated result is always terminated by an additional NULL byte.
// FastBuf_t itself is reset, similar to ResetFastBuf()
char * MoveFromFastBufString ( FastBuf_t *fb );
mem_t MoveFromFastBufMem ( FastBuf_t *fb );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// MatchPattern() ///////////////
///////////////////////////////////////////////////////////////////////////////
#define PATTERN_WILDCARDS "*# ?[{"
bool MatchPatternFull
(
ccp pattern, // pattern text
ccp text // raw text
);
bool MatchPattern
(
ccp pattern, // pattern text
ccp text, // raw text
char path_sep // path separator character, standard is '/'
);
char * MatchRuleLine
(
// returns a pointer to the first non scanned char
int *status, // not NULL: return match status here
// -2: no prefix found (no ruile found)
// -1: empty line (no ruile found)
// 0: rule found, but don't match
// 1: rule found and match
ccp src, // source line, scanned untilCONTROL
char rule_prefix, // NULL or a rule-beginning-char'
ccp path, // path to verify
char path_sep // path separator character, standard is '/'
);
///////////////////////////////////////////////////////////////////////////////
static inline int GetHexDigit ( char ch )
{
return ch >= '0' && ch <= '9' ? ch-'0'
: ch >= 'a' && ch <= 'f' ? ch-'a'+10
: ch >= 'A' && ch <= 'F' ? ch-'A'+10
: -1;
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// SplitArg_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[SplitArg_t]]
typedef struct SplitArg_t
{
uint argc; // number of arguments in 'argv'
char **argv; // array with 'argc' arguments + a NULL term, alloced
uint argv_size; // number of alloced pointers for 'argv' without NULL term
char *temp; // NULL or temporary buffer
uint temp_size; // size of 'temp'
}
SplitArg_t;
static inline void InitializeSplitArg ( SplitArg_t *sa )
{ DASSERT(sa); memset(sa,0,sizeof(*sa)); }
void ResetSplitArg ( SplitArg_t *sa );
int ScanSplitArg
(
// returns arg->argc;
SplitArg_t *arg, // pointer to object
bool init_arg, // true: call InitializeSplitArg()
ccp src, // NULL or source string
ccp src_end, // end of 'src'; if NULL: calculated by strlen(src)
char *work_buf // pointer to a buffer for strings, may be 'src'
// size must be: length of 'src' + 1
// if NULL, a temporary buffer is alloced.
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// arg_manager_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[arg_manager_t]]
typedef struct arg_manager_t
{
char **argv; // list of strings
int argc; // number of used elements in 'argv'
uint size; // >0: 'argv' is alloced to store # elements
// last NULL element is not counted here
}
arg_manager_t;
//-----------------------------------------------------------------------------
void ResetArgManager ( arg_manager_t *am );
void SetupArgManager ( arg_manager_t *am, int argc, char ** argv, bool clone );
void AttachArgManager ( arg_manager_t *am, int argc, char ** argv );
void CloneArgManager ( arg_manager_t *am, int argc, char ** argv );
void AddSpaceArgManager ( arg_manager_t *am, int needed_space );
void CopyArgManager ( arg_manager_t *dest, const arg_manager_t *src );
void MoveArgManager ( arg_manager_t *dest, arg_manager_t *src );
void PrepareEditArgManager ( arg_manager_t *am, int needed_space );
uint AppendArgManager ( arg_manager_t *am, ccp arg1, ccp arg2, bool move_arg );
uint InsertArgManager ( arg_manager_t *am, int pos, ccp arg1, ccp arg2, bool move_arg );
uint ReplaceArgManager( arg_manager_t *am, int pos, ccp arg1, ccp arg2, bool move_arg );
uint RemoveArgManager ( arg_manager_t *am, int pos, int count );
void DumpArgManager ( FILE *f, int indent, const arg_manager_t *am, ccp title );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// ContainerData_t, Container_t ///////////////
///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
// OVERVIEW
//-----------------------------------------------------------------------------
// Container_t * CreateContainer ( Container_t, protect, data,size, CopyMode_t )
// Container_t * InheritContainer ( Container_t cur, protect, Container_t parent, data,size )
// bool AssignContainer ( Container_t, protect, data,size, CopyMode_t )
// void ResetContainer ( Container_t )
// void DeleteContainer ( Container_t )
// void UnlinkContainerData ( Container_t )
// ContainerData_t * LinkContainerData ( Container_t )
// ContainerData_t * MoveContainerData ( Container_t )
// Container_t * CatchContainerData ( Container_t, int protect, ContainerData_t )
// Container_t * UseContainerData ( Container_t, int protect, Container_t )
//------------
// ContainerData_t DisjoinContainerData ( Container_t )
// void JoinContainerData ( Container_t, ContainerData_t )
// void FreeContainerData ( ContainerData_t )
//------------
// static inline bool ModificationAllowed ( const Container_t )
// void ModifyAllContainer ( Container_t )
// void ModifyContainer ( Container_t, data, size, CopyMode_t )
// int SetProtectContainer ( Container_t, int new_protect )
// static inline int AddProtectContainer ( Container_t, int add_protect )
// static inline bool IsValidContainer ( const Container_t * c )
// static inline bool InContainerP ( const Container_t, cvp ptr )
// static inline bool InContainerS ( const Container_t, cvp ptr, uint size )
// static inline bool InContainerE ( const Container_t, cvp ptr, cvp end )
// uint DumpInfoContainer ( f,colset,indent,prefix, Container_t, hexdump_len )
// uint DumpInfoContainerData ( f,col,indent,prefix, CData_t, hex_len, hex_indent )
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
// [[ContainerData_t]]
typedef struct ContainerData_t
{
u8 *data; // data
uint size; // size of 'data'
int ref_count; // reference counter
int protect_count; // >0: don't modify data
bool data_alloced; // true: free data
} ContainerData_t;
//-----------------------------------------------------------------------------
// [[Container_t]]
typedef struct Container_t
{
ContainerData_t *cdata; // NULL or pointer to ContainerData
int protect_level; // >0: don't modify data
} Container_t;
///////////////////////////////////////////////////////////////////////////////
Container_t * CreateContainer
(
// returns 'c' or the alloced container
// 'c' is always initialized
Container_t *c, // valid container, alloc one if NULL
int protect, // initial value for protection
const void *data, // data to copy/move/link
uint size, // size of 'data'
CopyMode_t mode // copy mode on creation
);
//-----------------------------------------------------------------------------
Container_t * InheritContainer
(
// returns 'c' or the alloced container
Container_t *c, // valid container, alloc one if NULL
int protect, // initial value for protection
const Container_t *parent, // NULL or valid parent container
const void *data, // data to copy/move/link
uint size // size of 'data'
);
//-----------------------------------------------------------------------------
bool AssignContainer
(
// return TRUE on new ContainerData_t
Container_t *c, // valid container; if NULL: only FREE(data)
int protect, // new protection level
const void *data, // data to copy/move/link
uint size, // size of 'data'
CopyMode_t mode // copy mode on creation
);
//-----------------------------------------------------------------------------
void ResetContainer
(
Container_t *c // container to reset => no data
);
//-----------------------------------------------------------------------------
void DeleteContainer
(
Container_t *c // container to reset and to free => no data
);
//-----------------------------------------------------------------------------
void UnlinkContainerData
(
Container_t *c // container to reset => no data
);
///////////////////////////////////////////////////////////////////////////////
ContainerData_t * LinkContainerData
(
// increment 'ref_count' and return NULL or current ContainerData
// => use CatchContainerData() to complete operation
const Container_t *c // NULL or valid container
);
//-----------------------------------------------------------------------------
ContainerData_t * MoveContainerData
(
// return NULL or unlinked current ContainerData
// => use CatchContainerData() to complete operation
Container_t *c // NULL or valid container
);
//-----------------------------------------------------------------------------
Container_t * CatchContainerData
(
// returns 'c' or the alloced container
// 'c' is always initialized
Container_t *c, // valid container, alloc one if NULL
int protect, // initial value for protection
ContainerData_t *cdata // if not NULL: catch this
);
//-----------------------------------------------------------------------------
static inline Container_t * UseContainerData
(
// returns 'c' or the alloced container
Container_t *c, // valid container, alloc one if NULL
int protect, // initial value for protection
const Container_t *src // if not NULL: catch this
)
{
return CatchContainerData(c,protect,LinkContainerData(src));
}
///////////////////////////////////////////////////////////////////////////////
ContainerData_t * DisjoinContainerData
(
// Disjoin data container date without freeing it. Call either
// JoinContainerData() or FreeContainerData() to finish operation.
// Reference counters are not modified.
// Return the data container or NULL
Container_t *c // NULL or valid container
);
//-----------------------------------------------------------------------------
void JoinContainerData
(
// Join a data container, that was diojoined by DisjoinContainerData()
// Reference counters are not modified.
Container_t *c, // if NULL: FreeContainerData()
ContainerData_t *cdata // NULL or container-data to join
);
//-----------------------------------------------------------------------------
void FreeContainerData
(
// Decrement the reference counter of an already disjoined data container
// and free it if unused.
ContainerData_t *cdata // NULL or container-data to free
);
///////////////////////////////////////////////////////////////////////////////
static inline bool ModificationAllowed ( const Container_t *c )
{ return c && c->cdata
&& ( c->cdata->ref_count <= 1
|| c->cdata->protect_count <= ( c->protect_level > 0 ) );
}
//-----------------------------------------------------------------------------
bool ModifyAllContainer
(
// prepare modification of container-data, create a copy if necessary
// return true, if a new container-data is used
Container_t *c // NULL or valid container
);
//-----------------------------------------------------------------------------
bool ModifyContainer
(
// prepare modification of container-data, create an extract if necessary
// return true, if a new container-data is used
Container_t *c, // NULL or valid container
const void *data, // data to copy/move/link
uint size, // size of 'data'
CopyMode_t mode // copy mode on creation
);
//-----------------------------------------------------------------------------
int SetProtectContainer
(
// returns 'c' new protection level
Container_t *c, // valid container, alloc one if NULL
int new_protect // new protection value
);
//-----------------------------------------------------------------------------
static inline int AddProtectContainer
(
// returns 'c' new protection level
Container_t *c, // valid container, alloc one if NULL
int add_protect // new protection value
)
{
DASSERT(c);
return SetProtectContainer(c,c->protect_level+add_protect);
}
///////////////////////////////////////////////////////////////////////////////
static inline bool IsValidContainer ( const Container_t * c )
{ return c && c->cdata && c->cdata->data; }
//-----------------------------------------------------------------------------
static inline bool InContainerP ( const Container_t * c, cvp ptr )
{ return c && c->cdata && ptr
&& (u8*)ptr >= c->cdata->data
&& (u8*)ptr <= c->cdata->data + c->cdata->size; }
//-----------------------------------------------------------------------------
static inline bool InContainerS ( const Container_t * c, cvp ptr, uint size )
{ return c && c->cdata && ptr
&& (u8*)ptr >= c->cdata->data
&& (u8*)ptr + size <= c->cdata->data + c->cdata->size; }
//-----------------------------------------------------------------------------
static inline bool InContainerE ( const Container_t * c, cvp ptr, cvp end )
{ return c && c->cdata
&& ptr && (u8*)ptr >= c->cdata->data
&& end && (u8*)end <= c->cdata->data + c->cdata->size; }
///////////////////////////////////////////////////////////////////////////////
struct ColorSet_t;
uint DumpInfoContainer
(
// return the number of printed lines
FILE *f, // valid output file
const ColorSet_t *colset, // NULL or color set
int indent, // indent the output
ccp prefix, // not NULL: use it as prefix behind indention
const Container_t *c, // dump infos for this container, NULL allowed
uint hexdump_len // max number of bytes used for a hexdump
);
//-----------------------------------------------------------------------------
uint DumpInfoContainerData
(
// return the number of printed lines
FILE *f, // valid output file
const ColorSet_t *colset, // NULL or color set
int indent, // indent of output
ccp prefix, // not NULL: use it as prefix behind indention
const
ContainerData_t *cdata, // dump infos for this container-data, NULL allowed
uint hexdump_len, // max number of bytes used for a hexdump
int hexdump_indent // indent of hexdump
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// print lines with autobreak ///////////////
///////////////////////////////////////////////////////////////////////////////
void PutLines
(
FILE * f, // valid output stream
int indent, // indent of output
int fw, // field width of output
int first_line, // length without prefix of already printed first line
ccp prefix, // NULL or prefix for each line
ccp text, // text to print
ccp eol // End-Of-Line text. If NULL -> LF
);
void PrintArgLines
(
FILE * f, // valid output stream
int indent, // indent of output
int fw, // field width of output
int first_line, // length without prefix of already printed first line
ccp prefix, // NULL or prefix for each line
ccp format, // format string for vsnprintf()
va_list arg // parameters for 'format'
);
void PrintLines
(
FILE * f, // valid output stream
int indent, // indent of output
int fw, // field width of output
int first_line, // length without prefix of already printed first line
ccp prefix, // NULL or prefix for each line
ccp format, // format string for vsnprintf()
... // arguments for 'vsnprintf(format,...)'
)
__attribute__ ((__format__(__printf__,6,7)));
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
// Rules for P*ColoredLines():
//
// |+ : Define a TAB pos (not is defined at start)
// |- : Remove a TAB pos
// |[pos,...] : Comma separated list: Clear all tabs and define new TABs
// POS : Define TAB add POS
// POS*DELTA : Define TAB add POS and then at every DELTA
// \t : Jump to TAB stop
//
// | or |> : Define an indention for continuation lines.
// Use the single char only, if a letter, digit, underline, SPACE,
// CTRL or '{' follows. All ather chars are reserved for extensions.
// \r : Force a new continuation line.
// \n : Terminate current line.
// \r\n : Same as '\n'.
//
// {name} : Change active color to NAME
// {name|text} : Use color NAME only for TEXT, then restore active color.
// {} : Reset and deactivate all colors.
//
// || : Print Single '|'
// {{ : Print Single '{'
//
///////////////////////////////////////////////////////////////////////////////
uint PutColoredLines
(
// returns the number of written lines
FILE * f, // valid output stream
const ColorSet_t *colset, // NULL or color set
int indent, // indent of output
int fw, // field width; indent+prefix+eol don't count
ccp prefix, // NULL or prefix for each line
ccp eol, // End-Of-Line text. If NULL -> LF
ccp text // text to print
);
uint PrintArgColoredLines
(
// returns the number of written lines
FILE * f, // valid output stream
const ColorSet_t *colset, // NULL or color set
int indent, // indent of output
int fw, // field width; indent+prefix+eol don't count
ccp prefix, // NULL or prefix for each line
ccp eol, // End-Of-Line text. If NULL -> LF
ccp format, // format string for vsnprintf()
va_list arg // parameters for 'format'
);
uint PrintColoredLines
(
// returns the number of written lines
FILE * f, // valid output stream
const ColorSet_t *colset, // NULL or color set
int indent, // indent of output
int fw, // field width; indent+prefix+eol don't count
ccp prefix, // NULL or prefix for each line
ccp eol, // End-Of-Line text. If NULL -> LF
ccp format, // format string for vsnprintf()
... // arguments for 'vsnprintf(format,...)'
)
__attribute__ ((__format__(__printf__,7,8)));
//
///////////////////////////////////////////////////////////////////////////////
/////////////// encoding/decoding ///////////////
///////////////////////////////////////////////////////////////////////////////
extern const u32 TableCRC32[0x100];
u32 CalcCRC32 ( u32 crc, cvp buf, uint size );
///////////////////////////////////////////////////////////////////////////////
extern const u16 TableCP1252_80[0x20];
///////////////////////////////////////////////////////////////////////////////
// [[CharMode_t]]
typedef enum CharMode_t // select encodig/decoding method
{
CHMD_UTF8 = 0x01, // UTF8 support enabled (compatible with legacy bool)
CHMD_ESC = 0x02, // escape ESC by \e
CHMD__ALL = CHMD_UTF8 | CHMD_ESC,
}
__attribute__ ((packed)) CharMode_t;
///////////////////////////////////////////////////////////////////////////////
// [[EncodeMode_t]]
typedef enum EncodeMode_t // select encodig/decoding method
{
ENCODE_OFF, // no encoding (always 0)
ENCODE_STRING, // ScanEscapedString(ANSI), byte mode
ENCODE_UTF8, // ScanEscapedString(UTF8), force UTF8 on decoding
ENCODE_BASE64, // Base64
ENCODE_BASE64URL, // Base64.URL (=) / decoder detects Standard + URL + STAR
ENCODE_BASE64STAR, // Base64.URL (*) / decoder detects Standard + URL + STAR
ENCODE_BASE64XML, // Base64 with XML name tokens / decoder detects Standard + XML (name+id)
ENCODE_JSON, // JSON string encoding
ENCODE__N // number of encoding modes
}
EncodeMode_t;
// [[doxygen]]
static inline bool NeedsQuotesByEncoding ( EncodeMode_t em )
{ return em > ENCODE_OFF && em < ENCODE_BASE64 || em > ENCODE_BASE64XML; }
ccp GetEncodingName ( EncodeMode_t em );
///////////////////////////////////////////////////////////////////////////////
// [[DecodeType_t]]
enum DecodeType_t // for decoding tables
{
DECODE_NULL = -1,
DECODE_CONTROL = -2,
DECODE_LINE = -3,
DECODE_SPACE = -4,
DECODE_SEPARATE = -5,
DECODE_FILLER = -6,
DECODE_OTHER = -7
};
extern const char TableNumbers[256];
extern char TableDecode64[256]; // Standard coding
extern char TableDecode64url[256]; // Standard + URL + STAR coding
extern char TableDecode64xml[256]; // Standard + XML (name tokens + identifiers)
extern const char TableEncode64[64+1]; // last char is filler
extern const char TableEncode64url[64+1]; // URL coding, used by Nintendo
extern const char TableEncode64star[64+1]; // Like URL, with '*' instead of '=' as filler
extern const char TableEncode64xml[64+1]; // XML name tokens
// for tests: encode the 48 bytes to get the full BASE64 alphabet
extern const u8 TableAlphabet64[48];
// Default tables for DecodeBase64() and EncodeBase64(), if no table is defined.
// They are initialized with TableDecode64[] and TableEncode64[].
extern ccp TableDecode64default, TableEncode64default;
///////////////////////////////////////////////////////////////////////////////
char * PrintEscapedString
(
// returns 'buf'
char *buf, // valid destination buffer
uint buf_size, // size of 'buf', >= 10
ccp source, // NULL string to print
int len, // length of string. if -1, str is null terminated
CharMode_t char_mode, // modes, bit field (CHMD_*)
char quote, // NULL or quotation char, that must be quoted
uint *dest_len // not NULL: Store length of result here
);
///////////////////////////////////////////////////////////////////////////////
uint ScanEscapedString
(
// returns the number of valid bytes in 'buf' (NULL term not counted)
char *buf, // valid destination buffer, maybe source
uint buf_size, // size of 'buf'
ccp source, // string to scan
int len, // length of string. if -1, str is null terminated
bool utf8, // true: source and output is UTF-8
int quote, // 0:none, -1:auto, >0: quotation char
uint *scanned_len // not NULL: Store number of scanned 'source' bytes here
);
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
u64 DecodeU64
(
ccp source, // string to decode
int len, // length of string. if -1, str is null terminated
uint base, // number of possible digits (2..36):
uint *scanned_len // not NULL: Store number of scanned 'str' bytes here
);
///////////////////////////////////////////////////////////////////////////////
ccp EncodeU64
(
char * buf, // result buffer (size depends on base)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u64 num, // number to encode
uint base // number of possible digits (2..64):
);
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
uint DecodeBase64
(
// returns the number of valid bytes in 'buf'
char *buf, // valid destination buffer
uint buf_size, // size of 'buf', >= 3
ccp source, // NULL or string to decode
int len, // length of string. if -1, str is null terminated
const char decode64[256], // decoding table; if NULL: use TableDecode64default
bool allow_white_spaces, // true: skip white spaces
uint *scanned_len // not NULL: Store number of scanned 'str' bytes here
);
//-----------------------------------------------------------------------------
mem_t DecodeBase64Circ
(
// Returns a buffer alloced by GetCircBuf()
// with valid pointer and null terminated.
// If result is too large (>CIRC_BUF_MAX_ALLOC) then (0,0) is returned.
ccp source, // NULL or string to decode
int len, // length of string. if -1, str is null terminated
const char decode64[256] // decoding table; if NULL: use TableDecode64default
);
///////////////////////////////////////////////////////////////////////////////
uint EncodeBase64
(
// returns the number of scanned bytes of 'source'
char *buf, // valid destination buffer
uint buf_size, // size of 'buf', >= 4
const void *source, // NULL or data to encode
int source_len, // length of 'source'; if <0: use strlen(source)
const char encode64[64+1], // encoding table; if NULL: use TableEncode64default
bool use_filler, // use filler for aligned output
ccp next_line, // not NULL: use this string as new line sep
uint bytes_per_line // >0: use 'next_line' every # input bytes
// will be rounded down to multiple of 3
);
//-----------------------------------------------------------------------------
mem_t EncodeBase64Circ
(
// Returns a buffer alloced by GetCircBuf()
// with valid pointer and null terminated.
// If result is too large (>CIRC_BUF_MAX_ALLOC) then (0,0) is returned.
const void *source, // NULL or data to encode
int source_len, // length of 'source'; if <0: use strlen(source)
const char encode64[64+1] // encoding table; if NULL: use TableEncode64default
);
//-----------------------------------------------------------------------------
uint EncodeBase64ml // ml: multi line
(
// returns the number of scanned bytes of 'source'
char *buf, // valid destination buffer
uint buf_size, // size of 'buf', >= 4
const void *source, // NULL or data to encode
int source_len, // length of 'source'; if <0: use strlen(source)
const char encode64[64+1], // encoding table; if NULL: use TableEncode64default
bool use_filler, // use filler for aligned output
int indent, // indention of output
ccp prefix, // NULL or prefix before encoded data
ccp eol, // line terminator, if NULL then use NL
int bytes_per_line // create a new line every # input bytes
// will be rounded down to multiple of 3
);
///////////////////////////////////////////////////////////////////////////////
static inline uint GetEncodeBase64Len ( uint src_len )
{ return 4 * (src_len+2) / 3; }
static inline uint GetEncodeBase64FillLen ( uint src_len )
{ return (src_len+2) / 3 * 4; }
static inline uint GetDecodeBase64Len ( uint src_len )
{ return 3 * src_len / 4; }
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static inline uint DecodeJSON
(
// returns the number of valid bytes in 'buf'
char *buf, // valid destination buffer
uint buf_size, // size of 'buf', >= 3
ccp source, // NULL or string to decode
int source_len, // length of 'source'. If -1, str is NULL terminated
int quote, // 0:none, -1:auto, >0: quotation char
uint *scanned_len // not NULL: Store number of scanned 'str' bytes here
)
{
return ScanEscapedString(buf,buf_size,source,source_len,true,quote,scanned_len);
}
//-----------------------------------------------------------------------------
mem_t DecodeJSONCirc
(
// Returns a buffer alloced by GetCircBuf()
// with valid pointer and null terminated.
// If result is too large (>CIRC_BUF_MAX_ALLOC) then (0,0) is returned.
ccp source, // NULL or string to decode
int source_len, // length of 'source'. If -1, str is NULL terminated
int quote // 0:none, -1:auto, >0: quotation char
);
///////////////////////////////////////////////////////////////////////////////
uint EncodeJSON
(
// returns the number of valid bytes in 'buf'. Result is NULL-terminated.
char *buf, // valid destination buffer
uint buf_size, // size of 'buf', >2 and 2 bytes longer than needed
const void *source, // NULL or data to encode
int source_len // length of 'source'; if <0: use strlen(source)
);
//-----------------------------------------------------------------------------
mem_t EncodeJSONCirc
(
// Returns a buffer alloced by GetCircBuf()
// with valid pointer and null terminated.
// If result is too large (>CIRC_BUF_MAX_ALLOC) then (0,0) is returned.
const void *source, // NULL or data to encode
int source_len // length of 'source'; if <0: use strlen(source)
);
//-----------------------------------------------------------------------------
mem_t QuoteJSONCirc
(
// Returns a buffer alloced by GetCircBuf()
// with valid pointer and null terminated.
// If result is too large (>CIRC_BUF_MAX_ALLOC) then (0,0) is returned.
const void *source, // NULL or data to encode
int source_len, // length of 'source'; if <0: use strlen(source)
int null_if // return 'null' without quotes ...
// <=0: never
// >=1: if source == NULL
// >=2: if source_len == 0
);
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
uint DecodeByMode
(
// returns the number of valid bytes in 'buf'. Result is NULL-terminated.
char *buf, // valid destination buffer
uint buf_size, // size of 'buf', >= 3
ccp source, // string to decode
int slen, // length of string. if -1, str is null terminated
EncodeMode_t emode, // encoding mode
uint *scanned_len // not NULL: Store number of scanned 'str' bytes here
);
///////////////////////////////////////////////////////////////////////////////
mem_t DecodeByModeMem
(
// Returns the decoded 'source'. Result is NULL-terminated.
// It points either to 'buf' or is alloced (on buf==NULL or too less space)
// If alloced (mem.ptr!=buf) => call FreeMem(&mem)
char *buf, // NULL or destination buffer
uint buf_size, // size of 'buf'
ccp source, // string to decode
int slen, // length of string. if -1, str is null terminated
EncodeMode_t emode, // encoding mode
uint *scanned_len // not NULL: Store number of scanned 'str' bytes here
);
///////////////////////////////////////////////////////////////////////////////
uint DecodeByModeMemList
(
// returns the number of scanned strings
mem_list_t *res, // result
uint res_mode, // 0:append, 1:clear, 2:init
ccp source, // string to decode
int slen, // length of string. if -1, str is null terminated
EncodeMode_t emode, // encoding mode
uint *scanned_len // not NULL: Store number of scanned 'str' bytes here
);
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
uint EncodeByMode
(
// returns the number of valid bytes in 'buf'. Result is NULL-terminated.
char *buf, // valid destination buffer
uint buf_size, // size of 'buf', >= 4
ccp source, // string to encode
int slen, // length of string. if -1, str is null terminated
EncodeMode_t emode // encoding mode
);
///////////////////////////////////////////////////////////////////////////////
mem_t EncodeByModeMem
(
// Returns the encoded 'source'. Result is NULL-terminated.
// It points either to 'buf' or is alloced (on buf==NULL or too less space)
// If alloced (mem.ptr!=buf) => call FreeMem(&mem)
char *buf, // NULL or destination buffer
uint buf_size, // size of 'buf'
ccp source, // string to encode
int slen, // length of string. if -1, str is null terminated
EncodeMode_t emode // encoding mode
);
///////////////////////////////////////////////////////////////////////////////
#define SJIS_TAB_MAPPING_BEG 0xeea0 // first code used for table mapping
#define SJIS_TAB_MAPPING_END 0xeecf // last code used for table mapping
#define SJIS_INVALID_CODE 0xffff // invalid code
#define SJIS_CACHE_SIZE 0xffff // number of elements in cache
static inline bool IsValidShiftJIS ( uint code )
{ return code < SJIS_TAB_MAPPING_BEG
|| code > SJIS_TAB_MAPPING_END && code < SJIS_INVALID_CODE; }
int ScanShiftJISChar ( cucp * str );
int ScanShiftJISCharE ( cucp * str, cvp end );
void SetupGetShiftJISCache(void);
int GetShiftJISChar ( u32 code );
ccp GetShiftJISStatistics(void);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// scan keywords ///////////////
///////////////////////////////////////////////////////////////////////////////
#define KEYWORD_NAME_MAX 100
typedef s64 (*KeywordCallbackFunc)
(
void *param, // NULL or user defined parameter
ccp name, // normalized name of option
const KeywordTab_t *key_tab, // valid pointer to command table
const KeywordTab_t *key, // valid pointer to found command
char prefix, // 0 | '-' | '+' | '='
s64 result // current value of result
);
///////////////////////////////////////////////////////////////////////////////
const KeywordTab_t *GetKewordById
(
const KeywordTab_t *key_tab, // NULL or pointer to command table
s64 id // id to search
);
//-----------------------------------------------------------------------------
const KeywordTab_t *GetKewordByIdAndOpt
(
const KeywordTab_t *key_tab, // NULL or pointer to command table
s64 id, // id to search
s64 opt // opt to search
);
//-----------------------------------------------------------------------------
static inline ccp GetKewordNameById
(
const KeywordTab_t *key_tab, // NULL or pointer to command table
s64 id, // id to search
ccp res_not_found // return this if not found
)
{
const KeywordTab_t *key = GetKewordById(key_tab,id);
return key ? key->name1 : res_not_found;
}
//-----------------------------------------------------------------------------
static inline ccp GetKewordNameByIdAndOpt
(
const KeywordTab_t *key_tab, // NULL or pointer to command table
s64 id, // id to search
s64 opt, // opt to search
ccp res_not_found // return this if not found
)
{
const KeywordTab_t *key = GetKewordByIdAndOpt(key_tab,id,opt);
return key ? key->name1 : res_not_found;
}
///////////////////////////////////////////////////////////////////////////////
const KeywordTab_t * ScanKeyword
(
int *res_abbrev, // NULL or pointer to result 'abbrev_count'
ccp arg, // argument to scan
const KeywordTab_t *key_tab // valid pointer to command table
);
//-----------------------------------------------------------------------------
s64 ScanKeywordListEx
(
ccp arg, // argument to scan
const KeywordTab_t * key_tab, // valid pointer to command table
KeywordCallbackFunc func, // NULL or calculation function
bool allow_prefix, // allow '-' | '+' | '=' as prefix
u32 max_number, // allow numbers < 'max_number' (0=disabled)
s64 result, // start value for result
uint err_mode, // bit field:
// 1: continue on error
ccp err_msg, // not NULL: print a warning message:
// "<ERR_MSG>: Unknown keyword: <KEY>"
enumError err_code, // use 'err_code' for the 'err_msg'
uint *err_count // not NULL: store errors here
);
static inline s64 ScanKeywordList
(
ccp arg, // argument to scan
const KeywordTab_t * key_tab, // valid pointer to command table
KeywordCallbackFunc func, // NULL or calculation function
bool allow_prefix, // allow '-' | '+' | '=' as prefix
u32 max_number, // allow numbers < 'max_number' (0=disabled)
s64 result, // start value for result
ccp err_msg, // not NULL: print a warning message:
// "<ERR_MSG>: Unknown keyword: <KEY>"
enumError err_code // use 'err_code' for the 'err_msg'
)
{
return ScanKeywordListEx( arg, key_tab, func, allow_prefix,
max_number, result, 0, err_msg, err_code, 0 );
}
//-----------------------------------------------------------------------------
enumError ScanKeywordListFunc
(
ccp arg, // argument to scan
const KeywordTab_t *key_tab, // valid pointer to command table
KeywordCallbackFunc func, // calculation function
void *param, // used define parameter for 'func'
bool allow_prefix // allow '-' | '+' | '=' as prefix
);
//-----------------------------------------------------------------------------
s64 ScanKeywordListMask
(
ccp arg, // argument to scan
const KeywordTab_t * key_tab // valid pointer to command table
);
//-----------------------------------------------------------------------------
enumError PrintKeywordError
(
const KeywordTab_t * key_tab, // NULL or pointer to command table
ccp key_arg, // analyzed command
int key_stat, // status of ScanKeyword()
ccp prefix, // NULL or prefix for messages
ccp object // NULL or object for error messages
// default= 'command'
);
uint CollectAmbiguousKeywords
(
char *buf, // destination buffer, 200 bytes are good
uint buf_size, // size of buffer
const KeywordTab_t * key_tab, // NULL or pointer to command table
ccp key_arg // analyzed command
);
//-----------------------------------------------------------------------------
char * PrintKeywordList
(
// returns a pointer to the result buffer
char *buf, // result buffer
// If NULL, a local circulary static buffer
// with max CIRC_BUF_MAX_ALLOC bytes is used
uint buf_size, // size of 'buf', at least 10 bytes if buf is set
uint *ret_length, // not NULL: store result length here
const KeywordTab_t *tab, // related keyword table
u64 mode, // mode to print
u64 default_mode, // NULL or default mode
u64 hide_mode // bit field to hide parameters
);
//-----------------------------------------------------------------------------
int ScanKeywordOffOn
(
// returns 0 for '0|OFF'; 1 for '1|ON; -1 for empty; -2 on error
ccp arg, // argument to scan
uint max_num, // >0: additionally accept+return number <= max_num
ccp object // NULL (silent) or object for error messages
);
//-----------------------------------------------------------------------------
enumError Command_ARGTEST ( int argc, char ** argv );
enumError Command_TESTCOLORS ( int argc, char ** argv );
enumError Command_COLORS
(
int level, // only used, if mode==NULL
// < 0: status message (ignore mode)
// >= 1: include names
// >= 2: include alt names
// >= 3: include color names incl bold
// >= 4: include background color names
uint mode, // output mode => see PrintColorSetHelper()
uint format // output format => see PrintColorSetEx()
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// OFF/AUTO/ON/FORCE ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[OffOn_t]]
typedef enum OffOn_t
{
OFFON_ERROR = -99,
OFFON_OFF = -1,
OFFON_AUTO = 0,
OFFON_ON = 1,
OFFON_FORCE = 2,
}
OffOn_t;
extern const KeywordTab_t KeyTab_OFF_AUTO_ON[];
int ScanKeywordOffAutoOn
(
// returns one of OFFON_*
ccp arg, // argument to scan
int on_empty, // return this value on empty
uint max_num, // >0: additionally accept+return number <= max_num
ccp object // NULL (silent) or object for error messages
);
ccp GetKeywordOffAutoOn ( OffOn_t value );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// LOWER/AUTO/UPPER ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[LowerUpper_t]]
typedef enum LowerUpper_t
{
LOUP_ERROR = -99,
LOUP_LOWER = -1,
LOUP_AUTO = 0,
LOUP_UPPER = 1,
}
LowerUpper_t;
extern const KeywordTab_t KeyTab_LOWER_AUTO_UPPER[];
int ScanKeywordLowerAutoUpper
(
// returns one of LOUP_*
ccp arg, // argument to scan
int on_empty, // return this value on empty
uint max_num, // >0: additionally accept+return number <= max_num
ccp object // NULL (silent) or object for error messages
);
ccp GetKeywordLowerAutoUpper ( LowerUpper_t value );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// scan command lists ///////////////
///////////////////////////////////////////////////////////////////////////////
//
// Scan a semicolon separated command list with binary support of format:
// COMMAND := NAME [BINLIST] CONTROL* ['='] CONTROL* [PARAM] CONTROL* ';'
// BLANK := TAB(9) or SPACE(32)
// CONTROL := NUL..SPACE (0..32)
// NAME := 1..99 alphanumeric chars incl. minus and underline
// BINLIST := BINARY [BINLIST]
// BINARY := BLANK* '\1' be16:SIZE DATA
// PARAM := all except ';'
//
// CONTROLS are possible before and behind the '=' and at the end of param.
// Only BLANK* are allowed between command NAME and BINARY block.
// BINARY data starts with \1 and is stored as '->bin' and '->bin_len' and
// inludes SIZE and DATA. The BINARY.SIZE includes itself and is a 16-bit
// big-endian number. In '->bin[]', member 'be16:SIZE' is excluded,
// but '->bin[].ptr - 2' points to be16:SIZE.
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// [[CommandList_t]] docu -> see above
struct TCPStream_t;
#define COMMAND_LIST_N_BIN 5
typedef struct CommandList_t
{
//--- input params
ccp command; // command text
uint command_len; // total length of 'command'
char command_sep; // not NULL: alternative command separator
u8 change_case; // >0: convert command to lower(1) or upper(2) case
bool term_param; // 1: write a NULL at end-of-param => modify 'command'
// 2: write a ';' at end-of-param => modify 'command'
// in this case, ';' is counted to 'record_len'
bool is_terminated; // true: last command is terminated
// even without ';'
//--- user parameters, pass-through
struct TCPStream_t
*user_ts; // NULL or a stream for replies
void *user_ptr; // any pointer
int user_int; // any number or id
ccp log_fname; // not NULL: open log file
uint log_level; // 0:off, 1:stat+err, 2:+cmd, 3:+bin+param, 4:+func
//--- analysed data, only valid at callback
char cmd[100]; // scanned command name
uint cmd_len; // length of scanned command name
uint n_bin; // number of binary blocks
mem_t bin[COMMAND_LIST_N_BIN];
// binary data detected, if bin[].ptr not null
ccp param; // pointer to text parameter, not NULL terminated
uint param_len; // length of text parameter
const u8 *record; // complete record from NAME to END_RECORD_CHAR
uint record_len; // length of trimmed record
uint input_len; // length of complete record at input time
//--- statistics
uint read_cmd_len; // number of used bytes of 'command'
uint fail_count; // number of failed commands
uint cmd_count; // number of commands
}
CommandList_t;
// return -1 to abort scanning
typedef int (*CommandListFunc) ( CommandList_t *cli );
//-----------------------------------------------------------------------------
static inline void InitializeCommandList ( CommandList_t *cli )
{ DASSERT(cli); memset(cli,0,sizeof(*cli)); }
static inline void ClearCommandList ( CommandList_t *cli )
{ DASSERT(cli); }
static inline void ResetCommandList ( CommandList_t *cli )
{ DASSERT(cli); memset(cli,0,sizeof(*cli)); }
// a test function for ScanCommandList()
int ExecCommandOfList ( CommandList_t *cli );
int ScanCommandList
(
// scan a semicolon separated command list. See above for format
// returns -1 on 'cli->fail_count'; otherwise 'cli->cmd_count'
CommandList_t *cli, // valid command list
CommandListFunc func // NULL or function for each command
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// numeric functions ///////////////
///////////////////////////////////////////////////////////////////////////////
extern int urandom_available; // <0:not, =0:not tested, >0:ok
extern bool use_urandom_for_myrandom;
uint ReadFromUrandom ( void *dest, uint size );
///////////////////////////////////////////////////////////////////////////////
u32 MyRandom ( u32 max );
u64 MySeed ( u64 base );
u64 MySeedByTime(void);
void MyRandomFill ( void * buf, size_t size );
///////////////////////////////////////////////////////////////////////////////
void CreateUUID ( uuid_buf_t dest );
uint CreateTextUUID ( char *buf, uint bufsize );
uint PrintUUID ( char *buf, uint bufsize, uuid_buf_t uuid );
char * ScanUUID ( uuid_buf_t uuid, ccp source );
///////////////////////////////////////////////////////////////////////////////
uint gcd ( uint n1, uint n2 ); // greatest common divisor, german: ggt
u32 gcd32 ( u32 n1, u32 n2 );
u64 gcd64 ( u64 n1, u64 n2 );
uint lcm ( uint n1, uint n2 ); // lowest common multiple, german: kgv
u32 lcm32 ( u32 n1, u32 n2 );
u64 lcm64 ( u64 n1, u64 n2 );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// scan number ///////////////
///////////////////////////////////////////////////////////////////////////////
enum DigitTable_t
{
NUMBER_NULL = -1, // NULL
NUMBER_CONTROL = -2, // < 0x20, but not SPACE or LINE
NUMBER_LINE = -3, // LF, CR
NUMBER_SPACE = -4, // SPACE, TAB, VT
NUMBER_TIE = -5, // ':' '-' '.'
NUMBER_SEPARATE = -6, // ',' ';'
NUMBER_OTHER = -7 // all other
};
extern const char DigitTable[256];
//-----------------------------------------------------------------------------
u64 ScanDigits
(
// same as ScanNumber(), but other interface
// returns the scanned number
ccp *source, // pointer to source string, modified
ccp end_source, // NULL or end of 'source'
uint intbase, // integer base, 2..36
int maxchar, // max number of digits to read
uint *count // not NULL: store number of scanned digits here
);
//-----------------------------------------------------------------------------
char * ScanNumber
(
// same as ScanDigits(), but other interface
// returns a pointer to the first not used character
uint *dest_num, // store result here, never NULL
ccp src, // pointer to source string, modified
ccp src_end, // NULL or end of 'src'
uint intbase, // integer base, 2..36
int maxchar // max number of digits to read
);
//-----------------------------------------------------------------------------
char * ScanEscape
(
// returns a pointer to the first not used character
uint *dest_code, // store result here, never NULL
ccp src, // pointer to source string (behind escape char)
ccp src_end // NULL or end of 'src'
);
//-----------------------------------------------------------------------------
uint ScanHexString
(
// return the number of written bytes
void *buf, // write scanned data here
uint buf_size, // size of buf
ccp *source, // pointer to source string, modified
ccp end_source, // NULL or end of 'source'
bool allow_tie // allow chars ' .:-' and TAB,VT as byte separator
);
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
char * ScanS32
(
// return 'source' on error
s32 *res_num, // not NULL: store result (only on success)
ccp source, // NULL or source text
uint default_base // base for numbers without '0x' prefix
// 0: C like with octal support
// 10: standard value for decimal numbers
// 16: standard value for hex numbers
);
static inline char * ScanU32 ( u32 *res_num, ccp source, uint default_base )
{ return ScanS32((s32*)res_num,source,default_base); }
//-----------------------------------------------------------------------------
char * ScanS64
(
// return 'source' on error
s64 *res_num, // not NULL: store result (only on success)
ccp source, // NULL or source text
uint default_base // base for numbers without '0x' prefix
// 0: C like with octal support
// 10: standard value for decimal numbers
// 16: standard value for hex numbers
);
static inline char * ScanU64 ( u64 *res_num, ccp source, uint default_base )
{ return ScanS64((s64*)res_num,source,default_base); }
//-----------------------------------------------------------------------------
#if __WORDSIZE >= 64
static inline char * ScanINT ( int *res_num, ccp source, uint default_base )
{ return ScanS32((s32*)res_num,source,default_base); }
static inline char * ScanUINT ( uint *res_num, ccp source, uint default_base )
{ return ScanS32((s32*)res_num,source,default_base); }
#else
static inline char * ScanINT ( int *res_num, ccp source, uint default_base )
{ return ScanS64((s64*)res_num,source,default_base); }
static inline char * ScanUINT ( uint *res_num, ccp source, uint default_base )
{ return ScanS64((s64*)res_num,source,default_base); }
#endif
//-----------------------------------------------------------------------------
// strto*() replacements: Use Scan*() with better base support, NULL allowed as source
long int str2l ( const char *nptr, char **endptr, int base );
long long int str2ll ( const char *nptr, char **endptr, int base );
unsigned long int str2ul ( const char *nptr, char **endptr, int base );
unsigned long long int str2ull ( const char *nptr, char **endptr, int base );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// print numbers and size ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[sizeform_mode_t]]
typedef enum sizeform_mode_t
{
DC_SFORM_ALIGN = 0x01, // aligned output
DC_SFORM_NARROW = 0x02, // suppress space between number and unit
DC_SFORM_UNIT1 = 0x04, // limit unit to 1 character (space for bytes)
DC_SFORM_INC = 0x08, // increment to next factor if no fraction is lost
DC_SFORM_PLUS = 0x10, // on signed output: print PLUS sign for values >0
DC_SFORM_DASH = 0x20, // on NULL: print only a dash (minus sign)
DC_SFORM__MASK = 0x3f, // mask of above
DC_SFORM__AUTO = 0x40, // hint for some functions to decide by themself
DC_SFORM_TINY = DC_SFORM_NARROW | DC_SFORM_UNIT1,
}
sizeform_mode_t;
///////////////////////////////////////////////////////////////////////////////
float RoundF2bytes ( float f );
float RoundF3bytes ( float f );
double RoundD6bytes ( double d );
double RoundD7bytes ( double d );
///////////////////////////////////////////////////////////////////////////////
char * PrintNumberU4 ( char *buf, size_t buf_size, u64 num, sizeform_mode_t mode );
char * PrintNumberU5 ( char *buf, size_t buf_size, u64 num, sizeform_mode_t mode );
char * PrintNumberU6 ( char *buf, size_t buf_size, u64 num, sizeform_mode_t mode );
char * PrintNumberU7 ( char *buf, size_t buf_size, u64 num, sizeform_mode_t mode );
char * PrintNumberS5 ( char *buf, size_t buf_size, s64 num, sizeform_mode_t mode );
char * PrintNumberS6 ( char *buf, size_t buf_size, s64 num, sizeform_mode_t mode );
char * PrintNumberS7 ( char *buf, size_t buf_size, s64 num, sizeform_mode_t mode );
char * PrintNumberD5 ( char *buf, size_t buf_size, double num, sizeform_mode_t mode );
char * PrintNumberD6 ( char *buf, size_t buf_size, double num, sizeform_mode_t mode );
char * PrintNumberD7 ( char *buf, size_t buf_size, double num, sizeform_mode_t mode );
// Parameters:
// char *buf, // result buffer
// // NULL: use a local circulary static buffer
// size_t buf_size, // size of 'buf', ignored if buf==NULL
// * num, // number to print
// sizeform_mode_t mode // any of DC_SFORM_ALIGN, DC_SFORM_DASH[not *D*()]
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// [[size_mode_t]]
typedef enum size_mode_t
{
//----- modes => used as index for dc_size_tab_1000[] & dc_size_tab_1024[]
DC_SIZE_DEFAULT = 0, // special default value, fall back to AUTO
DC_SIZE_AUTO, // select unit automatically
DC_SIZE_BYTES, // force output in bytes
DC_SIZE_K, // force output in KB or KiB (kilo,kibi)
DC_SIZE_M, // force output in MB or MiB (mega,mebi)
DC_SIZE_G, // force output in GB or GiB (giga,gibi)
DC_SIZE_T, // force output in TB or TiB (tera,tebi)
DC_SIZE_P, // force output in PB or PiB (peta,pebi)
DC_SIZE_E, // force output in EB or EiB (exa, exbi)
// zetta/zebi & yotta/yobi not supported because >2^64
DC_SIZE_N_MODES, // number of modes
//----- flags
DC_SIZE_F_1000 = 0x010, // force output in SI units (kB=1000, MB=1000000,...)
DC_SIZE_F_1024 = 0x020, // force output in IEC units (KiB=1024, MiB=1024*1024,...)
DC_SIZE_F_AUTO_UNIT = 0x040, // suppress output of unit for non AUTO mode
DC_SIZE_F_NO_UNIT = 0x080, // suppress allways output of unit
//----- masks
DC_SIZE_M_MODE = 0x00f, // mask for modes
DC_SIZE_M_BASE = 0x030, // mask for base
DC_SIZE_M_ALL = 0x0ff, // all relevant bits
}
size_mode_t;
//-----------------------------------------------------------------------------
extern ccp dc_size_tab_1000[DC_SIZE_N_MODES+1];
extern ccp dc_size_tab_1024[DC_SIZE_N_MODES+1];
//-----------------------------------------------------------------------------
ccp GetSizeUnit // get a unit for column headers
(
size_mode_t mode, // print mode
ccp if_invalid // output for invalid modes
);
//-----------------------------------------------------------------------------
int GetSizeFW // get a good value field width
(
size_mode_t mode, // print mode
int min_fw // minimum fw => return max(calc_fw,min_fw);
// this value is also returned for invalid modes
);
//-----------------------------------------------------------------------------
char * PrintSize
(
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
u64 size, // size to print
sizeform_mode_t sform_mode, // output format, bit field
size_mode_t mode // print mode
);
//-----------------------------------------------------------------------------
char * PrintSize1000
(
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
u64 size, // size to print
sizeform_mode_t sform_mode // output format, bit field
);
//-----------------------------------------------------------------------------
char * PrintSize1024
(
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
u64 size, // size to print
sizeform_mode_t sform_mode // output format, bit field
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// scan size ///////////////
///////////////////////////////////////////////////////////////////////////////
extern u64 (*ScanSizeFactorHook)
(
char ch_factor, // char to analyze
int force_base // if 1000|1024: force multiple of this
);
u64 ScanSizeFactor
(
char ch_factor, // char to analyze
int force_base // if 1000|1024: force multiple of this
);
char * ScanSizeTerm
(
double *num, // not NULL: store result
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
int force_base // if 1000|1024: force multiple of this
);
char * ScanSize
(
double *num, // not NULL: store result
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
u64 default_factor_add, // use this factor for summands
int force_base // if 1000|1024: force multiple of this
);
char * ScanSizeU32
(
u32 *num, // not NULL: store result
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
u64 default_factor_add, // use this factor for summands
int force_base // if 1000|1024: force multiple of this
);
char * ScanSizeU64
(
u64 *num, // not NULL: store result
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
u64 default_factor_add, // use this factor for summands
int force_base // if 1000|1024: force multiple of this
);
//-----------------------------------------------------------------------------
// strto*() like wrappers
u32 GetSizeU32 ( ccp src, char **end, int force_base );
u64 GetSizeU64 ( ccp src, char **end, int force_base );
double GetSizeD ( ccp src, char **end, int force_base );
//-----------------------------------------------------------------------------
char * ScanSizeRange
(
int *stat, // if not NULL: store result
// 0:none, 1:single, 2:range
double *num1, // not NULL: store 'from' result
double *num2, // not NULL: store 'to' result
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
u64 default_factor_add, // use this factor for summands
int force_base, // if 1000|1024: force multiple of this
double max_value // >0: max value for open ranges
);
char * ScanSizeRangeU32
(
int *stat, // if not NULL: store result
// 0:none, 1:single, 2:range
u32 *num1, // not NULL: store 'from' result
u32 *num2, // not NULL: store 'to' result
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
u64 default_factor_add, // use this factor for summands
int force_base, // if 1000|1024: force multiple of this
u32 max_value // >0: max value for open ranges
);
char * ScanSizeRangeU64
(
int *stat, // if not NULL: store result
// 0:none, 1:single, 2:range
u64 *num1, // not NULL: store 'from' result
u64 *num2, // not NULL: store 'to' result
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
u64 default_factor_add, // use this factor for summands
int force_base, // if 1000|1024: force multiple of this
u64 max_value // >0: max value for open ranges
);
//-----------------------------------------------------------------------------
char * ScanSizeRangeList
(
uint *n_range, // not NULL: store number of scanned ranges
double *num, // array with '2*max_range' elements
// unused elements are filled with 0.0
uint max_range, // max number of allowed ranges
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
u64 default_factor_add, // use this factor for summands
int force_base, // if 1000|1024: force multiple of this
double max_value // >0: max value for open ranges
);
char * ScanSizeRangeListU32
(
uint *n_range, // not NULL: store number of scanned ranges
u32 *num, // array with '2*max_range' elements
// unused elements are filled with 0
uint max_range, // max number of allowed ranges
ccp source, // source text
u32 default_factor, // use this factor if number hasn't one
u32 default_factor_add, // use this factor for summands
int force_base, // if 1000|1024: force multiple of this
u32 max_value // >0: max value for open ranges
);
char * ScanSizeRangeListU64
(
uint *n_range, // not NULL: store number of scanned ranges
u64 *num, // array with '2*max_range' elements
// unused elements are filled with 0
uint max_range, // max number of allowed ranges
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
u64 default_factor_add, // use this factor for summands
int force_base, // if 1000|1024: force multiple of this
u64 max_value // >0: max value for open ranges
);
//-----------------------------------------------------------------------------
enumError ScanSizeOpt
(
double *num, // not NULL: store result
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
u64 default_factor_add, // use this factor for summands
int force_base, // if 1000|1024: force multiple of this
ccp opt_name, // NULL or name of option for error messages
u64 min, // >0: minimum allowed value
u64 max, // >0: maximum allowed value
bool print_err // true: print error messages
);
enumError ScanSizeOptU64
(
u64 *num, // not NULL: store result
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
int force_base, // if 1000|1024: force multiple of this
ccp opt_name, // NULL or name of option for error messages
u64 min, // >0: minimum allowed value
u64 max, // >0: maximum allowed value
u32 multiple, // >0: result must be multiple
u32 pow2, // >0: result must power of '1<<pow2'
bool print_err // true: print error messages
);
enumError ScanSizeOptU32
(
u32 *num, // not NULL: store result
ccp source, // source text
u64 default_factor, // use this factor if number hasn't one
int force_base, // if 1000|1024: force multiple of this
ccp opt_name, // NULL or name of option for error messages
u64 min, // >0: minimum allowed value
u64 max, // >0: maximum allowed value
u32 multiple, // >0: result must be multiple
u32 pow2, // >0: result must power of '1<<pow2'
bool print_err // true: print error messages
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// Scan Duration ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[ScanDuration_t]]
typedef enum ScanDuration_t
{
SDUMD_SKIP_BLANKS = 0x01, // skip leading blanks
SDUMD_ALLOW_COLON = 0x02, // allow 'H:M' and 'H:M:S'
SDUMD_ALLOW_DIV = 0x04, // allow fraction 'n/d' as number
SDUMD_ALLOW_LIST = 0x10, // allow lists like '9h12m'
SDUMD_ALLOW_SPACE_SEP = 0x20, // lists: allow spaces as list separator
SDUMD_ALLOW_PLUS_SEP = 0x40, // lists: allow '+' as list separator
SDUMD_ALLOW_MINUS_SEP = 0x80, // lists: allow '-' as list separator
SDUMD_M_ALL = 0xf7,
}
ScanDuration_t;
//-----------------------------------------------------------------------------
char * ScanDuration
(
double *num, // not NULL: store result
ccp source, // source text
double default_factor, // default factor if no SI unit found
ScanDuration_t mode
);
double GetDurationFactor ( char ch );
char * ScanSIFactor
(
// returns end of scanned string
double *num, // not NULL: store result
ccp source, // source text
double default_factor // return this if no factor found
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// time & timer ///////////////
///////////////////////////////////////////////////////////////////////////////
//--- conversion factors
#define SEC_PER_MIN 60
#define SEC_PER_HOUR 3600
#define SEC_PER_DAY 86400
#define SEC_PER_WEEK 604800
#define MSEC_PER_SEC 1000ull
#define MSEC_PER_MIN (60*MSEC_PER_SEC)
#define MSEC_PER_HOUR (3600*MSEC_PER_SEC)
#define MSEC_PER_DAY (SEC_PER_DAY*MSEC_PER_SEC)
#define MSEC_PER_WEEK (SEC_PER_WEEK*MSEC_PER_SEC)
#define USEC_PER_MSEC 1000ull
#define USEC_PER_SEC 1000000ull
#define USEC_PER_MIN (60*USEC_PER_SEC)
#define USEC_PER_HOUR (3600*USEC_PER_SEC)
#define USEC_PER_DAY (SEC_PER_DAY*USEC_PER_SEC)
#define USEC_PER_WEEK (SEC_PER_WEEK*USEC_PER_SEC)
#define NSEC_PER_USEC 1000ull
#define NSEC_PER_MSEC 1000000ull
#define NSEC_PER_SEC 1000000000ull
#define NSEC_PER_MIN (60*NSEC_PER_SEC)
#define NSEC_PER_HOUR (3600*NSEC_PER_SEC)
#define NSEC_PER_DAY (SEC_PER_DAY*NSEC_PER_SEC)
#define NSEC_PER_WEEK (SEC_PER_WEEK*NSEC_PER_SEC)
//--- time types
typedef uint u_sec_t; // unsigned type to store time as seconds
typedef int s_sec_t; // signed type to store time as seconds
typedef u64 u_msec_t; // unsigned type to store time as milliseconds
typedef s64 s_msec_t; // signed type to store time as milliseconds
typedef u64 u_usec_t; // unsigned type to store time as microseconds
typedef s64 s_usec_t; // signed type to store time as microseconds
typedef u64 u_nsec_t; // unsigned type to store time as nanoseconds
typedef s64 s_nsec_t; // signed type to store time as nanoseconds
// [[DayTime_t]]
typedef struct DayTime_t
{
time_t time; // seconds since epoch, like time()
int day; // days since epoch
int hour; // hour of day
int min; // minute of hour
int sec; // second of minute
int usec; // microsecond of second
int nsec; // nanosecond of second
}
DayTime_t;
//--- only valid after call to SetupTimezone()
extern s64 timezone_adjust_sec;
extern s64 timezone_adjust_usec;
extern s64 timezone_adjust_nsec;
extern int timezone_adjust_isdst;
///////////////////////////////////////////////////////////////////////////////
void SetupTimezone ( bool force );
int GetTimezoneAdjust ( time_t tim );
struct timeval GetTimeOfDay ( bool localtime );
struct timespec GetClockTime ( bool localtime );
DayTime_t GetDayTime ( bool localtime );
u_sec_t GetTimeSec ( bool localtime );
u_msec_t GetTimeMSec ( bool localtime );
u_usec_t GetTimeUSec ( bool localtime );
u_nsec_t GetTimeNSec ( bool localtime );
u_msec_t GetTimerMSec(void);
u_usec_t GetTimerUSec(void);
u_nsec_t GetTimerNSec(void);
static inline u64 double2msec ( double d ) { return d>0.0 ? (u64)trunc( 1e3*d+0.5 ) : 0; }
static inline u64 double2usec ( double d ) { return d>0.0 ? (u64)trunc( 1e6*d+0.5 ) : 0; }
static inline u64 double2nsec ( double d ) { return d>0.0 ? (u64)trunc( 1e9*d+0.5 ) : 0; }
static inline double msec2double ( u64 num ) { return num * 1e-3; }
static inline double usec2double ( u64 num ) { return num * 1e-6; }
static inline double nsec2double ( u64 num ) { return num * 1e-9; }
//--- another epoch: Monday, 2001-01-01
#define EPOCH_2001_SEC 0x3a4fc880
#define EPOCH_2001_MSEC (EPOCH_2001_SEC*MSEC_PER_SEC)
#define EPOCH_2001_USEC (EPOCH_2001_SEC*USEC_PER_SEC)
#define EPOCH_2001_NSEC (EPOCH_2001_SEC*NSEC_PER_SEC)
///////////////////////////////////////////////////////////////////////////////
ccp PrintTimeByFormat
(
// returns temporary buffer by GetCircBuf();
ccp format, // format string for strftime()
time_t tim // seconds since epoch; 0 is replaced by time()
);
ccp PrintTimeByFormatUTC
(
// returns temporary buffer by GetCircBuf();
ccp format, // format string for strftime()
time_t tim // seconds since epoch; 0 is replaced by time()
);
//-----------------------------------------------------------------------------
ccp PrintNSecByFormat
(
// returns temporary buffer by GetCircBuf();
ccp format, // format string for strftime()
// 1-9 '@' in row replaced by digits of 'nsec'
time_t tim, // seconds since epoch
uint nsec // nanosecond of second
);
ccp PrintNSecByFormatUTC
(
// returns temporary buffer by GetCircBuf();
ccp format, // format string for strftime()
// 1-9 '@' in row replaced by digits of 'nsec'
time_t tim, // seconds since epoch
uint nsec // nanosecond of second
);
//-----------------------------------------------------------------------------
static inline ccp PrintUSecByFormat
(
// returns temporary buffer by GetCircBuf();
ccp format, // format string for strftime()
// 1-9 '@' in row replaced by digits of 'usec'
time_t tim, // seconds since epoch
uint usec // microsecond of second
)
{
return PrintNSecByFormat(format,tim,usec*NSEC_PER_USEC);
}
static inline ccp PrintUSecByFormatUTC
(
// returns temporary buffer by GetCircBuf();
ccp format, // format string for strftime()
// 1-9 '@' in row replaced by digits of 'usec'
time_t tim, // seconds since epoch
uint usec // microsecond of second
)
{
return PrintNSecByFormatUTC(format,tim,usec*NSEC_PER_USEC);
}
//-----------------------------------------------------------------------------
ccp PrintTimevalByFormat
(
// returns temporary buffer by GetCircBuf();
ccp format, // format string for strftime()
const struct timeval *tv // time to print, if NULL use gettimeofday()
);
ccp PrintTimevalByFormatUTC
(
// returns temporary buffer by GetCircBuf();
ccp format, // format string for strftime()
const struct timeval *tv // time to print, if NULL use gettimeofday()
);
//-----------------------------------------------------------------------------
ccp PrintTimespecByFormat
(
// returns temporary buffer by GetCircBuf();
ccp format, // format string for strftime()
const struct timespec *ts // time to print, if NULL use GetClockTime(false)
);
ccp PrintTimespecByFormatUTC
(
// returns temporary buffer by GetCircBuf();
ccp format, // format string for strftime()
const struct timespec *ts // time to print, if NULL use GetClockTime(false)
);
///////////////////////////////////////////////////////////////////////////////
ccp PrintTimeSec
(
char * buf, // result buffer (>19 bytes are good)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_sec_t sec // seconds (=time) to print
);
ccp PrintTimeMSec
(
char * buf, // result buffer (>23 bytes are good)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_msec_t msec, // milliseconds to print
uint fraction // number of digits (0-3) to print as fraction
);
ccp PrintTimeUSec
(
char * buf, // result buffer (>26 bytes are good)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_usec_t usec, // microseconds to print
uint fraction // number of digits (0-6) to print as fraction
);
ccp PrintTimeNSec
(
char * buf, // result buffer (>26 bytes are good)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_nsec_t nsec, // nanoseconds to print
uint fraction // number of digits (0-9) to print as fraction
);
//-----------------------------------------------------------------------------
ccp PrintTimerMSec
(
char * buf, // result buffer (>12 bytes are good)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u32 msec, // milliseconds to print
uint fraction // number of digits (0-3) to print as fraction
);
ccp PrintTimerUSec
(
char * buf, // result buffer (>16 bytes are good)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
s_usec_t usec, // microseconds to print
uint fraction // number of digits (0-6) to print as fraction
);
ccp PrintTimerNSec
(
char * buf, // result buffer (>16 bytes are good)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
s_nsec_t nsec, // nanoseconds to print
uint fraction // number of digits (0-9) to print as fraction
);
//-----------------------------------------------------------------------------
ccp PrintTimer3 // helper function
(
char * buf, // result buffer (>3 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u64 sec, // seconds to print
int usec, // 0...999999: usec fraction,
// otherwise suppress ms/us output
bool aligned // true: print aligned 3 character output
);
static inline ccp PrintTimerSec3
(
char * buf, // result buffer (>3 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_sec_t sec, // seconds to print
bool aligned // true: print aligned 3 character output
)
{
return PrintTimer3(buf,buf_size,sec,-1,aligned);
}
static inline ccp PrintTimerMSec3
(
char * buf, // result buffer (>3 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_msec_t msec, // microseconds to print
bool aligned // true: print aligned 3 character output
)
{
return PrintTimer3(buf,buf_size,msec/1000,msec%1000*1000,aligned);
}
static inline ccp PrintTimerUSec3
(
char * buf, // result buffer (>3 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_usec_t usec, // microseconds to print
bool aligned // true: print aligned 3 character output
)
{
return PrintTimer3(buf,buf_size,usec/1000000,usec%1000000,aligned);
}
//-----------------------------------------------------------------------------
ccp PrintTimer4 // helper function
(
char * buf, // result buffer (>4 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u64 sec, // seconds to print
int usec, // 0...999999: usec fraction,
// otherwise suppress ms/us output
bool aligned // true: print aligned 4 character output
);
static inline ccp PrintTimerSec4
(
char * buf, // result buffer (>4 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_sec_t sec, // seconds to print
bool aligned // true: print aligned 4 character output
)
{
return PrintTimer4(buf,buf_size,sec,-1,aligned);
}
static inline ccp PrintTimerMSec4
(
char * buf, // result buffer (>4 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_msec_t msec, // microseconds to print
bool aligned // true: print aligned 4 character output
)
{
return PrintTimer4(buf,buf_size,msec/1000,msec%1000*1000,aligned);
}
static inline ccp PrintTimerUSec4
(
char * buf, // result buffer (>4 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_usec_t usec, // microseconds to print
bool aligned // true: print aligned 4 character output
)
{
return PrintTimer4(buf,buf_size,usec/1000000,usec%1000000,aligned);
}
//-----------------------------------------------------------------------------
ccp PrintTimer6 // helper function
(
char * buf, // result buffer (>6 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u64 sec, // seconds to print
int usec, // 0...999999: usec fraction,
// otherwise suppress ms/us output
bool aligned // true: print aligned 6 character output
);
static inline ccp PrintTimerSec6
(
char * buf, // result buffer (>6 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_sec_t sec, // seconds to print
bool aligned // true: print aligned 6 character output
)
{
return PrintTimer6(buf,buf_size,sec,-1,aligned);
}
static inline ccp PrintTimerMSec6
(
char * buf, // result buffer (>6 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_msec_t msec, // microseconds to print
bool aligned // true: print aligned 6 character output
)
{
return PrintTimer6(buf,buf_size,msec/1000,msec%1000*1000,aligned);
}
static inline ccp PrintTimerUSec6
(
char * buf, // result buffer (>6 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u_usec_t usec, // microseconds to print
bool aligned // true: print aligned 6 character output
)
{
return PrintTimer6(buf,buf_size,usec/1000000,usec%1000000,aligned);
}
//-----------------------------------------------------------------------------
ccp PrintTimerUSec4s
(
char * buf, // result buffer (>4 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
s_usec_t usec, // microseconds to print
sizeform_mode_t mode // support of DC_SFORM_ALIGN, DC_SFORM_PLUS
);
static inline ccp PrintTimerMSec4s
(
char * buf, // result buffer (>4 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
s_msec_t msec, // milliseconds to print
sizeform_mode_t mode // support of DC_SFORM_ALIGN, DC_SFORM_PLUS
)
{
return PrintTimerUSec4s(buf,buf_size,1000*msec,mode);
}
static inline ccp PrintTimerSec4s
(
char * buf, // result buffer (>4 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
int sec, // seconds to print
sizeform_mode_t mode // support of DC_SFORM_ALIGN, DC_SFORM_PLUS
)
{
return PrintTimerUSec4s(buf,buf_size,1000000ll*sec,mode);
}
//-----------------------------------------------------------------------------
ccp PrintTimerUSec7s
(
char * buf, // result buffer (>7 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
s_usec_t usec, // microseconds to print
sizeform_mode_t mode // support of DC_SFORM_ALIGN, DC_SFORM_PLUS
);
static inline ccp PrintTimerMSec7s
(
char * buf, // result buffer (>7 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
s_msec_t msec, // milliseconds to print
sizeform_mode_t mode // support of DC_SFORM_ALIGN, DC_SFORM_PLUS
)
{
return PrintTimerUSec7s(buf,buf_size,1000*msec,mode);
}
static inline ccp PrintTimerSec7s
(
char * buf, // result buffer (>7 bytes)
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
int sec, // seconds to print
sizeform_mode_t mode // support of DC_SFORM_ALIGN, DC_SFORM_PLUS
)
{
return PrintTimerUSec7s(buf,buf_size,1000000ll*sec,mode);
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// scan date & time ///////////////
///////////////////////////////////////////////////////////////////////////////
// ScanInterval*() is old ==> try ScanDuration()
//-----------------------------------------------------------------------------
// new interface : all functions return first not scanned char
char * ScanIntervalTS
(
struct timespec *res_ts, // not NULL: store result here
ccp source // source text
);
char * ScanIntervalUSec
(
u_usec_t *res_usec, // not NULL: store total microseconds
ccp source // source text
);
char * ScanIntervalNSec
(
u_nsec_t *res_nsec, // not NULL: store total nanoseconds
ccp source // source text
);
//-----------------------------------------------------------------------------
// legacy interface : all functions return first not scanned char
char * ScanInterval
(
time_t *res_time, // not NULL: store seconds
u32 *res_usec, // not NULL: store microseconds of second
ccp source // source text
);
char * ScanInterval64
(
u64 *res_usec, // not NULL: store total microseconds
ccp source // source text
);
///////////////////////////////////////////////////////////////////////////////
char * ScanDateTime
(
time_t *res_time, // not NULL: store seconds
u32 *res_usec, // not NULL: store microseconds of second
ccp source, // source text
bool allow_delta // true: allow +|- interval
);
///////////////////////////////////////////////////////////////////////////////
char * ScanDateTime64
(
u64 *res_usec, // not NULL: store total microseconds
ccp source, // source text
bool allow_delta // true: allow +|- interval
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// Since 2001 ///////////////
///////////////////////////////////////////////////////////////////////////////
// Values for day and week are exact.
// Values for month, quarter and year are based on 365.2425 days/year.
//-----------------------------------------------------------------------
// name full/13 short/11 alt/10 tiny/8
//-----------------------------------------------------------------------
// hour YYYY-MM-DD HH YY-MM-DD HH DD. HH:xx MM-DD HH
// day YYYY-MM-DD << << YY-MM-DD
// week YYYYwWW << << <<
// month YYYY-MM << << <<
// quarter YYYYqQ << << <<
// year YYYY << << <<
//-----------------------------------------------------------------------
enum
{
S2001_FW_FULL = 13, ///< Standard-Text mit vollem Jahr (maximal 13 Zeichen).
S2001_FW_SHORT = 11, ///< Verkürzte Stundenangabe (maximal 11 Zeichen).
S2001_FW_ALT = 10, ///< Alternative Stundenangabe mit »:xx« (maximal 10 Zeichen).
S2001_FW_TINY = 8, ///< Verkürzte Stunden- und Wochenangabe (maximal 8 Zeichen).
S2001_DUR_HOUR = 3600, ///< Dauer einer Stunde in Sekunden.
S2001_DUR_DAY = 86400, ///< Dauer eines Tages in Sekunden.
S2001_DUR_WEEK = 604800, ///< Dauer einer Woche in Sekunden.
S2001_DUR_MONTH = 2629746, ///< Dauer eines Monats in Sekunden (365.2425/12 Tage).
S2001_DUR_QUARTER = 7889238, ///< Dauer eines Quartals in Sekunden (365.2425/4 Tage).
S2001_DUR_YEAR = 31556952, ///< Dauer eines Jahres in Sekunden (365.2425 Tage).
};
///////////////////////////////////////////////////////////////////////////
// hour support
static inline int time2hour ( s64 time )
{ return time / 3600 - 271752; }
static inline double time2hourF ( s64 time )
{ return time / 3600.0 - 271752; }
static inline s64 hour2time ( int hour )
{ return ( hour + 271752 ) * 3600; }
static inline int hour2duration ( int hour )
{ return S2001_DUR_HOUR; }
ccp hour2text
(
int hour, // hour to print
int fw, // wanted field width assuming separators are 1 char
ccp sep_date, // NULL or separator within date
// if NULL: use '-'
ccp sep_dt // NULL or separator of between date and time
// if NULL: use ' '
);
///////////////////////////////////////////////////////////////////////////
// day support
static inline int time2day ( s64 time )
{ return time / 86400 - 11323; }
static inline double time2dayF ( s64 time )
{ return time / 86400.0 - 11323; }
static inline s64 day2time ( int day )
{ return ( day + 11323 ) * 86400; }
static inline int day2duration ( int day )
{ return S2001_DUR_DAY; }
ccp day2text
(
int day, // day to print
int fw, // wanted field width assuming separators are 1 char
ccp sep_date, // NULL or separator within date
// if NULL: use '-'
ccp sep_dt // ignored
);
///////////////////////////////////////////////////////////////////////////
// week support
static inline int time2week ( s64 time )
{ return ( time - 978307200 ) / 604800; }
static inline double time2weekF ( s64 time )
{ return ( time - 978307200 ) / 604800.0; }
static inline s64 week2time ( int week )
{ return week * 604800 + 978307200; }
static inline int week2duration ( int week )
{ return S2001_DUR_WEEK; }
ccp week2text
(
int week, // week to print
int fw, // wanted field width assuming separators are 1 char
ccp sep_date, // NULL or separator within date
// if NULL: use 'w'
ccp sep_dt // ignored
);
///////////////////////////////////////////////////////////////////////////
// month support
int time2month ( time_t tim );
time_t month2time ( int month );
static inline int month2duration ( int month )
{ return month2time(month+1) - month2time(month); }
ccp month2text
(
int month, // month to print
int fw, // wanted field width assuming separators are 1 char
ccp sep_date, // NULL or separator within date
// if NULL: use '-'
ccp sep_dt // ignored
);
///////////////////////////////////////////////////////////////////////////
// quarter support
int time2quarter ( time_t tim );
time_t quarter2time ( int quarter );
static inline int quarter2duration ( int quarter )
{ return quarter2time(quarter+1) - quarter2time(quarter); }
ccp quarter2text
(
int quarter, // quarter to print
int fw, // wanted field width assuming separators are 1 char
ccp sep_date, // NULL or separator within date
// if NULL: use 'q'
ccp sep_dt // ignored
);
///////////////////////////////////////////////////////////////////////////
// year support
int time2year ( time_t tim );
time_t year2time ( int year );
static inline int year2duration ( int year )
{ return year2time(year+1) - year2time(year); }
static inline ccp year2text
(
int year, // year to print
int fw, // wanted field width assuming separators are 1 char
ccp sep_date, // ignored
ccp sep_dt // ignored
)
{
return PrintCircBuf("%04d",year);
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// sockaddr support ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[sockaddr_t]] [[sockaddr_in_t]] [[sockaddr_un_t]] [[sockaddr_info_t]]
typedef struct sockaddr sockaddr_t;
typedef struct sockaddr_in sockaddr_in_t;
typedef struct sockaddr_un sockaddr_un_t;
typedef struct sockaddr_info_t
{
sockaddr_t *sa; // pointer to sockaddr or sockaddr_*
uint size; // size of '*sa'
}
sockaddr_info_t;
//-----------------------------------------------------------------------------
// [[doxygen]]
static inline sockaddr_info_t CreateSockaddrInfo ( cvp sa, uint size )
{
sockaddr_info_t i =
{
.sa = (sockaddr_t*)sa,
.size = size
};
return i;
}
//-----------------------------------------------------------------------------
// [[doxygen]]
static inline void SetupSockaddrInfo ( sockaddr_info_t *sai, cvp sa, uint size )
{
DASSERT(sai);
sai->sa = (sockaddr_t*)sa;
sai->size = size ? size : sizeof(sockaddr_in_t);
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// PrintIP4*() ///////////////
///////////////////////////////////////////////////////////////////////////////
#define FW_IPV4_A 12 // 255.16777215
#define FW_IPV4_B 13 // 255.255.65535
#define FW_IPV4_C 15 // 255.255.255.255
#define FW_IPV4_PORT 5 // 65535
#define FW_IPV4_A_PORT (FW_IPV4_A+1+FW_IPV4_PORT)
#define FW_IPV4_B_PORT (FW_IPV4_B+1+FW_IPV4_PORT)
#define FW_IPV4_C_PORT (FW_IPV4_C+1+FW_IPV4_PORT)
//-----------------------------------------------------------------------------
char * PrintIP4A
(
// print in A-notation
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
u32 ip4, // IP4 to print
s32 port // 0..0xffff: add ':port'
);
char * PrintIP4B
(
// print in A-notation
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
u32 ip4, // IP4 to print
s32 port // 0..0xffff: add ':port'
);
//-----------------------------------------------------------------------------
// print in C-notation (usual notation)
char * PrintIP4
(
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
u32 ip4, // IP4 to print
s32 port // 0..0xffff: add ':port'
);
char * PrintLeftIP4
(
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
u32 ip4, // IP4 to print
s32 port // 0..0xffff: add ':port'
);
char * PrintRightIP4
(
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
u32 ip4, // IP4 to print
s32 port, // 0..0xffff: add ':port'
uint port_mode // 0: 'ip:port', 1: 'ip :port', 2: 'ip : port'
);
char * PrintAlignedIP4
(
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
u32 ip4, // IP4 to print
s32 port // 0..0xffff: add ':port'
);
//-----------------------------------------------------------------------------
struct sockaddr_in;
static inline char * PrintIP4sa
(
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
const struct sockaddr_in *sa // socket address
)
{
DASSERT(sa);
return PrintIP4(buf,buf_size,
ntohl(sa->sin_addr.s_addr), ntohs(sa->sin_port) );
}
static inline char * PrintRightIP4sa
(
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
struct sockaddr_in *sa // socket address
)
{
DASSERT(sa);
return PrintRightIP4(buf,buf_size,
ntohl(sa->sin_addr.s_addr), ntohs(sa->sin_port), 0 );
}
static inline char * PrintAlignedIP4sa
(
char *buf, // result buffer
// NULL: use a local circulary static buffer
size_t buf_size, // size of 'buf', ignored if buf==NULL
struct sockaddr_in *sa // socket address
)
{
DASSERT(sa);
return PrintAlignedIP4(buf,buf_size,
ntohl(sa->sin_addr.s_addr), ntohs(sa->sin_port) );
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// dynamic data support ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[DynData_t]]
typedef struct DynData_t
{
u8 *data; // pointer to data, alloced
uint len; // length of used data
uint size; // size of allocated data
}
DynData_t;
//-----------------------------------------------------------------------------
void ResetDD ( DynData_t *dd );
void InsertDD ( DynData_t *dd, const void *data, uint data_size );
///////////////////////////////////////////////////////////////////////////////
// [[DynDataList_t]]
typedef struct DynDataList_t
{
DynData_t *list; // pointer to the data list
uint used; // number of used data elements
uint size; // number of allocated pointers in 'list'
}
DynDataList_t;
//-----------------------------------------------------------------------------
void ResetDDL ( DynDataList_t *ddl );
DynData_t *InsertDDL
(
DynDataList_t *ddl, // valid dynamic data list
uint index, // element index to change
const void *data, // NULL or data to assign
uint data_size // size of data
);
DynData_t *GetDDL
(
DynDataList_t *ddl, // valid dynamic data list
uint index, // element index to get
uint create // >0: create an empty element if not exist
// >index: create at least # elements
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// struct StringField_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[StringField_t]]
typedef struct StringField_t
{
ccp * field; // pointer to the string field
uint used; // number of used titles in the title field
uint size; // number of allocated pointers in 'field'
int (*func_cmp)( ccp s1, ccp s2 ); // compare function, default is strcmp()
} StringField_t;
//-----------------------------------------------------------------------------
static inline void InitializeStringField ( StringField_t * sf )
{ DASSERT(sf); memset(sf,0,sizeof(*sf)); }
void ResetStringField ( StringField_t * sf );
void MoveStringField ( StringField_t * dest, StringField_t * src );
int FindStringFieldIndex ( const StringField_t * sf, ccp key, int not_found_value );
ccp FindStringField ( const StringField_t * sf, ccp key );
// return: true if item inserted/deleted
bool InsertStringField ( StringField_t * sf, ccp key, bool move_key );
bool RemoveStringField ( StringField_t * sf, ccp key );
uint RemoveStringFieldByIndex ( StringField_t * sf, int index, int n );
// helper function
ccp * InsertStringFieldHelper ( StringField_t * sf, int idx );
// append at the end an do not sort
void AppendStringField ( StringField_t * sf, ccp key, bool move_key );
// re-sort field using sf->func_cmp()
void SortStringField ( StringField_t * sf );
// return the index of the (next) item
uint FindStringFieldHelper ( const StringField_t * sf, bool * found, ccp key );
// find first pattern that matches
ccp MatchStringField ( const StringField_t * sf, ccp key );
//-----------------------------------------------------------------------------
// file support
enumError LoadStringField
(
StringField_t * sf, // string field
bool init_sf, // true: initialize 'sf' first
bool sort, // true: sort 'sf'
ccp filename, // filename of source file
bool silent // true: don't print open/read errors
);
enumError SaveStringField
(
StringField_t * sf, // valid string field
ccp filename, // filename of dest file
bool rm_if_empty // true: rm dest file if 'sf' is empty
);
enumError WriteStringField
(
FILE * f, // open destination file
ccp filename, // NULL or filename (needed on write error)
StringField_t * sf, // valid string field
ccp prefix, // not NULL: insert prefix before each line
ccp eol // end of line text (if NULL: use LF)
);
//-----------------------------------------------------------------------------
// save-state support
void SaveCurrentStateStringField
(
FILE *f, // output file
ccp name_prefix, // NULL or prefix of name, up to 40 chars
uint tab_pos, // tab pos of '='
const StringField_t *sf, // valid string field
EncodeMode_t emode // encoding mode
);
struct RestoreState_t;
void RestoreStateStringField
(
struct RestoreState_t *rs, // info data, may be modified
ccp name_prefix, // NULL or prefix of name, up to 40 chars
StringField_t *sf, // string field
bool init_sf, // true: initialize 'sf', otherwise reset
EncodeMode_t emode // encoding mode
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// struct ParamField_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[ParamFieldItem_t]]
typedef struct ParamFieldItem_t
{
ccp key; // string as main key
uint num; // user defined numeric parameter
void *data; // user defined pointer
} ParamFieldItem_t;
//-----------------------------------------------------------------------------
// [[ParamField_t]]
typedef struct ParamField_t
{
ParamFieldItem_t * field; // pointer to the string field
uint used; // number of used titles in the title field
uint size; // number of allocated pointers in 'field'
bool free_data; // true: unused data will be free'd
// initialized with 'false'
int (*func_cmp)( ccp s1, ccp s2 ); // compare function, default is strcmp()
} ParamField_t;
//-----------------------------------------------------------------------------
void InitializeParamField ( ParamField_t * pf );
void ResetParamField ( ParamField_t * pf );
void MoveParamField ( ParamField_t * dest, ParamField_t * src );
int FindParamFieldIndex ( const ParamField_t * pf, ccp key, int not_found_value );
ParamFieldItem_t * FindParamField ( const ParamField_t * pf, ccp key );
ParamFieldItem_t * FindParamFieldByNum ( const ParamField_t * pf, uint num );
ParamFieldItem_t * FindInsertParamField
( ParamField_t * pf, ccp key, bool move_key, bool *old_found );
ParamFieldItem_t * CountParamField ( ParamField_t * pf, ccp key, bool move_key );
// return: true if item inserted/deleted
bool InsertParamField ( ParamField_t * pf, ccp key, bool move_key, uint num, cvp data );
bool ReplaceParamField( ParamField_t * pf, ccp key, bool move_key, uint num, cvp data );
bool RemoveParamField ( ParamField_t * pf, ccp key );
ParamFieldItem_t * InsertParamFieldEx
( ParamField_t * pf, ccp key, bool move_key, uint num, bool *found );
// append at the end an do not sort
ParamFieldItem_t * AppendParamField
( ParamField_t * pf, ccp key, bool move_key, uint num, cvp data );
// return the index of the (next) item
uint FindParamFieldHelper ( const ParamField_t * pf, bool * found, ccp key );
// find first pattern that matches
ParamFieldItem_t * MatchParamField ( const ParamField_t * pf, ccp key );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// Memory Map ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[MemMapItem_t]]
typedef struct MemMapItem_t
{
u64 off; // offset
u64 size; // size
u8 overlap; // system info: item overlaps other items
u8 index; // user defined index
char info[62]; // user defined info text
} MemMapItem_t;
//-----------------------------------------------------------------------------
// [[MemMap_t]]
typedef struct MemMap_t
{
MemMapItem_t ** field; // pointer to the item field
uint used; // number of used titles in the item field
uint size; // number of allocated pointers in 'field'
u64 begin; // first address
} MemMap_t;
//-----------------------------------------------------------------------------
// Memory maps allow duplicate entries.
// The off+size pair is used as key.
// The entries are sorted by off and size (small values first).
//-----------------------------------------------------------------------------
void InitializeMemMap ( MemMap_t * mm );
void ResetMemMap ( MemMap_t * mm );
void CopyMemMap
(
MemMap_t * mm1, // merged mem map, not NULL, cleared
const MemMap_t * mm2, // NULL or second mem map
bool use_tie // TRUE: use InsertMemMapTie()
);
void MergeMemMap
(
MemMap_t * mm1, // merged mem map, not NULL, not cleared
const MemMap_t * mm2, // NULL or second mem map
bool use_tie // TRUE: use InsertMemMapTie()
);
MemMapItem_t * FindMemMap
(
// returns NULL or the pointer to the one! matched key (={off,size})
MemMap_t * mm,
off_t off,
off_t size
);
uint InsertMemMapIndex
(
// returns the index of the new item
MemMap_t * mm, // mem map pointer
off_t off, // offset of area
off_t size // size of area
);
MemMapItem_t * InsertMemMap
(
// returns a pointer to a new item (never NULL)
MemMap_t * mm, // mem map pointer
off_t off, // offset of area
off_t size // size of area
);
MemMapItem_t * InsertMemMapTie
(
// returns a pointer to a new or existing item (never NULL)
MemMap_t * mm, // mem map pointer
off_t off, // offset of area
off_t size // size of area
);
void InsertMemMapWrapper
(
void * param, // user defined parameter
u64 offset, // offset of object
u64 size, // size of object
ccp info // info about object
);
// Remove all entires with key. Return number of delete entries
//uint RemoveMemMap ( MemMap_t * mm, off_t off, off_t size );
// Return the index of the found or next item
uint FindMemMapHelper ( MemMap_t * mm, off_t off, off_t size );
// Calculate overlaps. Return number of items with overlap.
uint CalcOverlapMemMap ( MemMap_t * mm );
u64 FindFreeSpaceMemMap
(
// return address or NULL on not-found
const MemMap_t * mm,
u64 addr_beg, // first possible address
u64 addr_end, // last possible address
u64 size, // minimal size
u64 align, // aligning
u64 *space // not NULL: store available space here
);
// Print out memory map
void PrintMemMap ( MemMap_t * mm, FILE * f, int indent, ccp info_head );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// Memory Map: Scan addresses ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[ScanAddr_t]]
typedef struct ScanAddr_t
{
uint stat; // 0:invalid, 1:single address, 2:range
uint addr; // address
uint size; // size inf bytes
} ScanAddr_t;
//-----------------------------------------------------------------------------
char * ScanAddress
(
// returns first not scanned char of 'arg'
ScanAddr_t * result, // result, initialized by ScanAddress()
ccp arg, // format: ADDR | ADDR:END | ADDR#SIZE
uint default_base // base for numbers without '0x' prefix
// 0: C like with octal support
// 10: standard value for decimal numbers
// 16: standard value for hex numbers
);
//-----------------------------------------------------------------------------
uint InsertAddressMemMap
(
// returns the number of added addresses
MemMap_t * mm, // mem map pointer
bool use_tie, // TRUE: use InsertMemMapTie()
ccp arg, // comma separated list of addresses
// formats: ADDR | ADDR:END | ADDR#SIZE
uint default_base // base for numbers without '0x' prefix
// 0: C like with octal support
// 10: standard value for decimal numbers
// 16: standard value for hex numbers
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// DataBuf_t ///////////////
///////////////////////////////////////////////////////////////////////////////
struct DataBuf_t;
typedef enumError (*DataBufFill_t)
(
// function to fill the buffer
struct DataBuf_t* db, // valid pointer to related DataBuf_t to fill
uint need, // number of bytes needed => wait for more data
int mode // user mode, null on auto fill
);
//-----------------------------------------------------------------------------
// [[DataBuf_t]]
typedef struct DataBuf_t
{
uint size; // size of buffer
uint used; // number of used bytes
u64 offset; // current file offset, related to 'data'
u8 *buf; // pointer to buffer, cyclic usage
u8 *buf_end; // pointer to buffer end := buf + size
u8 *data; // pointer to data
u8 *data_end; // pointer to end of data
//--- user data
DataBufFill_t fill_func; // function to fill buffer
void *user_data; // NULL or pointer to user data for 'fill_func'
}
DataBuf_t;
///////////////////////////////////////////////////////////////////////////////
// management
void InitializeDataBuf
( DataBuf_t *db, uint buf_size, DataBufFill_t func, void *user_data );
void ResetDataBuf ( DataBuf_t *db );
void ResizeDataBuf ( DataBuf_t *db, uint new_buf_size );
void ClearDataBuf ( DataBuf_t *db, uint new_buf_size_if_not_null );
void EmptyDataBuf ( DataBuf_t *db );
void PackDataBuf ( DataBuf_t *db );
///////////////////////////////////////////////////////////////////////////////
// debugging
bool IsValidDataBuf ( DataBuf_t *db );
bool DumpDataBuf ( FILE *f, uint indent, DataBuf_t *db, bool if_invalid );
// fill the buffer with a test pattern:
// '-' for unused bytes
// ">0123456789<" in a longer or shorter form for used bytes
void FillPatternDataBuf ( DataBuf_t *db );
///////////////////////////////////////////////////////////////////////////////
// insert and remove data
static inline enumError FillDataBuf( DataBuf_t *db, uint need, int mode )
{
return db && db->fill_func ? db->fill_func(db,need,mode) : ERR_WARNING;
}
uint InsertDataBuf
(
// Insert at top of buffer.
// Return the number of inserted bytes
DataBuf_t *db, // valid pointer to data buffer
const void *data, // data to insert
uint data_size, // size of 'data'
bool all_or_none // true: insert complete data or nothing
);
uint AppendDataBuf
(
// Append data at end of buffer
// Return the number of inserted bytes
DataBuf_t *db, // valid pointer to data buffer
const void *data, // data to insert
uint data_size, // size of 'data'
bool all_or_none // true: insert complete data or nothing
);
///////////////////////////////////////////////////////////////////////////////
void * GetPtrDataBuf
(
// Return a pointer to the data and make sure,
// that the data is not wrapped. Return NULL if failed.
DataBuf_t *db, // valid pointer to data buffer
uint data_size, // number of needed bytes
uint align, // >1: Data alignment is also needed.
// Only 2,4,8,16,... are allowed, but not verified.
// Begin of buffer is always well aligned.
bool drop // true: On success, drop data from buffer
// In real, the data is available and only
// the pointers and counters are updated.
);
uint CopyDataBuf
(
// Copy data from buffer, but don't remove it
// Return the number of copied bytes
DataBuf_t *db, // valid pointer to data buffer
void *dest, // destination buffer wit at least 'max_size' space
uint min_read, // return with 0 read bytes if less bytes available
// if 'min_read' > 'size', NULL is returned
uint max_read // try to read upto 'max_read' bytes
);
uint GetDataBuf
(
// Copy data from buffer and remove it
// Return the number of copied bytes
DataBuf_t *db, // valid pointer to data buffer
void *dest, // destination buffer wit at least 'max_size' space
uint min_read, // return with 0 read bytes if less bytes available
uint max_read // try to read upto 'max_read' bytes
);
uint SkipDataBuf
(
// Skip 'skip_size' from input stream. Read more data if needed.
// Return the number of really skiped bytes
// If <skip_size is returned, no more data is available.
DataBuf_t *db, // valid pointer to data buffer
uint skip_size // number of bytes to drop
);
uint DropDataBuf
(
// Remove upto 'drop_size' bytes from buffer.
// Return the number of dropped bytes.
DataBuf_t *db, // valid pointer to data buffer
uint drop_size // number of bytes to drop
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// ResizeElement_t, ResizeHelper_t ///////////////
///////////////////////////////////////////////////////////////////////////////
typedef struct ResizeElement_t
{
uint n_elem;
uint factor;
}
ResizeElement_t;
//-----------------------------------------------------------------------------
typedef struct ResizeHelper_t
{
//--- basic data
uint src_size; // size of source
uint dest_size; // size of dest
//--- calculated data
uint sum_factor; // sum of all factors
uint half_factor; // half of 'sum_factor2'
uint idx_inc; // increment of 'src_idx'
uint frac_inc; // increment of 'src_frac'
uint frac_next; // if 'frac>=frac_next': increment 'src_idx'
//--- iteration data
uint dest_idx; // dest index: (0..dest_size(
uint src_idx; // src index: (0..src_size(
uint src_frac; // (0..frac_next(
ResizeElement_t // list terminates with n_elem=0
elem[4]; // ==> max 3 elements are used
}
ResizeHelper_t;
//-----------------------------------------------------------------------------
void InitializeResize
(
ResizeHelper_t *r, // data structure
uint s_size, // size of source
uint d_size // size of dest
);
bool FirstResize ( ResizeHelper_t *r );
bool NextResize ( ResizeHelper_t *r );
//
///////////////////////////////////////////////////////////////////////////////
/////////////// restore configuration ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[RestoreStateLog_t]]
typedef enum RestoreStateLog_t
{
RSL_WARNINGS = 0x01, // general warnings
RSL_MISSED_NAMES = 0x02, // hints about missed names
RSL_UNUSED_NAMES = 0x04, // hints about unused names
RSL_ALL = 0x07 // all flags
}
RestoreStateLog_t;
//-----------------------------------------------------------------------------
// [[RestoreState_t]]
typedef struct RestoreState_t
{
ccp sect; // section name
ccp path; // section sub path
int index; // index or -1
ParamField_t param; // name=value parameters
cvp user_param; // pointer provided as parameter by user
cvp user_info; // pointer to deliver insfos between stages
RestoreStateLog_t log_mode; // log mode
FILE *log_file; // log file
}
RestoreState_t;
///////////////////////////////////////////////////////////////////////////////
static inline void InitializeRestoreState ( RestoreState_t *rs )
{ DASSERT(rs); memset(rs,0,sizeof(*rs)); }
void ResetRestoreState ( RestoreState_t *rs );
///////////////////////////////////////////////////////////////////////////////
// [[RestoreStateFunc]]
typedef void (*RestoreStateFunc)
(
RestoreState_t *rs, // info data, can be modified (cleaned after call)
cvp user_table // pointer provided by RestoreStateTab_t[]
);
//-----------------------------------------------------------------------------
// [[RestoreStateTab_t]]
typedef struct RestoreStateTab_t
{
ccp name; // name of section
RestoreStateFunc func; // function to call
cvp user_table; // any user provided pointer
}
RestoreStateTab_t;
///////////////////////////////////////////////////////////////////////////////
enumError ScanRestoreState
(
RestoreState_t *rs, // valid control
void *data, // file data, modified, terminated by NULL or LF
uint size, // size of file data
void **end_data // not NULL: store end of analysed here
);
///////////////////////////////////////////////////////////////////////////////
enumError RestoreState
(
const RestoreStateTab_t
*table, // section table, terminated by name==0
void *data, // file data, modified, terminated by NULL or LF
uint size, // size of file data
RestoreStateLog_t log_mode, // print warnings (bit field)
FILE *log_file, // error file; if NULL use stderr
cvp user_param // pointer provided by user
);
///////////////////////////////////////////////////////////////////////////////
// print into cyclic buffer
ccp PrintRestoreStateSection ( const RestoreState_t *rs );
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
ParamFieldItem_t * GetParamField
(
const RestoreState_t *rs, // valid restore-state structure
ccp name // name of member
);
//-----------------------------------------------------------------------------
int GetParamFieldINT
(
const RestoreState_t *rs, // valid restore-state structure
ccp name, // name of member
int not_found // return this value if not found
);
//-----------------------------------------------------------------------------
uint GetParamFieldUINT
(
const RestoreState_t *rs, // valid restore-state structure
ccp name, // name of member
uint not_found // return this value if not found
);
//-----------------------------------------------------------------------------
u64 GetParamFieldS64
(
const RestoreState_t *rs, // valid restore-state structure
ccp name, // name of member
s64 not_found // return this value if not found
);
//-----------------------------------------------------------------------------
u64 GetParamFieldU64
(
const RestoreState_t *rs, // valid restore-state structure
ccp name, // name of member
u64 not_found // return this value if not found
);
//-----------------------------------------------------------------------------
float GetParamFieldFLOAT
(
const RestoreState_t *rs, // valid restore-state structure
ccp name, // name of member
float not_found // return this value if not found
);
//-----------------------------------------------------------------------------
double GetParamFieldDBL
(
const RestoreState_t *rs, // valid restore-state structure
ccp name, // name of member
double not_found // return this value if not found
);
//-----------------------------------------------------------------------------
long double GetParamFieldLDBL
(
const RestoreState_t *rs, // valid restore-state structure
ccp name, // name of member
long double not_found // return this value if not found
);
//-----------------------------------------------------------------------------
int GetParamFieldBUF
(
// returns >=0: length of read data; -1:nothing done (not_found==NULL)
char *buf, // buffer to store result
uint buf_size, // size of buffer
const RestoreState_t *rs, // valid restore-state structure
ccp name, // name of member
EncodeMode_t decode, // decoding mode, fall back to OFF
// supported: STRING, UTF8, BASE64, BASE64X
ccp not_found // not NULL: store this value if not found
);
//-----------------------------------------------------------------------------
mem_t GetParamFieldMEM
(
// Returns the decoded 'source'. Result is NULL-terminated.
// It points either to 'buf' or is alloced (on buf==NULL or too less space)
// If alloced (mem.ptr!=buf) => call FreeMem(&mem)
char *buf, // buffer to store result
uint buf_size, // size of buffer
const RestoreState_t *rs, // valid restore-state structure
ccp name, // name of member
EncodeMode_t decode, // decoding mode, fall back to OFF
// supported: STRING, UTF8, BASE64, BASE64X
mem_t not_found // not NULL: return this value
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// save + restore config by table ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[SaveRestoreType_t]]
typedef enum SaveRestoreType_t
{
SRT__TERM, // list terminator
SRT_BOOL, // type bool
SRT_UINT, // unsigned int of any size (%llu)
SRT_HEX, // unsigned int of any size as hex (%#llx)
SRT_COUNT, // unsigned int of any size (%llu), used as array counter
SRT_INT, // signed int of any size (%lld)
SRT_FLOAT, // float (%.8g) or double (%.16g) or long double (%.20g)
SRT_XFLOAT, // float or double or long double as hex-float (%a/%La)
SRT_STRING_SIZE, // string with max size, var is char[]
SRT_STRING_ALLOC, // alloced string, var is ccp or char*
SRT_MEM, // alloced string, var is mem_t
SRT_DEF_ARRAY, // define/end an array of structs
SRT__IS_LIST, //----- from here: print lists; also used as separator
SRT_STRING_FIELD, // var is StringField_t
SRT_PARAM_FIELD, // var is ParamField_t
SRT_F_SIZE = 0x100, // factor for implicit size-modes for numerical types
}
SaveRestoreType_t;
///////////////////////////////////////////////////////////////////////////////
// [[SaveRestoreTab_t]]
typedef struct SaveRestoreTab_t
{
uint offset; // offset of variable
uint size; // sizeof( var or string )
ccp name; // name in configuration file
s16 n_elem; // >0: is array with N elements
// <0: is array with -N elements, use last SRT_COUNT
u8 type; // SaveRestoreType_t
u8 emode; // EncodeMode_t
}
__attribute__ ((packed)) SaveRestoreTab_t;
//-----------------------------------------------------------------------------
// At the very beginning of the table, define 'SRT_NAME' by 2 lines:
// #undef SRT_NAME
// #define SRT_NAME name_of_the_struct
#define DEF_SRT_VAR(v,ne,n,t,e) {offsetof(SRT_NAME,v),sizeof(((SRT_NAME*)0)->v),n,ne,t,e}
#define DEF_SRT_BOOL(v,n) DEF_SRT_VAR(v,1,n,SRT_BOOL,0)
#define DEF_SRT_UINT(v,n) DEF_SRT_VAR(v,1,n,SRT_UINT,0)
#define DEF_SRT_HEX(v,n) DEF_SRT_VAR(v,1,n,SRT_HEX,0)
#define DEF_SRT_COUNT(v,n) DEF_SRT_VAR(v,1,n,SRT_COUNT,0)
#define DEF_SRT_INT(v,n) DEF_SRT_VAR(v,1,n,SRT_INT,0)
#define DEF_SRT_FLOAT(v,n) DEF_SRT_VAR(v,1,n,SRT_FLOAT,0)
#define DEF_SRT_XFLOAT(v,n) DEF_SRT_VAR(v,1,n,SRT_XFLOAT,0)
#define DEF_SRT_STR_SIZE(v,n,e) DEF_SRT_VAR(v,1,n,SRT_STRING_SIZE,e)
#define DEF_SRT_STR_ALLOC(v,n,e) DEF_SRT_VAR(v,1,n,SRT_STRING_ALLOC,e)
#define DEF_SRT_MEM(v,n,e) DEF_SRT_VAR(v,1,n,SRT_MEM,e)
#define DEF_SRT_STRING_FIELD(v,n,e) DEF_SRT_VAR(v,1,n,SRT_STRING_FIELD,e)
#define DEF_SRT_PARAM_FIELD(v,n,e) DEF_SRT_VAR(v,1,n,SRT_PARAM_FIELD,e)
//--- n elements : if n<0, then n_elem=-n && use last SRT_COUNT
#define DEF_SRT_BOOL_N(v,ne,n) DEF_SRT_VAR(v,ne,n,SRT_BOOL,0)
#define DEF_SRT_UINT_N(v,ne,n) DEF_SRT_VAR(v,ne,n,SRT_UINT,0)
#define DEF_SRT_HEX_N(v,ne,n) DEF_SRT_VAR(v,ne,n,SRT_HEX,0)
#define DEF_SRT_INT_N(v,ne,n) DEF_SRT_VAR(v,ne,n,SRT_INT,0)
#define DEF_SRT_FLOAT_N(v,ne,n) DEF_SRT_VAR(v,ne,n,SRT_FLOAT,0)
#define DEF_SRT_XFLOAT_N(v,ne,n) DEF_SRT_VAR(v,ne,n,SRT_XFLOAT,0)
#define DEF_SRT_STR_SIZE_N(v,ne,n,e) DEF_SRT_VAR(v,ne,n,SRT_STRING_SIZE,e)
#define DEF_SRT_STR_ALLOC_N(v,ne,n,e) DEF_SRT_VAR(v,ne,n,SRT_STRING_ALLOC,e)
#define DEF_SRT_MEM_N(v,ne,n,e) DEF_SRT_VAR(v,ne,n,SRT_MEM,e)
//--- auto array
#define DEF_SRT_ARRAY(v,n,t,e) \
{offsetof(SRT_NAME,v), sizeof(((SRT_NAME*)0)->v[0]), n, \
sizeof(((SRT_NAME*)0)->v)/sizeof(*((SRT_NAME*)0)->v), t, e }
#define DEF_SRT_BOOL_A(v,n) DEF_SRT_ARRAY(v,n,SRT_BOOL,0)
#define DEF_SRT_UINT_A(v,n) DEF_SRT_ARRAY(v,n,SRT_UINT,0)
#define DEF_SRT_HEX_A(v,n) DEF_SRT_ARRAY(v,n,SRT_HEX,0)
#define DEF_SRT_INT_A(v,n) DEF_SRT_ARRAY(v,n,SRT_INT,0)
#define DEF_SRT_FLOAT_A(v,n) DEF_SRT_ARRAY(v,n,SRT_FLOAT,0)
#define DEF_SRT_XFLOAT_A(v,n) DEF_SRT_ARRAY(v,n,SRT_XFLOAT,0)
#define DEF_SRT_STR_SIZE_A(v,n,e) DEF_SRT_ARRAY(v,n,SRT_STRING_SIZE,e)
#define DEF_SRT_STR_ALLOC_A(v,n,e) DEF_SRT_ARRAY(v,n,SRT_STRING_ALLOC,e)
#define DEF_SRT_MEM_A(v,n,e) DEF_SRT_ARRAY(v,n,SRT_MEM,e)
//--- auto array, use last SRT_COUNT for element-count
#define DEF_SRT_ARRAY_C(v,n,t,e) \
{offsetof(SRT_NAME,v), sizeof(((SRT_NAME*)0)->v[0]), n, \
-(s16)(sizeof(((SRT_NAME*)0)->v)/sizeof(*((SRT_NAME*)0)->v)), t, e }
#define DEF_SRT_BOOL_AC(v,n) DEF_SRT_ARRAY_C(v,n,SRT_BOOL,0)
#define DEF_SRT_UINT_AC(v,n) DEF_SRT_ARRAY_C(v,n,SRT_UINT,0)
#define DEF_SRT_HEX_AC(v,n) DEF_SRT_ARRAY_C(v,n,SRT_HEX,0)
#define DEF_SRT_INT_AC(v,n) DEF_SRT_ARRAY_C(v,n,SRT_INT,0)
#define DEF_SRT_FLOAT_AC(v,n) DEF_SRT_ARRAY_C(v,n,SRT_FLOAT,0)
#define DEF_SRT_XFLOAT_AC(v,n) DEF_SRT_ARRAY_C(v,n,SRT_XFLOAT,0)
#define DEF_SRT_STR_SIZE_AC(v,n,e) DEF_SRT_ARRAY_C(v,n,SRT_STRING_SIZE,e)
#define DEF_SRT_STR_ALLOC_AC(v,n,e) DEF_SRT_ARRAY_C(v,n,SRT_STRING_ALLOC,e)
#define DEF_SRT_MEM_AC(v,n,e) DEF_SRT_ARRAY_C(v,n,SRT_MEM,e)
//--- array of structs
#define DEF_SRT_ARRAY_FL(f,l) \
{offsetof(SRT_NAME,f), sizeof(((SRT_NAME*)0)->f), 0, \
(offsetof(SRT_NAME,l)-offsetof(SRT_NAME,f))/sizeof(((SRT_NAME*)0)->f)+1, \
SRT_DEF_ARRAY, 0 }
#define DEF_SRT_ARRAY_FLC(f,l) \
{offsetof(SRT_NAME,f), sizeof(((SRT_NAME*)0)->f), 0, \
-(s16)((offsetof(SRT_NAME,l)-offsetof(SRT_NAME,f)) \
/sizeof(((SRT_NAME*)0)->f)+1), SRT_DEF_ARRAY, 0 }
#define DEF_SRT_ARRAY_N(v,ne) DEF_SRT_VAR(v[0],ne,0,SRT_DEF_ARRAY,0)
#define DEF_SRT_ARRAY_A(v) DEF_SRT_ARRAY(v,0,SRT_DEF_ARRAY,0)
#define DEF_SRT_ARRAY_AC(v) DEF_SRT_ARRAY_C(v,0,SRT_DEF_ARRAY,0)
#define DEF_SRT_ARRAY_END() {0,0,0,0,SRT_DEF_ARRAY,0}
//--- global array of structs
#define DEF_SRT_ARRAY_GN(ne) {0,sizeof(SRT_NAME),0,ne,SRT_DEF_ARRAY,0}
#define DEF_SRT_ARRAY_GA(v) {0,sizeof(*v),0,sizeof(v)/sizeof(*v),SRT_DEF_ARRAY,0}
//--- special
#define DEF_SRT_SEPARATOR() {0,0,0,0,SRT__IS_LIST,0}
#define DEF_SRT_COMMENT(c) {0,0,c,0,SRT__IS_LIST,0}
#define DEF_SRT_TERM() {0,0,0,0,SRT__TERM,0}
///////////////////////////////////////////////////////////////////////////////
extern int srt_auto_dump;
extern FILE *srt_auto_dump_file;
void DumpStateTable
(
FILE *f, // valid output file
int indent,
const SaveRestoreTab_t
*srt // list of variables
);
void SaveCurrentStateByTable
(
FILE *f, // valid output file
cvp data0, // valid pointer to source struct
const SaveRestoreTab_t
*srt, // list of variables
ccp prefix, // NULL or prefix for names
uint fw_name // field width of name, 0=AUTO
// tabs are used for multiple of 8 and for AUTO
);
void RestoreStateByTable
(
RestoreState_t *rs, // info data, can be modified (cleaned after call)
void *data0, // valid pointer to destination struct
const SaveRestoreTab_t
*srt, // list of variables
ccp prefix // NULL or prefix for names
);
//
///////////////////////////////////////////////////////////////////////////////
/////////////// GrowBuffer_t ///////////////
///////////////////////////////////////////////////////////////////////////////
// [[GrowBuffer_t]]
typedef struct GrowBuffer_t
{
// allways: gb->ptr[gb->used] == 0
u8 *buf; // NULL or data buffer
uint size; // size of 'buf'
uint grow_size; // >0: auto grow buffer by multiple of this
uint max_size; // >0: max size for auto grow
u8 *ptr; // pointer to first valid byte
uint used; // number of valid bytes behind 'ptr'
uint max_used; // max 'used' value
uint disabled; // disable rading from TCP socket
}
GrowBuffer_t;
///////////////////////////////////////////////////////////////////////////////
void LogGrowBuffer
(
FILE *f, // output file
int indent, // indention
const GrowBuffer_t *gb, // valid grow-buffer
ccp format, // format string for vfprintf()
... // arguments for 'vfprintf(format,...)'
)
__attribute__ ((__format__(__printf__,4,5)));
//-----------------------------------------------------------------------------
void InitializeGrowBuffer ( GrowBuffer_t *gb, uint max_buf_size );
void ResetGrowBuffer ( GrowBuffer_t *gb );
uint ClearGrowBuffer ( GrowBuffer_t *gb ); // returns available space
uint PurgeGrowBuffer ( GrowBuffer_t *gb ); // returns available space
//-----------------------------------------------------------------------------
uint GetSpaceGrowBuffer
(
// returns the maximum possible space
const GrowBuffer_t *gb // valid grow-buffer object
);
//-----------------------------------------------------------------------------
uint PrepareGrowBuffer
(
// returns the number of available space, smaller than or equal 'size'
GrowBuffer_t *gb, // valid grow-buffer object
uint size, // needed size
bool force // always alloc enough space and return 'size'
);
uint InsertGrowBuffer
(
// returns the number of inserted bytes
GrowBuffer_t *gb, // valid grow-buffer object
const void *data, // data to insert
uint size // size of 'data'
);
static inline uint InsertCharGrowBuffer
(
// returns the number of inserted bytes (0 on fail, 1 else)
GrowBuffer_t *gb, // valid grow-buffer object
char ch // any character including NULL
)
{
return InsertGrowBuffer(gb,&ch,1);
}
static inline uint InsertMemGrowBuffer
(
// returns the number of inserted bytes
GrowBuffer_t *gb, // valid grow-buffer object
mem_t mem // any data
)
{
return InsertGrowBuffer(gb,mem.ptr,mem.len);
}
uint InsertCapNGrowBuffer
(
// insert 'cap_1' or 'cap_n' depending of 'n' and the existence of 'cap_*'
// returns the number of inserted bytes
GrowBuffer_t *gb, // valid grow-buffer object
int n, // repeat number; if <1: insert nothing
mem_t cap_1, // default for N=1
mem_t cap_n // default for N>1
);
uint WriteCapN
(
// print 'cap_1' or 'cap_n' depending of 'n' and the existence of 'cap_*'
// returns the number of printed bytes
FILE *f, // valid FILE
int n, // repeat number; if <1: insert nothing
mem_t cap_1, // default for N=1
mem_t cap_n // default for N>1
);
uint InsertCatchedGrowBuffer
(
// returns the number of inserted bytes
GrowBuffer_t *gb, // valid grow-buffer object
SavedStdFiles_t *ssf // saved output and catched data
);
///////////////////////////////////////////////////////////////////////////////
uint PrintArgGrowBuffer
(
// returns the number of inserted bytes
GrowBuffer_t *gb, // valid grow-buffer object
ccp format, // format string for vfprintf()
va_list arg // arguments for 'vsnprintf(format,...)'
);
uint PrintGrowBuffer
(
// returns the number of inserted bytes
GrowBuffer_t *gb, // valid grow-buffer object
ccp format, // format string for vfprintf()
... // arguments for 'vsnprintf(format,...)'
)
__attribute__ ((__format__(__printf__,2,3)));
//-----------------------------------------------------------------------------
uint ConvertToCrLfGrowBuffer
(
// returns the number of inserted CR bytes
GrowBuffer_t *gb, // valid grow-buffer object
uint begin // index to begin with conversion
);
//-----------------------------------------------------------------------------
uint DropGrowBuffer
(
// returns the number of dropped bytes
GrowBuffer_t *gb, // valid grow-buffer object
uint size // number of bytes to drop
);
//-----------------------------------------------------------------------------
#ifndef __APPLE__
FILE * OpenFileGrowBuffer
(
// returns a FILE opened by fmemopen() => call CloseFileGrowBuffer()
GrowBuffer_t *gb, // valid grow-buffer object
uint size // needed size
);
int CloseFileGrowBuffer
(
// returns the number of wirtten bytes, or -1 on error
GrowBuffer_t *gb, // valid grow-buffer object
FILE *f // FILE returned by OpenFileGrowBuffer()
);
#endif // !__APPLE__
//-----------------------------------------------------------------------------
void SaveCurrentStateGrowBuffer
(
FILE *f, // output file
ccp name_prefix, // NULL or prefix of name
uint tab_pos, // tab pos of '='
const GrowBuffer_t *gb // valid grow-buffer
);
void RestoreStateGrowBuffer
(
GrowBuffer_t *gb, // valid grow-buffer
ccp name_prefix, // NULL or prefix of name
RestoreState_t *rs // info data, can be modified (cleaned after call)
);
//-----------------------------------------------------------------------------
int WriteDirectGrowBuffer
(
// write() data direct without blocking and without calling any notifier.
// If output buf is not empty or send failed, append the data to 'gb'.
// Returns the number of bytes added to the grow buffer or -1 on error.
// The data is send+stored completely (returns 'size') or not at all
// (returns '0'). -1 is returned if the socket is invalid.
GrowBuffer_t *gb, // grow buffer to cache data
int fd, // destination file
bool flush_output, // true: try to flush 'gb' before
cvp data, // data to send
uint size, // size of 'data', If NULL && flush: flush only
uint *send_count // not NULL: add num of sent bytes
);
//-----------------------------------------------------------------------------
int SendDirectGrowBuffer
(
// send() data direct without blocking and without calling any notifier.
// If output buf is not empty or send failed, append the data to 'gb'.
// Returns the number of bytes added to the grow buffer or -1 on error.
// The data is send+stored completely (returns 'size') or not at all
// (returns '0'). -1 is returned if the socket is invalid.
GrowBuffer_t *gb, // grow buffer to cache data
int sock, // destination socket
bool flush_output, // true: try to flush 'gb' before
cvp data, // data to send
uint size, // size of 'data', If NULL && flush: flush only
uint *send_count // not NULL: add num of sent bytes
);
//-----------------------------------------------------------------------------
static inline int FlushGrowBuffer
(
// Try to flush the bufer using SendDirectGrowBuffer() withour new data.
GrowBuffer_t *gb, // grow buffer to cache data
int sock, // destination socket
uint *send_count // not NULL: add num of sent bytes
)
{
return SendDirectGrowBuffer(gb,sock,true,0,0,send_count);
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// misc ///////////////
///////////////////////////////////////////////////////////////////////////////
#define CONVERT_TO_STRING1(a) #a
#define CONVERT_TO_STRING(a) CONVERT_TO_STRING1(a)
extern int opt_new; // default 0
float double2float ( double d ); // reduce precision
uint CreateUniqueIdN ( int range );
static inline uint CreateUniqueId() { return CreateUniqueIdN(1); }
static inline uint CreateUniqueIdNBO() { return htonl(CreateUniqueIdN(1)); }
void Sha1Hex2Bin ( sha1_hash_t bin, ccp src, ccp end );
void Sha1Bin2Hex ( sha1_hex_t hex, cvp bin );
ccp GetSha1Hex ( cvp bin );
///////////////////////////////////////////////////////////////////////////////
static uint snprintfS ( char *buf, size_t size, ccp format, ... )
__attribute__ ((__format__(__printf__,3,4)));
static inline uint snprintfS ( char *buf, size_t size, ccp format, ... )
{
va_list arg;
va_start(arg,format);
const int res = vsnprintf(buf,size,format,arg);
va_end(arg);
return res < 0 ? 0 : res < size ? res : size - 1;
}
static char * snprintfE ( char *buf, char *end, ccp format, ... )
__attribute__ ((__format__(__printf__,3,4)));
static inline char * snprintfE ( char *buf, char *end, ccp format, ... )
{
const int size = end - buf;
DASSERT( size > 0 );
va_list arg;
va_start(arg,format);
const int res = vsnprintf(buf,size,format,arg);
va_end(arg);
return buf + ( res < 0 ? 0 : res < size ? res : size - 1 );
}
///////////////////////////////////////////////////////////////////////////////
void * dc_memrchr ( cvp src, int ch, size_t size );
bool IsMemConst ( cvp mem, uint size, u8 val );
#ifdef __APPLE__
#define memrchr dc_memrchr
#endif
//
///////////////////////////////////////////////////////////////////////////////
/////////////// E N D ///////////////
///////////////////////////////////////////////////////////////////////////////
#endif // DCLIB_BASICS_H
|