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
|
/*
* File: itemfield.cpp
*
* Author: Lukas Zeller (luz@plan44.ch)
*
* TItemField
* Abstract class, holds a single field value
* TStringField, TIntegerField, TTimeStampField etc.
* Implementations of field types
*
* Copyright (c) 2001-2011 by Synthesis AG + plan44.ch
*
* 2001-08-08 : luz : created
*
*/
// includes
#include "prefix_file.h"
#include "sysync.h"
#include "itemfield.h"
#include "multifielditemtype.h"
#if defined(CHECKSUM_CHANGELOG) && !defined(RECORDHASH_FROM_DBAPI)
#include "sysync_crc16.h"
#endif
using namespace sysync;
namespace sysync {
// type names
cAppCharP const ItemFieldTypeNames[numFieldTypes] = {
"string",
"telephone",
"integer",
"timestamp",
"date",
"url",
"multiline",
"blob",
"none"
};
// corresponding SyncML devInf <DataType>
const TPropDataTypes devInfPropTypes[numFieldTypes] = {
proptype_chr, // string -> Character
proptype_phonenum, // telephone -> Phone number
proptype_int, // integer -> Integer
proptype_datetime, // Timestamp -> Date and time of day
proptype_datetime, // Date -> Date and time of day
proptype_chr, // URL -> Character
proptype_text, // multiline -> plain text
proptype_bin, // BLOB -> Binary
proptype_unknown, // none -> unknown
};
/*
* Implementation of TItemField
*/
TItemField::TItemField() :
fAssigned(false) // unassigned at creation
{
} // TItemField::TItemField
TItemField::~TItemField()
{
} // TItemField::~TItemField
#ifdef SYDEBUG
// show field contents as string for debug output
size_t TItemField::StringObjFieldAppend(string &s, uInt16 aMaxStrLen)
{
if (isUnassigned())
s+="<unassigned>";
else if (isEmpty())
s+="<empty>";
else
appendToString(s, aMaxStrLen);
return 0; // non-strings do not have a real size
} // TItemField::StringObjFieldAppend
#endif
// append (default to appending string value of other field)
void TItemField::append(TItemField &aItemField)
{
string s;
aItemField.getAsString(s);
appendString(s);
} // TItemField::append
// generic cross-type assignment via string format
TItemField& TItemField::operator=(TItemField &aItemField)
{
string s;
if (aItemField.isUnassigned()) unAssign(); // copy unassigned status
else if (aItemField.isEmpty()) assignEmpty(); // copy empty status
else {
aItemField.getAsString(s);
setAsString(s);
}
return *this;
} // TItemField::operator=
// get any field as integer
fieldinteger_t TItemField::getAsInteger(void)
{
// convert to integer number
string s;
fieldinteger_t i;
getAsString(s);
if (!
#ifndef NO64BITINT
StrToLongLong(s.c_str(),i)
#else
StrToLong(s.c_str(),i)
#endif
)
i=0; // defined value
return i;
} // TItemField::getAsInteger
// get integer as numeric string
void TItemField::setAsInteger(fieldinteger_t aInteger)
{
string s;
#ifndef NO64BITINT
LONGLONGTOSTR(s,PRINTF_LLD_ARG(aInteger));
#else
StringObjPrintf(s,"%ld",(sInt32)aInteger);
#endif
// set as string
setAsString(s);
} // TItemField::setAsInteger
// set as string, max number of chars = aLen
void TItemField::setAsString(cAppCharP aString, size_t aLen)
{
if (!aString)
assignEmpty();
else {
string t(aString,aLen);
// now call basic setter
setAsString(t.c_str());
}
} // TItemField::setAsString
size_t TItemField::getStringSize(void)
{
if (getType()==fty_none) return 0; // empty/unassigned field has no size (avoid unneeded getAsString)
string s;
getAsString(s);
return s.size();
} // TItemField::getStringSize
bool TItemField::contains(TItemField &aItemField, bool aCaseInsensitive)
{
string s;
aItemField.getAsString(s);
return findInString(s.c_str(), aCaseInsensitive)>=0;
}
#ifdef STREAMFIELD_SUPPORT
// reset stream (start reading and/or writing at specified position)
void TItemField::resetStream(size_t aPos)
{
if (aPos==0)
fStreamPos=0; // optimized short-cut
else {
size_t sz=getStreamSize();
if (aPos>sz)
fStreamPos=sz;
else
fStreamPos=aPos;
}
} // TItemField::resetStream
// read from stream
size_t TItemField::readStream(void *aBuffer, size_t aMaxBytes)
{
string s;
getAsString(s);
size_t sz=s.size();
if (fStreamPos>sz) fStreamPos=sz;
if (fStreamPos+aMaxBytes > sz) aMaxBytes=sz-fStreamPos;
if (aMaxBytes>0) {
// copy memory
memcpy(aBuffer,s.c_str()+fStreamPos,aMaxBytes);
}
// return number of chars actually read
fStreamPos+=aMaxBytes;
return aMaxBytes;
} // TItemField::readStream
// write to stream
size_t TItemField::writeStream(void *aBuffer, size_t aNumBytes)
{
if (aNumBytes==0) return 0;
if (fStreamPos!=0) {
// not replacing entire contents, need read-modify-write of string
string s;
getAsString(s);
if (fStreamPos>s.size()) fStreamPos=s.size();
s.resize(fStreamPos);
s.append((cAppCharP)aBuffer,aNumBytes);
setAsString(s);
}
else {
setAsString((cAppCharP)aBuffer,aNumBytes);
}
fStreamPos+=aNumBytes;
return aNumBytes;
} // TItemField::writeStream
#endif
/* end of TItemField implementation */
#ifdef ARRAYFIELD_SUPPORT
/*
* Implementation of TArrayField
*/
// constructor
TArrayField::TArrayField(TItemFieldTypes aLeafFieldType, GZones *aGZonesP)
{
fLeafFieldType=aLeafFieldType;
fGZonesP = aGZonesP;
// field for index==0 always exists
fFirstField = newItemField(fLeafFieldType,fGZonesP,false);
} // TArrayField::TArrayField
// destructor
TArrayField::~TArrayField()
{
// make sure leaf fields (except idx==0) are gone
unAssign();
// and kill firstfield
delete fFirstField;
} // TArrayField::~TArrayField
bool TArrayField::elementsBasedOn(TItemFieldTypes aFieldType) const
{
return fFirstField->isBasedOn(aFieldType);
} // TArrayField::elementsBasedOn
#ifdef SYDEBUG
// show field contents as string for debug output
size_t TArrayField::StringObjFieldAppend(string &s, uInt16)
{
if (!isAssigned())
return inherited::StringObjFieldAppend(s,0); // let ancestor show
StringObjAppendPrintf(s,"<array with %ld elements>",(long int)(arraySize()));
return 0;
} // TArrayField::StringObjFieldAppend
#endif
// get field from array (creates new if index is larger than current array size)
TItemField *TArrayField::getArrayField(sInt16 aArrIdx, bool aExistingOnly)
{
TItemField *fldP = NULL;
// check index, negative index means array field itself
if (aArrIdx<0) return this;
// check if we have that field already
if (aArrIdx<arraySize()) {
// pointer array is large enough
fldP = fArray[aArrIdx];
if (fldP==NULL) {
// but element does not exist yet, create field for it
if (aArrIdx==0)
fldP = fFirstField;
else
fldP = newItemField(fLeafFieldType,fGZonesP,false);
fArray[aArrIdx]=fldP;
}
}
else if (aExistingOnly) {
// element does not exist and we want not to create new elements:
return NULL;
}
else {
// element does not exist yet, create new ones up to requested index
fArray.reserve(aArrIdx+1);
for (sInt16 idx=arraySize(); idx<=aArrIdx; idx++) {
fArray.push_back(NULL);
}
// actually create last field only (and use firstField if that happens to be idx==0)
if (aArrIdx==0)
fldP = fFirstField;
else
fldP = newItemField(fLeafFieldType,fGZonesP,false);
fArray[aArrIdx]=fldP;
}
// return field
return fldP;
} // TArrayField::getArrayField
#if defined(CHECKSUM_CHANGELOG) && !defined(RECORDHASH_FROM_DBAPI)
// calc CRC over all fields
uInt16 TArrayField::getDataCRC(uInt16 crc)
{
for (sInt16 idx=0; idx<arraySize(); idx++) {
crc=getArrayField(idx)->getDataCRC(crc);
}
return crc;
} // TArrayField::getDataCRC
#endif
// clear all leaf fields
void TArrayField::unAssign(void)
{
for (sInt16 idx=0; idx<arraySize(); idx++) {
if (fArray[idx]) {
if (idx==0)
fArray[idx]->unAssign(); // first is always kept, it is the fFirstField, so only unassign to remove content
else
delete fArray[idx];
fArray[idx]=NULL;
}
}
// clear list now
fArray.clear();
// clear flag as well that could be set in case of an explicitly assigned empty array
fAssigned = false;
} // TArrayField::unAssign
// assign
TItemField& TArrayField::operator=(TItemField &aItemField)
{
// assignment of empty (could be EMPTY or UNASSIGNED) must be handled by base class
if (aItemField.isEmpty()) return TItemField::operator=(aItemField);
// handle array-to-array assignments
if (aItemField.isArray()) {
// delete my current contents
unAssign();
// copy leaf fields from other field
for (sInt16 idx=0; idx<aItemField.arraySize(); idx++) {
// assign array members
*(getArrayField(idx)) = *(((TItemField *)(&aItemField))->getArrayField(idx));
}
}
else {
// non-array (non-empty) assigned to array: just assign value to first element
unAssign();
*(getArrayField(0)) = aItemField;
}
return *this;
} // TArrayField::operator=
// append values (only assigned ones)
// - if other field is an array, too, all elements of other array
// will be appended at end of this array.
// - if other field is a scalar, it's value will be appended to
// the array.
void TArrayField::append(TItemField &aItemField)
{
if (aItemField.isArray()) {
// append elements of another array to this array
for (sInt16 idx=0; idx<aItemField.arraySize(); idx++) {
// append all array members to this array
// Note: calling append recursively!
append(*(aItemField.getArrayField(idx)));
}
}
else {
// append value of another field to this array
// - do not append unassigned fields
if (aItemField.isAssigned()) {
*(getArrayField(arraySize())) = aItemField;
}
}
} // TArrayField::append
// append string to array = append string as last element of array
void TArrayField::appendString(cAppCharP aString, size_t aMaxChars)
{
getArrayField(arraySize())->setAsString(aString,aMaxChars);
} // TArrayField::appendString
// contains for arrays means "contains in any of the elements"
// (if aItemField is an array as well, this means that every element must be
// contained somewhere in my own array)
bool TArrayField::contains(TItemField &aItemField, bool aCaseInsensitive)
{
bool contained = false;
if (aItemField.isArray()) {
contained=true;
// array: all array elements must be contained in at least one of my elements
for (sInt16 idx=0; idx<aItemField.arraySize(); idx++) {
if (!contains(*(aItemField.getArrayField(idx)),aCaseInsensitive)) {
// one of the elements of aItemField is not contained in myself -> not contained
contained = false;
break;
}
}
}
else {
// leaf element: must be contained in at least one of my elements
contained = false;
for (sInt16 idx=0; idx<arraySize(); idx++) {
if (getArrayField(idx)->contains(aItemField,aCaseInsensitive)) {
// the value of aItemField is contained in one of my elements -> contained
contained = true;
break;
}
}
}
return contained;
} // TItemField::contains
// compare: returns 0 if equal, 1 if this > aItem, -1 if this < aItem,
// SYSYNC_NOT_COMPARABLE if not comparable at all or not equal and no ordering known
// Note: array fields are only comparable with other array fields
sInt16 TArrayField::compareWith(TItemField &aItemField, bool aCaseInsensitive)
{
if (!aItemField.isArray()) return SYSYNC_NOT_COMPARABLE;
// get sizes of arrays
sInt16 mysz = arraySize();
sInt16 othersz = aItemField.arraySize();
sInt16 commonsz = mysz>othersz ? othersz : mysz;
// compare common array elements
for (sInt16 idx=0; idx<commonsz; idx++) {
sInt16 res = getArrayField(idx)->compareWith(*(aItemField.getArrayField(idx)), aCaseInsensitive);
if (res!=0) return res; // all non-equal return
}
// all compared fields are equal
if (mysz==othersz) return 0; // same size : equal
// Sizes differ, but that only matters if the extra entries are
// actually assigned. Without that special case, we end up
// with the situation where parsing, encoding and parsing
// again leads to different fields:
// - EMAIL;TYPE=OTHER:foo -> EMAIL_FLAGS 1 entry "unassigned"
// - unassigned -> EMAIL:foo
// - EMAIL:foo -> EMAIL_FLAGS 0 entry
// - EMAIL_FLAGS 1 entry "unassigned" > EMAIL_FLAGS 0 entry
TItemField &largerField = mysz < othersz ? aItemField : *this;
sInt16 minsz, maxsz;
sInt16 res; // larger array is greater
if (mysz < othersz) {
minsz = mysz;
maxsz = othersz;
res = -1;
} else {
minsz = othersz;
maxsz = mysz;
res = 1;
}
for (sInt16 idx=minsz; idx<maxsz; idx++) {
if (largerField.getArrayField(idx)->isAssigned())
// found real difference
return res;
}
// larger array contains only extra unassigned entries, ignore them
return 0;
} // TArrayField::compareWith
/* end of TArrayField implementation */
#endif
/*
* Implementation of TStringField
*/
TStringField::TStringField()
{
#ifdef STREAMFIELD_SUPPORT
fBlobProxyP=NULL;
#endif
} // TStringField::TStringField
TStringField::~TStringField()
{
#ifdef STREAMFIELD_SUPPORT
// remove proxy if any
TBlobProxy::unlink(fBlobProxyP);
#endif
} // TStringField::~TStringField
#if defined(CHECKSUM_CHANGELOG) && !defined(RECORDHASH_FROM_DBAPI)
// changelog support: calculate CRC over contents
uInt16 TStringField::getDataCRC(uInt16 crc)
{
// CRC over characters in the string
return sysync_crc16_block(getCStr(),getStringSize(),crc);
} // TStringField::getDataCRC
#endif
// assignment
TItemField& TStringField::operator=(TItemField &aItemField)
{
DELETEPROXY; // forget old value and old proxy as well
// handle myself only if other is string field as well
if (aItemField.isBasedOn(fty_string)) {
// copy fields 1:1
const TStringField *sfP = static_cast<const TStringField *>(&aItemField);
fString=sfP->fString;
fAssigned=sfP->fAssigned;
#ifdef STREAMFIELD_SUPPORT
fBlobProxyP=sfP->fBlobProxyP; // copy proxy as well
if (fBlobProxyP) fBlobProxyP->link(); // link again, now both fields use the proxy
#endif
}
else
TItemField::operator=(aItemField); // generic cross-type assignment (via string)
return *this;
} // TStringField::operator=
// get size of string
size_t TStringField::getStringSize(void)
{
#ifdef STREAMFIELD_SUPPORT
return getStreamSize();
#else
return fString.size();
#endif
} // TStringField::getStringSize
// normalized string is contents without leading or trailing control chars or spaces
void TStringField::getAsNormalizedString(string &aString)
{
size_t nsiz = getStringSize();
// we need the actual string to do this
PULLFROMPROXY;
// find first non-control or non-WSP char
size_t start = 0;
while (start<nsiz && ((uInt8)fString[start])<=' ') start++;
// find last non-control or non-WSP char
while (nsiz>0 && ((uInt8)fString[nsiz-1])<=' ') nsiz--;
// take string without any leading or trailing white space or other control chars
aString.assign(fString,start,nsiz-start);
} // TStringField::getAsNormalizedString
#ifdef SYDEBUG
// show field contents as string for debug output
size_t TStringField::StringObjFieldAppend(string &s, uInt16 aMaxStrLen)
{
size_t n = 0;
// empty or unassigned is handled by base class
if (isEmpty()) { return inherited::StringObjFieldAppend(s,aMaxStrLen); }
// with proxy installed, do not pull the string
if (PROXYINSTALLED) {
s+="<proxy installed>";
n=0; // unknown size at this time
}
else {
// no proxy installed
s+='"';
// - check if display length is sufficient
size_t i;
n = getStringSize();
if (aMaxStrLen==0 || n<=10 || n<=size_t(aMaxStrLen-2)) {
// strings below 11 chars are always shown in full
s.append(fString);
}
else {
i = (aMaxStrLen-5)/2; // half of the size that can be displayed
s.append(fString,0,i);
s.append("...");
s.append(fString,n-i,i);
}
s+='"';
}
// return actual string size (not shortened one!)
return n;
} // TStringField::StringObjFieldAppend
#endif
#ifdef STREAMFIELD_SUPPORT
// set blob loader proxy (ownership is passed to field)
void TStringField::setBlobProxy(TBlobProxy *aBlobProxyP)
{
if (fBlobProxyP==aBlobProxyP) return; // same proxy, just ignore
// unlink previous proxy, if any
TBlobProxy::unlink(fBlobProxyP);
// assign new proxy
fBlobProxyP=aBlobProxyP;
// assigning a proxy means that the field is now assigned
fAssigned=true;
} // TStringField::setBlobProxy
// pull entire string from proxy and forget proxy
void TStringField::pullFromProxy(void)
{
if (fBlobProxyP) {
const size_t bufsiz=4096;
cAppCharP bufP = new char[bufsiz];
resetStream();
size_t by;
SYSYNC_TRY {
do {
by=fBlobProxyP->readBlobStream(this, fStreamPos, (void *)bufP, bufsiz);
fString.append(bufP,by);
} while (by==bufsiz);
}
SYSYNC_CATCH(exception &e)
// avoid crashing session if proxy pull fails
fString="Server error while getting data from proxy object: ";
fString+=e.what();
SYSYNC_ENDCATCH
delete bufP;
// proxy is no longer needed
TBlobProxy::unlink(fBlobProxyP);
}
} // TStringField::pullFromProxy
// return size of stream
size_t TStringField::getStreamSize(void)
{
if (fBlobProxyP)
SYSYNC_TRY {
return fBlobProxyP->getBlobSize(this); // return size of entire blob
}
SYSYNC_CATCH(exception &e)
return 0; // cannot return actual size
SYSYNC_ENDCATCH
else
return fString.size(); // just return size of already stored string
} // TStringField::getStreamSize
// read as stream
size_t TStringField::readStream(void *aBuffer, size_t aMaxBytes)
{
if (fBlobProxyP) {
SYSYNC_TRY {
// let proxy handle this
return fBlobProxyP->readBlobStream(this, fStreamPos, aBuffer, aMaxBytes);
}
SYSYNC_CATCH(...)
// do not return anything
return 0; // do not return anything
SYSYNC_ENDCATCH
}
else {
// read from string itself
size_t sz=fString.size();
if (fStreamPos>sz) fStreamPos=sz;
if (fStreamPos+aMaxBytes > sz) aMaxBytes=sz-fStreamPos;
if (aMaxBytes>0) {
// copy memory
memcpy(aBuffer,fString.c_str()+fStreamPos,aMaxBytes);
}
// return number of chars actually read
fStreamPos+=aMaxBytes;
return aMaxBytes;
}
} // TStringField::readStream
// write as stream
size_t TStringField::writeStream(void *aBuffer, size_t aNumBytes)
{
if (aNumBytes==0) return 0;
if (fStreamPos>fString.size()) fStreamPos=fString.size();
fString.resize(fStreamPos);
fString.append((cAppCharP)aBuffer,aNumBytes);
fStreamPos+=aNumBytes;
return aNumBytes;
} // TStringField::writeStream
#endif
// set as string, max number of chars to assign = aLen
void TStringField::setAsString(cAppCharP aString, size_t aLen)
{
DELETEPROXY; // forget old value and old proxy as well
if (!aString)
fString.erase();
else
fString.assign(aString,aLen);
stringWasAssigned();
} // TStringField::setAsString
// append to string
void TStringField::appendString(cAppCharP aString, size_t aMaxChars)
{
PULLFROMPROXY; // make sure we have all chars
if (aString) {
fString.append(aString,aMaxChars);
}
stringWasAssigned();
} // TStringField::appendString
// merge field contents into this field
bool TStringField::merge(TItemField &aItemField, const char aSep)
{
bool mergedsomething=false;
if (aItemField.isBasedOn(fty_string)) {
TStringField *sfP = static_cast<TStringField *>(&aItemField);
#ifdef STREAMFIELD_SUPPORT
pullFromProxy(); // make sure we have all chars
sfP->pullFromProxy(); // make sure we have all chars
#endif
if (aSep) {
// take each aSep separated part of source (aItemField), and if it is not
// yet part of target (this), then add it
string::size_type j,i=0;
string part;
do {
// extract part from source
j=sfP->fString.find(aSep,i);
if (j==string::npos)
part.assign(sfP->fString,i,sfP->fString.size()-i);
else
part.assign(sfP->fString,i,j-i);
// see if it is contained in target already
if (!part.empty() && fString.find(part)==string::npos) {
// not contained, add
if (!fString.empty()) fString+=aSep;
inherited::appendString(part);
mergedsomething=true;
}
// check next part
i=j+1; // char after last separator
} while (j!=string::npos);
}
else {
// no intelligent separator based merge, just append if not equal
if (sfP->fString != fString) {
inherited::appendString(sfP->fString);
mergedsomething=true;
}
}
}
return mergedsomething;
} // TStringField::merge
// compare: returns 0 if equal, 1 if this > aItem, -1 if this < aItem,
// SYSYNC_NOT_COMPARABLE if not equal and no ordering known or if field
// types do not match.
// Note: ordering may NOT be age relevant; it just means that an ordering
// for this field type exists.
// Note: Both fields must be assigned. NO TEST IS DONE HERE!
sInt16 TStringField::compareWith(TItemField &aItemField, bool aCaseInsensitive)
{
int result;
PULLFROMPROXY;
if (aItemField.isBasedOn(fty_string)) {
TStringField *sfP = static_cast<TStringField *>(&aItemField);
#ifdef STREAMFIELD_SUPPORT
sfP->pullFromProxy(); // make sure we have all chars
#endif
// direct compare possible, return strcmp
if (aCaseInsensitive)
result=strucmp(fString.c_str(),sfP->fString.c_str());
else
result=fString.compare(sfP->fString);
}
else {
// convert other field to string
string s;
aItemField.getAsString(s);
if (aCaseInsensitive)
result=strucmp(fString.c_str(),s.c_str());
else
result=fString.compare(s);
}
return result >0 ? 1 : (result<0 ? -1 : 0);
} // TStringField::compareWith
// check if specified field is shortened version of this one
bool TStringField::isShortVers(TItemField &aItemField, sInt32 aOthersMax)
{
if (!aItemField.isBasedOn(fty_string)) return false; // different types
TStringField *sfP = static_cast<TStringField *>(&aItemField);
#ifdef STREAMFIELD_SUPPORT
pullFromProxy(); // make sure we have all chars
sfP->pullFromProxy(); // make sure we have all chars
#endif
// same type
// - if other field is empty, it does not count as a shortened version in any case
if (sfP->isEmpty()) return false;
// - show if other field is shortened version of this one
return (
aOthersMax!=FIELD_OPT_MAXSIZE_NONE && // other field is limited (if not, cannot be short version)
(aOthersMax==FIELD_OPT_MAXSIZE_UNKNOWN || uInt32(aOthersMax)==sfP->getStringSize()) && // size is unknown or other value is max size
findInString(sfP->fString.c_str())==0 // other value is contained at beginning of this value
);
} // TStringField::isShortVers
// - check if String is contained in value and returns position
sInt16 TStringField::findInString(cAppCharP aString, bool aCaseInsensitive)
{
PULLFROMPROXY;
if (aString==NULL || *aString==0)
return fString.empty() ? 0 : -1; // if I am empty myself,treat empty string as contained at beginning
// no empty reference string
if (!aCaseInsensitive) {
return fString.find(aString);
}
else {
return strupos(fString.c_str(),aString,fString.size());
}
};
/* end of TStringField implementation */
/*
* Implementation of TBlobField
*/
TBlobField::TBlobField()
{
// nothing known about contents yet
fHasEncoding = enc_none;
fWantsEncoding = enc_none;
fCharset = chs_utf8;
} // TBlobField::TBlobField
TBlobField::~TBlobField()
{
} // TBlobField::~TBlobField
// assignment
TItemField& TBlobField::operator=(TItemField &aItemField)
{
DELETEPROXY; // forget old value and old proxy as well
// assignment of empty (could be EMPTY or UNASSIGNED) must be handled by base class
if (aItemField.isEmpty()) return TItemField::operator=(aItemField);
// handle non-empty myself if other is based on string (blob is just a enhanced string)
if (aItemField.isBasedOn(fty_string)) {
// assign string portions
TStringField::operator=(aItemField);
if(aItemField.isBasedOn(fty_blob)) {
// other is a blob, copy blob specific options
const TBlobField *bfP = static_cast<const TBlobField *>(&aItemField);
fHasEncoding=bfP->fHasEncoding;
fWantsEncoding=bfP->fWantsEncoding;
fCharset=bfP->fCharset;
}
else {
// reset blob options to default for string content
fHasEncoding = enc_none;
fWantsEncoding = enc_none;
fCharset = chs_utf8;
}
}
else
TItemField::operator=(aItemField); // generic cross-type assignment (via string)
return *this;
} // TBlobField::operator=
#ifdef SYDEBUG
// debug support
size_t TBlobField::StringObjFieldAppend(string &s, uInt16 aMaxStrLen)
{
// empty or unassigned is handled by base class
if (isEmpty()) { return inherited::StringObjFieldAppend(s, aMaxStrLen); }
// avoid pulling a proxy for debug
if (!PROXYINSTALLED) {
appendToString(s); // show standard string which is pseudo-content
return getStringSize(); // return actual size of BLOB
}
else
return inherited::StringObjFieldAppend(s, aMaxStrLen); // with proxy installed, base class will show that
} // TBlobField::StringObjFieldAppend
#endif // SYDEBUG
/* end of TBlobField implementation */
/*
* Implementation of TTelephoneField
*/
TTelephoneField::TTelephoneField()
{
} // TTelephoneField::TTelephoneField
TTelephoneField::~TTelephoneField()
{
} // TTelephoneField::~TTelephoneField
void TTelephoneField::getAsNormalizedString(string &aString)
{
cAppCharP p = getCStr();
char c;
aString.erase();
// only returns alphanum and +,*,#, rest is filtered out
while ((c=*p++)!=0) {
if (isalnum(c) || c=='+' || c=='#' || c=='*') aString+=c;
}
} // TTelephoneField::getAsNormalizedString
// compare: returns 0 if equal, 1 if this > aItem, -1 if this < aItem,
// SYSYNC_NOT_COMPARABLE if not equal and no ordering known or if field
// types do not match.
// Note: ordering may NOT be age relevant; it just means that an ordering
// for this field type exists.
// Note: Both fields must be assigned. NO TEST IS DONE HERE!
sInt16 TTelephoneField::compareWith(TItemField &aItemField, bool aCaseInsensitive)
{
if (!aItemField.isBasedOn(fty_telephone)) {
// if other field is not telephone, try normal string comparison
return TStringField::compareWith(aItemField, aCaseInsensitive);
}
// compare possible, return strcmp of normalized numbers
TTelephoneField *tfP = static_cast<TTelephoneField *>(&aItemField);
string s1,s2;
getAsNormalizedString(s1);
tfP->getAsNormalizedString(s2);
if (s1==s2) return 0; // equal
else return SYSYNC_NOT_COMPARABLE; // not equal, ordering makes no sense for tel numbers
} // TTelephoneField::compareWith
/* end of TTelephoneField implementation */
/*
* Implementation of TTelephoneField
*/
TMultilineField::TMultilineField()
{
} // TMultilineField::TMultilineField
TMultilineField::~TMultilineField()
{
} // TMultilineField::~TMultilineField
// compare: returns 0 if equal, 1 if this > aItem, -1 if this < aItem,
// SYSYNC_NOT_COMPARABLE if not equal and no ordering known or if field
// types do not match.
// Note: ordering may NOT be age relevant; it just means that an ordering
// for this field type exists.
// Note: Both fields must be assigned. NO TEST IS DONE HERE!
sInt16 TMultilineField::compareWith(TItemField &aItemField, bool aCaseInsensitive)
{
if (!aItemField.isBasedOn(fty_multiline)) {
// if other field is not multiline, try normal string comparison
return TStringField::compareWith(aItemField);
}
// compare possible, return strcmp of normalized texts
TMultilineField *mfP = static_cast<TMultilineField *>(&aItemField);
string s1,s2;
getAsNormalizedString(s1);
mfP->getAsNormalizedString(s2);
// compare
sInt8 result;
if (aCaseInsensitive)
result=strucmp(s1.c_str(),s2.c_str());
else
result=strcmp(s1.c_str(),s2.c_str());
return result >0 ? 1 : (result<0 ? -1 : 0);
} // TMultilineField::compareWith
/* end of TMultilineField implementation */
/*
* Implementation of TURLField
*/
TURLField::TURLField()
{
} // TURLField::TURLField
TURLField::~TURLField()
{
} // TURLField::~TURLField
void TURLField::stringWasAssigned(void)
{
// post-process string that was just assigned
string proto;
if (!fString.empty()) {
// make sure we have a URL with protocol
splitURL(fString.c_str() ,&proto, NULL, NULL, NULL, NULL, NULL, NULL);
if (proto.empty()) {
// no protocol set, but string not empty --> assume http
fString.insert(0, "http://");
}
}
inherited::stringWasAssigned();
} // TURLField::stringWasAssigned
/* end of TURLField implementation */
/*
* Implementation of TTimestampField
*/
TTimestampField::TTimestampField(GZones *aGZonesP)
{
fGZonesP = aGZonesP;
fTimestamp=0;
fTimecontext=TCTX_UNKNOWN;
} // TTimestampField::TTimestampField
TTimestampField::~TTimestampField()
{
} // TTimestampField::~TTimestampField
#if defined(CHECKSUM_CHANGELOG) && !defined(RECORDHASH_FROM_DBAPI)
// changelog support: calculate CRC over contents
uInt16 TTimestampField::getDataCRC(uInt16 crc)
{
// calculate CRC such that only a effective change in the time zone will cause a
// CRC change: TCTX_SYSTEM must be converted to UTC as changing the system time zone
// should not re-sync the entire calendar
lineartime_t crcts = fTimestamp;
timecontext_t crcctx = fTimecontext;
if (TCTX_IS_SYSTEM(crcctx)) {
if (TzConvertTimestamp(crcts,crcctx,TCTX_UTC,fGZonesP))
crcctx=TCTX_UTC; // use UTC representation for CRC (= same as in 3.0)
}
// try to avoid re-sync when upgrading from 3.0 to 3.1
if (TCTX_IS_UTC(crcctx)) crcctx=0; // this is what we had in 3.0 for non-floating timestamps
// CRC over timestamp itself and zone offset
crc=sysync_crc16_block(&crcts,sizeof(crcts),crc);
return sysync_crc16_block(&crcctx,sizeof(crcctx),crc);
} // TTimestampField::getDataCRC
#endif
// assignment
TItemField& TTimestampField::operator=(TItemField &aItemField)
{
// assignment of empty (could be EMPTY or UNASSIGNED) must be handled by base class
if (aItemField.isEmpty()) return TItemField::operator=(aItemField);
// handle non-empty myself
if (aItemField.isBasedOn(fty_timestamp)) {
const TTimestampField *sfP = static_cast<const TTimestampField *>(&aItemField);
fTimestamp=sfP->fTimestamp;
fTimecontext=sfP->fTimecontext;
fAssigned=sfP->fAssigned;
}
else if (aItemField.getCalcType()==fty_integer) {
setAsInteger(aItemField.getAsInteger());
}
else
TItemField::operator=(aItemField); // generic cross-type assignment (via string)
return *this;
} // TTimestampField::operator=
// compare: returns 0 if equal, 1 if this > aItem, -1 if this < aItem,
// SYSYNC_NOT_COMPARABLE if not equal and no ordering known or if field
// types do not match.
// Note: ordering may NOT be age relevant; it just means that an ordering
// for this field type exists.
// Note: Both fields must be assigned. NO TEST IS DONE HERE!
sInt16 TTimestampField::compareWith(TItemField &aItemField, bool aCaseInsensitive)
{
sInt16 res;
lineartime_t cmpval;
timecontext_t cmpcontext;
// determine value to compare with
if (aItemField.isBasedOn(fty_timestamp)) {
cmpval=static_cast<TTimestampField *>(&aItemField)->fTimestamp;
cmpcontext=static_cast<TTimestampField *>(&aItemField)->fTimecontext;
}
else {
// not same type
if (aItemField.getCalcType()==fty_integer) {
// use second argument as lineartime units number
cmpval=aItemField.getAsInteger();
cmpcontext=fTimecontext; // treat number as timestamp value in my own context
}
else {
// use second argument as ISO8601 string
string s;
aItemField.getAsString(s);
ISO8601StrToTimestamp(s.c_str(), cmpval, cmpcontext);
if (TCTX_IS_UNKNOWN(cmpcontext)) cmpcontext=fTimecontext; // treat unqualified ISO timestamp in my own context
}
}
// convert compare value into my own context (if not already so)
if (!TzConvertTimestamp(cmpval,cmpcontext,fTimecontext,fGZonesP))
return SYSYNC_NOT_COMPARABLE; // contexts are not compatible to compare
// compare possible, return strcmp-type result
res=fTimestamp==cmpval ? 0 : (fTimestamp>cmpval ? 1 : -1);
if (res!=0) {
// greater/less-type result is valid only if both sides are not empty
if (fTimestamp==0) return SYSYNC_NOT_COMPARABLE; // empty value, cannot compare
if (cmpval==0) return SYSYNC_NOT_COMPARABLE; // empty value, cannot compare
}
return res;
} // TTimestampField::compareWith
#ifdef SYDEBUG
// debug support
size_t TTimestampField::StringObjFieldAppend(string &s, uInt16 aMaxStrLen)
{
// empty or unassigned is handled by base class
if (isEmpty()) { return inherited::StringObjFieldAppend(s, aMaxStrLen); }
string str;
timecontext_t tctx;
TimestampToISO8601Str(str,fTimestamp,fTimecontext,true,true);
s.append(str);
tctx=fTimecontext;
if (TCTX_IS_TZ(tctx)) {
// symbolic time zone not represented in ISO8601, show extra
s.append(" (");
if (TCTX_IS_UNKNOWN(tctx)) {
s.append("floating");
}
else {
if (TCTX_IS_SYSTEM(tctx)) {
// is the system zone
s.append("System ");
// resolve from meta to symbolic zone
TzResolveMetaContext(tctx,fGZonesP);
}
if (!TCTX_IS_BUILTIN(tctx)) {
s.append("imported ");
}
s.append("TZ: ");
// show time zone name
TimeZoneContextToName(tctx,str,fGZonesP);
s.append(str);
}
s.append(")");
}
// size not known
return 0;
} // TTimestampField::StringObjFieldAppend
#endif // SYDEBUG
// set timestamp from ISO8601 string
void TTimestampField::setAsString(cAppCharP aString)
{
// - set context if contained in ISO string, otherwise leave it as TCTX_UNKNOWN
setAsISO8601(aString, TCTX_UNKNOWN, false);
} // TTimestampField::setAsString
// get timestamp as ISO8601 string (default representation)
void TTimestampField::getAsString(string &aString)
{
// use default rendering
// - show with current context, show UTC with Z but other zones without zone specifier
getAsISO8601(aString,TCTX_UNKNOWN,true,false,false,false);
} // getAsString
/// @brief add a delta time to the timestamp
/// @param aDeltaTime[in] : delta time value in lineartime_t units
void TTimestampField::addTime(lineartime_t aDeltaTime)
{
fTimestamp=fTimestamp+aDeltaTime;
fAssigned=true;
} // TTimestampField::addTime
/// @brief get time context
/// @return minute offset east of UTC, returns 0 for floating timestamps (and UTC, of course)
sInt16 TTimestampField::getMinuteOffset(void)
{
sInt16 moffs;
if (TzResolveToOffset(fTimecontext, moffs, fTimestamp, false, fGZonesP))
return moffs; // found offset
else
return 0; // no offset (e.g. floating timestamp)
} // TTimestampField::getMinuteOffset
/// @brief test for floating time (=time not in a specified zone context)
/// @return true if context is TCTX_UNKNOWN
bool TTimestampField::isFloating(void)
{
return TCTX_IS_UNKNOWN(fTimecontext);
} // TTimestampField::isFloating
/// @brief make timestamp floating (i.e. remove time zone info from context)
void TTimestampField::makeFloating(void)
{
// rendering flags from original context, new zone is UNKNOWN (=floating)
fTimecontext = TCTX_JOIN_RFLAGS_TZ(fTimecontext, TCTX_UNKNOWN);
} // TTimestampField::makeFloating
/// @brief test for duration
/// @return true if context has TCTX_DURATION rendering flag set
bool TTimestampField::isDuration(void)
{
return TCTX_IS_DURATION(fTimecontext);
} // TTimestampField::isDuration
/// @brief make timestamp a duration (also implies making it floating)
void TTimestampField::makeDuration(void)
{
// rendering flags from original context, new zone is UNKNOWN (=floating)
fTimecontext |= TCTX_DURATION;
makeFloating();
} // TTimestampField::makeDuration
/// @brief get timestamp converted to a specified time context
/// @param aTargetContext[in] : requests output context for timestamp.
/// Use TCTX_UNKNOWN to get timestamp as-is.
/// If timestamp is floating, it will always be returned as-is
/// @param aActualContext[out] : if not NULL, the actual context of the returned value
/// will be returned here. This might be another
// context than specified with aTargetContext depending on floating/notime status.
/// @return timestamp in lineartime
lineartime_t TTimestampField::getTimestampAs(timecontext_t aTargetContext, timecontext_t *aActualContext)
{
// default context is that of the field (will be overwritten if result is moved to another context)
if (aActualContext) *aActualContext = fTimecontext; // current field's context
// if no time set or TCTX_UNKNOWN specified, just return the timestamp as-is
if (fTimestamp==noLinearTime || TCTX_IS_UNKNOWN(aTargetContext)) {
return
TCTX_IS_DATEONLY(aTargetContext) ?
lineartime2dateonlyTime(fTimestamp) : // truncated to date-only
fTimestamp; // as-is
}
// convert to the requested context
lineartime_t ts = fTimestamp;
if (TCTX_IS_DATEONLY(fTimecontext)) {
// is date only, connot convert to another zone, just return date part (and original context)
return lineartime2dateonlyTime(ts);
}
else {
// datetime or time
if (!TzConvertTimestamp(ts,fTimecontext,aTargetContext,fGZonesP)) {
// cannot convert, but we return unconverted timestamp if we can also return the actual context
if (aActualContext)
return fTimestamp; // return as-is
else
return noLinearTime; // invalid conversion, can't return a value
}
}
// return target context as actual context
if (aActualContext) *aActualContext = aTargetContext;
// return timestamp, possibly truncated to date-only or time-only if requested
if (TCTX_IS_DATEONLY(aTargetContext))
return lineartime2dateonlyTime(ts);
else if (TCTX_IS_TIMEONLY(aTargetContext))
return lineartime2timeonly(ts);
else
return ts; // date and time
} // TTimestampField::getTimestampAs
/// @brief get timestamp as ISO8601 string.
/// @param aISOString[out] : timestamp in ISO8601 format
/// @param aTargetContext[in] : requests output context for timestamp. Use TCTX_UNKNOWN to show timestamp (or dateonly or timeonly) as-is.
/// @param aShowWithZ[in] : if set and timezone is UTC, value will be shown with "Z" appended
/// @param aShowWithZone[in] : if set and timestamp is not floating, zone offset will be appended in +-xx:xx form
/// @param aExtFormat[in] : if set, ISO8601 extended format is used
/// @param aWithFracSecs[in] : if set, fractions of seconds will be shown (millisecond resolution)
void TTimestampField::getAsISO8601(string &aISOString, timecontext_t aTargetContext, bool aShowWithZ, bool aShowWithZone, bool aExtFormat, bool aWithFracSecs)
{
// get time in requested context
lineartime_t ts = getTimestampAs(aTargetContext);
// if target context was set to TCTX_UNKNOWN, this means that we want to use the stored context
if (aTargetContext==TCTX_UNKNOWN) // really explicitly TCTX_UNKNOWN without any rendering flags!
aTargetContext = fTimecontext;
// return empty string if no timestamp or conversion impossible (but show empty duration as such!)
if (ts==noLinearTime && !TCTX_IS_DURATION(fTimecontext)) {
aISOString.erase();
return;
}
// check if time zone should be included or not
if (!TCTX_IS_UNKNOWN(aTargetContext)) {
if (
TCTX_IS_UTC(aTargetContext) ?
!aShowWithZ :
!aShowWithZone
) {
// prevent showing with time zone
aTargetContext = TCTX_JOIN_RFLAGS_TZ(aTargetContext,TCTX_UNKNOWN);
}
}
// now convert
TimestampToISO8601Str(aISOString, ts, aTargetContext, aExtFormat, aWithFracSecs);
} // TTimestampField::getAsISO8601
/// @brief move timestamp to specified context (i.e. convert the timestamp value from current to
/// specified context). Floating timestamps cannot and will not be moved.
/// @param aNewcontext[in] : context to move timestamp to.
/// timestamp will be converted to represent the same point in time in the new context
/// @param aSetUnmovables : if set, non-movable timestamps will be just assigned the new context,
// that is floating timestamps will be bound to specified context or
// non-floating timestamps will be made floating if new context is TCTX_UNKNOWN
bool TTimestampField::moveToContext(timecontext_t aNewcontext, bool aSetUnmovables)
{
bool ok=true;
if (isFloating() || fTimestamp==noLinearTime) {
// floating and empty timestamp cannot be moved
if (aSetUnmovables) {
// bind floating timestamp to specified zone
fTimecontext = aNewcontext;
}
else
ok=false; // floating/empty timestamps can only be fixed, not moved
}
else {
// timestamp is not floating or empty
if (TCTX_IS_UNKNOWN(aNewcontext)) {
if (aSetUnmovables)
fTimecontext = aNewcontext; // make floating
else
ok = false;
}
else {
// new context is not floating and old context isn't, either -> convert
ok = TzConvertTimestamp(fTimestamp, fTimecontext, aNewcontext, fGZonesP);
}
}
if (ok)
fTimecontext = aNewcontext; // we are now in the new context
return ok;
} // TTimestampField::moveToContext
/// @brief set timestamp from ISO8601 string.
/// @return true if successful
/// @param aISOString[in] : timestamp in ISO8601 basic or extended format, optionally including Z or +xx:xx zone specifier
/// @param aDefaultContext[in] : timezone context to use when ISO8601 does not specify a zone context or when aIgnoreZone is true
/// @param aIgnoreZone[in] : if set, timezone specification contained in ISO8601 is ignored. Resulting time context will be aDefaultContext (or TCTX_UNKNOWN for date-only)
bool TTimestampField::setAsISO8601(cAppCharP aISOString, timecontext_t aDefaultContext, bool aIgnoreZone)
{
bool ok = ISO8601StrToTimestamp(aISOString, fTimestamp, fTimecontext);
if (ok) {
fAssigned=true;
// if timestamp has unknown zone because it is a date-only or a duration, do NOT assign the default zone!
if (aIgnoreZone || (TCTX_IS_UNKNOWN(fTimecontext) && !TCTX_IS_DATEONLY(fTimecontext) && !TCTX_IS_DURATION(fTimecontext))) {
// set default context
fTimecontext = aDefaultContext;
}
}
else
assignEmpty();
// in all cases, this is now assigned (but probably empty if string was not ok)
return ok;
} // TTimestampField::setAsISO8601
#ifdef EMAIL_FORMAT_SUPPORT
static cAppCharP const rfc822_weekdays[7] = {
"Sun","Mon","Tue","Wed","Thu","Fri","Sat"
};
const size_t rfc822maxMonthLen = 3;
static cAppCharP const rfc822_months[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/// @brief get timestamp as RFC(2)822 style date
/// @param aRFC822String[out] : timestamp in RFC(2)822 format
/// @param aTargetContext[in] : requests output context for timestamp. Use TCTX_UNKNOWN to show timestamp as-is.
/// @param aShowWithZone[in] : if set and timestamp is not floating, zone offset will be shown
void TTimestampField::getAsRFC822date(string &aRFC822String, timecontext_t aTargetContext, bool aShowWithZone)
{
// get time in requested context
lineartime_t ts = getTimestampAs(aTargetContext);
// if target context was set to TCTX_UNKNOWN, this means that we want to use the stored context
if (TCTX_IS_UNKNOWN(aTargetContext))
aTargetContext = fTimecontext;
// get elements
sInt16 y,mo,d,h,mi,s,ms,w, moffs;
lineartime2date(ts,&y,&mo,&d);
lineartime2time(ts,&h,&mi,&s,&ms);
w = lineartime2weekday(ts);
// now print
StringObjPrintf(
aRFC822String,
"%s, %02hd %s %04hd %02hd:%02hd:%02hd",
rfc822_weekdays[w],
d,rfc822_months[mo-1],y,
h,mi,s
);
if (aShowWithZone && !TCTX_IS_UNKNOWN(aTargetContext)) {
// get offset
TzResolveToOffset(aTargetContext, moffs, ts, false, fGZonesP);
StringObjAppendPrintf(
aRFC822String,
" %c%02hd%02hd",
moffs>=0 ? '+' : '-',
(uInt16)(abs((int)moffs) / MinsPerHour),
(uInt16)(abs((int)moffs) % MinsPerHour)
);
}
} // TTimestampField::getAsRFC822date
/// @brief set timestamp as RFC(2)822 style date
/// @return true if successful
/// @param aRFC822String[in] : timestamp in RFC(2)822 format
/// @param aDefaultContext[in] : timezone context to use when RFC822 date does not specify a time zone
/// @param aIgnoreZone[in] : if set, timezone specification contained in input string is ignored. Resulting time context will be aDefaultContext
bool TTimestampField::setAsRFC822date(cAppCharP aRFC822String, timecontext_t aDefaultContext, bool aIgnoreZone)
{
cAppCharP p;
size_t inpSiz = strlen(aRFC822String);
cAppCharP eot=aRFC822String+inpSiz;
sInt16 minoffs=0;
// check for weekday
p = (cAppCharP) memchr(aRFC822String,',',inpSiz);
if (p)
p++; // skip comma
else
p=aRFC822String; // start at beginning
// scan elements
char month[rfc822maxMonthLen+1];
sInt16 y,mo,d,h,m,s;
s=0; // optional second
// scan day
while (p<eot && *p==' ') p++;
if (p+2>eot) return false; // string too short
p+=StrToShort(p,d,2);
// scan month
while (p<eot && *p==' ') p++;
uInt8 mi=0;
while (p<eot && *p!=' ' && mi<rfc822maxMonthLen) month[mi++]=*p++;
month[mi]=0;
// scan year
while (p<eot && *p==' ') p++;
if (p+4>eot) return false; // string too short
p+=StrToShort(p,y,4);
// scan time
while (p<eot && *p==' ') p++;
if (p+2>eot) return false; // string too short
p+=StrToShort(p,h,2);
if (p+3>eot || *p++ !=':') return false; // one for colon, two for minutes
p+=StrToShort(p,m,2);
if (p+3<=eot && *p==':') { // one for colon, two for minutes
// optional second
p++;
p+=StrToShort(p,s,2);
}
// convert month
for (mo=0; mo<12; mo++) if (strucmp(month,rfc822_months[mo])==0) break;
if (mo==12) return false; // bad format
mo++; // make month number
// make timestamp
fTimestamp = date2lineartime(y,mo,d) + time2lineartime(h,m,s,0);
// check time zone
bool neg=false;
while (p<eot && *p==' ') p++;
if (!aIgnoreZone) {
aIgnoreZone=true; // assume invalid zone spec
// check for numeric time zone
if (*p=='-' || *p=='+') {
// there is a timezone and we don't want to ignore it
if (*p=='-') neg=true;
if (p>=eot) return false; // string too short
p++;
sInt16 tzh,tzm;
// scan timezone
if (
p+4<=eot &&
StrToShort(p,tzh,2)==2 &&
StrToShort(p+2,tzm,2)==2
)
{
p+=4;
// set time zone
minoffs=((tzh*MinsPerHour)+tzm);
if (neg) minoffs = -minoffs;
// make zone
fTimecontext = TCTX_OFFSCONTEXT(minoffs);
aIgnoreZone=false; // zone spec valid
}
}
else if (isalpha(*p)) {
// could be time zone name, internal or olson name (if not, ignore zone spec)
aIgnoreZone = !TimeZoneNameToContext(p,fTimecontext,fGZonesP,true);
}
}
// if no valid zone, use default
if (aIgnoreZone) {
// assume default time
fTimecontext = aDefaultContext;
}
return true;
} // RFC822dateToTimeStamp
#endif // EMAIL_FORMAT_SUPPORT
/* end of TTimestampField implementation */
/*
* Implementation of TDateField
*/
TDateField::TDateField(GZones *aGZonesP) :
TTimestampField(aGZonesP)
{
fTimecontext |= TCTX_DATEONLY;
} // TDateField::TDateField
TDateField::~TDateField()
{
} // TDateField::~TDateField
// compare: returns 0 if equal, 1 if this > aItem, -1 if this < aItem,
// SYSYNC_NOT_COMPARABLE if not equal and no ordering known or if field
// types do not match.
// Note: ordering may NOT be age relevant; it just means that an ordering
// for this field type exists.
// Note: Both fields must be assigned. NO TEST IS DONE HERE!
sInt16 TDateField::compareWith(TItemField &aItemField, bool aCaseInsensitive)
{
sInt16 res;
// check comparison with non-date
if (!aItemField.isBasedOn(fty_timestamp))
return TTimestampField::compareWith(aItemField); // handles all comparisons with other types
// date comparison is context independent
TTimestampField *sfP = static_cast<TTimestampField *>(&aItemField);
// compare possible, return strcmp-type result
// - convert to date only
lineardate_t a = getTimestampAs(TCTX_UNKNOWN+TCTX_DATEONLY) / linearDateToTimeFactor;
lineardate_t b = sfP->getTimestampAs(TCTX_UNKNOWN+TCTX_DATEONLY) / linearDateToTimeFactor;
// - compare dates
res=a==b ? 0 : (a>b ? 1 : -1);
if (res!=0) {
// greater/less-type result is valid only if both sides are not empty
if ( isEmpty()) return SYSYNC_NOT_COMPARABLE; // empty value, cannot compare
if (sfP->isEmpty()) return SYSYNC_NOT_COMPARABLE; // empty value, cannot compare
}
return res;
} // TDateField::compareWith
// get date as ISO8601 string (default representation)
void TDateField::getAsString(string &aString)
{
// use default rendering
// - show with current context, but always date-only, no Z or zone info
inherited::getAsISO8601(aString,TCTX_UNKNOWN+TCTX_DATEONLY,false,false,false,false);
} // TDateField::getAsString
// set date from ISO8601 string (default interpretation)
void TDateField::setAsString(cAppCharP aString)
{
// default interpretation is floating date
// - parse timestamp, ignore timezone
if (setAsISO8601(aString, TCTX_UNKNOWN+TCTX_DATEONLY, true)) {
// truncate to date-only
fTimestamp = lineartime2dateonlyTime(fTimestamp);
}
// in all cases, this is now assigned (but probably empty)
fAssigned=true;
} // TDateField::setAsString
/* end of TDateField implementation */
/*
* Implementation of TIntegerField
*/
TIntegerField::TIntegerField()
{
fInteger=0;
fEmpty=true;
} // TIntegerField::TIntegerField
TIntegerField::~TIntegerField()
{
} // TIntegerField::~TIntegerField
#if defined(CHECKSUM_CHANGELOG) && !defined(RECORDHASH_FROM_DBAPI)
// changelog support: calculate CRC over contents
uInt16 TIntegerField::getDataCRC(uInt16 crc)
{
// CRC over integer number
return sysync_crc16_block(&fInteger,sizeof(fInteger),crc);
} // TIntegerField::getDataCRC
#endif
// assignment
TItemField& TIntegerField::operator=(TItemField &aItemField)
{
// assignment of empty (could be EMPTY or UNASSIGNED) must be handled by base class
if (aItemField.isEmpty()) return TItemField::operator=(aItemField);
// handle non-empty myself
if (aItemField.isBasedOn(fty_integer)) {
const TIntegerField *sfP = static_cast<const TIntegerField *>(&aItemField);
fInteger=sfP->fInteger;
fEmpty=sfP->fEmpty;
fAssigned=sfP->fAssigned;
}
else if (aItemField.getCalcType()==fty_integer) {
fInteger=aItemField.getAsInteger();
fEmpty=false;
fAssigned=aItemField.isAssigned();
}
else
TItemField::operator=(aItemField); // generic cross-type assignment (via string)
return *this;
} // TIntegerField::operator=
// compare: returns 0 if equal, 1 if this > aItem, -1 if this < aItem,
// SYSYNC_NOT_COMPARABLE if not equal and no ordering known or if field
// types do not match.
// Note: ordering may NOT be age relevant; it just means that an ordering
// for this field type exists.
// Notes: - Both fields must be assigned. NO TEST IS DONE HERE!
// - emptyness is not taken into account (empty counts as 0 here)
sInt16 TIntegerField::compareWith(TItemField &aItemField, bool aCaseInsensitive)
{
if (aItemField.getCalcType()!=fty_integer) return TItemField::compareWith(aItemField); // handles generic comparisons
fieldinteger_t otherInt = aItemField.getAsInteger();
// compare possible, return strcmp-type result
return fInteger==otherInt ? 0 : (fInteger>otherInt ? 1 : -1);
} // TIntegerField::compareWith
// get integer as integer
fieldinteger_t TIntegerField::getAsInteger(void)
{
return fInteger;
} // TIntegerField::getAsInteger
// set integer
void TIntegerField::setAsInteger(fieldinteger_t aInteger)
{
fInteger=aInteger;
fAssigned=true;
fEmpty=false;
} // TIntegerField::setAsInteger
// set integer from numeric string
void TIntegerField::setAsString(cAppCharP aString)
{
// check for hex
bool ok;
if (strucmp(aString,"0x",2)==0) {
// hex number
#ifndef NO64BITINT
ok = HexStrToULongLong(aString+2,*((uInt64 *)&fInteger));
#else
ok = HexStrToULong(aString+2,*((uInt32 *)&fInteger));
#endif
}
else {
// decimal integer number
#ifndef NO64BITINT
ok = StrToLongLong(aString,fInteger);
#else
ok = StrToLong(aString,fInteger);
#endif
}
if (!ok)
fInteger=0; // defined value
// setting with a non-integer value makes the integer empty (but having integer value 0)
fEmpty = !ok;
// in all cases, this is now assigned (but probably with the default=0)
fAssigned=true;
} // TIntegerField::setAsString
// get integer as numeric string
void TIntegerField::getAsString(string &aString)
{
if (fEmpty)
aString.erase();
else {
#ifndef NO64BITINT
LONGLONGTOSTR(aString,PRINTF_LLD_ARG(fInteger));
#else
StringObjPrintf(aString,"%ld",(sInt32)fInteger);
#endif
}
} // TIntegerField::getAsString
/* end of TIntegerField implementation */
// factory function
TItemField *newItemField(const TItemFieldTypes aType, GZones *aGZonesP, bool aAsArray)
{
#ifdef ARRAYFIELD_SUPPORT
if (aAsArray) {
return new TArrayField(aType,aGZonesP);
}
else
#endif
{
switch (aType) {
case fty_string: return new TStringField;
case fty_telephone: return new TTelephoneField;
case fty_integer: return new TIntegerField;
case fty_timestamp: return new TTimestampField(aGZonesP);
case fty_date: return new TDateField(aGZonesP);
case fty_url: return new TURLField;
case fty_multiline: return new TMultilineField;
case fty_blob: return new TBlobField;
case fty_none: return new TItemField; // base class, can represent EMPTY and UNASSIGNED
default: return NULL;
}
}
} // newItemField
#ifdef ENGINEINTERFACE_SUPPORT
// TItemFieldKey
// =============
// get value's ID (e.g. internal index)
sInt32 TItemFieldKey::GetValueID(cAppCharP aName)
{
// check special suffixes
size_t namsz = strlen(aName);
sInt32 fldID = 0; // basic
if (namsz >= strlen(VALSUFF_TZNAME) &&
strucmp(aName+namsz-strlen(VALSUFF_TZNAME),VALSUFF_TZNAME)==0) {
// time zone name as string requested
namsz-=7;
fldID += VALID_FLAG_TZNAME;
}
else if (namsz >= strlen(VALSUFF_TZOFFS) &&
strucmp(aName+namsz-strlen(VALSUFF_TZOFFS),VALSUFF_TZOFFS)==0) {
// time zone offset in minutes
namsz-=7;
fldID += VALID_FLAG_TZOFFS;
}
else if (namsz >= strlen(VALSUFF_NORM) &&
strucmp(aName+namsz-strlen(VALSUFF_NORM),VALSUFF_NORM)==0) {
// normalized value
namsz-=5;
fldID += VALID_FLAG_NORM;
}
else if (namsz >= strlen(VALSUFF_ARRSZ) &&
strucmp(aName+namsz-strlen(VALSUFF_ARRSZ),VALSUFF_ARRSZ)==0) {
// array size
namsz-=10;
fldID += VALID_FLAG_ARRSIZ;
}
else if (namsz >= strlen(VALSUFF_NAME) &&
strucmp(aName+namsz-strlen(VALSUFF_NAME),VALSUFF_NAME)==0) {
// value name
namsz-=8;
fldID += VALID_FLAG_VALNAME;
}
else if (namsz >= strlen(VALSUFF_TYPE) &&
strucmp(aName+namsz-strlen(VALSUFF_TYPE),VALSUFF_TYPE)==0) {
// value type
namsz-=8;
fldID += VALID_FLAG_VALTYPE;
}
// check if this is only a query for the flags alone
if (strucmp(aName,VALNAME_FLAG,namsz)==0)
return fldID; // return flags alone
// get fid for given name
sInt16 fid = getFidFor(aName,namsz);
if (fid==VARIDX_UNDEFINED)
return KEYVAL_ID_UNKNOWN; // unknown field
else
return fldID + (sInt32)((uInt16)fid); // return FID in lo word, flags in highword
} // TItemFieldKey::GetValueID
// get value's native type
uInt16 TItemFieldKey::GetValueType(sInt32 aID)
{
// fid is lower 16 bits of aID (and gets negative if bit15 of aID is set)
sInt16 fid;
*((uInt16 *)(&fid)) = aID & VALID_MASK_FID; // assign without sign extension
// now get base field
TItemField *baseFieldP = getBaseFieldFromFid(fid);
if (baseFieldP) {
// field exists
// - check special queries
if (aID & VALID_FLAG_ARRSIZ)
return VALTYPE_INT16; // size is a 16-bit integer, regardless of field type
else if (aID & VALID_FLAG_VALNAME)
return VALTYPE_TEXT; // name of the value is text
else if (aID & VALID_FLAG_VALTYPE)
return VALTYPE_INT16; // VALTYPE_XXX are 16-bit integers
else if (aID & VALID_FLAG_NORM)
return VALTYPE_TEXT; // normalized value is text
// - otherwise, valtype depends on field type
TItemFieldTypes fty = baseFieldP->getElementType();
// return native type
switch (fty) {
case fty_none:
return VALTYPE_UNKNOWN;
case fty_blob:
return VALTYPE_BUF;
case fty_string:
case fty_multiline:
case fty_telephone:
case fty_url:
return VALTYPE_TEXT;
case fty_integer:
return VALTYPE_INT64;
case fty_timestamp:
case fty_date:
if (aID & VALID_FLAG_TZNAME)
return VALTYPE_TEXT; // time zone name is a text
else if (aID & VALID_FLAG_TZOFFS)
return VALTYPE_INT16; // minute offset is 16-bit integer
else
return VALTYPE_TIME64; // time values
case numFieldTypes:
// invalid
break;
} // switch
}
// no field, no type
return VALTYPE_UNKNOWN;
} // TItemFieldKey::GetValueType
// helper for getting a leaf field pointer (interpretation of aRepOffset depends on array field or not)
TItemField *TItemFieldKey::getFieldFromFid(sInt16 aFid, sInt16 aRepOffset, bool aExistingOnly)
{
TItemField *fieldP=NULL;
// get field (or base field)
#ifdef ARRAYFIELD_SUPPORT
fieldP = getBaseFieldFromFid(aFid);
if (!fieldP) return NULL; // no field
if (fieldP->isArray()) {
// use aRepOffset as array index
fieldP = fieldP->getArrayField(aRepOffset,aExistingOnly);
}
else
#endif
{
// use aRepOffset as fid offset
#ifdef SCRIPT_SUPPORT
if (aFid<0) aRepOffset=0; // locals are never offset
#endif
fieldP = getBaseFieldFromFid(aFid+aRepOffset);
}
return fieldP;
} // TItemFieldKey::getFieldFromFid
// get value
TSyError TItemFieldKey::GetValueInternal(
sInt32 aID, sInt32 aArrayIndex,
appPointer aBuffer, memSize aBufSize, memSize &aValSize
)
{
string sval;
TItemField *fieldP;
// fid is lower 16 bits of aID (and gets negative if bit15 of aID is set)
sInt16 fid;
*((uInt16 *)(&fid)) = aID & VALID_MASK_FID; // assign without sign extension
// check for special queries not actually accessing a leaf field
if (aID & VALID_FLAG_ARRSIZ) {
// return array size (returns DB_NotFound for non-arrays)
fieldP = getBaseFieldFromFid(fid);
// if not an array, return DB_NotFound
if (!fieldP || !fieldP->isArray())
return DB_NotFound; // there is no array size for non-arrays
// return size
aValSize=sizeof(sInt16);
// value if enough room
if (aBufSize>=aValSize)
*((sInt16 *)aBuffer)=fieldP->arraySize();
// ok
return LOCERR_OK;
}
else if (aID & VALID_FLAG_VALNAME) {
// only return name of field
if (!getFieldNameFromFid(fid,sval)) return DB_NotFound;
if (aBufSize>=sval.size())
memcpy(aBuffer,sval.c_str(),sval.size()); // copy name data
aValSize=sval.size();
return LOCERR_OK;
}
else if (aID & VALID_FLAG_VALTYPE) {
// only return VALTYPE of field
uInt16 valtype = GetValueType(aID & ~VALID_FLAG_VALTYPE);
aValSize = sizeof(valtype);
if (aBufSize>=aValSize)
memcpy(aBuffer,&valtype,aValSize); // copy valtype uInt16
return LOCERR_OK;
}
// Get actual data - we need the leaf field (unless we pass <0 for aArrayIndex, which accesses the array base field)
fieldP = getFieldFromFid(fid, aArrayIndex, true); // existing array elements only
if (!fieldP) {
// array instance does not exist
return LOCERR_OUTOFRANGE;
}
else {
// leaf field (or explicitly requested array base field) exists
if (!fieldP->isAssigned()) return DB_NotFound; // no content found because none assigned
if (fieldP->isEmpty()) return DB_NoContent; // empty
if (fieldP->isArray()) return LOCERR_OUTOFRANGE; // no real access for array base field possible - if we get here it means non-empty
// assigned and not empty, return actual value
TItemFieldTypes fty = fieldP->getType();
appPointer valPtr = NULL;
size_t sz;
fieldinteger_t intVal;
lineartime_t ts;
timecontext_t tctx;
sInt16 minOffs;
TTimestampField *tsFldP;
if (aID & VALID_FLAG_NORM) {
// for all field types: get normalized string value
fieldP->getAsNormalizedString(sval);
aValSize = sval.size();
valPtr = (appPointer)sval.c_str();
}
else {
switch (fty) {
case fty_timestamp:
case fty_date:
tsFldP = static_cast<TTimestampField *>(fieldP);
if (aID & VALID_FLAG_TZNAME) {
// time zone name is a text
sval.erase(); // no zone
tctx = tsFldP->getTimeContext();
if (!TCTX_IS_UNKNOWN(tctx) || TCTX_IS_DURATION(tctx) || TCTX_IS_DATEONLY(tctx)) {
// has a zone (or is duration/dateonly), get name
TimeZoneContextToName(tctx, sval, tsFldP->getGZones());
}
aValSize = sval.size();
valPtr = (appPointer)sval.c_str();
}
else if (aID & VALID_FLAG_TZOFFS) {
// minute offset as 16-bit integer
aValSize = sizeof(sInt16);
if (
tsFldP->isFloating() ||
!TzResolveToOffset(tsFldP->getTimeContext(), minOffs, tsFldP->getTimestampAs(TCTX_UNKNOWN), false, tsFldP->getGZones())
)
return DB_NotFound; // cannot get minute offset or floating timestamp -> no result
// return minute offset
valPtr = &minOffs;
}
else {
// return timestamp itself (either as-is or in UTC, if TMODE_FLAG_FLOATING not set)
aValSize = sizeof(lineartime_t);
if (fTimeMode & TMODE_FLAG_FLOATING)
ts = tsFldP->getTimestampAs(TCTX_UNKNOWN); // as-is
else
ts = tsFldP->getTimestampAs(TCTX_UTC,&tctx); // always as UTC, will be converted to SYSTEM possibly by caller
// return timestamp
valPtr = &ts;
}
break;
case fty_integer:
aValSize = sizeof(fieldinteger_t);
intVal = fieldP->getAsInteger();
valPtr = &intVal;
break;
case fty_none:
aValSize = 0;
break;
case fty_blob:
static_cast<TBlobField *>(fieldP)->getBlobDataPtrSz(valPtr,sz);
aValSize = sz;
break;
case fty_string:
case fty_multiline:
case fty_telephone:
case fty_url:
aValSize = fieldP->getStringSize();
valPtr = (appPointer)static_cast<TStringField *>(fieldP)->getCStr();
break;
case numFieldTypes:
// invalid
break;
} // switch
}
// now we have valid aValSize and valPtr
if (aBuffer && aBufSize>=aValSize) {
// copy value data
memcpy(aBuffer,valPtr,aValSize);
}
}
// ok
return LOCERR_OK;
} // TItemFieldKey::GetValueInternal
// set value
TSyError TItemFieldKey::SetValueInternal(
sInt32 aID, sInt32 aArrayIndex,
cAppPointer aBuffer, memSize aValSize
)
{
TItemField *fieldP;
// fid is lower 16 bits of aID (and gets negative if bit15 of aID is set)
sInt16 fid;
*((uInt16 *)(&fid)) = aID & VALID_MASK_FID; // assign without sign extension
// check for special array size query
if (aID & VALID_FLAG_ARRSIZ) {
// array size is not writable
return DB_NotAllowed;
}
// now get leaf field
fieldP = getFieldFromFid(fid, aArrayIndex, false); // create element if needed
if (!fieldP) {
// should not happen
return DB_Error;
}
else {
// leaf field exists, set value
fWritten = true;
TItemFieldTypes fty = fieldP->getType();
fieldinteger_t intVal;
lineartime_t ts;
timecontext_t tctx;
sInt16 minOffs;
string sval;
TTimestampField *tsFldP;
// treat setting normalized value like setting as string
if (aID & VALID_FLAG_NORM)
fty = fty_string; // treat like string
// handle NULL (empty) case
if (aBuffer==0) {
// buffer==NULL means NULL value
fieldP->assignEmpty();
return LOCERR_OK;
}
// now handle according to type
switch (fty) {
case fty_timestamp:
case fty_date:
tsFldP = static_cast<TTimestampField *>(fieldP);
if (aID & VALID_FLAG_TZNAME) {
// set time zone by name
sval.assign((cAppCharP)aBuffer,aValSize);
tctx = TCTX_UNKNOWN;
if (!sval.empty()) {
// convert (internal or olson names allowed)
if (!TimeZoneNameToContext(sval.c_str(), tctx, tsFldP->getGZones(), true))
return LOCERR_BADPARAM; // bad timezone name
}
// set context
tsFldP->setTimeContext(tctx);
}
else if (aID & VALID_FLAG_TZOFFS) {
// minute offset as 16-bit integer
minOffs = *((sInt16 *)aBuffer);
// set context
tsFldP->setTimeContext(TCTX_MINOFFSET(minOffs));
}
else {
// set timestamp itself (either as-is or in UTC, if TMODE_FLAG_FLOATING not set)
ts = *((lineartime_t *)aBuffer);
if ((fTimeMode & TMODE_FLAG_FLOATING)==0)
tsFldP->setTimestampAndContext(ts,TCTX_UTC); // incoming timestamp is UTC, set it as such
else
tsFldP->setTimestamp(ts); // just set timestamp as-is and don't touch rest
}
break;
case fty_integer:
intVal = *((fieldinteger_t *)aBuffer);
fieldP->setAsInteger(intVal);
break;
case fty_blob:
static_cast<TBlobField *>(fieldP)->setBlobDataPtrSz((void *)aBuffer,aValSize);
break;
case fty_string:
case fty_multiline:
case fty_telephone:
case fty_url:
fieldP->setAsString((cAppCharP)aBuffer,aValSize);
break;
case fty_none:
case numFieldTypes:
// invalid
break;
} // switch
}
// ok
return LOCERR_OK;
} // TItemFieldKey::SetValueInternal
#endif // ENGINEINTERFACE_SUPPORT
} // namespace sysync
// eof
|