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

Last change on this file since 9447 was 9406, checked in by davidb, 19 years ago

Added include statement for stdio.h. Caused compile error on Puka without
it.

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