File: meta.cpp

package info (click to toggle)
ismrmrd 1.14.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,564 kB
  • sloc: cpp: 6,439; ansic: 2,276; xml: 1,025; sh: 187; python: 72; makefile: 42
file content (56 lines) | stat: -rw-r--r-- 1,871 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "ismrmrd/meta.h"
#include "pugixml.hpp"


namespace ISMRMRD {
    void deserialize(const char *xml, MetaContainer &h) {
        pugi::xml_document doc;
        pugi::xml_parse_result result = doc.load_string(xml);

        if (!result) {
            throw std::runtime_error("Unable to load ISMRMRD Meta XML document");
        }

        pugi::xml_node root = doc.child("ismrmrdMeta");

        if (!root) {
            throw std::runtime_error("ismrmrdMeta tag not found in meta data header");
        }

        pugi::xml_node meta = root.child("meta");
        while (meta) {
            pugi::xml_node name = meta.child("name");
            pugi::xml_node value = meta.child("value");

            if (!name || !value) {
                throw std::runtime_error("Malformed metadata value");
            }

            while (value) {
                h.append(name.child_value(), value.child_value());
                value = value.next_sibling("value");
            }

            meta = meta.next_sibling("meta");
        }
    }

    void serialize(const MetaContainer &h, std::ostream &o) {
        pugi::xml_document doc;
        pugi::xml_node root = doc.append_child("ismrmrdMeta");

//        for (auto &node : h) {
        typedef std::map<std::string, std::vector<MetaValue> > map_t;
        for (map_t::const_iterator node = h.begin(); node != h.end(); ++node) {
            pugi::xml_node meta = root.append_child("meta");
            pugi::xml_node name = meta.append_child("name");
            name.append_child(pugi::node_pcdata).set_value(node->first.c_str());
            for (unsigned int i = 0; i < node->second.size(); i++) {
                pugi::xml_node cname = meta.append_child("value");
                cname.append_child(pugi::node_pcdata).set_value(node->second[i].as_str());
            }
        }
        doc.save(o);
    }

}