~ubuntu-branches/ubuntu/lucid/nspluginwrapper/lucid

« back to all changes in this revision

Viewing changes to src/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Rob Andrews
  • Date: 2007-05-10 12:17:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070510121709-xb8fx1cmr6kae2ba
Tags: upstream-0.9.91.4
ImportĀ upstreamĀ versionĀ 0.9.91.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  utils.c - Utility functions
 
3
 *
 
4
 *  nspluginwrapper (C) 2005-2007 Gwenole Beauchesne
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 */
 
20
 
 
21
#include "sysdeps.h"
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <stdarg.h>
 
26
#include <glib.h> /* <glib/ghash.h> */
 
27
 
 
28
#include "utils.h"
 
29
#include "rpc.h"
 
30
 
 
31
#ifndef TEST_UTILS
 
32
#define XP_UNIX 1
 
33
#define MOZ_X11 1
 
34
#include <npapi.h>
 
35
#endif
 
36
 
 
37
#define DEBUG 1
 
38
#include "debug.h"
 
39
 
 
40
 
 
41
/* ====================================================================== */
 
42
/* === Hashes                                                         === */
 
43
/* ====================================================================== */
 
44
 
 
45
static GHashTable *g_ids;
 
46
 
 
47
static inline void *id_key(uint32_t id)
 
48
{
 
49
  return (void *)(uintptr_t)id;
 
50
}
 
51
 
 
52
bool id_init(void)
 
53
{
 
54
  return (g_ids = g_hash_table_new(NULL, NULL)) != NULL;
 
55
}
 
56
 
 
57
void id_kill(void)
 
58
{
 
59
  if (g_ids) {
 
60
        g_hash_table_destroy(g_ids);
 
61
        g_ids = NULL;
 
62
  }
 
63
}
 
64
 
 
65
void id_link(int id, void *ptr)
 
66
{
 
67
  g_hash_table_insert(g_ids, id_key(id), ptr);
 
68
}
 
69
 
 
70
int id_create(void *ptr)
 
71
{
 
72
  static int id = 0;
 
73
  id_link(++id, ptr);
 
74
  return id;
 
75
}
 
76
 
 
77
bool id_remove(int id)
 
78
{
 
79
  return g_hash_table_remove(g_ids, id_key(id));
 
80
}
 
81
 
 
82
void *id_lookup(int id)
 
83
{
 
84
  return g_hash_table_lookup(g_ids, id_key(id));
 
85
}
 
86
 
 
87
static gboolean id_match_value(gpointer key, gpointer value, gpointer user_data)
 
88
{
 
89
  if (value == *(gpointer *)user_data) {
 
90
        *(int *)user_data = (uintptr_t)key;
 
91
        return true;
 
92
  }
 
93
  return false;
 
94
}
 
95
 
 
96
int id_lookup_value(void *ptr)
 
97
{
 
98
  return g_hash_table_find(g_ids, id_match_value, &ptr) ? (uintptr_t)ptr : -1;
 
99
}
 
100
 
 
101
 
 
102
/* ====================================================================== */
 
103
/* === String expansions                                              === */
 
104
/* ====================================================================== */
 
105
 
 
106
#ifndef TEST_UTILS
 
107
const char *string_of_NPError(int error)
 
108
{
 
109
  const char *str;
 
110
 
 
111
  switch ((NPError)error) {
 
112
#define _(VAL) case VAL: str = #VAL; break;
 
113
        _(NPERR_NO_ERROR);
 
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);
 
125
        _(NPERR_NO_DATA);
 
126
        _(NPERR_STREAM_NOT_SEEKABLE);
 
127
#undef _
 
128
  default:
 
129
        str = "<unknown error>";
 
130
        break;
 
131
  }
 
132
 
 
133
  return str;
 
134
}
 
135
 
 
136
const char *string_of_NPReason(int reason)
 
137
{
 
138
  const char *str;
 
139
 
 
140
  switch ((NPReason)reason) {
 
141
#define _(VAL) case VAL: str = #VAL; break;
 
142
    _(NPRES_DONE);
 
143
    _(NPRES_NETWORK_ERR);
 
144
    _(NPRES_USER_BREAK);
 
145
#undef _
 
146
  default:
 
147
        str = "<unknown reason>";
 
148
        break;
 
149
  }
 
150
 
 
151
  return str;
 
152
}
 
