1 | #include <string.h>
|
---|
2 | #include <expat.h>
|
---|
3 |
|
---|
4 | #include "expat_document.h"
|
---|
5 |
|
---|
6 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
7 | # include <ospace\std\iostream>
|
---|
8 | #elif defined(GSDL_USE_IOS_H)
|
---|
9 | # include <iostream.h>
|
---|
10 | #else
|
---|
11 | # include <iostream>
|
---|
12 | using namespace std;
|
---|
13 | #endif
|
---|
14 |
|
---|
15 | #include "text_t.h"
|
---|
16 | #include "fileutil.h"
|
---|
17 | #include "expat_resultset.h"
|
---|
18 |
|
---|
19 | struct resultpack {
|
---|
20 | text_t *current_text;
|
---|
21 | text_t section_num;
|
---|
22 | text_t section_level;
|
---|
23 | bool store_text;
|
---|
24 | };
|
---|
25 |
|
---|
26 |
|
---|
27 | static void XMLCALL
|
---|
28 | startElement(void *userData, const char *name, const char **attributes)
|
---|
29 | {
|
---|
30 | resultpack * rpack_ptr = (resultpack*)userData;
|
---|
31 | text_t element_name = (char*)name;
|
---|
32 | if (element_name == rpack_ptr->section_level) {
|
---|
33 | text_t id_att = (char *)get_attribute(attributes,"gs2:id");
|
---|
34 | if (id_att == rpack_ptr->section_num) {
|
---|
35 | rpack_ptr->store_text = true;
|
---|
36 | }
|
---|
37 |
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | static void XMLCALL
|
---|
42 | endElement(void *userData, const char *name)
|
---|
43 | {
|
---|
44 | resultpack * rpack_ptr = (resultpack*)userData;
|
---|
45 | text_t element_name = (char*)name;
|
---|
46 | if (element_name == rpack_ptr->section_level) {
|
---|
47 | if (rpack_ptr->store_text == true) {
|
---|
48 | // we have finished now, can we quit this??
|
---|
49 | rpack_ptr->store_text = false;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | static void XMLCALL
|
---|
56 | characterData(void *userData, const char * text, int len) {
|
---|
57 | resultpack * rpack_ptr = (resultpack*)userData;
|
---|
58 | if (rpack_ptr->store_text) {
|
---|
59 | rpack_ptr->current_text->appendcarr(text, len);
|
---|
60 | }
|
---|
61 | }
|
---|
62 | int expat_document(const text_t &filename, const text_t &sec_level, const text_t &sec_num, text_t & doc_content){
|
---|
63 |
|
---|
64 | text_t current_text;
|
---|
65 | current_text.clear();
|
---|
66 | text_t section_num = sec_num;
|
---|
67 | text_t section_level = sec_level;
|
---|
68 |
|
---|
69 |
|
---|
70 | resultpack rpack = { ¤t_text, section_num, section_level, false};
|
---|
71 | //cerr << "sec num = "<<sec_num<<", sec level="<<sec_level<<", filename="<<filename<<endl;
|
---|
72 | text_t doc_text;
|
---|
73 | read_file(filename, doc_text);
|
---|
74 |
|
---|
75 |
|
---|
76 | char * c_doc_text = doc_text.getcstr();
|
---|
77 | XML_Parser parser = XML_ParserCreate(NULL);
|
---|
78 |
|
---|
79 | XML_SetUserData(parser, &rpack);
|
---|
80 | XML_SetElementHandler(parser, startElement, endElement);
|
---|
81 | XML_SetCharacterDataHandler(parser, characterData);
|
---|
82 | int return_status = 0;
|
---|
83 | const int parse_status
|
---|
84 | = XML_Parse(parser, c_doc_text, strlen(c_doc_text), XML_TRUE);
|
---|
85 |
|
---|
86 | if (parse_status == XML_STATUS_ERROR) {
|
---|
87 | fprintf(stderr,
|
---|
88 | "%s at line %d\n",
|
---|
89 | XML_ErrorString(XML_GetErrorCode(parser)),
|
---|
90 | XML_GetCurrentLineNumber(parser));
|
---|
91 | return_status = 1;
|
---|
92 | }
|
---|
93 |
|
---|
94 | XML_ParserFree(parser);
|
---|
95 | free(c_doc_text);
|
---|
96 | doc_content.clear();
|
---|
97 | doc_content.append(current_text);
|
---|
98 | return return_status;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|