Skip to content

Commit

Permalink
fixup! fixup! wip
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodan-ilic committed Jun 12, 2024
1 parent 8b453bd commit 0a83ade
Showing 1 changed file with 16 additions and 38 deletions.
54 changes: 16 additions & 38 deletions src/spss/readstat_sav_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,26 @@ static readstat_error_t sav_parse_long_string_value_labels_record(const void *da
static readstat_error_t sav_parse_long_string_missing_values_record(const void *data, size_t size, size_t count, sav_ctx_t *ctx);
static readstat_error_t sav_read_multiple_response_sets(size_t data_len, sav_ctx_t *ctx);

static readstat_error_t parse_mr_counted_value(const char *line, mr_set_t *result) {
static readstat_error_t parse_mr_counted_value(const char **next_part, mr_set_t *result) {
readstat_error_t retval = READSTAT_OK;
if (result->type == 'D') {
result->is_dichotomy = 1;
const char *digit_start = line;
while (*line != ' ' && *line != '\0') {
line++;
const char *digit_start = (*next_part);
while (*(*next_part) != ' ' && *(*next_part) != '\0') {
(*next_part)++;
}
int internal_count = (int)strtol(digit_start, NULL, 10);
if (*line != ' ') {
if (*(*next_part) != ' ') {
retval = READSTAT_ERROR_BAD_MR_STRING;
goto cleanup;
}
line++;
digit_start = line;
for (int i = 0; i < internal_count && isdigit(*line); i++) {
line++;
(*next_part)++;
digit_start = (*next_part);
for (int i = 0; i < internal_count && isdigit(*(*next_part)); i++) {
(*next_part)++;
}
result->counted_value = (int)strtol(digit_start, NULL, 10);
if (*line != ' ' && *line != '\0') {
if (*(*next_part) != ' ' && *(*next_part) != '\0') {
retval = READSTAT_ERROR_BAD_MR_STRING;
goto cleanup;
}
Expand Down Expand Up @@ -200,39 +200,16 @@ static readstat_error_t parse_mr_line(const char *line, mr_set_t *result) {
strncpy(result->name, line, name_length);
result->name[name_length] = '\0';
const char *next_part = equals_pos + 2; // Start after the '=' and type character
if (result->type == 'D') {
result->is_dichotomy = 1;
const char *digit_start = next_part;
while (*next_part != ' ' && *next_part != '\0') {
next_part++;
}
int internal_count = (int)strtol(digit_start, NULL, 10);
if (*next_part != ' ') {
retval = READSTAT_ERROR_BAD_MR_STRING;
goto cleanup;
}
next_part++;
digit_start = next_part;
for (int i = 0; i < internal_count && isdigit(*next_part); i++) {
next_part++;
}
result->counted_value = (int)strtol(digit_start, NULL, 10);
if (*next_part != ' ' && *next_part != '\0') {
retval = READSTAT_ERROR_BAD_MR_STRING;
goto cleanup;
}
}
else if (result->type == 'C') {
result->is_dichotomy = 0;
result->counted_value = -1;
}
if ((retval = parse_mr_counted_value(&next_part, result)) != READSTAT_OK) goto cleanup;
if (*next_part != ' ') {
retval = READSTAT_ERROR_BAD_MR_STRING;
goto cleanup;
}
next_part++;
const char *digit_start = next_part;
while (isdigit(*next_part)) next_part++;
while (isdigit(*next_part)) {
next_part++;
}
if (*next_part != ' ') {
retval = READSTAT_ERROR_BAD_MR_STRING;
goto cleanup;
Expand All @@ -244,7 +221,8 @@ static readstat_error_t parse_mr_line(const char *line, mr_set_t *result) {
goto cleanup;
}

if ((result->label = malloc(count + 1)) == NULL) {
result->label = malloc(count + 1); // +1 for the null-terminator
if (result->label == NULL) {
retval = READSTAT_ERROR_MALLOC;
goto cleanup;
}
Expand Down

0 comments on commit 0a83ade

Please sign in to comment.