From ab0d7647292c6b8d8f8de57e7fb8888dee12acda Mon Sep 17 00:00:00 2001 From: Evan Miller Date: Thu, 11 Aug 2016 15:49:28 +0100 Subject: [PATCH] Rename machine_needs_byte_swap => bswap --- src/spss/readstat_sav.c | 14 +++---- src/spss/readstat_sav.h | 2 +- src/spss/readstat_sav_read.c | 70 +++++++++++++++++------------------ src/stata/readstat_dta.c | 8 ++-- src/stata/readstat_dta.h | 2 +- src/stata/readstat_dta_read.c | 38 +++++++++---------- 6 files changed, 64 insertions(+), 70 deletions(-) diff --git a/src/spss/readstat_sav.c b/src/spss/readstat_sav.c index 3a0e8d88..52649498 100644 --- a/src/spss/readstat_sav.c +++ b/src/spss/readstat_sav.c @@ -26,21 +26,17 @@ sav_ctx_t *sav_ctx_init(sav_file_header_record_t *header, readstat_io_t *io) { } memset(ctx, 0, sizeof(sav_ctx_t)); - if (header->layout_code == 2 || header->layout_code == 3) { - ctx->machine_needs_byte_swap = 0; - } else { - ctx->machine_needs_byte_swap = 1; - } - + ctx->bswap = !(header->layout_code == 2 || header->layout_code == 3); + ctx->data_is_compressed = (header->compressed != 0); - ctx->record_count = ctx->machine_needs_byte_swap ? byteswap4(header->ncases) : header->ncases; - ctx->fweight_index = ctx->machine_needs_byte_swap ? byteswap4(header->weight_index) : header->weight_index; + ctx->record_count = ctx->bswap ? byteswap4(header->ncases) : header->ncases; + ctx->fweight_index = ctx->bswap ? byteswap4(header->weight_index) : header->weight_index; ctx->missing_double = SAV_MISSING_DOUBLE; ctx->lowest_double = SAV_LOWEST_DOUBLE; ctx->highest_double = SAV_HIGHEST_DOUBLE; - double bias = ctx->machine_needs_byte_swap ? byteswap_double(header->bias) : header->bias; + double bias = ctx->bswap ? byteswap_double(header->bias) : header->bias; if (bias != 100.0) { sav_ctx_free(ctx); diff --git a/src/spss/readstat_sav.h b/src/spss/readstat_sav.h index e372ce46..a309473b 100644 --- a/src/spss/readstat_sav.h +++ b/src/spss/readstat_sav.h @@ -102,7 +102,7 @@ typedef struct sav_ctx_s { uint64_t highest_double; unsigned int data_is_compressed:1; - unsigned int machine_needs_byte_swap:1; + unsigned int bswap:1; } sav_ctx_t; #define SAV_RECORD_TYPE_VARIABLE 2 diff --git a/src/spss/readstat_sav_read.c b/src/spss/readstat_sav_read.c index 91b37731..1ebd06be 100644 --- a/src/spss/readstat_sav_read.c +++ b/src/spss/readstat_sav_read.c @@ -169,7 +169,7 @@ static readstat_error_t sav_skip_variable_record(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - label_len = ctx->machine_needs_byte_swap ? byteswap4(label_len) : label_len; + label_len = ctx->bswap ? byteswap4(label_len) : label_len; int32_t label_capacity = (label_len + 3) / 4 * 4; if (io->seek(label_capacity, READSTAT_SEEK_CUR, io->io_ctx) == -1) { retval = READSTAT_ERROR_SEEK; @@ -177,7 +177,7 @@ static readstat_error_t sav_skip_variable_record(sav_ctx_t *ctx) { } } if (variable.n_missing_values) { - int n_missing_values = ctx->machine_needs_byte_swap ? byteswap4(variable.n_missing_values) : variable.n_missing_values; + int n_missing_values = ctx->bswap ? byteswap4(variable.n_missing_values) : variable.n_missing_values; if (io->seek(abs(n_missing_values) * sizeof(double), READSTAT_SEEK_CUR, io->io_ctx) == -1) { retval = READSTAT_ERROR_SEEK; goto cleanup; @@ -201,11 +201,11 @@ static readstat_error_t sav_read_variable_record(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - variable.print = ctx->machine_needs_byte_swap ? byteswap4(variable.print) : variable.print; - variable.write = ctx->machine_needs_byte_swap ? byteswap4(variable.write) : variable.write; + variable.print = ctx->bswap ? byteswap4(variable.print) : variable.print; + variable.write = ctx->bswap ? byteswap4(variable.write) : variable.write; readstat_type_t dta_type = READSTAT_TYPE_DOUBLE; - int32_t type = ctx->machine_needs_byte_swap ? byteswap4(variable.type) : variable.type; + int32_t type = ctx->bswap ? byteswap4(variable.type) : variable.type; int i; if (type < 0) { if (ctx->var_index == 0) { @@ -252,7 +252,7 @@ static readstat_error_t sav_read_variable_record(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - label_len = ctx->machine_needs_byte_swap ? byteswap4(label_len) : label_len; + label_len = ctx->bswap ? byteswap4(label_len) : label_len; int32_t label_capacity = (label_len + 3) / 4 * 4; char *label_buf = malloc(label_capacity); size_t out_label_len = label_len*4+1; @@ -277,7 +277,7 @@ static readstat_error_t sav_read_variable_record(sav_ctx_t *ctx) { ctx->varinfo[ctx->var_index].labels_index = -1; if (variable.n_missing_values) { - info->n_missing_values = ctx->machine_needs_byte_swap ? byteswap4(variable.n_missing_values) : variable.n_missing_values; + info->n_missing_values = ctx->bswap ? byteswap4(variable.n_missing_values) : variable.n_missing_values; if (info->n_missing_values < 0) { info->missing_range = 1; info->n_missing_values = abs(info->n_missing_values); @@ -293,7 +293,7 @@ static readstat_error_t sav_read_variable_record(sav_ctx_t *ctx) { goto cleanup; } for (i=0; in_missing_values; i++) { - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { info->missing_values[i] = byteswap_double(info->missing_values[i]); } @@ -328,7 +328,7 @@ static readstat_error_t sav_skip_value_label_record(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) label_count = byteswap4(label_count); int i; for (i=0; imachine_needs_byte_swap) + if (ctx->bswap) rec_type = byteswap4(rec_type); if (rec_type != 4) { @@ -359,7 +359,7 @@ static readstat_error_t sav_skip_value_label_record(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) var_count = byteswap4(var_count); if (io->seek(var_count * sizeof(int32_t), READSTAT_SEEK_CUR, io->io_ctx) == -1) { @@ -385,7 +385,7 @@ static readstat_error_t sav_submit_value_labels(value_label_t *value_labels, int if (value_type == READSTAT_TYPE_DOUBLE) { double val_d = 0.0; memcpy(&val_d, vlabel->value, 8); - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) val_d = byteswap_double(val_d); value.v.double_value = val_d; @@ -419,7 +419,7 @@ static readstat_error_t sav_read_value_label_record(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) label_count = byteswap4(label_count); if ((value_labels = malloc(label_count * sizeof(value_label_t))) == NULL) { @@ -448,7 +448,7 @@ static readstat_error_t sav_read_value_label_record(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) rec_type = byteswap4(rec_type); if (rec_type != 4) { @@ -459,7 +459,7 @@ static readstat_error_t sav_read_value_label_record(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) var_count = byteswap4(var_count); if ((vars = malloc(var_count * sizeof(int32_t))) == NULL) { @@ -472,7 +472,7 @@ static readstat_error_t sav_read_value_label_record(sav_ctx_t *ctx) { } for (i=0; imachine_needs_byte_swap) + if (ctx->bswap) var_offset = byteswap4(var_offset); var_offset--; // Why subtract 1???? @@ -506,7 +506,7 @@ static readstat_error_t sav_skip_document_record(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) n_lines = byteswap4(n_lines); if (io->seek(n_lines * SPSS_DOC_LINE_SIZE, READSTAT_SEEK_CUR, io->io_ctx) == -1) { retval = READSTAT_ERROR_SEEK; @@ -528,7 +528,7 @@ static readstat_error_t sav_read_document_record(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) n_lines = byteswap4(n_lines); char raw_buffer[SPSS_DOC_LINE_SIZE]; @@ -644,7 +644,7 @@ static readstat_error_t sav_process_row(unsigned char *buffer, size_t buffer_len } } else if (var_info->type == READSTAT_TYPE_DOUBLE) { memcpy(&fp_value, &buffer[data_offset], 8); - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { fp_value = byteswap_double(fp_value); } value.v.double_value = fp_value; @@ -706,8 +706,8 @@ static readstat_error_t sav_read_compressed_data(sav_ctx_t *ctx) { off_t uncompressed_offset = 0; unsigned char *uncompressed_row = malloc(uncompressed_row_len); - int machine_needs_byte_swap = ctx->machine_needs_byte_swap; - ctx->machine_needs_byte_swap = 0; + int bswap = ctx->bswap; + ctx->bswap = 0; while (1) { if (data_offset >= buffer_used) { @@ -772,7 +772,7 @@ static readstat_error_t sav_read_compressed_data(sav_ctx_t *ctx) { if (uncompressed_row) free(uncompressed_row); - ctx->machine_needs_byte_swap = machine_needs_byte_swap; + ctx->bswap = bswap; return retval; } @@ -785,7 +785,7 @@ static readstat_error_t sav_parse_machine_integer_info_record(const void *data, const char *dst_charset = ctx->output_encoding; sav_machine_integer_info_record_t record; memcpy(&record, data, data_len); - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { record.character_code = byteswap4(record.character_code); } if (ctx->input_encoding) { @@ -823,9 +823,9 @@ static readstat_error_t sav_parse_machine_floating_point_record(const void *data sav_machine_floating_point_info_record_t fp_info; memcpy(&fp_info, data, sizeof(sav_machine_floating_point_info_record_t)); - ctx->missing_double = ctx->machine_needs_byte_swap ? byteswap8(fp_info.sysmis) : fp_info.sysmis; - ctx->highest_double = ctx->machine_needs_byte_swap ? byteswap8(fp_info.highest) : fp_info.highest; - ctx->lowest_double = ctx->machine_needs_byte_swap ? byteswap8(fp_info.lowest) : fp_info.lowest; + ctx->missing_double = ctx->bswap ? byteswap8(fp_info.sysmis) : fp_info.sysmis; + ctx->highest_double = ctx->bswap ? byteswap8(fp_info.highest) : fp_info.highest; + ctx->lowest_double = ctx->bswap ? byteswap8(fp_info.lowest) : fp_info.lowest; return READSTAT_OK; } @@ -842,7 +842,7 @@ static readstat_error_t sav_store_variable_display_parameter_record(const void * ctx->variable_display_values_count = count; for (i=0; ivariable_display_values[i] = ctx->machine_needs_byte_swap ? byteswap4(data_ptr[i]) : data_ptr[i]; + ctx->variable_display_values[i] = ctx->bswap ? byteswap4(data_ptr[i]) : data_ptr[i]; } return READSTAT_OK; } @@ -894,7 +894,7 @@ static readstat_error_t sav_parse_long_value_labels_record(const void *data, siz } memcpy(&label_name_len, data_ptr, sizeof(int32_t)); - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) label_name_len = byteswap4(label_name_len); data_ptr += sizeof(int32_t); @@ -934,7 +934,7 @@ static readstat_error_t sav_parse_long_value_labels_record(const void *data, siz } memcpy(&label_count, data_ptr, sizeof(int32_t)); - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) label_count = byteswap4(label_count); data_ptr += sizeof(int32_t); @@ -949,7 +949,7 @@ static readstat_error_t sav_parse_long_value_labels_record(const void *data, siz } memcpy(&value_len, data_ptr, sizeof(int32_t)); - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) value_len = byteswap4(value_len); data_ptr += sizeof(int32_t); @@ -978,7 +978,7 @@ static readstat_error_t sav_parse_long_value_labels_record(const void *data, siz } memcpy(&label_len, data_ptr, sizeof(int32_t)); - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) label_len = byteswap4(label_len); data_ptr += sizeof(int32_t); @@ -1030,7 +1030,7 @@ static readstat_error_t sav_parse_records_pass1(sav_ctx_t *ctx) { goto cleanup; } - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { rec_type = byteswap4(rec_type); } @@ -1058,7 +1058,7 @@ static readstat_error_t sav_parse_records_pass1(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { for (i=0; i<3; i++) extra_info[i] = byteswap4(extra_info[i]); } @@ -1119,7 +1119,7 @@ static readstat_error_t sav_parse_records_pass2(sav_ctx_t *ctx) { goto cleanup; } - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { rec_type = byteswap4(rec_type); } @@ -1150,7 +1150,7 @@ static readstat_error_t sav_parse_records_pass2(sav_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { for (i=0; i<3; i++) extra_info[i] = byteswap4(extra_info[i]); } diff --git a/src/stata/readstat_dta.c b/src/stata/readstat_dta.c index d9c82d0d..122ba4c7 100644 --- a/src/stata/readstat_dta.c +++ b/src/stata/readstat_dta.c @@ -38,12 +38,10 @@ readstat_error_t dta_ctx_init(dta_ctx_t *ctx, int16_t nvar, int32_t nobs, machine_byteorder = DTA_LOHI; } - if (byteorder != machine_byteorder) { - ctx->machine_needs_byte_swap = 1; - } + ctx->bswap = (byteorder != machine_byteorder); - ctx->nvar = ctx->machine_needs_byte_swap ? byteswap2(nvar) : nvar; - ctx->nobs = ctx->machine_needs_byte_swap ? byteswap4(nobs) : nobs; + ctx->nvar = ctx->bswap ? byteswap2(nvar) : nvar; + ctx->nobs = ctx->bswap ? byteswap4(nobs) : nobs; ctx->machine_is_twos_complement = READSTAT_MACHINE_IS_TWOS_COMPLEMENT; diff --git a/src/stata/readstat_dta.h b/src/stata/readstat_dta.h index 2534e9a7..894ace1b 100644 --- a/src/stata/readstat_dta.h +++ b/src/stata/readstat_dta.h @@ -71,7 +71,7 @@ typedef struct dta_ctx_s { int row_limit; int current_row; - int machine_needs_byte_swap; + int bswap; int machine_is_twos_complement; int file_is_xmlish; int supports_tagged_missing; diff --git a/src/stata/readstat_dta_read.c b/src/stata/readstat_dta_read.c index 7d42c278..ca6100b3 100644 --- a/src/stata/readstat_dta_read.c +++ b/src/stata/readstat_dta_read.c @@ -106,9 +106,9 @@ static readstat_error_t dta_read_map(dta_ctx_t *ctx) { goto cleanup; } - ctx->data_offset = ctx->machine_needs_byte_swap ? byteswap8(map_buffer[9]) : map_buffer[9]; - ctx->strls_offset = ctx->machine_needs_byte_swap ? byteswap8(map_buffer[10]) : map_buffer[10]; - ctx->value_labels_offset = ctx->machine_needs_byte_swap ? byteswap8(map_buffer[11]) : map_buffer[11]; + ctx->data_offset = ctx->bswap ? byteswap8(map_buffer[9]) : map_buffer[9]; + ctx->strls_offset = ctx->bswap ? byteswap8(map_buffer[10]) : map_buffer[10]; + ctx->value_labels_offset = ctx->bswap ? byteswap8(map_buffer[11]) : map_buffer[11]; cleanup: return retval; @@ -132,7 +132,7 @@ static readstat_error_t dta_read_descriptors(dta_ctx_t *ctx) { } } else if (ctx->typlist_entry_len == 2) { memcpy(ctx->typlist, buffer, buffer_len); - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { for (i=0; invar; i++) { ctx->typlist[i] = byteswap2(ctx->typlist[i]); } @@ -219,14 +219,14 @@ static readstat_error_t dta_read_expansion_fields(dta_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - len = ctx->machine_needs_byte_swap ? byteswap2(len16) : len16; + len = ctx->bswap ? byteswap2(len16) : len16; } else { int32_t len32; if (io->read(&len32, sizeof(int32_t), io->io_ctx) != sizeof(int32_t)) { retval = READSTAT_ERROR_READ; goto cleanup; } - len = ctx->machine_needs_byte_swap ? byteswap2(len32) : len32; + len = ctx->bswap ? byteswap2(len32) : len32; } if (data_type == 0 && len == 0) @@ -301,7 +301,7 @@ static int dta_compare_strls(const void *elem1, const void *elem2) { } static void dta_interpret_strl_vo_bytes(dta_ctx_t *ctx, unsigned char *vo_bytes, dta_strl_t *strl) { - int file_is_big_endian = (!machine_is_little_endian() ^ ctx->machine_needs_byte_swap); + int file_is_big_endian = (!machine_is_little_endian() ^ ctx->bswap); if (ctx->strl_v_len == 2) { if (file_is_big_endian) { @@ -325,8 +325,8 @@ static void dta_interpret_strl_vo_bytes(dta_ctx_t *ctx, unsigned char *vo_bytes, memcpy(&v, &vo_bytes[0], sizeof(uint32_t)); memcpy(&o, &vo_bytes[4], sizeof(uint32_t)); - strl->v = ctx->machine_needs_byte_swap ? byteswap4(v) : v; - strl->o = ctx->machine_needs_byte_swap ? byteswap4(o) : o; + strl->v = ctx->bswap ? byteswap4(v) : v; + strl->o = ctx->bswap ? byteswap4(o) : o; } } @@ -342,7 +342,7 @@ static readstat_error_t dta_read_strl(dta_ctx_t *ctx, dta_strl_t *strl) { dta_interpret_strl_vo_bytes(ctx, header.vo_bytes, strl); strl->type = header.type; - strl->len = ctx->machine_needs_byte_swap ? byteswap4(header.len) : header.len; + strl->len = ctx->bswap ? byteswap4(header.len) : header.len; cleanup: return retval; @@ -467,7 +467,7 @@ static readstat_error_t dta_handle_rows(dta_ctx_t *ctx) { value.v.i8_value = byte; } else if (value.type == READSTAT_TYPE_INT16) { int16_t num = *((int16_t *)&buf[offset]); - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { num = byteswap2(num); } if (ctx->machine_is_twos_complement) { @@ -484,7 +484,7 @@ static readstat_error_t dta_handle_rows(dta_ctx_t *ctx) { value.v.i16_value = num; } else if (value.type == READSTAT_TYPE_INT32) { int32_t num = *((int32_t *)&buf[offset]); - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { num = byteswap4(num); } if (ctx->machine_is_twos_complement) { @@ -502,7 +502,7 @@ static readstat_error_t dta_handle_rows(dta_ctx_t *ctx) { } else if (value.type == READSTAT_TYPE_FLOAT) { int32_t num = *((int32_t *)&buf[offset]); float f_num = NAN; - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { num = byteswap4(num); } if (num > ctx->max_float) { @@ -519,7 +519,7 @@ static readstat_error_t dta_handle_rows(dta_ctx_t *ctx) { } else if (value.type == READSTAT_TYPE_DOUBLE) { int64_t num = *((int64_t *)&buf[offset]); double d_num = NAN; - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { num = byteswap8(num); } if (num > ctx->max_double) { @@ -676,7 +676,7 @@ static readstat_error_t dta_read_label_and_timestamp(dta_ctx_t *ctx) { retval = READSTAT_ERROR_READ; goto cleanup; } - label_len = ctx->machine_needs_byte_swap ? byteswap2(label_len) : label_len; + label_len = ctx->bswap ? byteswap2(label_len) : label_len; } else if (ctx->data_label_len_len == 1) { unsigned char label_len_char; if (io->read(&label_len_char, sizeof(unsigned char), io->io_ctx) != sizeof(unsigned char)) { @@ -836,7 +836,7 @@ static readstat_error_t dta_handle_value_labels(dta_ctx_t *ctx) { len = table_header_len; - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) len = byteswap2(table_header_len); n = len / 8; @@ -851,7 +851,7 @@ static readstat_error_t dta_handle_value_labels(dta_ctx_t *ctx) { len = table_header_len; - if (ctx->machine_needs_byte_swap) + if (ctx->bswap) len = byteswap4(table_header_len); } @@ -891,7 +891,7 @@ static readstat_error_t dta_handle_value_labels(dta_ctx_t *ctx) { n = *(int32_t *)table_buffer; int32_t txtlen = *((int32_t *)table_buffer+1); - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { n = byteswap4(n); txtlen = byteswap4(txtlen); } @@ -904,7 +904,7 @@ static readstat_error_t dta_handle_value_labels(dta_ctx_t *ctx) { int32_t *val = (int32_t *)table_buffer+2+n; char *txt = &table_buffer[8*n+8]; - if (ctx->machine_needs_byte_swap) { + if (ctx->bswap) { for (i=0; i