Fix:vehicle_demo:Give back a reasonable timestamp
[navit-package] / navit / util.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <glib.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include "util.h"
25
26 void
27 strtoupper(char *dest, const char *src)
28 {
29         while (*src)
30                 *dest++=toupper(*src++);
31         *dest='\0';
32 }
33
34 void
35 strtolower(char *dest, const char *src)
36 {
37         while (*src)
38                 *dest++=tolower(*src++);
39         *dest='\0';
40 }
41
42
43 static void
44 hash_callback(gpointer key, gpointer value, gpointer user_data)
45 {
46         GList **l=user_data;
47         *l=g_list_prepend(*l, value);
48 }
49
50 GList *
51 g_hash_to_list(GHashTable *h)
52 {
53         GList *ret=NULL;
54         g_hash_table_foreach(h, hash_callback, &ret);
55
56         return ret;
57 }
58
59 static void
60 hash_callback_key(gpointer key, gpointer value, gpointer user_data)
61 {
62         GList **l=user_data;
63         *l=g_list_prepend(*l, key);
64 }
65
66 GList *
67 g_hash_to_list_keys(GHashTable *h)
68 {
69         GList *ret=NULL;
70         g_hash_table_foreach(h, hash_callback_key, &ret);
71
72         return ret;
73 }
74
75 gchar *
76 g_strconcat_printf(gchar *buffer, gchar *fmt, ...)
77 {
78         gchar *str,*ret;
79         va_list ap;
80
81         va_start(ap, fmt);
82         str=g_strdup_vprintf(fmt, ap);
83         va_end(ap);
84         if (! buffer)
85                 return str;
86         ret=g_strconcat(buffer, str, NULL);
87         g_free(buffer);
88         g_free(str);
89         return ret;
90 }
91
92 #ifndef HAVE_GLIB
93 int g_utf8_strlen_force_link(gchar *buffer, int max);
94 int
95 g_utf8_strlen_force_link(gchar *buffer, int max)
96 {
97         return g_utf8_strlen(buffer, max);
98 }
99 #endif
100
101 #if defined(_WIN32) || defined(__CEGCC__)
102 #include <windows.h>
103 #endif
104
105 #if defined(_WIN32) || defined(__CEGCC__) || defined (__APPLE__)
106 #include <stdio.h>
107 char *stristr(const char *String, const char *Pattern)
108 {
109       char *pptr, *sptr, *start;
110
111       for (start = (char *)String; *start != (int)NULL; start++)
112       {
113             /* find start of pattern in string */
114             for ( ; ((*start!=(int)NULL) && (toupper(*start) != toupper(*Pattern))); start++)
115                   ;
116             if ((int)NULL == *start)
117                   return NULL;
118
119             pptr = (char *)Pattern;
120             sptr = (char *)start;
121
122             while (toupper(*sptr) == toupper(*pptr))
123             {
124                   sptr++;
125                   pptr++;
126
127                   /* if end of pattern then pattern was found */
128
129                   if ((int)NULL == *pptr)
130                         return (start);
131             }
132       }
133       return NULL;
134 }
135
136 #ifndef SIZE_MAX
137 # define SIZE_MAX ((size_t) -1)
138 #endif
139 #ifndef SSIZE_MAX
140 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
141 #endif
142 #if !HAVE_FLOCKFILE
143 # undef flockfile
144 # define flockfile(x) ((void) 0)
145 #endif
146 #if !HAVE_FUNLOCKFILE
147 # undef funlockfile
148 # define funlockfile(x) ((void) 0)
149 #endif
150
151 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW.  */
152 #ifndef EOVERFLOW
153 # define EOVERFLOW E2BIG
154 #endif
155
156 /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
157    NUL-terminate it).  *LINEPTR is a pointer returned from malloc (or
158    NULL), pointing to *N characters of space.  It is realloc'ed as
159    necessary.  Returns the number of characters read (not including
160    the null terminator), or -1 on error or EOF.  */
161
162 int
163 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
164 {
165   int result;
166   size_t cur_len = 0;
167
168   if (lineptr == NULL || n == NULL || fp == NULL)
169     {
170       return -1;
171     }
172
173   flockfile (fp);
174
175   if (*lineptr == NULL || *n == 0)
176     {
177       *n = 120;
178       *lineptr = (char *) realloc (*lineptr, *n);
179       if (*lineptr == NULL)
180         {
181           result = -1;
182           goto unlock_return;
183         }
184     }
185
186   for (;;)
187     {
188       int i;
189
190       i = getc (fp);
191       if (i == EOF)
192         {
193           result = -1;
194           break;
195         }
196
197       /* Make enough space for len+1 (for final NUL) bytes.  */
198       if (cur_len + 1 >= *n)
199         {
200           size_t needed_max =
201             SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
202           size_t needed = 2 * *n + 1;   /* Be generous. */
203           char *new_lineptr;
204
205           if (needed_max < needed)
206             needed = needed_max;
207           if (cur_len + 1 >= needed)
208             {
209               result = -1;
210               goto unlock_return;
211             }
212
213           new_lineptr = (char *) realloc (*lineptr, needed);
214           if (new_lineptr == NULL)
215             {
216               result = -1;
217               goto unlock_return;
218             }
219
220           *lineptr = new_lineptr;
221           *n = needed;
222         }
223
224       (*lineptr)[cur_len] = i;
225       cur_len++;
226
227       if (i == delimiter)
228         break;
229     }
230   (*lineptr)[cur_len] = '\0';
231   result = cur_len ? cur_len : result;
232
233  unlock_return:
234   funlockfile (fp); /* doesn't set errno */
235
236   return result;
237 }
238
239 int
240 getline (char **lineptr, size_t *n, FILE *stream)
241 {
242   return getdelim (lineptr, n, '\n', stream);
243 }
244
245 #if defined(_UNICODE)
246 wchar_t* newSysString(const char *toconvert)
247 {
248         int newstrlen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, toconvert, -1, 0, 0);
249         wchar_t *newstring = g_new(wchar_t,newstrlen);
250         MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, toconvert, -1, newstring, newstrlen) ;
251         return newstring;
252 }
253 #else
254 char * newSysString(const char *toconvert)
255 {
256         return g_strdup(toconvert);
257 }
258 #endif
259 #endif
260
261 unsigned int
262 iso8601_to_secs(char *iso8601)
263 {
264         int a,b,d,val[6],i=0;
265         char *start=iso8601,*pos=iso8601;
266         while (*pos && i < 6) {
267                 if (*pos < '0' || *pos > '9') {
268                         val[i++]=atoi(start);
269                         pos++;
270                         start=pos;
271                 } 
272                 pos++;
273         }
274         
275         a=val[0]/100;
276         b=2-a+a/4;
277
278         if (val[1] < 2) {
279                 val[0]--;
280                 val[1]+=12;
281         }
282
283         d=1461*(val[0]+4716)/4+306001*(val[1]+1)/10000+val[2]+b-2442112;
284
285         return ((d*24+val[3])*60+val[4])*60+val[5];
286 }
287
288 char *
289 current_to_iso8601(void)
290 {
291         char buffer[32];
292         char *timep=NULL;
293 #ifdef HAVE_GLIB
294         GTimeVal time; 
295         g_get_current_time(&time); 
296         timep = g_time_val_to_iso8601(&time);
297 #else
298         time_t tnow;
299         struct tm *tm;
300         tnow = time(0);
301         tm = gmtime(&tnow);
302         if (tm) {
303                 strftime(buffer, sizeof(buffer), "%Y-%m-%dT%TZ", tm);
304                 timep=g_strdup(buffer); 
305         }
306 #endif
307         return timep;
308 }