~alberto-cuda/pdflow/win32-port

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * PDFlow -- status.c
 *
 *  Copyright (c) 2010 Alberto Cuda
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 *  USA.
 */
/*
 * Status routines. We also implement here the two basic subsystem
 * (i.e. common and os) 
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "status.h"

#if defined(_WIN32) || defined(WIN32)
#include <windows.h>
#endif

#define MAX_PHRASE 1024
#define HISTORY    20

static const char *cind_get_string (enum s_code code);
static const char *cs_get_string (enum s_code code);
static const char *os_get_string (enum s_code code);
#if defined(_WIN32) || defined(WIN32)
static const char *win32_get_string (enum s_code code);
#endif

static struct s_system_ifc *ifc [S_SYS__LAST__];

static struct s_system_ifc indirect_ifc = {
	"Common-Indirect", S_SYS_INDIRECT, cind_get_string
};

static struct s_system_ifc common_ifc = {
	"Common", S_SYS_COMMON,	cs_get_string
};

static struct s_system_ifc os_ifc = {
	"OS", S_SYS_OS,	os_get_string
};

#if defined(_WIN32) || defined(WIN32)
static struct s_system_ifc win32_ifc = {
	"Win32", S_SYS_WIN32, win32_get_string
};
#endif

struct ind_data {
	int idx;
	struct {
		enum s_status major;
		char phrase [MAX_PHRASE];
	} code [HISTORY];
};

static struct ind_data indd;

/* Return string associated to a given status code */
const char *s_get_string (enum s_status status)
{
	enum s_system sys;
	enum s_code code;

	sys = S_SYS (status);
	code = S_CODE (status);

	if (sys < 0 || sys >= S_SYS__LAST__)
		return "(Unknown subsystem code)";

	if (ifc [sys] == NULL)
		return "(Unregistered subsystem code)";

	return ifc [sys] -> get_string (code);
}

enum s_status s_get_code (enum s_status status)
{
	return S_SYS (status) == S_SYS_INDIRECT ?
		indd.code [S_CODE (status)].major : status;
}


/* Indirect subsystem */
enum s_status s_make_indirect (enum s_status major, const char *format, ...)
{
	va_list ap;
	int wr, idx;

	idx = indd.idx;
	indd.idx = (indd.idx + 1) % HISTORY;

	va_start (ap, format);
	wr = vsnprintf (indd.code [idx].phrase, MAX_PHRASE, format, ap);
	if (wr >= MAX_PHRASE || wr < 0) /* Old glibs return -1 when trunc */
		strcpy (indd.code [idx].phrase + MAX_PHRASE - 4, "...");

	va_end (ap);

	indd.code [idx].major = major;

	return S_STATUS (S_SYS_INDIRECT, idx);
}

static const char *cind_get_string (enum s_code code)
{
	return indd.code [code].phrase;
}

/* Common subsystem */
static const char *cs_get_string (enum s_code code)
{
	switch (code) {
	case S_CCC_OK:
		return "No error";

	case S_CCC_OUT_OF_MEMORY:
		return "Out of Memory";

	case S_CCC_INTERNAL_ERROR:
		return "Internal error";

	case S_CCC_PARSE_ERROR:
		return "Parse error";

	case S_CCC_NOT_SUPPORTED:
		return "Not supported";

	case S_CCC_USER_CANCEL:
		return "User cancel";

		/* Registry */

	case S_CCC_REGISTRY_FULL:
		return "Registry full";

	case S_CCC_ENTRY_NOT_FOUND:
		return "Entry not found";

	case S_CCC_ENTRY_ALREADY_PRESENT:
		return "Entry already present";

	case S_CCC_BAD_ENTRY:
		return "Bad entry";
	}

	return "Unknown common code";
}

/* OS substem */
static const char *os_get_string (enum s_code code)
{
	return strerror (code);
}


#if defined(_WIN32) || defined(WIN32)
static const char *win32_get_string (enum s_code code)
{
	static char buf [1024];

	if (!FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0,
			    buf, sizeof (buf), NULL))
		snprintf (buf, sizeof (buf), 
			  "Unknown error message: %x", code);

	return buf;
}
#endif

/* Status registry */
enum s_status s_register (struct s_system_ifc *sys)
{
	if (sys -> sys < 0 || sys -> sys >= S_SYS__LAST__)
		return S_CSC_BAD_ENTRY;

	if (ifc [sys -> sys] != NULL)
		return S_CSC_ENTRY_ALREADY_PRESENT;

	ifc [sys -> sys] = sys;

	return S_CSC_OK;
}

enum s_status s_unregister (struct s_system_ifc *sys)
{
	if (sys -> sys < 0 || sys -> sys >= S_SYS__LAST__)
		return S_CSC_BAD_ENTRY;

	if (ifc [sys -> sys] != sys)
		return S_CSC_ENTRY_NOT_FOUND;

	ifc [sys -> sys] = NULL;

	return S_CSC_OK;
}

/* Init and exit */
enum s_status s_init (void)
{
	s_register (&indirect_ifc);
	s_register (&common_ifc);
	s_register (&os_ifc);
#if defined(_WIN32) || defined(WIN32)
	s_register (&win32_ifc);
#endif

	return S_CSC_OK;
}

void s_cleanup (void)
{
	s_unregister (&common_ifc);
	s_unregister (&os_ifc);
#if defined(_WIN32) || defined(WIN32)
	s_unregister (&win32_ifc);
#endif
}