[go: up one dir, main page]

Menu

[r999]: / trunk / VTS3 / VTSValue.cpp  Maximize  Restore  History

Download this file

2072 lines (1664 with data), 50.6 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
// VTSValue.cpp: implementation of the VTSValue class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "vts.h"
#include "VTSValue.h"
#include <memory.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// VTSObjPropValueList::VTSObjPropValueList
//
VTSObjPropValueList::VTSObjPropValueList( VTSDocPtr doc, objId listID )
: objPropDoc(doc), objPropListID(listID), objPropBusy(false)
{
// load the list
doc->m_pDB->pListMgr->GetList( objPropList, listID );
}
//
// VTSObjPropValueList~VTSObjPropValueList
//
VTSObjPropValueList::~VTSObjPropValueList( void )
{
}
//
// VTSObjPropValueList::Length
//
int VTSObjPropValueList::Length( void )
{
return objPropList.Length();
}
//
// VTSObjPropValueList::InsertElem
//
void VTSObjPropValueList::InsertElem( int indx, VTSObjPropValue &val )
{
objPropList.NewElem( &indx, &val );
// if inserting before the current scan element, it has moved
if (objPropScanIndx >= indx)
objPropScanIndx += 1;
}
//
// VTSObjPropValueList::RemoveElem
//
void VTSObjPropValueList::RemoveElem( int indx )
{
objPropList.DeleteElem( indx );
// if deleting the current scan element, it has 'moved'
if (objPropScanIndx >= indx)
objPropScanIndx -= 1;
}
//
// VTSObjPropValueList::ReadElem
//
void VTSObjPropValueList::ReadElem( int indx, VTSObjPropValue& val )
{
objPropList.ReadElem( indx, &val );
}
//
// VTSObjPropValueList::WriteElem
//
void VTSObjPropValueList::WriteElem( int indx, const VTSObjPropValue& val )
{
objPropList.WriteElem( indx, (void *)&val );
}
//
// VTSObjPropValueList::ReadComponent
//
void VTSObjPropValueList::ReadComponent( int indx, VTSObjPropValueElemDesc& desc )
{
// make sure it's not out of bounds
ASSERT( (indx >= 0) && (indx < objPropList.Length()) );
// read the current contents
ReadElem( indx, desc.descValue );
// if there's an associated object, read it
if (desc.descValue.valueElemID)
objPropDoc->m_pDB->pObjMgr->ReadObject( desc.descValue.valueElemID, (JDBObjPtr)&desc.descElement );
else
memset( &desc.descElement, 0, sizeof(desc.descElement) );
}
//
// VTSObjPropValueList::WriteComponent
//
void VTSObjPropValueList::WriteComponent( int indx, VTSObjPropValueElemDesc& desc )
{
// make sure it's not out of bounds
ASSERT( (indx >= 0) && (indx < objPropList.Length()) );
// if there is an element ID but the new content length is zero, delete the object
if (desc.descValue.valueElemID && (desc.descElement.elemLen == 0)) {
// delete the object
objPropDoc->m_pDB->pObjMgr->DeleteObject( desc.descValue.valueElemID );
// no more object
desc.descValue.valueElemID = 0;
}
// calculate the object size correctly
int objSize = kVTSValueElemHeaderLength + desc.descElement.elemLen; // expected size
objSize += (objSize & 1); // must be even
// make sure the element object size is correct
desc.descElement.objSize = objSize;
// if there is no element ID but the new content length is non-zero, create a new object
if (!desc.descValue.valueElemID && (desc.descElement.elemLen != 0)) {
// allocate a new object
objPropDoc->m_pDB->pObjMgr->NewObject( &desc.descValue.valueElemID, (JDBObjPtr)&desc.descElement, objSize );
// initialize the element object header
desc.descElement.objID = (unsigned char)(desc.descValue.valueElemID & 0xFF);
desc.descElement.objType = 0;
}
// write the current contents
WriteElem( indx, desc.descValue );
// if there's an associated object, write it
if (desc.descValue.valueElemID)
objPropDoc->m_pDB->pObjMgr->WriteObject( desc.descValue.valueElemID, (JDBObjPtr)&desc.descElement );
}
//
// VTSObjPropValueList::InsertComponent
//
void VTSObjPropValueList::InsertComponent( int indx, VTSObjPropValueElemDesc& desc )
{
// calculate the object size correctly
int objSize = kVTSValueElemHeaderLength + desc.descElement.elemLen; // expected size
objSize += (objSize & 1); // must be even
// if content length is non zero, create and save an object
if (desc.descElement.elemLen != 0) {
// allocate a new object
objPropDoc->m_pDB->pObjMgr->NewObject( &desc.descValue.valueElemID, (JDBObjPtr)&desc.descElement, objSize );
// save it
objPropDoc->m_pDB->pObjMgr->WriteObject( desc.descValue.valueElemID, (JDBObjPtr)&desc.descElement );
} else
desc.descValue.valueElemID = 0;
// insert the value
InsertElem( indx, desc.descValue );
}
//
// VTSObjPropValueList::ScanList
//
// Most of the helper functions involve scanning the list for specific elements. If the helper
// wants to modify the element and write it back, that's simple to support. If the helper thinks
// the current location is the correct spot to add a new element, it should be able to call
// InsertElem() and not interfere with the rest of the scanning of the list (if that's desireable).
//
// If the helper function returns true, the scanning process will continue. If it returns false,
// ScanList will return the index of the element that was 'found'. If the helper never returns
// false, ScanList will return -1 as an indication that all of the elements (and there may be none)
// were scanned.
//
int VTSObjPropValueList::ScanList( VTSObjPropValueProc fp, int startIndx )
{
VTSObjPropValue val
;
objPropScanIndx = startIndx;
// read in the elements and pass them along
while (objPropScanIndx < objPropList.Length()) {
objPropList.ReadElem( objPropScanIndx, &val );
if (!(*fp)( objPropScanIndx, val ))
return objPropScanIndx;
// move on to the next element
objPropScanIndx += 1;
}
// end of list reached
return -1;
}
//
// VTSObjPropValueList::Sort
//
int VTSObjPropValueListSortHelper( VTSObjPropValuePtr ap, VTSObjPropValuePtr bp );
void VTSObjPropValueList::Sort( void )
{
objPropList.Sort( (JDBListCompFnPtr)VTSObjPropValueListSortHelper );
}
int VTSObjPropValueListSortHelper( VTSObjPropValuePtr ap, VTSObjPropValuePtr bp )
{
if (ap->valueObjID < bp->valueObjID) return -1;
if (ap->valueObjID > bp->valueObjID) return 1;
if (ap->valueProperty < bp->valueProperty) return -1;
if (ap->valueProperty > bp->valueProperty) return 1;
if (ap->valueIndx < bp->valueIndx) return -1;
if (ap->valueIndx > bp->valueIndx) return 1;
if (ap->valueComponent < bp->valueComponent) return -1;
if (ap->valueComponent > bp->valueComponent) return 1;
// should never get here!
return 0;
}
//
// VTSObjPropValueList::SetBusy
//
void VTSObjPropValueList::SetBusy( void )
{
CSingleLock lock( &objPropLock )
;
// make sure no other threads are mucking around
lock.Lock();
// user is editing list
objPropBusy = true;
// be nice and release it before returning
lock.Unlock();
}
//
// VTSObjPropValueList::ResetBusy
//
void VTSObjPropValueList::ResetBusy( void )
{
CSingleLock lock( &objPropLock )
;
// make sure no other threads are mucking around
lock.Lock();
// user has finished editing
objPropBusy = false;
// be nice and release it before returning
lock.Unlock();
}
//
// VTSObjPropValueList::Encode
//
void VTSObjPropValueList::Encode( unsigned int objID, int propID, int indx, BACnetAPDUEncoder& enc )
{
CSingleLock lock( &objPropLock )
;
int i, elemCount
;
VTSObjPropValue val
;
VTSObjPropValueElemDesc desc
;
// make sure no other threads are mucking around
lock.Lock();
// if the user is editing, toss out "device configuration in progress" error
if (objPropBusy)
throw (2);
// look for the start of the obj/prop/indx
for (i = 0; i < objPropList.Length(); i++) {
objPropList.ReadElem( i, &val );
if ((val.valueObjID == objID) && (val.valueProperty == propID))
break;
}
// check to see if we found something
if ((i >= objPropList.Length()) || (val.valueObjID != objID))
throw (31); // unknown-object
if (val.valueProperty != propID)
throw (32); // unknown-property
// case 1: looking for a value, got a value
if (indx == -1) {
// looking for the whole thing
if (val.valueIndx == -1) {
// got a simple value, return all of it
} else
if (val.valueIndx == 0) {
// got an array, skip over the header, return the whole thing
i += 1;
} else
throw (25); // operational-problem, database configuration error
// copy the whole thing
for (; i < objPropList.Length(); i++) {
ReadComponent( i, desc );
if ((val.valueObjID != objID) || (val.valueProperty != propID))
break;
enc.Append( desc.descElement.elemContent, desc.descElement.elemLen );
}
} else
if (indx == 0) {
// looking for the length of an array
if (val.valueIndx == -1)
throw (7); // inconsistent-parameters, can't index a simple value
else
if (val.valueIndx != 0)
throw (25); // operational-problem, database configuration error
// got an array, skip the marker
i += 1;
// count the number of elements
elemCount = 0;
for (; i < objPropList.Length(); i++) {
objPropList.ReadElem( i, &val );
if ((val.valueObjID != objID) || (val.valueProperty != propID))
break;
if (val.valueIndx > elemCount)
elemCount += 1;
}
// encode the count
BACnetUnsigned( elemCount ).Encode( enc );
} else {
// looking for the length of an array
if (val.valueIndx == -1)
throw (7); // inconsistent-parameters, can't index a simple value
else
if (val.valueIndx == 0) {
// skip the marker
i += 1;
// scan for a specific index
int found = 0;
for (; i < objPropList.Length(); i++) {
objPropList.ReadElem( i, &val );
if ((val.valueObjID != objID) || (val.valueProperty != propID) || (val.valueIndx > indx))
break;
if (val.valueIndx == indx) {
found = 1;
break;
}
}
// make sure we've got something
if (!found)
throw (42); // invalid-array-index
// copy the whole thing
for (; i < objPropList.Length(); i++) {
ReadComponent( i, desc );
if ((val.valueObjID != objID) || (val.valueProperty != propID) || (val.valueIndx != indx))
break;
enc.Append( desc.descElement.elemContent, desc.descElement.elemLen );
}
} else
throw (25); // operational-problem, database configuration error
}
// be nice and release it before returning
lock.Unlock();
}
//
// VTSObjPropValueList::Decode
//
void VTSObjPropValueList::Decode( unsigned int objID, int propID, int indx, BACnetAPDUDecoder& dec )
{
CSingleLock lock( &objPropLock )
;
int i, fini = 0, nest = 0, elemCount
;
VTSObjPropValue val
;
VTSObjPropValueElemDesc desc
;
// make sure no other threads are mucking around
lock.Lock();
// if the user is editing, toss out "device configuration in progress" error
if (objPropBusy)
throw (2);
// look for the start of the obj/prop/indx
for (i = 0; i < objPropList.Length(); i++) {
objPropList.ReadElem( i, &val );
if ((val.valueObjID == objID) && (val.valueProperty == propID))
break;
}
// check to see if we found something
if ((i >= objPropList.Length()) || (val.valueObjID != objID))
throw (31); // unknown-object
if (val.valueProperty != propID)
throw (32); // unknown-property
// case 1: looking for a value, got a value
if (indx == -1) {
// looking for the whole thing
if (val.valueIndx != -1)
throw (25); // operational-problem, can't write whole array
// remove the existing content
while (i < objPropList.Length()) {
objPropList.ReadElem( i, &val );
if ((val.valueObjID == objID) && (val.valueProperty == propID))
objPropList.DeleteElem( i );
else
break;
}
} else
if (indx == 0) {
// looking for the length of an array
if (val.valueIndx == -1)
throw (7); // inconsistent-parameters, can't index a simple value
else
if (val.valueIndx != 0)
throw (25); // operational-problem, database configuration error
// got an array, change the size
BACnetUnsigned newLen;
newLen.Decode( dec );
// check for valid value
if (newLen.uintValue < 0)
throw (42); // invalid-array-index
// skip over existing content that will remain
elemCount = 0;
while (i < objPropList.Length()) {
objPropList.ReadElem( i, &val );
if ((val.valueObjID != objID) || (val.valueProperty != propID) || (val.valueIndx > (int)newLen.uintValue))
break;
if (val.valueIndx > elemCount)
elemCount += 1;
i += 1;
}
if (elemCount > (int)newLen.uintValue)
throw (25); // operational-problem, database configuration error
if (elemCount == (int)newLen.uintValue) {
// remove the rest of the content
while (i < objPropList.Length()) {
objPropList.ReadElem( i, &val );
if ((val.valueObjID == objID) && (val.valueProperty == propID))
objPropList.DeleteElem( i );
else
break;
}
} else {
// add more elements to fill out the array
elemCount += 1;
// the major section will be the same
desc.descValue.valueObjID = objID;
desc.descValue.valueProperty = propID; // property identifier
desc.descValue.valueComponent = 0; // only one component
desc.descValue.valueType = 0; // null
desc.descValue.valueContext = -1; // application encoded
desc.descElement.elemLen = 1; // one octet
desc.descElement.elemContent[0] = 0x00; // application encoded null
// write them out
while (elemCount <= (int)newLen.uintValue) {
// set the index
desc.descValue.valueIndx = elemCount++;
// save what we just built
InsertComponent( i++, desc );
}
// dont try to put in more content
fini = 1;
}
} else {
// looking for a specific element
if (val.valueIndx == -1)
throw (7); // inconsistent-parameters, can't index a simple value
else
if (val.valueIndx == 0) {
// skip the marker
i += 1;
// scan for a specific index
int found = 0;
for (; i < objPropList.Length(); i++) {
objPropList.ReadElem( i, &val );
if ((val.valueObjID != objID) || (val.valueProperty != propID) || (val.valueIndx > indx))
break;
if (val.valueIndx == indx) {
found = 1;
break;
}
}
// make sure we've got something
if (!found)
throw (42); // invalid-array-index
// remove the existing content
while (i < objPropList.Length()) {
objPropList.ReadElem( i, &val );
if ((val.valueObjID == objID) && (val.valueProperty == propID) && (val.valueIndx == indx))
objPropList.DeleteElem( i );
else
break;
}
} else
throw (25); // operational-problem, database configuration error
}
// put in the new stuff
BACnetAPDUTag t
;
// the major section will be the same
desc.descValue.valueObjID = objID;
desc.descValue.valueProperty = propID; // property identifier
desc.descValue.valueIndx = indx; // element index
// first piece will be component 0
desc.descValue.valueComponent = 0;
// scan through the rest of the data
while (!fini && (dec.pktLength > 0)) {
try {
// get a peek at the tag
dec.ExamineTag( t );
switch (t.tagClass) {
case contextTagClass:
// set this as an octet string. Because it is context tagged, the datatype connot
// be determined, the context isn't maintained. It could be, because we know the
// object type and property, and we could even validate the datatype along the
// way, but that will be saved for future work.
desc.descValue.valueType = octetStringAppTag;
desc.descValue.valueContext = (int)t.tagNumber;
break;
case openingTagClass:
// nested
nest++;
desc.descValue.valueType = 13; // opening tag
desc.descValue.valueContext = (int)t.tagNumber;
break;
case closingTagClass:
// not nested so deep
if (!nest--) {
fini = 1;
break;
}
desc.descValue.valueType = 14; // closing tag
desc.descValue.valueContext = (int)t.tagNumber;
break;
case applicationTagClass:
desc.descValue.valueType = t.tagNumber; // tag number matches type
desc.descValue.valueContext = -1;
break;
}
}
catch (...) {
// decoding error
break;
}
// done yet?
if (fini)
break;
// copy the tag and data to the element
desc.descElement.elemLen = dec.ExtractTagData( desc.descElement.elemContent );
// save what we just built
InsertComponent( i++, desc );
// get ready for the next component
desc.descValue.valueComponent += 1;
}
// be nice and release it before returning
lock.Unlock();
}
//
// VTSObjPropValueList::DescribeValue
//
// This member function builds a description of a value, even when there is more than
// one component. To keep the strings short there are no spaces included, and only a comma ',' is
// used to separate elements.
//
CString VTSObjPropValueList::DescribeValue( int indx )
{
VTSObjPropValueElemDesc first, cur
;
CString rslt
;
bool needsComma
;
// read the first component
ReadComponent( indx, first );
// check for an array
if (first.descValue.valueIndx == 0)
return _T("[array]");
// turn it into a description
rslt = DescribeComponent( first );
needsComma = (first.descValue.valueType != 13); // openTag
// read the rest of the components and add them
for (;;) {
indx += 1;
if (indx >= objPropList.Length())
break;
// read the component
ReadComponent( indx, cur );
// make sure it's part of the same value
if (cur.descValue.valueObjID != first.descValue.valueObjID) break;
if (cur.descValue.valueProperty != first.descValue.valueProperty) break;
if (cur.descValue.valueIndx != first.descValue.valueIndx) break;
// get a description of it
CString comp = DescribeComponent( cur );
// figure out the length
int rsltLen = rslt.GetLength();
int compLen = comp.GetLength();
// get a buffer big enough to hold the results
LPTSTR p = rslt.GetBuffer( rsltLen + compLen + 3 );
// comma seaparated list of values
if (needsComma && (cur.descValue.valueType != 14)) // closeTag
lstrcat( p, _T(",") );
// append the description
lstrcat( p, comp );
// release the buffer
rslt.ReleaseBuffer();
needsComma = (cur.descValue.valueType != 13); // openTag
}
// all done
return rslt;
}
//
// VTSObjPropValueList::DescribeComponent
//
// This member function builds a description of a component. Opening tags are encoded as '(', closing
// tags as ')', and all the rest of the data types are encoded as you would expect.
//
CString VTSObjPropValueList::DescribeComponent( const VTSObjPropValueElemDesc& desc )
{
CString rslt
;
BACnetAPDUDecoder dec( desc.descElement.elemContent, desc.descElement.elemLen )
;
// depending on the type, decode the value
switch (desc.descValue.valueType) {
case 0: {
rslt = _T( "null" );
break;
}
case 1: {
BACnetBoolean m_Boolean
;
m_Boolean.Decode( dec );
m_Boolean.Encode( rslt.GetBuffer(16) );
break;
}
case 2: {
BACnetUnsigned m_Unsigned
;
m_Unsigned.Decode( dec );
m_Unsigned.Encode( rslt.GetBuffer(16) );
break;
}
case 3: {
BACnetInteger m_Integer
;
m_Integer.Decode( dec );
m_Integer.Encode( rslt.GetBuffer(16) );
break;
}
case 4: {
BACnetReal m_Real
;
m_Real.Decode( dec );
m_Real.Encode( rslt.GetBuffer(16) );
break;
}
case 5: {
BACnetDouble m_Double
;
m_Double.Decode( dec );
m_Double.Encode( rslt.GetBuffer(16) );
break;
}
case 6: {
BACnetOctetString m_OctetStr
;
m_OctetStr.Decode( dec );
m_OctetStr.Encode( rslt.GetBuffer(1024) );
break;
}
case 7: {
BACnetCharacterString m_CharStr
;
m_CharStr.Decode( dec );
m_CharStr.Encode( rslt.GetBuffer(1024) );
break;
}
case 8: {
BACnetBitString m_BitStr
;
m_BitStr.Decode( dec );
m_BitStr.Encode( rslt.GetBuffer(16) );
break;
}
case 9: {
BACnetEnumerated m_Enumerated
;
m_Enumerated.Decode( dec );
m_Enumerated.Encode( rslt.GetBuffer(16) );
break;
}
case 10: {
BACnetDate m_Date
;
m_Date.Decode( dec );
m_Date.Encode( rslt.GetBuffer(16) );
break;
}
case 11: {
BACnetTime m_Time
;
m_Time.Decode( dec );
m_Time.Encode( rslt.GetBuffer(16) );
break;
}
case 12: {
BACnetObjectIdentifier m_ObjID
;
m_ObjID.Decode( dec );
m_ObjID.Encode( rslt.GetBuffer(16) );
break;
}
case 13: {
rslt = _T( "(" );
break;
}
case 14: {
rslt = _T( ")" );
break;
}
}
// release the buffer
rslt.ReleaseBuffer( );
// all done
return rslt;
}
//
// VTSObjectListContext
//
// The VTSObjectList member functions need a context for doing their work, and this is a context
// control class. Note that all of the members are private, which should reduce namespace
// polution and keep the number of global variables to a minimum. Since C++ is not a block
// structure language like Algol or Pascal, this information needs to be stored in a separate
// object.
//
class VTSObjectListContext {
friend class VTSObjectList;
VTSObjectListPtr contextObjListPtr;
objId contextObjID;
objId contextOldID;
objId contextNewID;
static bool CtorHelper( int indx, VTSObjPropValue &val );
static bool RemoveHelper( int indx, VTSObjPropValue &val );
static bool MoveHelper( int indx, VTSObjPropValue &val );
};
VTSObjectListContext gVTSObjectListContext;
//
// VTSObjectList::VTSObjectList
//
// This object list looks like just a list of the unique BACnetObjectIdentifiers in the list. In
// fact it maintains a shadow as if that was the case.
//
VTSObjectList::VTSObjectList( VTSObjPropValueListPtr lp )
: objOPVL(lp)
{
// set up the context
gVTSObjectListContext.contextObjListPtr = this;
gVTSObjectListContext.contextObjID = -1;
// scan the list looking for unique ID's
objOPVL->ScanList( VTSObjectListContext::CtorHelper );
}
//
// VTSObjectListContext::CtorHelper
//
// If a new ID is found, add it to the end of the list, set the context so the new one
// isn't added more than once, and continue searching.
//
bool VTSObjectListContext::CtorHelper( int indx, VTSObjPropValue &val )
{
if (val.valueObjID != gVTSObjectListContext.contextObjID) {
// add this to the list we are building
gVTSObjectListContext.contextObjListPtr->objIDList.AddTail( val.valueObjID );
// dont add this one again
gVTSObjectListContext.contextObjID = val.valueObjID;
}
// keep searching
return true;
}
//
// VTSObjectList::~VTSObjectList
//
VTSObjectList::~VTSObjectList( void )
{
}
//
// VTSObjectList::Add
//
void VTSObjectList::Add( void )
{
int len = objOPVL->Length()
;
VTSObjPropValue val
;
// first element?
if (len == 0)
val.valueObjID = 0;
else {
objOPVL->ReadElem( len - 1, val );
val.valueObjID += 1;
}
// initialize the rest of the fields
val.valueProperty = 75; // object-identifier
val.valueIndx = -1; // not an array, just a value
val.valueComponent = 0; // only one component
val.valueType = 0; // null
val.valueContext = -1; // application encoded
val.valueElemID = 0; // no more data
// add it to the list
objOPVL->InsertElem( len, val );
// add it to the shadow list
objIDList.AddTail( val.valueObjID );
}
//
// VTSObjectList::Remove
//
void VTSObjectList::Remove( int indx )
{
// set up the context, get the ID
gVTSObjectListContext.contextObjListPtr = this;
gVTSObjectListContext.contextObjID = operator []( indx );
// remove it from the shadow list
objIDList.RemoveAt(objIDList.FindIndex(indx));
// scan through the list and remove them all
objOPVL->ScanList( VTSObjectListContext::RemoveHelper );
}
//
// VTSObjectListContext::RemoveHelper
//
// Remove the record if the ID's match.
//
bool VTSObjectListContext::RemoveHelper( int indx, VTSObjPropValue &val )
{
if (val.valueObjID == gVTSObjectListContext.contextObjID) {
// remove it from the list
gVTSObjectListContext.contextObjListPtr->objOPVL->RemoveElem( indx );
}
// keep searching
return true;
}
//
// VTSObjectList::Exists
//
// True iff object ID already exists. This is helpful when the object ID is changing,
// the user is told that the object already exists.
//
bool VTSObjectList::Exists( unsigned int objID )
{
POSITION pos = objIDList.Find( objID )
;
// if pos is NULL the value was not found
return (pos != NULL);
}
//
// VTSObjectList::Move
//
void VTSObjectList::Move( unsigned int oldID, unsigned int newID )
{
POSITION pos = objIDList.Find( oldID )
;
// if the old one isn't found this should probably throw something
if (pos == NULL)
return;
// change the value
objIDList.SetAt( pos, newID );
// set up the context
gVTSObjectListContext.contextObjListPtr = this;
gVTSObjectListContext.contextOldID = oldID;
gVTSObjectListContext.contextNewID = newID;
// scan the list looking for unique ID's
objOPVL->ScanList( VTSObjectListContext::MoveHelper );
}
//
// VTSObjectListContext::MoveHelper
//
bool VTSObjectListContext::MoveHelper( int indx, VTSObjPropValue &val )
{
if (val.valueObjID == gVTSObjectListContext.contextOldID) {
// get this record its new ID
val.valueObjID = gVTSObjectListContext.contextNewID;
// save this record
gVTSObjectListContext.contextObjListPtr->objOPVL->WriteElem( indx, val );
}
// keep searching
return true;
}
//
// VTSObjectList::Length
//
int VTSObjectList::Length( void )
{
return objIDList.GetCount();
}
//
// VTSObjectList::operator[]
//
unsigned int VTSObjectList::operator[]( int indx )
{
POSITION pos = objIDList.FindIndex( indx )
;
// make sure it's valid
ASSERT( pos != NULL );
// return the object ID
return objIDList.GetAt(pos);
}
//
// VTSObjectPropListContext
//
// Same design pattern as VTSObjectListContext.
//
class VTSObjectPropListContext {
friend class VTSObjectPropList;
VTSObjectPropListPtr contextObjListPtr;
objId contextObjID;
int contextPropID;
int contextOldID;
int contextNewID;
int contextIndx;
static bool BindHelper( int indx, VTSObjPropValue &val );
static bool AddHelper( int indx, VTSObjPropValue &val );
static bool RemoveHelper( int indx, VTSObjPropValue &val );
static bool MoveHelper( int indx, VTSObjPropValue &val );
static bool ArrayToValueHelper( int indx, VTSObjPropValue &val );
static bool ValueToArrayHelper( int indx, VTSObjPropValue &val );
static bool NewDescriptionHelper( int indx, VTSObjPropValue &val );
};
VTSObjectPropListContext gVTSObjectPropListContext;
//
// VTSObjectPropList::VTSObjectPropList
//
VTSObjectPropList::VTSObjectPropList( VTSObjPropValueListPtr lp )
: propOPVL(lp), propObjID(-1)
{
}
//
// VTSObjectPropList::~VTSObjectPropList
//
VTSObjectPropList::~VTSObjectPropList( void )
{
}
//
// VTSObjectPropList::Bind
//
void VTSObjectPropList::Bind( unsigned int objID )
{
// set the bound object ID
propObjID = objID;
// set up the context
gVTSObjectPropListContext.contextObjListPtr = this;
gVTSObjectPropListContext.contextObjID = propObjID;
gVTSObjectPropListContext.contextPropID = -1;
// clear out all the existing properties
propList.RemoveAll();
// scan the list looking for unique ID's
propOPVL->ScanList( VTSObjectPropListContext::BindHelper );
}
//
// VTSObjectPropListContext::BindHelper
//
// If a new property ID is found, add it to the end of the list, set the context so the new one
// isn't added more than once, and continue searching.
//
bool VTSObjectPropListContext::BindHelper( int indx, VTSObjPropValue &val )
{
VTSObjectPropDesc desc
;
// match the current object and a new property
if ((val.valueObjID == gVTSObjectPropListContext.contextObjID)
&& (val.valueProperty != gVTSObjectPropListContext.contextPropID)) {
// build a descriptor
desc.propID = val.valueProperty;
desc.propIsArray = (val.valueIndx == 0);
desc.propValue = gVTSObjectPropListContext.contextObjListPtr->propOPVL->DescribeValue( indx );
// add this to the list we are building
gVTSObjectPropListContext.contextObjListPtr->propList.AddTail( desc );
// dont add this one again
gVTSObjectPropListContext.contextPropID = val.valueProperty;
}
// keep searching
return true;
}
//
// VTSObjectPropList::Add
//
void VTSObjectPropList::Add( void )
{
int indx
;
VTSObjectPropDesc desc
;
VTSObjPropValue val
;
unsigned int propID
;
// set up the context
gVTSObjectPropListContext.contextObjListPtr = this;
gVTSObjectPropListContext.contextObjID = propObjID;
gVTSObjectPropListContext.contextPropID = -1;
// scan through the list and look for the right place
indx = propOPVL->ScanList( VTSObjectPropListContext::AddHelper );
// if no place to insert the new record was found, add it to the end
if (indx < 0)
indx = propOPVL->Length();
// pick the next property
propID = gVTSObjectPropListContext.contextPropID + 1;
// make sure it hasn't already been used
while (Exists(propID))
propID += 1;
// initialize the fields
val.valueObjID = propObjID;
val.valueProperty = propID;
val.valueIndx = -1; // not an array, just a value
val.valueComponent = 0; // only one component
val.valueType = 0; // null
val.valueContext = -1; // application encoded
val.valueElemID = 0; // no more data
// add it to the list
propOPVL->InsertElem( indx, val );
// initialize a descriptor
desc.propID = propID;
desc.propIsArray = false;
desc.propValue = _T("null");
// add it to the shadow list
propList.AddTail( desc );
}
//
// VTSObjectPropListContext::AddHelper
//
bool VTSObjectPropListContext::AddHelper( int indx, VTSObjPropValue &val )
{
// match the current object
if (val.valueObjID == gVTSObjectPropListContext.contextObjID) {
// look for the maximum property
if (val.valueProperty > gVTSObjectPropListContext.contextPropID)
gVTSObjectPropListContext.contextPropID = val.valueProperty;
} else
if (gVTSObjectPropListContext.contextPropID >= 0)
return false;
// keep searching
return true;
}
//
// VTSObjectPropList::Remove
//
void VTSObjectPropList::Remove( int indx )
{
// set up the context, get the ID
gVTSObjectPropListContext.contextObjListPtr = this;
gVTSObjectPropListContext.contextObjID = propObjID;
gVTSObjectPropListContext.contextPropID = operator []( indx ).propID;
// remove it from the shadow list
propList.RemoveAt(propList.FindIndex(indx));
// scan through the list and remove them all
propOPVL->ScanList( VTSObjectPropListContext::RemoveHelper );
}
//
// VTSObjectPropListContext::RemoveHelper
//
// Remove the record if the ID's match.
//
bool VTSObjectPropListContext::RemoveHelper( int indx, VTSObjPropValue &val )
{
// match the current object and property
if ((val.valueObjID == gVTSObjectPropListContext.contextObjID)
&& (val.valueProperty == gVTSObjectPropListContext.contextPropID)) {
// remove it from the list
gVTSObjectPropListContext.contextObjListPtr->propOPVL->RemoveElem( indx );
}
// keep searching
return true;
}
//
// VTSObjectPropList::Exists
//
bool VTSObjectPropList::Exists( int propID )
{
bool found
;
POSITION pos = NULL
;
// find the position of the oldID
found = false;
pos = propList.GetHeadPosition();
while (pos != NULL)
if (propList.GetNext(pos).propID == propID) {
found = true;
break;
}
// return result
return (found);
}
//
// VTSObjectPropList::Move
//
void VTSObjectPropList::Move( int oldID, int newID )
{
bool found
;
POSITION pos, foundPos = NULL
;
VTSObjectPropDesc desc
;
// find the position of the oldID
found = false;
pos = propList.GetHeadPosition();
while (pos != NULL) {
foundPos = pos;
desc = propList.GetNext(pos);
if (desc.propID == oldID) {
found = true;
break;
}
}
// if the old one isn't found this should probably throw something
if (!found)
return;
// update the current contents
desc.propID = newID;
// change the shadow value
propList.SetAt( foundPos, desc );
// set up the context
gVTSObjectPropListContext.contextObjListPtr = this;
gVTSObjectPropListContext.contextObjID = propObjID;
gVTSObjectPropListContext.contextOldID = oldID;
gVTSObjectPropListContext.contextNewID = newID;
// scan the list looking for unique ID's
propOPVL->ScanList( VTSObjectPropListContext::MoveHelper );
}
//
// VTSObjectPropListContext::MoveHelper
//
bool VTSObjectPropListContext::MoveHelper( int indx, VTSObjPropValue &val )
{
// match the current object and property
if ((val.valueObjID == gVTSObjectPropListContext.contextObjID)
&& (val.valueProperty == gVTSObjectPropListContext.contextOldID)) {
// get this record its new ID
val.valueProperty = gVTSObjectPropListContext.contextNewID;
// save this record
gVTSObjectPropListContext.contextObjListPtr->propOPVL->WriteElem( indx, val );
}
// keep searching
return true;
}
//
// VTSObjectPropList::ArrayToValue
//
// Translating an array to a value consists of removing the array marker, changing the
// valueIndx of the first element (1) to (-1) so that it becomes a simple value, and
// deleting all the rest of the array elements.
//
void VTSObjectPropList::ArrayToValue( int propID )
{
// set up the context
gVTSObjectPropListContext.contextObjListPtr = this;
gVTSObjectPropListContext.contextObjID = propObjID;
gVTSObjectPropListContext.contextPropID = propID;
gVTSObjectPropListContext.contextIndx = -1;
// scan the list looking for array elements
propOPVL->ScanList( VTSObjectPropListContext::ArrayToValueHelper );
// the property must have some value, but if the array was empty there wouldn't
// be anything that marks that the property exists. The old location of the
// array marker is a good spot to insert a 'null'.
if (gVTSObjectPropListContext.contextIndx >= 0) {
VTSObjPropValue val
;
// initialize the fields
val.valueObjID = propObjID;
val.valueProperty = propID;
val.valueIndx = -1; // not an array, just a value
val.valueComponent = 0; // only one component
val.valueType = 0; // null
val.valueContext = -1; // application encoded
val.valueElemID = 0; // no more data
// make it the property value
propOPVL->InsertElem( gVTSObjectPropListContext.contextIndx, val );
}
}
//
// VTSObjectPropListContext::ArrayToValueHelper
//
bool VTSObjectPropListContext::ArrayToValueHelper( int indx, VTSObjPropValue &val )
{
// match the current object and property
if ((val.valueObjID != gVTSObjectPropListContext.contextObjID)
|| (val.valueProperty != gVTSObjectPropListContext.contextPropID))
return true;
// save the position of the array marker
if (val.valueIndx == 0)
gVTSObjectPropListContext.contextIndx = indx;
if (val.valueIndx == 1) {
// this is no longer an array
val.valueIndx = -1;
// save this record
gVTSObjectPropListContext.contextObjListPtr->propOPVL->WriteElem( indx, val );
// reset the "add a value" flag
gVTSObjectPropListContext.contextIndx = -1;
} else {
// remove this record
gVTSObjectPropListContext.contextObjListPtr->propOPVL->RemoveElem( indx );
}
// keep searching
return true;
}
//
// VTSObjectPropList::ValueToArray
//
void VTSObjectPropList::ValueToArray( int propID )
{
VTSObjPropValue val
;
// set up the context
gVTSObjectPropListContext.contextObjListPtr = this;
gVTSObjectPropListContext.contextObjID = propObjID;
gVTSObjectPropListContext.contextPropID = propID;
gVTSObjectPropListContext.contextIndx = -1;
// scan the list looking for array elements
propOPVL->ScanList( VTSObjectPropListContext::ValueToArrayHelper );
// make sure there is a place to insert the marker
ASSERT( gVTSObjectPropListContext.contextIndx >= 0 );
// initialize the fields for a marker
val.valueObjID = propObjID;
val.valueProperty = propID;
val.valueIndx = 0; // not an array, just a value
val.valueComponent = 0; // only one component
val.valueType = 0; // null
val.valueContext = 0; // application encoded
val.valueElemID = 0; // no more data
// add it to the list
propOPVL->InsertElem( gVTSObjectPropListContext.contextIndx, val );
}
//
// VTSObjectPropListContext::ValueToArrayHelper
//
bool VTSObjectPropListContext::ValueToArrayHelper( int indx, VTSObjPropValue &val )
{
// match the current object and property
if ((val.valueObjID != gVTSObjectPropListContext.contextObjID)
|| (val.valueProperty != gVTSObjectPropListContext.contextPropID))
return true;
// check for value components
if (val.valueIndx == -1) {
// first component?
if (gVTSObjectPropListContext.contextIndx < 0)
gVTSObjectPropListContext.contextIndx = indx;
// make this the first array element
val.valueIndx = 1;
// save this record
gVTSObjectPropListContext.contextObjListPtr->propOPVL->WriteElem( indx, val );
}
// keep searching
return true;
}
//
// VTSObjectPropList::NewDescription
//
void VTSObjectPropList::NewDescription( int propID )
{
// set up the context
gVTSObjectPropListContext.contextObjListPtr = this;
gVTSObjectPropListContext.contextObjID = propObjID;
gVTSObjectPropListContext.contextPropID = propID;
// scan the list looking for property
propOPVL->ScanList( VTSObjectPropListContext::NewDescriptionHelper );
}
//
// VTSObjectPropListContext::NewDescriptionHelper
//
bool VTSObjectPropListContext::NewDescriptionHelper( int indx, VTSObjPropValue &val )
{
POSITION pos
;
VTSObjectPropDesc desc
;
// match the current object and a new property
if ((val.valueObjID == gVTSObjectPropListContext.contextObjID)
&& (val.valueProperty == gVTSObjectPropListContext.contextPropID)) {
// look for the descriptor
pos = gVTSObjectPropListContext.contextObjListPtr->propList.GetHeadPosition();
while (pos != NULL) {
VTSObjectPropDesc &desc = gVTSObjectPropListContext.contextObjListPtr->propList.GetNext(pos);
if (desc.propID == gVTSObjectPropListContext.contextPropID) {
desc.propValue = gVTSObjectPropListContext.contextObjListPtr->propOPVL->DescribeValue( indx );
desc.propIsArray = (val.valueIndx == 0);
break;
}
}
// all done
return false;
}
// keep searching
return true;
}
//
// VTSObjectPropList::Length
//
int VTSObjectPropList::Length( void )
{
return propList.GetCount();
}
//
// VTSObjectPropList::operator[]
//
const VTSObjectPropDesc &VTSObjectPropList::operator[]( int indx )
{
POSITION pos = propList.FindIndex( indx )
;
// make sure it's valid
ASSERT( pos != NULL );
// return the object ID
return propList.GetAt(pos);
}
//
// VTSValueContext
//
// Same design pattern as VTSObjectListContext.
//
class VTSValueContext {
friend class VTSValue;
VTSValuePtr contextValuePtr;
objId contextObjID;
int contextPropID;
int contextArrayIndex;
static bool BindHelper( int indx, VTSObjPropValue &val );
static bool RemoveHelper( int indx, VTSObjPropValue &val );
};
VTSValueContext gVTSValueContext;
//
// VTSValue::VTSValue
//
VTSValue::VTSValue( VTSObjPropValueListPtr lp )
: valueOPVL(lp), valueObjID(-1), valuePropID(-1), valueArrayIndex(-1)
{
}
//
// VTSValue::~VTSValue
//
VTSValue::~VTSValue( void )
{
}
//
// VTSValue::Bind
//
void VTSValue::Bind( unsigned int objID, int propID, int arrayIndex )
{
// set the bindings
valueObjID = objID;
valuePropID = propID;
valueArrayIndex = arrayIndex;
// clear the component index and length
valueCompIndex = -1;
valueCompLen = 0;
// set the context
gVTSValueContext.contextValuePtr = this;
gVTSValueContext.contextObjID = valueObjID;
gVTSValueContext.contextPropID = valuePropID;
gVTSValueContext.contextArrayIndex = valueArrayIndex;
// scan the list looking for components
valueOPVL->ScanList( VTSValueContext::BindHelper );
}
//
// VTSValueContext::BindHelper
//
// If a new property ID is found, add it to the end of the list, set the context so the new one
// isn't added more than once, and continue searching.
//
bool VTSValueContext::BindHelper( int indx, VTSObjPropValue &val )
{
// match the current object, property, and index. If we've already found the
// content then don't bother searching more.
if ((val.valueObjID != gVTSValueContext.contextObjID)
|| (val.valueProperty != gVTSValueContext.contextPropID)
|| (val.valueIndx != gVTSValueContext.contextArrayIndex))
return (gVTSValueContext.contextValuePtr->valueCompIndex == -1);
if (gVTSValueContext.contextValuePtr->valueCompIndex == -1) {
// the first component must be zero
ASSERT( val.valueComponent == 0 );
// set the index and count
gVTSValueContext.contextValuePtr->valueCompIndex = indx;
gVTSValueContext.contextValuePtr->valueCompLen = 1;
} else {
// update the length
gVTSValueContext.contextValuePtr->valueCompLen =
indx - gVTSValueContext.contextValuePtr->valueCompIndex + 1;
// make sure the components are in order
ASSERT( val.valueComponent == gVTSValueContext.contextValuePtr->valueCompLen - 1 );
}
// keep searching
return true;
}
//
// VTSValue::Add
//
// Adding a component is easy because the components default to application encoded nulls.
//
void VTSValue::Add( void )
{
VTSObjPropValue val
;
// initialize a value
val.valueObjID = valueObjID;
val.valueProperty = valuePropID;
val.valueIndx = valueArrayIndex;
val.valueComponent = valueCompLen; // component added on the end
val.valueType = 0; // null data type
val.valueContext = -1; // application encoded
val.valueElemID = 0; // no additional element
// stuff it into the list
valueOPVL->InsertElem( valueCompIndex + valueCompLen, val );
// add one to the length
valueCompLen += 1;
}
//
// VTSValue::Remove
//
// Removing a component is easy because it is easy to calculate the offset into the list.
// If there is an associated object make sure it gets deleted, delete the component,
// then use a helper to renumber the rest of the components.
//
void VTSValue::Remove( int indx )
{
VTSObjPropValue val
;
// make sure there is more than one component
ASSERT( valueCompLen > 1 );
// make sure it's not out of bounds
ASSERT( (indx >= 0) && (indx < valueCompLen) );
// compute the component index
int compIndex = valueCompIndex + indx
;
// read the current contents
valueOPVL->ReadElem( compIndex, val );
// if there's an associated object, delete it
if (val.valueElemID)
valueOPVL->objPropDoc->m_pDB->pObjMgr->DeleteObject( val.valueElemID );
// delete the component
valueOPVL->RemoveElem( compIndex );
// set the context
gVTSValueContext.contextValuePtr = this;
gVTSValueContext.contextObjID = valueObjID;
gVTSValueContext.contextPropID = valuePropID;
gVTSValueContext.contextArrayIndex = valueArrayIndex;
// scan the list looking for components, starting at the new component
valueOPVL->ScanList( VTSValueContext::RemoveHelper, compIndex );
// not so many
valueCompLen -= 1;
}
//
// VTSValueContext::RemoveHelper
//
// If a new property ID is found, add it to the end of the list, set the context so the new one
// isn't added more than once, and continue searching.
//
bool VTSValueContext::RemoveHelper( int indx, VTSObjPropValue &val )
{
// match the current object, property, and index, if there's no match there's no more looking
if ((val.valueObjID != gVTSValueContext.contextObjID)
|| (val.valueProperty != gVTSValueContext.contextPropID)
|| (val.valueIndx != gVTSValueContext.contextArrayIndex))
return false;
// make this a previous component
val.valueComponent -= 1;
// save this record
gVTSValueContext.contextValuePtr->valueOPVL->WriteElem( indx, val );
// keep searching
return true;
}
//
// VTSValue::Length
//
int VTSValue::Length( void )
{
return valueCompLen;
}
//
// VTSValue::ReadComponent
//
void VTSValue::ReadComponent( int indx, VTSObjPropValueElemDesc& desc )
{
// make sure it's not out of bounds
ASSERT( (indx >= 0) && (indx < valueCompLen) );
// compute the component index
int compIndex = valueCompIndex + indx
;
// read the current contents
valueOPVL->ReadComponent( compIndex, desc );
}
//
// VTSValue::WriteComponent
//
void VTSValue::WriteComponent( int indx, VTSObjPropValueElemDesc& desc )
{
// make sure it's not out of bounds
ASSERT( (indx >= 0) && (indx < valueCompLen) );
// compute the component index
int compIndex = valueCompIndex + indx
;
// write the current contents
valueOPVL->WriteComponent( compIndex, desc );
}
//
// VTSValueArrayContext
//
// Same design pattern as VTSObjectListContext.
//
class VTSValueArrayContext {
friend class VTSValueArray;
VTSValueArrayPtr contextArrayPtr;
objId contextObjID;
int contextPropID;
int contextArrayIndex;
static bool BindHelper( int indx, VTSObjPropValue &val );
static bool AddHelper( int indx, VTSObjPropValue &val );
static bool RemoveHelper( int indx, VTSObjPropValue &val );
static bool NewDescriptionHelper( int indx, VTSObjPropValue &val );
};
VTSValueArrayContext gVTSValueArrayContext;
//
// VTSValueArray::VTSValueArray
//
VTSValueArray::VTSValueArray( VTSObjPropValueListPtr lp )
: arrayOPVL(lp), arrayObjID(0), arrayPropID(-1), arrayIndex(-1)
{
}
//
// VTSValueArray::~VTSValueArray
//
VTSValueArray::~VTSValueArray( void )
{
}
//
// VTSValueArray::Bind
//
void VTSValueArray::Bind( unsigned int objID, int propID )
{
// set the bindings
arrayObjID = objID;
arrayPropID = propID;
// clear the component index and length
arrayIndex = -1;
// clear any current element descriptions
arrayText.RemoveAll();
// set the context
gVTSValueArrayContext.contextArrayPtr = this;
gVTSValueArrayContext.contextObjID = arrayObjID;
gVTSValueArrayContext.contextPropID = arrayPropID;
gVTSValueArrayContext.contextArrayIndex = -1;
// scan the list looking for components
arrayOPVL->ScanList( VTSValueArrayContext::BindHelper );
}
//
// VTSValueArrayContext::BindHelper
//
bool VTSValueArrayContext::BindHelper( int indx, VTSObjPropValue &val )
{
VTSValueArrayPtr vap = gVTSValueArrayContext.contextArrayPtr
;
// check for a match
if ((val.valueObjID != gVTSValueArrayContext.contextObjID)
|| (val.valueProperty != gVTSValueArrayContext.contextPropID))
return (vap->arrayIndex == -1);
if (vap->arrayIndex == -1)
// set the index so we can scan faster the next time
vap->arrayIndex = indx;
// skip the marker
if (val.valueIndx == 0)
return true;
// if we haven't seen this array element yet, get a description of it
if (val.valueIndx != gVTSValueArrayContext.contextArrayIndex) {
// ask the list for a description
vap->arrayText.AddTail( vap->arrayOPVL->DescribeValue( indx ) );
// skip along until the next element
gVTSValueArrayContext.contextArrayIndex = val.valueIndx;
}
// keep searching
return true;
}
//
// VTSValueArray::Add
//
void VTSValueArray::Add( void )
{
int indx
;
VTSObjPropValue val
;
// set the context
gVTSValueArrayContext.contextArrayPtr = this;
gVTSValueArrayContext.contextObjID = arrayObjID;
gVTSValueArrayContext.contextPropID = arrayPropID;
// find the index of the last element
indx = arrayOPVL->ScanList( VTSValueArrayContext::AddHelper, arrayIndex );
// if the indx is -1 there are no more elements in the array
if (indx < 0)
indx = arrayOPVL->Length();
// there must be at least something
ASSERT( indx > 0 );
// read the last element
arrayOPVL->ReadElem( indx - 1, val );
// initialize a value
val.valueIndx += 1; // index becomes the next value
val.valueComponent = 0; // first and only component
val.valueType = 0; // null data type
val.valueContext = -1; // application encoded
val.valueElemID = 0; // no additional element
// stuff it into the list
arrayOPVL->InsertElem( indx, val );
// add its description
arrayText.AddTail( arrayOPVL->DescribeValue(indx) );
}
//
// VTSValueArrayContext::AddHelper
//
// Unlike the other BindHelpers, this is only interested in finding the first record
// that matches the object and property.
//
bool VTSValueArrayContext::AddHelper( int indx, VTSObjPropValue &val )
{
// check for a match
return ((val.valueObjID == gVTSValueArrayContext.contextObjID)
&& (val.valueProperty == gVTSValueArrayContext.contextPropID));
}
//
// VTSValueArray::Remove
//
void VTSValueArray::Remove( int indx )
{
// set the context
gVTSValueArrayContext.contextArrayPtr = this;
gVTSValueArrayContext.contextObjID = arrayObjID;
gVTSValueArrayContext.contextPropID = arrayPropID;
gVTSValueArrayContext.contextArrayIndex = indx;
// scan the list looking for components
arrayOPVL->ScanList( VTSValueArrayContext::RemoveHelper, arrayIndex );
}
//
// VTSValueArrayContext::RemoveHelper
//
bool VTSValueArrayContext::RemoveHelper( int indx, VTSObjPropValue &val )
{
// check for a match
if ((val.valueObjID == gVTSValueArrayContext.contextObjID)
&& (val.valueProperty == gVTSValueArrayContext.contextPropID)) {
if (val.valueIndx == gVTSValueArrayContext.contextArrayIndex) {
// get rid of this one
gVTSValueArrayContext.contextArrayPtr->arrayOPVL->RemoveElem( indx );
} else
if (val.valueIndx > gVTSValueArrayContext.contextArrayIndex) {
// move this down one
val.valueIndx -= 1;
// save it
gVTSValueArrayContext.contextArrayPtr->arrayOPVL->WriteElem( indx, val );
} else
;
}
// keep searching
return true;
}
//
// VTSValueArray::NewDescription
//
void VTSValueArray::NewDescription( int indx )
{
// set the context
gVTSValueArrayContext.contextArrayPtr = this;
gVTSValueArrayContext.contextObjID = arrayObjID;
gVTSValueArrayContext.contextPropID = arrayPropID;
gVTSValueArrayContext.contextArrayIndex = indx;
// scan the list looking for components
arrayOPVL->ScanList( VTSValueArrayContext::NewDescriptionHelper, arrayIndex );
}
//
// VTSValueArrayContext::NewDescriptionHelper
//
bool VTSValueArrayContext::NewDescriptionHelper( int indx, VTSObjPropValue &val )
{
// match the current object, property, and array index
if ((val.valueObjID == gVTSValueArrayContext.contextObjID)
&& (val.valueProperty == gVTSValueArrayContext.contextPropID)
&& (val.valueIndx == gVTSValueArrayContext.contextArrayIndex)) {
// make SetAt() a little easier to read
VTSValueArrayPtr vap = gVTSValueArrayContext.contextArrayPtr
;
// build a new description
vap->arrayText.SetAt( vap->arrayText.FindIndex(gVTSValueArrayContext.contextArrayIndex - 1)
, vap->arrayOPVL->DescribeValue( indx )
);
// all done
return false;
}
// keep searching
return true;
}
//
// VTSValueArray::Length
//
int VTSValueArray::Length( void )
{
return arrayText.GetCount();
}
//
// VTSValueArray::GetText
//
CString& VTSValueArray::GetText( int indx )
{
return arrayText.GetAt( arrayText.FindIndex(indx) );
}
//
// VTSValueArray::SetText
//
void VTSValueArray::SetText( int indx, CString& valu )
{
arrayText.SetAt( arrayText.FindIndex(indx), valu );
}