11/* -----------------------------------------------------------------------
22 * formatting.c
33 *
4- * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.85 2005/03/25 16:08:40 tgl Exp $
4+ * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.86 2005/03/26 00:41:31 tgl Exp $
55 *
66 *
77 * Portions Copyright (c) 1999-2005, PostgreSQL Global Development Group
@@ -133,18 +133,19 @@ typedef struct FormatNode FormatNode;
133133
134134typedef struct
135135{
136- char * name ;/* keyword*/
137- /* action for keyword*/
138- int len ,/* keyword length*/
139- (* action ) (int arg ,char * inout ,int suf ,int flag ,FormatNode * node ,void * data ),
140- id ;/* keyword id*/
136+ const char * name ;/* keyword*/
137+ int len ;/* keyword length*/
138+ int (* action ) (int arg ,char * inout ,/* action for keyword */
139+ int suf ,int flag ,
140+ FormatNode * node ,void * data );
141+ int id ;/* keyword id*/
141142bool isitdigit ;/* is expected output/input digit */
142143}KeyWord ;
143144
144145struct FormatNode
145146{
146147int type ;/* node type*/
147- KeyWord * key ;/* if node type is KEYWORD*/
148+ const KeyWord * key ;/* if node type is KEYWORD*/
148149int character ,/* if node type is CHAR*/
149150suffix ;/* keyword suffix*/
150151};
@@ -648,7 +649,7 @@ typedef enum
648649 * KeyWords for DATE-TIME version
649650 * ----------
650651 */
651- static KeyWord DCH_keywords []= {
652+ static const KeyWord DCH_keywords []= {
652653/*keyword, len, func, type, isitdigit is in Index */
653654{"A.D." ,4 ,dch_date ,DCH_A_D , FALSE},/* A */
654655{"A.M." ,4 ,dch_time ,DCH_A_M , FALSE},
@@ -745,7 +746,7 @@ static KeyWord DCH_keywords[] = {
745746 * KeyWords for NUMBER version (now, isitdigit info is not needful here..)
746747 * ----------
747748 */
748- static KeyWord NUM_keywords []= {
749+ static const KeyWord NUM_keywords []= {
749750/*keyword,len, func.type is in Index */
750751{"," ,1 ,NULL ,NUM_COMMA },/* , */
751752{"." ,1 ,NULL ,NUM_DEC },/* . */
@@ -792,7 +793,7 @@ static KeyWord NUM_keywords[] = {
792793 * KeyWords index for DATE-TIME version
793794 * ----------
794795 */
795- static int DCH_index [KeyWord_INDEX_SIZE ]= {
796+ static const int DCH_index [KeyWord_INDEX_SIZE ]= {
796797/*
7977980123456789
798799*/
@@ -816,7 +817,7 @@ static intDCH_index[KeyWord_INDEX_SIZE] = {
816817 * KeyWords index for NUMBER version
817818 * ----------
818819 */
819- static int NUM_index [KeyWord_INDEX_SIZE ]= {
820+ static const int NUM_index [KeyWord_INDEX_SIZE ]= {
820821/*
8218220123456789
822823*/
@@ -876,15 +877,16 @@ typedef struct NUMProc
876877 * Functions
877878 * ----------
878879 */
879- static KeyWord * index_seq_search (char * str ,KeyWord * kw ,int * index );
880+ static const KeyWord * index_seq_search (char * str ,const KeyWord * kw ,
881+ const int * index );
880882static KeySuffix * suff_search (char * str ,KeySuffix * suf ,int type );
881883static void NUMDesc_prepare (NUMDesc * num ,FormatNode * n );
882- static void parse_format (FormatNode * node ,char * str ,KeyWord * kw ,
883- KeySuffix * suf ,int * index ,int ver ,NUMDesc * Num );
884+ static void parse_format (FormatNode * node ,char * str ,const KeyWord * kw ,
885+ KeySuffix * suf ,const int * index ,int ver ,NUMDesc * Num );
884886static char * DCH_processor (FormatNode * node ,char * inout ,int flag ,void * data );
885887
886888#ifdef DEBUG_TO_FROM_CHAR
887- static void dump_index (KeyWord * k ,int * index );
889+ static void dump_index (const KeyWord * k ,const int * index );
888890static void dump_node (FormatNode * node ,int max );
889891#endif
890892
@@ -924,8 +926,8 @@ static void NUM_cache_remove(NUMCacheEntry *ent);
924926 * (can't be used binary search in format parsing)
925927 * ----------
926928 */
927- static KeyWord *
928- index_seq_search (char * str ,KeyWord * kw ,int * index )
929+ static const KeyWord *
930+ index_seq_search (char * str ,const KeyWord * kw ,const int * index )
929931{
930932int poz ;
931933
@@ -934,8 +936,7 @@ index_seq_search(char *str, KeyWord *kw, int *index)
934936
935937if ((poz = * (index + (* str - ' ' )))> -1 )
936938{
937-
938- KeyWord * k = kw + poz ;
939+ const KeyWord * k = kw + poz ;
939940
940941do
941942{
@@ -1167,8 +1168,8 @@ NUMDesc_prepare(NUMDesc *num, FormatNode *n)
11671168 * ----------
11681169 */
11691170static void
1170- parse_format (FormatNode * node ,char * str ,KeyWord * kw ,
1171- KeySuffix * suf ,int * index ,int ver ,NUMDesc * Num )
1171+ parse_format (FormatNode * node ,char * str ,const KeyWord * kw ,
1172+ KeySuffix * suf ,const int * index ,int ver ,NUMDesc * Num )
11721173{
11731174KeySuffix * s ;
11741175FormatNode * n ;
@@ -1594,7 +1595,7 @@ seq_search(char *name, char **array, int type, int max, int *len)
15941595 * ----------
15951596 */
15961597static void
1597- dump_index (KeyWord * k ,int * index )
1598+ dump_index (const KeyWord * k ,const int * index )
15981599{
15991600int i ,
16001601count = 0 ,
@@ -2182,6 +2183,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
21822183}
21832184break ;
21842185case DCH_MONTH :
2186+ if (!tm -> tm_mon )
2187+ return -1 ;
21852188strcpy (workbuff ,months_full [tm -> tm_mon - 1 ]);
21862189sprintf (inout ,"%*s" ,S_FM (suf ) ?0 :-9 ,str_toupper (workbuff ));
21872190if (S_FM (suf ))
@@ -2190,13 +2193,17 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
21902193return 8 ;
21912194
21922195case DCH_Month :
2196+ if (!tm -> tm_mon )
2197+ return -1 ;
21932198sprintf (inout ,"%*s" ,S_FM (suf ) ?0 :-9 ,months_full [tm -> tm_mon - 1 ]);
21942199if (S_FM (suf ))
21952200return strlen (p_inout )- 1 ;
21962201else
21972202return 8 ;
21982203
21992204case DCH_month :
2205+ if (!tm -> tm_mon )
2206+ return -1 ;
22002207sprintf (inout ,"%*s" ,S_FM (suf ) ?0 :-9 ,months_full [tm -> tm_mon - 1 ]);
22012208* inout = pg_tolower ((unsignedchar )* inout );
22022209if (S_FM (suf ))
@@ -2205,15 +2212,21 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
22052212return 8 ;
22062213
22072214case DCH_MON :
2215+ if (!tm -> tm_mon )
2216+ return -1 ;
22082217strcpy (inout ,months [tm -> tm_mon - 1 ]);
22092218inout = str_toupper (inout );
22102219return 2 ;
22112220
22122221case DCH_Mon :
2222+ if (!tm -> tm_mon )
2223+ return -1 ;
22132224strcpy (inout ,months [tm -> tm_mon - 1 ]);
22142225return 2 ;
22152226
22162227case DCH_mon :
2228+ if (!tm -> tm_mon )
2229+ return -1 ;
22172230strcpy (inout ,months [tm -> tm_mon - 1 ]);
22182231* inout = pg_tolower ((unsignedchar )* inout );
22192232return 2 ;
@@ -2228,7 +2241,6 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
22282241return strlen (p_inout )- 1 ;
22292242else
22302243return 1 ;
2231-
22322244}
22332245else if (flag == FROM_CHAR )
22342246{
@@ -2388,7 +2400,6 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
23882400return strlen (p_inout )- 1 ;
23892401else
23902402return 1 ;
2391-
23922403}
23932404else if (flag == FROM_CHAR )
23942405{
@@ -2407,14 +2418,15 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
24072418case DCH_Q :
24082419if (flag == TO_CHAR )
24092420{
2421+ if (!tm -> tm_mon )
2422+ return -1 ;
24102423sprintf (inout ,"%d" , (tm -> tm_mon - 1 ) /3 + 1 );
24112424if (S_THth (suf ))
24122425{
24132426str_numth (p_inout ,inout ,S_TH_TYPE (suf ));
24142427return 2 ;
24152428}
24162429return 0 ;
2417-
24182430}
24192431else if (flag == FROM_CHAR )
24202432{
@@ -2613,6 +2625,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
26132625case DCH_RM :
26142626if (flag == TO_CHAR )
26152627{
2628+ if (!tm -> tm_mon )
2629+ return -1 ;
26162630sprintf (inout ,"%*s" ,S_FM (suf ) ?0 :-4 ,
26172631rm_months_upper [12 - tm -> tm_mon ]);
26182632if (S_FM (suf ))
@@ -2634,6 +2648,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
26342648case DCH_rm :
26352649if (flag == TO_CHAR )
26362650{
2651+ if (!tm -> tm_mon )
2652+ return -1 ;
26372653sprintf (inout ,"%*s" ,S_FM (suf ) ?0 :-4 ,
26382654rm_months_lower [12 - tm -> tm_mon ]);
26392655if (S_FM (suf ))