Skip to content

Commit

Permalink
fix windows compilation error
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 18, 2024
1 parent 265cef2 commit 2cf68e0
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions c/src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ typedef struct {
} Answer;

Answer get_answer(uint16_t id) {
char *linepointer;
char *tabpointer;
Answer ret = {
.id = id,
};
char *answers = get_data_file("answers.tsv");
char *linepointer, *tabpointer;
#ifdef _WIN32
rsize_t linemax = strlen(answers) + 1, tabmax;
#endif

if (!answers) {
fprintf(stderr, "Error: Unable to get data from file\n");
Expand All @@ -147,13 +149,27 @@ Answer get_answer(uint16_t id) {
char s_id[6];
snprintf(s_id, sizeof(s_id), "%" PRIu16, id);

#ifdef _WIN32
char *line = strtok_s(answers, &linemax, "\n", &linepointer); // skip header
while ((line = strtok_s(NULL, &linemax, "\n", &linepointer)) != NULL) {
tabmax = strlen(line) + 1;
#else
char *line = strtok_r(answers, "\n", &linepointer); // skip header
while ((line = strtok_r(NULL, "\n", &linepointer)) != NULL) {
#endif
#ifdef _WIN32
char *token = strtok_s(line, &tabmax, "\t", &tabpointer);
#else
char *token = strtok_r(line, "\t", &tabpointer);
#endif
if (strcmp(token, s_id) != 0)
continue;

#ifdef _WIN32
token = strtok_s(NULL, &tabmax, "\t", &tabpointer);
#else
token = strtok_r(NULL, "\t", &tabpointer);
#endif
if (!token)
continue;

Expand All @@ -168,12 +184,20 @@ Answer get_answer(uint16_t id) {
return ret;
}

#ifdef _WIN32
token = strtok_s(NULL, &tabmax, "\t", &tabpointer);
#else
token = strtok_r(NULL, "\t", &tabpointer);
#endif
if (!token)
continue;
size_t size = strtoull(token, NULL, 10);

#ifdef _WIN32
token = strtok_s(NULL, &tabmax, "\t", &tabpointer);
#else
token = strtok_r(NULL, "\t", &tabpointer);
#endif
if (!token)
continue;

Expand Down

0 comments on commit 2cf68e0

Please sign in to comment.