expat_stubs.c 11.8 KB
Newer Older
1
2
3
4
5
6
7
/***********************************************************************/
/* The OcamlExpat library                                              */
/*                                                                     */
/* Copyright 2002, 2003 Maas-Maarten Zeeman. All rights reserved. See  */ 
/* LICENCE for details.                                                */
/***********************************************************************/

8
/* $Id: expat_stubs.c,v 1.2 2003/06/03 20:36:11 cvscast Exp $ */
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

/* Stub code to interface Ocaml with Expat */

#include <stdio.h>
#include <string.h>
#include <expat.h>

#include <caml/mlvalues.h>
#include <caml/custom.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/callback.h>
#include <caml/fail.h>

#define XML_Parser_val(v) (*((XML_Parser *) Data_custom_val(v)))

/* Define the place where the handlers will be located inside the
 * handler tuple which is registered as global root. Handlers for
 * new functions should go here.
 */
#define NUM_HANDLERS 5
enum expat_handler {
  EXPAT_START_ELEMENT_HANDLER,
  EXPAT_END_ELEMENT_HANDLER,
  EXPAT_CHARACTER_DATA_HANDLER,
  EXPAT_PROCESSING_INSTRUCTION_HANDLER,
  EXPAT_COMMENT_HANDLER
};

static void 
xml_parser_finalize(value parser) 
{ 
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  remove_global_root(handlers);
  stat_free(handlers);

  XML_ParserFree(xml_parser);  
}

static int 
xml_parser_compare(value v1, value v2) 
{ 
  XML_Parser p1 = XML_Parser_val(v1);
  XML_Parser p2 = XML_Parser_val(v2);
  if(p1 == p2) return 0;
  if(p1 < p2) return -1; 
  return 1;
}

static long 
xml_parser_hash(value v) 
{ 
  return (long) XML_Parser_val(v);
}

static struct custom_operations xml_parser_ops = {
  "Expat_XML_Parser",
  xml_parser_finalize,
  xml_parser_compare,
  xml_parser_hash,
  custom_serialize_default,
  custom_deserialize_default    
};

static XML_Parser
parser_create(char *encoding)
{
  if(encoding[0] == '\0') 
    return XML_ParserCreate(NULL);
  return XML_ParserCreate(encoding);
}

static XML_Parser
parser_create_ns(char *encoding, char sep)
{
  if(encoding[0] == '\0')
    return XML_ParserCreateNS(NULL, sep);
  return XML_ParserCreateNS(encoding, sep);
}

static value
create_ocaml_expat_parser(XML_Parser xml_parser)
{
  CAMLparam0();
  CAMLlocal1(parser);
  int i;
  value *handlers;

  /* I don't know how to find out how much memory the parser consumes, so
   * I've set some figures here, which seems to do well.
   */
  parser = alloc_custom(&xml_parser_ops, sizeof(XML_Parser), 1, 5000);
  Store_field(parser, 1, Val_bp(xml_parser));
    
  /* Malloc a value for a tuple which will contain the callback
   * handlers and register it as global root. 
   */
  handlers = stat_alloc(sizeof *handlers);
109
  *handlers = Val_unit;
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
  register_global_root(handlers);
  *handlers = alloc_tuple(NUM_HANDLERS);
  for(i = 0; i < NUM_HANDLERS; i++) {
    Field(*handlers, i) = Val_unit;
  }

  /* Associate it as user data with the parser. This is possible because 
   * a global root will not be relocated. 
   */
  XML_SetUserData(xml_parser, handlers);
  
  CAMLreturn (parser);
}

CAMLprim value 
expat_XML_ParserCreate(value encoding)
{
  return create_ocaml_expat_parser(parser_create(String_val(encoding)));
}

CAMLprim value
expat_XML_ParserCreateNS(value encoding, value sep)
{ 
  return create_ocaml_expat_parser(parser_create_ns(String_val(encoding),
						    (char) Long_val(sep)));
}

