source: trunk/gsdl/src/colservr/expat_document.cpp@ 9175

Last change on this file since 9175 was 9173, checked in by kjdon, 19 years ago

added expat_document for parsing the source docs to get the content

  • Property svn:keywords set to Author Date Id Revision
File size: 2.1 KB
Line 
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>
12using namespace std;
13#endif
14
15#include "text_t.h"
16#include "fileutil.h"
17#include "expat_resultset.h"
18
19struct resultpack {
20 text_t text;
21 text_t current_section;
22};
23
24text_t current_text;
25text_t section_num;
26
27bool store_text = false;
28
29
30static void XMLCALL
31startElement(void *userData, const char *name, const char **attributes)
32{
33 text_t element_name = (char*)name;
34 if (element_name == "Sec") {
35 text_t id_att = (char *)get_attribute(attributes,"gs2:id");
36 if (id_att == section_num) {
37 store_text = true;
38 }
39
40 }
41}
42
43static void XMLCALL
44endElement(void *userData, const char *name)
45{
46 text_t element_name = (char*)name;
47 if (element_name == "Sec") {
48 if (store_text == true) {
49 // we have finished now, can we quit this??
50 store_text = false;
51 }
52 }
53
54}
55
56static void XMLCALL
57characterData(void *userData, const char * text, int len) {
58 if (store_text) {
59 current_text.appendcarr(text, len);
60 }
61}
62int expat_document(const text_t &filename, const text_t &sec_num, text_t & doc_content){
63
64 current_text.clear();
65 text_t current_section;
66 resultpack rpack = { doc_content, current_section };
67
68 section_num = sec_num;
69 text_t doc_text;
70 read_file(filename, doc_text);
71
72
73 char * c_doc_text = doc_text.getcstr();
74 XML_Parser parser = XML_ParserCreate(NULL);
75
76 XML_SetUserData(parser, &rpack);
77 XML_SetElementHandler(parser, startElement, endElement);
78 XML_SetCharacterDataHandler(parser, characterData);
79 int return_status = 0;
80 const int parse_status
81 = XML_Parse(parser, c_doc_text, strlen(c_doc_text), XML_TRUE);
82
83 if (parse_status == XML_STATUS_ERROR) {
84 fprintf(stderr,
85 "%s at line %d\n",
86 XML_ErrorString(XML_GetErrorCode(parser)),
87 XML_GetCurrentLineNumber(parser));
88 return_status = 1;
89 }
90
91 XML_ParserFree(parser);
92 free(c_doc_text);
93 doc_content.clear();
94 doc_content.append(current_text);
95 return return_status;
96}
97
98
Note: See TracBrowser for help on using the repository browser.