static int handle_value(int obs_index, readstat_variable_t *variable, readstat_value_t value, void *ctx) { mod_csv_ctx_t *mod_ctx = (mod_csv_ctx_t *)ctx; readstat_type_t type = readstat_value_type(value); int var_index = readstat_variable_get_index(variable); if (var_index > 0) { fprintf(mod_ctx->out_file, ","); } if (readstat_value_is_system_missing(value)) { /* void */ } else if (type == READSTAT_TYPE_STRING) { /* TODO escape */ fprintf(mod_ctx->out_file, "\"%s\"", readstat_string_value(value)); } else if (type == READSTAT_TYPE_INT8) { fprintf(mod_ctx->out_file, "%hhd", readstat_int8_value(value)); } else if (type == READSTAT_TYPE_INT16) { fprintf(mod_ctx->out_file, "%hd", readstat_int16_value(value)); } else if (type == READSTAT_TYPE_INT32) { fprintf(mod_ctx->out_file, "%d", readstat_int32_value(value)); } else if (type == READSTAT_TYPE_FLOAT) { fprintf(mod_ctx->out_file, "%f", readstat_float_value(value)); } else if (type == READSTAT_TYPE_DOUBLE) { fprintf(mod_ctx->out_file, "%lf", readstat_double_value(value)); } if (var_index == mod_ctx->var_count - 1) { fprintf(mod_ctx->out_file, "\n"); } return 0; }
static int handle_value(int obs_index, int var_index, readstat_value_t value, void *ctx) { rs_ctx_t *rs_ctx = (rs_ctx_t *)ctx; readstat_writer_t *writer = rs_ctx->writer; readstat_variable_t *variable = readstat_get_variable(writer, var_index); readstat_types_t type = readstat_value_type(value); readstat_error_t error = READSTAT_OK; if (var_index == 0) { if (obs_index == 0) { if (rs_ctx->out_format == RS_FORMAT_SAV) { error = readstat_begin_writing_sav(writer, rs_ctx, rs_ctx->row_count); } else if (rs_ctx->out_format == RS_FORMAT_DTA) { error = readstat_begin_writing_dta(writer, rs_ctx, rs_ctx->row_count); } if (error != READSTAT_OK) goto cleanup; } if (var_index == 0) { error = readstat_begin_row(writer); if (error != READSTAT_OK) goto cleanup; } } if (readstat_value_is_system_missing(value)) { error = readstat_insert_missing_value(writer, variable); } else if (type == READSTAT_TYPE_STRING || type == READSTAT_TYPE_LONG_STRING) { error = readstat_insert_string_value(writer, variable, readstat_string_value(value)); } else if (type == READSTAT_TYPE_CHAR) { error = readstat_insert_char_value(writer, variable, readstat_char_value(value)); } else if (type == READSTAT_TYPE_INT16) { error = readstat_insert_int16_value(writer, variable, readstat_int16_value(value)); } else if (type == READSTAT_TYPE_INT32) { error = readstat_insert_int32_value(writer, variable, readstat_int32_value(value)); } else if (type == READSTAT_TYPE_FLOAT) { error = readstat_insert_float_value(writer, variable, readstat_float_value(value)); } else if (type == READSTAT_TYPE_DOUBLE) { error = readstat_insert_double_value(writer, variable, readstat_double_value(value)); } if (error != READSTAT_OK) goto cleanup; if (var_index == rs_ctx->var_count - 1) { error = readstat_end_row(writer); if (error != READSTAT_OK) goto cleanup; if (obs_index == rs_ctx->row_count - 1) { error = readstat_end_writing(writer); if (error != READSTAT_OK) goto cleanup; } } cleanup: if (error != READSTAT_OK) return 1; return 0; }