1
1
/* -----------------------------------------------------------------------
2
2
* formatting.c
3
3
*
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 $
5
5
*
6
6
*
7
7
* Portions Copyright (c) 1999-2005, PostgreSQL Global Development Group
@@ -133,18 +133,19 @@ typedef struct FormatNode FormatNode;
133
133
134
134
typedef struct
135
135
{
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 */
141
142
bool isitdigit ; /* is expected output/input digit */
142
143
} KeyWord ;
143
144
144
145
struct FormatNode
145
146
{
146
147
int type ; /* node type */
147
- KeyWord * key ; /* if node type is KEYWORD */
148
+ const KeyWord * key ; /* if node type is KEYWORD */
148
149
int character , /* if node type is CHAR */
149
150
suffix ; /* keyword suffix */
150
151
};
@@ -648,7 +649,7 @@ typedef enum
648
649
* KeyWords for DATE-TIME version
649
650
* ----------
650
651
*/
651
- static KeyWord DCH_keywords [] = {
652
+ static const KeyWord DCH_keywords [] = {
652
653
/* keyword, len, func, type, isitdigit is in Index */
653
654
{"A.D." , 4 , dch_date , DCH_A_D , FALSE}, /* A */
654
655
{"A.M." , 4 , dch_time , DCH_A_M , FALSE},
@@ -745,7 +746,7 @@ static KeyWord DCH_keywords[] = {
745
746
* KeyWords for NUMBER version (now, isitdigit info is not needful here..)
746
747
* ----------
747
748
*/
748
- static KeyWord NUM_keywords [] = {
749
+ static const KeyWord NUM_keywords [] = {
749
750
/* keyword, len, func. type is in Index */
750
751
{"," , 1 , NULL , NUM_COMMA }, /* , */
751
752
{"." , 1 , NULL , NUM_DEC }, /* . */
@@ -792,7 +793,7 @@ static KeyWord NUM_keywords[] = {
792
793
* KeyWords index for DATE-TIME version
793
794
* ----------
794
795
*/
795
- static int DCH_index [KeyWord_INDEX_SIZE ] = {
796
+ static const int DCH_index [KeyWord_INDEX_SIZE ] = {
796
797
/*
797
798
0 1 2 3 4 5 6 7 8 9
798
799
*/
@@ -816,7 +817,7 @@ static int DCH_index[KeyWord_INDEX_SIZE] = {
816
817
* KeyWords index for NUMBER version
817
818
* ----------
818
819
*/
819
- static int NUM_index [KeyWord_INDEX_SIZE ] = {
820
+ static const int NUM_index [KeyWord_INDEX_SIZE ] = {
820
821
/*
821
822
0 1 2 3 4 5 6 7 8 9
822
823
*/
@@ -876,15 +877,16 @@ typedef struct NUMProc
876
877
* Functions
877
878
* ----------
878
879
*/
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 );
880
882
static KeySuffix * suff_search (char * str , KeySuffix * suf , int type );
881
883
static 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 );
884
886
static char * DCH_processor (FormatNode * node , char * inout , int flag , void * data );
885
887
886
888
#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 );
888
890
static void dump_node (FormatNode * node , int max );
889
891
#endif
890
892
@@ -924,8 +926,8 @@ static void NUM_cache_remove(NUMCacheEntry *ent);
924
926
* (can't be used binary search in format parsing)
925
927
* ----------
926
928
*/
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 )
929
931
{
930
932
int poz ;
931
933
@@ -934,8 +936,7 @@ index_seq_search(char *str, KeyWord *kw, int *index)
934
936
935
937
if ((poz = * (index + (* str - ' ' ))) > -1 )
936
938
{
937
-
938
- KeyWord * k = kw + poz ;
939
+ const KeyWord * k = kw + poz ;
939
940
940
941
do
941
942
{
@@ -1167,8 +1168,8 @@ NUMDesc_prepare(NUMDesc *num, FormatNode *n)
1167
1168
* ----------
1168
1169
*/
1169
1170
static 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 )
1172
1173
{
1173
1174
KeySuffix * s ;
1174
1175
FormatNode * n ;
@@ -1594,7 +1595,7 @@ seq_search(char *name, char **array, int type, int max, int *len)
1594
1595
* ----------
1595
1596
*/
1596
1597
static void
1597
- dump_index (KeyWord * k , int * index )
1598
+ dump_index (const KeyWord * k , const int * index )
1598
1599
{
1599
1600
int i ,
1600
1601
count = 0 ,
@@ -2182,6 +2183,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
2182
2183
}
2183
2184
break ;
2184
2185
case DCH_MONTH :
2186
+ if (!tm -> tm_mon )
2187
+ return -1 ;
2185
2188
strcpy (workbuff , months_full [tm -> tm_mon - 1 ]);
2186
2189
sprintf (inout , "%*s" , S_FM (suf ) ? 0 : -9 , str_toupper (workbuff ));
2187
2190
if (S_FM (suf ))
@@ -2190,13 +2193,17 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
2190
2193
return 8 ;
2191
2194
2192
2195
case DCH_Month :
2196
+ if (!tm -> tm_mon )
2197
+ return -1 ;
2193
2198
sprintf (inout , "%*s" , S_FM (suf ) ? 0 : -9 , months_full [tm -> tm_mon - 1 ]);
2194
2199
if (S_FM (suf ))
2195
2200
return strlen (p_inout ) - 1 ;
2196
2201
else
2197
2202
return 8 ;
2198
2203
2199
2204
case DCH_month :
2205
+ if (!tm -> tm_mon )
2206
+ return -1 ;
2200
2207
sprintf (inout , "%*s" , S_FM (suf ) ? 0 : -9 , months_full [tm -> tm_mon - 1 ]);
2201
2208
* inout = pg_tolower ((unsigned char ) * inout );
2202
2209
if (S_FM (suf ))
@@ -2205,15 +2212,21 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
2205
2212
return 8 ;
2206
2213
2207
2214
case DCH_MON :
2215
+ if (!tm -> tm_mon )
2216
+ return -1 ;
2208
2217
strcpy (inout , months [tm -> tm_mon - 1 ]);
2209
2218
inout = str_toupper (inout );
2210
2219
return 2 ;
2211
2220
2212
2221
case DCH_Mon :
2222
+ if (!tm -> tm_mon )
2223
+ return -1 ;
2213
2224
strcpy (inout , months [tm -> tm_mon - 1 ]);
2214
2225
return 2 ;
2215
2226
2216
2227
case DCH_mon :
2228
+ if (!tm -> tm_mon )
2229
+ return -1 ;
2217
2230
strcpy (inout , months [tm -> tm_mon - 1 ]);
2218
2231
* inout = pg_tolower ((unsigned char ) * inout );
2219
2232
return 2 ;
@@ -2228,7 +2241,6 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
2228
2241
return strlen (p_inout ) - 1 ;
2229
2242
else
2230
2243
return 1 ;
2231
-
2232
2244
}
2233
2245
else if (flag == FROM_CHAR )
2234
2246
{
@@ -2388,7 +2400,6 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
2388
2400
return strlen (p_inout ) - 1 ;
2389
2401
else
2390
2402
return 1 ;
2391
-
2392
2403
}
2393
2404
else if (flag == FROM_CHAR )
2394
2405
{
@@ -2407,14 +2418,15 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
2407
2418
case DCH_Q :
2408
2419
if (flag == TO_CHAR )
2409
2420
{
2421
+ if (!tm -> tm_mon )
2422
+ return -1 ;
2410
2423
sprintf (inout , "%d" , (tm -> tm_mon - 1 ) / 3 + 1 );
2411
2424
if (S_THth (suf ))
2412
2425
{
2413
2426
str_numth (p_inout , inout , S_TH_TYPE (suf ));
2414
2427
return 2 ;
2415
2428
}
2416
2429
return 0 ;
2417
-
2418
2430
}
2419
2431
else if (flag == FROM_CHAR )
2420
2432
{
@@ -2613,6 +2625,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
2613
2625
case DCH_RM :
2614
2626
if (flag == TO_CHAR )
2615
2627
{
2628
+ if (!tm -> tm_mon )
2629
+ return -1 ;
2616
2630
sprintf (inout , "%*s" , S_FM (suf ) ? 0 : -4 ,
2617
2631
rm_months_upper [12 - tm -> tm_mon ]);
2618
2632
if (S_FM (suf ))
@@ -2634,6 +2648,8 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
2634
2648
case DCH_rm :
2635
2649
if (flag == TO_CHAR )
2636
2650
{
2651
+ if (!tm -> tm_mon )
2652
+ return -1 ;
2637
2653
sprintf (inout , "%*s" , S_FM (suf ) ? 0 : -4 ,
2638
2654
rm_months_lower [12 - tm -> tm_mon ]);
2639
2655
if (S_FM (suf ))
0 commit comments