Commit 089d2453 authored by Pietro Abate's avatar Pietro Abate
Browse files

[r2004-06-29 21:35:43 by afrisch] Remove expat stub files (external package)

Original author: afrisch
Date: 2004-06-29 21:35:44+00:00
parent fb2c4e98
Copyright (c) 2002, 2003 by Maas-Maarten Zeeman
The package ocaml-expat is copyright by Maas-Maarten Zeeman.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this document and the ocaml-expat software ("the Software"),
to deal in the Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
The Software is provided ``as is'', without warranty of any kind,
express or implied, including but not limited to the warranties of
merchantability, fitness for a particular purpose and noninfringement.
In no event shall Maas-Maarten Zeeman be liable for any claim, damages
or other liability, whether in an action of contract, tort or
otherwise, arising from, out of or in connection with the Software or
the use or other dealings in the software.
name="expat"
version="0.0.3"
description="Expat XML parser"
requires=""
archive(byte)="expat.cma"
archive(native)="expat.cmxa"
linkopts = ""
-include Makefile.conf
OCAMLMAKEFILE = OCamlMakefile
SOURCES = expat_stubs.c expat.mli expat.ml
CFLAGS = -O2 -fPIC -DPIC -DFULL_UNROLL -O2
CLIBS = expat
RESULT = expat
all: byte-code-library native-code-library
install: libinstall
uninstall: libuninstall
clean: nobackup clean-doc
-include $(OCAMLMAKEFILE)
# export STATIC = yes
# export INCDIRS := /usr/include
# export LIBDIRS := /usr/lib
This diff is collapsed.
This directory contains code written by Maas-Maarten Zeeman
and included here for convenience:
Home page: http://home.wanadoo.nl/maas/ocaml/
The build process has been adapted from the PCRE-OCAML wrapper:
http://www.ai.univie.ac.at/~markus/home/ocaml_sources.html
(***********************************************************************)
(* The OcamlExpat library *)
(* *)
(* Copyright 2002, 2003 Maas-Maarten Zeeman. All rights reserved. See *)
(* LICENCE for details. *)
(***********************************************************************)
(* $Id: expat.ml,v 1.4 2003/07/03 17:56:52 cvscast Exp $ *)
type expat_parser
type xml_error =
NONE
| NO_MEMORY
| SYNTAX
| NO_ELEMENTS
| INVALID_TOKEN
| UNCLOSED_TOKEN
| PARTIAL_CHAR
| TAG_MISMATCH
| DUPLICATE_ATTRIBUTE
| JUNK_AFTER_DOC_ELEMENT
| PARAM_ENTITY_REF
| UNDEFINED_ENTITY
| RECURSIVE_ENTITY_REF
| ASYNC_ENTITY
| BAD_CHAR_REF
| BINARY_ENTITY_REF
| ATTRIBUTE_EXTERNAL_ENTITY_REF
| MISPLACED_XML_PI
| UNKNOWN_ENCODING
| INCORRECT_ENCODING
| UNCLOSED_CDATA_SECTION
| EXTERNAL_ENTITY_HANDLING
| NOT_STANDALONE
| UNEXPECTED_STATE
| ENTITY_DECLARED_IN_PE
| FEATURE_REQUIRES_XML_DTD
| CANT_CHANGE_FEATURE_ONCE_PARSING
exception Expat_error of xml_error
external xml_error_to_string : xml_error -> string = "expat_XML_ErrorString"
(* exception is currently too minimalistic, but it's a start *)
let _ = Callback.register_exception "expat_error" (Expat_error NONE)
(* param entity handling *)
type xml_param_entity_parsing_choice =
NEVER
| UNLESS_STANDALONE
| ALWAYS
external set_param_entity_parsing : expat_parser ->
xml_param_entity_parsing_choice -> bool =
"expat_XML_SetParamEntityParsing"
(* return the version number of the expat library *)
external expat_version : unit -> string = "expat_XML_ExpatVersion"
(* calls to create a parser *)
external parser_create : encoding:string -> expat_parser =
"expat_XML_ParserCreate"
external parser_create_ns : encoding:string -> separator:char -> expat_parser =
"expat_XML_ParserCreateNS"
(* calls needed to parse *)
external parse : expat_parser -> string -> unit = "expat_XML_Parse"
external parse_sub : expat_parser -> string -> int -> int -> unit =
"expat_XML_ParseSub"
external final : expat_parser -> unit = "expat_XML_Final"
(* start element handler calls *)
external set_start_element_handler : expat_parser ->
(string -> (string * string) list -> unit) -> unit =
"expat_XML_SetStartElementHandler"
external reset_start_element_handler : expat_parser -> unit =
"expat_XML_ResetStartElementHandler"
(* end element handler calls *)
external set_end_element_handler : expat_parser -> (string -> unit) -> unit =
"expat_XML_SetEndElementHandler"
external reset_end_element_handler : expat_parser -> unit =
"expat_XML_ResetEndElementHandler"
(* character data handler calls *)
external set_character_data_handler : expat_parser -> (string -> unit) -> unit =
"expat_XML_SetCharacterDataHandler"
external reset_character_data_handler : expat_parser -> unit =
"expat_XML_ResetCharacterDataHandler"
(* processing instruction handler calls *)
external set_processing_instruction_handler : expat_parser ->
(string -> string -> unit) -> unit =
"expat_XML_SetProcessingInstructionHandler"
external reset_processing_instruction_handler : expat_parser -> unit =
"expat_XML_ResetProcessingInstructionHandler"
(* comment handler *)
external set_comment_handler : expat_parser -> (string -> unit) -> unit =
"expat_XML_SetCommentHandler"
external reset_comment_handler : expat_parser -> unit =
"expat_XML_ResetCommentHandler"
(* start cdata handler *)
external set_start_cdata_handler : expat_parser -> (unit -> unit) -> unit =
"expat_XML_SetStartCDataHandler"
external reset_start_cdata_handler : expat_parser -> unit =
"expat_XML_ResetStartCDataHandler"
(* end cdata handler *)
external set_end_cdata_handler : expat_parser -> (unit -> unit) -> unit =
"expat_XML_SetEndCDataHandler"
external reset_end_cdata_handler : expat_parser -> unit =
"expat_XML_ResetEndCDataHandler"
(* default handler *)
external set_default_handler : expat_parser -> (string -> unit) -> unit =
"expat_XML_SetDefaultHandler"
external reset_default_handler : expat_parser -> unit =
"expat_XML_ResetDefaultHandler"
(* external entity ref handler *)
external set_external_entity_ref_handler : expat_parser ->
(string option -> string option -> string -> string option -> unit) ->
unit = "expat_XML_SetExternalEntityRefHandler"
external reset_external_entity_ref_handler : expat_parser -> unit =
"expat_XML_ResetDefaultHandler"
(* some general parser query calls *)
external get_current_byte_index : expat_parser -> int =
"expat_XML_GetCurrentByteIndex"
external get_current_column_number : expat_parser -> int =
"expat_XML_GetCurrentColumnNumber"
external get_current_line_number : expat_parser -> int =
"expat_XML_GetCurrentLineNumber"
external get_current_byte_count : expat_parser -> int =
"expat_XML_GetCurrentByteCount"
(***********************************************************************)
(* The OcamlExpat library *)
(* *)
(* Copyright 2002, 2003 Maas-Maarten Zeeman. All rights reserved. See *)
(* LICENCE for details. *)
(***********************************************************************)
(* $Id: expat.mli,v 1.5 2003/07/03 17:56:52 cvscast Exp $ *)
(** The Ocaml Expat library provides an interface to the Expat XML Parser.
Expat is a library, written C, for parsing XML documents. It's the
underlying for Mozilla, Perl's [XML::Parser], Python's
[xml.parser.expat], and other open source XML parsers.
To use this library, link with
[ocamlc expat.cma]
or
[ocamlopt expat.cmxa]
@author Maas-Maarten Zeeman
*)
(** The type of expat parsers *)
type expat_parser
(** {5 Parser Creation} *)
(** Create a new XML parser. If encoding is not empty, it specifies
a character encoding to use for the document. This overrides the
document encoding declaration. Expat has four built in encodings.
[US-ASCII], [UTF-8], [UTF-16], [ISO-8859-1] *)
val parser_create : encoding:string -> expat_parser
(** Create a new XML parser that has namespace processing in effect *)
val parser_create_ns : encoding:string -> separator:char -> expat_parser
(** {5 Parsing} *)
(** Let the parser parse a chunk of an XML document.
@raise Expat_error error *)
val parse : expat_parser -> string -> unit
(** Let the parser parse a chunk of an XML document in a substring
@raise Expat_error error *)
val parse_sub : expat_parser -> string -> int -> int -> unit
(** Inform the parser that the entire document has been parsed. *)
val final : expat_parser -> unit
(** {5 Handler Setting and Resetting}
The strings that are passed to the handlers are always encoded in
[UTF-8]. Your application is responsible for translation of these
strings into other encodings.
*)
(** {6 Start element setting and resetting} *)
val set_start_element_handler : expat_parser ->
(string -> (string * string) list -> unit) -> unit
val reset_start_element_handler : expat_parser -> unit
(** {6 End element setting and resetting} *)
val set_end_element_handler : expat_parser -> (string -> unit) -> unit
val reset_end_element_handler : expat_parser -> unit
(** {6 Character data hander setting and resetting} *)
val set_character_data_handler : expat_parser -> (string -> unit) -> unit
val reset_character_data_handler : expat_parser -> unit
(** {6 Processing Instruction handler setting and resetting} *)
val set_processing_instruction_handler : expat_parser ->
(string -> string -> unit) -> unit
val reset_processing_instruction_handler : expat_parser -> unit
(** {6 Comment handler setting and resetting} *)
val set_comment_handler : expat_parser -> (string -> unit) -> unit
val reset_comment_handler : expat_parser -> unit
(** {6 CData Section handler setting and resetting} *)
val set_start_cdata_handler : expat_parser -> (unit -> unit) -> unit
val reset_start_cdata_handler : expat_parser -> unit
val set_end_cdata_handler : expat_parser -> (unit -> unit) -> unit
val reset_end_cdata_handler : expat_parser -> unit
(** {6 Default Handler setting and resetting} *)
val set_default_handler : expat_parser -> (string -> unit) -> unit
val reset_default_handler : expat_parser -> unit
(** {6 External Entity Ref Handler setting and resetting} *)
val set_external_entity_ref_handler :
expat_parser ->
(string option -> string option -> string -> string option -> unit) ->
unit
val reset_external_entity_ref_handler : expat_parser -> unit
(** {5 Parse Position Functions} *)
val get_current_byte_index : expat_parser -> int
val get_current_column_number : expat_parser -> int
val get_current_line_number : expat_parser -> int
val get_current_byte_count : expat_parser -> int
(** {5 Error Reporting} *)
type xml_error =
NONE
| NO_MEMORY
| SYNTAX
| NO_ELEMENTS
| INVALID_TOKEN
| UNCLOSED_TOKEN
| PARTIAL_CHAR
| TAG_MISMATCH
| DUPLICATE_ATTRIBUTE
| JUNK_AFTER_DOC_ELEMENT
| PARAM_ENTITY_REF
| UNDEFINED_ENTITY
| RECURSIVE_ENTITY_REF
| ASYNC_ENTITY
| BAD_CHAR_REF
| BINARY_ENTITY_REF
| ATTRIBUTE_EXTERNAL_ENTITY_REF
| MISPLACED_XML_PI
| UNKNOWN_ENCODING
| INCORRECT_ENCODING
| UNCLOSED_CDATA_SECTION
| EXTERNAL_ENTITY_HANDLING
| NOT_STANDALONE
| UNEXPECTED_STATE
| ENTITY_DECLARED_IN_PE
| FEATURE_REQUIRES_XML_DTD
| CANT_CHANGE_FEATURE_ONCE_PARSING
(** Exception raised by parse function to report error conditions *)
exception Expat_error of xml_error
(** Converts a xml_error to a string *)
val xml_error_to_string : xml_error -> string
(** {5 Miscellaneous Functions} *)
(** Parameter entity handling types *)
type xml_param_entity_parsing_choice =
NEVER
| UNLESS_STANDALONE
| ALWAYS
(** Enable the parsing of parameter entities *)
val set_param_entity_parsing :
expat_parser -> xml_param_entity_parsing_choice -> bool
(** Return the Expat library version as a string (e.g. "expat_1.95.1" *)
val expat_version : unit -> string
/***********************************************************************/
/* The OcamlExpat library */
/* */
/* Copyright 2002, 2003 Maas-Maarten Zeeman. All rights reserved. See */
/* LICENCE for details. */
/***********************************************************************/
/* $Id: expat_stubs.c,v 1.6 2003/07/27 12:40:39 cvscast Exp $ */
/* 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 9
enum expat_handler {
EXPAT_START_ELEMENT_HANDLER,
EXPAT_END_ELEMENT_HANDLER,
EXPAT_CHARACTER_DATA_HANDLER,
EXPAT_PROCESSING_INSTRUCTION_HANDLER,
EXPAT_COMMENT_HANDLER,
EXPAT_START_CDATA_HANDLER,
EXPAT_END_CDATA_HANDLER,
EXPAT_DEFAULT_HANDLER,
EXPAT_EXTERNAL_ENTITY_REF_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);
XML_ParserFree(xml_parser);
stat_free(handlers);
}
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);
XML_Parser_val(parser) = 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);
*handlers = Val_unit;
register_global_root(handlers);
/*
* Create a tuple which will hold the 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());
}
/*
* Set the expat parameter for entity parsing
*/
value
expat_XML_SetParamEntityParsing(value parser, value choice) {
CAMLparam2(parser, choice);
CAMLreturn (Val_bool(XML_SetParamEntityParsing(XML_Parser_val(parser),
Int_val(choice))));
}
/*
* 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);
}
/*
* A sub-string variant of the parse call
*/
CAMLprim value
expat_XML_ParseSub(value parser, value string, value pos, value len)
{
CAMLparam4(parser, string, pos, len);
XML_Parser xml_parser = XML_Parser_val(parser);
if(!XML_Parse(xml_parser,