Esempio n. 1
0
static readstat_error_t read_value_label_record(readstat_por_ctx_t *ctx) {
    double dval;
    int i;
    char string[256];
    int count = 0, label_count = 0;
    char label_name_buf[256];
    char label_buf[256];
    snprintf(label_name_buf, sizeof(label_name_buf), POR_LABEL_NAME_PREFIX "%d", ctx->labels_offset);
    readstat_types_t value_type = READSTAT_TYPE_DOUBLE;
    if (read_double(ctx, &dval) == -1) {
        return READSTAT_ERROR_PARSE;
    }
    count = (int)dval;
    for (i=0; i<count; i++) {
        if (read_string(ctx, string, sizeof(string)) == -1) {
            return READSTAT_ERROR_PARSE;
        }
        spss_varinfo_t *info = (spss_varinfo_t *)ck_str_hash_lookup(string, ctx->var_dict);
        if (info) {
            value_type = info->type;
            info->labels_index = ctx->labels_offset;
        }
    }
    if (read_double(ctx, &dval) == -1) {
        return READSTAT_ERROR_PARSE;
    }
    label_count = (int)dval;
    for (i=0; i<label_count; i++) {
        readstat_value_t value = { .type = value_type };
        if (value_type == READSTAT_TYPE_STRING) {
            if (read_string(ctx, string, sizeof(string)) == -1) {
                return READSTAT_ERROR_PARSE;
            }
            if (read_string(ctx, label_buf, sizeof(label_buf)) == -1) {
                return READSTAT_ERROR_PARSE;
            }
            value.v.string_value = string;
        } else {
            if (read_double(ctx, &dval) == -1) {
                return READSTAT_ERROR_PARSE;
            }
            if (read_string(ctx, label_buf, sizeof(label_buf)) == -1) {
                return READSTAT_ERROR_PARSE;
            }
            value.v.double_value = dval;
        }
        ctx->value_label_handler(label_name_buf, value, label_buf, ctx->user_ctx);
    }
    ctx->labels_offset++;
    return READSTAT_OK;
}
Esempio n. 2
0
static int handle_variable(int index, readstat_variable_t *variable,
                           const char *val_labels, void *ctx) {
    rs_ctx_t *rs_ctx = (rs_ctx_t *)ctx;
    readstat_writer_t *writer = rs_ctx->writer;

    readstat_types_t type = readstat_variable_get_type(variable);
    const char *name = readstat_variable_get_name(variable);
    const char *label = readstat_variable_get_label(variable);
    char *format = NULL;
    size_t width = readstat_variable_get_width(variable);
    readstat_label_set_t *label_set = NULL;
    
    if (val_labels) {
        label_set = (readstat_label_set_t *)ck_str_hash_lookup(val_labels, rs_ctx->label_set_dict);
    }

    readstat_add_variable(writer, type, width, name, label, format, label_set);

    return 0;
}
Esempio n. 3
0
static int handle_value_label(const char *val_labels, readstat_value_t value,
                              const char *label, void *ctx) {
    rs_ctx_t *rs_ctx = (rs_ctx_t *)ctx;
    readstat_writer_t *writer = rs_ctx->writer;
    readstat_label_set_t *label_set = NULL;
    readstat_types_t type = readstat_value_type(value);

    label_set = (readstat_label_set_t *)ck_str_hash_lookup(val_labels, rs_ctx->label_set_dict);
    if (label_set == NULL) {
        label_set = readstat_add_label_set(writer, type, val_labels);
        ck_str_hash_insert(val_labels, label_set, rs_ctx->label_set_dict);
    }

    if (type == READSTAT_TYPE_INT32) {
        readstat_label_int32_value(label_set, readstat_int32_value(value), label);
    } else if (type == READSTAT_TYPE_DOUBLE) {
        readstat_label_double_value(label_set, readstat_double_value(value), label);
    } else if (type == READSTAT_TYPE_STRING) {
        readstat_label_string_value(label_set, readstat_string_value(value), label);
    }

    return 0;
}