root/trunk/libffado/src/libutil/serialize.cpp

Revision 445, 6.5 kB (checked in by pieterpalmers, 17 years ago)

* name change from FreeBoB to FFADO
* replaced tabs by 4 spaces
* got rid of end-of-line spaces
* made all license and copyrights conform

library becomes LGPL, apps become GPL
explicitly state LGPL v2.1 and GPL v2 (don't like v3 draft)

copyrights are 2005-2007 Daniel & Pieter
except for the MotU stuff (C) Jonathan, Pieter

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