Changeset 95ee579 in subsurface


Ignore:
Timestamp:
Jun 11, 2017, 1:50:59 PM (2 weeks ago)
Author:
Dirk Hohndel <dirk@…>
Branches:
master
Children:
bde73f0
Parents:
b47d18b4
git-author:
Salvador Cuñat <salvador.cunat@…> (05/20/17 00:41:55)
git-committer:
Dirk Hohndel <dirk@…> (06/11/17 13:50:59)
Message:

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@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • smtk-import/smartrak.c

    rb47d18b4 r95ee579  
    1616 */
    1717
    18 #ifndef __USE_XOPEN
    19 #define __USE_XOPEN
    20 #endif
    21 
    22 #ifndef _GNU_SOURCE
    23 #define _GNU_SOURCE
    24 #endif
    25 
    2618#include <stdlib.h>
    2719#include <stdio.h>
     
    9991 * Fills the date part of a tm structure with the string data obtained
    10092 * from smartrak in format "DD/MM/YY HH:MM:SS" where time is irrelevant.
    101  * TODO: Verify localization.
    10293 */
    10394static void smtk_date_to_tm(char *d_buffer, struct tm *tm_date)
    10495{
    105         char *temp = NULL;
    106 
    107         temp = copy_string(d_buffer);
    108         strtok(temp, " ");
    109         if (temp) {
    110                 strptime(temp, "%x", tm_date);
    111                 free(temp);
     96        int n, d, m, y;
     97
     98        if ((d_buffer) && (!same_string(d_buffer, ""))) {
     99                n = sscanf(d_buffer, "%d/%d/%d ", &m, &d, &y);
     100                y = (y < 70) ? y + 100 : y;
     101                if (n == 3) {
     102                        tm_date->tm_mday = d;
     103                        tm_date->tm_mon = m - 1;
     104                        tm_date->tm_year = y;
     105                } else {
     106                        tm_date->tm_mday = tm_date->tm_mon = tm_date->tm_year = 0;
     107                }
     108        } else {
     109                tm_date->tm_mday = tm_date->tm_mon = tm_date->tm_year = 0;
    112110        }
    113111}
     
    116114 * Fills the time part of a tm structure with the string data obtained
    117115 * from smartrak in format "DD/MM/YY HH:MM:SS" where date is irrelevant.
    118  * TODO: Verify localization.
    119116 */
    120117static void smtk_time_to_tm(char *t_buffer, struct tm *tm_date)
    121118{
    122         char *temp = NULL;
    123 
    124         temp = index(t_buffer, ' ');
    125         if (temp)
    126                 strptime(temp, "%X", tm_date);
     119        unsigned int n, hr, min, sec;
     120
     121        if ((t_buffer) && (!same_string(t_buffer, ""))) {
     122                n = sscanf(t_buffer, "%*m[/0-9] %d:%d:%d ", &hr, &min, &sec);
     123                if (n == 3) {
     124                        tm_date->tm_hour = hr;
     125                        tm_date->tm_min = min;
     126                        tm_date->tm_sec = sec;
     127                } else {
     128                        tm_date->tm_hour = tm_date->tm_min = tm_date->tm_sec = 0;
     129                }
     130        } else {
     131                tm_date->tm_hour = tm_date->tm_min = tm_date->tm_sec = 0;
     132        }
     133        tm_date->tm_isdst = -1;
    127134}
    128135
     
    143150                return 0;
    144151        }
     152}
     153
     154/*
     155 * Emulate the non portable timegm() function.
     156 * Based on timegm man page, changing setenv and unsetenv with putenv,
     157 * because of portability issues.
     158 */
     159static time_t smtk_timegm(struct tm *tm)
     160{
     161        time_t ret;
     162        char *tz, *str;
     163
     164        tz = getenv("TZ");
     165        putenv("TZ=");
     166        tzset();
     167        ret = mktime(tm);
     168        if (tz) {
     169                str = calloc(strlen(tz)+4, 1);
     170                sprintf(str, "TZ=%s", tz);
     171                putenv(str);
     172        } else {
     173                putenv("TZ");
     174        }
     175        tzset();
     176        return ret;
    145177}
    146178
     
    285317                                        tmp = copy_string(col[i]->bind_ptr);
    286318                                        if (tmp)
    287                                                 notes = smtk_concat_str(notes, "\n", "%s: %s", wreck_fields[i - 3], rindex(tmp, ' '));
     319                                                notes = smtk_concat_str(notes, "\n", "%s: %s", wreck_fields[i - 3], strrchr(tmp, ' '));
    288320                                        free(tmp);
    289321                                        break;
     
    924956                smtk_date_to_tm(col[coln(_DATE)]->bind_ptr, tm_date);
    925957                smtk_time_to_tm(col[coln(INTIME)]->bind_ptr, tm_date);
    926                 smtkdive->dc.when = smtkdive->when = timegm(tm_date);
     958                smtkdive->dc.when = smtkdive->when = smtk_timegm(tm_date);
    927959                free(tm_date);
    928960                smtkdive->dc.surfacetime.seconds = smtk_time_to_secs(col[coln(INTVAL)]->bind_ptr);
Note: See TracChangeset for help on using the changeset viewer.