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
|
/*
* tclClockFmt.c --
*
* Contains the date format (and scan) routines. This code is back-ported
* from the time and date facilities of tclSE engine, by Serg G. Brester.
*
* Copyright (c) 2015 by Sergey G. Brester aka sebres. All rights reserved.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tclInt.h"
#include "tclStrIdxTree.h"
#include "tclDate.h"
/*
* Miscellaneous forward declarations and functions used within this file
*/
static void
ClockFmtObj_DupInternalRep(Tcl_Obj *srcPtr, Tcl_Obj *copyPtr);
static void
ClockFmtObj_FreeInternalRep(Tcl_Obj *objPtr);
static int
ClockFmtObj_SetFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr);
static void
ClockFmtObj_UpdateString(Tcl_Obj *objPtr);
TCL_DECLARE_MUTEX(ClockFmtMutex); /* Serializes access to common format list. */
static void ClockFmtScnStorageDelete(ClockFmtScnStorage *fss);
static void ClockFrmScnFinalize(ClientData clientData);
/* Msgcat index literals prefixed with _IDX_, used for quick dictionary search */
CLOCK_LOCALE_LITERAL_ARRAY(MsgCtLitIdxs, "_IDX_");
/*
* Clock scan and format facilities.
*/
inline int
_str2int(
time_t *out,
register
const char *p,
const char *e,
int sign)
{
register time_t val = 0, prev = 0;
if (sign > 0) {
while (p < e) {
val = val * 10 + (*p++ - '0');
if (val < prev) {
return TCL_ERROR;
}
prev = val;
}
} else {
while (p < e) {
val = val * 10 - (*p++ - '0');
if (val > prev) {
return TCL_ERROR;
}
prev = val;
}
}
*out = val;
return TCL_OK;
}
inline int
_str2wideInt(
Tcl_WideInt *out,
register
const char *p,
const char *e,
int sign)
{
register Tcl_WideInt val = 0, prev = 0;
if (sign > 0) {
while (p < e) {
val = val * 10 + (*p++ - '0');
if (val < prev) {
return TCL_ERROR;
}
prev = val;
}
} else {
while (p < e) {
val = val * 10 - (*p++ - '0');
if (val > prev) {
return TCL_ERROR;
}
prev = val;
}
}
*out = val;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*/
#if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0
static struct {
ClockFmtScnStorage *stackPtr;
ClockFmtScnStorage *stackBound;
unsigned int count;
} ClockFmtScnStorage_GC = {NULL, NULL, 0};
inline void
ClockFmtScnStorageGC_In(ClockFmtScnStorage *entry)
{
/* add new entry */
TclSpliceIn(entry, ClockFmtScnStorage_GC.stackPtr);
if (ClockFmtScnStorage_GC.stackBound == NULL) {
ClockFmtScnStorage_GC.stackBound = entry;
}
ClockFmtScnStorage_GC.count++;
/* if GC ist full */
if (ClockFmtScnStorage_GC.count > CLOCK_FMT_SCN_STORAGE_GC_SIZE) {
/* GC stack is LIFO: delete first inserted entry */
ClockFmtScnStorage *delEnt = ClockFmtScnStorage_GC.stackBound;
ClockFmtScnStorage_GC.stackBound = delEnt->prevPtr;
TclSpliceOut(delEnt, ClockFmtScnStorage_GC.stackPtr);
ClockFmtScnStorage_GC.count--;
delEnt->prevPtr = delEnt->nextPtr = NULL;
/* remove it now */
ClockFmtScnStorageDelete(delEnt);
}
}
inline void
ClockFmtScnStorage_GC_Out(ClockFmtScnStorage *entry)
{
TclSpliceOut(entry, ClockFmtScnStorage_GC.stackPtr);
ClockFmtScnStorage_GC.count--;
if (ClockFmtScnStorage_GC.stackBound == entry) {
ClockFmtScnStorage_GC.stackBound = entry->prevPtr;
}
entry->prevPtr = entry->nextPtr = NULL;
}
#endif
/*
*----------------------------------------------------------------------
*/
static Tcl_HashTable FmtScnHashTable;
static int initialized = 0;
inline Tcl_HashEntry *
HashEntry4FmtScn(ClockFmtScnStorage *fss) {
return (Tcl_HashEntry*)(fss + 1);
};
inline ClockFmtScnStorage *
FmtScn4HashEntry(Tcl_HashEntry *hKeyPtr) {
return (ClockFmtScnStorage*)(((char*)hKeyPtr) - sizeof(ClockFmtScnStorage));
};
/*
* Format storage hash (list of formats shared across all threads).
*/
static Tcl_HashEntry *
ClockFmtScnStorageAllocProc(
Tcl_HashTable *tablePtr, /* Hash table. */
void *keyPtr) /* Key to store in the hash table entry. */
{
ClockFmtScnStorage *fss;
const char *string = (const char *) keyPtr;
Tcl_HashEntry *hPtr;
unsigned int size,
allocsize = sizeof(ClockFmtScnStorage) + sizeof(Tcl_HashEntry);
allocsize += (size = strlen(string) + 1);
if (size > sizeof(hPtr->key)) {
allocsize -= sizeof(hPtr->key);
}
fss = ckalloc(allocsize);
/* initialize */
memset(fss, 0, sizeof(*fss));
hPtr = HashEntry4FmtScn(fss);
memcpy(&hPtr->key.string, string, size);
hPtr->clientData = 0; /* currently unused */
return hPtr;
}
static void
ClockFmtScnStorageFreeProc(
Tcl_HashEntry *hPtr)
{
ClockFmtScnStorage *fss = FmtScn4HashEntry(hPtr);
if (fss->scnTok != NULL) {
ckfree(fss->scnTok);
fss->scnTok = NULL;
fss->scnTokC = 0;
}
if (fss->fmtTok != NULL) {
ckfree(fss->fmtTok);
fss->fmtTok = NULL;
fss->fmtTokC = 0;
}
ckfree(fss);
}
static void
ClockFmtScnStorageDelete(ClockFmtScnStorage *fss) {
Tcl_HashEntry *hPtr = HashEntry4FmtScn(fss);
/*
* This will delete a hash entry and call "ckfree" for storage self, if
* some additionally handling required, freeEntryProc can be used instead
*/
Tcl_DeleteHashEntry(hPtr);
}
/*
* Derivation of tclStringHashKeyType with another allocEntryProc
*/
static Tcl_HashKeyType ClockFmtScnStorageHashKeyType;
/*
* Type definition.
*/
Tcl_ObjType ClockFmtObjType = {
"clock-format", /* name */
ClockFmtObj_FreeInternalRep, /* freeIntRepProc */
ClockFmtObj_DupInternalRep, /* dupIntRepProc */
ClockFmtObj_UpdateString, /* updateStringProc */
ClockFmtObj_SetFromAny /* setFromAnyProc */
};
#define ObjClockFmtScn(objPtr) \
(*((ClockFmtScnStorage **)&(objPtr)->internalRep.twoPtrValue.ptr1))
#define ObjLocFmtKey(objPtr) \
(*((Tcl_Obj **)&(objPtr)->internalRep.twoPtrValue.ptr2))
/*
*----------------------------------------------------------------------
*/
static void
ClockFmtObj_DupInternalRep(srcPtr, copyPtr)
Tcl_Obj *srcPtr;
Tcl_Obj *copyPtr;
{
ClockFmtScnStorage *fss = ObjClockFmtScn(srcPtr);
if (fss != NULL) {
Tcl_MutexLock(&ClockFmtMutex);
fss->objRefCount++;
Tcl_MutexUnlock(&ClockFmtMutex);
}
ObjClockFmtScn(copyPtr) = fss;
/* regards special case - format not localizable */
if (ObjLocFmtKey(srcPtr) != srcPtr) {
Tcl_InitObjRef(ObjLocFmtKey(copyPtr), ObjLocFmtKey(srcPtr));
} else {
ObjLocFmtKey(copyPtr) = copyPtr;
}
copyPtr->typePtr = &ClockFmtObjType;
/* if no format representation, dup string representation */
if (fss == NULL) {
copyPtr->bytes = ckalloc(srcPtr->length + 1);
memcpy(copyPtr->bytes, srcPtr->bytes, srcPtr->length + 1);
copyPtr->length = srcPtr->length;
}
}
/*
*----------------------------------------------------------------------
*/
static void
ClockFmtObj_FreeInternalRep(objPtr)
Tcl_Obj *objPtr;
{
ClockFmtScnStorage *fss = ObjClockFmtScn(objPtr);
if (fss != NULL) {
Tcl_MutexLock(&ClockFmtMutex);
/* decrement object reference count of format/scan storage */
if (--fss->objRefCount <= 0) {
#if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0
/* don't remove it right now (may be reusable), just add to GC */
ClockFmtScnStorageGC_In(fss);
#else
/* remove storage (format representation) */
ClockFmtScnStorageDelete(fss);
#endif
}
Tcl_MutexUnlock(&ClockFmtMutex);
}
ObjClockFmtScn(objPtr) = NULL;
if (ObjLocFmtKey(objPtr) != objPtr) {
Tcl_UnsetObjRef(ObjLocFmtKey(objPtr));
} else {
ObjLocFmtKey(objPtr) = NULL;
}
objPtr->typePtr = NULL;
};
/*
*----------------------------------------------------------------------
*/
static int
ClockFmtObj_SetFromAny(interp, objPtr)
Tcl_Interp *interp;
Tcl_Obj *objPtr;
{
/* validate string representation before free old internal represenation */
(void)TclGetString(objPtr);
/* free old internal represenation */
if (objPtr->typePtr && objPtr->typePtr->freeIntRepProc)
objPtr->typePtr->freeIntRepProc(objPtr);
/* initial state of format object */
ObjClockFmtScn(objPtr) = NULL;
ObjLocFmtKey(objPtr) = NULL;
objPtr->typePtr = &ClockFmtObjType;
return TCL_OK;
};
/*
*----------------------------------------------------------------------
*/
static void
ClockFmtObj_UpdateString(objPtr)
Tcl_Obj *objPtr;
{
char *name = "UNKNOWN";
int len;
ClockFmtScnStorage *fss = ObjClockFmtScn(objPtr);
if (fss != NULL) {
Tcl_HashEntry *hPtr = HashEntry4FmtScn(fss);
name = hPtr->key.string;
}
len = strlen(name);
objPtr->length = len,
objPtr->bytes = ckalloc((size_t)++len);
if (objPtr->bytes)
memcpy(objPtr->bytes, name, len);
}
/*
*----------------------------------------------------------------------
*/
MODULE_SCOPE Tcl_Obj*
ClockFrmObjGetLocFmtKey(
Tcl_Interp *interp,
Tcl_Obj *objPtr)
{
Tcl_Obj *keyObj;
if (objPtr->typePtr != &ClockFmtObjType) {
if (ClockFmtObj_SetFromAny(interp, objPtr) != TCL_OK) {
return NULL;
}
}
keyObj = ObjLocFmtKey(objPtr);
if (keyObj) {
return keyObj;
}
keyObj = Tcl_ObjPrintf("FMT_%s", TclGetString(objPtr));
Tcl_InitObjRef(ObjLocFmtKey(objPtr), keyObj);
return keyObj;
}
/*
*----------------------------------------------------------------------
*/
static ClockFmtScnStorage *
FindOrCreateFmtScnStorage(
Tcl_Interp *interp,
Tcl_Obj *objPtr)
{
const char *strFmt = TclGetString(objPtr);
ClockFmtScnStorage *fss = NULL;
int new;
Tcl_HashEntry *hPtr;
Tcl_MutexLock(&ClockFmtMutex);
/* if not yet initialized */
if (!initialized) {
/* initialize type */
memcpy(&ClockFmtScnStorageHashKeyType, &tclStringHashKeyType, sizeof(tclStringHashKeyType));
ClockFmtScnStorageHashKeyType.allocEntryProc = ClockFmtScnStorageAllocProc;
ClockFmtScnStorageHashKeyType.freeEntryProc = ClockFmtScnStorageFreeProc;
/* initialize hash table */
Tcl_InitCustomHashTable(&FmtScnHashTable, TCL_CUSTOM_TYPE_KEYS,
&ClockFmtScnStorageHashKeyType);
initialized = 1;
Tcl_CreateExitHandler(ClockFrmScnFinalize, NULL);
}
/* get or create entry (and alocate storage) */
hPtr = Tcl_CreateHashEntry(&FmtScnHashTable, strFmt, &new);
if (hPtr != NULL) {
fss = FmtScn4HashEntry(hPtr);
#if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0
/* unlink if it is currently in GC */
if (new == 0 && fss->objRefCount == 0) {
ClockFmtScnStorage_GC_Out(fss);
}
#endif
/* new reference, so increment in lock right now */
fss->objRefCount++;
ObjClockFmtScn(objPtr) = fss;
}
Tcl_MutexUnlock(&ClockFmtMutex);
if (fss == NULL && interp != NULL) {
Tcl_AppendResult(interp, "retrieve clock format failed \"",
strFmt ? strFmt : "", "\"", NULL);
Tcl_SetErrorCode(interp, "TCL", "EINVAL", NULL);
}
return fss;
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetClockFrmScnFromObj --
*
* Returns a clock format/scan representation of (*objPtr), if possible.
* If something goes wrong, NULL is returned, and if interp is non-NULL,
* an error message is written there.
*
* Results:
* Valid representation of type ClockFmtScnStorage.
*
* Side effects:
* Caches the ClockFmtScnStorage reference as the internal rep of (*objPtr)
* and in global hash table, shared across all threads.
*
*----------------------------------------------------------------------
*/
ClockFmtScnStorage *
Tcl_GetClockFrmScnFromObj(
Tcl_Interp *interp,
Tcl_Obj *objPtr)
{
ClockFmtScnStorage *fss;
if (objPtr->typePtr != &ClockFmtObjType) {
if (ClockFmtObj_SetFromAny(interp, objPtr) != TCL_OK) {
return NULL;
}
}
fss = ObjClockFmtScn(objPtr);
if (fss == NULL) {
fss = FindOrCreateFmtScnStorage(interp, objPtr);
}
return fss;
}
/*
* DetermineGreedySearchLen --
*
* Determine min/max lengths as exact as possible (speed, greedy match)
*
*/
inline
void DetermineGreedySearchLen(ClockFmtScnCmdArgs *opts,
DateInfo *info, ClockScanToken *tok, int *minLen, int *maxLen)
{
register const char*p = yyInput;
*minLen = 0;
/* max length to the end regarding distance to end (min-width of following tokens) */
*maxLen = info->dateEnd - p - tok->endDistance;
/* if no tokens anymore */
if (!(tok+1)->map) {
/* should match to end or first space */
while (!isspace(UCHAR(*p)) && ++p < info->dateEnd) {};
*minLen = p - yyInput;
} else
/* next token is a word */
if ((tok+1)->map->type == CTOKT_WORD) {
/* should match at least to the first char of this word */
while (*p != *((tok+1)->tokWord.start) && ++p < info->dateEnd) {};
*minLen = p - yyInput;
}
if (*minLen > *maxLen) {
*maxLen = *minLen;
}
}
inline int
ObjListSearch(ClockFmtScnCmdArgs *opts,
DateInfo *info, int *val,
Tcl_Obj **lstv, int lstc,
int minLen, int maxLen)
{
int i, l, lf = -1;
const char *s;
/* search in list */
for (i = 0; i < lstc; i++) {
s = TclGetString(lstv[i]);
l = lstv[i]->length;
if ( l >= minLen && l <= maxLen
&& strncasecmp(yyInput, s, l) == 0
) {
/* found, try to find longest value (greedy search) */
if (l < maxLen && minLen != maxLen) {
lf = i;
minLen = l + 1;
continue;
}
*val = i;
yyInput += l;
break;
}
}
/* if found */
if (i < lstc) {
return TCL_OK;
}
if (lf >= 0) {
*val = lf;
yyInput += minLen - 1;
return TCL_OK;
}
return TCL_RETURN;
}
static int
LocaleListSearch(ClockFmtScnCmdArgs *opts,
DateInfo *info, int mcKey, int *val,
int minLen, int maxLen)
{
Tcl_Obj **lstv;
int lstc;
Tcl_Obj *valObj;
/* get msgcat value */
valObj = ClockMCGet(opts, mcKey);
if (valObj == NULL) {
return TCL_ERROR;
}
/* is a list */
if (TclListObjGetElements(opts->interp, valObj, &lstc, &lstv) != TCL_OK) {
return TCL_ERROR;
}
/* search in list */
return ObjListSearch(opts, info, val, lstv, lstc,
minLen, maxLen);
}
static TclStrIdxTree *
ClockMCGetListIdxTree(
ClockFmtScnCmdArgs *opts,
int mcKey)
{
TclStrIdxTree * idxTree;
Tcl_Obj *objPtr = ClockMCGetIdx(opts, mcKey);
if ( objPtr != NULL
&& (idxTree = TclStrIdxTreeGetFromObj(objPtr)) != NULL
) {
return idxTree;
} else {
/* build new index */
Tcl_Obj **lstv;
int lstc;
Tcl_Obj *valObj;
objPtr = TclStrIdxTreeNewObj();
if ((idxTree = TclStrIdxTreeGetFromObj(objPtr)) == NULL) {
goto done; /* unexpected, but ...*/
}
valObj = ClockMCGet(opts, mcKey);
if (valObj == NULL) {
goto done;
}
if (TclListObjGetElements(opts->interp, valObj,
&lstc, &lstv) != TCL_OK) {
goto done;
};
if (TclStrIdxTreeBuildFromList(idxTree, lstc, lstv) != TCL_OK) {
goto done;
}
ClockMCSetIdx(opts, mcKey, objPtr);
objPtr = NULL;
};
done:
if (objPtr) {
Tcl_DecrRefCount(objPtr);
idxTree = NULL;
}
return idxTree;
}
static TclStrIdxTree *
ClockMCGetMultiListIdxTree(
ClockFmtScnCmdArgs *opts,
int mcKey,
int *mcKeys)
{
TclStrIdxTree * idxTree;
Tcl_Obj *objPtr = ClockMCGetIdx(opts, mcKey);
if ( objPtr != NULL
&& (idxTree = TclStrIdxTreeGetFromObj(objPtr)) != NULL
) {
return idxTree;
} else {
/* build new index */
Tcl_Obj **lstv;
int lstc;
Tcl_Obj *valObj;
objPtr = TclStrIdxTreeNewObj();
if ((idxTree = TclStrIdxTreeGetFromObj(objPtr)) == NULL) {
goto done; /* unexpected, but ...*/
}
while (*mcKeys) {
valObj = ClockMCGet(opts, *mcKeys);
if (valObj == NULL) {
goto done;
}
if (TclListObjGetElements(opts->interp, valObj,
&lstc, &lstv) != TCL_OK) {
goto done;
};
if (TclStrIdxTreeBuildFromList(idxTree, lstc, lstv) != TCL_OK) {
goto done;
}
mcKeys++;
}
ClockMCSetIdx(opts, mcKey, objPtr);
};
done:
if (objPtr) {
Tcl_DecrRefCount(objPtr);
idxTree = NULL;
}
return idxTree;
}
inline int
ClockStrIdxTreeSearch(ClockFmtScnCmdArgs *opts,
DateInfo *info, TclStrIdxTree *idxTree, int *val,
int minLen, int maxLen)
{
const char *f;
TclStrIdx *foundItem;
f = TclStrIdxTreeSearch(NULL, &foundItem, idxTree,
yyInput, yyInput + maxLen);
if (f <= yyInput || (f - yyInput) < minLen) {
/* not found */
return TCL_RETURN;
}
if (foundItem->value == -1) {
/* ambigous */
return TCL_RETURN;
}
*val = foundItem->value;
/* shift input pointer */
yyInput = f;
return TCL_OK;
}
static int
StaticListSearch(ClockFmtScnCmdArgs *opts,
DateInfo *info, const char **lst, int *val)
{
int len;
const char **s = lst;
while (*s != NULL) {
len = strlen(*s);
if ( len <= info->dateEnd - yyInput
&& strncasecmp(yyInput, *s, len) == 0
) {
*val = (s - lst);
yyInput += len;
break;
}
s++;
}
if (*s != NULL) {
return TCL_OK;
}
return TCL_RETURN;
}
inline const char *
FindWordEnd(
ClockScanToken *tok,
register const char * p, const char * end)
{
register const char *x = tok->tokWord.start;
const char *pfnd;
if (x == tok->tokWord.end - 1) { /* fast phase-out for single char word */
if (*p == *x) {
return ++p;
}
}
/* multi-char word */
x = TclUtfFindEqualNC(x, tok->tokWord.end, p, end, &pfnd);
if (x < tok->tokWord.end) {
/* no match -> error */
return NULL;
}
return pfnd;
}
static int
ClockScnToken_Month_Proc(ClockFmtScnCmdArgs *opts,
DateInfo *info, ClockScanToken *tok)
{
/*
static const char * months[] = {
/* full * /
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
/* abbr * /
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
NULL
};
int val;
if (StaticListSearch(opts, info, months, &val) != TCL_OK) {
return TCL_RETURN;
}
yyMonth = (val % 12) + 1;
return TCL_OK;
*/
int ret, val;
int minLen, maxLen;
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
ret = LocaleListSearch(opts, info, MCLIT_MONTHS_FULL, &val,
minLen, maxLen);
if (ret != TCL_OK) {
/* if not found */
if (ret == TCL_RETURN) {
ret = LocaleListSearch(opts, info, MCLIT_MONTHS_ABBREV, &val,
minLen, maxLen);
}
if (ret != TCL_OK) {
return ret;
}
}
yyMonth = val + 1;
return TCL_OK;
}
static int
ClockScnToken_DayOfWeek_Proc(ClockFmtScnCmdArgs *opts,
DateInfo *info, ClockScanToken *tok)
{
int ret, val;
int minLen, maxLen;
char curTok = *tok->tokWord.start;
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
/* %u %w %Ou %Ow */
if ( curTok != 'a' && curTok != 'A'
&& ((minLen <= 1 && maxLen >= 1) || (int)tok->map->data)
) {
val = -1;
if (!(int)tok->map->data) {
if (*yyInput >= '0' && *yyInput <= '9') {
val = *yyInput - '0';
}
} else {
int ret = LocaleListSearch(opts, info, (int)tok->map->data, &val,
minLen, maxLen);
if (ret == TCL_ERROR) {
return ret;
}
}
if (val != -1) {
if (val == 0) {
val = 7;
}
if (val > 7 && curTok != 'a' && curTok != 'A') {
Tcl_SetResult(opts->interp, "day of week is greater than 7",
TCL_STATIC);
Tcl_SetErrorCode(opts->interp, "CLOCK", "badDayOfWeek", NULL);
return TCL_ERROR;
}
info->date.dayOfWeek = val;
yyInput++;
return TCL_OK;
}
return TCL_RETURN;
}
/* %a %A */
ret = LocaleListSearch(opts, info, MCLIT_DAYS_OF_WEEK_FULL, &val,
minLen, maxLen);
if (ret != TCL_OK) {
/* if not found */
if (ret == TCL_RETURN) {
ret = LocaleListSearch(opts, info, MCLIT_DAYS_OF_WEEK_ABBREV, &val,
minLen, maxLen);
}
if (ret != TCL_OK) {
return ret;
}
}
if (val == 0) {
val = 7;
}
info->date.dayOfWeek = val;
return TCL_OK;
}
static int
ClockScnToken_amPmInd_Proc(ClockFmtScnCmdArgs *opts,
DateInfo *info, ClockScanToken *tok)
{
int ret, val;
int minLen, maxLen;
Tcl_Obj *amPmObj[2];
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
amPmObj[0] = ClockMCGet(opts, MCLIT_AM);
amPmObj[1] = ClockMCGet(opts, MCLIT_PM);
if (amPmObj[0] == NULL || amPmObj[1] == NULL) {
return TCL_ERROR;
}
ret = ObjListSearch(opts, info, &val, amPmObj, 2,
minLen, maxLen);
if (ret != TCL_OK) {
return ret;
}
if (val == 0) {
yyMeridian = MERam;
} else {
yyMeridian = MERpm;
}
return TCL_OK;
}
static int
ClockScnToken_LocaleERA_Proc(ClockFmtScnCmdArgs *opts,
DateInfo *info, ClockScanToken *tok)
{
ClockClientData *dataPtr = opts->clientData;
int ret, val;
int minLen, maxLen;
Tcl_Obj *eraObj[6];
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
eraObj[0] = ClockMCGet(opts, MCLIT_BCE);
eraObj[1] = ClockMCGet(opts, MCLIT_CE);
eraObj[2] = dataPtr->mcLiterals[MCLIT_BCE2];
eraObj[3] = dataPtr->mcLiterals[MCLIT_CE2];
eraObj[4] = dataPtr->mcLiterals[MCLIT_BCE3];
eraObj[5] = dataPtr->mcLiterals[MCLIT_CE3];
if (eraObj[0] == NULL || eraObj[1] == NULL) {
return TCL_ERROR;
}
ret = ObjListSearch(opts, info, &val, eraObj, 6,
minLen, maxLen);
if (ret != TCL_OK) {
return ret;
}
if (val & 1) {
yydate.era = CE;
} else {
yydate.era = BCE;
}
return TCL_OK;
}
static int
ClockScnToken_LocaleListMatcher_Proc(ClockFmtScnCmdArgs *opts,
DateInfo *info, ClockScanToken *tok)
{
int ret, val;
int minLen, maxLen;
TclStrIdxTree *idxTree;
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
/* get or create tree in msgcat dict */
idxTree = ClockMCGetListIdxTree(opts, (int)tok->map->data /* mcKey */);
if (idxTree == NULL) {
return TCL_ERROR;
}
ret = ClockStrIdxTreeSearch(opts, info, idxTree, &val, minLen, maxLen);
if (ret != TCL_OK) {
return ret;
}
if (tok->map->offs > 0) {
*(time_t *)(((char *)info) + tok->map->offs) = val;
}
return TCL_OK;
}
static int
ClockScnToken_TimeZone_Proc(ClockFmtScnCmdArgs *opts,
DateInfo *info, ClockScanToken *tok)
{
int minLen, maxLen;
int len = 0;
register const char *p = yyInput;
Tcl_Obj *tzObjStor = NULL;
DetermineGreedySearchLen(opts, info, tok, &minLen, &maxLen);
/* numeric timezone */
if (*p == '+' || *p == '-') {
/* max chars in numeric zone = "+00:00:00" */
#define MAX_ZONE_LEN 9
char buf[MAX_ZONE_LEN + 1];
char *bp = buf;
*bp++ = *p++; len++;
if (maxLen > MAX_ZONE_LEN)
maxLen = MAX_ZONE_LEN;
/* cumulate zone into buf without ':' */
while (len + 1 < maxLen) {
if (!isdigit(UCHAR(*p))) break;
*bp++ = *p++; len++;
if (!isdigit(UCHAR(*p))) break;
*bp++ = *p++; len++;
if (len + 2 < maxLen) {
if (*p == ':') {
*p++; len++;
}
}
}
*bp = '\0';
if (len < minLen) {
return TCL_RETURN;
}
#undef MAX_ZONE_LEN
/* timezone */
tzObjStor = Tcl_NewStringObj(buf, bp-buf);
} else {
/* legacy (alnum) timezone like CEST, etc. */
if (maxLen > 4)
maxLen = 4;
while (len < maxLen) {
if ( (*p & 0x80)
|| (!isalpha(UCHAR(*p)) && !isdigit(UCHAR(*p)))
) { /* INTL: ISO only. */
break;
}
p++; len++;
}
if (len < minLen) {
return TCL_RETURN;
}
/* timezone */
tzObjStor = Tcl_NewStringObj(yyInput, p-yyInput);
/* convert using dict */
}
/* try to apply new time zone */
Tcl_IncrRefCount(tzObjStor);
opts->timezoneObj = ClockSetupTimeZone(opts->clientData, opts->interp,
tzObjStor);
Tcl_DecrRefCount(tzObjStor);
if (opts->timezoneObj == NULL) {
return TCL_ERROR;
}
yyInput += len;
return TCL_OK;
}
static const char *ScnSTokenMapIndex =
"dmbyYHMSpJjCgGVazs";
static ClockScanTokenMap ScnSTokenMap[] = {
/* %d %e */
{CTOKT_DIGIT, CLF_DAYOFMONTH, 0, 1, 2, TclOffset(DateInfo, date.dayOfMonth),
NULL},
/* %m */
{CTOKT_DIGIT, CLF_MONTH, 0, 1, 2, TclOffset(DateInfo, date.month),
NULL},
/* %b %B %h */
{CTOKT_PARSER, CLF_MONTH, 0, 0, 0, 0,
ClockScnToken_Month_Proc},
/* %y */
{CTOKT_DIGIT, CLF_YEAR, 0, 1, 2, TclOffset(DateInfo, date.year),
NULL},
/* %Y */
{CTOKT_DIGIT, CLF_YEAR | CLF_CENTURY, 0, 1, 4, TclOffset(DateInfo, date.year),
NULL},
/* %H %k %I %l */
{CTOKT_DIGIT, CLF_TIME, 0, 1, 2, TclOffset(DateInfo, date.hour),
NULL},
/* %M */
{CTOKT_DIGIT, CLF_TIME, 0, 1, 2, TclOffset(DateInfo, date.minutes),
NULL},
/* %S */
{CTOKT_DIGIT, CLF_TIME, 0, 1, 2, TclOffset(DateInfo, date.secondOfDay),
NULL},
/* %p %P */
{CTOKT_PARSER, CLF_ISO8601, 0, 0, 0, 0,
ClockScnToken_amPmInd_Proc, NULL},
/* %J */
{CTOKT_DIGIT, CLF_JULIANDAY, 0, 1, 0xffff, TclOffset(DateInfo, date.julianDay),
NULL},
/* %j */
{CTOKT_DIGIT, CLF_DAYOFYEAR, 0, 1, 3, TclOffset(DateInfo, date.dayOfYear),
NULL},
/* %C */
{CTOKT_DIGIT, CLF_CENTURY|CLF_ISO8601CENTURY, 0, 1, 2, TclOffset(DateInfo, dateCentury),
NULL},
/* %g */
{CTOKT_DIGIT, CLF_ISO8601YEAR | CLF_ISO8601, 0, 2, 2, TclOffset(DateInfo, date.iso8601Year),
NULL},
/* %G */
{CTOKT_DIGIT, CLF_ISO8601YEAR | CLF_ISO8601 | CLF_ISO8601CENTURY, 0, 1, 4, TclOffset(DateInfo, date.iso8601Year),
NULL},
/* %V */
{CTOKT_DIGIT, CLF_ISO8601, 0, 1, 2, TclOffset(DateInfo, date.iso8601Week),
NULL},
/* %a %A %u %w */
{CTOKT_PARSER, CLF_ISO8601, 0, 0, 0, 0,
ClockScnToken_DayOfWeek_Proc, NULL},
/* %z %Z */
{CTOKT_PARSER, 0, 0, 0, 0, 0,
ClockScnToken_TimeZone_Proc, NULL},
/* %s */
{CTOKT_DIGIT, CLF_LOCALSEC | CLF_SIGNED, 0, 1, 0xffff, TclOffset(DateInfo, date.localSeconds),
NULL},
};
static const char *ScnSTokenWrapMapIndex[2] = {
"eNBhkIlPAuwZ",
"dmbbHHHpaaaz"
};
static const char *ScnETokenMapIndex =
"Ey";
static ClockScanTokenMap ScnETokenMap[] = {
/* %EE */
{CTOKT_PARSER, 0, 0, 0, 0, TclOffset(DateInfo, date.year),
ClockScnToken_LocaleERA_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %Ey */
{CTOKT_PARSER, 0, 0, 0, 0, 0, /* currently no capture, parse only token */
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
};
static const char *ScnETokenWrapMapIndex[2] = {
"",
""
};
static const char *ScnOTokenMapIndex =
"dmyHMSu";
static ClockScanTokenMap ScnOTokenMap[] = {
/* %Od %Oe */
{CTOKT_PARSER, CLF_DAYOFMONTH, 0, 0, 0, TclOffset(DateInfo, date.dayOfMonth),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %Om */
{CTOKT_PARSER, CLF_MONTH, 0, 0, 0, TclOffset(DateInfo, date.month),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %Oy */
{CTOKT_PARSER, CLF_YEAR, 0, 0, 0, TclOffset(DateInfo, date.year),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %OH %Ok %OI %Ol */
{CTOKT_PARSER, CLF_TIME, 0, 0, 0, TclOffset(DateInfo, date.hour),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %OM */
{CTOKT_PARSER, CLF_TIME, 0, 0, 0, TclOffset(DateInfo, date.minutes),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %OS */
{CTOKT_PARSER, CLF_TIME, 0, 0, 0, TclOffset(DateInfo, date.secondOfDay),
ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS},
/* %Ou Ow */
{CTOKT_PARSER, CLF_ISO8601, 0, 0, 0, 0,
ClockScnToken_DayOfWeek_Proc, (void *)MCLIT_LOCALE_NUMERALS},
};
static const char *ScnOTokenWrapMapIndex[2] = {
"ekIlw",
"dHHHu"
};
static const char *ScnSpecTokenMapIndex =
" ";
static ClockScanTokenMap ScnSpecTokenMap[] = {
{CTOKT_SPACE, 0, 0, 0, 0xffff, 0,
NULL},
};
static ClockScanTokenMap ScnWordTokenMap = {
CTOKT_WORD, 0, 0, 1, 0, 0,
NULL
};
#define AllocTokenInChain(tok, chain, tokCnt) \
if (++(tok) >= (chain) + (tokCnt)) { \
(char *)(chain) = ckrealloc((char *)(chain), \
(tokCnt + CLOCK_MIN_TOK_CHAIN_BLOCK_SIZE) * sizeof(*(tok))); \
if ((chain) == NULL) { goto done; }; \
(tok) = (chain) + (tokCnt); \
(tokCnt) += CLOCK_MIN_TOK_CHAIN_BLOCK_SIZE; \
} \
memset(tok, 0, sizeof(*(tok)));
/*
*----------------------------------------------------------------------
*/
ClockScanToken *
ClockGetOrParseScanFormat(
Tcl_Interp *interp, /* Tcl interpreter */
Tcl_Obj *formatObj) /* Format container */
{
ClockFmtScnStorage *fss;
ClockScanToken *tok;
if (formatObj->typePtr != &ClockFmtObjType) {
if (ClockFmtObj_SetFromAny(interp, formatObj) != TCL_OK) {
return NULL;
}
}
fss = ObjClockFmtScn(formatObj);
if (fss == NULL) {
fss = FindOrCreateFmtScnStorage(interp, formatObj);
if (fss == NULL) {
return NULL;
}
}
/* if first time scanning - tokenize format */
if (fss->scnTok == NULL) {
const char *strFmt;
register const char *p, *e, *cp;
e = strFmt = HashEntry4FmtScn(fss)->key.string;
e += strlen(strFmt);
/* estimate token count by % char and format length */
fss->scnTokC = 0;
p = strFmt;
while (p != e) {
if (*p++ == '%') fss->scnTokC++;
}
p = strFmt + fss->scnTokC * 2;
if (p < e) {
if ((unsigned int)(e - p) < fss->scnTokC) {
fss->scnTokC += (e - p);
} else {
fss->scnTokC += fss->scnTokC;
}
}
fss->scnTokC++;
Tcl_MutexLock(&ClockFmtMutex);
fss->scnTok = tok = ckalloc(sizeof(*tok) * fss->scnTokC);
memset(tok, 0, sizeof(*(tok)));
for (p = strFmt; p != e; p++) {
switch (*p) {
case '%':
if (1) {
ClockScanTokenMap * scnMap = ScnSTokenMap;
const char *mapIndex = ScnSTokenMapIndex,
**wrapIndex = ScnSTokenWrapMapIndex;
if (p+1 >= e) {
goto word_tok;
}
p++;
/* try to find modifier: */
switch (*p) {
case '%':
/* begin new word token - don't join with previous word token,
* because current mapping should be "...%%..." -> "...%..." */
tok->map = &ScnWordTokenMap;
tok->tokWord.start = p;
tok->tokWord.end = p+1;
AllocTokenInChain(tok, fss->scnTok, fss->scnTokC);
continue;
break;
case 'E':
scnMap = ScnETokenMap,
mapIndex = ScnETokenMapIndex,
wrapIndex = ScnETokenWrapMapIndex;
p++;
break;
case 'O':
scnMap = ScnOTokenMap,
mapIndex = ScnOTokenMapIndex,
wrapIndex = ScnOTokenWrapMapIndex;
p++;
break;
}
/* search direct index */
cp = strchr(mapIndex, *p);
if (!cp || *cp == '\0') {
/* search wrapper index (multiple chars for same token) */
cp = strchr(wrapIndex[0], *p);
if (!cp || *cp == '\0') {
p--;
goto word_tok;
}
cp = strchr(mapIndex, wrapIndex[1][cp - wrapIndex[0]]);
if (!cp || *cp == '\0') { /* unexpected, but ... */
#ifdef DEBUG
Tcl_Panic("token \"%c\" has no map in wrapper resolver", *p);
#endif
p--;
goto word_tok;
}
}
tok->map = &scnMap[cp - mapIndex];
tok->tokWord.start = p;
/* calculate look ahead value by standing together tokens */
if (tok > fss->scnTok && tok->map->minSize) {
unsigned int lookAhead = tok->map->minSize;
ClockScanToken *prevTok = tok - 1;
while (prevTok >= fss->scnTok) {
if (prevTok->map->type != tok->map->type) {
break;
}
prevTok->lookAhead += lookAhead;
prevTok--;
}
}
/* next token */
AllocTokenInChain(tok, fss->scnTok, fss->scnTokC);
}
break;
case ' ':
cp = strchr(ScnSpecTokenMapIndex, *p);
if (!cp || *cp == '\0') {
p--;
goto word_tok;
}
tok->map = &ScnSpecTokenMap[cp - ScnSpecTokenMapIndex];
AllocTokenInChain(tok, fss->scnTok, fss->scnTokC);
break;
default:
word_tok:
if (1) {
ClockScanToken *wordTok = tok;
if (tok > fss->scnTok && (tok-1)->map == &ScnWordTokenMap) {
wordTok = tok-1;
}
wordTok->tokWord.end = p+1;
if (wordTok == tok) {
wordTok->tokWord.start = p;
wordTok->map = &ScnWordTokenMap;
AllocTokenInChain(tok, fss->scnTok, fss->scnTokC);
}
continue;
}
break;
}
continue;
}
/* calculate end distance value for each tokens */
if (tok > fss->scnTok) {
unsigned int endDist = 0;
ClockScanToken *prevTok = tok-1;
while (prevTok >= fss->scnTok) {
prevTok->endDistance = endDist;
if (prevTok->map->type != CTOKT_WORD) {
endDist += prevTok->map->minSize;
} else {
endDist += prevTok->tokWord.end - prevTok->tokWord.start;
}
prevTok--;
}
}
done:
Tcl_MutexUnlock(&ClockFmtMutex);
}
return fss->scnTok;
}
MODULE_SCOPE Tcl_Obj *
ClockLocalizeFormat(
ClockFmtScnCmdArgs *opts)
{
ClockClientData *dataPtr = opts->clientData;
Tcl_Obj *valObj = NULL, *keyObj;
keyObj = ClockFrmObjGetLocFmtKey(opts->interp, opts->formatObj);
/* special case - format object is not localizable */
if (keyObj == opts->formatObj) {
return opts->formatObj;
}
if (opts->mcDictObj == NULL) {
ClockMCDict(opts);
if (opts->mcDictObj == NULL)
return NULL;
}
/* try to find in cache within locale mc-catalog */
if (Tcl_DictObjGet(NULL, opts->mcDictObj,
keyObj, &valObj) != TCL_OK) {
return NULL;
}
/* call LocalizeFormat locale format fmtkey */
if (valObj == NULL) {
Tcl_Obj *callargs[4];
callargs[0] = dataPtr->literals[LIT_LOCALIZE_FORMAT];
callargs[1] = opts->localeObj;
callargs[2] = opts->formatObj;
callargs[3] = keyObj;
Tcl_IncrRefCount(keyObj);
if (Tcl_EvalObjv(opts->interp, 4, callargs, 0) != TCL_OK
) {
goto clean;
}
valObj = Tcl_GetObjResult(opts->interp);
/* cache it inside mc-dictionary (this incr. ref count of keyObj/valObj) */
if (Tcl_DictObjPut(opts->interp, opts->mcDictObj,
keyObj, valObj) != TCL_OK
) {
valObj = NULL;
goto clean;
}
/* check special case - format object is not localizable */
if (valObj == opts->formatObj) {
/* mark it as unlocalizable, by setting self as key (without refcount incr) */
if (opts->formatObj->typePtr == &ClockFmtObjType) {
Tcl_UnsetObjRef(ObjLocFmtKey(opts->formatObj));
ObjLocFmtKey(opts->formatObj) = opts->formatObj;
}
}
clean:
Tcl_UnsetObjRef(keyObj);
if (valObj) {
Tcl_ResetResult(opts->interp);
}
}
return (opts->formatObj = valObj);
}
/*
*----------------------------------------------------------------------
*/
int
ClockScan(
ClientData clientData, /* Client data containing literal pool */
Tcl_Interp *interp, /* Tcl interpreter */
register DateInfo *info, /* Date fields used for parsing & converting */
Tcl_Obj *strObj, /* String containing the time to scan */
ClockFmtScnCmdArgs *opts) /* Command options */
{
ClockClientData *dataPtr = clientData;
ClockScanToken *tok;
ClockScanTokenMap *map;
register const char *p, *x, *end;
unsigned short int flags = 0;
int ret = TCL_ERROR;
/* get localized format */
if (ClockLocalizeFormat(opts) == NULL) {
return TCL_ERROR;
}
if ((tok = ClockGetOrParseScanFormat(interp, opts->formatObj)) == NULL) {
return TCL_ERROR;
}
/* prepare parsing */
yyMeridian = MER24;
/* lower case given string into new object */
strObj = Tcl_NewStringObj(TclGetString(strObj), strObj->length);
Tcl_IncrRefCount(strObj);
strObj->length = Tcl_UtfToLower(TclGetString(strObj));
p = TclGetString(strObj);
end = p + strObj->length;
/* in strict mode - bypass spaces at begin / end only (not between tokens) */
if (opts->flags & CLF_STRICT) {
while (p < end && isspace(UCHAR(*p))) {
p++;
}
}
info->dateStart = yyInput = p;
info->dateEnd = end;
/* parse string */
for (; tok->map != NULL; tok++) {
map = tok->map;
/* bypass spaces at begin of input before parsing each token */
if ( !(opts->flags & CLF_STRICT)
&& (map->type != CTOKT_SPACE && map->type != CTOKT_WORD)
) {
while (p < end && isspace(UCHAR(*p))) {
p++;
}
}
yyInput = p;
switch (map->type)
{
case CTOKT_DIGIT:
if (1) {
int size = map->maxSize;
int sign = 1;
if (map->flags & CLF_SIGNED) {
if (*p == '+') { yyInput = ++p; }
else
if (*p == '-') { yyInput = ++p; sign = -1; };
}
/* greedy find digits (look for forward digits consider spaces),
* corresponding pre-calculated lookAhead */
if (size != map->minSize && tok->lookAhead) {
int spcnt = 0;
const char *pe;
size += tok->lookAhead;
x = p + size; if (x > end) { x = end; };
pe = x;
while (p < x) {
if (isspace(UCHAR(*p))) {
if (pe > p) { pe = p; };
if (x < end) x++;
p++;
spcnt++;
continue;
}
if (isdigit(UCHAR(*p))) {
p++;
continue;
}
break;
}
/* consider reserved (lookAhead) for next tokens */
p -= tok->lookAhead + spcnt;
if (p > pe) {
p = pe;
}
} else {
x = p + size; if (x > end) { x = end; };
while (isdigit(UCHAR(*p)) && p < x) { p++; };
}
size = p - yyInput;
if (size < map->minSize) {
/* missing input -> error */
goto not_match;
}
/* string 2 number, put number into info structure by offset */
p = yyInput; x = p + size;
if (!(map->flags & CLF_LOCALSEC)) {
if (_str2int((time_t *)(((char *)info) + map->offs),
p, x, sign) != TCL_OK) {
goto overflow;
}
p = x;
} else {
if (_str2wideInt((Tcl_WideInt *)(((char *)info) + map->offs),
p, x, sign) != TCL_OK) {
goto overflow;
}
p = x;
}
flags = (flags & ~map->clearFlags) | map->flags;
}
break;
case CTOKT_PARSER:
switch (map->parser(opts, info, tok)) {
case TCL_OK:
break;
case TCL_RETURN:
goto not_match;
break;
default:
goto done;
break;
};
p = yyInput;
flags = (flags & ~map->clearFlags) | map->flags;
break;
case CTOKT_SPACE:
/* at least one space in strict mode */
if (opts->flags & CLF_STRICT) {
if (!isspace(UCHAR(*p))) {
/* unmatched -> error */
goto not_match;
}
p++;
}
while (p < end && isspace(UCHAR(*p))) {
p++;
}
break;
case CTOKT_WORD:
x = FindWordEnd(tok, p, end);
if (!x) {
/* no match -> error */
goto not_match;
}
p = x;
continue;
break;
}
}
/* ignore spaces at end */
while (p < end && isspace(UCHAR(*p))) {
p++;
}
/* check end was reached */
if (p < end) {
/* something after last token - wrong format */
goto not_match;
}
/*
* Invalidate result
*/
/* seconds token (%s) take precedence over all other tokens */
if ((opts->flags & CLF_EXTENDED) || !(flags & CLF_LOCALSEC)) {
if (flags & CLF_DATE) {
if (!(flags & CLF_JULIANDAY)) {
info->flags |= CLF_ASSEMBLE_SECONDS|CLF_ASSEMBLE_JULIANDAY;
/* dd precedence below ddd */
switch (flags & (CLF_MONTH|CLF_DAYOFYEAR|CLF_DAYOFMONTH)) {
case (CLF_DAYOFYEAR|CLF_DAYOFMONTH):
/* miss month: ddd over dd (without month) */
flags &= ~CLF_DAYOFMONTH;
case (CLF_DAYOFYEAR):
/* ddd over naked weekday */
if (!(flags & CLF_ISO8601YEAR)) {
flags &= ~CLF_ISO8601;
}
break;
case (CLF_MONTH|CLF_DAYOFYEAR|CLF_DAYOFMONTH):
/* both available: mmdd over ddd */
flags &= ~CLF_DAYOFYEAR;
case (CLF_MONTH|CLF_DAYOFMONTH):
case (CLF_DAYOFMONTH):
/* mmdd / dd over naked weekday */
if (!(flags & CLF_ISO8601YEAR)) {
flags &= ~CLF_ISO8601;
}
break;
}
/* YearWeekDay below YearMonthDay */
if ( (flags & CLF_ISO8601)
&& ( (flags & (CLF_YEAR|CLF_DAYOFYEAR)) == (CLF_YEAR|CLF_DAYOFYEAR)
|| (flags & (CLF_YEAR|CLF_DAYOFMONTH|CLF_MONTH)) == (CLF_YEAR|CLF_DAYOFMONTH|CLF_MONTH)
)
) {
/* yy precedence below yyyy */
if (!(flags & CLF_ISO8601CENTURY) && (flags & CLF_CENTURY)) {
/* normally precedence of ISO is higher, but no century - so put it down */
flags &= ~CLF_ISO8601;
}
else
/* yymmdd or yyddd over naked weekday */
if (!(flags & CLF_ISO8601YEAR)) {
flags &= ~CLF_ISO8601;
}
}
if (!(flags & CLF_ISO8601)) {
if (yyYear < 100) {
if (!(flags & CLF_CENTURY)) {
if (yyYear >= dataPtr->yearOfCenturySwitch) {
yyYear -= 100;
}
yyYear += dataPtr->currentYearCentury;
} else {
yyYear += info->dateCentury * 100;
}
}
} else {
if (info->date.iso8601Year < 100) {
if (!(flags & CLF_ISO8601CENTURY)) {
if (info->date.iso8601Year >= dataPtr->yearOfCenturySwitch) {
info->date.iso8601Year -= 100;
}
info->date.iso8601Year += dataPtr->currentYearCentury;
} else {
info->date.iso8601Year += info->dateCentury * 100;
}
}
}
}
}
/* if no time - reset time */
if (!(flags & (CLF_TIME|CLF_LOCALSEC))) {
info->flags |= CLF_ASSEMBLE_SECONDS;
yydate.localSeconds = 0;
}
if (flags & CLF_TIME) {
info->flags |= CLF_ASSEMBLE_SECONDS;
yySeconds = ToSeconds(yyHour, yyMinutes,
yySeconds, yyMeridian);
} else
if (!(flags & CLF_LOCALSEC)) {
info->flags |= CLF_ASSEMBLE_SECONDS;
yySeconds = yydate.localSeconds % SECONDS_PER_DAY;
}
}
/* tell caller which flags were set */
info->flags |= flags;
ret = TCL_OK;
goto done;
overflow:
Tcl_SetResult(interp, "requested date too large to represent",
TCL_STATIC);
Tcl_SetErrorCode(interp, "CLOCK", "dateTooLarge", NULL);
goto done;
not_match:
Tcl_SetResult(interp, "input string does not match supplied format",
TCL_STATIC);
Tcl_SetErrorCode(interp, "CLOCK", "badInputString", NULL);
done:
Tcl_DecrRefCount(strObj);
return ret;
}
/*
*----------------------------------------------------------------------
*/
int
ClockFormat(
ClientData clientData, /* Client data containing literal pool */
Tcl_Interp *interp, /* Tcl interpreter */
register DateInfo *info, /* Date fields used for parsing & converting */
ClockFmtScnCmdArgs *opts) /* Command options */
{
ClockClientData *dataPtr = clientData;
ClockFormatToken *tok;
ClockFormatTokenMap *map;
/* get localized format */
if (ClockLocalizeFormat(opts) == NULL) {
return TCL_ERROR;
}
/* if ((tok = ClockGetOrParseFmtFormat(interp, opts->formatObj)) == NULL) {
return TCL_ERROR;
}
*/
}
MODULE_SCOPE void
ClockFrmScnClearCaches(void)
{
Tcl_MutexLock(&ClockFmtMutex);
/* clear caches ... */
Tcl_MutexUnlock(&ClockFmtMutex);
}
static void
ClockFrmScnFinalize(
ClientData clientData) /* Not used. */
{
Tcl_MutexLock(&ClockFmtMutex);
#if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0
/* clear GC */
ClockFmtScnStorage_GC.stackPtr = NULL;
ClockFmtScnStorage_GC.stackBound = NULL;
ClockFmtScnStorage_GC.count = 0;
#endif
if (initialized) {
Tcl_DeleteHashTable(&FmtScnHashTable);
initialized = 0;
}
Tcl_MutexUnlock(&ClockFmtMutex);
Tcl_MutexFinalize(&ClockFmtMutex);
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|