Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
Problems due to MINGW32 build solved (gmtime incosistencies)
Browse files Browse the repository at this point in the history
compile commands in README.md edited
  • Loading branch information
ThomTe committed Sep 18, 2017
1 parent 67ec82f commit 5628398
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ Usage example
Compilation and tests run examples
----------------------------------

gcc ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c89 && ./a.out
g++ ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c++11 && ./a.out
g++ ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c++11 -DCRON_COMPILE_AS_CXX && ./a.out
gcc ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c89 -DCRON_TEST_MALLOC -o a.out && ./a.out
g++ ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c++11 -DCRON_TEST_MALLOC -o a.out && ./a.out
g++ ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c++11 -DCRON_TEST_MALLOC -DCRON_COMPILE_AS_CXX -o a.out && ./a.out

clang ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c89 && ./a.out
clang++ ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c++11 && ./a.out
clang++ ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c++11 -DCRON_COMPILE_AS_CXX && ./a.out
clang ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c89 -DCRON_TEST_MALLOC -o a.out && ./a.out
clang++ ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c++11 -DCRON_TEST_MALLOC -o a.out && ./a.out
clang++ ccronexpr.c ccronexpr_test.c -I. -Wall -Wextra -std=c++11 -DCRON_TEST_MALLOC -DCRON_COMPILE_AS_CXX -o a.out && ./a.out

cl ccronexpr.c ccronexpr_test.c /W4 /D_CRT_SECURE_NO_WARNINGS && ccronexpr.exe

Expand Down
18 changes: 12 additions & 6 deletions ccronexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ time_t cron_mktime(struct tm* tm) {
}
#endif /* _WIN32 */

#ifndef TEST

#ifndef CRON_TEST_MALLOC
#define cronFree(x) free(x);
#define cronMalloc(x) malloc(x);
#else
Expand All @@ -109,11 +110,16 @@ void cronFree(void* p);
#endif

struct tm* cron_time(time_t* date, struct tm* out) {
#ifdef _WIN32
#ifdef __MINGW32__
(void)(out); /* To avoid unused warning */
return gmtime(date);
#else /* __MINGW32__ */
#ifdef _WIN32
return gmtime_s(date, out);
#else /* _WIN32 */
return gmtime_r(date, out);
#endif /* _WIN32 */
#endif /* __MINGW32__ */
}

#else /* CRON_USE_LOCAL_TIME */
Expand Down Expand Up @@ -419,7 +425,7 @@ static int do_next(cron_expr* expr, struct tm* calendar, unsigned int dot) {
if (0 != res) goto return_result;
}

month = calendar->tm_mon; //day already adds one if no day in same month is found
month = calendar->tm_mon; /*day already adds one if no day in same month is found*/
update_month = find_next(expr->months, CRON_MAX_MONTHS, month, calendar, CRON_CF_MONTH, CRON_CF_YEAR, resets, &res);
if (0 != res) goto return_result;
if (month != update_month) {
Expand Down Expand Up @@ -794,7 +800,7 @@ void set_number_hits(const char* value, uint8_t* target, unsigned int min, unsig

static void set_months(char* value, uint8_t* targ, const char** error) {
int err;

unsigned int i;
unsigned int max = 12;

char* replaced = NULL;
Expand All @@ -808,7 +814,7 @@ static void set_months(char* value, uint8_t* targ, const char** error) {
cronFree(replaced);

/* ... and then rotate it to the front of the months */
for (int i = 1; i <= max; i++) {
for (i = 1; i <= max; i++) {
if (cron_getBit(targ, i)) {
cron_setBit(targ, i - 1);
cron_delBit(targ, i);
Expand Down Expand Up @@ -864,7 +870,7 @@ void cron_parse_expr(const char* expression, cron_expr* target, const char** err
cronFree(days_replaced);
if (*error) goto return_res;
if (cron_getBit(target->days_of_week, 7)) {
// Sunday can be represented as 0 or 7
/* Sunday can be represented as 0 or 7*/
cron_setBit(target->days_of_week, 0);
cron_delBit(target->days_of_week, 7);
}
Expand Down
9 changes: 7 additions & 2 deletions ccronexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ extern "C" {
#include <time64.h>
#endif /* ANDROID */

#include <stdint.h> //added for use if uint*_t data types
#include <stdint.h> /*added for use if uint*_t data types*/

#ifndef ARRAY_LEN
#define ARRAY_LEN(x) sizeof(x)/sizeof(x[0])
#endif

#ifdef __MINGW32__
/* To avoid warning when building with mingw */
time_t _mkgmtime(struct tm* tm);
#endif /* __MINGW32__ */

/**
* Parsed cron expression
*/
Expand Down Expand Up @@ -93,7 +98,7 @@ void cron_delBit(uint8_t* rbyte, int idx);
void cron_expr_free(cron_expr* expr);

#if defined(__cplusplus) && !defined(CRON_COMPILE_AS_CXX)
} // extern "C"
} /* extern "C"*/
#endif

#endif /* CCRONEXPR_H */
Expand Down
18 changes: 11 additions & 7 deletions ccronexpr_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

#define DATE_FORMAT "%Y-%m-%d_%H:%M:%S"

#ifdef TEST
#ifdef CRON_TEST_MALLOC
static int cronAllocations = 0;
static int cronTotalAllocations = 0;
static int maxAlloc = 0;
Expand Down Expand Up @@ -80,7 +80,7 @@ static time_t timegm(struct tm * const t) {
#endif

static int crons_equal(cron_expr* cr1, cron_expr* cr2) {
int i;
unsigned int i;
for (i = 0; i < ARRAY_LEN(cr1->seconds); i++) {
if (cr1->seconds[i] != cr2->seconds[i]) {
printf("seconds not equal @%d %02x != %02x", i, cr1->seconds[i], cr2->seconds[i]);
Expand Down Expand Up @@ -330,8 +330,9 @@ void test_bits() {
uint8_t testbyte[8];
memset(testbyte, 0, 8);
int err = 0;
int i;

for (int i = 0; i <= 63; i++) {
for (i = 0; i <= 63; i++) {
cron_setBit(testbyte, i);
if (!cron_getBit(testbyte, i)) {
printf("Bit set error! Bit: %d!\n", i);
Expand All @@ -345,7 +346,7 @@ void test_bits() {
assert(!err);
}

for (int i = 0; i < 12; i++) {
for (i = 0; i < 12; i++) {
cron_setBit(testbyte, i);
}
if (testbyte[0] != 0xff) {
Expand All @@ -358,7 +359,8 @@ void test_bits() {
assert(!err);
}

// For this test to work you need to set "-DTEST=1"
/* For this test to work you need to set "-DCRON_TEST_MALLOC=1"*/
#ifdef CRON_TEST_MALLOC
void test_memory() {
cron_expr cron;
const char* err;
Expand All @@ -370,6 +372,7 @@ void test_memory() {
}
printf("Allocations: total: %d, max: %d", cronTotalAllocations, maxAlloc);
}
#endif

int main() {

Expand All @@ -378,8 +381,9 @@ int main() {
test_expr();
test_parse();
check_calc_invalid();
test_memory(); // For this test to work you need to set "-DTEST=1"

#ifdef CRON_TEST_MALLOC
test_memory(); /* For this test to work you need to set "-DCRON_TEST_MALLOC=1"*/
#endif
printf("\nAll OK!");
return 0;
}
Expand Down

0 comments on commit 5628398

Please sign in to comment.