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_decoders.hpp"
19 #include "map_decoder.hpp"
20 
21 #include <msgpack.hpp>
22 #include <fstream>
23 #include <sstream>
24 #include <string>
25 
26 namespace mmtf {
27 
34 inline void decodeFromMapDecoder(StructureData& data, MapDecoder& mapDecoder);
35 
43 inline void decodeFromBuffer(StructureData& data, const char* buffer,
44  size_t size);
45 
57 template <typename Stream>
58 inline void decodeFromStream(StructureData& data, Stream& stream);
59 
66 inline void decodeFromFile(StructureData& data, const std::string& filename);
67 
75 inline void mapDecoderFromBuffer(MapDecoder& mapDecoder, const char* buffer,
76  std::size_t size);
77 
85 template <typename Stream>
86 inline void mapDecoderFromStream(MapDecoder& mapDecoder, Stream& stream);
87 
95 inline void mapDecoderFromFile(MapDecoder& mapDecoder,
96  const std::string& filename);
97 
98 // *************************************************************************
99 // IMPLEMENTATION
100 // *************************************************************************
101 
104 }
105 
106 inline void decodeFromBuffer(StructureData& data, const char* buffer,
107  size_t size) {
108  MapDecoder md;
109  mapDecoderFromBuffer(md, buffer, size);
110  decodeFromMapDecoder(data, md);
111 }
112 
113 template <typename Stream>
114 inline void decodeFromStream(StructureData& data, Stream& stream) {
115  MapDecoder md;
116  mapDecoderFromStream(md, stream);
117  decodeFromMapDecoder(data, md);
118 }
119 
120 inline void decodeFromFile(StructureData& data, const std::string& filename) {
121  MapDecoder md;
122  mapDecoderFromFile(md, filename);
123  decodeFromMapDecoder(data, md);
124 }
125 
126 inline void mapDecoderFromBuffer(MapDecoder& mapDecoder, const char* buffer,
127  std::size_t size) {
128  mapDecoder.initFromBuffer(buffer, size);
129 }
130 
131 template <typename Stream>
132 inline void mapDecoderFromStream(MapDecoder& mapDecoder, Stream& stream) {
133  // parse straight into string buffer
134  std::string buffer;
135  stream.seekg(0, std::ios::end);
136  buffer.resize(stream.tellg());
137  stream.seekg(0, std::ios::beg);
138  if (!buffer.empty()) stream.read(&buffer[0], buffer.size());
139  mapDecoderFromBuffer(mapDecoder, buffer.data(), buffer.size());
140 }
141 
142 inline void mapDecoderFromFile(MapDecoder& mapDecoder,
143  const std::string& filename) {
144  // read file as binary
145  std::ifstream ifs(filename.c_str(), std::ifstream::in | std::ios::binary);
146  if (!ifs.is_open()) {
147  throw DecodeError("Could not open file: " + filename);
148  }
149  mapDecoderFromStream(mapDecoder, ifs);
150 }
151 
152 } // mmtf namespace
153 
154 #endif
void decodeFromBuffer(StructureData &data, const char *buffer, size_t size)
Decode an MMTF data structure from a byte buffer.
Definition: decoder.hpp:106
Helper class to decode msgpack maps into object fields. Class cannot be copied as it contains unique ...
Definition: map_decoder.hpp:31
Exception thrown when failing during decoding.
Definition: errors.hpp:23
void initFromBuffer(const char *buffer, size_t size)
Initialize from byte buffer of given size. Unpacks data and then same effect as MapDecoder::initFromO...
Definition: map_decoder.hpp:165
void mapDecoderFromBuffer(MapDecoder &mapDecoder, const char *buffer, std::size_t size)
Get a mapDecoder for un-decoded MMTF data.
Definition: decoder.hpp:126
void decodeFromStream(StructureData &data, Stream &stream)
Decode an MMTF data structure from a stream.
Definition: decoder.hpp:114
Top level MMTF data container.
Definition: structure_data.hpp:157
Definition: binary_decoder.hpp:25
void mapDecoderFromStream(MapDecoder &mapDecoder, Stream &stream)
Get a mapDecoder into an un-decoded MMTF data.
Definition: decoder.hpp:132
void mapDecoderFromFile(MapDecoder &mapDecoder, const std::string &filename)
Get a mapDecoder into an un-decoded MMTF data.
Definition: decoder.hpp:142
void decodeFromFile(StructureData &data, const std::string &filename)
Decode an MMTF data structure from an existing file.
Definition: decoder.hpp:120
void decodeFromMapDecoder(StructureData &data, MapDecoder &mapDecoder)
Decode an MMTF data structure from a mapDecoder.
Definition: decoder.hpp:102