diff options
author | Salvador Cuñat <salvador.cunat@gmail.com> | 2017-05-20 09:41:55 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2017-06-11 13:50:59 -0700 |
commit | 95ee57915027b41040e57fdeac8cd8e0b6be306d (patch) | |
tree | 0445cbafe34292d1a504d2c0919b7825d8a92597 /smtk-import | |
parent | b47d18b45ac135ab9c7c0152fd9713d7aee2b407 (diff) | |
download | subsurface-95ee57915027b41040e57fdeac8cd8e0b6be306d.tar.gz |
smtk-import: portability: rework non portable funcs
For different reasons some used functions aren't portable or simply are
not included in mingw. This includes index, rindex, strptime, and
timegm.
A workaround for this is needed, if we want to build for windows using
mingw based mxe environment. This patch does:
- drops index and rindex in favor of strchr and strrchr
- substitute strptime with a sscanf parsing
- emulate timegm with a private func smtk_timegm()
- remove definitions needed by strptime
Signed-off-by: Salvador Cuñat <salvador.cunat@gmail.com>
Diffstat (limited to 'smtk-import')
-rw-r--r-- | smtk-import/smartrak.c | 78 |
1 files changed, 55 insertions, 23 deletions
diff --git a/smtk-import/smartrak.c b/smtk-import/smartrak.c index 3d9cfe0c3..3997975de 100644 --- a/smtk-import/smartrak.c +++ b/smtk-import/smartrak.c @@ -15,14 +15,6 @@ * to call smartrak_import() */ -#ifndef __USE_XOPEN -#define __USE_XOPEN -#endif - -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -98,32 +90,47 @@ static int coln(enum field_pos pos) /* * Fills the date part of a tm structure with the string data obtained * from smartrak in format "DD/MM/YY HH:MM:SS" where time is irrelevant. - * TODO: Verify localization. */ static void smtk_date_to_tm(char *d_buffer, struct tm *tm_date) { - char *temp = NULL; - - temp = copy_string(d_buffer); - strtok(temp, " "); - if (temp) { - strptime(temp, "%x", tm_date); - free(temp); + int n, d, m, y; + + if ((d_buffer) && (!same_string(d_buffer, ""))) { + n = sscanf(d_buffer, "%d/%d/%d ", &m, &d, &y); + y = (y < 70) ? y + 100 : y; + if (n == 3) { + tm_date->tm_mday = d; + tm_date->tm_mon = m - 1; + tm_date->tm_year = y; + } else { + tm_date->tm_mday = tm_date->tm_mon = tm_date->tm_year = 0; + } + } else { + tm_date->tm_mday = tm_date->tm_mon = tm_date->tm_year = 0; } } /* * Fills the time part of a tm structure with the string data obtained * from smartrak in format "DD/MM/YY HH:MM:SS" where date is irrelevant. - * TODO: Verify localization. */ static void smtk_time_to_tm(char *t_buffer, struct tm *tm_date) { - char *temp = NULL; + unsigned int n, hr, min, sec; - temp = index(t_buffer, ' '); - if (temp) - strptime(temp, "%X", tm_date); + if ((t_buffer) && (!same_string(t_buffer, ""))) { + n = sscanf(t_buffer, "%*m[/0-9] %d:%d:%d ", &hr, &min, &sec); + if (n == 3) { + tm_date->tm_hour = hr; + tm_date->tm_min = min; + tm_date->tm_sec = sec; + } else { + tm_date->tm_hour = tm_date->tm_min = tm_date->tm_sec = 0; + } + } else { + tm_date->tm_hour = tm_date->tm_min = tm_date->tm_sec = 0; + } + tm_date->tm_isdst = -1; } /* @@ -145,6 +152,31 @@ static unsigned int smtk_time_to_secs(char *t_buffer) } /* + * Emulate the non portable timegm() function. + * Based on timegm man page, changing setenv and unsetenv with putenv, + * because of portability issues. + */ +static time_t smtk_timegm(struct tm *tm) +{ + time_t ret; + char *tz, *str; + + tz = getenv("TZ"); + putenv("TZ="); + tzset(); + ret = mktime(tm); + if (tz) { + str = calloc(strlen(tz)+4, 1); + sprintf(str, "TZ=%s", tz); + putenv(str); + } else { + putenv("TZ"); + } + tzset(); + return ret; +} + +/* * Returns an opened table given its name and mdb. outcol and outbounder have to be allocated * by the caller and are filled here. */ @@ -284,7 +316,7 @@ static void smtk_wreck_site(MdbHandle *mdb, char *site_idx, struct dive_site *ds case 5: tmp = copy_string(col[i]->bind_ptr); if (tmp) - notes = smtk_concat_str(notes, "\n", "%s: %s", wreck_fields[i - 3], rindex(tmp, ' ')); + notes = smtk_concat_str(notes, "\n", "%s: %s", wreck_fields[i - 3], strrchr(tmp, ' ')); free(tmp); break; case 6 ... 9: @@ -923,7 +955,7 @@ void smartrak_import(const char *file, struct dive_table *divetable) /* Date issues with libdc parser - Take date time from mdb */ smtk_date_to_tm(col[coln(_DATE)]->bind_ptr, tm_date); smtk_time_to_tm(col[coln(INTIME)]->bind_ptr, tm_date); - smtkdive->dc.when = smtkdive->when = timegm(tm_date); + smtkdive->dc.when = smtkdive->when = smtk_timegm(tm_date); free(tm_date); smtkdive->dc.surfacetime.seconds = smtk_time_to_secs(col[coln(INTVAL)]->bind_ptr); |