Statistics
| Revision:

root / logic / trunk / src / mxml / mxml-private.c @ 49

History | View | Annotate | Download (5.7 KB)

1
/*
2
 * "$Id: mxml-private.c 315 2007-11-22 18:01:52Z mike $"
3
 *
4
 * Private functions for Mini-XML, a small XML-like file parsing library.
5
 *
6
 * Copyright 2003-2007 by Michael Sweet.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Library General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * Contents:
19
 *
20
 *   mxml_error()      - Display an error message.
21
 *   mxml_integer_cb() - Default callback for integer values.
22
 *   mxml_opaque_cb()  - Default callback for opaque values.
23
 *   mxml_real_cb()    - Default callback for real number values.
24
 *   _mxml_global()    - Get global data.
25
 */
26

    
27
/*
28
 * Include necessary headers...
29
 */
30

    
31
#include "mxml-private.h"
32

    
33

    
34
/*
35
 * 'mxml_error()' - Display an error message.
36
 */
37

    
38
void
39
mxml_error(const char *format,                /* I - Printf-style format string */
40
           ...)                                /* I - Additional arguments as needed */
41
{
42
  va_list        ap;                        /* Pointer to arguments */
43
  char                s[1024];                /* Message string */
44
  _mxml_global_t *global = _mxml_global();
45
                                        /* Global data */
46

    
47

    
48
 /*
49
  * Range check input...
50
  */
51

    
52
  if (!format)
53
    return;
54

    
55
 /*
56
  * Format the error message string...
57
  */
58

    
59
  va_start(ap, format);
60

    
61
  vsnprintf(s, sizeof(s), format, ap);
62

    
63
  va_end(ap);
64

    
65
 /*
66
  * And then display the error message...
67
  */
68

    
69
  if (global->error_cb)
70
    (*global->error_cb)(s);
71
  else
72
    fprintf(stderr, "mxml: %s\n", s);
73
}
74

    
75

    
76
/*
77
 * 'mxml_ignore_cb()' - Default callback for ignored values.
78
 */
79

    
80
mxml_type_t                                /* O - Node type */
81
mxml_ignore_cb(mxml_node_t *node)        /* I - Current node */
82
{
83
  (void)node;
84

    
85
  return (MXML_IGNORE);
86
}
87

    
88

    
89
/*
90
 * 'mxml_integer_cb()' - Default callback for integer values.
91
 */
92

    
93
mxml_type_t                                /* O - Node type */
94
mxml_integer_cb(mxml_node_t *node)        /* I - Current node */
95
{
96
  (void)node;
97

    
98
  return (MXML_INTEGER);
99
}
100

    
101

    
102
/*
103
 * 'mxml_opaque_cb()' - Default callback for opaque values.
104
 */
105

    
106
mxml_type_t                                /* O - Node type */
107
mxml_opaque_cb(mxml_node_t *node)        /* I - Current node */
108
{
109
  (void)node;
110

    
111
  return (MXML_OPAQUE);
112
}
113

    
114

    
115
/*
116
 * 'mxml_real_cb()' - Default callback for real number values.
117
 */
118

    
119
mxml_type_t                                /* O - Node type */
120
mxml_real_cb(mxml_node_t *node)                /* I - Current node */
121
{
122
  (void)node;
123

    
124
  return (MXML_REAL);
125
}
126

    
127

    
128
#ifdef HAVE_PTHREAD_H                        /**** POSIX threading ****/
129
#  include <pthread.h>
130

    
131
static pthread_key_t        _mxml_key = -1;        /* Thread local storage key */
132
static pthread_once_t        _mxml_key_once = PTHREAD_ONCE_INIT;
133
                                        /* One-time initialization object */
134
static void                _mxml_init(void);
135
static void                _mxml_destructor(void *g);
136

    
137

    
138
/*
139
 * '_mxml_global()' - Get global data.
140
 */
