23 #include <rapidxml/rapidxml.hpp>
24 #include <rapidxml/rapidxml_print.hpp>
40 #define GSXML_COMMON_FUNCTIONS(obj) \
41 static bool has(gsXmlNode * node) \
42 { return firstByTag(tag(), node) != 0;} \
43 static bool hasAny(gsXmlNode * node) \
44 { return anyByTag(tag(), node) != 0;} \
45 static bool count(gsXmlNode * node) \
46 { return countByTag(tag(), node) != 0; } \
47 static obj * getFirst (gsXmlNode * node) \
48 { return get(firstByTag(tag(), node)); } \
49 static obj * getAny (gsXmlNode * node) \
50 { return get(anyByTag(tag(), node)); } \
51 static obj * getId (gsXmlNode * node, int id) \
52 { return getById< obj >(node, id); } \
53 static obj * getLabel(gsXmlNode * node, const std::string & label) \
54 { return getByLabel< obj >(node, label); }
56 #define GSXML_GET_POINTER(obj) \
57 static obj * get (gsXmlNode * node) \
58 { obj * result = new obj; \
59 get_into(node, *result); \
62 #define GSXML_GET_INTO(obj) \
63 static void get_into (gsXmlNode * node, obj & result) \
64 { result = *get(node); }
66 #define TMPLA2(t1,t2) t1,t2
67 #define TMPLA3(t1,t2,t3) t1,t2,t3
68 #define TMPLA4(t1,t2,t3,t4) t1,t2,t3,t4
73 inline std::istringstream &
74 operator>>(std::istringstream & is, mpq_class & var)
78 if ( !(is >> dn) )
return is;
79 const std::string::size_type comma( dn.find(
".") );
80 if( comma != std::string::npos )
82 const std::string::size_type exp = dn.size() - comma - 1;
83 const mpz_class num( dn.erase(comma,1), 10);
85 mpz_ui_pow_ui(den.get_mpz_t(),10,exp);
86 var = mpq_class(num, den);
101 template <
class U>
inline std::ofstream &
operator<<
102 (std::ofstream &fs, __gmp_expr<U,U> & var)
115 inline bool gsGetReal(std::istream & is, T & var)
117 GISMO_STATIC_ASSERT(!std::numeric_limits<T>::is_integer,
118 "The second parameter needs to be an integer type.");
120 if ( !(is >> dn) )
return false;
121 const std::string::size_type slh( dn.find(
"/") );
122 if( slh != std::string::npos )
124 var = strtod(dn.substr(0,slh).c_str(), NULL) /
125 strtod(dn.substr(slh+1).c_str(), NULL) ;
128 var = strtod(dn.c_str(), NULL);
134 inline bool gsGetInt(std::istream & is, Z & var)
136 GISMO_STATIC_ASSERT(std::numeric_limits<Z>::is_integer,
137 "The second parameter needs to be an integer type.");
139 return !(is >> var).fail();
144 inline bool gsGetReal(std::istream & is, mpq_class & var)
149 if ( !(is >> dn) )
return false;
150 const std::string::size_type comma( dn.find(
".") );
151 if( comma != std::string::npos )
153 const std::string::size_type exp = dn.size() - comma - 1;
154 const mpz_class num( dn.erase(comma,1), 10);
156 mpz_ui_pow_ui(den.get_mpz_t(),10,exp);
157 var = mpq_class(num, den);
161 if (
'+'==dn[0]) dn.erase(0, 1);
162 ok = (0==var.set_str(dn,10));
177 template <
typename Z>
178 typename util::enable_if<std::numeric_limits<Z>::is_integer,
bool>::type
179 gsGetValue(std::istream & is, Z & var)
180 {
return gsGetInt<Z>(is,var); }
182 template <
typename T>
183 typename util::enable_if<!std::numeric_limits<T>::is_integer,
bool>::type
184 gsGetValue(std::istream & is, T & var)
185 {
return gsGetReal<T>(is,var); }
189 typedef rapidxml::xml_node<char> gsXmlNode;
190 typedef rapidxml::xml_attribute<char> gsXmlAttribute;
191 typedef rapidxml::xml_document<char> gsXmlTree;
195 template<
class Object>
202 static std::string tag ();
209 static std::string type ();
210 static Object *
get (gsXmlNode * node);
211 static void get_into (gsXmlNode * node, Object & result);
212 static gsXmlNode * put (
const Object & obj, gsXmlTree & data);
215 static bool has (gsXmlNode * node);
216 static bool count (gsXmlNode * node);
217 static Object * getFirst (gsXmlNode * node);
219 static Object * getAny (gsXmlNode * node);
221 static Object * getId (gsXmlNode * node,
int id);
223 static Object * getLabel(gsXmlNode * node,
const std::string & label);
232 const std::string & attr_name,
233 const std::string & value,
234 const char *tag_name = NULL )
236 for (gsXmlNode * child = root->first_node(tag_name);
237 child; child = child->next_sibling(tag_name))
239 const gsXmlAttribute * attribute = child->first_attribute(attr_name.c_str());
240 if ( attribute && !strcmp(attribute->value(),value.c_str()) )
242 else if ( attribute &&
"time"==attr_name && atof(value.c_str()) == atof(attribute->value()) )
245 gsWarn <<
"gsXmlUtils: No "<< tag_name <<
" object with attribute '"<<attr_name<<
" = "<< value<<
"' found.\n";
254 inline gsXmlNode*
searchId(
const int id, gsXmlNode* root,
255 const char* tag_name = NULL,
256 const bool print_warning =
true) {
257 for (gsXmlNode* child = root->first_node(tag_name); child;
258 child = child->next_sibling(tag_name)) {
259 const gsXmlAttribute* id_at = child->first_attribute(
"id");
260 if (id_at && atoi(id_at->value()) ==
id)
return child;
263 gsWarn <<
"gsXmlUtils: No object with id = " <<
id <<
" found.\n";
271 template<
class Object>
272 Object *
getByLabel(gsXmlNode * node,
const std::string & label)
274 std::string tag = internal::gsXml<Object>::tag();
275 gsXmlNode * nd =
searchNode(node,
"label", label, tag.c_str());
278 return internal::gsXml<Object>::get(nd);
280 std::cerr<<
"gsXmlUtils Warning: "<< internal::gsXml<Object>::tag()
281 <<
" with label="<<label<<
" not found.\n";
290 template<
class Object>
291 Object *
getById(gsXmlNode * node,
const int &
id)
293 std::string tag = internal::gsXml<Object>::tag();
294 gsXmlNode * nd =
searchId(
id, node, tag.c_str());
297 return internal::gsXml<Object>::get(nd);
299 std::cerr<<
"gsXmlUtils Warning: "<< internal::gsXml<Object>::tag()
300 <<
" with id="<<
id<<
" not found.\n";
304 GISMO_EXPORT
char *
makeValue(
const std::string & value, gsXmlTree & data);
312 GISMO_EXPORT gsXmlAttribute *
makeAttribute(
const std::string & name,
313 const std::string & value, gsXmlTree & data);
316 GISMO_EXPORT gsXmlAttribute *
makeAttribute(
const std::string & name,
317 const unsigned & value, gsXmlTree & data);
320 GISMO_EXPORT gsXmlNode *
makeNode(
const std::string & name, gsXmlTree & data);
323 GISMO_EXPORT gsXmlNode *
makeNode(
const std::string & name,
324 const std::string & value, gsXmlTree & data);
327 GISMO_EXPORT gsXmlNode *
makeComment(
const std::string &, gsXmlTree & data);
330 GISMO_EXPORT std::string
to_string(
const unsigned & i);
334 GISMO_EXPORT
int countByTag(
const std::string & tag, gsXmlNode * root );
339 const std::string & type,
344 GISMO_EXPORT gsXmlNode *
firstByTag(
const std::string & tag,
350 const std::string & type,
360 GISMO_EXPORT gsXmlNode *
anyByTag(
const std::string & tag,
363 GISMO_EXPORT
void getBoundaries(gsXmlNode * node,
364 std::map<int, int> & ids,
365 std::vector< patchSide > & result);
367 GISMO_EXPORT
void getInterfaces(gsXmlNode* node,
369 std::map<int, int>& ids,
370 std::vector< boundaryInterface > & result);
378 gsXmlNode *
makeNode(
const std::string & name,
380 bool transposed =
false );
386 const std::string& base_type_flag =
"ascii");
391 gsXmlTree & data, std::string name =
"Matrix");
401 gsXmlTree & data, std::string name =
"SparseMatrix");
407 #ifndef GISMO_BUILD_LIB
408 #include GISMO_HPP_HEADER(gsXml.hpp)
Class that provides a container for triplets (i,j,value) to be filled in a sparse matrix...
Definition: gsSparseMatrix.h:33
gsXmlNode * anyByTag(const std::string &tag, gsXmlNode *root)
Helper to get any object (by tag) if one exists in the XML tree.
Definition: gsXml.cpp:155
int countByTagType(const std::string &tag, const std::string &type, gsXmlNode *root)
Definition: gsXml.cpp:97
int countByTag(const std::string &tag, gsXmlNode *root)
Definition: gsXml.cpp:81
gsXmlNode * firstByTag(const std::string &tag, gsXmlNode *root)
Definition: gsXml.cpp:119
Object * getById(gsXmlNode *node, const int &id)
Definition: gsXml.h:291
gsXmlNode * searchId(const int id, gsXmlNode *root, const char *tag_name=NULL, const bool print_warning=true)
Definition: gsXml.h:254
Handles shared library creation and other class attributes.
gsXmlNode * searchNode(gsXmlNode *root, const std::string &attr_name, const std::string &value, const char *tag_name=NULL)
Definition: gsXml.h:231
std::string to_string(const unsigned &i)
Helper to convert small unsigned to string.
Definition: gsXml.cpp:74
#define gsWarn
Definition: gsDebug.h:50
gsXmlNode * putSparseMatrixToXml(gsSparseMatrix< T > const &mat, gsXmlTree &data, std::string name="SparseMatrix")
Helper to insert sparse matrices into XML.
Definition: gsXml.hpp:111
gsXmlNode * makeComment(const std::string &comment, gsXmlTree &data)
Helper to create an XML comment node.
Definition: gsXml.cpp:68
Object * getByLabel(gsXmlNode *node, const std::string &label)
Definition: gsXml.h:272
gsXmlAttribute * makeAttribute(const std::string &name, const std::string &value, gsXmlTree &data)
Helper to allocate XML attribute.
Definition: gsXml.cpp:37
Provides forward declarations of types and structs.
gsXmlNode * makeNode(const std::string &name, gsXmlTree &data)
Helper to allocate XML node.
Definition: gsXml.cpp:54
char * makeValue(const std::string &value, gsXmlTree &data)
Helper to allocate XML value.
Definition: gsXml.cpp:32
gsXmlNode * putMatrixToXml(gsMatrix< T > const &mat, gsXmlTree &data, std::string name="Matrix")
Helper to insert matrices into XML.
Definition: gsXml.hpp:103
gsXmlNode * firstByTagType(const std::string &tag, const std::string &type, gsXmlNode *root)
Definition: gsXml.cpp:133
void getSparseEntriesFromXml(gsXmlNode *node, gsSparseEntries< T > &result)
Helper to fetch sparse entries.
Definition: gsXml.hpp:133
void appendBoxTopology(const gsBoxTopology &topology, gsXmlNode *node, gsXmlTree &data)
Appends a box topology into node, used for gsMultiPatch and gsMultiBasis.
Definition: gsXml.cpp:186
Defines a topological arrangement of a collection of "boxes" (e.g., parameter domains that map to phy...
Definition: gsBoxTopology.h:38
void getMatrixFromXml(gsXmlNode *node, unsigned const &rows, unsigned const &cols, gsMatrix< T > &result, const std::string &base_type_flag="ascii")
Helper to fetch matrices.
Definition: gsXml.hpp:71