2
* utils.c - Utility functions
4
* nspluginwrapper (C) 2005-2007 Gwenole Beauchesne
6
* This program 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 of the License, or
9
* (at your option) any later version.
11
* This program 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.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
#include <glib.h> /* <glib/ghash.h> */
41
/* ====================================================================== */
43
/* ====================================================================== */
45
static GHashTable *g_ids;
47
static inline void *id_key(uint32_t id)
49
return (void *)(uintptr_t)id;
54
return (g_ids = g_hash_table_new(NULL, NULL)) != NULL;
60
g_hash_table_destroy(g_ids);
65
void id_link(int id, void *ptr)
67
g_hash_table_insert(g_ids, id_key(id), ptr);
70
int id_create(void *ptr)
77
bool id_remove(int id)
79
return g_hash_table_remove(g_ids, id_key(id));
82
void *id_lookup(int id)
84
return g_hash_table_lookup(g_ids, id_key(id));
87
static gboolean id_match_value(gpointer key, gpointer value, gpointer user_data)
89
if (value == *(gpointer *)user_data) {
90
*(int *)user_data = (uintptr_t)key;
96
int id_lookup_value(void *ptr)
98
return g_hash_table_find(g_ids, id_match_value, &ptr) ? (uintptr_t)ptr : -1;
102
/* ====================================================================== */
103
/* === String expansions === */
104
/* ====================================================================== */
107
const char *string_of_NPError(int error)
111
switch ((NPError)error) {
112
#define _(VAL) case VAL: str = #VAL; break;
114
_(NPERR_GENERIC_ERROR);
115
_(NPERR_INVALID_INSTANCE_ERROR);
116
_(NPERR_INVALID_FUNCTABLE_ERROR);
117
_(NPERR_MODULE_LOAD_FAILED_ERROR);
118
_(NPERR_OUT_OF_MEMORY_ERROR);
119
_(NPERR_INVALID_PLUGIN_ERROR);
120
_(NPERR_INVALID_PLUGIN_DIR_ERROR);
121
_(NPERR_INCOMPATIBLE_VERSION_ERROR);
122
_(NPERR_INVALID_PARAM);
123
_(NPERR_INVALID_URL);
124
_(NPERR_FILE_NOT_FOUND);
126
_(NPERR_STREAM_NOT_SEEKABLE);
129
str = "<unknown error>";
136
const char *string_of_NPReason(int reason)
140
switch ((NPReason)reason) {
141
#define _(VAL) case VAL: str = #VAL; break;
143
_(NPRES_NETWORK_ERR);
147
str = "<unknown reason>";
154
const char *string_of_NPStreamType(int stype)
159
#define _(VAL) case VAL: str = #VAL; break;
166
str = "<unknown stream type>";
175
/* ====================================================================== */
176
/* === Misc utility functions === */
177
/* ====================================================================== */
179
void npw_perror(const char *prefix, int error)
181
if (prefix && *prefix)
182
npw_printf("ERROR: %s: %s\n", prefix, npw_strerror(error));
184
npw_printf("ERROR: %s\n", npw_strerror(error));
187
const char *npw_strerror(int error)
189
if (error > -1100 && error <= -1000) // RPC errors
190
return rpc_strerror(error);
193
case 0: return "No error";
196
return "Unknown error";
200
/* ====================================================================== */
201
/* === Test Program === */
202
/* ====================================================================== */
212
#define N_CELLS_PER_SLOT 8
213
#define N_STRINGS ((2 * N_CELLS_PER_SLOT) + 3)
214
char *strings[N_STRINGS];
217
for (i = 0; i < N_STRINGS; i++) {
219
sprintf(str, "%d", i);
223
fprintf(stderr, "ERROR: failed to allocate ID for '%s'\n", str);
230
id = ids[N_CELLS_PER_SLOT / 2];
232
printf("str(%d) : '%s'\n", id, str);
235
id = ids[N_CELLS_PER_SLOT];
236
if (id_remove(id) < 0) {
237
fprintf(stderr, "ERROR: failed to unlink ID %d\n", id);
240
ids[N_CELLS_PER_SLOT] = 0;
243
for (i = 0; i < N_CELLS_PER_SLOT; i++) {
244
id = ids[N_CELLS_PER_SLOT + i];
245
if (id && id_remove(id) < 0) {
246
fprintf(stderr, "ERROR: failed to unlink ID %d from slot 1\n", id);
249
ids[N_CELLS_PER_SLOT + i] = 0;
252
// basic lookup after slot removal
253
id = ids[2 * N_CELLS_PER_SLOT];
255
printf("str(%d) : '%s'\n", id, str);
257
// check slot 1 was removed and slots 0 & 2 linked together
258
for (i = 0; i < N_STRINGS; i++) {
260
if (id && id_remove(id) < 0) {
261
fprintf(stderr, "ERROR: failed to unlink ID %d for final cleanup\n", id);
266
for (i = 0; i < N_STRINGS; i++)