@@ -6,65 +6,52 @@ DummyDictionary::DummyDictionary(const char *f, const char *dict)
66{
77 file = f;
88 file += " config.xml" ;
9- parser = scew_parser_create ();
10- scew_parser_ignore_whitespaces (parser, 1 );
11- if (!scew_parser_load_file (parser, file.c_str ())) {
12- createNewConfig (file.c_str ());
13- scew_parser_load_file (parser, file.c_str ());
9+ if (!doc.LoadFile (file)) {
10+ createNewConfig (file);
11+ doc.LoadFile ();
1412 }
15- tree = scew_parser_tree (parser);
16- root = scew_tree_root (tree);
17- scew_element* i = scew_element_next (root, NULL );
18- while (i) {
19- if (!strcmp (scew_element_name (i), " dict" )) {
20- scew_attribute* at = scew_attribute_by_name (i, " name" );
21- if (at && !strcmp (scew_attribute_value (at), dict))
22- break ;
23- }
24- i = scew_element_next (root, i);
25- }
26- module = i;
13+
14+ module = findChild (doc.RootElement (), " dict" , dict);
2715 name = dict;
2816}
2917
30- bool DummyDictionary::createNewConfig (const char *f )
18+ bool DummyDictionary::createNewConfig (std::string file )
3119{
32- scew_tree *t = NULL ;
33- scew_element *r = NULL ;
34- t = scew_tree_create ();
35- r = scew_tree_add_root (t, " OpenVanilla" );
36- scew_writer_tree_file (t, f);
37- scew_tree_free (t);
20+ TiXmlDocument tmp (file);
21+ tmp.Parse (" <?xml version=\" 1.0\" encoding=\" UTF-8\" ?><OpenVanilla></OpenVanilla>" );
22+ tmp.SaveFile ();
3823 return true ;
3924}
4025
4126void DummyDictionary::newDict ()
4227{
43- module = scew_element_add (root, " dict" );
44- scew_element_add_attr_pair (module , " name" , name.c_str ());
45- scew_writer_tree_file (tree, file.c_str ());
28+ TiXmlElement tmp (" dict" );
29+ tmp.SetAttribute (" name" , name.c_str ());
30+ module = doc.RootElement ()->InsertEndChild (tmp);
31+ doc.SaveFile ();
32+ }
33+
34+ TiXmlNode *DummyDictionary::findChild (TiXmlNode *parent, const char *node, const char *name)
35+ {
36+ TiXmlNode *child = 0 ;
37+ while ( child = parent->ToElement ()->IterateChildren ( child ) ) {
38+ if (!strcmp (child->Value (), node))
39+ if (!strcmp (child->ToElement ()->Attribute (" name" ), name))
40+ break ;
41+ }
42+ return child;
4643}
4744
4845DummyDictionary::~DummyDictionary ()
4946{
50- scew_parser_free (parser);
5147}
5248
5349int DummyDictionary::keyExist (const char *key)
5450{
55- unsigned int count;
56- int retval = 0 ;
57- if (!module ) return retval;
58- scew_element** list = scew_element_list (module , " key" , &count);
59- for (int i = 0 ; i < count; i++) {
60- scew_attribute* at = scew_attribute_by_name (list[i], " name" );
61- if (at && !strcmp (scew_attribute_value (at), key)) {
62- retval = 1 ;
63- break ;
64- }
65- }
66- scew_element_list_free (list);
67- return retval;
51+ if (module && findChild (module , " key" , key))
52+ return 1 ;
53+ else
54+ return 0 ;
6855}
6956
7057int DummyDictionary::getInteger (const char *key)
@@ -82,61 +69,43 @@ int DummyDictionary::setInteger(const char *key, int value)
8269
8370const char * DummyDictionary::getString (const char *key)
8471{
85- int i;
86- unsigned int count;
8772 if (!module ) return " " ;
88- scew_element** list = scew_element_list (module , " key" , &count);
89- for (i = 0 ; i < count; i++) {
90- scew_attribute* at = scew_attribute_by_name (list[i], " name" );
91- if (at && !strcmp (scew_attribute_value (at), key))
92- break ;
93- }
94- if (i == count) {
95- scew_element_list_free (list);
96- return " " ;
97- }
98- scew_attribute* at = scew_attribute_by_name (list[i], " value" );
99- const XML_Char *value = at ? scew_attribute_value (at) : NULL ;
100- scew_element_list_free (list);
101- if (value)
102- return value;
73+ TiXmlNode *child = findChild (module , " key" , key);
74+ if (child)
75+ return child->ToElement ()->Attribute (" value" );
10376 else
10477 return " " ;
10578}
10679
10780const char * DummyDictionary::setString (const char *key, const char *value)
10881{
109- int i;
110- unsigned int count;
111- scew_element *cur;
11282 if (!module )
11383 newDict ();
114- scew_element** list = scew_element_list (module , " key" , &count);
115- for (i = 0 ; i < count; i++) {
116- scew_attribute* at = scew_attribute_by_name (list[i], " name" );
117- if (at && !strcmp (scew_attribute_value (at), key))
118- break ;
84+ TiXmlNode *child;
85+ child = findChild (module , " key" , key);
86+ if (!child) {
87+ TiXmlElement tmp (" key" );
88+ tmp.SetAttribute (" name" , key);
89+ child = module ->ToElement ()->InsertEndChild (tmp);
11990 }
120- if (i == count)
121- cur = scew_element_add (module , " key" );
122- else
123- cur = list[i];
124- scew_element_add_attr_pair (cur, " name" , key);
125- scew_element_add_attr_pair (cur, " value" , value);
126- scew_writer_tree_file (tree, file.c_str ());
127- scew_element_list_free (list);
91+ child->ToElement ()->SetAttribute (" value" , value);
92+ doc.SaveFile ();
12893 return value;
12994}
13095
13196/*
13297int main()
13398{
134- DummyDictionary d("F:\\windows\\openvanilla\\", "Orz");
99+ DummyDictionary d(".\\", "Orz");
100+ if(d.keyExist("ccc"))
101+ std::cout << "Ya!" << d.getString("ccc") << std::endl;
135102 d.setInteger("ccc", 456);
103+ d.setString("bbb", "hahaha");
136104 if(d.keyExist("ccc"))
137105 {
138106 printf("orz/aaa: %d\n", d.getInteger("aaa"));
139107 printf("orz/aaa: %s\n", d.getString("ccc"));
108+ printf("orz/bbb: %s\n", d.getString("bbb"));
140109 }
141110 return 0;
142111}
0 commit comments