Пример #1
0
static void set_field(OhmFact *fact, fsif_fldtype_t type,char *name,void *vptr)
{
    fsif_value_t *v = (fsif_value_t *)vptr;
    GValue       *gv;

    switch (type) {
    case fldtype_string:    gv = ohm_value_from_string(v->string);      break;
    case fldtype_integer:   gv = ohm_value_from_int(v->integer);        break;
    case fldtype_unsignd:   gv = ohm_value_from_unsigned(v->unsignd);   break;
    case fldtype_floating:  gv = ohm_value_from_double(v->floating);    break;
    case fldtype_time:      gv = ohm_value_from_time(v->time);          break;
    default:          OHM_ERROR("resource: invalid type for %s", name); return;
    }

    ohm_fact_set(fact, name, gv);
}
Пример #2
0
static int load_field(FILE *fp, char *key, size_t size, GValue **value)
{
    char val[256], *e;
    size_t len;
    int i;
    double d;

    if (fgets(key, size, fp) == NULL)
        return ENOENT;

    if ((len = strlen(key)) == 0 || key[len - 1] != '\n')
        return EINVAL;

    key[len - 1] = '\0';
    
    if (fgets(val, sizeof(val), fp) == NULL)
        return EINVAL;

    if ((len = strlen(val)) < 2 || val[1] != ':' || val[len - 1] != '\n')
        return EINVAL;

    val[len - 1] = '\0';

    switch (val[0]) {
    case 's':
        *value = ohm_value_from_string(val + 2);
        break;
    case 'i':
        i = (int)strtol(val + 2, &e, 10);
        if (e != NULL && *e)
            return EINVAL;
        *value = ohm_value_from_int(i);
        break;
    case 'f':
        d = strtod(val + 2, &e);
        if (e != NULL && *e)
            return EINVAL;
        *value = ohm_value_from_double(d);
        break;
    default:
        return EINVAL;
    }

    return 0;
}
Пример #3
0
/********************
 * create_variable
 ********************/
static int
create_variable(dres_t *dres, char *name, dres_init_t *fields)
{
    dres_init_t  *init;
    char         *field;
    dres_value_t *value;
    OhmFactStore *store = ohm_get_fact_store();
    OhmFact      *fact;
    GValue       *gval;

    if (store == NULL)
        return EINVAL;

    if ((fact = ohm_fact_new(name)) == NULL)
        return ENOMEM;

    for (init = fields; init != NULL; init = init->next) {
        field = init->field.name;
        value = &init->field.value;
        switch (value->type) {
        case DRES_TYPE_INTEGER: gval = ohm_value_from_int(value->v.i);    break;
        case DRES_TYPE_DOUBLE:  gval = ohm_value_from_double(value->v.d); break;
        case DRES_TYPE_STRING:  gval = ohm_value_from_string(value->v.s); break;
        case DRES_TYPE_UNKNOWN:
            DRES_ERROR("Missing field initialiser for fact field %s:%s.",
                       name, field);
            return EINVAL;
        default:
            DRES_ERROR("Invalid field initialiser for fact field %s:%s.",
                       name, field);
            return EINVAL;
        }

        ohm_fact_set(fact, field, gval);
    }

    if (!ohm_fact_store_insert(store, fact))
        return EINVAL;

    return 0;

    (void)dres;
}