pflib v3.14.3-4-g3289bd3
Pretty Fine HGCROC Interaction Library
Loading...
Searching...
No Matches
intel_hex.h
1#pragma once
2#ifndef PFLIB_UTILITY_INTEL_HEX_H
3#define PFLIB_UTILITY_INTEL_HEX_H
4
5#include <cstdint>
6#include <cstdio>
7#include <fstream>
8#include <string>
9#include <type_traits>
10#include <vector>
11
12namespace pflib::utility {
13
32namespace intel_hex {
33
44 uint16_t addr_;
45
46 public:
48 void from_bytes(uint16_t addr, std::vector<uint8_t> data);
50 template <typename WordType,
51 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
52 void from(uint16_t addr, WordType value) {
53 addr_ = addr;
54 data_.resize(sizeof(WordType));
55 for (int i_byte{0}; i_byte < sizeof(WordType); i_byte++) {
56 data_[i_byte] = static_cast<uint8_t>(
57 (value >> 8 * (sizeof(WordType) - i_byte - 1)) & 0xff);
58 }
59 }
61 const std::vector<uint8_t>& get_bytes() const;
63 template <typename WordType,
64 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
65 WordType get() {
66 WordType word = 0;
67 for (int i_byte{0}; i_byte < get_bytes().size(); i_byte++) {
68 word |= (static_cast<WordType>(get_bytes().at(i_byte))
69 << 8 * (sizeof(WordType) - i_byte - 1));
70 }
71 return word;
72 }
74 const uint16_t& addr() const;
75};
76
93class Reader {
94 std::ifstream input_file_;
95 DataRecord current_record_;
96 std::size_t error_count_;
97
98 public:
99 Reader(const Reader&) = delete;
100 Reader& operator=(const Reader&) = delete;
101 ~Reader() = default;
102 Reader(const std::string& input_filepath);
103 std::size_t errors() const;
104 bool next();
105 const DataRecord& get();
106};
107
124class Writer {
125 FILE* output_file_;
126
127 public:
128 Writer(const Writer&) = delete;
129 Writer& operator=(const Writer&) = delete;
130 Writer(const std::string& output_filepath);
131 ~Writer();
132 void add(const DataRecord& data);
133 void close();
134};
135
136} // namespace intel_hex
137
138} // namespace pflib::utility
139
140#endif
T at(T... args)
a record of data from/to the IntelHex format
Definition intel_hex.h:40
void from(uint16_t addr, WordType value)
set contents of data record using width of integer type
Definition intel_hex.h:52
const uint16_t & addr() const
get address
Definition intel_hex.cxx:16
const std::vector< uint8_t > & get_bytes() const
get raw bytes of data
Definition intel_hex.cxx:14
void from_bytes(uint16_t addr, std::vector< uint8_t > data)
set contents of data record
Definition intel_hex.cxx:9
std::vector< uint8_t > data_
actual data in this record
Definition intel_hex.h:42
uint16_t addr_
address for the data
Definition intel_hex.h:44
WordType get()
get data in terms of integer type
Definition intel_hex.h:65
reader for the IntelHex format
Definition intel_hex.h:93
writer for the IntelHexFormat
Definition intel_hex.h:124
Dumping ground for various functions that are used in many places.
Definition crc.cxx:9
T resize(T... args)
T size(T... args)