conf: added config_get_block_unsigned()

Eliminate some more getBlockParam() invocations.
This commit is contained in:
Max Kellermann 2009-01-18 19:45:51 +01:00
parent a531a1e650
commit 65f2386b39
6 changed files with 33 additions and 39 deletions

View File

@ -25,6 +25,7 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <errno.h> #include <errno.h>
#define MAX_STRING_SIZE MPD_PATH_MAX+80 #define MAX_STRING_SIZE MPD_PATH_MAX+80
@ -457,6 +458,27 @@ config_get_block_string(struct config_param *param, const char *name,
return bp->value; return bp->value;
} }
unsigned
config_get_block_unsigned(struct config_param *param, const char *name,
unsigned default_value)
{
struct block_param *bp = getBlockParam(param, name);
long value;
char *endptr;
if (bp == NULL)
return default_value;
value = strtol(bp->value, &endptr, 0);
if (*endptr != 0)
g_error("Not a valid number in line %i", bp->line);
if (value < 0)
g_error("Not a positive number in line %i", bp->line);
return (unsigned)value;
}
bool bool
config_get_block_bool(struct config_param *param, const char *name, config_get_block_bool(struct config_param *param, const char *name,
bool default_value) bool default_value)

View File

@ -125,6 +125,10 @@ config_dup_block_string(struct config_param *param, const char *name,
return g_strdup(config_get_block_string(param, name, default_value)); return g_strdup(config_get_block_string(param, name, default_value));
} }
unsigned
config_get_block_unsigned(struct config_param *param, const char *name,
unsigned default_value);
bool bool
config_get_block_bool(struct config_param *param, const char *name, config_get_block_bool(struct config_param *param, const char *name,
bool default_value); bool default_value);

View File

@ -90,16 +90,12 @@ static void freeAlsaData(AlsaData * ad)
static void static void
alsa_configure(AlsaData *ad, struct config_param *param) alsa_configure(AlsaData *ad, struct config_param *param)
{ {
struct block_param *bp;
ad->device = config_dup_block_string(param, "device", NULL); ad->device = config_dup_block_string(param, "device", NULL);
ad->useMmap = config_get_block_bool(param, "use_mmap", false); ad->useMmap = config_get_block_bool(param, "use_mmap", false);
if ((bp = getBlockParam(param, "buffer_time"))) ad->buffer_time = config_get_block_unsigned(param, "buffer_time", 0);
ad->buffer_time = atoi(bp->value); ad->period_time = config_get_block_unsigned(param, "period_time", 0);
if ((bp = getBlockParam(param, "period_time")))
ad->period_time = atoi(bp->value);
#ifdef SND_PCM_NO_AUTO_RESAMPLE #ifdef SND_PCM_NO_AUTO_RESAMPLE
if (!config_get_block_bool(param, "auto_resample", true)) if (!config_get_block_bool(param, "auto_resample", true))

View File

@ -80,19 +80,10 @@ audioOutputAo_initDriver(struct audio_output *ao,
struct config_param *param) struct config_param *param)
{ {
ao_info *ai; ao_info *ai;
char *test;
AoData *ad = newAoData(); AoData *ad = newAoData();
struct block_param *blockParam;
const char *value; const char *value;
if ((blockParam = getBlockParam(param, "write_size"))) { ad->writeSize = config_get_block_unsigned(param, "write_size", 1024);
ad->writeSize = strtol(blockParam->value, &test, 10);
if (*test != '\0') {
g_error("\"%s\" is not a valid write size at line %i\n",
blockParam->value, blockParam->line);
}
} else
ad->writeSize = 1024;
if (driverInitCount == 0) { if (driverInitCount == 0) {
ao_initialize(); ao_initialize();

View File

@ -187,9 +187,6 @@ mpd_jack_init(struct audio_output *ao,
{ {
struct jack_data *jd; struct jack_data *jd;
const char *value; const char *value;
struct block_param *bp;
char *endptr;
int val;
jd = mpd_jack_new(); jd = mpd_jack_new();
jd->ao = ao; jd->ao = ao;
@ -212,18 +209,8 @@ mpd_jack_init(struct audio_output *ao,
g_free(ports); g_free(ports);
} }
if ( (bp = getBlockParam(param, "ringbuffer_size")) ) { jd->ringbuffer_size =
errno = 0; config_get_block_unsigned(param, "ringbuffer_size", 32768);
val = strtol(bp->value, &endptr, 10);
if ( errno == 0 && endptr != bp->value) {
jd->ringbuffer_size = val < 32768 ? 32768 : val;
g_debug("ringbuffer_size=%d", jd->ringbuffer_size);
} else {
g_error("%s is not a number; ringbuf_size=%d",
bp->value, jd->ringbuffer_size);
}
}
return jd; return jd;
} }

View File

@ -221,14 +221,8 @@ static void *my_shout_init_driver(struct audio_output *audio_output,
} }
/* optional paramters */ /* optional paramters */
block_param = getBlockParam(param, "timeout"); sd->timeout = config_get_block_unsigned(param, "timeout",
if (block_param) { DEFAULT_CONN_TIMEOUT);
sd->timeout = (int)strtol(block_param->value, &test, 10);
if (*test != '\0' || sd->timeout <= 0) {
g_error("shout timeout is not a positive integer, "
"line %i\n", block_param->line);
}
}
value = config_get_block_string(param, "genre", NULL); value = config_get_block_string(param, "genre", NULL);
if (value != NULL && shout_set_genre(sd->shout_conn, value)) { if (value != NULL && shout_set_genre(sd->shout_conn, value)) {