kosma / minmea

a lightweight GPS NMEA 0183 parser library in pure C
Do What The F*ck You Want To Public License
755 stars 246 forks source link

Hi, and few remarks about minmea lib =) #10

Closed Looongcat closed 10 years ago

Looongcat commented 10 years ago

Hi guys! You're doing awesome project, but here's some small troubles i've got on windows platform with code::blocks & mingw (dunno what about stm32, but i think that iar will get the same troubles):

1) Loop variables was declared inside of loop header Such construct requests at least C99 mode. It's better to declare all necessary variables before function's code for compability with C89 so the code looks like:

int foo(void) {
    int i = 0;
    for (i = 0; ...; ...) {
        ...
    }
}

2) Seems like timegm function isn't presented in windows libraries, so here it's code (which is obviously very bad for embedded because of multiplication):

static int is_leap(unsigned y) {
    y += 1900;
    return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
}

time_t timegm(struct tm *tm) {
    static const unsigned ndays[2][12] = {
            {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
            {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };

    time_t res = 0;
    int i;

    for (i = 70; i < tm->tm_year; ++i)
        res += is_leap(i) ? 366 : 365;

    for (i = 0; i < tm->tm_mon; ++i)
        res += ndays[is_leap(tm->tm_year)][i];

    res += tm->tm_mday - 1;
    res *= 24;
    res += tm->tm_hour;
    res *= 60;
    res += tm->tm_min;
    res *= 60;
    res += tm->tm_sec;
    return res;
}
kosma commented 10 years ago

1) Look at me. Now look at your compiler. Now look at me. Now look at the calendar. It's 2014. C89 is 25 years old. Everything is possible when your IDE's default settings suck. I'm on a horse.

C99 is widely supported, and writing in that 1989 dialect is no fun at all. Here's how to set it under Code::Blocks:

2) Have you tried building with -Dtimegm=_mkgmtime or -Dtimegm=mktime?

Looongcat commented 10 years ago

1) But if you can write code which will work under any c compiler - why not? 2) Tried when you said, but it didn't help at all (dunno why). Nope, my fault. It really works.

swistakm commented 10 years ago

ad 1) as stated in README.md: "Written in ISO C99". It's a project decision made at the beginning of development. Standard compliance it's not only a matter of "for" syntax but any other feature of C99 that is in use. Case closed.

Looongcat commented 10 years ago

P. S. Maybe it'll help to add in .h:

#ifdef __WIN__
#define timegm=mktime
#endif

or something similar :)

kosma commented 10 years ago

The problem with timegm is that it's a GNU extension (that's also present on some other platforms besides Linux, notably OS X) and there's no single corresponding function on other platforms. Most systems have mktime, but it only works correctly as long as you're running under UTC timezone; Windows has _mkgmtime but I have no idea how portable between different Windows implementations it is. Doing this correctly requires lots of cross-platform knowledge, which I unfortunately lack - so I left it as-is.

kosma commented 10 years ago

And yeah, as @swistakm said, using the modern C99 features is a design decision.

Looongcat commented 10 years ago

Well, case is really closed. Thank you guys.

konosubakonoakua commented 5 years ago
  1. Look at me. Now look at your compiler. Now look at me. Now look at the calendar. It's 2014. C89 is 25 years old. Everything is possible when your IDE's default settings suck. I'm on a horse.

C99 is widely supported, and writing in that 1989 dialect is no fun at all. Here's how to set it under Code::Blocks:

  1. Have you tried building with -Dtimegm=_mkgmtime or -Dtimegm=mktime?

Add Dtimegm=mktime worked for STM32 platform compiled by ARMCLANG.