[go: up one dir, main page]

Menu

[9c90d7]: / util / utmanage.c  Maximize  Restore  History

Download this file

2257 lines (2095 with data), 73.0 kB

   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
/*
* This file was written by Bill Cox. It is hereby placed into the public domain.
*/
/*--------------------------------------------------------------------------------------------------
This module provides basic database administration capabilities.
--------------------------------------------------------------------------------------------------*/
#include <stdlib.h>
#include <ctype.h>
#include "ddutil.h"
#include "utpersist.h"
static char *utLineBuffer;
static uint32 utLineSize, utLinePosition;
static uint64 utLineNum;
static bool utAtEndOfFile;
static char *utLastToken;
static bool utShowHidden;
static FILE *utInputFile, *utOutputFile;
static utField *utFieldTranslationTable;
static uint32 utNumFields;
/*--------------------------------------------------------------------------------------------------
Read a module header.
--------------------------------------------------------------------------------------------------*/
static void reportError(
char *format,
...)
{
char *buff;
va_list ap;
va_start(ap, format);
buff = utVsprintf(format, ap);
va_end(ap);
if(utInputFile != stdin) {
utWarning("Line %lld, token \"%s\": %s", utLineNum, utLastToken, buff);
} else {
utWarning("Token \"%s\": %s", utLastToken, buff);
}
}
/*--------------------------------------------------------------------------------------------------
Return an integer of the given size.
--------------------------------------------------------------------------------------------------*/
uint64 utFindIntValue(
void *values,
uint8 size)
{
switch(size) {
case 1: return *(uint8 *)values;
case 2: return *(uint16 *)values;
case 4: return *(uint32 *)values;
case 8: return *(uint64 *)values;
}
utExit("Illegal integer size");
return 0;
}
/*--------------------------------------------------------------------------------------------------
Print out help.
--------------------------------------------------------------------------------------------------*/
static void printHelp(void)
{
fprintf(utOutputFile,
"create <module> <class> - allocate a new object, and return it's object number\n"
"compact - Compact the database, and delete the recent_changes file\n"
"destroy <module> <class> <object number> - Destroy an object\n"
"help - this command\n"
"list - list the modules in the database, their object counts and memory usage\n"
"list <module> - list classes in the module, their object counts and memory usage\n"
"list <module> <class> - list fields of a class\n"
"quit - quit the database manager\n"
"set <module> <class> <object number> = comma separated values – \n"
" set all fields of an object\n"
"set <module> <class> <object number> <field> = <value> - set a value\n"
"show <module> <class> - show all field values of all objects of the class\n"
"show <module> <class> <object number> - show an object's fields\n"
"show_hidden <true or false> - Enable/disable listing of DataDraw internal fields\n"
"load_binary <file> - Read the data from the binary database file into the database\n"
"save_binary <file> - Write out the database in binary format to the file\n"
"load_text <file> - Read the data from the text database file into the database\n"
"save_text <file> - Write out the database in text format to the file\n");
}
/*--------------------------------------------------------------------------------------------------
Return the number of objects allocated for the module.
--------------------------------------------------------------------------------------------------*/
static uint64 countModuleObjects(
utModule module)
{
utClass theClass;
uint64 numObjects = 0;
utForeachModuleClass(module, theClass) {
numObjects += utFindIntValue(utClassGetNumUsedPtr(theClass), utClassGetReferenceSize(theClass));
} utEndModuleClass;
return numObjects;
}
/*--------------------------------------------------------------------------------------------------
Return the total memory size used by the class.
--------------------------------------------------------------------------------------------------*/
static uint64 findClassMemory(
utClass theClass)
{
utField field;
uint64 memory = 0;
uint64 numUsed;
utForeachClassField(theClass, field) {
if(utFieldArray(field)) {
if(!utFieldFixedSize(field)) {
numUsed = *utFieldGetNumUsedPtr(field);
} else {
numUsed = utFindIntValue(utClassGetNumUsedPtr(theClass), utClassGetReferenceSize(theClass));
numUsed *= utFieldGetLength(field);
}
} else {
numUsed = utFindIntValue(utClassGetNumUsedPtr(theClass), utClassGetReferenceSize(theClass));
}
memory += utFieldGetSize(field)*numUsed;
} utEndClassField;
return memory;
}
/*--------------------------------------------------------------------------------------------------
List the modules in the database.
--------------------------------------------------------------------------------------------------*/
static uint64 findModuleMemory(
utModule module)
{
utClass theClass;
uint64 memory = 0;
utForeachModuleClass(module, theClass) {
memory += findClassMemory(theClass);
} utEndModuleClass;
return memory;
}
/*--------------------------------------------------------------------------------------------------
Find the ammount in memory units.
--------------------------------------------------------------------------------------------------*/
char *utMemoryUnits(
uint64 memory)
{
#if (_MSC_VER >= 1200) && (_MSC_VER < 1300)
//for Visual Studio 6 use a signed __int64
signed __int64 tempMem = memory;
if(memory > 1024*1024) {
return utSprintf("%.5g MB", tempMem/(1024.0*1024.0));
} else if(memory > 1024) {
return utSprintf("%.5g KB", tempMem/1024.0);
}
return utSprintf("%lu bytes", tempMem);
#else
if(memory > 1024*1024) {
return utSprintf("%.5g MB", memory/(1024.0*1024.0));
} else if(memory > 1024) {
return utSprintf("%.5g KB", memory/1024.0);
}
return utSprintf("%lu bytes", memory);
#endif
}
/*--------------------------------------------------------------------------------------------------
List the modules in the database.
--------------------------------------------------------------------------------------------------*/
static void listModules(void)
{
utModule module;
fprintf(utOutputFile, "modules:\n");
utForeachModule(module) {
if(utModuleInitialized(module)) {
fprintf(utOutputFile, " %s - %llu objects, %s memory\n", utModuleGetPrefix(module),
countModuleObjects(module), utMemoryUnits(findModuleMemory(module)));
}
} utEndModule;
}
/*--------------------------------------------------------------------------------------------------
List the classes in the module.
--------------------------------------------------------------------------------------------------*/
static void listClasses(
utModule module)
{
utClass theClass;
uint64 numUsed;
fprintf(utOutputFile, "Module %s classes:\n", utModuleGetPrefix(module));
utForeachModuleClass(module, theClass) {
numUsed = utFindIntValue(utClassGetNumUsedPtr(theClass), utClassGetReferenceSize(theClass));
fprintf(utOutputFile, " %s - %llu objects, %s memory\n", utClassGetName(theClass), numUsed,
utMemoryUnits(findClassMemory(theClass)));
} utEndModuleClass;
}
/*--------------------------------------------------------------------------------------------------
Just return a name for the field type.
--------------------------------------------------------------------------------------------------*/
static char *findBaseFieldTypeName(
utField field)
{
switch(utFieldGetType(field)) {
case UT_INT: return utSprintf("int%u", utFieldGetSize(field) << 3);
case UT_UINT: return utSprintf("uint%u", utFieldGetSize(field) << 3);
case UT_FLOAT: return "float";
case UT_DOUBLE: return "double";
case UT_BIT: return "bit";
case UT_BOOL: return "bool";
case UT_CHAR: return "char";
case UT_SYM: return "sym";
case UT_ENUM: return "enum";
case UT_TYPEDEF: return "typedef";
case UT_POINTER: return utSprintf("pointer %s", utFieldGetDestName(field));
case UT_UNION: return utSprintf("union %s", utFieldGetDestName(field));
default:
utExit("Unknown property type");
}
return NULL; /* Dummy return */
}
/*--------------------------------------------------------------------------------------------------
Just return a name for the field type.
--------------------------------------------------------------------------------------------------*/
static char *findFieldTypeName(
utField field)
{
char *baseName = findBaseFieldTypeName(field);
if(utFieldArray(field)) {
if(!utFieldFixedSize(field)) {
return utSprintf("array %s", baseName);
}
return utSprintf("array %s[%u]", baseName, utFieldGetLength(field));
}
return baseName;
}
/*--------------------------------------------------------------------------------------------------
List the fields of the class.
--------------------------------------------------------------------------------------------------*/
static void listFields(
utClass theClass)
{
utField field;
char *length;
fprintf(utOutputFile, "Class %s fields:\n", utClassGetName(theClass));
utForeachClassField(theClass, field) {
if(!utFieldHidden(field) || utShowHidden) {
length = "";
if(utFieldFixedSize(field)) {
length = utSprintf("[%u]", utFieldGetLength(field));
}
fprintf(utOutputFile, " %s %s%s\n", findFieldTypeName(field), utFieldGetName(field), length);
}
} utEndClassField;
}
/*--------------------------------------------------------------------------------------------------
Find the enum with the given name.
--------------------------------------------------------------------------------------------------*/
static utEnum findEnum(
utModule module,
char *name)
{
utEnum theEnum;
utForeachModuleEnum(module, theEnum) {
if(!strcmp(utEnumGetName(theEnum), name)) {
return theEnum;
}
} utEndModuleEnum;
return NULL;
}
/*--------------------------------------------------------------------------------------------------
Find the entry with the given value.
--------------------------------------------------------------------------------------------------*/
static utEntry findEntryFromValue(
utEnum theEnum,
uint32 value)
{
utEntry entry;
utForeachEnumEntry(theEnum, entry) {
if(utEntryGetValue(entry) == value) {
return entry;
}
} utEndEnumEntry;
return NULL;
}
/*--------------------------------------------------------------------------------------------------
Find the entry with the given name.
--------------------------------------------------------------------------------------------------*/
static utEntry findEntryFromName(
utEnum theEnum,
char *name)
{
utEntry entry;
utForeachEnumEntry(theEnum, entry) {
if(!strcmp(utEntryGetName(entry), name)) {
return entry;
}
} utEndEnumEntry;
return NULL;
}
/*--------------------------------------------------------------------------------------------------
Return a hexidecimal digit representation of the 4-bit value.
--------------------------------------------------------------------------------------------------*/
static char findHexDigit(
uint8 value)
{
if(value <= 9) {
return '0' + value;
}
return 'A' + value - 10;
}
/*--------------------------------------------------------------------------------------------------
Return a long hexidecimal string representing the binary value.
--------------------------------------------------------------------------------------------------*/
char *utFindHexString(
uint8 *values,
uint32 size)
{
char *buffer = utMakeString((size << 1) + 1);
char *p = buffer + (size << 1) - 1;
buffer[size*2] = '\0';
while(size-- != 0) {
*p-- = findHexDigit((uint8)(*values & 0xf));
*p-- = findHexDigit((uint8)((*values++) >> 4));
}
return buffer;
}
/*--------------------------------------------------------------------------------------------------
Count the non "isprint" characters in the string.
--------------------------------------------------------------------------------------------------*/
static uint32 countNonprintableChars(
char *string)
{
uint32 nonPrintableChars = 0;
while(*string) {
if(!isprint((unsigned char)*string)) {
nonPrintableChars++;
}
string++;
}
return nonPrintableChars;
}
/*--------------------------------------------------------------------------------------------------
Convert any non-string friendly characters to \" or \<number> format.
--------------------------------------------------------------------------------------------------*/
static char *mungeString(
char *string)
{
char *buffer = utMakeString(strlen(string) + 4*countNonprintableChars(string) + 1);
char *p = buffer;
char intValue[4];
while(*string != '\0') {
if(isprint((unsigned char)*string)) {
if(*string == '"' || *string == '\\') {
*p++ = '\\';
}
*p++ = *string++;
} else {
sprintf(intValue, "\\%u", *string);
intValue[3] = '\0';
strcpy(p, intValue);
p += strlen(intValue);
string++;
}
}
*p = '\0';
return buffer;
}
/*--------------------------------------------------------------------------------------------------
Find the union case based on the switch field.
--------------------------------------------------------------------------------------------------*/
static utUnioncase findUnioncase(
utUnion theUnion,
uint64 objectNumber)
{
utField switchField = utUnionGetSwitchField(theUnion);
utUnioncase unioncase;
uint32 value = *(*(uint32 **)(utFieldGetArrayPtr(switchField)) + objectNumber);
utForeachUnionUnioncase(theUnion, unioncase) {
if(utUnioncaseGetValue(unioncase) == value) {
return unioncase;
}
} utEndUnionUnioncase;
return NULL;
}
/*--------------------------------------------------------------------------------------------------
Return a string representation of the field value.
--------------------------------------------------------------------------------------------------*/
static char *findFieldValue(
utField field,
uint64 objectNumber)
{
utModule module;
utClass theClass;
utEnum theEnum;
utEntry entry;
utUnion theUnion;
utUnioncase unioncase;
utSym sym;
uint8 *values;
uint32 size = utFieldGetSize(field);
utFieldType type = utFieldGetType(field);
if(type == UT_BIT) {
values = *(uint8 **)(utFieldGetArrayPtr(field)) + (objectNumber >> 3)*utFieldGetSize(field);
} else {
values = *(uint8 **)(utFieldGetArrayPtr(field)) + objectNumber*utFieldGetSize(field);
}
if(type == UT_UNION) {
theUnion = utFieldGetUnion(field);
unioncase = findUnioncase(theUnion, objectNumber);
if(unioncase != NULL) {
type = utUnioncaseGetType(unioncase);
size = utUnioncaseGetSize(unioncase);
}
}
switch(type) {
case UT_BIT:
if((*values >> (objectNumber & 7)) & 1) {
return "true";
}
return "false";
case UT_BOOL:
if(*values == 1) {
return "true";
} else if(*values == 0) {
return "false";
}
return utSprintf("%d", *values);
case UT_INT:
switch(size) {
case 1: return utSprintf("%d", *(int8 *)values);
case 2: return utSprintf("%d", *(int16 *)values);
case 4: return utSprintf("%d", *(int32 *)values);
case 8: return utSprintf("%d", *(int64 *)values);
}
utExit("Invalid integer size");
case UT_UINT:
switch(size) {
case 1: return utSprintf("%u", *(uint8 *)values);
case 2: return utSprintf("%u", *(uint16 *)values);
case 4: return utSprintf("%u", *(uint32 *)values);
case 8: return utSprintf("%u", *(uint64 *)values);
}
utExit("Invalid integer size");
case UT_CHAR:
if(isprint(*values)) {
return utSprintf("'%c'", *values);
}
return utSprintf("'\\%u'", *values);
case UT_FLOAT:
return utSprintf("%g", *(float *)values);
case UT_DOUBLE:
return utSprintf("%g", *(double *)values);
case UT_POINTER:
switch(size) {
case 1: return utSprintf("0x%x", *(uint8 *)values);
case 2: return utSprintf("0x%x", *(uint16 *)values);
case 4: return utSprintf("0x%x", *(uint32 *)values);
case 8: return utSprintf("0x%x", *(uint64 *)values);
}
case UT_TYPEDEF: case UT_UNION:
return utFindHexString(values, size);
case UT_ENUM:
theClass = utFieldGetClass(field);
module = utClassGetModule(theClass);
theEnum = findEnum(module, utFieldGetDestName(field));
entry = findEntryFromValue(theEnum, *(uint32 *)values);
return utEntryGetName(entry);
case UT_SYM:
sym = *(utSym *)values;
if(sym == utSymNull) {
return utSprintf("0x%x", utSymNull);
}
if((uint32)(sym - (utSym)0) < utRootData.usedSym) {
return utSprintf("\"%s\"", mungeString(utSymGetName(*(utSym *)values)));
}
return utSprintf("invalid:0x%x", (sym - (utSym)0));
}
utExit("Unknow field type");
return NULL;
}
/*--------------------------------------------------------------------------------------------------
Determine if the only '\0' is at the end of the character array.
--------------------------------------------------------------------------------------------------*/
static bool zeroTerminated(
char *values,
uint32 length)
{
if(values[--length] != '\0') {
return false;
}
while(length != 0 && values[length] != '\0') {
length--;
}
return values[length] == '\0';
}
/*--------------------------------------------------------------------------------------------------
Show the data in the array. If we go over maxFields, we print '...'.
--------------------------------------------------------------------------------------------------*/
static void writeArray(
utField field,
uint64 objectNumber,
uint32 maxFields)
{
uint32 numElements;
uint32 xElement;
bool firstTime = true;
char *values = utFieldGetGetValues(field)(objectNumber, &numElements);
uint32 firstElement = (values - *(char **)(utFieldGetArrayPtr(field)))/utFieldGetSize(field);
if(numElements != 0 && utFieldGetType(field) == UT_CHAR && zeroTerminated(values, numElements)) {
fprintf(utOutputFile, "\"%s\"", mungeString(values));
return;
}
fprintf(utOutputFile, "(");
for(xElement = 0; xElement < numElements && xElement < maxFields; xElement++) {
if(!firstTime) {
fprintf(utOutputFile, ", ");
}
firstTime = false;
fprintf(utOutputFile, "%s", findFieldValue(field, firstElement + xElement));
}
if(xElement == maxFields) {
fprintf(utOutputFile, ", ...");
}
fprintf(utOutputFile, ")");
}
/*--------------------------------------------------------------------------------------------------
Show the fields of an object.
--------------------------------------------------------------------------------------------------*/
static void showObject(
utClass theClass,
uint64 objectNumber)
{
utField field;
bool firstTime = true;
utForeachClassField(theClass, field) {
if(!utFieldHidden(field) || utShowHidden) {
if(!firstTime) {
fprintf(utOutputFile, ", ");
}
firstTime = false;
fprintf(utOutputFile, "%s=", utFieldGetName(field));
if(!utFieldArray(field)) {
fprintf(utOutputFile, "%s", findFieldValue(field, objectNumber));
} else {
writeArray(field, objectNumber, 16);
}
}
} utEndClassField;
fprintf(utOutputFile, "\n");
}
/*--------------------------------------------------------------------------------------------------
Just print the field names.
--------------------------------------------------------------------------------------------------*/
static void printColumnHeaders(
utClass theClass)
{
utField field;
bool firstTime = true;
fprintf(utOutputFile, "ObjectNumber, ");
utForeachClassField(theClass, field) {
if(!utFieldHidden(field)) {
if(!firstTime) {
fprintf(utOutputFile, ", ");
}
firstTime = false;
fprintf(utOutputFile, "%s", utFieldGetName(field));
}
} utEndClassField;
fprintf(utOutputFile, "\n");
}
/*--------------------------------------------------------------------------------------------------
Just print the field types.
--------------------------------------------------------------------------------------------------*/
static void printColumnTypes(
utClass theClass)
{
utField field;
bool firstTime = true;
fprintf(utOutputFile, "uint%u, ", utClassGetReferenceSize(theClass) << 3);
utForeachClassField(theClass, field) {
if(!utFieldHidden(field)) {
if(!firstTime) {
fprintf(utOutputFile, ", ");
}
firstTime = false;
fprintf(utOutputFile, "%s", findFieldTypeName(field));
}
} utEndClassField;
fprintf(utOutputFile, "\n");
}
/*--------------------------------------------------------------------------------------------------
Print the object's values in a comma separated list.
--------------------------------------------------------------------------------------------------*/
static void showObjectFields(
utClass theClass,
uint64 objectNumber)
{
utField field;
bool firstTime = true;
fprintf(utOutputFile, "0x%llx, ", objectNumber);
utForeachClassField(theClass, field) {
if(!utFieldHidden(field) || utShowHidden) {
if(!firstTime) {
fprintf(utOutputFile, ", ");
}
firstTime = false;
if(!utFieldArray(field)) {
fprintf(utOutputFile, "%s", findFieldValue(field, objectNumber));
} else {
writeArray(field, objectNumber, UINT32_MAX);
}
}
} utEndClassField;
fprintf(utOutputFile, "\n");
}
/*--------------------------------------------------------------------------------------------------
Find the base class for this class.
--------------------------------------------------------------------------------------------------*/
utClass utClassGetBaseClass(
utClass theClass)
{
uint16 baseClassIndex = utClassGetBaseClassIndex(theClass);
if(baseClassIndex == UINT16_MAX) {
return utClassNull;
}
return utClasses + baseClassIndex;
}
/*--------------------------------------------------------------------------------------------------
Find the root class for this class, where we will find the memory management stuff.
--------------------------------------------------------------------------------------------------*/
utClass utClassFindRootClass(
utClass theClass)
{
utClass baseClass;
utDo {
baseClass = utClassGetBaseClass(theClass);
} utWhile(baseClass != utClassNull) {
theClass = baseClass;
} utRepeat;
return theClass;
}
/*--------------------------------------------------------------------------------------------------
Set the used flags for the objects of the class.
--------------------------------------------------------------------------------------------------*/
static void setObjectFreeFlags(
utClass theClass,
uint8 *objectFree)
{
utField field;
uint64 objectNumber;
theClass = utClassFindRootClass(theClass);
field = utClassGetNextFreeField(theClass);
objectNumber = utFindIntValue(utClassGetFirstFreePtr(theClass), utClassGetReferenceSize(theClass));
while(objectNumber != 0) {
objectFree[objectNumber >> 3] |= 1 << (objectNumber & 7);
objectNumber = utFindIntValue(*(uint8 **)(utFieldGetArrayPtr(field)) + objectNumber*utFieldGetSize(field),
(uint8)utFieldGetSize(field));
}
}
/*--------------------------------------------------------------------------------------------------
Show all the objects of a class.
--------------------------------------------------------------------------------------------------*/
static void showClass(
utModule module,
utClass theClass)
{
uint64 numUsed = utFindIntValue(utClassGetNumUsedPtr(theClass), utClassGetReferenceSize(theClass));
uint64 objectNumber;
bool hasFreeList = utClassGetDestructor(theClass) != NULL;
uint64 spaceNeeded = (numUsed + 7) >> 3;
uint8 *objectFree = NULL;
if(hasFreeList) {
objectFree = calloc(spaceNeeded, sizeof(uint8));
setObjectFreeFlags(theClass, objectFree);
}
fprintf(utOutputFile, "class %s %llu\n", utClassGetName(theClass), numUsed);
printColumnHeaders(theClass);
printColumnTypes(theClass);
for(objectNumber = 1; objectNumber < numUsed; objectNumber++) {
if(!hasFreeList || !(objectFree[objectNumber >> 3] & (1 << (objectNumber & 7)))) {
showObjectFields(theClass, objectNumber);
}
}
if(hasFreeList) {
free(objectFree);
}
}
/*--------------------------------------------------------------------------------------------------
Write the integer, given the width.
--------------------------------------------------------------------------------------------------*/
void utSetInteger(
uint8 *dest,
uint64 value,
uint8 width)
{
switch(width) {
case 1:
*dest = (uint8)value;
break;
case 2:
*(uint16 *)dest = (uint16)value;
break;
case 4:
*(uint32 *)dest = (uint32)value;
break;
case 8:
*(uint64 *)dest = (uint64)value;
break;
default:
utExit("Invalid integer width");
}
}
/*--------------------------------------------------------------------------------------------------
Parse the integer and return it's value. Allow hex if we see "0x" in front. Allow negation.
Set passed to indicate success.
--------------------------------------------------------------------------------------------------*/
bool utParseInteger(
int64 *dest,
char *string)
{
uint64 value = 0, oldValue = 0;
char c;
uint8 digit;
bool negate = false;
if(*string == '0' && *(string + 1) == 'x') {
string += 2;
while(*string) {
c = toupper((unsigned char)*string++);
if(c >= '0' && c <= '9') {
digit = c - '0';
} else if(c >= 'A' && c <= 'F') {
digit = c - 'A' + 10;
} else {
return false;
}
value <<= 4;
value |= digit;
if(value < oldValue) {
return false;
}
oldValue = value;
}
} else {
if(*string == '-') {
string++;
negate = true;
}
while(*string) {
c = *string++;
if(c < '0' || c > '9') {
return false;
}
value *= 10;
value += c - '0';
if(value < oldValue) {
return false;
}
oldValue = value;
}
}
*dest = negate? -value : value;
return true;
}
/*--------------------------------------------------------------------------------------------------
Read an integer and complain if it has bad width or syntax.
--------------------------------------------------------------------------------------------------*/
static bool readInteger(
uint8 *dest,
char *string,
uint8 width,
bool unsignedInt)
{
int64 value;
if(!utParseInteger(&value, string)) {
return false;
}
utSetInteger(dest, value, width);
return true;
}
/*--------------------------------------------------------------------------------------------------
Parse a character value.
--------------------------------------------------------------------------------------------------*/
static bool readChar(
uint8 *dest,
char *value)
{
char *end = value + strlen(value) - 1;
if(*value++ != '\'') {
return false;
}
if(*--end != '\'') {
return false;
}
*end = '\0';
if(*value == '\\') {
return readInteger(dest, value, 1, true);
}
*(char *)dest = *value;
return true;
}
/*--------------------------------------------------------------------------------------------------
Parse a floating point number.
--------------------------------------------------------------------------------------------------*/
static bool readFloat(
uint8 *dest,
char *value,
bool isFloat)
{
double floatVal;
char *endPtr;
floatVal = strtod(value, &endPtr);
if(endPtr == NULL || *endPtr != '\0') {
return false;
}
if(isFloat) {
*(float *)dest = (float)floatVal;
} else {
*(double *)dest = floatVal;
}
return true;
}
/*--------------------------------------------------------------------------------------------------
Read a 2-character hex byte.
--------------------------------------------------------------------------------------------------*/
static bool readHex(
uint8 *dest,
char *value)
{
char c = toupper((unsigned char)*value++);
uint8 byte;
if(c >= '0' && c <= '9') {
byte = c - '0';
} else if(c >= 'A' && c <= 'F') {
byte = c - 'A' + 10;
} else {
return false;
}
byte <<= 4;
c = toupper((unsigned char)*value);
if(c >= '0' && c <= '9') {
byte |= c - '0';
} else if(c >= 'A' && c <= 'F') {
byte |= c - 'A' + 10;
} else {
return false;
}
*dest = byte;
return true;
}
/*--------------------------------------------------------------------------------------------------
Parse a typedef value.
--------------------------------------------------------------------------------------------------*/
bool utReadHex(
uint8 *dest,
char *value,
uint32 size)
{
uint32 length = 0;
while(*value != '\0' && length <= size) {
if(!readHex(dest, value)) {
return false;
}
dest++;
value += 2;
length++;
}
return length == size;
}
/*--------------------------------------------------------------------------------------------------
Convert any \ escapes into characters.
--------------------------------------------------------------------------------------------------*/
static char *unmungeString(
char *string,
char **end)
{
char *buffer = utMakeString(strlen(string) + 1);
char *p = buffer;
char value;
*end = NULL;
if(*string++ != '"') {
return NULL;
}
while(*string != '\0' && *string != '"') {
if(*string == '\\') {
string++;
if(*string >= '0' && *string <= '9') {
value = 0;
do {
value = 10*value + *string++ - '0';
} while(*string >= '0' && *string <= '9');
*p++ = value;
} else {
*p++ = *string++;
}
} else {
if(!isprint((unsigned char)*string)) {
return NULL;
}
*p++ = *string++;
}
}
*p = '\0';
if(*string == '\0') {
return NULL;
}
*end = string + 1;
return buffer;
}
/*--------------------------------------------------------------------------------------------------
Parse a symbol. Valid values are a string, or 0x0.
--------------------------------------------------------------------------------------------------*/
static bool readSym(
uint8 *dest,
char *value)
{
utSym *symPtr = (utSym *)dest;
char *end;
char *string;
if(*value == '"') {
string = unmungeString(value, &end);
if(end == NULL) {
return false;
}
*symPtr = utSymCreate(string);
} else {
readInteger(dest, value, sizeof(utSym), true);
if(*symPtr != utSymNull) {
return false;
}
}
return true;
}
/*--------------------------------------------------------------------------------------------------
Parse an enumerated type, and write it to the destination.
--------------------------------------------------------------------------------------------------*/
static bool readEnum(
utEnum theEnum,
uint8 *dest,
char *value)
{
utEntry entry;
entry = findEntryFromName(theEnum, value);
if(entry == NULL) {
reportError("Unknown enumerated type value %s", value);
entry = utEnumGetFirstEntry(theEnum);
}
*(uint32 *)dest = utEntryGetValue(entry);
return true;
}
/*--------------------------------------------------------------------------------------------------
Parse a value of a given type, and write it to the destination.
--------------------------------------------------------------------------------------------------*/
static bool parseSpecificValue(
uint8 *dest,
char *value,
utFieldType type,
uint32 size,
uint64 objectNumber)
{
switch(type) {
case UT_BIT:
if(!strcasecmp(value, "true")) {
*(uint8 *)dest |= 1 << (objectNumber & 7);
} else if(!strcasecmp(value, "false")) {
*(uint8 *)dest &= ~(1 << (objectNumber & 7));
} else {
return false;
}
return true;
case UT_BOOL:
if(!strcasecmp(value, "true")) {
*(bool *)dest = true;
} else if(!strcasecmp(value, "false")) {
*(bool *)dest = false;
} else {
return readInteger(dest, value, 1, true);
}
return true;
case UT_INT:
return readInteger(dest, value, (uint8)size, false);
case UT_UINT:
return readInteger(dest, value, (uint8)size, true);
case UT_CHAR:
return readChar(dest, value);
case UT_FLOAT:
return readFloat(dest, value, true);
case UT_DOUBLE:
return readFloat(dest, value, true);
case UT_POINTER:
return readInteger(dest, value, (uint8)size, true);
case UT_TYPEDEF:
return utReadHex(dest, value, size);
case UT_SYM:
return readSym(dest, value);
default:
utExit("Invalid type");
}
return false; /* Dummy return */
}
/*--------------------------------------------------------------------------------------------------
Parse a value of a given type, and write it to the destination.
--------------------------------------------------------------------------------------------------*/
static bool parseValue(
uint8 *dest,
char *value,
utField field,
uint64 objectNumber)
{
utClass theClass = utFieldGetClass(field);
utModule module = utClassGetModule(theClass);
utUnion theUnion;
utUnioncase unioncase;
utEnum theEnum;
if(utFieldGetType(field) == UT_ENUM) {
theEnum = findEnum(module, utFieldGetDestName(field));
return readEnum(theEnum, dest, value);
}
if(utFieldGetType(field) != UT_UNION) {
return parseSpecificValue(dest, value, utFieldGetType(field), utFieldGetSize(field), objectNumber);
}
theUnion = utFieldGetUnion(field);
unioncase = findUnioncase(theUnion, objectNumber);
if(unioncase == NULL) {
return utReadHex(dest, value, utFieldGetSize(field));
}
if(utUnioncaseGetType(unioncase) == UT_ENUM) {
theEnum = findEnum(module, utFieldGetDestName(field));
return readEnum(theEnum, dest, value);
}
return parseSpecificValue(dest, value, utUnioncaseGetType(unioncase), utUnioncaseGetSize(unioncase), objectNumber);
}
/*--------------------------------------------------------------------------------------------------
Skip to the next comma or the end of a value list. If we find a syntax error of some sort,
return NULL.
--------------------------------------------------------------------------------------------------*/
static char *skipToNextComma(
char *valueList)
{
char *end, *prevChar;
if(*valueList == ',') {
valueList++;
}
while(isspace((unsigned char)*valueList)) {
valueList++;
}
if(*valueList == '"') {
unmungeString(valueList, &end);
if(end == NULL) {
return NULL;
}
valueList = end;
} else if(*valueList == '(') {
valueList++;
do {
valueList = skipToNextComma(valueList);
prevChar = valueList - 1;
while(isspace((unsigned char)*prevChar)) {
prevChar--;
}
} while(valueList != NULL && *prevChar != ')');
if(valueList == NULL) {
return NULL;
}
}
while(*valueList != '\0' && *valueList != ',') {
valueList++;
}
if(*valueList != ',' && *valueList != '\0') {
return NULL;
}
return valueList;
}
/*--------------------------------------------------------------------------------------------------
Count how many values are in the list.
--------------------------------------------------------------------------------------------------*/
static uint32 countListValues(
char *valueList)
{
uint32 numValues = 0;
while(isspace((unsigned char)*valueList)) {
valueList++;
}
while(valueList != NULL && *valueList != '\0') {
valueList = skipToNextComma(valueList);
numValues++;
}
if(valueList == NULL || *valueList != '\0') {
return 0; /* Invalid list */
}
return numValues;
}
/*--------------------------------------------------------------------------------------------------
Return the next element in a list of array values. Update the list pointer to the next element.
--------------------------------------------------------------------------------------------------*/
static char *parseArrayValue(
char **valueList)
{
char *end, *buffer, *prevChar;
uint32 length;
bool isArray;
while(isspace((unsigned char)**valueList)) {
(*valueList)++;
}
isArray = **valueList == '(';
end = skipToNextComma(*valueList);
if(end == NULL) {
return NULL;
}
prevChar = end;
if(isArray) {
(*valueList)++;
prevChar = end - 1;
while(isspace((unsigned char)*prevChar)) {
prevChar--;
}
}
length = prevChar - *valueList;
buffer = utMakeString(length + 1);
strncpy(buffer, *valueList, length);
buffer[length] = '\0';
if(*end == ',') {
end++;
}
*valueList = end;
utLastToken = buffer;
return buffer;
}
/*--------------------------------------------------------------------------------------------------
Parse an array of values.
--------------------------------------------------------------------------------------------------*/
static bool parseArray(
utField field,
uint64 objectNumber,
char *valueList)
{
uint32 numValues, destSize;
uint8 *dest;
char *value, *string, *end;
if(utFieldGetType(field) == UT_CHAR && *valueList == '"') {
string = unmungeString(valueList, &end);
if(end == NULL || *end != '\0') {
return false;
}
numValues = strlen(string) + 1;
dest = utFieldGetAllocValues(field)(objectNumber, numValues);
strcpy((char *)dest, string);
return true;
}
numValues = countListValues(valueList);
if(!utFieldFixedSize(field)) {
dest = utFieldGetAllocValues(field)(objectNumber, numValues);
} else {
dest = utFieldGetGetValues(field)(objectNumber, &destSize);
numValues = utMin(numValues, destSize);
}
while(numValues-- != 0) {
value = parseArrayValue(&valueList);
if(!parseValue(dest, value, field, objectNumber)) {
return false;
}
dest += utFieldGetSize(field);
}
if(*valueList != '\0' && !utFieldFixedSize(field)) {
return false;
}
return true;
}
/*--------------------------------------------------------------------------------------------------
Set a field of an object.
--------------------------------------------------------------------------------------------------*/
static bool setObjectField(
utClass theClass,
utField field,
uint64 objectNumber,
char *value)
{
uint64 numUsed = utFindIntValue(utClassGetNumUsedPtr(theClass), utClassGetReferenceSize(theClass));
uint8 *dest;
bool passed;
if(objectNumber >= numUsed) {
reportError("Invalid object ID");
return false;
}
if(!utFieldArray(field)) {
if(utFieldGetType(field) == UT_BIT) {
dest = *(uint8 **)(utFieldGetArrayPtr(field)) + (objectNumber >> 3)*utFieldGetSize(field);
} else {
dest = *(uint8 **)(utFieldGetArrayPtr(field)) + objectNumber*utFieldGetSize(field);
}
passed = parseValue(dest, value, field, objectNumber);
} else {
passed = parseArray(field, objectNumber, value);
}
if(!passed) {
reportError("Invalid value for field %s", utFieldGetName(field));
}
return passed;
}
/*--------------------------------------------------------------------------------------------------
Set all the fields of an object.
--------------------------------------------------------------------------------------------------*/
static bool setObjectFields(
utClass theClass,
uint64 objectNumber,
char *valueList,
utField *fieldTranslationTable)
{
utField field;
uint16 numValues = countListValues(valueList);
uint16 xField;
char *value;
if(numValues != utClassGetNumFields(theClass) - utClassGetNumHiddenFields(theClass)) {
reportError("Incorrect number of values for class %s", utClassGetName(theClass));
return false;
}
for(xField = 0; xField < numValues; xField++) {
if(fieldTranslationTable == NULL) {
field = utClassGetiField(theClass, xField);
} else {
field = fieldTranslationTable[xField];
}
value = parseArrayValue(&valueList);
if(field != NULL && !setObjectField(theClass, field, objectNumber, value)) {
return false;
}
}
return true;
}
/*--------------------------------------------------------------------------------------------------
Read a line of text from stdin.
--------------------------------------------------------------------------------------------------*/
static bool readLine(void)
{
uint32 linePosition = 0;
int c;
utDo {
c = getc(utInputFile);
} utWhile(c != '\n' && c != EOF) {
if(linePosition == utLineSize) {
utLineSize <<= 1;
utResizeArray(utLineBuffer, utLineSize);
}
if(isprint(c)) {
utLineBuffer[linePosition++] = c;
}
} utRepeat;
if(c == EOF) {
utAtEndOfFile = true;
}
if(linePosition == utLineSize) {
utLineSize <<= 1;
utResizeArray(utLineBuffer, utLineSize);
}
utLineBuffer[linePosition] = '\0';
utLinePosition = 0;
utLineNum++;
return c != EOF;
}
/*--------------------------------------------------------------------------------------------------
Find the length of the token.
--------------------------------------------------------------------------------------------------*/
static uint32 findTokenLength(void)
{
uint32 linePosition = utLinePosition;
char c = utLineBuffer[linePosition++];
if(c == '\0') {
return 0;
}
if(!isalnum((unsigned char)c) && c != '_') {
return 1;
}
do {
c = utLineBuffer[linePosition++];
} while(isalnum((unsigned char)c) || c == '_');
return linePosition - utLinePosition - 1;
}
/*--------------------------------------------------------------------------------------------------
Just skip space.
--------------------------------------------------------------------------------------------------*/
void skipSpace(void)
{
while(isspace((unsigned char)utLineBuffer[utLinePosition])) {
utLinePosition++;
}
}
/*--------------------------------------------------------------------------------------------------
Read a token from the line buffer.
--------------------------------------------------------------------------------------------------*/
char *readToken(void)
{
char *token;
uint32 length;
skipSpace();
length = findTokenLength();
token = utMakeString(length + 1);
strncpy(token, utLineBuffer + utLinePosition, length);
utLinePosition += length;
token[length] = '\0';
utLastToken = token;
return token;
}
/*--------------------------------------------------------------------------------------------------
Find the module with the given prefix.
--------------------------------------------------------------------------------------------------*/
utModule utFindModule(
char *prefix)
{
utModule module;
uint8 xModule;
for(xModule = 0; xModule < utUsedModules; xModule++) {
module = utModules + xModule;
if(!strcmp(utModuleGetPrefix(module), prefix)) {
return module;
}
}
return NULL;
}
/*--------------------------------------------------------------------------------------------------
Find the class with the given name.
--------------------------------------------------------------------------------------------------*/
static utClass findClass(
utModule module,
char *name)
{
utClass theClass;
utForeachModuleClass(module, theClass) {
if(!strcmp(utClassGetName(theClass), name)) {
return theClass;
}
} utEndModuleClass;
return NULL;
}
/*--------------------------------------------------------------------------------------------------
Find the field with the given name.
--------------------------------------------------------------------------------------------------*/
utField utFindField(
utClass theClass,
char *name)
{
utField field;
utForeachClassField(theClass, field) {
if(!strcmp(utFieldGetName(field), name)) {
return field;
}
} utEndClassField;
return NULL;
}
/*--------------------------------------------------------------------------------------------------
Check that we parsed the whole line, and complain if we didn't. Return true if the end was
correct.
--------------------------------------------------------------------------------------------------*/
static bool expectEnd(void)
{
char *token = readToken();
if(*token != '\0') {
reportError("Extra characters at end of command.\n");
return false;
}
return true;
}
/*--------------------------------------------------------------------------------------------------
Parse a module name.
--------------------------------------------------------------------------------------------------*/
static utModule parseModule(
bool required,
bool *error)
{
utModule module;
char *token = readToken();
*error = false;
if(*token == '\0') {
if(required) {
reportError("Expecting a module name.");
*error = true;
}
return NULL;
}
module = utFindModule(token);
if(module == NULL) {
reportError("Invalid module name.");
*error = true;
}
return module;
}
/*--------------------------------------------------------------------------------------------------
Parse a class name.
--------------------------------------------------------------------------------------------------*/
static utClass parseClass(
utModule module,
bool required,
bool *error)
{
utClass theClass;
char *token = readToken();
*error = false;
if(*token == '\0') {
if(required) {
reportError("Expecting a class name.");
*error = true;
}
return NULL;
}
theClass = findClass(module, token);
if(theClass == NULL) {
reportError("Invalid class name.");
*error = true;
}
return theClass;
}
/*--------------------------------------------------------------------------------------------------
Parse an object number.
--------------------------------------------------------------------------------------------------*/
static uint64 parseObjectNumber(
utClass theClass,
bool required,
bool *error)
{
uint64 numUsed = utFindIntValue(utClassGetNumUsedPtr(theClass), utClassGetReferenceSize(theClass));
char *token = readToken();
uint64 objectNumber;
*error = false;
if(*token == '\0') {
if(required) {
reportError("Expecting an object number");
*error = true;
}
return UINT64_MAX;
}
if(!readInteger((uint8 *)&objectNumber, token, 8, true)) {
reportError("Invalid number");
*error = true;
return UINT64_MAX;
}
if(objectNumber >= numUsed) {
reportError("Object number too large");
*error = true;
return UINT64_MAX;
}
return objectNumber;
}
/*--------------------------------------------------------------------------------------------------
Process a list command.
--------------------------------------------------------------------------------------------------*/
static void processListCommand(void)
{
bool error;
utModule module = parseModule(false, &error);
utClass theClass;
if(error) {
return;
}
if(module == NULL) {
listModules();
return;
}
theClass = parseClass(module, false, &error);
if(error) {
return;
}
if(theClass == NULL) {
listClasses(module);
return;
}
if(!expectEnd()) {
return;
}
listFields(theClass);
}
/*--------------------------------------------------------------------------------------------------
Process a show command.
--------------------------------------------------------------------------------------------------*/
static void processShowCommand(void)
{
bool error;
utModule module = parseModule(true, &error);
utClass theClass;
uint64 objectNumber;
if(error) {
return;
}
theClass = parseClass(module, true, &error);
if(error) {
return;
}
objectNumber = parseObjectNumber(theClass, false, &error);
if(error) {
return;
}
if(objectNumber == UINT64_MAX) {
showClass(module, theClass);
return;
}
if(!expectEnd()) {
return;
}
showObject(theClass, objectNumber);
}
/*--------------------------------------------------------------------------------------------------
Process a set command.
"set <module> <class> <object number> = comma separated values – \n"
" set all fields of an object\n"
"set <module> <class> <object number> <field> = <value> – set a field value\n"
--------------------------------------------------------------------------------------------------*/
static void processSetCommand(void)
{
bool error;
utModule module = parseModule(true, &error);
utClass theClass;
utField field;
uint64 objectNumber;
char *token;
if(error) {
return;
}
theClass = parseClass(module, true, &error);
if(error) {
return;
}
objectNumber = parseObjectNumber(theClass, true, &error);
if(error) {
return;
}
token = readToken();
if(!strcmp(token, "=")) {
skipSpace();
setObjectFields(theClass, objectNumber, utLineBuffer + utLinePosition, NULL);
return;
}
field = utFindField(theClass, token);
if(field == NULL) {
reportError("Field not found");
return;
}
token = readToken();
if(strcmp(token, "=")) {
reportError("Expected '='");
return;
}
skipSpace();
setObjectField(theClass, field, objectNumber, utLineBuffer + utLinePosition);
}
/*--------------------------------------------------------------------------------------------------
Process a create command.
--------------------------------------------------------------------------------------------------*/
static void processCreateCommand(void)
{
bool error;
utModule module = parseModule(true, &error);
utClass theClass;
if(error) {
return;
}
theClass = parseClass(module, true, &error);
if(error) {
return;
}
if(utClassGetConstructor(theClass) == NULL) {
reportError("This is a class extension. Call the base class constructor instead");
return;
}
if(!expectEnd()) {
return;
}
fprintf(utOutputFile, "New %s object 0x%llx\n", utClassGetName(theClass), utClassGetConstructor(theClass)());
}
/*--------------------------------------------------------------------------------------------------
Destroy an object.
--------------------------------------------------------------------------------------------------*/
static void processDestroyCommand(void)
{
bool error;
utModule module = parseModule(true, &error);
utClass theClass;
uint64 objectNumber;
if(error) {
return;
}
theClass = parseClass(module, true, &error);
if(error) {
return;
}
objectNumber = parseObjectNumber(theClass, true, &error);
if(error) {
return;
}
if(!expectEnd()) {
return;
}
if(utClassGetDestructor(theClass) == NULL) {
reportError("This class has no destructor, either because it is an extension, or "
"because it is declared 'create_only'");
return;
}
utClassGetDestructor(theClass)(objectNumber);
fprintf(utOutputFile, "Destroyed %s object 0x%llx\n", utClassGetName(theClass), objectNumber);
}
/*--------------------------------------------------------------------------------------------------
Process a show_hidden command.
--------------------------------------------------------------------------------------------------*/
static void processShowHiddenCommand(void)
{
char *token = readToken();
if(!expectEnd()) {
return;
}
if(!strcasecmp(token, "true")) {
utShowHidden = true;
} else if(!strcasecmp(token, "false")) {
utShowHidden = false;
} else {
reportError("Expecting 'true' or 'false'\n");
}
}
/*--------------------------------------------------------------------------------------------------
Process a load text command.
--------------------------------------------------------------------------------------------------*/
static void processLoadTextCommand(void)
{
char *fileName = readToken();
FILE *file;
if(!expectEnd()) {
return;
}
file = fopen(fileName, "r");
if(file == NULL) {
reportError("Could not open file %s for reading", fileName);
return;
}
utLoadTextDatabase(file);
fclose(file);
utMemCheck();
}
/*--------------------------------------------------------------------------------------------------
Process a write command.
--------------------------------------------------------------------------------------------------*/
static void processSaveTextCommand(void)
{
char *fileName = readToken();
FILE *file;
if(!expectEnd()) {
return;
}
file = fopen(fileName, "w");
if(file == NULL) {
reportError("Could not open file %s for writing", fileName);
return;
}
utSaveTextDatabase(file);
fclose(file);
}
/*--------------------------------------------------------------------------------------------------
Process a load binary command.
--------------------------------------------------------------------------------------------------*/
static void processLoadBinaryCommand(void)
{
char *fileName = readToken();
FILE *file;
if(!expectEnd()) {
return;
}
file = fopen(fileName, "rb");
if(file == NULL) {
reportError("Could not open file %s for reading", fileName);
return;
}
utLoadBinaryDatabase(file);
fclose(file);
}
/*--------------------------------------------------------------------------------------------------
Process a save binary command.
--------------------------------------------------------------------------------------------------*/
static void processSaveBinaryCommand(void)
{
char *fileName = readToken();
FILE *file;
if(!expectEnd()) {
return;
}
file = fopen(fileName, "wb");
if(file == NULL) {
reportError("Could not open file %s for writing", fileName);
return;
}
utSaveBinaryDatabase(file);
fclose(file);
}
/*--------------------------------------------------------------------------------------------------
Process the command in the line buffer.
--------------------------------------------------------------------------------------------------*/
static bool processCommand(void)
{
char *token = readToken();
if(*token == '\0') {
return true; /* Empty line */
}
if(!strcmp(token, "create")) {
processCreateCommand();
} else if(!strcmp(token, "compact")) {
if(utPersistenceInitialized) {
utCompactDatabase();
} else {
reportError("Compact is only for persitent databases.");
}
} else if(!strcmp(token, "destroy")) {
processDestroyCommand();
} else if(!strcmp(token, "help")) {
printHelp();
return true;
} else if(!strcmp(token, "list")) {
processListCommand();
} else if(!strcmp(token, "quit")) {
return false;
} else if(!strcmp(token, "set")) {
processSetCommand();
} else if(!strcmp(token, "show")) {
processShowCommand();
} else if(!strcmp(token, "show_hidden")) {
processShowHiddenCommand();
} else if(!strcmp(token, "load_text")) {
processLoadTextCommand();
} else if(!strcmp(token, "save_text")) {
processSaveTextCommand();
} else if(!strcmp(token, "load_binary")) {
processLoadBinaryCommand();
} else if(!strcmp(token, "save_binary")) {
processSaveBinaryCommand();
} else {
fprintf(utOutputFile, "Invalid command. Type 'help' for a list of commands\n");
}
return true;
}
/*--------------------------------------------------------------------------------------------------
Initialize the database manager.
--------------------------------------------------------------------------------------------------*/
void utDatabaseManagerStart(void)
{
utLineSize = 42;
utLineBuffer = utNewA(char, utLineSize);
utOutputFile = stdout;
utInputFile = stdin;
}
/*--------------------------------------------------------------------------------------------------
Clean up after the database manager.
--------------------------------------------------------------------------------------------------*/
void utDatabaseManagerStop(void)
{
utFree(utLineBuffer);
}
/*--------------------------------------------------------------------------------------------------
Interpret commands to manage the database.
--------------------------------------------------------------------------------------------------*/
void utManager(void)
{
utAtEndOfFile = false;
fprintf(utOutputFile, "For help, enter the 'help' command\n");
do {
fprintf(utOutputFile, "> ");
readLine();
} while(processCommand());
}
/*--------------------------------------------------------------------------------------------------
Skip blank lines.
--------------------------------------------------------------------------------------------------*/
static void skipBlankLines(void)
{
while(readLine()) {
if(utLineBuffer[0] != '\0') {
return;
}
}
}
/*--------------------------------------------------------------------------------------------------
Read a module header.
--------------------------------------------------------------------------------------------------*/
static utModule readModuleHeader(void)
{
utModule module;
char *token;
token = readToken();
if(strcmp(token, "module")) {
if(!utAtEndOfFile) {
reportError("Expected 'module' keyword");
}
return NULL;
}
token = readToken();
if(token == NULL) {
reportError("Expected module name");
return NULL;
}
module = utFindModule(token);
if(module == NULL) {
reportError("Module %s not found", token);
return NULL;
}
if(!expectEnd()) {
return NULL;
}
return module;
}
/*--------------------------------------------------------------------------------------------------
Reallocate object fields for the class.
--------------------------------------------------------------------------------------------------*/
static void reallocObjects(
utClass theClass,
uint64 numUsed)
{
utField field;
utSetInteger(utClassGetNumAllocatedPtr(theClass), numUsed, utClassGetReferenceSize(theClass));
utForeachClassField(theClass, field) {
if(!utFieldArray(field)) {
*(uint8 **)(utFieldGetArrayPtr(field)) = utRealloc(*(uint8 **)utFieldGetArrayPtr(field), numUsed,
utFieldGetSize(field));
} else if(utFieldFixedSize(field)) {
*(uint8 **)(utFieldGetArrayPtr(field)) = utRealloc(*(uint8 **)utFieldGetArrayPtr(field),
numUsed*utFieldGetLength(field), utFieldGetSize(field));
}
} utEndClassField;
}
/*--------------------------------------------------------------------------------------------------
Read a class header, and allocate space for the objects.
--------------------------------------------------------------------------------------------------*/
static utClass readClassHeader(
utModule module)
{
utClass theClass;
char *token;
uint64 numUsed;
skipBlankLines();
token = readToken();
if(strcmp(token, "class")) {
utLinePosition = 0;
return NULL;
}
token = readToken();
if(token == NULL) {
reportError("Expected class name");
return NULL;
}
theClass = findClass(module, token);
if(theClass == NULL) {
reportError("Class %s not found", token);
return NULL;
}
token = readToken();
if(!readInteger(utClassGetNumUsedPtr(theClass), token, utClassGetReferenceSize(theClass), true)) {
reportError("Expected number of objects");
return NULL;
}
if(!expectEnd()) {
return NULL;
}
numUsed = utFindIntValue(utClassGetNumUsedPtr(theClass), utClassGetReferenceSize(theClass));
numUsed = utMax(2, numUsed);
reallocObjects(theClass, numUsed);
return theClass;
}
/*--------------------------------------------------------------------------------------------------
Read in column headers. Build a translation table from column number to field. The caller
will have to free utFieldTranslationTable.
--------------------------------------------------------------------------------------------------*/
static bool readColumnHeaders(
utClass theClass)
{
utField field;
char *valueList, *value;
uint32 numFields, xField;
if(!readLine()) {
reportError("Expected column headers");
return false;
}
valueList = utLineBuffer;
numFields = countListValues(valueList) - 1;
utFieldTranslationTable = calloc(numFields, sizeof(utField));
value = parseArrayValue(&valueList);
if(value == NULL || strcmp(value, "ObjectNumber")) {
reportError("Expected 'objectNumber' to be the first field");
return false;
}
for(xField = 0; xField < numFields; xField++) {
value = parseArrayValue(&valueList);
if(value == NULL) {
return false;
}
field = utFindField(theClass, value);
if(field == NULL) {
reportError("Unknown field %s in class %s -- dropping", value, utClassGetName(theClass));
}
utFieldTranslationTable[xField] = field;
}
utNumFields = numFields;
return true;
}
/*--------------------------------------------------------------------------------------------------
Read in column types. If it is incompatible with the field, print a warning, and null out the
entry in the translation table.
--------------------------------------------------------------------------------------------------*/
static bool readColumnTypes(
utClass theClass)
{
char *valueList, *value;
uint32 numFields, xField;
if(!readLine()) {
reportError("Expected column types");
return false;
}
valueList = utLineBuffer;
numFields = countListValues(valueList) - 1;
if(numFields != utNumFields) {
reportError("Column number mismatch");
return false;
}
value = parseArrayValue(&valueList);
if(value == NULL) {
reportError("Expected class reference type to be the first field");
return false;
}
for(xField = 0; xField < numFields; xField++) {
value = parseArrayValue(&valueList);
if(value == NULL) {
return false;
}
}
return true;
}
/*--------------------------------------------------------------------------------------------------
Add the objects between the start and stop to the free list.
--------------------------------------------------------------------------------------------------*/
static void addObjectsToFreeList(
utModule module,
utClass theClass,
uint32 start,
uint32 stop)
{
utField field = utClassGetNextFreeField(theClass);
uint64 firstFree = utFindIntValue(utClassGetFirstFreePtr(theClass), utClassGetReferenceSize(theClass));
uint64 xObject;
uint8 *dest;
for(xObject = start + 1; xObject < stop; xObject++) {
firstFree = utFindIntValue(utClassGetFirstFreePtr(theClass), utClassGetReferenceSize(theClass));
dest = *(uint8 **)(utFieldGetArrayPtr(field)) + xObject*utFieldGetSize(field);
utSetInteger(dest, firstFree, utClassGetReferenceSize(theClass));
utSetInteger(utClassGetFirstFreePtr(theClass), xObject, utClassGetReferenceSize(theClass));
}
}
/*--------------------------------------------------------------------------------------------------
Read in a table of objects. A blank line ends the table.
--------------------------------------------------------------------------------------------------*/
static bool readClassTable(
utModule module,
utClass theClass)
{
char *valueList;
uint32 numValues;
uint64 objectNumber, lastObjectNumber = 0;
uint64 numUsed;
bool error;
utDo {
if(!readLine()) {
return false;
}
skipSpace();
} utWhile(utLineBuffer[utLinePosition] != '\0') {
objectNumber = parseObjectNumber(theClass, true, &error);
if(error) {
return false;
}
if(strcmp(readToken(), ",")) {
reportError("Expecting a ','");
}
valueList = utLineBuffer + utLinePosition;
numValues = countListValues(valueList);
if(numValues != utNumFields) {
reportError("Mismatched number of fields");
return false;
}
if(!setObjectFields(theClass, objectNumber, valueList, utFieldTranslationTable)) {
return false;
}
if(utClassGetDestructor(theClass) != NULL) {
addObjectsToFreeList(module, theClass, lastObjectNumber, objectNumber);
}
lastObjectNumber = objectNumber;
} utRepeat;
if(utClassGetDestructor(theClass) != NULL) {
numUsed = utFindIntValue(utClassGetNumUsedPtr(theClass), utClassGetReferenceSize(theClass));
addObjectsToFreeList(module, theClass, lastObjectNumber, numUsed);
}
return true;
}
/*--------------------------------------------------------------------------------------------------
Read in the module.
--------------------------------------------------------------------------------------------------*/
static bool readModule(
utModule module)
{
utClass theClass;
bool passed;
utDo {
theClass = readClassHeader(module);
} utWhile(theClass != NULL) {
if(!readColumnHeaders(theClass)) {
return false;
}
if(!readColumnTypes(theClass)) {
return false;
}
passed = readClassTable(module, theClass);
free(utFieldTranslationTable);
if(!passed) {
return false;
}
} utRepeat;
return true;
}
/*--------------------------------------------------------------------------------------------------
Read in the database in ASCII.
--------------------------------------------------------------------------------------------------*/
void utLoadTextDatabase(
FILE *file)
{
char *fileName = NULL;
utModule module;
FILE *savedFile = utInputFile;
bool useDefault = file == NULL;
if(useDefault) {
fileName = utSprintf("%s%cdatabase", utDatabaseDirectory, UTDIRSEP);
file = fopen(fileName, "r");
}
if(file == NULL) {
utExit("Could not read from %s", fileName);
}
utResetDatabase();
utAtEndOfFile = false;
utLineNum = 0;
utInputFile = file;
skipBlankLines();
do {
module = readModuleHeader();
} while(module != NULL && readModule(module));
utInputFile = savedFile;
if(useDefault) {
fclose(file);
}
}
/*--------------------------------------------------------------------------------------------------
Write out the database in ASCII. Don't write the utility module.
--------------------------------------------------------------------------------------------------*/
void utSaveTextDatabase(
FILE *file)
{
char *fileName = NULL;
utModule module;
utClass theClass;
FILE *savedFile = utOutputFile;
bool useDefault = file == NULL;
if(useDefault) {
fileName = utSprintf("%s%cdatabase", utDatabaseDirectory, UTDIRSEP);
file = fopen(fileName, "w");
}
if(file == NULL) {
utExit("Could not write to %s", fileName);
}
utOutputFile = file;
utForeachModule(module) {
if(strcmp(utModuleGetPrefix(module), "ut") && utModuleInitialized(module) &&
(utModulePersistent(module) || !utPersistenceInitialized) &&
utModuleSaved(module)) {
fprintf(utOutputFile, "module %s\n\n", utModuleGetPrefix(module));
utForeachModuleClass(module, theClass) {
showClass(module, theClass);
fprintf(utOutputFile, "\n");
} utEndModuleClass;
}
} utEndModule;
if(useDefault) {
fclose(file);
}
utOutputFile = savedFile;
}
/*--------------------------------------------------------------------------------------------------
Write out the fields of an object.
--------------------------------------------------------------------------------------------------*/
void utDatabaseShowObject(
char *modulePrefix,
char *className,
uint64 objectNumber)
{
utModule module = utFindModule(modulePrefix);
utClass theClass;
uint64 numUsed;
FILE *savedFile = utOutputFile;
if(module == NULL) {
reportError("Invalid module %s\n", modulePrefix);
return;
}
theClass = findClass(module, className);
if(theClass == NULL) {
reportError("Invalid class %s\n", className);
return;
}
utOutputFile = stdout;
showObject(theClass, objectNumber);
utOutputFile = savedFile;
numUsed = utFindIntValue(utClassGetNumUsedPtr(theClass), utClassGetReferenceSize(theClass));
if(objectNumber >= numUsed) {
reportError("Object number too large");
return;
}
}