-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhdf5_io.h
More file actions
40 lines (33 loc) · 1.52 KB
/
Copy pathhdf5_io.h
File metadata and controls
40 lines (33 loc) · 1.52 KB
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
#pragma once
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
#include "hdf5_types.h"
namespace hdf5_reader {
namespace detail {
// ---- Little-endian helpers ----
inline uint16_t r16(const uint8_t* p) { uint16_t v; memcpy(&v, p, 2); return v; }
inline uint32_t r32(const uint8_t* p) { uint32_t v; memcpy(&v, p, 4); return v; }
inline uint64_t r64(const uint8_t* p) { uint64_t v; memcpy(&v, p, 8); return v; }
inline uint8_t frd8 (std::ifstream& f) { uint8_t v = 0; f.read((char*)&v, 1); return v; }
inline uint16_t frd16(std::ifstream& f) { uint16_t v = 0; f.read((char*)&v, 2); return v; }
inline uint32_t frd32(std::ifstream& f) { uint32_t v = 0; f.read((char*)&v, 4); return v; }
inline uint64_t frd64(std::ifstream& f) { uint64_t v = 0; f.read((char*)&v, 8); return v; }
inline void fskip(std::ifstream& f, std::streamoff n) { f.seekg(n, std::ios::cur); }
inline void fseek(std::ifstream& f, uint64_t pos) {
f.seekg((std::streamoff)pos, std::ios::beg);
if (!f.good()) throw std::runtime_error(
"hdf5_reader: seek failed at " + std::to_string(pos));
}
// ---- Read null-terminated string from heap ----
inline std::string heap_str(const std::vector<uint8_t>& heap, uint64_t off) {
if (off >= heap.size()) throw std::runtime_error(
"hdf5_reader: heap offset " + std::to_string(off) + " out of range");
return std::string(reinterpret_cast<const char*>(heap.data() + off));
}
} // namespace detail
} // namespace hdf5_reader