CAMLprim value
expat_XML_GetCurrentByteIndex(value parser)
{
  return Val_long(XML_GetCurrentByteIndex(XML_Parser_val(parser))); 
}

/*
 * Return the current byte count
 */
CAMLprim value
expat_XML_GetCurrentByteCount(value parser)
{
  return Val_long(XML_GetCurrentByteCount(XML_Parser_val(parser))); 
}

/* 
 * Return the current column number
 */
CAMLprim value
expat_XML_GetCurrentColumnNumber(value parser)
{
  return Val_long(XML_GetCurrentColumnNumber(XML_Parser_val(parser)));
}

/* 
 * Returns the current line number
 */
CAMLprim value
expat_XML_GetCurrentLineNumber(value parser)
{
  return Val_long(XML_GetCurrentLineNumber(XML_Parser_val(parser)));
}

/*
 * Return the expat version number as a string
 */
value
expat_XML_ExpatVersion(value unit)
{
  return copy_string(XML_ExpatVersion());
}

/*
 * Return a string with the expat error message.
 */
CAMLprim value
expat_XML_ErrorString(value error_code)
{
  CAMLparam1(error_code);
  const char *error_string = XML_ErrorString(Int_val(error_code));

  /* XML_ErrorString(XML_ERROR_NONE) returns NULL, this check
   * will return an empty string whenever this happens. Note:
   * it checks for NULL, because that is the safest way.
   */
  if (error_string == NULL) 
    CAMLreturn (alloc_string(0));
  CAMLreturn (copy_string(error_string));
}

/* 
 * Raise an expat_error exception
 */
static void
expat_error(int error_code)
{
  static value * expat_error_exn = NULL;

  if(expat_error_exn == NULL) {
    expat_error_exn = caml_named_value("expat_error");
    if(expat_error_exn == NULL) {
      invalid_argument("Exception Expat_error not initialized");
    }
  }

  raise_with_arg(*expat_error_exn, Val_long(error_code));
}

/*
 * The parse call
 */
CAMLprim value
expat_XML_Parse(value parser, value string)
{
  CAMLparam2(parser, string);
  XML_Parser xml_parser =  XML_Parser_val(parser);

  if(!XML_Parse(xml_parser, String_val(string), string_length(string), 0)) {
    expat_error(XML_GetErrorCode(xml_parser));
  }
  
  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_Parse_sub(value parser, value string, value pos, value len)
{
  CAMLparam2(parser, string);
  XML_Parser xml_parser =  XML_Parser_val(parser);

  if(!XML_Parse(xml_parser, String_val(string)+Int_val(pos), Int_val(len), 0)) {
    expat_error(XML_GetErrorCode(xml_parser));
  }
  
  CAMLreturn (Val_unit);
}

/*
 * The final call
 */
CAMLprim value
expat_XML_Final(value parser)
{
  CAMLparam1(parser);
  XML_Parser xml_parser =  XML_Parser_val(parser);

  if(!XML_Parse(xml_parser, NULL, 0, 1)) {
    expat_error(XML_GetErrorCode(xml_parser));
  }
  
  CAMLreturn (Val_unit);
}


/*
 * Start element handling, setting and resetting.
 */
static void 
start_element_handler(void *user_data, const char *name, const char **attr)
{
  CAMLparam0();
  CAMLlocal5(list, cons, prev, att, tag);
  value *handlers = user_data;
  int i;

  list = Val_unit;
  prev = Val_unit;

  /* Create an assoc list with the attributes */
  for(i = 0; attr[i]; i += 2) {
    /* Create a tuple */
    att = alloc_tuple(2);
    Store_field(att, 0, copy_string(attr[i]));
    Store_field(att, 1, copy_string(attr[i + 1]));
    
    /* Create a cons */
    cons = alloc_tuple(2);
    Store_field(cons, 0, att);
    Store_field(cons, 1, Val_unit);
    if(prev != Val_unit) {
      Store_field(prev, 1, cons);      
    } 
    prev = cons;
    if(list == Val_unit) {
      list = cons;
    }
  }
  tag = copy_string(name);
  callback2(Field(*handlers, EXPAT_START_ELEMENT_HANDLER), tag, list);
  
  CAMLreturn0;
}

static value set_start_handler(value parser,
			       XML_StartElementHandler c_handler,
			       value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_START_ELEMENT_HANDLER, ocaml_handler);
  XML_SetStartElementHandler(xml_parser, c_handler); 

  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetStartElementHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_start_handler(parser, start_element_handler, handler));
}

