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 
25 namespace mmtf {
26 
34 inline void decodeFromBuffer(StructureData& data, const char* buffer,
35  size_t size);
36 
46 template <typename Stream>
47 inline void decodeFromStream(StructureData& data, Stream& stream);
48 
55 inline void decodeFromFile(StructureData& data, const std::string& filename);
56 
57 // *************************************************************************
58 // IMPLEMENTATION
59 // *************************************************************************
60 
61 inline 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 
70 template <typename Stream>
71 inline 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 
78 inline 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
mmtf::StructureData
Top level MMTF data container.
Definition: structure_data.hpp:151
errors.hpp
mmtf::decodeFromStream
void decodeFromStream(StructureData &data, Stream &stream)
Decode an MMTF data structure from a stream.
Definition: decoder.hpp:71
structure_data.hpp
mmtf::DecodeError
Exception thrown when failing during decoding.
Definition: errors.hpp:23
mmtf::decodeFromFile
void decodeFromFile(StructureData &data, const std::string &filename)
Decode an MMTF data structure from an existing file.
Definition: decoder.hpp:78
mmtf::decodeFromBuffer
void decodeFromBuffer(StructureData &data, const char *buffer, size_t size)
Decode an MMTF data structure from a byte buffer.
Definition: decoder.hpp:61
mmtf
Definition: binary_decoder.hpp:24