Added fallback to strtok on windows since strtok_s is not available on all windows platforms

This commit is contained in:
Snaipe 2015-07-31 08:17:19 +02:00
parent 0e879aa34b
commit 9f26b192ec
2 changed files with 23 additions and 10 deletions

View file

@ -35,10 +35,11 @@
#include "i18n.h"
#include "posix-compat.h"
#define USED __attribute__ ((used))
#ifdef VANILLA_WIN32
// provided by windows' libc implementation
char *strtok_s(char *strToken, const char *strDelimit, char **context);
# define strtok_r strtok_s
// fallback to strtok on windows since strtok_s is not available everywhere
# define strtok_r(str, delim, saveptr) strtok(str, delim)
#endif
typedef const char *const msg_t;
@ -151,8 +152,13 @@ void normal_log_assert(struct criterion_assert_stats *stats) {
if (!stats->passed) {
char *dup = strdup(*stats->message ? stats->message
: stats->condition);
#ifdef VANILLA_WIN32
char *line = strtok(dup, "\n");
#else
char *saveptr = NULL;
char *line = strtok_r(dup, "\n", &saveptr);
#endif
criterion_pimportant(CRITERION_PREFIX_DASHES,
_(msg_assert_fail),
@ -160,7 +166,11 @@ void normal_log_assert(struct criterion_assert_stats *stats) {
FG_RED, stats->line, RESET,
line);
#ifdef VANILLA_WIN32
while ((line = strtok(NULL, "\n")))
#else
while ((line = strtok_r(NULL, "\n", &saveptr)))
#endif
criterion_pimportant(CRITERION_PREFIX_DASHES, _(msg_desc), line);
free(dup);
}

View file

@ -33,12 +33,6 @@
#include "config.h"
#include "posix-compat.h"
#ifdef VANILLA_WIN32
// provided by windows' libc implementation
char *strtok_s(char *strToken, const char *strDelimit, char **context);
# define strtok_r strtok_s
#endif
void tap_log_pre_all(struct criterion_test_set *set) {
size_t enabled_count = 0;
FOREACH_SET(struct criterion_suite_set *s, set->suites) {
@ -88,13 +82,22 @@ void tap_log_post_test(struct criterion_test_stats *stats) {
stats->elapsed_time);
for (struct criterion_assert_stats *asrt = stats->asserts; asrt; asrt = asrt->next) {
if (!asrt->passed) {
char *dup = strdup(*asrt->message ? asrt->message : asrt->condition), *saveptr = NULL;
char *dup = strdup(*asrt->message ? asrt->message : asrt->condition);
#ifdef VANILLA_WIN32
char *line = strtok(dup, "\n");
#else
char *saveptr = NULL;
char *line = strtok_r(dup, "\n", &saveptr);
#endif
criterion_important(" %s:%u: Assertion failed: %s\n",
asrt->file,
asrt->line,
line);
#ifdef VANILLA_WIN32
while ((line = strtok(NULL, "\n")))
#else
while ((line = strtok_r(NULL, "\n", &saveptr)))
#endif
criterion_important(" %s\n", line);
free(dup);
}