CAMLprim value
expat_XML_ResetStartElementHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_start_handler(parser, NULL, Val_unit));
}

/*
 * End element handling, setting and resetting.
 */
static void
end_element_handler(void *user_data, const char *name)
{
  value tag;
  value *handlers = user_data;

  tag = copy_string(name);
  callback(Field(*handlers, EXPAT_END_ELEMENT_HANDLER), tag);
}

static value 
set_end_handler(value parser, XML_EndElementHandler c_handler, 
		value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_END_ELEMENT_HANDLER, ocaml_handler);
  XML_SetEndElementHandler(xml_parser, c_handler); 

  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetEndElementHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_end_handler(parser, end_element_handler, handler));
}

CAMLprim value
expat_XML_ResetEndElementHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_end_handler(parser, NULL, Val_unit));
}

/*
 * Character data handling, setting, and resetting
 */
static void
character_data_handler(void *user_data, const char *data, int len)
{
  CAMLparam0();
  CAMLlocal1(str);
  value *handlers = user_data;

  str = alloc_string(len);
  memcpy(String_val(str), data, len);
  callback(Field(*handlers, EXPAT_CHARACTER_DATA_HANDLER), str);

  CAMLreturn0;
}

static value set_character_data_handler(value parser,
					XML_CharacterDataHandler c_handler,
					value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_CHARACTER_DATA_HANDLER, ocaml_handler);
  XML_SetCharacterDataHandler(xml_parser, c_handler);

  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetCharacterDataHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_character_data_handler(parser, character_data_handler, 
					 handler));
}

CAMLprim value
expat_XML_ResetCharacterDataHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_character_data_handler(parser, NULL, Val_unit));
}

/*
 * Process instruction, setting and resetting
 */
static void
processing_instruction_handler(void *user_data,  const char *target,  
			       const char *data)
{
  CAMLparam0();
  CAMLlocal2(t, d);
  value *handlers = user_data;

  t = copy_string(target);
  d = copy_string(data);
  callback2(Field(*handlers, EXPAT_PROCESSING_INSTRUCTION_HANDLER), t, d);

  CAMLreturn0;
}

static value 
set_processing_instruction_handler(value parser,
				   XML_ProcessingInstructionHandler c_handler,
				   value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_PROCESSING_INSTRUCTION_HANDLER, ocaml_handler);
  XML_SetProcessingInstructionHandler(xml_parser, c_handler);

  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetProcessingInstructionHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_processing_instruction_handler(parser, 
					       processing_instruction_handler, 
					       handler));
}

CAMLprim value
expat_XML_ResetProcessingInstructionHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_processing_instruction_handler(parser, NULL, Val_unit));
}

/*
 * Comment handler, setting and resetting
 */
static void
comment_handler(void *user_data, const char *data)
{
  CAMLparam0();
  CAMLlocal1(d);

  value *handlers = user_data;
  d = copy_string(data);
  callback(Field(*handlers, EXPAT_COMMENT_HANDLER), d);

  CAMLreturn0;
}

static value 
set_comment_handler(value parser, 
		    XML_CommentHandler c_handler, value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);
  
  Store_field(*handlers, EXPAT_COMMENT_HANDLER, ocaml_handler);
  XML_SetCommentHandler(xml_parser, c_handler);

  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetCommentHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_comment_handler(parser, comment_handler, handler));
}

CAMLprim value
expat_XML_ResetCommentHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_comment_handler(parser, NULL, Val_unit));
}