153
 
 
154
const char *string_of_NPStreamType(int stype)
 
155
{
 
156
  const char *str;
 
157
 
 
158
  switch (stype) {
 
159
#define _(VAL) case VAL: str = #VAL; break;
 
160
    _(NP_NORMAL);
 
161
    _(NP_SEEK);
 
162
    _(NP_ASFILE);
 
163
    _(NP_ASFILEONLY);
 
164
#undef _
 
165
  default:
 
166
        str = "<unknown stream type>";
 
167
        break;
 
168
  }
 
169
 
 
170
  return str;
 
171
}
 
172
#endif
 
173
 
 
174
 
 
175
/* ====================================================================== */
 
176
/* === Misc utility functions                                         === */
 
177
/* ====================================================================== */
 
178
 
 
179
void npw_perror(const char *prefix, int error)
 
180
{
 
181
  if (prefix && *prefix)
 
182
        npw_printf("ERROR: %s: %s\n", prefix, npw_strerror(error));
 
183
  else
 
184
        npw_printf("ERROR: %s\n", npw_strerror(error));
 
185
}
 
186
 
 
187
const char *npw_strerror(int error)
 
188
{
 
189
  if (error > -1100 && error <= -1000)  // RPC errors
 
190
        return rpc_strerror(error);
 
191
 
 
192
  switch (error) {
 
193
  case 0:       return "No error";
 
194
  }
 
195
 
 
196
  return "Unknown error";
 
197
}
 
198
 
 
199
 
 
200
/* ====================================================================== */
 
201
/* === Test Program                                                   === */
 
202
/* ====================================================================== */
 
203
 
 
204
#ifdef TEST_UTILS
 
205
int main(void)
 
206
{
 
207
  char *str;
 
208
  int i, id;
 
209
 
 
210
  id_init();
 
211
 
 
212
#define N_CELLS_PER_SLOT 8
 
213
#define N_STRINGS ((2 * N_CELLS_PER_SLOT) + 3)
 
214
  char *strings[N_STRINGS];
 
215
  int ids[N_STRINGS];
 
216
 
 
217
  for (i = 0; i < N_STRINGS; i++) {
 
218
        str = malloc(10);
 
219
        sprintf(str, "%d", i);
 
220
        strings[i] = str;
 
221
        id = id_create(str);
 
222
        if (id < 0) {
 
223
          fprintf(stderr, "ERROR: failed to allocate ID for '%s'\n", str);
 
224
          return 1;
 
225
        }
 
226
        ids[i] = id;
 
227
  }
 
228
 
 
229
  // basic lookup
 
230
  id = ids[N_CELLS_PER_SLOT / 2];
 
231
  str = id_lookup(id);
 
232
  printf("str(%d) : '%s'\n", id, str);
 
233
 
 
234
  // basic unlink
 
235
  id = ids[N_CELLS_PER_SLOT];
 
236
  if (id_remove(id) < 0) {
 
237
        fprintf(stderr, "ERROR: failed to unlink ID %d\n", id);
 
238
        return 1;
 
239
  }
 
240
  ids[N_CELLS_PER_SLOT] = 0;
 
241
 
 
242
  // remove slot 1
 
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);
 
247
          return 1;
 
248
        }
 
249
        ids[N_CELLS_PER_SLOT + i] = 0;
 
250
  }
 
251
 
 
252
  // basic lookup after slot removal
 
253
  id = ids[2 * N_CELLS_PER_SLOT];
 
254
  str = id_lookup(id);
 
255
  printf("str(%d) : '%s'\n", id, str);
 
256
 
 
257
  // check slot 1 was removed and slots 0 & 2 linked together
 
258
  for (i = 0; i < N_STRINGS; i++) {
 
259
        id = ids[i];
 
260
        if (id && id_remove(id) < 0) {
 
261
          fprintf(stderr, "ERROR: failed to unlink ID %d for final cleanup\n", id);
 
262
          return 1;
 
263
        }
 
264
  }
 
265
 
 
266
  for (i = 0; i < N_STRINGS; i++)
 
267
        free(strings[i]);
 
268
 
 
269
  id_kill();
 
270
  return 0;
 
271
}
 
272
#endif