Skip to content

Commit

Permalink
Provide default formatting options (SAV)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanmiller committed Feb 25, 2015
1 parent 9e8ab6f commit 9bc3ad1
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/readstat_sav_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

#define MAX_TEXT_SIZE 256

static int32_t sav_encode_format(spss_format_t *spss_format) {
return ((spss_format->type << 16) |
(spss_format->width << 8) |
spss_format->decimal_places);
}

static readstat_error_t sav_emit_header(readstat_writer_t *writer) {
readstat_error_t retval = READSTAT_OK;
time_t now = time(NULL);
Expand Down Expand Up @@ -82,21 +88,30 @@ static readstat_error_t sav_emit_variable_records(readstat_writer_t *writer) {
variable.type = (r_variable->type == READSTAT_TYPE_STRING) ? 255 : 0;
variable.has_var_label = (title_data_len > 0);
variable.n_missing_values = 1;

spss_format_t spss_format;
memset(&spss_format, 0, sizeof(spss_format_t));

if (r_variable->format[0]) {
const char *fmt = r_variable->format;
spss_format_t spss_format = { .type = 0, .width = 0, .decimal_places = 0 };
if (spss_parse_format(fmt, strlen(fmt), &spss_format) == READSTAT_OK) {
variable.print = (
(spss_format.type << 16) |
(spss_format.width << 8) |
spss_format.decimal_places);
} else {
if (spss_parse_format(fmt, strlen(fmt), &spss_format) != READSTAT_OK) {
retval = READSTAT_ERROR_BAD_FORMAT_STRING;
goto cleanup;
}
variable.write = variable.print;
} else if (r_variable->type == READSTAT_TYPE_STRING) {
spss_format.type = SPSS_FORMAT_TYPE_A;
} else {
spss_format.type = SPSS_FORMAT_TYPE_F;
spss_format.width = 8;
if (r_variable->type == READSTAT_TYPE_DOUBLE ||
r_variable->type == READSTAT_TYPE_FLOAT) {
spss_format.decimal_places = 2;
}
}

variable.print = sav_encode_format(&spss_format);
variable.write = variable.print;

memset(variable.name, ' ', sizeof(variable.name));
if (name_data_len > 0 && name_data_len <= sizeof(variable.name))
memcpy(variable.name, name_data, name_data_len);
Expand Down

0 comments on commit 9bc3ad1

Please sign in to comment.