MMTF-C++
The C++ language MMTF libraries
decoder.hpp
Go to the documentation of this file.
1// *************************************************************************
2//
3// Licensed under the MIT License (see accompanying LICENSE file).
4//
5// The authors of this code are: Gabriel Studer, Gerardo Tauriello
6//
7// Based on mmtf_c developed by Julien Ferte (http://www.julienferte.com/),
8// Anthony Bradley, Thomas Holder with contributions from Yana Valasatava,
9// Gazal Kalyan, Alexander Rose.
10//
11// *************************************************************************
12
13#ifndef MMTF_DECODER_H
14#define MMTF_DECODER_H
15
16#include "structure_data.hpp"
17#include "errors.hpp"
18#include "msgpack_decoder.hpp"
19
20#include <msgpack.hpp>
21#include <fstream>
22#include <sstream>
23#include <string>
24
25namespace mmtf {
26
34inline void decodeFromBuffer(StructureData& data, const char* buffer,
35 size_t size);
36
46template <typename Stream>
47inline void decodeFromStream(StructureData& data, Stream& stream);
48
55inline void decodeFromFile(StructureData& data, const std::string& filename);
56
57// *************************************************************************
58// IMPLEMENTATION
59// *************************************************************************
60
61inline void decodeFromBuffer(StructureData& data, const char* buffer,
62 size_t size) {
63 // load msgpack object and directly convert it
64 msgpack::unpacked upd;
65 msgpack::unpack(upd, buffer, size);
66 msgpack::object obj(upd.get());
67 obj.convert(data);
68}
69
70template <typename Stream>
71inline void decodeFromStream(StructureData& data, Stream& stream) {
72 // parse with stringstream
73 std::stringstream buffer;
74 buffer << stream.rdbuf();
75 decodeFromBuffer(data, buffer.str().data(), buffer.str().size());
76}
77
78inline void decodeFromFile(StructureData& data, const std::string& filename) {
79 // read file, ios::binary is required for windows
80 std::ifstream ifs(filename.c_str(), std::ifstream::in | std::ios::binary);
81 if (!ifs.is_open()) {
82 throw DecodeError("Could not open file: " + filename);
83 }
84 decodeFromStream(data, ifs);
85}
86
87} // mmtf namespace
88
89#endif
Exception thrown when failing during decoding.
Definition: errors.hpp:23
Definition: binary_decoder.hpp:24
void decodeFromBuffer(StructureData &data, const char *buffer, size_t size)
Decode an MMTF data structure from a byte buffer.
Definition: decoder.hpp:61
void decodeFromFile(StructureData &data, const std::string &filename)
Decode an MMTF data structure from an existing file.
Definition: decoder.hpp:78
void decodeFromStream(StructureData &data, Stream &stream)
Decode an MMTF data structure from a stream.
Definition: decoder.hpp:71
Top level MMTF data container.
Definition: structure_data.hpp:151