10
10
* Written by Peter Eisentraut <peter_e@gmx.net>.
11
11
*
12
12
* IDENTIFICATION
13
- * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.481 2008/11/21 20:14:27 mha Exp $
13
+ * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.482 2008/12/02 02:00:32 alvherre Exp $
14
14
*
15
15
*--------------------------------------------------------------------
16
16
*/
@@ -4382,16 +4382,17 @@ parse_real(const char *value, double *result)
4382
4382
const char *
4383
4383
config_enum_lookup_by_value (struct config_enum * record , int val )
4384
4384
{
4385
- const struct config_enum_entry * entry = record -> options ;
4386
- while (entry && entry -> name )
4385
+ const struct config_enum_entry * entry ;
4386
+
4387
+ for (entry = record -> options ; entry && entry -> name ; entry ++ )
4387
4388
{
4388
4389
if (entry -> val == val )
4389
4390
return entry -> name ;
4390
- entry ++ ;
4391
4391
}
4392
+
4392
4393
elog (ERROR , "could not find enum option %d for %s" ,
4393
4394
val , record -> gen .name );
4394
- return NULL ; /* silence compiler */
4395
+ return NULL ; /* silence compiler */
4395
4396
}
4396
4397
4397
4398
@@ -4400,86 +4401,71 @@ config_enum_lookup_by_value(struct config_enum *record, int val)
4400
4401
* (case-insensitive).
4401
4402
* If the enum option is found, sets the retval value and returns
4402
4403
* true. If it's not found, return FALSE and retval is set to 0.
4403
- *
4404
4404
*/
4405
4405
bool
4406
- config_enum_lookup_by_name (struct config_enum * record , const char * value , int * retval )
4406
+ config_enum_lookup_by_name (struct config_enum * record , const char * value ,
4407
+ int * retval )
4407
4408
{
4408
- const struct config_enum_entry * entry = record -> options ;
4409
-
4410
- if (retval )
4411
- * retval = 0 ; /* suppress compiler warning */
4412
-
4413
- while (entry && entry -> name )
4409
+ const struct config_enum_entry * entry ;
4410
+
4411
+ for (entry = record -> options ; entry && entry -> name ; entry ++ )
4414
4412
{
4415
4413
if (pg_strcasecmp (value , entry -> name ) == 0 )
4416
4414
{
4417
4415
* retval = entry -> val ;
4418
4416
return TRUE;
4419
4417
}
4420
- entry ++ ;
4421
4418
}
4419
+
4420
+ * retval = 0 ;
4422
4421
return FALSE;
4423
4422
}
4424
4423
4425
4424
4426
4425
/*
4427
4426
* Return a list of all available options for an enum, excluding
4428
- * hidden ones, separated by ", " (comma-space) .
4427
+ * hidden ones, separated by the given separator .
4429
4428
* If prefix is non-NULL, it is added before the first enum value.
4430
4429
* If suffix is non-NULL, it is added to the end of the string.
4431
4430
*/
4432
4431
static char *
4433
4432
config_enum_get_options (struct config_enum * record , const char * prefix ,
4434
4433
const char * suffix , const char * separator )
4435
4434
{
4436
- const struct config_enum_entry * entry = record -> options ;
4437
- int len = 0 ;
4438
- char * hintmsg ;
4439
-
4440
- if (!entry || !entry -> name )
4441
- return NULL ; /* Should not happen */
4435
+ const struct config_enum_entry * entry ;
4436
+ StringInfoData retstr ;
4437
+ int seplen ;
4442
4438
4443
- while (entry && entry -> name )
4444
- {
4445
- if (!entry -> hidden )
4446
- len += strlen (entry -> name ) + strlen (separator );
4447
-
4448
- entry ++ ;
4449
- }
4450
-
4451
- hintmsg = palloc (len + strlen (prefix ) + strlen (suffix ) + 2 );
4452
-
4453
- strcpy (hintmsg , prefix );
4439
+ initStringInfo (& retstr );
4440
+ appendStringInfoString (& retstr , prefix );
4454
4441
4455
- entry = record -> options ;
4456
- while (entry && entry -> name )
4442
+ seplen = strlen ( separator ) ;
4443
+ for (entry = record -> options ; entry && entry -> name ; entry ++ )
4457
4444
{
4458
4445
if (!entry -> hidden )
4459
4446
{
4460
- strcat ( hintmsg , entry -> name );
4461
- strcat ( hintmsg , separator );
4447
+ appendStringInfoString ( & retstr , entry -> name );
4448
+ appendBinaryStringInfo ( & retstr , separator , seplen );
4462
4449
}
4463
-
4464
- entry ++ ;
4465
4450
}
4466
4451
4467
- len = strlen (hintmsg );
4468
-
4469
4452
/*
4470
4453
* All the entries may have been hidden, leaving the string empty
4471
4454
* if no prefix was given. This indicates a broken GUC setup, since
4472
4455
* there is no use for an enum without any values, so we just check
4473
4456
* to make sure we don't write to invalid memory instead of actually
4474
4457
* trying to do something smart with it.
4475
4458
*/
4476
- if (len >= strlen (separator ))
4459
+ if (retstr .len >= seplen )
4460
+ {
4477
4461
/* Replace final separator */
4478
- hintmsg [len - strlen (separator )] = '\0' ;
4462
+ retstr .data [retstr .len - seplen ] = '\0' ;
4463
+ retstr .len -= seplen ;
4464
+ }
4479
4465
4480
- strcat ( hintmsg , suffix );
4466
+ appendStringInfoString ( & retstr , suffix );
4481
4467
4482
- return hintmsg ;
4468
+ return retstr . data ;
4483
4469
}
4484
4470
4485
4471
/*
@@ -5047,7 +5033,11 @@ set_config_option(const char *name, const char *value,
5047
5033
{
5048
5034
if (!config_enum_lookup_by_name (conf , value , & newval ))
5049
5035
{
5050
- char * hintmsg = config_enum_get_options (conf , "Available values: " , "." , ", " );
5036
+ char * hintmsg ;
5037
+
5038
+ hintmsg = config_enum_get_options (conf ,
5039
+ "Available values: " ,
5040
+ "." , ", " );
5051
5041
5052
5042
ereport (elevel ,
5053
5043
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
@@ -6253,13 +6243,16 @@ GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
6253
6243
6254
6244
/* enumvals */
6255
6245
/* NOTE! enumvals with double quotes in them are not supported! */
6256
- values [11 ] = config_enum_get_options ((struct config_enum * ) conf , "{\"" , "\"}" , "\",\"" );
6246
+ values [11 ] = config_enum_get_options ((struct config_enum * ) conf ,
6247
+ "{\"" , "\"}" , "\",\"" );
6257
6248
6258
6249
/* boot_val */
6259
- values [12 ] = pstrdup (config_enum_lookup_by_value (lconf , lconf -> boot_val ));
6250
+ values [12 ] = pstrdup (config_enum_lookup_by_value (lconf ,
6251
+ lconf -> boot_val ));
6260
6252
6261
6253
/* reset_val */
6262
- values [13 ] = pstrdup (config_enum_lookup_by_value (lconf , lconf -> reset_val ));
6254
+ values [13 ] = pstrdup (config_enum_lookup_by_value (lconf ,
6255
+ lconf -> reset_val ));
6263
6256
}
6264
6257
break ;
6265
6258
@@ -6672,8 +6665,8 @@ is_newvalue_equal(struct config_generic * record, const char *newvalue)
6672
6665
struct config_enum * conf = (struct config_enum * ) record ;
6673
6666
int newval ;
6674
6667
6675
- return config_enum_lookup_by_name (conf , newvalue , & newval )
6676
- && * conf -> variable == newval ;
6668
+ return config_enum_lookup_by_name (conf , newvalue , & newval ) &&
6669
+ * conf -> variable == newval ;
6677
6670
}
6678
6671
}
6679
6672
0 commit comments