Exemplo n.º 1
0
/**
 * Load size with SI prefix.
 * @param opt Option name.
 * @param val Value pointer.
 * @param base Value base (e.g. 1000, 1024, etc.).
 * @return <0 - error. 0 - success. >0 - not found.
 */
static int zcfg_load_kmgt(const config_setting_t *option, uint64_t *val, uint64_t base)
{
    *val = 0ull;

    if (!option) {
        return 1;
    }

    if ((CONFIG_TYPE_INT == option->type) || (CONFIG_TYPE_INT64 == option->type)) {
        int64_t ival64 = config_setting_get_int64(option);
        if (ival64 < 0) {
            return -1;
        }
        return 0;
    } else if (CONFIG_TYPE_STRING == option->type) {
        const char *str = config_setting_get_string(option);
        if (0 != str_to_u64(str, val)) {
            return -1;
        }
        char prefix = str[strlen(str) - 1];
        *val *= str_parse_si_unit(prefix, base);
        return 0;
    }

    return -1;
}
Exemplo n.º 2
0
long long config_setting_get_int64_elem(const config_setting_t *vector,
                                        int idx)
{
  const config_setting_t *element = config_setting_get_elem(vector, idx);

  return(element ? config_setting_get_int64(element) : 0);
}
Exemplo n.º 3
0
void config_setting_copy_simple(config_setting_t *parent, const config_setting_t *src)
{
	if (config_setting_is_aggregate(src)) {
		config_setting_copy_aggregate(parent, src);
	}
	else {
		config_setting_t *set;
		
		if( config_setting_get_member(parent, config_setting_name(src)) != NULL )
			return;
		
		if ((set = config_setting_add(parent, config_setting_name(src), config_setting_type(src))) == NULL)
			return;

		if (CONFIG_TYPE_INT == config_setting_type(src)) {
			config_setting_set_int(set, config_setting_get_int(src));
			config_setting_set_format(set, src->format);
		} else if (CONFIG_TYPE_INT64 == config_setting_type(src)) {
			config_setting_set_int64(set, config_setting_get_int64(src));
			config_setting_set_format(set, src->format);
		} else if (CONFIG_TYPE_FLOAT == config_setting_type(src)) {
			config_setting_set_float(set, config_setting_get_float(src));
		} else if (CONFIG_TYPE_STRING == config_setting_type(src)) {
			config_setting_set_string(set, config_setting_get_string(src));
		} else if (CONFIG_TYPE_BOOL == config_setting_type(src)) {
			config_setting_set_bool(set, config_setting_get_bool(src));
		}
	}
}
Exemplo n.º 4
0
int config_setting_lookup_int64(const config_setting_t *setting,
                                const char *name, long long *value)
{
  config_setting_t *member = config_setting_get_member(setting, name);
  if(! member)
    return(CONFIG_FALSE);

  if(config_setting_type(member) != CONFIG_TYPE_INT64)
    return(CONFIG_FALSE);

  *value = config_setting_get_int64(member);
  return(CONFIG_TRUE);
}
Exemplo n.º 5
0
int config_lookup_int64(const config_t *config, const char *path,
                        long long *value)
{
  const config_setting_t *s = config_lookup(config, path);
  if(! s)
    return(CONFIG_FALSE);

  if(config_setting_type(s) != CONFIG_TYPE_INT64)
    return(CONFIG_FALSE);

  *value = config_setting_get_int64(s);

  return(CONFIG_TRUE);
}
Exemplo n.º 6
0
/**
 * Load int64 from config.
 * @param[in] option Option name.
 * @param[out] value Value pointer.
 * @return <0 - error. 0 - success. >0 - not found.
 */
