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 text;
|
---|
21 | text_t current_section;
|
---|
22 | };
|
---|
23 |
|
---|
24 | text_t current_text;
|
---|
25 | text_t section_num;
|
---|
26 | text_t section_level;
|
---|
27 |
|
---|
28 | bool store_text = false;
|
---|
29 |
|
---|
30 |
|
---|
31 | static void XMLCALL
|
---|
32 | startElement(void *userData, const char *name, const char **attributes)
|
---|
33 | {
|
---|
34 | text_t element_name = (char*)name;
|
---|
35 | if (element_name == section_level) {
|
---|
36 | text_t id_att = (char *)get_attribute(attributes,"gs2:id");
|
---|
37 | if (id_att == section_num) {
|
---|
38 | store_text = true;
|
---|
39 | }
|
---|
40 |
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | static void XMLCALL
|
---|
45 | endElement(void *userData, const char *name)
|
---|
46 | {
|
---|
47 | text_t element_name = (char*)name;
|
---|
48 | if (element_name == section_level) {
|
---|
49 | if (store_text == true) {
|
---|
50 | // we have finished now, can we quit this??
|
---|
51 | store_text = false;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | }
|
---|
56 |
|
---|
57 | static void XMLCALL
|
---|
58 | characterData(void *userData, const char * text, int len) {
|
---|
59 | if (store_text) {
|
---|
60 | current_text.appendcarr(text, len);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | int expat_document(const text_t &filename, const text_t &sec_level, const text_t &sec_num, text_t & doc_content){
|
---|
64 |
|
---|
65 | current_text.clear();
|
---|
66 | text_t current_section;
|
---|
67 | resultpack rpack = { doc_content, current_section };
|
---|
68 |
|
---|
69 | section_num = sec_num;
|
---|
70 | section_level = sec_level;
|
---|
71 | text_t doc_text;
|
---|
72 | read_file(filename, doc_text);
|
---|
73 |
|
---|
74 |
|
---|
75 | char * c_doc_text = doc_text.getcstr();
|
---|
76 | XML_Parser parser = XML_ParserCreate(NULL);
|
---|
77 |
|
---|
78 | XML_SetUserData(parser, &rpack);
|
---|
79 | XML_SetElementHandler(parser, startElement, endElement);
|
---|
80 | XML_SetCharacterDataHandler(parser, characterData);
|
---|
81 | int return_status = 0;
|
---|
82 | const int parse_status
|
---|
83 | = XML_Parse(parser, c_doc_text, strlen(c_doc_text), XML_TRUE);
|
---|
84 |
|
---|
85 | if (parse_status == XML_STATUS_ERROR) {
|
---|
86 | fprintf(stderr,
|
---|
87 | "%s at line %d\n",
|
---|
88 | XML_ErrorString(XML_GetErrorCode(parser)),
|
---|
89 | XML_GetCurrentLineNumber(parser));
|
---|
90 | return_status = 1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | XML_ParserFree(parser);
|
---|
94 | free(c_doc_text);
|
---|
95 | doc_content.clear();
|
---|
96 | doc_content.append(current_text);
|
---|
97 | return return_status;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|