$treeview $search $mathjax
Palabos  Version 1.1
$projectbrief
$projectbrief
$searchbox

TINYXML_xmlIO.hh

Go to the documentation of this file.
00001 /* This file is part of the Palabos library.
00002  *
00003  * Copyright (C) 2011 FlowKit Sarl
00004  * Avenue de Chailly 23
00005  * 1012 Lausanne, Switzerland
00006  * E-mail contact: contact@flowkit.com
00007  *
00008  * The most recent release of Palabos can be downloaded at 
00009  * <http://www.palabos.org/>
00010  *
00011  * The library Palabos is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Affero General Public License as
00013  * published by the Free Software Foundation, either version 3 of the
00014  * License, or (at your option) any later version.
00015  *
00016  * The library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Affero General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Affero General Public License
00022  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00029 #ifndef XML_IO_HH
00030 #define XML_IO_HH
00031 
00032 #include "libraryInterfaces/TINYXML_xmlIO.h"
00033 #include "core/runTimeDiagnostics.h"
00034 #include "core/globalDefs.h"
00035 #include "core/util.h"
00036 #include "parallelism/mpiManager.h"
00037 #include "io/parallelIO.h"
00038 #include <typeinfo>
00039 #include <cctype>
00040 
00041 namespace plb {
00042 
00043 template <typename T>
00044 void XMLreaderProxy::read(T& value) const {
00045     if (!reader) return;
00046     std::stringstream valueStr(reader->getText(id));
00047     T tmp = T();
00048     if (!(valueStr>>tmp)) {
00049         plbIOError(std::string("Cannot read value from XML element ") + reader->getName());
00050     }
00051     value = tmp;
00052 }
00053 
00054 template <>
00055 inline void XMLreaderProxy::read<bool>(bool& value) const {
00056     if (!reader) return;
00057     std::stringstream valueStr(reader->getText(id));
00058     std::string word;
00059     valueStr >> word;
00060     // Transform to lower-case, so that "true" and "false" are case-insensitive.
00061     word = util::tolower(word);
00062     if (word=="true") {
00063         value = true;
00064     }
00065     else if (word=="false") {
00066         value=false;
00067     }
00068     else {
00069         plbIOError(std::string("Cannot read boolean value from XML element ") + reader->getName());
00070     }
00071 }
00072 
00073 template <>
00074 inline void XMLreaderProxy::read<std::string>(std::string& entry) const {
00075     if (!reader) return;
00076     entry = reader->getText(id);
00077 }
00078 
00079 template <typename T>
00080 bool XMLreaderProxy::readNoThrow(T& value) const {
00081     if (!reader) return false;
00082     std::stringstream valueStr(reader->getText(id));
00083     T tmp = T();
00084     if (!(valueStr >> tmp)) {
00085         return false;
00086     }
00087     value = tmp;
00088     return true;
00089 }
00090 
00091 template <>
00092 inline bool XMLreaderProxy::readNoThrow<bool>(bool& value) const {
00093     if (!reader) return false;
00094     std::stringstream valueStr(reader->getText(id));
00095     std::string word;
00096     valueStr >> word;
00097     // Transform to lower-case, so that "true" and "false" are case-insensitive.
00098     word = util::tolower(word);
00099     if (word=="true") {
00100         value = true;
00101         return true;
00102     }
00103     else if (word=="false") {
00104         value=false;
00105         return true;
00106     }
00107     return false;
00108 }
00109 
00110 template <>
00111 inline bool XMLreaderProxy::readNoThrow<std::string>(std::string& entry) const {
00112     if (!reader) return false;
00113     entry = reader->getText(id);
00114     return true;
00115 }
00116 
00117 template <typename T>
00118 void XMLreaderProxy::read(std::vector<T>& values) const {
00119     if (!reader) return;
00120     std::stringstream multiValueStr(reader->getText(id));
00121     std::string word;
00122     std::vector<T> tmp(values);
00123     while (multiValueStr>>word) {
00124         std::stringstream valueStr(word);
00125         T value;
00126         if (!(valueStr >> value)) {
00127             plbIOError(std::string("Cannot read value array from XML element ") + reader->getName());
00128         }
00129         tmp.push_back(value);
00130     }
00131     values.swap(tmp);
00132 }
00133 
00134 template <typename T>
00135 bool XMLreaderProxy::readNoThrow(std::vector<T>& values) const {
00136     if (!reader) return false;
00137     std::stringstream multiValueStr(reader->getText(id));
00138     std::string word;
00139     std::vector<T> tmp(values);
00140     while (multiValueStr>>word) {
00141         std::stringstream valueStr(word);
00142         T value;
00143         if (!(valueStr >> value)) {
00144             return false;
00145         }
00146         tmp.push_back(value);
00147     }
00148     values.swap(tmp);
00149     return true;
00150 }
00151 
00152 template <typename T, plint N>
00153 void XMLreaderProxy::read(Array<T,N>& values) const {
00154     if (!reader) return;
00155     std::stringstream multiValueStr(reader->getText(id));
00156     std::string word;
00157     values.resetToZero();
00158     plint i=0;
00159     while (multiValueStr>>word && i<N) {
00160         std::stringstream valueStr(word);
00161         T value;
00162         if (!(valueStr >> value)) {
00163             plbIOError(std::string("Cannot read value array from XML element ") + reader->getName());
00164         }
00165         values[i] = value;
00166         ++i;
00167     }
00168 }
00169 
00170 template <typename T, plint N>
00171 bool XMLreaderProxy::readNoThrow(Array<T,N>& values) const {
00172     if (!reader) return false;
00173     std::stringstream multiValueStr(reader->getText(id));
00174     std::string word;
00175     plint i=0;
00176     while (multiValueStr>>word && i<N) {
00177         std::stringstream valueStr(word);
00178         T value;
00179         if (!(valueStr >> value)) {
00180             return false;
00181         }
00182         values[i] = value;
00183         ++i;
00184     }
00185     return true;
00186 }
00187 
00188 template<typename T> void XMLwriter::set(T const& value)
00189 {
00190     std::stringstream valuestr;
00191     valuestr << value;
00192     valuestr >> data_map[currentId].text;
00193 }
00194 
00195 template<typename T> void XMLwriter::set(std::vector<T> const& values)
00196 {
00197     std::stringstream valuestr;
00198     for (pluint i=0; i<values.size(); ++i) {
00199         if (i != 0) {
00200             valuestr << " ";
00201         }
00202         valuestr << values[i];
00203     }
00204     data_map[currentId].text = valuestr.str();
00205 }
00206 
00207 template<typename T, int N> void XMLwriter::set(Array<T,N> const& values)
00208 {
00209     std::stringstream valuestr;
00210     for (pluint i=0; i<N; ++i) {
00211         if (i != 0) {
00212             valuestr << " ";
00213         }
00214         valuestr << values[i];
00215     }
00216     data_map[currentId].text = valuestr.str();
00217 }
00218 
00219 template<> inline void XMLwriter::set<bool>(bool const& value)
00220 {
00221     if (value) {
00222         data_map[currentId].text = "True";
00223     }
00224     else {
00225         data_map[currentId].text = "False";
00226     }
00227 }
00228 
00229 template<typename ostrT>
00230 void XMLwriter::toOutputStream(ostrT& ostr, int indent) const {
00231     if (!global::mpi().isMainProcessor()) return;
00232     if (data_map.empty()) return;
00233 
00234     if (isDocument) {
00235         ostr << "<?xml version=\"1.0\" ?>\n";
00236         std::vector<XMLwriter*> const& children = data_map.begin()->second.children;
00237         for (pluint iNode=0; iNode<children.size(); ++iNode) {
00238             children[iNode]->toOutputStream(ostr);
00239         }
00240     }
00241     else {
00242         std::map<plint,Data>::const_iterator it = data_map.begin();
00243         for (; it != data_map.end(); ++it) {
00244             std::vector<XMLwriter*> const& children = it->second.children;
00245             std::string const& text = it->second.text;
00246             std::string indentStr(indent, ' ');
00247             ostr << indentStr << "<" << name;
00248             if (data_map.size()>1 || it->first!=0) {
00249                 ostr << " id=\"" << it->first << "\"";
00250             }
00251             if (children.empty()) {
00252                 ostr << ">";
00253             }
00254             else {
00255                 ostr << ">\n";
00256             }
00257             if (!text.empty()) {
00258                 if (children.empty()) {
00259                     ostr << " " << text << " ";
00260                 }
00261                 else {
00262                     ostr << indentStr << "    " << text << "\n";
00263                 }
00264             }
00265             for (pluint iNode=0; iNode<children.size(); ++iNode) {
00266                 children[iNode]->toOutputStream(ostr, indent+4);
00267             }
00268             if (!children.empty()) {
00269                 ostr << indentStr;
00270             }
00271             ostr << "</" << name << ">\n";
00272         }
00273     }
00274 }
00275 
00276 }  // namespace plb
00277 
00278 #endif  // XML_IO_HH