static int zcfg_load_int64(const config_setting_t *option, int64_t *value)
{
    *value = 0;

    if (!option) {
        return 1;
    }

    if (CONFIG_TYPE_INT64 != option->type) {
        return -1;
    }

    *value = config_setting_get_int64(option);

    return 0;
}
Exemplo n.º 7
0
static void get_value_libconfig(const config_setting_t *e, void *dest)
{
	int type = config_setting_type(e);
	switch (type) {
	case CONFIG_TYPE_INT:
		*(int *)dest = config_setting_get_int(e);
	case CONFIG_TYPE_INT64:
		*(long long *)dest = config_setting_get_int64(e);
		break;
	case CONFIG_TYPE_STRING:
		dest = (void *)config_setting_get_string(e);
		break;
	case CONFIG_TYPE_BOOL:
		*(int *)dest = config_setting_get_bool(e);
		break;
	case CONFIG_TYPE_FLOAT:
		*(double *)dest = config_setting_get_float(e);
		/* Do nothing, add if needed */
	}
}
Exemplo n.º 8
0
void config_setting_copy_elem(config_setting_t *parent, const config_setting_t *src)
{
	config_setting_t *set = NULL;

	if (config_setting_is_aggregate(src))
		config_setting_copy_aggregate(parent, src);
	else if (CONFIG_TYPE_INT == config_setting_type(src)) {
		set = config_setting_set_int_elem(parent, -1, config_setting_get_int(src));
		config_setting_set_format(set, src->format);
	} else if (CONFIG_TYPE_INT64 == config_setting_type(src)) {
		set = config_setting_set_int64_elem(parent, -1, config_setting_get_int64(src));
		config_setting_set_format(set, src->format);   
	} else if (CONFIG_TYPE_FLOAT == config_setting_type(src)) {
		config_setting_set_float_elem(parent, -1, config_setting_get_float(src));
	} else if (CONFIG_TYPE_STRING == config_setting_type(src)) {
		config_setting_set_string_elem(parent, -1, config_setting_get_string(src));
	} else if (CONFIG_TYPE_BOOL == config_setting_type(src)) {
		config_setting_set_bool_elem(parent, -1, config_setting_get_bool(src));
	}
}
Exemplo n.º 9
0
/**
 * Load unsigned int from config.
 * @param[in] option Option name.
 * @param[out] value Value pointer.
 * @return <0 - error. 0 - success. >0 - not found.
 */
static int zcfg_load_uint(const config_setting_t *option, u_int *value)
{
    *value = 0u;

    if (!option) {
        return 1;
    }

    if ((CONFIG_TYPE_INT != option->type) && (CONFIG_TYPE_INT64 != option->type)) {
        return -1;
    }

    int64_t tmp = config_setting_get_int64(option);

    if ((UINT_MAX < tmp) || (0 > tmp)) {
        return -1;
    }

    *value = (u_int) tmp;

    return 0;
}
Exemplo n.º 10
0
/*********************
return RET_NOK on error
*********************/
ret_code_t entry_copy_config(config_setting_t * source, config_setting_t * dest)
{
	config_setting_t * new_source;
	config_setting_t * new_dest;
	int index = -1;
	int int_value;
	long long long_value;
	double double_value;
	const char * string;

	while((new_source=config_setting_get_elem(source,index+1))!= NULL ) {
		index++;
		if(config_setting_is_group(new_source)) {
			if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_GROUP)) {
				return RET_NOK;
			}
		}

		else if(config_setting_is_array(new_source)) {
			if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_ARRAY)) {
				return RET_NOK;
			}
		}

		else if(config_setting_is_list(new_source)) {
			if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_LIST)) {
				return RET_NOK;
			}
		}

		else {
			switch(config_setting_type(new_source)) {
			case CONFIG_TYPE_INT:
				int_value = config_setting_get_int(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_INT);
				config_setting_set_int(new_dest,int_value);
				continue;
			case CONFIG_TYPE_INT64:
				long_value = config_setting_get_int64(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_INT64);
				config_setting_set_int64(new_dest,long_value);
				continue;
			case CONFIG_TYPE_FLOAT:
				double_value = config_setting_get_float(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_FLOAT);
				config_setting_set_float(new_dest,double_value);
				continue;
			case CONFIG_TYPE_BOOL:
				int_value = config_setting_get_bool(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_BOOL);
				config_setting_set_bool(new_dest,int_value);
				continue;
			case CONFIG_TYPE_STRING:
				string = config_setting_get_string(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_STRING);
				config_setting_set_string(new_dest,string);
				continue;
			default:
				return RET_NOK;
			}
		}
	}

	return RET_OK;
}