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