Skip to content

Commit

Permalink
meeting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Sep 8, 2011
1 parent 66fbf15 commit f756bec
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 67 deletions.
46 changes: 6 additions & 40 deletions ext/ox/ox.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static VALUE
get_def_opts(VALUE self) {
VALUE opts = rb_hash_new();
int elen = (int)strlen(default_options.encoding);

rb_hash_aset(opts, encoding_sym, (0 == elen) ? Qnil : rb_str_new(default_options.encoding, elen));
rb_hash_aset(opts, indent_sym, INT2FIX(default_options.indent));
rb_hash_aset(opts, trace_sym, INT2FIX(default_options.trace));
Expand Down Expand Up @@ -443,50 +443,16 @@ load_file(int argc, VALUE *argv, VALUE self) {
return load(xml, argc - 1, argv + 1, self);
}

static size_t
read_from_io(SaxControl ctrl, char *buf, size_t max_len) {
// TBD
return 0;
}

static size_t
read_from_file(SaxControl ctrl, char *buf, size_t max_len) {
// TBD
return 0;
}

/* call-seq: sax_parse(handler, io, options) => Ox::Document or Ox::Element or Object
/* call-seq: sax_parse(handler, io)
*
* Parses and IO stream or file containing an XML document into an
* Ox::Document, or Ox::Element, or Object depending on the options. Raises
* an exception if the XML is malformed or the classes specified are not
* valid.
* @param [Ox::Sax] handler SAX like handler
* Parses an IO stream or file containing an XML document. Raises an exception
* if the XML is malformed or the classes specified are not valid.
* @param [Ox::Sax] handler SAX (responds to OX::Sax methods) like handler
* @param [IO|String] io IO Object or file path to read from
*/
static VALUE
sax_parse(VALUE self, VALUE handler, VALUE io) {
struct _SaxControl ctrl;

if (T_STRING == rb_type(io)) {
ctrl.read_func = read_from_file;
ctrl.fp = fopen(StringValuePtr(io), "r");
} else if (rb_respond_to(io, read_nonblock_id)) {
ctrl.read_func = read_from_io;
ctrl.io = io;
} else {
rb_raise(rb_eArgError, "sax_parser io argument must respond to read_nonblock().\n");
}
ctrl.has_instruct = rb_respond_to(handler, instruct_id);
ctrl.has_doctype = rb_respond_to(handler, doctype_id);
ctrl.has_comment = rb_respond_to(handler, comment_id);
ctrl.has_cdata = rb_respond_to(handler, cdata_id);
ctrl.has_text = rb_respond_to(handler, text_id);
ctrl.has_start_element = rb_respond_to(handler, start_element_id);
ctrl.has_end_element = rb_respond_to(handler, end_element_id);
ctrl.has_error = rb_respond_to(handler, error_id);

ox_sax_parse(handler, &ctrl);
ox_sax_parse(handler, io);

return Qnil;
}
Expand Down
28 changes: 10 additions & 18 deletions ext/ox/ox.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,27 +195,10 @@ typedef struct _Options {
char effort; // Effort
} *Options;

typedef struct _SaxControl {
// TBD reader function
void *read_func;
union {
FILE *fp;
VALUE io;
};
int has_instruct;
int has_doctype;
int has_comment;
int has_cdata;
int has_text;
int has_start_element;
int has_end_element;
int has_error;
} *SaxControl;

extern VALUE parse(char *xml, ParseCallbacks pcb, char **endp, int trace, Effort effort);
extern void _raise_error(const char *msg, const char *xml, const char *current, const char* file, int line);

extern void ox_sax_parse(VALUE handler, SaxControl ctrl);
extern void ox_sax_parse(VALUE handler, VALUE io);

extern char* write_obj_to_str(VALUE obj, Options copts);
extern void write_obj_to_file(VALUE obj, const char *path, Options copts);
Expand All @@ -225,15 +208,24 @@ extern VALUE Ox;
extern ID at_id;
extern ID attributes_id;
extern ID beg_id;
extern ID cdata_id;
extern ID comment_id;
extern ID den_id;
extern ID doctype_id;
extern ID end_element_id;
extern ID end_id;
extern ID error_id;
extern ID excl_id;
extern ID inspect_id;
extern ID instruct_id;
extern ID keys_id;
extern ID local_id;
extern ID nodes_id;
extern ID num_id;
extern ID parse_id;
extern ID read_nonblock_id;
extern ID start_element_id;
extern ID text_id;
extern ID to_c_id;
extern ID to_s_id;
extern ID tv_sec_id;
Expand Down
76 changes: 67 additions & 9 deletions ext/ox/sax.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,73 @@
// 65536 = 0x00010000
#define BUF_SIZE = 0x00010000

typedef struct _SaxDrive {
// TBD reader function
void *read_func;
union {
FILE *fp;
VALUE io;
};
int has_instruct;
int has_doctype;
int has_comment;
int has_cdata;
int has_text;
int has_start_element;
int has_end_element;
int has_error;
} *SaxDrive;

static void ox_sax_drive_init(SaxDrive dr, VALUE handler, VALUE io);
static size_t read_from_io(SaxDrive dr, char *buf, size_t max_len);
static size_t read_from_file(SaxDrive dr, char *buf, size_t max_len);

void
ox_sax_parse(VALUE handler, SaxControl sc) {
ox_sax_parse(VALUE handler, VALUE io) {
struct _SaxDrive dr;

ox_sax_drive_init(&dr, handler, io);

printf("*** sax_parse with these flags\n");
printf(" has_instruct = %s\n", sc->has_instruct ? "true" : "false");
printf(" has_doctype = %s\n", sc->has_doctype ? "true" : "false");
printf(" has_comment = %s\n", sc->has_comment ? "true" : "false");
printf(" has_cdata = %s\n", sc->has_cdata ? "true" : "false");
printf(" has_text = %s\n", sc->has_text ? "true" : "false");
printf(" has_start_element = %s\n", sc->has_start_element ? "true" : "false");
printf(" has_end_element = %s\n", sc->has_end_element ? "true" : "false");
printf(" has_error = %s\n", sc->has_error ? "true" : "false");
printf(" has_instruct = %s\n", dr.has_instruct ? "true" : "false");
printf(" has_doctype = %s\n", dr.has_doctype ? "true" : "false");
printf(" has_comment = %s\n", dr.has_comment ? "true" : "false");
printf(" has_cdata = %s\n", dr.has_cdata ? "true" : "false");
printf(" has_text = %s\n", dr.has_text ? "true" : "false");
printf(" has_start_element = %s\n", dr.has_start_element ? "true" : "false");
printf(" has_end_element = %s\n", dr.has_end_element ? "true" : "false");
printf(" has_error = %s\n", dr.has_error ? "true" : "false");
}

static void
ox_sax_drive_init(SaxDrive dr, VALUE handler, VALUE io) {
if (T_STRING == rb_type(io)) {
dr->read_func = read_from_file;
dr->fp = fopen(StringValuePtr(io), "r");
} else if (rb_respond_to(io, read_nonblock_id)) {
dr->read_func = read_from_io;
dr->io = io;
} else {
rb_raise(rb_eArgError, "sax_parser io argument must respond to read_nonblock().\n");
}
dr->has_instruct = rb_respond_to(handler, instruct_id);
dr->has_doctype = rb_respond_to(handler, doctype_id);
dr->has_comment = rb_respond_to(handler, comment_id);
dr->has_cdata = rb_respond_to(handler, cdata_id);
dr->has_text = rb_respond_to(handler, text_id);
dr->has_start_element = rb_respond_to(handler, start_element_id);
dr->has_end_element = rb_respond_to(handler, end_element_id);
dr->has_error = rb_respond_to(handler, error_id);
}

static size_t
read_from_io(SaxDrive dr, char *buf, size_t max_len) {
// TBD
return 0;
}

static size_t
read_from_file(SaxDrive dr, char *buf, size_t max_len) {
// TBD
return 0;
}
1 change: 1 addition & 0 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- if word spans blocks then create new buffer and copy into that or maybe memmove
- given a file name, open and start reading in blocks
- given an IO, start reading in blocks
- general object/generic parser

- hints
xmllint --valid --noout --dtdvalid ../misc/ox.dtd --debug sample.xml
Expand Down

0 comments on commit f756bec

Please sign in to comment.