source: trunk/gsdl/src/colservr/expat_resultset.cpp@ 9400

Last change on this file since 9400 was 9070, checked in by davidb, 19 years ago

See expat_resultset.h

  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
Line 
1
2#include <string.h>
3#include <expat.h>
4
5#if defined(GSDL_USE_OBJECTSPACE)
6# include <ospace\std\iostream>
7#elif defined(GSDL_USE_IOS_H)
8# include <iostream.h>
9#else
10# include <iostream>
11using namespace std;
12#endif
13
14#include "text_t.h"
15#include "queryinfo.h"
16
17struct queryresultpack {
18 queryresultsclass* queryresult_ptr;
19 int match_count;
20};
21
22char* get_attribute(const char** attr, char* att_name)
23{
24 char* att_val = NULL;
25
26 for (int i = 0; attr[i]; i += 2) {
27 if (strcmp(attr[i],att_name)==0) {
28 att_val = strdup(attr[i+1]);
29 }
30 }
31
32 return att_val;
33}
34
35static void XMLCALL
36startElement(void *userData, const char *name, const char **attributes)
37{
38 queryresultpack* qrpack_ptr = (queryresultpack*)userData;
39 queryresultsclass* queryresult_ptr = qrpack_ptr->queryresult_ptr;
40
41 text_t element_name = (char*)name;
42
43 if (element_name=="MatchingDocsInfo") {
44 char* num_match_docs_str = get_attribute(attributes,"num");
45 if (num_match_docs_str != NULL) {
46 int num_match_docs = atoi(num_match_docs_str);
47
48 queryresult_ptr->docs_matched = num_match_docs;
49 free(num_match_docs_str);
50 }
51 else {
52 queryresult_ptr->docs_matched = 0;
53 }
54
55 }
56
57
58 if (element_name=="Match") {
59
60 char* id = get_attribute(attributes,"id");
61 if (id!=NULL) {
62 int id_num = atoi(id);
63
64 docresultclass doc;
65 doc.clear();
66 doc.docnum = id_num;
67 doc.docweight = qrpack_ptr->match_count;
68 queryresult_ptr->docs.docset[doc.docnum] = doc;
69 queryresult_ptr->docs.docorder.push_back(doc.docnum);
70 qrpack_ptr->match_count++;
71
72 free(id);
73 }
74
75 }
76}
77
78static void XMLCALL
79endElement(void *userData, const char *name)
80{
81 // no need to do anything
82}
83
84
85
86
87int expat_resultset(text_t& xml_text, queryresultsclass& queryresult)
88{
89 queryresult.clear();
90 queryresultpack qrpack = { &queryresult, 0 };
91
92 char * xml_text_cstr = xml_text.getcstr();
93
94 cerr << "**** xml_text = " << xml_text.getcstr() << endl;
95
96 XML_Parser parser = XML_ParserCreate(NULL);
97
98 XML_SetUserData(parser, &qrpack);
99 XML_SetElementHandler(parser, startElement, endElement);
100
101 int return_status = 0;
102 const int parse_status
103 = XML_Parse(parser, xml_text_cstr, strlen(xml_text_cstr), XML_TRUE);
104
105 if (parse_status == XML_STATUS_ERROR) {
106 fprintf(stderr,
107 "%s at line %d\n",
108 XML_ErrorString(XML_GetErrorCode(parser)),
109 XML_GetCurrentLineNumber(parser));
110 return_status = 1;
111 }
112
113 XML_ParserFree(parser);
114 free(xml_text_cstr);
115
116 return return_status;
117}
Note: See TracBrowser for help on using the repository browser.