00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef XMLPARSER_H_
00022 #define XMLPARSER_H_
00023
00024 class Card;
00025
00027 class XMLParser
00028 {
00029 public:
00031 XMLParser();
00034 void Init(const std::string& file_path);
00035
00037 Card card(unsigned index, unsigned grade_min = 0, unsigned grade_max = 0,
00038 unsigned strokes_min = 0, unsigned strokes_max = 0);
00039
00041 int package_records () const;
00043 int QueryResultSize (unsigned grade_min, unsigned grade_max,
00044 unsigned strokes_min, unsigned strokes_max);
00045
00046 ~XMLParser ();
00047 private:
00049 enum mode
00050 {
00051 XML_KANJI,
00052 XML_VOCABULARY
00053 };
00054
00056 FILE* file_;
00058 std::string file_path_;
00060 std::string xml_version_;
00062 std::string package_name_;
00064 int package_records_;
00066 std::string package_format_;
00068 std::string package_license_;
00070 mode file_format_;
00071
00073 fpos_t* file_cursor_;
00075 std::vector< int >* grade_;
00077 std::vector< int >* strokes_;
00078
00081 std::vector<int> query_result_;
00082
00083 unsigned q_grade_min_,
00084 q_grade_max_,
00085 q_strokes_min_,
00086 q_strokes_max_;
00087
00089 static const int BUFFER_SIZE = 512;
00090
00091
00093 inline bool compare (const char* s1, const char* s2);
00095 inline bool find(const char* s, const char* buffer, int& position);
00097 void GenerateIndexes();
00107 unsigned GetIndex(unsigned index, unsigned grade_min, unsigned grade_max,
00108 unsigned strokes_min, unsigned strokes_max);
00110 void QueryResult (unsigned grade_min, unsigned grade_max,
00111 unsigned strokes_min, unsigned strokes_max);
00112
00118 std::string attribute_value(const char* name, const char* buffer,
00119 int& position);
00120 };
00121
00122 inline bool XMLParser::compare (const char* s1, const char* s2)
00123 {
00124 bool equal = false;
00125 if (s1 && s2)
00126 {
00127 size_t i = 0;
00128 for (;s1[i] == s2[i] && s1[i] != '\0' && s2 != '\0'; ++i);
00129 if (s1[i] == '\0')
00130 equal = true;
00131 }
00132 return equal;
00133 }
00134
00135 inline bool XMLParser::find(const char* s, const char* buffer, int& position)
00136 {
00137 bool found = false;
00138 int i = position;
00139 for (; buffer[i] != '\0' && !found && i < BUFFER_SIZE ; ++i)
00140 {
00141 if (compare (s, buffer+i))
00142 found = true;
00143 }
00144 if (!found)
00145 {
00146 i = 0;
00147 for (; buffer[i] != '\0' && !found && i < position ; ++i)
00148 {
00149 if (compare (s, buffer+i))
00150 found = true;
00151 }
00152 }
00153
00154 if (found)
00155 position = i - 1 ;
00156
00157 return found;
00158 }
00159
00160 #endif // XMLPARSER_H_