diff options
author | dkf <donal.k.fellows@manchester.ac.uk> | 2024-04-11 15:19:45 (GMT) |
---|---|---|
committer | dkf <donal.k.fellows@manchester.ac.uk> | 2024-04-11 15:19:45 (GMT) |
commit | 1d169110747850ae6a984c886a5bfeef9e9cb7aa (patch) | |
tree | cce8825d9fe1379f600427fe7884e5183459e98d /generic/tclClockFmt.c | |
parent | a1e84061ef1e26112a3ae3408a31512dc5fbf521 (diff) | |
download | tcl-1d169110747850ae6a984c886a5bfeef9e9cb7aa.zip tcl-1d169110747850ae6a984c886a5bfeef9e9cb7aa.tar.gz tcl-1d169110747850ae6a984c886a5bfeef9e9cb7aa.tar.bz2 |
Next stage of migrating things to follow style: clock format support
Diffstat (limited to 'generic/tclClockFmt.c')
-rw-r--r-- | generic/tclClockFmt.c | 1196 |
1 files changed, 637 insertions, 559 deletions
diff --git a/generic/tclClockFmt.c b/generic/tclClockFmt.c index b5d507c..06684a6 100644 --- a/generic/tclClockFmt.c +++ b/generic/tclClockFmt.c @@ -18,22 +18,22 @@ * 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); - +static void ClockFmtObj_DupInternalRep(Tcl_Obj *srcPtr, Tcl_Obj *copyPtr); +static void ClockFmtObj_FreeInternalRep(Tcl_Obj *objPtr); +static int ClockFmtObj_SetFromAny(Tcl_Interp *, Tcl_Obj *objPtr); +static void ClockFmtObj_UpdateString(Tcl_Obj *objPtr); -TCL_DECLARE_MUTEX(ClockFmtMutex); /* Serializes access to common format list. */ +TCL_DECLARE_MUTEX(ClockFmtMutex); /* Serializes access to common format list. */ -static void ClockFmtScnStorageDelete(ClockFmtScnStorage *fss); +static void ClockFmtScnStorageDelete(ClockFmtScnStorage *fss); +static void ClockFrmScnFinalize(void *); -static void ClockFrmScnFinalize(void *clientData); +/* + * Derivation of tclStringHashKeyType with another allocEntryProc + */ +static Tcl_HashKeyType ClockFmtScnStorageHashKeyType; + /* * Clock scan and format facilities. */ @@ -58,35 +58,39 @@ static void ClockFrmScnFinalize(void *clientData); static inline void _str2int_no( - int *out, + int *out, const char *p, const char *e, int sign) { - /* assert(e <= p+10); */ + /* assert(e <= p + 10); */ int val = 0; /* overflow impossible for 10 digits ("9..9"), so no needs to check at all */ while (p < e) { /* never overflows */ val = val * 10 + (*p++ - '0'); } - if (sign < 0) { val = -val; } + if (sign < 0) { + val = -val; + } *out = val; } static inline void _str2wideInt_no( Tcl_WideInt *out, - const char *p, - const char *e, + const char *p, + const char *e, int sign) { - /* assert(e <= p+18); */ + /* assert(e <= p + 18); */ Tcl_WideInt val = 0; /* overflow impossible for 18 digits ("9..9"), so no needs to check at all */ while (p < e) { /* never overflows */ val = val * 10 + (*p++ - '0'); } - if (sign < 0) { val = -val; } + if (sign < 0) { + val = -val; + } *out = val; } @@ -97,14 +101,14 @@ _str2wideInt_no( static inline int _str2int( - int *out, + int *out, const char *p, const char *e, int sign) { int val = 0; /* overflow impossible for 10 digits ("9..9"), so no needs to check before */ - const char *eNO = p+10; + const char *eNO = p + 10; if (eNO > e) { eNO = e; } @@ -136,13 +140,13 @@ _str2int( static inline int _str2wideInt( Tcl_WideInt *out, - const char *p, - const char *e, + const char *p, + const char *e, int sign) { Tcl_WideInt val = 0; /* overflow impossible for 18 digits ("9..9"), so no needs to check before */ - const char *eNO = p+18; + const char *eNO = p + 18; if (eNO > e) { eNO = e; } @@ -174,8 +178,8 @@ _str2wideInt( int TclAtoWIe( Tcl_WideInt *out, - const char *p, - const char *e, + const char *p, + const char *e, int sign) { return _str2wideInt(out, p, e, sign); @@ -205,16 +209,15 @@ static inline char * _itoaw( char *buf, int val, - char padchar, - unsigned short int width) + char padchar, + unsigned short width) { char *p; static int wrange[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; /* positive integer */ - if (val >= 0) - { + if (val >= 0) { /* check resp. recalculate width */ while (width <= 9 && val >= wrange[width]) { width++; @@ -223,7 +226,8 @@ _itoaw( p = buf + width; *p-- = '\0'; do { - char c = (val % 10); val /= 10; + char c = (val % 10); + val /= 10; *p-- = '0' + c; } while (val > 0); /* fulling with pad-char */ @@ -235,7 +239,9 @@ _itoaw( } /* negative integer */ - if (!width) width++; + if (!width) { + width++; + } /* check resp. recalculate width (regarding sign) */ width--; while (width <= 9 && val <= -wrange[width]) { @@ -248,23 +254,29 @@ _itoaw( /* differentiate platforms with -1 % 10 == 1 and -1 % 10 == -1 */ if (-1 % 10 == -1) { do { - char c = (val % 10); val /= 10; + char c = (val % 10); + val /= 10; *p-- = '0' - c; } while (val < 0); } else { do { - char c = (val % 10); val /= 10; + char c = (val % 10); + val /= 10; *p-- = '0' + c; } while (val < 0); } /* sign by 0 padding */ - if (padchar != '0') { *p-- = '-'; } + if (padchar != '0') { + *p-- = '-'; + } /* fulling with pad-char */ while (p >= buf + 1) { *p-- = padchar; } /* sign by non 0 padding */ - if (padchar == '0') { *p = '-'; } + if (padchar == '0') { + *p = '-'; + } return buf + width; } @@ -272,8 +284,8 @@ char * TclItoAw( char *buf, int val, - char padchar, - unsigned short int width) + char padchar, + unsigned short width) { return _itoaw(buf, val, padchar, width); } @@ -282,20 +294,18 @@ static inline char * _witoaw( char *buf, Tcl_WideInt val, - char padchar, - unsigned short int width) + char padchar, + unsigned short width) { char *p; static int wrange[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; /* positive integer */ - if (val >= 0) - { + if (val >= 0) { /* check resp. recalculate width */ if (val >= 10000000000LL) { - Tcl_WideInt val2; - val2 = val / 10000000000LL; + Tcl_WideInt val2 = val / 10000000000LL; while (width <= 9 && val2 >= wrange[width]) { width++; } @@ -309,7 +319,8 @@ _witoaw( p = buf + width; *p-- = '\0'; do { - char c = (val % 10); val /= 10; + char c = (val % 10); + val /= 10; *p-- = '0' + c; } while (val > 0); /* fulling with pad-char */ @@ -322,12 +333,13 @@ _witoaw( /* negative integer */ - if (!width) width++; + if (!width) { + width++; + } /* check resp. recalculate width (regarding sign) */ width--; if (val <= -10000000000LL) { - Tcl_WideInt val2; - val2 = val / 10000000000LL; + Tcl_WideInt val2 = val / 10000000000LL; while (width <= 9 && val2 <= -wrange[width]) { width++; } @@ -344,23 +356,29 @@ _witoaw( /* differentiate platforms with -1 % 10 == 1 and -1 % 10 == -1 */ if (-1 % 10 == -1) { do { - char c = (val % 10); val /= 10; + char c = (val % 10); + val /= 10; *p-- = '0' - c; } while (val < 0); } else { do { - char c = (val % 10); val /= 10; + char c = (val % 10); + val /= 10; *p-- = '0' + c; } while (val < 0); } /* sign by 0 padding */ - if (padchar != '0') { *p-- = '-'; } + if (padchar != '0') { + *p-- = '-'; + } /* fulling with pad-char */ while (p >= buf + 1) { *p-- = padchar; } /* sign by non 0 padding */ - if (padchar == '0') { *p = '-'; } + if (padchar == '0') { + *p = '-'; + } return buf + width; } @@ -379,10 +397,10 @@ _witoaw( #if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0 -static struct { - ClockFmtScnStorage *stackPtr; - ClockFmtScnStorage *stackBound; - unsigned int count; +static struct ClockFmtScnStorage_GC { + ClockFmtScnStorage *stackPtr; + ClockFmtScnStorage *stackBound; + unsigned count; } ClockFmtScnStorage_GC = {NULL, NULL, 0}; /* @@ -404,7 +422,8 @@ static struct { */ static inline void -ClockFmtScnStorageGC_In(ClockFmtScnStorage *entry) +ClockFmtScnStorageGC_In( + ClockFmtScnStorage *entry) { /* add new entry */ TclSpliceIn(entry, ClockFmtScnStorage_GC.stackPtr); @@ -415,9 +434,9 @@ ClockFmtScnStorageGC_In(ClockFmtScnStorage *entry) /* 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--; @@ -443,7 +462,8 @@ ClockFmtScnStorageGC_In(ClockFmtScnStorage *entry) */ static inline void -ClockFmtScnStorage_GC_Out(ClockFmtScnStorage *entry) +ClockFmtScnStorage_GC_Out( + ClockFmtScnStorage *entry) { TclSpliceOut(entry, ClockFmtScnStorage_GC.stackPtr); ClockFmtScnStorage_GC.count--; @@ -452,10 +472,8 @@ ClockFmtScnStorage_GC_Out(ClockFmtScnStorage *entry) } entry->prevPtr = entry->nextPtr = NULL; } - #endif - /* * Global format storage hash table of type ClockFmtScnStorageHashKeyType * (contains list of scan/format object storages, shared across all threads). @@ -463,20 +481,25 @@ ClockFmtScnStorage_GC_Out(ClockFmtScnStorage *entry) * Used for fast searching by format string. */ static Tcl_HashTable FmtScnHashTable; -static int initialized = 0; +static int initialized = 0; /* * Wrappers between pointers to hash entry and format storage object */ static inline Tcl_HashEntry * -HashEntry4FmtScn(ClockFmtScnStorage *fss) { +HashEntry4FmtScn( + ClockFmtScnStorage *fss) +{ return (Tcl_HashEntry*)(fss + 1); -}; +} + static inline ClockFmtScnStorage * -FmtScn4HashEntry(Tcl_HashEntry *hKeyPtr) { +FmtScn4HashEntry( + Tcl_HashEntry *hKeyPtr) +{ return (ClockFmtScnStorage*)(((char*)hKeyPtr) - sizeof(ClockFmtScnStorage)); -}; - +} + /* *---------------------------------------------------------------------- * @@ -499,26 +522,26 @@ ClockFmtScnStorageAllocProc( ClockFmtScnStorage *fss; const char *string = (const char *) keyPtr; Tcl_HashEntry *hPtr; - unsigned int size, - allocsize = sizeof(ClockFmtScnStorage) + sizeof(Tcl_HashEntry); + unsigned size = strlen(string) + 1; + unsigned allocsize = sizeof(ClockFmtScnStorage) + sizeof(Tcl_HashEntry); - allocsize += (size = strlen(string) + 1); + allocsize += size; if (size > sizeof(hPtr->key)) { allocsize -= sizeof(hPtr->key); } - fss = (ClockFmtScnStorage *)ckalloc(allocsize); + fss = (ClockFmtScnStorage *) ckalloc(allocsize); /* initialize */ memset(fss, 0, sizeof(*fss)); hPtr = HashEntry4FmtScn(fss); memcpy(&hPtr->key.string, string, size); - hPtr->clientData = 0; /* currently unused */ + hPtr->clientData = 0; /* currently unused */ return hPtr; } - + /* *---------------------------------------------------------------------- * @@ -551,7 +574,7 @@ ClockFmtScnStorageFreeProc( ckfree(fss); } - + /* *---------------------------------------------------------------------- * @@ -566,33 +589,27 @@ ClockFmtScnStorageFreeProc( */ static void -ClockFmtScnStorageDelete(ClockFmtScnStorage *fss) { - Tcl_HashEntry *hPtr = HashEntry4FmtScn(fss); +ClockFmtScnStorageDelete( + ClockFmtScnStorage *fss) +{ + Tcl_HashEntry *hPtr = HashEntry4FmtScn(fss); /* * This will delete a hash entry and call "Tcl_Free" 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 of clock-format tcl object type. */ static const Tcl_ObjType ClockFmtObjType = { - "clock-format", /* name */ - ClockFmtObj_FreeInternalRep, /* freeIntRepProc */ - ClockFmtObj_DupInternalRep, /* dupIntRepProc */ - ClockFmtObj_UpdateString, /* updateStringProc */ - ClockFmtObj_SetFromAny /* setFromAnyProc */ + "clock-format", /* name */ + ClockFmtObj_FreeInternalRep, /* freeIntRepProc */ + ClockFmtObj_DupInternalRep, /* dupIntRepProc */ + ClockFmtObj_UpdateString, /* updateStringProc */ + ClockFmtObj_SetFromAny /* setFromAnyProc */ }; #define ObjClockFmtScn(objPtr) \ @@ -623,10 +640,9 @@ ClockFmtObj_DupInternalRep( } copyPtr->typePtr = &ClockFmtObjType; - /* if no format representation, dup string representation */ if (fss == NULL) { - copyPtr->bytes = (char *)ckalloc(srcPtr->length + 1); + copyPtr->bytes = (char *) ckalloc(srcPtr->length + 1); memcpy(copyPtr->bytes, srcPtr->bytes, srcPtr->length + 1); copyPtr->length = srcPtr->length; } @@ -641,13 +657,13 @@ ClockFmtObj_FreeInternalRep( 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 +#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); } @@ -658,12 +674,12 @@ ClockFmtObj_FreeInternalRep( ObjLocFmtKey(objPtr) = NULL; } objPtr->typePtr = NULL; -}; +} static int ClockFmtObj_SetFromAny( TCL_UNUSED(Tcl_Interp *), - Tcl_Obj *objPtr) + Tcl_Obj *objPtr) { /* validate string representation before free old internal representation */ (void)TclGetString(objPtr); @@ -677,11 +693,11 @@ ClockFmtObj_SetFromAny( objPtr->typePtr = &ClockFmtObjType; return TCL_OK; -}; +} static void ClockFmtObj_UpdateString( - Tcl_Obj *objPtr) + Tcl_Obj *objPtr) { const char *name = "UNKNOWN"; size_t len; @@ -693,7 +709,7 @@ ClockFmtObj_UpdateString( } len = strlen(name); objPtr->length = len++, - objPtr->bytes = (char *)ckalloc(len); + objPtr->bytes = (char *) attemptckalloc(len); if (objPtr->bytes) { memcpy(objPtr->bytes, name, len); } @@ -723,7 +739,7 @@ ClockFmtObj_UpdateString( Tcl_Obj* ClockFrmObjGetLocFmtKey( Tcl_Interp *interp, - Tcl_Obj *objPtr) + Tcl_Obj *objPtr) { Tcl_Obj *keyObj; @@ -770,7 +786,7 @@ ClockFrmObjGetLocFmtKey( static ClockFmtScnStorage * FindOrCreateFmtScnStorage( Tcl_Interp *interp, - Tcl_Obj *objPtr) + Tcl_Obj *objPtr) { const char *strFmt = TclGetString(objPtr); ClockFmtScnStorage *fss = NULL; @@ -788,7 +804,7 @@ FindOrCreateFmtScnStorage( /* initialize hash table */ Tcl_InitCustomHashTable(&FmtScnHashTable, TCL_CUSTOM_TYPE_KEYS, - &ClockFmtScnStorageHashKeyType); + &ClockFmtScnStorageHashKeyType); initialized = 1; Tcl_CreateExitHandler(ClockFrmScnFinalize, NULL); @@ -797,19 +813,17 @@ FindOrCreateFmtScnStorage( /* get or create entry (and alocate storage) */ hPtr = Tcl_CreateHashEntry(&FmtScnHashTable, strFmt, &isNew); if (hPtr != NULL) { - fss = FmtScn4HashEntry(hPtr); - #if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0 - /* unlink if it is currently in GC */ - if (isNew == 0 && fss->objRefCount == 0) { - ClockFmtScnStorage_GC_Out(fss); - } - #endif +#if CLOCK_FMT_SCN_STORAGE_GC_SIZE > 0 + /* unlink if it is currently in GC */ + if (isNew == 0 && fss->objRefCount == 0) { + ClockFmtScnStorage_GC_Out(fss); + } +#endif /* new reference, so increment in lock right now */ fss->objRefCount++; - ObjClockFmtScn(objPtr) = fss; } @@ -817,7 +831,7 @@ FindOrCreateFmtScnStorage( if (fss == NULL && interp != NULL) { Tcl_AppendResult(interp, "retrieve clock format failed \"", - strFmt ? strFmt : "", "\"", NULL); + strFmt ? strFmt : "", "\"", NULL); Tcl_SetErrorCode(interp, "TCL", "EINVAL", (char *)NULL); } @@ -905,25 +919,25 @@ ClockLocalizeFormat( if (opts->mcDictObj == NULL) { ClockMCDict(opts); - if (opts->mcDictObj == NULL) + if (opts->mcDictObj == NULL) { goto done; + } } /* try to find in cache within locale mc-catalog */ - if (Tcl_DictObjGet(NULL, opts->mcDictObj, - keyObj, &valObj) != TCL_OK) { + if (Tcl_DictObjGet(NULL, opts->mcDictObj, keyObj, &valObj) != TCL_OK) { goto done; } /* 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] = opts->mcDictObj; - if (Tcl_EvalObjv(opts->interp, 4, callargs, 0) == TCL_OK - ) { + if (Tcl_EvalObjv(opts->interp, 4, callargs, 0) == TCL_OK) { valObj = Tcl_GetObjResult(opts->interp); } @@ -931,15 +945,13 @@ ClockLocalizeFormat( if (opts->mcDictObj->refCount > 1) { /* smart reference (shared dict as object with no ref-counter) */ opts->mcDictObj = TclDictObjSmartRef(opts->interp, - opts->mcDictObj); + opts->mcDictObj); } if (!valObj) { goto done; } /* cache it inside mc-dictionary (this incr. ref count of keyObj/valObj) */ - if (Tcl_DictObjPut(opts->interp, opts->mcDictObj, - keyObj, valObj) != TCL_OK - ) { + if (Tcl_DictObjPut(opts->interp, opts->mcDictObj, keyObj, valObj) != TCL_OK) { valObj = NULL; goto done; } @@ -984,31 +996,31 @@ FindTokenBegin( const char *end, ClockScanToken *tok) { - char c; if (p < end) { + char c; + /* next token a known token type */ switch (tok->map->type) { case CTOKT_INT: case CTOKT_WIDE: /* should match at least one digit */ - while (!isdigit(UCHAR(*p)) && (p = Tcl_UtfNext(p)) < end) {}; + while (!isdigit(UCHAR(*p)) && (p = Tcl_UtfNext(p)) < end) {} return p; - break; + case CTOKT_WORD: c = *(tok->tokWord.start); /* should match at least to the first char of this word */ - while (*p != c && (p = Tcl_UtfNext(p)) < end) {}; + while (*p != c && (p = Tcl_UtfNext(p)) < end) {} return p; - break; + case CTOKT_SPACE: - while (!isspace(UCHAR(*p)) && (p = Tcl_UtfNext(p)) < end) {}; + while (!isspace(UCHAR(*p)) && (p = Tcl_UtfNext(p)) < end) {} return p; - break; + case CTOKT_CHAR: c = *((char *)tok->map->data); - while (*p != c && (p = Tcl_UtfNext(p)) < end) {}; + while (*p != c && (p = Tcl_UtfNext(p)) < end) {} return p; - break; } } return p; @@ -1032,8 +1044,10 @@ FindTokenBegin( static void DetermineGreedySearchLen( - DateInfo *info, ClockScanToken *tok, - int *minLenPtr, int *maxLenPtr) + DateInfo *info, + ClockScanToken *tok, + int *minLenPtr, + int *maxLenPtr) { int minLen = tok->map->minSize; int maxLen; @@ -1041,10 +1055,10 @@ DetermineGreedySearchLen( *end = info->dateEnd; /* if still tokens available, try to correct minimum length */ - if ((tok+1)->map) { + if ((tok + 1)->map) { end -= tok->endDistance + yySpaceCount; /* find position of next known token */ - p = FindTokenBegin(p, end, tok+1); + p = FindTokenBegin(p, end, tok + 1); if (p < end) { minLen = p - yyInput; } @@ -1055,7 +1069,7 @@ DetermineGreedySearchLen( /* several amendments */ if (maxLen > tok->map->maxSize) { maxLen = tok->map->maxSize; - }; + } if (minLen < tok->map->minSize) { minLen = tok->map->minSize; } @@ -1070,8 +1084,12 @@ DetermineGreedySearchLen( if (tok->map->type == CTOKT_INT || tok->map->type == CTOKT_WIDE) { p = yyInput; end = p + maxLen; - if (end > info->dateEnd) { end = info->dateEnd; }; - while (isdigit(UCHAR(*p)) && p < end) { p++; }; + if (end > info->dateEnd) { + end = info->dateEnd; + } + while (isdigit(UCHAR(*p)) && p < end) { + p++; + } maxLen = p - yyInput; } @@ -1079,6 +1097,7 @@ DetermineGreedySearchLen( * check the next ahead token available there */ if (minLen < maxLen && tok->lookAhTok) { ClockScanToken *laTok = tok + tok->lookAhTok + 1; + p = yyInput + maxLen; /* regards all possible spaces here (because they are optional) */ end = p + tok->lookAhMax + yySpaceCount + 1; @@ -1087,10 +1106,10 @@ DetermineGreedySearchLen( } p += tok->lookAhMin; if (laTok->map && p < end) { - const char *f; + /* try to find laTok between [lookAhMin, lookAhMax] */ while (minLen < maxLen) { - f = FindTokenBegin(p, end, laTok); + const char *f = FindTokenBegin(p, end, laTok); /* if found (not below lookAhMax) */ if (f < end) { break; @@ -1131,19 +1150,22 @@ DetermineGreedySearchLen( static inline int ObjListSearch( - DateInfo *info, int *val, - Tcl_Obj **lstv, Tcl_Size lstc, - int minLen, int maxLen) + DateInfo *info, + int *val, + Tcl_Obj **lstv, + Tcl_Size lstc, + int minLen, + int maxLen) { Tcl_Size i, l, lf = -1; const char *s, *f, *sf; + /* search in list */ for (i = 0; i < lstc; i++) { s = TclGetStringFromObj(lstv[i], &l); - if ( l >= minLen - && (f = TclUtfFindEqualNC(yyInput, yyInput + maxLen, s, s + l, &sf)) > yyInput - ) { + if (l >= minLen + && (f = TclUtfFindEqualNC(yyInput, yyInput + maxLen, s, s + l, &sf)) > yyInput) { l = f - yyInput; if (l < minLen) { continue; @@ -1197,7 +1219,7 @@ LocaleListSearch(ClockFmtScnCmdArgs *opts, /* search in list */ return ObjListSearch(info, val, lstv, lstc, - minLen, maxLen); + minLen, maxLen); } #endif @@ -1224,15 +1246,14 @@ LocaleListSearch(ClockFmtScnCmdArgs *opts, static TclStrIdxTree * ClockMCGetListIdxTree( ClockFmtScnCmdArgs *opts, - int mcKey) + int mcKey) { - TclStrIdxTree * idxTree; + TclStrIdxTree *idxTree; Tcl_Obj *objPtr = ClockMCGetIdx(opts, mcKey); - if ( objPtr != NULL - && (idxTree = TclStrIdxTreeGetFromObj(objPtr)) != NULL - ) { - return idxTree; + if (objPtr != NULL + && (idxTree = TclStrIdxTreeGetFromObj(objPtr)) != NULL) { + return idxTree; } else { /* build new index */ @@ -1242,28 +1263,25 @@ ClockMCGetListIdxTree( objPtr = TclStrIdxTreeNewObj(); if ((idxTree = TclStrIdxTreeGetFromObj(objPtr)) == NULL) { - goto done; /* unexpected, but ...*/ + goto done; /* unexpected, but ...*/ } valObj = ClockMCGet(opts, mcKey); if (valObj == NULL) { goto done; } - - if (TclListObjGetElements(opts->interp, valObj, - &lstc, &lstv) != TCL_OK) { + if (TclListObjGetElements(opts->interp, valObj, &lstc, &lstv) != TCL_OK) { goto done; - }; - + } if (TclStrIdxTreeBuildFromList(idxTree, lstc, lstv, NULL) != TCL_OK) { goto done; } ClockMCSetIdx(opts, mcKey, objPtr); objPtr = NULL; - }; + } -done: + done: if (objPtr) { Tcl_DecrRefCount(objPtr); idxTree = NULL; @@ -1296,40 +1314,35 @@ done: static TclStrIdxTree * ClockMCGetMultiListIdxTree( ClockFmtScnCmdArgs *opts, - int mcKey, + int mcKey, int *mcKeys) { TclStrIdxTree * idxTree; Tcl_Obj *objPtr = ClockMCGetIdx(opts, mcKey); - if ( objPtr != NULL - && (idxTree = TclStrIdxTreeGetFromObj(objPtr)) != NULL - ) { - return idxTree; + if (objPtr != NULL + && (idxTree = TclStrIdxTreeGetFromObj(objPtr)) != NULL) { + return idxTree; } else { /* build new index */ Tcl_Obj **lstv; - Tcl_Size lstc; + Tcl_Size lstc; Tcl_Obj *valObj; objPtr = TclStrIdxTreeNewObj(); if ((idxTree = TclStrIdxTreeGetFromObj(objPtr)) == NULL) { - goto done; /* unexpected, but ...*/ + goto done; /* unexpected, but ...*/ } while (*mcKeys) { - valObj = ClockMCGet(opts, *mcKeys); if (valObj == NULL) { goto done; } - - if (TclListObjGetElements(opts->interp, valObj, - &lstc, &lstv) != TCL_OK) { + if (TclListObjGetElements(opts->interp, valObj, &lstc, &lstv) != TCL_OK) { goto done; - }; - + } if (TclStrIdxTreeBuildFromList(idxTree, lstc, lstv, NULL) != TCL_OK) { goto done; } @@ -1338,9 +1351,9 @@ ClockMCGetMultiListIdxTree( ClockMCSetIdx(opts, mcKey, objPtr); objPtr = NULL; - }; + } -done: + done: if (objPtr) { Tcl_DecrRefCount(objPtr); idxTree = NULL; @@ -1370,12 +1383,14 @@ done: static inline int ClockStrIdxTreeSearch( - DateInfo *info, TclStrIdxTree *idxTree, int *val, - int minLen, int maxLen) + DateInfo *info, + TclStrIdxTree *idxTree, + int *val, + int minLen, + int maxLen) { - const char *f; - TclStrIdx *foundItem; - f = TclStrIdxTreeSearch(NULL, &foundItem, idxTree, + TclStrIdx *foundItem; + const char *f = TclStrIdxTreeSearch(NULL, &foundItem, idxTree, yyInput, yyInput + maxLen); if (f <= yyInput || (f - yyInput) < minLen) { @@ -1398,16 +1413,19 @@ ClockStrIdxTreeSearch( /* currently unused */ static int -StaticListSearch(ClockFmtScnCmdArgs *opts, - DateInfo *info, const char **lst, int *val) +StaticListSearch( + ClockFmtScnCmdArgs *opts, + DateInfo *info, + const char **lst, + int *val) { size_t len; const char **s = lst; + while (*s != NULL) { len = strlen(*s); - if ( len <= info->dateEnd - yyInput - && strncasecmp(yyInput, *s, len) == 0 - ) { + if (len <= info->dateEnd - yyInput + && strncasecmp(yyInput, *s, len) == 0) { *val = (s - lst); yyInput += len; break; @@ -1424,10 +1442,12 @@ StaticListSearch(ClockFmtScnCmdArgs *opts, static inline const char * FindWordEnd( ClockScanToken *tok, - const char * p, const char * end) + const char *p, + const char *end) { const char *x = tok->tokWord.start; const char *pfnd = p; + if (x == tok->tokWord.end - 1) { /* fast phase-out for single char word */ if (*p == *x) { return ++p; @@ -1443,8 +1463,10 @@ FindWordEnd( } static int -ClockScnToken_Month_Proc(ClockFmtScnCmdArgs *opts, - DateInfo *info, ClockScanToken *tok) +ClockScnToken_Month_Proc( + ClockFmtScnCmdArgs *opts, + DateInfo *info, + ClockScanToken *tok) { #if 0 /* currently unused, test purposes only */ @@ -1464,9 +1486,7 @@ ClockScnToken_Month_Proc(ClockFmtScnCmdArgs *opts, return TCL_RETURN; } yyMonth = (val % 12) + 1; - return TCL_OK; -#endif - +#else static int monthsKeys[] = {MCLIT_MONTHS_FULL, MCLIT_MONTHS_ABBREV, 0}; int ret, val; @@ -1488,13 +1508,15 @@ ClockScnToken_Month_Proc(ClockFmtScnCmdArgs *opts, } yyMonth = val; +#endif return TCL_OK; - } static int -ClockScnToken_DayOfWeek_Proc(ClockFmtScnCmdArgs *opts, - DateInfo *info, ClockScanToken *tok) +ClockScnToken_DayOfWeek_Proc( + ClockFmtScnCmdArgs *opts, + DateInfo *info, + ClockScanToken *tok) { static int dowKeys[] = {MCLIT_DAYS_OF_WEEK_ABBREV, MCLIT_DAYS_OF_WEEK_FULL, 0}; @@ -1506,10 +1528,8 @@ ClockScnToken_DayOfWeek_Proc(ClockFmtScnCmdArgs *opts, DetermineGreedySearchLen(info, tok, &minLen, &maxLen); /* %u %w %Ou %Ow */ - if ( curTok != 'a' && curTok != 'A' - && ((minLen <= 1 && maxLen >= 1) || PTR2INT(tok->map->data)) - ) { - + if (curTok != 'a' && curTok != 'A' + && ((minLen <= 1 && maxLen >= 1) || PTR2INT(tok->map->data))) { val = -1; if (PTR2INT(tok->map->data) == 0) { @@ -1529,22 +1549,22 @@ ClockScnToken_DayOfWeek_Proc(ClockFmtScnCmdArgs *opts, --val; } - if (val != -1) { - if (val == 0) { - val = 7; - } - if (val > 7) { - Tcl_SetObjResult(opts->interp, Tcl_NewStringObj("day of week is greater than 7", -1)); - Tcl_SetErrorCode(opts->interp, "CLOCK", "badDayOfWeek", (char *)NULL); - return TCL_ERROR; - } - info->date.dayOfWeek = val; - yyInput++; - return TCL_OK; + if (val == -1) { + return TCL_RETURN; } - - return TCL_RETURN; + if (val == 0) { + val = 7; + } + if (val > 7) { + Tcl_SetObjResult(opts->interp, Tcl_NewStringObj( + "day of week is greater than 7", -1)); + Tcl_SetErrorCode(opts->interp, "CLOCK", "badDayOfWeek", (char *)NULL); + return TCL_ERROR; + } + info->date.dayOfWeek = val; + yyInput++; + return TCL_OK; } /* %a %A */ @@ -1564,12 +1584,13 @@ ClockScnToken_DayOfWeek_Proc(ClockFmtScnCmdArgs *opts, } info->date.dayOfWeek = val; return TCL_OK; - } static int -ClockScnToken_amPmInd_Proc(ClockFmtScnCmdArgs *opts, - DateInfo *info, ClockScanToken *tok) +ClockScnToken_amPmInd_Proc( + ClockFmtScnCmdArgs *opts, + DateInfo *info, + ClockScanToken *tok) { int ret, val; int minLen, maxLen; @@ -1584,8 +1605,7 @@ ClockScnToken_amPmInd_Proc(ClockFmtScnCmdArgs *opts, return TCL_ERROR; } - ret = ObjListSearch(info, &val, amPmObj, 2, - minLen, maxLen); + ret = ObjListSearch(info, &val, amPmObj, 2, minLen, maxLen); if (ret != TCL_OK) { return ret; } @@ -1600,8 +1620,10 @@ ClockScnToken_amPmInd_Proc(ClockFmtScnCmdArgs *opts, } static int -ClockScnToken_LocaleERA_Proc(ClockFmtScnCmdArgs *opts, - DateInfo *info, ClockScanToken *tok) +ClockScnToken_LocaleERA_Proc( + ClockFmtScnCmdArgs *opts, + DateInfo *info, + ClockScanToken *tok) { ClockClientData *dataPtr = (ClockClientData *)opts->clientData; @@ -1622,8 +1644,7 @@ ClockScnToken_LocaleERA_Proc(ClockFmtScnCmdArgs *opts, return TCL_ERROR; } - ret = ObjListSearch(info, &val, eraObj, 6, - minLen, maxLen); + ret = ObjListSearch(info, &val, eraObj, 6, minLen, maxLen); if (ret != TCL_OK) { return ret; } @@ -1638,8 +1659,10 @@ ClockScnToken_LocaleERA_Proc(ClockFmtScnCmdArgs *opts, } static int -ClockScnToken_LocaleListMatcher_Proc(ClockFmtScnCmdArgs *opts, - DateInfo *info, ClockScanToken *tok) +ClockScnToken_LocaleListMatcher_Proc( + ClockFmtScnCmdArgs *opts, + DateInfo *info, + ClockScanToken *tok) { int ret, val; int minLen, maxLen; @@ -1667,26 +1690,31 @@ ClockScnToken_LocaleListMatcher_Proc(ClockFmtScnCmdArgs *opts, } static int -ClockScnToken_JDN_Proc(TCL_UNUSED(ClockFmtScnCmdArgs *), - DateInfo *info, ClockScanToken *tok) +ClockScnToken_JDN_Proc( + TCL_UNUSED(ClockFmtScnCmdArgs *), + DateInfo *info, + ClockScanToken *tok) { int minLen, maxLen; - const char *p = yyInput, *end; const char *s; - Tcl_WideInt intJD; int fractJD = 0, fractJDDiv = 1; + const char *p = yyInput, *end, *s; + Tcl_WideInt intJD; + int fractJD = 0, fractJDDiv = 1; DetermineGreedySearchLen(info, tok, &minLen, &maxLen); end = yyInput + maxLen; /* currently positive astronomic dates only */ - if (*p == '+' || *p == '-') { p++; }; + if (*p == '+' || *p == '-') { + p++; + } s = p; while (p < end && isdigit(UCHAR(*p))) { p++; } - if ( _str2wideInt(&intJD, s, p, (*yyInput != '-' ? 1 : -1)) != TCL_OK) { + if (_str2wideInt(&intJD, s, p, (*yyInput != '-' ? 1 : -1)) != TCL_OK) { return TCL_RETURN; - }; + } yyInput = p; if (p >= end || *p++ != '.') { /* allow pure integer JDN */ /* by astronomical JD the seconds of day offs is 12 hours */ @@ -1702,12 +1730,12 @@ ClockScnToken_JDN_Proc(TCL_UNUSED(ClockFmtScnCmdArgs *), fractJDDiv *= 10; p++; } - if ( _str2int(&fractJD, s, p, 1) != TCL_OK) { + if (_str2int(&fractJD, s, p, 1) != TCL_OK) { return TCL_RETURN; - }; + } yyInput = p; -done: + done: /* * Build a date from julian day (integer and fraction). * Note, astronomical JDN starts at noon in opposite to calendar julianday. @@ -1723,9 +1751,9 @@ done: yydate.julianDay = intJD; yydate.seconds = - -210866803200LL - + ( SECONDS_PER_DAY * intJD ) - + ( fractJD ); + -210866803200LL + + (SECONDS_PER_DAY * intJD) + + fractJD; info->flags |= CLF_POSIXSEC; @@ -1733,8 +1761,10 @@ done: } static int -ClockScnToken_TimeZone_Proc(ClockFmtScnCmdArgs *opts, - DateInfo *info, ClockScanToken *tok) +ClockScnToken_TimeZone_Proc( + ClockFmtScnCmdArgs *opts, + DateInfo *info, + ClockScanToken *tok) { int minLen, maxLen; int len = 0; @@ -1746,21 +1776,31 @@ ClockScnToken_TimeZone_Proc(ClockFmtScnCmdArgs *opts, /* numeric timezone */ if (*p == '+' || *p == '-') { /* max chars in numeric zone = "+00:00:00" */ - #define MAX_ZONE_LEN 9 +#define MAX_ZONE_LEN 9 char buf[MAX_ZONE_LEN + 1]; char *bp = buf; - *bp++ = *p++; len++; - if (maxLen > MAX_ZONE_LEN) + + *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 (!isdigit(UCHAR(*p))) { + break; + } + *bp++ = *p++; + len++; + if (!isdigit(UCHAR(*p))) { + break; + } + *bp++ = *p++; + len++; if (len + 2 < maxLen) { if (*p == ':') { - p++; len++; + p++; + len++; } } } @@ -1769,21 +1809,22 @@ ClockScnToken_TimeZone_Proc(ClockFmtScnCmdArgs *opts, if (len < minLen) { return TCL_RETURN; } - #undef MAX_ZONE_LEN +#undef MAX_ZONE_LEN /* timezone */ - tzObjStor = Tcl_NewStringObj(buf, bp-buf); + tzObjStor = Tcl_NewStringObj(buf, bp - buf); } else { /* legacy (alnum) timezone like CEST, etc. */ - if (maxLen > 4) + if (maxLen > 4) { maxLen = 4; + } while (len < maxLen) { - if ( (*p & 0x80) - || (!isalpha(UCHAR(*p)) && !isdigit(UCHAR(*p))) - ) { /* INTL: ISO only. */ + if ((*p & 0x80) + || (!isalpha(UCHAR(*p)) && !isdigit(UCHAR(*p)))) { /* INTL: ISO only. */ break; } - p++; len++; + p++; + len++; } if (len < minLen) { @@ -1791,7 +1832,7 @@ ClockScnToken_TimeZone_Proc(ClockFmtScnCmdArgs *opts, } /* timezone */ - tzObjStor = Tcl_NewStringObj(yyInput, p-yyInput); + tzObjStor = Tcl_NewStringObj(yyInput, p - yyInput); /* convert using dict */ } @@ -1800,7 +1841,7 @@ ClockScnToken_TimeZone_Proc(ClockFmtScnCmdArgs *opts, Tcl_IncrRefCount(tzObjStor); opts->timezoneObj = ClockSetupTimeZone(opts->clientData, opts->interp, - tzObjStor); + tzObjStor); Tcl_DecrRefCount(tzObjStor); if (opts->timezoneObj == NULL) { @@ -1808,16 +1849,17 @@ ClockScnToken_TimeZone_Proc(ClockFmtScnCmdArgs *opts, } yyInput += len; - return TCL_OK; } static int -ClockScnToken_StarDate_Proc(TCL_UNUSED(ClockFmtScnCmdArgs *), - DateInfo *info, ClockScanToken *tok) +ClockScnToken_StarDate_Proc( + TCL_UNUSED(ClockFmtScnCmdArgs *), + DateInfo *info, + ClockScanToken *tok) { int minLen, maxLen; - const char *p = yyInput, *end; const char *s; + const char *p = yyInput, *end, *s; int year, fractYear, fractDayDiv, fractDay; static const char *stardatePref = "stardate "; @@ -1838,7 +1880,9 @@ ClockScnToken_StarDate_Proc(TCL_UNUSED(ClockFmtScnCmdArgs *), return TCL_RETURN; } /* currently positive stardate only */ - if (*p == '+') { p++; }; + if (*p == '+') { + p++; + } s = p; while (p < end && isdigit(UCHAR(*p))) { p++; @@ -1846,10 +1890,10 @@ ClockScnToken_StarDate_Proc(TCL_UNUSED(ClockFmtScnCmdArgs *), if (p >= end || p - s < 4) { return TCL_RETURN; } - if ( _str2int(&year, s, p-3, 1) != TCL_OK - || _str2int(&fractYear, p-3, p, 1) != TCL_OK) { + if (_str2int(&year, s, p - 3, 1) != TCL_OK + || _str2int(&fractYear, p - 3, p, 1) != TCL_OK) { return TCL_RETURN; - }; + } if (*p++ != '.') { return TCL_RETURN; } @@ -1859,9 +1903,9 @@ ClockScnToken_StarDate_Proc(TCL_UNUSED(ClockFmtScnCmdArgs *), fractDayDiv *= 10; p++; } - if ( _str2int(&fractDay, s, p, 1) != TCL_OK) { + if (_str2int(&fractDay, s, p, 1) != TCL_OK) { return TCL_RETURN; - }; + } yyInput = p; /* Build a date from year and fraction. */ @@ -1883,16 +1927,15 @@ ClockScnToken_StarDate_Proc(TCL_UNUSED(ClockFmtScnCmdArgs *), GetJulianDayFromEraYearDay(&yydate, GREGORIAN_CHANGE_DATE); yydate.localSeconds = - -210866803200LL - + ( SECONDS_PER_DAY * yydate.julianDay ) - + ( SECONDS_PER_DAY * fractDay / fractDayDiv ); + -210866803200LL + + (SECONDS_PER_DAY * yydate.julianDay) + + (SECONDS_PER_DAY * fractDay / fractDayDiv); return TCL_OK; } -static const char *ScnSTokenMapIndex = - "dmbyYHMSpJjCgGVazUsntQ"; -static ClockScanTokenMap ScnSTokenMap[] = { +static const char *ScnSTokenMapIndex = "dmbyYHMSpJjCgGVazUsntQ"; +static const ClockScanTokenMap ScnSTokenMap[] = { /* %d %e */ {CTOKT_INT, CLF_DAYOFMONTH, 0, 1, 2, offsetof(DateInfo, date.dayOfMonth), NULL, NULL}, @@ -1963,9 +2006,8 @@ static const char *ScnSTokenMapAliasIndex[2] = { "dmbbHHHpaaazU" }; -static const char *ScnETokenMapIndex = - "EJjys"; -static ClockScanTokenMap ScnETokenMap[] = { +static const char *ScnETokenMapIndex = "EJjys"; +static const ClockScanTokenMap ScnETokenMap[] = { /* %EE */ {CTOKT_PARSER, 0, 0, 0, 0xffff, offsetof(DateInfo, date.year), ClockScnToken_LocaleERA_Proc, (void *)MCLIT_LOCALE_NUMERALS}, @@ -1987,9 +2029,8 @@ static const char *ScnETokenMapAliasIndex[2] = { "" }; -static const char *ScnOTokenMapIndex = - "dmyHMSu"; -static ClockScanTokenMap ScnOTokenMap[] = { +static const char *ScnOTokenMapIndex = "dmyHMSu"; +static const ClockScanTokenMap ScnOTokenMap[] = { /* %Od %Oe */ {CTOKT_PARSER, CLF_DAYOFMONTH, 0, 0, 0xffff, offsetof(DateInfo, date.dayOfMonth), ClockScnToken_LocaleListMatcher_Proc, (void *)MCLIT_LOCALE_NUMERALS}, @@ -2018,24 +2059,21 @@ static const char *ScnOTokenMapAliasIndex[2] = { }; /* Token map reserved for CTOKT_SPACE */ -static ClockScanTokenMap ScnSpaceTokenMap = { - CTOKT_SPACE, 0, 0, 1, 1, 0, - NULL, NULL +static const ClockScanTokenMap ScnSpaceTokenMap = { + CTOKT_SPACE, 0, 0, 1, 1, 0, NULL, NULL }; -static ClockScanTokenMap ScnWordTokenMap = { - CTOKT_WORD, 0, 0, 1, 1, 0, - NULL, NULL +static const ClockScanTokenMap ScnWordTokenMap = { + CTOKT_WORD, 0, 0, 1, 1, 0, NULL, NULL }; - -static inline unsigned int +static inline unsigned EstimateTokenCount( const char *fmt, const char *end) { const char *p = fmt; - unsigned int tokcnt; + unsigned tokcnt; /* estimate token count by % char and format length */ tokcnt = 0; while (p <= end) { @@ -2046,7 +2084,7 @@ EstimateTokenCount( } p = fmt + tokcnt * 2; if (p < end) { - if ((unsigned int)(end - p) < tokcnt) { + if ((unsigned)(end - p) < tokcnt) { tokcnt += (end - p); } else { tokcnt += tokcnt; @@ -2056,13 +2094,15 @@ EstimateTokenCount( } #define AllocTokenInChain(tok, chain, tokCnt, type) \ - if (++(tok) >= (chain) + (tokCnt)) { \ - chain = (type)ckrealloc((char *)(chain), \ + if (++(tok) >= (chain) + (tokCnt)) { \ + chain = (type) attemptckrealloc((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; \ - } \ + if ((chain) == NULL) { \ + goto done; \ + } \ + (tok) = (chain) + (tokCnt); \ + (tokCnt) += CLOCK_MIN_TOK_CHAIN_BLOCK_SIZE; \ + } \ memset(tok, 0, sizeof(*(tok))); /* @@ -2071,7 +2111,7 @@ EstimateTokenCount( ClockFmtScnStorage * ClockGetOrParseScanFormat( Tcl_Interp *interp, /* Tcl interpreter */ - Tcl_Obj *formatObj) /* Format container */ + Tcl_Obj *formatObj) /* Format container */ { ClockFmtScnStorage *fss; @@ -2090,7 +2130,7 @@ ClockGetOrParseScanFormat( /* first time scanning - tokenize format */ if (fss->scnTok == NULL) { ClockScanToken *tok, *scnTok; - unsigned int tokCnt; + unsigned tokCnt; const char *p, *e, *cp; e = p = HashEntry4FmtScn(fss)->key.string; @@ -2101,17 +2141,17 @@ ClockGetOrParseScanFormat( fss->scnSpaceCount = 0; - scnTok = tok = (ClockScanToken *)ckalloc(sizeof(*tok) * fss->scnTokC); + scnTok = tok = (ClockScanToken *) ckalloc(sizeof(*tok) * fss->scnTokC); memset(tok, 0, sizeof(*(tok))); tokCnt = 1; while (p < e) { switch (*p) { - case '%': - if (1) { - ClockScanTokenMap * scnMap = ScnSTokenMap; - const char *mapIndex = ScnSTokenMapIndex, - **aliasIndex = ScnSTokenMapAliasIndex; - if (p+1 >= e) { + case '%': { + const ClockScanTokenMap *scnMap = ScnSTokenMap; + const char *mapIndex = ScnSTokenMapIndex; + const char **aliasIndex = ScnSTokenMapAliasIndex; + + if (p + 1 >= e) { goto word_tok; } p++; @@ -2122,23 +2162,23 @@ ClockGetOrParseScanFormat( * because current mapping should be "...%%..." -> "...%..." */ tok->map = &ScnWordTokenMap; tok->tokWord.start = p; - tok->tokWord.end = p+1; - AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *); tokCnt++; + tok->tokWord.end = p + 1; + AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *); + tokCnt++; p++; continue; - break; case 'E': scnMap = ScnETokenMap, mapIndex = ScnETokenMapIndex, aliasIndex = ScnETokenMapAliasIndex; p++; - break; + break; case 'O': scnMap = ScnOTokenMap, mapIndex = ScnOTokenMapIndex, aliasIndex = ScnOTokenMapAliasIndex; p++; - break; + break; } /* search direct index */ cp = strchr(mapIndex, *p); @@ -2146,15 +2186,21 @@ ClockGetOrParseScanFormat( /* search wrapper index (multiple chars for same token) */ cp = strchr(aliasIndex[0], *p); if (!cp || *cp == '\0') { - p--; if (scnMap != ScnSTokenMap) p--; + p--; + if (scnMap != ScnSTokenMap) { + p--; + } goto word_tok; } cp = strchr(mapIndex, aliasIndex[1][cp - aliasIndex[0]]); if (!cp || *cp == '\0') { /* unexpected, but ... */ - #ifdef DEBUG +#ifdef DEBUG Tcl_Panic("token \"%c\" has no map in wrapper resolver", *p); - #endif - p--; if (scnMap != ScnSTokenMap) p--; +#endif + p--; + if (scnMap != ScnSTokenMap) { + p--; + } goto word_tok; } } @@ -2163,7 +2209,7 @@ ClockGetOrParseScanFormat( /* calculate look ahead value by standing together tokens */ if (tok > scnTok) { - ClockScanToken *prevTok = tok - 1; + ClockScanToken *prevTok = tok - 1; while (prevTok >= scnTok) { if (prevTok->map->type != tok->map->type) { @@ -2177,58 +2223,59 @@ ClockGetOrParseScanFormat( } /* increase space count used in format */ - if ( tok->map->type == CTOKT_CHAR - && isspace(UCHAR(*((char *)tok->map->data))) - ) { + if (tok->map->type == CTOKT_CHAR + && isspace(UCHAR(*((char *)tok->map->data)))) { fss->scnSpaceCount++; } /* next token */ - AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *); tokCnt++; + AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *); + tokCnt++; p++; continue; } - break; default: - if ( *p == ' ' || isspace(UCHAR(*p)) ) { - tok->map = &ScnSpaceTokenMap; - tok->tokWord.start = p++; - while (p < e && isspace(UCHAR(*p))) { - p++; + if (*p == ' ' || isspace(UCHAR(*p))) { + tok->map = &ScnSpaceTokenMap; + tok->tokWord.start = p++; + while (p < e && isspace(UCHAR(*p))) { + p++; + } + tok->tokWord.end = p; + /* increase space count used in format */ + fss->scnSpaceCount++; + /* next token */ + AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *); + tokCnt++; + continue; } - tok->tokWord.end = p; - /* increase space count used in format */ - fss->scnSpaceCount++; - /* next token */ - AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *); tokCnt++; - continue; - } word_tok: - if (1) { - ClockScanToken *wordTok = tok; + { + ClockScanToken *wordTok = tok; if (tok > scnTok && (tok-1)->map == &ScnWordTokenMap) { - wordTok = tok-1; + wordTok = tok - 1; } /* new word token */ if (wordTok == tok) { wordTok->tokWord.start = p; wordTok->map = &ScnWordTokenMap; - AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *); tokCnt++; + AllocTokenInChain(tok, scnTok, fss->scnTokC, ClockScanToken *); + tokCnt++; } if (isspace(UCHAR(*p))) { fss->scnSpaceCount++; } p = Tcl_UtfNext(p); wordTok->tokWord.end = p; + break; } - break; } } /* calculate end distance value for each tokens */ if (tok > scnTok) { - unsigned int endDist = 0; - ClockScanToken *prevTok = tok-1; + unsigned endDist = 0; + ClockScanToken *prevTok = tok-1; while (prevTok >= scnTok) { prevTok->endDistance = endDist; @@ -2244,7 +2291,8 @@ word_tok: /* correct count of real used tokens and free mem if desired * (1 is acceptable delta to prevent memory fragmentation) */ if (fss->scnTokC > tokCnt + (CLOCK_MIN_TOK_CHAIN_BLOCK_SIZE / 2)) { - if ( (tok = (ClockScanToken *)ckrealloc(scnTok, tokCnt * sizeof(*tok))) != NULL ) { + if ((tok = (ClockScanToken *) + attemptckrealloc(scnTok, tokCnt * sizeof(*tok))) != NULL) { scnTok = tok; } } @@ -2253,7 +2301,7 @@ word_tok: fss->scnTok = scnTok; fss->scnTokC = tokCnt; } -done: + done: Tcl_MutexUnlock(&ClockFmtMutex); return fss; @@ -2264,16 +2312,16 @@ done: */ int ClockScan( - DateInfo *info, /* Date fields used for parsing & converting */ + DateInfo *info, /* Date fields used for parsing & converting */ Tcl_Obj *strObj, /* String containing the time to scan */ ClockFmtScnCmdArgs *opts) /* Command options */ { ClockClientData *dataPtr = (ClockClientData *)opts->clientData; - ClockFmtScnStorage *fss; - ClockScanToken *tok; - ClockScanTokenMap *map; + ClockFmtScnStorage *fss; + ClockScanToken *tok; + const ClockScanTokenMap *map; const char *p, *x, *end; - unsigned short int flags = 0; + unsigned short flags = 0; int ret = TCL_ERROR; /* get localized format */ @@ -2281,9 +2329,8 @@ ClockScan( return TCL_ERROR; } - if ( !(fss = ClockGetOrParseScanFormat(opts->interp, opts->formatObj)) - || !(tok = fss->scnTok) - ) { + if (!(fss = ClockGetOrParseScanFormat(opts->interp, opts->formatObj)) + || !(tok = fss->scnTok)) { return TCL_ERROR; } @@ -2304,7 +2351,7 @@ ClockScan( x = end; while (p < end) { if (isspace(UCHAR(*p))) { - x = ++p; /* after first space in space block */ + x = ++p; /* after first space in space block */ yySpaceCount++; while (p < end && isspace(UCHAR(*p))) { p++; @@ -2330,11 +2377,10 @@ ClockScan( 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 - && map->type != CTOKT_CHAR ) - ) { + if (!(opts->flags & CLF_STRICT) + && (map->type != CTOKT_SPACE + && map->type != CTOKT_WORD + && map->type != CTOKT_CHAR)) { while (p < end && isspace(UCHAR(*p))) { yySpaceCount--; p++; @@ -2345,17 +2391,19 @@ ClockScan( if (p >= end) { break; } - switch (map->type) - { + switch (map->type) { case CTOKT_INT: - case CTOKT_WIDE: - if (1) { + case CTOKT_WIDE: { int minLen, size; int sign = 1; + if (map->flags & CLF_SIGNED) { - if (*p == '+') { yyInput = ++p; } - else - if (*p == '-') { yyInput = ++p; sign = -1; }; + if (*p == '+') { + yyInput = ++p; + } else if (*p == '-') { + yyInput = ++p; + sign = -1; + } } DetermineGreedySearchLen(info, tok, &minLen, &size); @@ -2369,7 +2417,8 @@ ClockScan( } /* string 2 number, put number into info structure by offset */ if (map->offs) { - p = yyInput; x = p + size; + p = yyInput; + x = p + size; if (map->type == CTOKT_INT) { if (size <= 10) { _str2int_no((int *)(((char *)info) + map->offs), @@ -2395,23 +2444,21 @@ ClockScan( } flags = (flags & ~map->clearFlags) | map->flags; } + break; } - break; case CTOKT_PARSER: switch (map->parser(opts, info, tok)) { - case TCL_OK: + case TCL_OK: break; - case TCL_RETURN: - if ((map->flags & CLF_OPTIONAL)) { - yyInput = p; - continue; - } - goto not_match; - break; - default: - goto done; - break; - }; + case TCL_RETURN: + if ((map->flags & CLF_OPTIONAL)) { + yyInput = p; + continue; + } + goto not_match; + default: + goto done; + } /* decrement count for possible spaces in match */ while (p < yyInput) { if (isspace(UCHAR(*p))) { @@ -2421,7 +2468,7 @@ ClockScan( } p = yyInput; flags = (flags & ~map->clearFlags) | map->flags; - break; + break; case CTOKT_SPACE: /* at least one space */ if (!isspace(UCHAR(*p))) { @@ -2435,7 +2482,7 @@ ClockScan( yySpaceCount--; p++; } - break; + break; case CTOKT_WORD: x = FindWordEnd(tok, p, end); if (!x) { @@ -2443,7 +2490,7 @@ ClockScan( goto not_match; } p = x; - break; + break; case CTOKT_CHAR: x = (char *)map->data; if (*x != *p) { @@ -2454,13 +2501,13 @@ ClockScan( yySpaceCount--; } p++; - break; + break; } } /* check end was reached */ if (p < end) { /* in non-strict mode bypass spaces at end of input */ - if ( !(opts->flags & CLF_STRICT) && isspace(UCHAR(*p)) ) { + if (!(opts->flags & CLF_STRICT) && isspace(UCHAR(*p))) { p++; while (p < end && isspace(UCHAR(*p))) { p++; @@ -2500,27 +2547,27 @@ ClockScan( /* dd precedence below ddd */ switch (flags & (CLF_MONTH|CLF_DAYOFYEAR|CLF_DAYOFMONTH)) { - case (CLF_DAYOFYEAR|CLF_DAYOFMONTH): + case (CLF_DAYOFYEAR | CLF_DAYOFMONTH): /* miss month: ddd over dd (without month) */ flags &= ~CLF_DAYOFMONTH; /* fallthrough */ - case (CLF_DAYOFYEAR): + case CLF_DAYOFYEAR: /* ddd over naked weekday */ if (!(flags & CLF_ISO8601YEAR)) { flags &= ~CLF_ISO8601WEAK; } break; - case (CLF_MONTH|CLF_DAYOFYEAR|CLF_DAYOFMONTH): + case CLF_MONTH | CLF_DAYOFYEAR | CLF_DAYOFMONTH: /* both available: mmdd over ddd */ - case (CLF_MONTH|CLF_DAYOFMONTH): - case (CLF_DAYOFMONTH): + case CLF_MONTH | CLF_DAYOFMONTH: + case CLF_DAYOFMONTH: /* mmdd / dd over naked weekday */ if (!(flags & CLF_ISO8601YEAR)) { flags &= ~CLF_ISO8601WEAK; } break; /* neither mmdd nor ddd available */ - case 0: + case 0: /* but we have day of the week, which can be used */ if (flags & CLF_DAYOFWEEK) { /* prefer week based calculation of julianday */ @@ -2529,24 +2576,21 @@ ClockScan( } /* YearWeekDay below YearMonthDay */ - if ( (flags & CLF_ISO8601WEAK) - && ( (flags & (CLF_YEAR|CLF_DAYOFYEAR)) == (CLF_YEAR|CLF_DAYOFYEAR) - || (flags & (CLF_YEAR|CLF_DAYOFMONTH|CLF_MONTH)) == (CLF_YEAR|CLF_DAYOFMONTH|CLF_MONTH) - ) - ) { + if ((flags & CLF_ISO8601WEAK) + && ((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_ISO8601WEAK; - } - else - /* yymmdd or yyddd over naked weekday */ - if (!(flags & CLF_ISO8601YEAR)) { + } else if (!(flags & CLF_ISO8601YEAR)) { + /* yymmdd or yyddd over naked weekday */ flags &= ~CLF_ISO8601WEAK; } } - if ( (flags & CLF_YEAR) ) { + if (flags & CLF_YEAR) { if (yyYear < 100) { if (!(flags & CLF_CENTURY)) { if (yyYear >= dataPtr->yearOfCenturySwitch) { @@ -2558,13 +2602,11 @@ ClockScan( } } } - if ( (flags & (CLF_ISO8601WEAK|CLF_ISO8601YEAR)) ) { - if ((flags & (CLF_ISO8601YEAR|CLF_YEAR)) == CLF_YEAR) { + if (flags & (CLF_ISO8601WEAK | CLF_ISO8601YEAR)) { + if ((flags & (CLF_ISO8601YEAR | CLF_YEAR)) == CLF_YEAR) { /* for calculations expected iso year */ info->date.iso8601Year = yyYear; - } - else - if (info->date.iso8601Year < 100) { + } else if (info->date.iso8601Year < 100) { if (!(flags & CLF_ISO8601CENTURY)) { if (info->date.iso8601Year >= dataPtr->yearOfCenturySwitch) { info->date.iso8601Year -= 100; @@ -2574,7 +2616,7 @@ ClockScan( info->date.iso8601Year += info->dateCentury * 100; } } - if ((flags & (CLF_ISO8601YEAR|CLF_YEAR)) == CLF_ISO8601YEAR) { + if ((flags & (CLF_ISO8601YEAR | CLF_YEAR)) == CLF_ISO8601YEAR) { /* for calculations expected year (e. g. CLF_ISO8601WEAK not set) */ yyYear = info->date.iso8601Year; } @@ -2583,7 +2625,7 @@ ClockScan( } /* if no time - reset time */ - if (!(flags & (CLF_TIME|CLF_LOCALSEC|CLF_POSIXSEC))) { + if (!(flags & (CLF_TIME | CLF_LOCALSEC | CLF_POSIXSEC))) { info->flags |= CLF_ASSEMBLE_SECONDS; yydate.localSeconds = 0; } @@ -2591,9 +2633,8 @@ ClockScan( if (flags & CLF_TIME) { info->flags |= CLF_ASSEMBLE_SECONDS; yySecondOfDay = ToSeconds(yyHour, yyMinutes, - yySeconds, yyMeridian); - } else - if (!(flags & (CLF_LOCALSEC|CLF_POSIXSEC))) { + yySeconds, yyMeridian); + } else if (!(flags & (CLF_LOCALSEC | CLF_POSIXSEC))) { info->flags |= CLF_ASSEMBLE_SECONDS; yySecondOfDay = yydate.localSeconds % SECONDS_PER_DAY; } @@ -2603,34 +2644,32 @@ ClockScan( info->flags |= flags; ret = TCL_OK; - goto done; + done: + return ret; -overflow: + /* Error case reporting. */ - Tcl_SetObjResult(opts->interp, Tcl_NewStringObj("integer value too large to represent", - -1)); + overflow: + Tcl_SetObjResult(opts->interp, Tcl_NewStringObj( + "integer value too large to represent", -1)); Tcl_SetErrorCode(opts->interp, "CLOCK", "dateTooLarge", (char *)NULL); goto done; -not_match: - - #if 1 - Tcl_SetObjResult(opts->interp, Tcl_NewStringObj("input string does not match supplied format", - -1)); - #else - /* to debug where exactly scan breaks */ + not_match: +#if 1 + Tcl_SetObjResult(opts->interp, Tcl_NewStringObj( + "input string does not match supplied format", -1)); +#else + /* to debug where exactly scan breaks */ Tcl_SetObjResult(opts->interp, Tcl_ObjPrintf( - "input string \"%s\" does not match supplied format \"%s\"," - " locale \"%s\" - token \"%s\"", - info->dateStart, HashEntry4FmtScn(fss)->key.string, - TclGetString(opts->localeObj), - tok && tok->tokWord.start ? tok->tokWord.start : "NULL")); - #endif + "input string \"%s\" does not match supplied format \"%s\"," + " locale \"%s\" - token \"%s\"", + info->dateStart, HashEntry4FmtScn(fss)->key.string, + TclGetString(opts->localeObj), + tok && tok->tokWord.start ? tok->tokWord.start : "NULL")); +#endif Tcl_SetErrorCode(opts->interp, "CLOCK", "badInputString", (char *)NULL); - -done: - - return ret; + goto done; } #define FrmResultIsAllocated(dateFmt) \ @@ -2642,19 +2681,19 @@ FrmResultAllocate( int len) { int needed = dateFmt->output + len - dateFmt->resEnd; - if (needed >= 0) { /* >= 0 - regards NTS zero */ + if (needed >= 0) { /* >= 0 - regards NTS zero */ int newsize = dateFmt->resEnd - dateFmt->resMem - + needed + MIN_FMT_RESULT_BLOCK_ALLOC*2; + + needed + MIN_FMT_RESULT_BLOCK_ALLOC * 2; char *newRes; /* differentiate between stack and memory */ if (!FrmResultIsAllocated(dateFmt)) { - newRes = (char *)ckalloc(newsize); + newRes = (char *) attemptckalloc(newsize); if (newRes == NULL) { return TCL_ERROR; } memcpy(newRes, dateFmt->resMem, dateFmt->output - dateFmt->resMem); } else { - newRes = (char *)ckrealloc(dateFmt->resMem, newsize); + newRes = (char *) attemptckrealloc(dateFmt->resMem, newsize); if (newRes == NULL) { return TCL_ERROR; } @@ -2673,7 +2712,7 @@ ClockFmtToken_HourAMPM_Proc( TCL_UNUSED(ClockFormatToken *), int *val) { - *val = ( ( *val + SECONDS_PER_DAY - 3600 ) / 3600 ) % 12 + 1; + *val = ((*val + SECONDS_PER_DAY - 3600) / 3600) % 12 + 1; return TCL_OK; } @@ -2697,7 +2736,9 @@ ClockFmtToken_AMPM_Proc( return TCL_ERROR; } s = TclGetStringFromObj(mcObj, &len); - if (FrmResultAllocate(dateFmt, len) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, len) != TCL_OK) { + return TCL_ERROR; + } memcpy(dateFmt->output, s, len + 1); if (*tok->tokWord.start == 'p') { len = Tcl_UtfToUpper(dateFmt->output); @@ -2726,19 +2767,22 @@ ClockFmtToken_StarDate_Proc( } /* Put together the StarDate as "Stardate %02d%03d.%1d" */ - if (FrmResultAllocate(dateFmt, 30) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, 30) != TCL_OK) { + return TCL_ERROR; + } memcpy(dateFmt->output, "Stardate ", 9); dateFmt->output += 9; dateFmt->output = _itoaw(dateFmt->output, - dateFmt->date.year - RODDENBERRY, '0', 2); + dateFmt->date.year - RODDENBERRY, '0', 2); dateFmt->output = _itoaw(dateFmt->output, - fractYear, '0', 3); + fractYear, '0', 3); *dateFmt->output++ = '.'; /* be sure positive after decimal point (note: clock-value can be negative) */ - v = dateFmt->date.secondOfDay / ( SECONDS_PER_DAY / 10 ); - if (v < 0) v = 10 + v; + v = dateFmt->date.secondOfDay / (SECONDS_PER_DAY / 10); + if (v < 0) { + v = 10 + v; + } dateFmt->output = _itoaw(dateFmt->output, v, '0', 1); - return TCL_OK; } static int @@ -2756,7 +2800,7 @@ ClockFmtToken_WeekOfYear_Proc( } dow++; } - *val = ( dateFmt->date.dayOfYear - dow + 7 ) / 7; + *val = (dateFmt->date.dayOfYear - dow + 7) / 7; return TCL_OK; } static int @@ -2771,16 +2815,18 @@ ClockFmtToken_JDN_Proc( /* Convert to JDN parts (regarding start offset) and time fraction */ fractJD = dateFmt->date.secondOfDay - - (int)tok->map->offs; /* 0 for calendar or 43200 for astro JD */ + - (int)tok->map->offs; /* 0 for calendar or 43200 for astro JD */ if (fractJD < 0) { intJD--; fractJD += SECONDS_PER_DAY; } - if (fractJD && intJD < 0) { /* avoid jump over 0, by negative JD's */ + if (fractJD && intJD < 0) { /* avoid jump over 0, by negative JD's */ intJD++; if (intJD == 0) { /* -0.0 / -0.9 has zero integer part, so append "-" extra */ - if (FrmResultAllocate(dateFmt, 1) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, 1) != TCL_OK) { + return TCL_ERROR; + } *dateFmt->output++ = '-'; } /* and inverse seconds of day, -0(75) -> -0.25 as float */ @@ -2788,33 +2834,42 @@ ClockFmtToken_JDN_Proc( } /* 21 is max width of (negative) wide-int (rather smaller, but anyway a time fraction below) */ - if (FrmResultAllocate(dateFmt, 21) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, 21) != TCL_OK) { + return TCL_ERROR; + } dateFmt->output = _witoaw(dateFmt->output, intJD, '0', 1); /* simplest cases .0 and .5 */ if (!fractJD || fractJD == (SECONDS_PER_DAY / 2)) { /* point + 0 or 5 */ - if (FrmResultAllocate(dateFmt, 1+1) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, 1 + 1) != TCL_OK) { + return TCL_ERROR; + } *dateFmt->output++ = '.'; *dateFmt->output++ = !fractJD ? '0' : '5'; *dateFmt->output = '\0'; return TCL_OK; } else { /* wrap the time fraction */ - #define JDN_MAX_PRECISION 8 - #define JDN_MAX_PRECBOUND 100000000 /* 10**JDN_MAX_PRECISION */ +#define JDN_MAX_PRECISION 8 +#define JDN_MAX_PRECBOUND 100000000 /* 10**JDN_MAX_PRECISION */ char *p; /* to float (part after floating point, + 0.5 to round it up) */ fractJD = (int)( - (double)fractJD * JDN_MAX_PRECBOUND / SECONDS_PER_DAY + 0.5 - ); + (double)fractJD * JDN_MAX_PRECBOUND / SECONDS_PER_DAY + 0.5); + /* point + integer (as time fraction after floating point) */ - if (FrmResultAllocate(dateFmt, 1+JDN_MAX_PRECISION) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, 1 + JDN_MAX_PRECISION) != TCL_OK) { + return TCL_ERROR; + } *dateFmt->output++ = '.'; p = _itoaw(dateFmt->output, fractJD, '0', JDN_MAX_PRECISION); + /* remove trailing zero's */ dateFmt->output++; - while (p > dateFmt->output && *(p-1) == '0') {p--;} + while (p > dateFmt->output && *(p-1) == '0') { + p--; + } *p = '\0'; dateFmt->output = p; } @@ -2831,11 +2886,13 @@ ClockFmtToken_TimeZone_Proc( int z = dateFmt->date.tzOffset; char sign = '+'; - if ( z < 0 ) { + if (z < 0) { z = -z; sign = '-'; } - if (FrmResultAllocate(dateFmt, 7) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, 7) != TCL_OK) { + return TCL_ERROR; + } *dateFmt->output++ = sign; dateFmt->output = _itoaw(dateFmt->output, z / 3600, '0', 2); z %= 3600; @@ -2846,16 +2903,20 @@ ClockFmtToken_TimeZone_Proc( } } else { Tcl_Obj * objPtr; - const char *s; Tcl_Size len; + const char *s; + Tcl_Size len; + /* convert seconds to local seconds to obtain tzName object */ if (ConvertUTCToLocal(opts->clientData, opts->interp, &dateFmt->date, opts->timezoneObj, GREGORIAN_CHANGE_DATE) != TCL_OK) { return TCL_ERROR; - }; + } objPtr = dateFmt->date.tzName; s = TclGetStringFromObj(objPtr, &len); - if (FrmResultAllocate(dateFmt, len) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, len) != TCL_OK) { + return TCL_ERROR; + } memcpy(dateFmt->output, s, len + 1); dateFmt->output += len; } @@ -2882,10 +2943,12 @@ ClockFmtToken_LocaleERA_Proc( return TCL_ERROR; } s = TclGetStringFromObj(mcObj, &len); - if (FrmResultAllocate(dateFmt, len) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, len) != TCL_OK) { + return TCL_ERROR; + } + memcpy(dateFmt->output, s, len + 1); dateFmt->output += len; - return TCL_OK; } @@ -2909,7 +2972,7 @@ ClockFmtToken_LocaleERAYear_Proc( } if (rowc != 0) { dateFmt->localeEra = LookupLastTransition(opts->interp, - dateFmt->date.localSeconds, rowc, rowv, NULL); + dateFmt->date.localSeconds, rowc, rowv, NULL); } if (dateFmt->localeEra == NULL) { dateFmt->localeEra = (Tcl_Obj*)1; @@ -2918,28 +2981,31 @@ ClockFmtToken_LocaleERAYear_Proc( /* if no LOCALE_ERAS in catalog or era not found */ if (dateFmt->localeEra == (Tcl_Obj*)1) { - if (FrmResultAllocate(dateFmt, 11) != TCL_OK) { return TCL_ERROR; }; - if (*tok->tokWord.start == 'C') { /* %EC */ + if (FrmResultAllocate(dateFmt, 11) != TCL_OK) { + return TCL_ERROR; + } + if (*tok->tokWord.start == 'C') { /* %EC */ *val = dateFmt->date.year / 100; dateFmt->output = _itoaw(dateFmt->output, - *val, '0', 2); - } else { /* %Ey */ + *val, '0', 2); + } else { /* %Ey */ *val = dateFmt->date.year % 100; dateFmt->output = _itoaw(dateFmt->output, - *val, '0', 2); + *val, '0', 2); } } else { Tcl_Obj *objPtr; const char *s; Tcl_Size len; - if (*tok->tokWord.start == 'C') { /* %EC */ + + if (*tok->tokWord.start == 'C') { /* %EC */ if (Tcl_ListObjIndex(opts->interp, dateFmt->localeEra, 1, - &objPtr) != TCL_OK ) { + &objPtr) != TCL_OK) { return TCL_ERROR; } - } else { /* %Ey */ + } else { /* %Ey */ if (Tcl_ListObjIndex(opts->interp, dateFmt->localeEra, 2, - &objPtr) != TCL_OK ) { + &objPtr) != TCL_OK) { return TCL_ERROR; } if (Tcl_GetIntFromObj(opts->interp, objPtr, val) != TCL_OK) { @@ -2958,24 +3024,27 @@ ClockFmtToken_LocaleERAYear_Proc( } } else { /* year as integer */ - if (FrmResultAllocate(dateFmt, 11) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, 11) != TCL_OK) { + return TCL_ERROR; + } dateFmt->output = _itoaw(dateFmt->output, - *val, '0', 2); + *val, '0', 2); return TCL_OK; } } s = TclGetStringFromObj(objPtr, &len); - if (FrmResultAllocate(dateFmt, len) != TCL_OK) { return TCL_ERROR; }; + if (FrmResultAllocate(dateFmt, len) != TCL_OK) { + return TCL_ERROR; + } memcpy(dateFmt->output, s, len + 1); dateFmt->output += len; } return TCL_OK; } - static const char *FmtSTokenMapIndex = "demNbByYCHMSIklpaAuwUVzgGjJsntQ"; -static ClockFormatTokenMap FmtSTokenMap[] = { +static const ClockFormatTokenMap FmtSTokenMap[] = { /* %d */ {CTOKT_INT, "0", 2, 0, 0, 0, offsetof(DateFormat, date.dayOfMonth), NULL, NULL}, /* %e */ @@ -3054,9 +3123,8 @@ static const char *FmtSTokenMapAliasIndex[2] = { "bpUz" }; -static const char *FmtETokenMapIndex = - "EJjys"; -static ClockFormatTokenMap FmtETokenMap[] = { +static const char *FmtETokenMapIndex = "EJjys"; +static const ClockFormatTokenMap FmtETokenMap[] = { /* %EE */ {CFMTT_PROC, NULL, 0, 0, 0, 0, 0, ClockFmtToken_LocaleERA_Proc, NULL}, @@ -3077,9 +3145,8 @@ static const char *FmtETokenMapAliasIndex[2] = { "y" }; -static const char *FmtOTokenMapIndex = - "dmyHIMSuw"; -static ClockFormatTokenMap FmtOTokenMap[] = { +static const char *FmtOTokenMapIndex = "dmyHIMSuw"; +static const ClockFormatTokenMap FmtOTokenMap[] = { /* %Od %Oe */ {CTOKT_INT, NULL, 0, CLFMT_LOCALE_INDX, 0, 100, offsetof(DateFormat, date.dayOfMonth), NULL, (void *)MCLIT_LOCALE_NUMERALS}, @@ -3113,7 +3180,7 @@ static const char *FmtOTokenMapAliasIndex[2] = { "dHI" }; -static ClockFormatTokenMap FmtWordTokenMap = { +static const ClockFormatTokenMap FmtWordTokenMap = { CTOKT_WORD, NULL, 0, 0, 0, 0, 0, NULL, NULL }; @@ -3123,7 +3190,7 @@ static ClockFormatTokenMap FmtWordTokenMap = { ClockFmtScnStorage * ClockGetOrParseFmtFormat( Tcl_Interp *interp, /* Tcl interpreter */ - Tcl_Obj *formatObj) /* Format container */ + Tcl_Obj *formatObj) /* Format container */ { ClockFmtScnStorage *fss; @@ -3142,7 +3209,7 @@ ClockGetOrParseFmtFormat( /* first time formatting - tokenize format */ if (fss->fmtTok == NULL) { ClockFormatToken *tok, *fmtTok; - unsigned int tokCnt; + unsigned tokCnt; const char *p, *e, *cp; e = p = HashEntry4FmtScn(fss)->key.string; @@ -3151,17 +3218,17 @@ ClockGetOrParseFmtFormat( /* estimate token count by % char and format length */ fss->fmtTokC = EstimateTokenCount(p, e); - fmtTok = tok = (ClockFormatToken *)ckalloc(sizeof(*tok) * fss->fmtTokC); + fmtTok = tok = (ClockFormatToken *) ckalloc(sizeof(*tok) * fss->fmtTokC); memset(tok, 0, sizeof(*(tok))); tokCnt = 1; while (p < e) { switch (*p) { - case '%': - if (1) { - ClockFormatTokenMap * fmtMap = FmtSTokenMap; - const char *mapIndex = FmtSTokenMapIndex, - **aliasIndex = FmtSTokenMapAliasIndex; - if (p+1 >= e) { + case '%': { + const ClockFormatTokenMap *fmtMap = FmtSTokenMap; + const char *mapIndex = FmtSTokenMapIndex; + const char **aliasIndex = FmtSTokenMapAliasIndex; + + if (p + 1 >= e) { goto word_tok; } p++; @@ -3172,23 +3239,23 @@ ClockGetOrParseFmtFormat( * because current mapping should be "...%%..." -> "...%..." */ tok->map = &FmtWordTokenMap; tok->tokWord.start = p; - tok->tokWord.end = p+1; - AllocTokenInChain(tok, fmtTok, fss->fmtTokC, ClockFormatToken *); tokCnt++; + tok->tokWord.end = p + 1; + AllocTokenInChain(tok, fmtTok, fss->fmtTokC, ClockFormatToken *); + tokCnt++; p++; continue; - break; case 'E': fmtMap = FmtETokenMap, mapIndex = FmtETokenMapIndex, aliasIndex = FmtETokenMapAliasIndex; p++; - break; + break; case 'O': fmtMap = FmtOTokenMap, mapIndex = FmtOTokenMapIndex, aliasIndex = FmtOTokenMapAliasIndex; p++; - break; + break; } /* search direct index */ cp = strchr(mapIndex, *p); @@ -3196,49 +3263,57 @@ ClockGetOrParseFmtFormat( /* search wrapper index (multiple chars for same token) */ cp = strchr(aliasIndex[0], *p); if (!cp || *cp == '\0') { - p--; if (fmtMap != FmtSTokenMap) p--; + p--; + if (fmtMap != FmtSTokenMap) { + p--; + } goto word_tok; } cp = strchr(mapIndex, aliasIndex[1][cp - aliasIndex[0]]); - if (!cp || *cp == '\0') { /* unexpected, but ... */ - #ifdef DEBUG + if (!cp || *cp == '\0') { /* unexpected, but ... */ +#ifdef DEBUG Tcl_Panic("token \"%c\" has no map in wrapper resolver", *p); - #endif - p--; if (fmtMap != FmtSTokenMap) p--; +#endif + p--; + if (fmtMap != FmtSTokenMap) { + p--; + } goto word_tok; } } tok->map = &fmtMap[cp - mapIndex]; tok->tokWord.start = p; /* next token */ - AllocTokenInChain(tok, fmtTok, fss->fmtTokC, ClockFormatToken *); tokCnt++; + AllocTokenInChain(tok, fmtTok, fss->fmtTokC, ClockFormatToken *); + tokCnt++; p++; continue; } - break; default: -word_tok: - if (1) { + word_tok: { ClockFormatToken *wordTok = tok; + if (tok > fmtTok && (tok-1)->map == &FmtWordTokenMap) { wordTok = tok-1; } if (wordTok == tok) { wordTok->tokWord.start = p; wordTok->map = &FmtWordTokenMap; - AllocTokenInChain(tok, fmtTok, fss->fmtTokC, ClockFormatToken *); tokCnt++; + AllocTokenInChain(tok, fmtTok, fss->fmtTokC, ClockFormatToken *); + tokCnt++; } p = Tcl_UtfNext(p); wordTok->tokWord.end = p; + break; } - break; } } /* correct count of real used tokens and free mem if desired * (1 is acceptable delta to prevent memory fragmentation) */ if (fss->fmtTokC > tokCnt + (CLOCK_MIN_TOK_CHAIN_BLOCK_SIZE / 2)) { - if ( (tok = (ClockFormatToken *)ckrealloc(fmtTok, tokCnt * sizeof(*tok))) != NULL ) { + if ((tok = (ClockFormatToken *) + attemptckrealloc(fmtTok, tokCnt * sizeof(*tok))) != NULL) { fmtTok = tok; } } @@ -3247,9 +3322,8 @@ word_tok: fss->fmtTok = fmtTok; fss->fmtTokC = tokCnt; } -done: + done: Tcl_MutexUnlock(&ClockFmtMutex); - return fss; } @@ -3258,12 +3332,12 @@ done: */ int ClockFormat( - DateFormat *dateFmt, /* Date fields used for parsing & converting */ - ClockFmtScnCmdArgs *opts) /* Command options */ + DateFormat *dateFmt, /* Date fields used for parsing & converting */ + ClockFmtScnCmdArgs *opts) /* Command options */ { - ClockFmtScnStorage *fss; - ClockFormatToken *tok; - ClockFormatTokenMap *map; + ClockFmtScnStorage *fss; + ClockFormatToken *tok; + const ClockFormatTokenMap *map; char resMem[MIN_FMT_RESULT_BLOCK_ALLOC]; /* get localized format */ @@ -3271,9 +3345,8 @@ ClockFormat( return TCL_ERROR; } - if ( !(fss = ClockGetOrParseFmtFormat(opts->interp, opts->formatObj)) - || !(tok = fss->fmtTok) - ) { + if (!(fss = ClockGetOrParseFmtFormat(opts->interp, opts->formatObj)) + || !(tok = fss->fmtTok)) { return TCL_ERROR; } @@ -3281,11 +3354,11 @@ ClockFormat( dateFmt->resMem = resMem; dateFmt->resEnd = dateFmt->resMem + sizeof(resMem); if (fss->fmtMinAlloc > sizeof(resMem)) { - dateFmt->resMem = (char *)ckalloc(fss->fmtMinAlloc); - dateFmt->resEnd = dateFmt->resMem + fss->fmtMinAlloc; + dateFmt->resMem = (char *) attemptckalloc(fss->fmtMinAlloc); if (dateFmt->resMem == NULL) { return TCL_ERROR; } + dateFmt->resEnd = dateFmt->resMem + fss->fmtMinAlloc; } dateFmt->output = dateFmt->resMem; *dateFmt->output = '\0'; @@ -3293,11 +3366,10 @@ ClockFormat( /* do format each token */ for (; tok->map != NULL; tok++) { map = tok->map; - switch (map->type) - { - case CTOKT_INT: - if (1) { + switch (map->type) { + case CTOKT_INT: { int val = (int)*(int *)(((char *)dateFmt) + map->offs); + if (map->fmtproc == NULL) { if (map->flags & CLFMT_DECR) { val--; @@ -3321,7 +3393,9 @@ ClockFormat( } } if (!(map->flags & CLFMT_LOCALE_INDX)) { - if (FrmResultAllocate(dateFmt, 11) != TCL_OK) { goto error; }; + if (FrmResultAllocate(dateFmt, 11) != TCL_OK) { + goto error; + } if (map->width) { dateFmt->output = _itoaw(dateFmt->output, val, *map->tostr, map->width); } else { @@ -3333,78 +3407,83 @@ ClockFormat( if (mcObj == NULL) { goto error; } - if ( Tcl_ListObjIndex(opts->interp, mcObj, val, &mcObj) != TCL_OK - || mcObj == NULL - ) { + if (Tcl_ListObjIndex(opts->interp, mcObj, val, &mcObj) != TCL_OK + || mcObj == NULL) { goto error; } s = TclGetString(mcObj); - if (FrmResultAllocate(dateFmt, mcObj->length) != TCL_OK) { goto error; }; + if (FrmResultAllocate(dateFmt, mcObj->length) != TCL_OK) { + goto error; + } memcpy(dateFmt->output, s, mcObj->length + 1); dateFmt->output += mcObj->length; } + break; } - break; - case CTOKT_WIDE: - if (1) { + case CTOKT_WIDE: { Tcl_WideInt val = *(Tcl_WideInt *)(((char *)dateFmt) + map->offs); - if (FrmResultAllocate(dateFmt, 21) != TCL_OK) { goto error; }; + + if (FrmResultAllocate(dateFmt, 21) != TCL_OK) { + goto error; + } if (map->width) { dateFmt->output = _witoaw(dateFmt->output, val, *map->tostr, map->width); } else { dateFmt->output += sprintf(dateFmt->output, map->tostr, val); } + break; } - break; case CTOKT_CHAR: - if (FrmResultAllocate(dateFmt, 1) != TCL_OK) { goto error; }; + if (FrmResultAllocate(dateFmt, 1) != TCL_OK) { + goto error; + } *dateFmt->output++ = *map->tostr; - break; + break; case CFMTT_PROC: if (map->fmtproc(opts, dateFmt, tok, NULL) != TCL_OK) { goto error; - }; - break; - case CTOKT_WORD: - if (1) { - Tcl_Size len = tok->tokWord.end - tok->tokWord.start; - if (FrmResultAllocate(dateFmt, len) != TCL_OK) { goto error; }; - if (len == 1) { - *dateFmt->output++ = *tok->tokWord.start; - } else { - memcpy(dateFmt->output, tok->tokWord.start, len); - dateFmt->output += len; - } } - break; + break; + case CTOKT_WORD: { + Tcl_Size len = tok->tokWord.end - tok->tokWord.start; + + if (FrmResultAllocate(dateFmt, len) != TCL_OK) { + goto error; + } + if (len == 1) { + *dateFmt->output++ = *tok->tokWord.start; + } else { + memcpy(dateFmt->output, tok->tokWord.start, len); + dateFmt->output += len; + } + break; + } } } - goto done; -error: - + error: if (dateFmt->resMem != resMem) { ckfree(dateFmt->resMem); } dateFmt->resMem = NULL; -done: - + done: if (dateFmt->resMem) { size_t size; Tcl_Obj *result; + TclNewObj(result); result->length = dateFmt->output - dateFmt->resMem; - size = result->length+1; + size = result->length + 1; if (dateFmt->resMem == resMem) { - result->bytes = (char *)ckalloc(size); + result->bytes = (char *) attemptckalloc(size); if (result->bytes == NULL) { return TCL_ERROR; } memcpy(result->bytes, dateFmt->resMem, size); } else if ((dateFmt->resEnd - dateFmt->resMem) / size > MAX_FMT_RESULT_THRESHOLD) { - result->bytes = (char *)ckrealloc(dateFmt->resMem, size); + result->bytes = (char *) attemptckrealloc(dateFmt->resMem, size); if (result->bytes == NULL) { result->bytes = dateFmt->resMem; } @@ -3412,9 +3491,8 @@ done: result->bytes = dateFmt->resMem; } /* save last used buffer length */ - if ( dateFmt->resMem != resMem - && fss->fmtMinAlloc < size + MIN_FMT_RESULT_BLOCK_DELTA - ) { + if (dateFmt->resMem != resMem + && fss->fmtMinAlloc < size + MIN_FMT_RESULT_BLOCK_DELTA) { fss->fmtMinAlloc = size + MIN_FMT_RESULT_BLOCK_DELTA; } result->bytes[result->length] = '\0'; |