1 /*
2    Copyright (C) 2003-2004 Borut Razem
3 
4 This file is part of gpsim.
5 
6 gpsim is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 gpsim is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with gpsim; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20 
21 /*
22 Minimal implementation of glib:
23 only slist functionality and g_win32_error_message
24 is needed by gpsim with --disable-gui
25 */
26 
27 #include "glib.h"
28 #include <windows.h>
29 
30 gpointer
g_malloc0(gulong n_bytes)31 g_malloc0 (gulong n_bytes)
32 {
33   if (n_bytes)
34     {
35       return calloc (1, n_bytes);
36     }
37 
38   return NULL;
39 }
40 
41 
42 void
g_free(gpointer mem)43 g_free (gpointer mem)
44 {
45   if (mem)
46     free (mem);
47 }
48 
49 
50 #define _g_slist_alloc g_slist_alloc
51 GSList*
g_slist_alloc(void)52 g_slist_alloc (void)
53 {
54   GSList *list;
55 
56   list = g_new0 (GSList, 1);
57 
58   return list;
59 }
60 
61 
62 void
g_slist_free_1(GSList * list)63 g_slist_free_1 (GSList *list)
64 {
65   g_free (list);
66 }
67 
68 
69 GSList*
g_slist_append(GSList * list,gpointer data)70 g_slist_append (GSList   *list,
71                 gpointer  data)
72 {
73   GSList *new_list;
74   GSList *last;
75 
76   new_list = _g_slist_alloc ();
77   new_list->data = data;
78 
79   if (list)
80     {
81       last = g_slist_last (list);
82       /* g_assert (last != NULL); */
83       last->next = new_list;
84 
85       return list;
86     }
87   else
88       return new_list;
89 }
90 
91 
92 GSList*
g_slist_remove(GSList * list,gconstpointer data)93 g_slist_remove (GSList        *list,
94                 gconstpointer  data)
95 {
96   GSList *tmp, *prev = NULL;
97 
98   tmp = list;
99   while (tmp)
100     {
101       if (tmp->data == data)
102         {
103           if (prev)
104             prev->next = tmp->next;
105           else
106             list = tmp->next;
107 
108           g_slist_free_1 (tmp);
109           break;
110         }
111       prev = tmp;
112       tmp = prev->next;
113     }
114 
115   return list;
116 }
117 
118 
119 GSList*
g_slist_last(GSList * list)120 g_slist_last (GSList *list)
121 {
122   if (list)
123     {
124       while (list->next)
125         list = list->next;
126     }
127 
128   return list;
129 }
130 
131 
132 gchar *
g_win32_error_message(gint error)133 g_win32_error_message (gint error)
134 {
135   gchar *msg;
136   gchar *retval;
137   int nbytes;
138 
139   FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
140                  |FORMAT_MESSAGE_IGNORE_INSERTS
141                  |FORMAT_MESSAGE_FROM_SYSTEM,
142                  NULL, error, 0,
143                  (LPTSTR) &msg, 0, NULL);
144   nbytes = strlen (msg);
145 
146   if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
147     msg[nbytes-2] = '\0';
148 
149   retval = strdup (msg);
150 
151   if (msg != NULL)
152     LocalFree (msg);
153 
154   return retval;
155 }
156