Add DC_getClientCfgDouble() because BOINC sometimes requires configuration

values that do not fit in an 'int'


git-svn-id: svn+ssh://cvs.lpds.sztaki.hu/var/lib/svn/szdg/dcapi/trunk@539 a7169a2c-3604-0410-bc95-c702d8d87f7a
This commit is contained in:
gombasg 2006-05-10 11:54:23 +00:00 committed by Adam Visegradi
parent ebde576b43
commit a3e599332e
5 changed files with 85 additions and 19 deletions

View File

@ -154,11 +154,16 @@ int DC_getCfgInt(const char *key, int defaultValue)
val = strtol(pairs[i].value, &p, 10);
/* Check for unit suffixes */
if (p && *p && _DC_processSuffix(&val, p))
if (p && *p)
{
DC_log(LOG_WARNING, "Configuration value for key %s is not "
"a valid number, ignoring", key);
return defaultValue;
long mult = _DC_processSuffix(p);
if (mult == -1)
{
DC_log(LOG_WARNING, "Configuration value for key %s "
"is not a valid number, ignoring", key);
return defaultValue;
}
val *= mult;
}
return val;
}

View File

@ -90,15 +90,56 @@ static int getCfgInt(const char *group, const char *key, int defaultValue,
retval = strtol(value, &p, 10);
/* Check for unit suffixes */
if (p && *p && _DC_processSuffix(&retval, p))
if (p && *p)
{
long mult = _DC_processSuffix(p);
if (mult == -1)
{
DC_log(LOG_WARNING, "Configuration value for key %s "
"is not a valid number, ignoring", key);
g_free(value);
*err = 1;
return defaultValue;
}
retval *= mult;
}
g_free(value);
*err = 0;
return retval;
}
static double getCfgDouble(const char *group, const char *key,
double defaultValue, int *err)
{
char *value, *p;
double retval;
if (!config || !key)
return defaultValue;
value = g_key_file_get_value(config, group, key, NULL);
if (!value)
{
DC_log(LOG_WARNING, "Configuration value for key %s is not "
"a valid number, ignoring", key);
g_free(value);
*err = 1;
return defaultValue;
}
retval = strtod(value, &p);
if (p && *p)
{
long long mult = _DC_processSuffix(p);
if (mult == -1)
{
DC_log(LOG_WARNING, "Configuration value for key %s "
"is not a valid number, ignoring", key);
g_free(value);
*err = 1;
return defaultValue;
}
retval *= mult;
}
g_free(value);
*err = 0;
return retval;
@ -148,3 +189,21 @@ int DC_getClientCfgInt(const char *clientName, const char *key,
val = getCfgInt(MASTER_GROUP, key, defaultValue, &err);
return val;
}
double DC_getClientCfgDouble(const char *clientName, const char *key,
double defaultValue, int fallbackGlobal)
{
char *group;
double val;
int err;
if (!clientName)
return defaultValue;
group = g_strdup_printf("Client-%s", clientName);
val = getCfgDouble(group, key, defaultValue, &err);
g_free(group);
if (err)
val = getCfgDouble(MASTER_GROUP, key, defaultValue, &err);
return val;
}

View File

@ -101,23 +101,21 @@ error:
return -1;
}
int _DC_processSuffix(long *value, const char *suffix)
long long _DC_processSuffix(const char *suffix)
{
while (*suffix == ' ' || *suffix == '\t')
suffix++;
if (!strcasecmp(suffix, "kb") || !strcasecmp(suffix, "kib"))
*value <<= 10;
return 1ll << 10;
else if (!strcasecmp(suffix, "mb") || !strcasecmp(suffix, "mib"))
*value <<= 20;
return 1ll << 20;
else if (!strcasecmp(suffix, "gb") || !strcasecmp(suffix, "gib"))
*value <<= 30;
return 1ll << 30;
else if (!strcasecmp(suffix, "min"))
*value *= 60;
return 60ll;
else if (!strcasecmp(suffix, "h") || !strcasecmp(suffix, "hour"))
*value *= 60 * 60;
return 60ll * 60;
else if (!strcasecmp(suffix, "day"))
*value *= 24 * 60 * 60;
else
return -1;
return 0;
return 24ll * 60 * 60;
return -1;
}

View File

@ -136,6 +136,10 @@ char *DC_getClientCfgStr(const char *clientName, const char *key,
int DC_getClientCfgInt(const char *clientName, const char *key,
int defaultValue, int fallbackGlobal);
/* Queries per-client configuration variables */
double DC_getClientCfgDouble(const char *clientName, const char *key,
double defaultValue, int fallbackGlobal);
/********************************************************************
* Function prototypes: Work unit management
*/

View File

@ -29,7 +29,7 @@ int _DC_parseCfg(const char *cfgfile);
int _DC_copyFile(const char *src, const char *dst);
/* Processes a unit suffix and adjust the value accordingly */
int _DC_processSuffix(long *value, const char *suffix);
long long _DC_processSuffix(const char *suffix);
#ifdef __cplusplus
}