141

    
142
_mxml_global_t *                        /* O - Global data */
143
_mxml_global(void)
144
{
145
  _mxml_global_t        *global;        /* Global data */
146

    
147

    
148
  pthread_once(&_mxml_key_once, _mxml_init);
149

    
150
  if ((global = (_mxml_global_t *)pthread_getspecific(_mxml_key)) == NULL)
151
  {
152
    global = (_mxml_global_t *)calloc(1, sizeof(_mxml_global_t));
153
    pthread_setspecific(_mxml_key, global);
154

    
155
    global->num_entity_cbs = 1;
156
    global->entity_cbs[0]  = _mxml_entity_cb;
157
    global->wrap           = 72;
158
  }
159

    
160
  return (global);
161
}
162

    
163

    
164
/*
165
 * '_mxml_init()' - Initialize global data...
166
 */
167

    
168
static void
169
_mxml_init(void)
170
{
171
  pthread_key_create(&_mxml_key, _mxml_destructor);
172
}
173

    
174

    
175
/*
176
 * '_mxml_destructor()' - Free memory used for globals...
177
 */
178

    
179
static void
180
_mxml_destructor(void *g)                /* I - Global data */
181
{
182
  free(g);
183
}
184

    
185

    
186
#elif defined(WIN32)                        /**** WIN32 threading ****/
187
#  include <windows.h>
188

    
189
static DWORD _mxml_tls_index;                /* Index for global storage */
190

    
191

    
192
/*
193
 * 'DllMain()' - Main entry for library.
194
 */
195
 
196
BOOL WINAPI                                /* O - Success/failure */
197
DllMain(HINSTANCE hinst,                /* I - DLL module handle */
198
        DWORD     reason,                /* I - Reason */
199
        LPVOID    reserved)                /* I - Unused */
200
{
201
  _mxml_global_t        *global;        /* Global data */
202

    
203

    
204
  (void)hinst;
205
  (void)reserved;
206

    
207
  switch (reason) 
208
  { 
209
    case DLL_PROCESS_ATTACH :                /* Called on library initialization */
210
        if ((_mxml_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES) 
211
          return (FALSE); 
212
        break; 
213

    
214
    case DLL_THREAD_DETACH :                /* Called when a thread terminates */
215
        if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) != NULL)
216
          free(global);
217
        break; 
218

    
219
    case DLL_PROCESS_DETACH :                /* Called when library is unloaded */
220
        if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) != NULL)
221
          free(global);
222

    
223
        TlsFree(_mxml_tls_index); 
224
        break; 
225

    
226
    default: 
227
        break; 
228
  } 
229

    
230
  return (TRUE);
231
}
232

    
233

    
234
/*
235
 * '_mxml_global()' - Get global data.
236
 */
237

    
238
_mxml_global_t *                        /* O - Global data */
239
_mxml_global(void)
240
{
241
  _mxml_global_t        *global;        /* Global data */
242

    
243

    
244
  if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) == NULL)
245
  {
246
    global = (_mxml_global_t *)calloc(1, sizeof(_mxml_global_t));
247

    
248
    global->num_entity_cbs = 1;
249
    global->entity_cbs[0]  = _mxml_entity_cb;
250
    global->wrap           = 72;
251

    
252
    TlsSetValue(_mxml_tls_index, (LPVOID)global); 
253
  }
254

    
255
  return (global);
256
}
257

    
258

    
259
#else                                        /**** No threading ****/
260
/*
261
 * '_mxml_global()' - Get global data.
262
 */
263

    
264
_mxml_global_t *                        /* O - Global data */
265
_mxml_global(void)
266
{
267
  static _mxml_global_t        global =        /* Global data */
268
  {
269
    NULL,                                /* error_cb */
270
    1,                                        /* num_entity_cbs */
271
    { _mxml_entity_cb },                /* entity_cbs */
272
    72,                                        /* wrap */
273
    NULL,                                /* custom_load_cb */
274
    NULL                                /* custom_save_cb */
275
  };
276

    
277

    
278
  return (&global);
279
}
280
#endif /* HAVE_PTHREAD_H */
281

    
282

    
283
/*
284
 * End of "$Id: mxml-private.c 315 2007-11-22 18:01:52Z mike $".
285
 */