root/trunk/libfreebob/src/bebob/bebob_serialize.cpp

Revision 361, 6.0 kB (checked in by wagi, 17 years ago)

serializing interface extended. write and read except a 'string' as argument

Line 
1 /* bebob_serialize.cpp
2  * Copyright (C) 2006 by Daniel Wagner
3  *
4  * This file is part of FreeBoB.
5  *
6  * FreeBoB is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * FreeBoB is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with FreeBoB; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA.
19  */
20
21 #include "bebob_serialize.h"
22
23 using namespace std;
24
25 void tokenize(const string& str,
26               vector<string>& tokens,
27               const string& delimiters = " ")
28 {
29     // Skip delimiters at beginning.
30     string::size_type lastPos = str.find_first_not_of(delimiters, 0);
31     // Find first "non-delimiter".
32     string::size_type pos     = str.find_first_of(delimiters, lastPos);
33
34     while (string::npos != pos || string::npos != lastPos) {
35         // Found a token, add it to the vector.
36         tokens.push_back(str.substr(lastPos, pos - lastPos));
37         // Skip delimiters.  Note the "not_of"
38         lastPos = str.find_first_not_of(delimiters, pos);
39         // Find next "non-delimiter"
40         pos = str.find_first_of(delimiters, lastPos);
41     }
42 }
43
44 /////////////////////////////////
45
46 BeBoB::XMLSerialize::XMLSerialize( const char* pFileName )
47     : IOSerialize()
48     , m_filepath( pFileName )
49 {
50     try {
51         m_doc.create_root_node( "bebob_cache" );
52     } catch ( const exception& ex ) {
53         cout << "Exception caught: " << ex.what();
54     }
55 }
56
57
58 BeBoB::XMLSerialize::~XMLSerialize()
59 {
60     try {
61         m_doc.write_to_file_formatted( m_filepath );
62     } catch ( const exception& ex ) {
63         cout << "Exception caugth: " << ex.what();
64     }
65
66 }
67
68 bool
69 BeBoB::XMLSerialize::write( const char* pMemberName,
70                             long long value )
71
72 {
73     vector<string> tokens;
74     string str( pMemberName );
75     tokenize( str, tokens, "/" );
76
77     if ( tokens.size() == 0 ) {
78         return false;
79     }
80
81     xmlpp::Node* pNode = m_doc.get_root_node();
82     pNode = getNodePath( pNode, tokens );
83
84     // element to be added
85     xmlpp::Element* pElem = pNode->add_child( tokens[tokens.size() - 1] );
86     char* valstr;
87     asprintf( &valstr, "%lld", value );
88     pElem->set_child_text( valstr );
89     free( valstr );
90
91     return true;
92 }
93
94 bool
95 BeBoB::XMLSerialize::write( const char* pMemberName,
96                             Glib::ustring str)
97 {
98     vector<string> tokens;
99     string memberName( pMemberName );
100     tokenize( memberName, tokens, "/" );
101
102     if ( tokens.size() == 0 ) {
103         return false;
104     }
105
106     xmlpp::Node* pNode = m_doc.get_root_node();
107     pNode = getNodePath( pNode, tokens );
108
109     // element to be added
110     xmlpp::Element* pElem = pNode->add_child( tokens[tokens.size() - 1] );
111     pElem->set_child_text( str );
112
113     return true;
114 }
115
116 xmlpp::Node*
117 BeBoB::XMLSerialize::getNodePath( xmlpp::Node* pRootNode, std::vector<string>& tokens )
118 {
119     // returns the correct node on which the new element has to be added.
120     // if the path does not exist, it will be created.
121
122     if ( tokens.size() == 1 ) {
123         return pRootNode;
124     }
125
126     unsigned int iTokenIdx = 0;
127     xmlpp::Node* pCurNode = pRootNode;
128     for (bool bFound = false;
129          ( iTokenIdx < tokens.size() - 1 );
130          bFound = false, iTokenIdx++ )
131     {
132         xmlpp::Node::NodeList nodeList = pCurNode->get_children();
133         for ( xmlpp::Node::NodeList::iterator it = nodeList.begin();
134               it != nodeList.end();
135               ++it )
136         {
137             if ( ( *it )->get_name() == tokens[iTokenIdx] ) {
138                 pCurNode = *it;
139                 bFound = true;
140                 break;
141             }
142         }
143         if ( !bFound ) {
144             break;
145         }
146     }
147
148     for ( unsigned int i = iTokenIdx; i < tokens.size() - 1; i++, iTokenIdx++ ) {
149         pCurNode = pCurNode->add_child( tokens[iTokenIdx] );
150     }
151     return pCurNode;
152
153 }
154
155 /***********************************/
156
157 BeBoB::XMLDeserialize::XMLDeserialize( const char* pFileName )
158     : IODeserialize()
159     , m_filepath( pFileName )
160 {
161     try {
162         m_parser.set_substitute_entities(); //We just want the text to
163                                             //be resolved/unescaped
164                                             //automatically.
165         m_parser.parse_file( m_filepath );
166     } catch ( const exception& ex ) {
167         cout << "Exception caught: " << ex.what();
168     }
169 }
170
171
172 BeBoB::XMLDeserialize::~XMLDeserialize()
173 {
174 }
175
176 bool
177 BeBoB::XMLDeserialize::read( const char* pMemberName,
178                              long long& value )
179
180 {
181     xmlpp::Node* pNode = m_parser.get_document()->get_root_node();
182
183     xmlpp::NodeSet nodeSet = pNode->find( pMemberName );
184     for ( xmlpp::NodeSet::iterator it = nodeSet.begin();
185           it != nodeSet.end();
186           ++it )
187     {
188         const xmlpp::Element* pElement = dynamic_cast< const xmlpp::Element* >( *it );
189         if ( pElement && pElement->has_child_text() ) {
190             char* tail;
191             value = strtoll( pElement->get_child_text()->get_content().c_str(), &tail, 0 );
192             return true;
193         }
194         return false;
195     }
196
197     return false;
198 }
199
200 bool
201 BeBoB::XMLDeserialize::read( const char* pMemberName,
202                              Glib::ustring& str )
203 {
204     xmlpp::Node* pNode = m_parser.get_document()->get_root_node();
205
206     xmlpp::NodeSet nodeSet = pNode->find( pMemberName );
207     for ( xmlpp::NodeSet::iterator it = nodeSet.begin();
208           it != nodeSet.end();
209           ++it )
210     {
211         const xmlpp::Element* pElement = dynamic_cast< const xmlpp::Element* >( *it );
212         if ( pElement && pElement->has_child_text() ) {
213             str = pElement->get_child_text()->get_content();
214             return true;
215         }
216         return false;
217     }
218
219     return false;
220 }
Note: See TracBrowser for help on using the browser.