Subversion Repositories public

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
136 andreas 1
/***************************************************************************
2
 *   Copyright (C) 2007, 2008 by Andreas Theofilu                          *
3
 *   andreas@theosys.at                                                    *
4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   it under the terms of the GNU General Public License as published by  *
7
 *   the Free Software Foundation version 3 of the License.                *
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.,                                       *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 ***************************************************************************/
19
 
20
#include "garmin.h"
21
#include <stdio.h>
22
#include <string.h>
23
 
24
struct GARMIN_ERROR
25
{
26
	int idx;
27
	err_type type;
28
	char *gerr;
29
	struct GARMIN_ERROR *next;
30
} GARMIN_ERROR;
31
 
32
static struct GARMIN_ERROR *gerr = NULL;
33
 
34
void garmin_queue_error(char *str, err_type type)
35
{
36
int idx = 0;
37
char *p, *s;
38
 
39
	if (!str)
40
	   return;
41
 
42
	switch (type)
43
	{
44
	   case err_info:
45
	      p = strdup("Information");
46
	   break;
47
 
48
	   case err_warning:
49
	      p = strdup("Warning");
50
	   break;
51
 
52
	   case err_error:
53
	      p = strdup("Error");
54
	   break;
55
 
56
	   case err_fatal:
57
	      p = strdup("Fatal error");
58
	   break;
59
 
60
	   default:
61
	      p = strdup("Unknown");
62
	}
63
 
64
	if ((s = (char *)malloc(strlen(p) + strlen(str) + 10)) == NULL)
65
	{
66
	   if (p)
67
	      free(p);
68
 
69
	   fprintf(stderr, "Not enough memory!\n");
70
	   return;
71
	}
72
 
73
	sprintf(s, "%s:\n%s", p, str);
74
	free(p);
75
 
76
	if (!gerr)
77
	{
78
	   if ((gerr = (struct GARMIN_ERROR *)malloc(sizeof(struct GARMIN_ERROR))) == NULL)
79
	   {
80
	      fprintf(stderr, "Not enough memory!\n");
81
	      return;
82
	   }
83
 
84
	   gerr->idx = idx;
85
	   gerr->type = type;
86
	   gerr->gerr = s;
87
	   gerr->next = NULL;
88
	}
89
	else
90
	{
91
	   struct GARMIN_ERROR *akt, *n;
92
 
93
	   if ((n = (struct GARMIN_ERROR *)malloc(sizeof(struct GARMIN_ERROR))) == NULL)
94
	   {
95
	      fprintf(stderr, "Not enough memory!\n");
96
	      return;
97
	   }
98
 
99
	   /* Find last error message */
100
	   akt = gerr;
101
 
102
	   while (akt->next)
103
	   {
104
	      akt = akt->next;
105
	      idx++;
106
	   }
107
 
108
	   idx++;
109
	   n->idx = idx;
110
	   n->type = type;
111
	   n->gerr = s;
112
	   n->next = NULL;
113
	   akt->next = n;
114
	}
115
}
116
 
117
void garmin_clear_errors()
118
{
119
struct GARMIN_ERROR *akt, *old;
120
 
121
	akt = gerr;
122
 
123
	while (akt)
124
	{
125
	   old = akt;
126
	   akt = akt->next;
127
 
128
	   if (old->gerr)
129
	      free(old->gerr);
130
 
131
	   free(old);
132
	}
133
 
134
	gerr = NULL;
135
}
136
 
137
char *garmin_get_first_error(int *key)
138
{
139
	if (key)
140
	   *key = gerr->idx;
141
 
142
	return gerr->gerr;
143
}
144
 
145
char *garmin_get_next_error(int *key)
146
{
147
struct GARMIN_ERROR *akt;
148
 
149
	if (!key)
150
	   return NULL;
151
 
152
	if (*key == -1)
153
	   return garmin_get_first_error(key);
154
 
155
	akt = gerr;
156
 
157
	while (akt)
158
	{
159
	   if (*key == akt->idx && akt->next)
160
	   {
161
	      *key = akt->next->idx;
162
	      return akt->next->gerr;
163
	   }
164
 
165
	   akt = akt->next;
166
	}
167
 
168
	return NULL;
169
}
170
 
171
int garmin_count_error()
172
{
173
struct GARMIN_ERROR *akt = gerr;
174
int idx = 0;
175
 
176
	while (akt)
177
	{
178
	   idx++;
179
	   akt = akt->next;
180
	}
181
 
182
	return idx;
183
}