mirror of https://github.com/openXC7/prjxray.git
lib: accessor to get contents of a memory-mapped file as bytes
MemoryMappedFile's data() method returns a void* as it has no idea what type the contents are. Viewing it as bytes is a very common operation so add a convience method that wraps the pointer in a Span<uint8_t>. Signed-off-by: Rick Altherr <kc8apf@kc8apf.net>
This commit is contained in:
parent
5a155de8bc
commit
a4858be23a
|
|
@ -4,6 +4,8 @@
|
|||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <absl/types/span.h>
|
||||
|
||||
namespace prjxray {
|
||||
|
||||
class MemoryMappedFile {
|
||||
|
|
@ -16,6 +18,10 @@ class MemoryMappedFile {
|
|||
void* const data() const { return data_; }
|
||||
const size_t size() const { return size_; }
|
||||
|
||||
absl::Span<uint8_t> as_bytes() const {
|
||||
return {static_cast<uint8_t*>(data_), size_};
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryMappedFile(void *data, size_t size)
|
||||
: data_(data), size_(size) {};
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ class BitstreamReader {
|
|||
// Construct a `BitstreamReader` from a Container of bytes.
|
||||
// Any bytes preceding an initial sync word are ignored.
|
||||
template<typename T>
|
||||
static absl::optional<BitstreamReader> InitWithBytes(T &bitstream);
|
||||
static absl::optional<BitstreamReader> InitWithBytes(T bitstream);
|
||||
|
||||
const std::vector<uint32_t> &words() { return words_; };
|
||||
|
||||
|
|
@ -62,7 +62,7 @@ class BitstreamReader {
|
|||
};
|
||||
|
||||
template<typename T>
|
||||
absl::optional<BitstreamReader> BitstreamReader::InitWithBytes(T &bitstream) {
|
||||
absl::optional<BitstreamReader> BitstreamReader::InitWithBytes(T bitstream) {
|
||||
// If this is really a Xilinx 7-Series bitstream, there will be a sync
|
||||
// word somewhere toward the beginning.
|
||||
auto sync_pos = std::search(bitstream.begin(), bitstream.end(),
|
||||
|
|
|
|||
|
|
@ -78,10 +78,8 @@ int main(int argc, char **argv) {
|
|||
std::cout << "Bitstream size: " << in_file->size() << " bytes"
|
||||
<< std::endl;
|
||||
|
||||
auto in_bytes = absl::Span<uint8_t>(
|
||||
static_cast<uint8_t*>(in_file->data()),
|
||||
in_file->size());
|
||||
reader = xc7series::BitstreamReader::InitWithBytes(in_bytes);
|
||||
reader = xc7series::BitstreamReader::InitWithBytes(
|
||||
in_file->as_bytes());
|
||||
} else {
|
||||
std::vector<uint8_t> bitdata;
|
||||
while (1) {
|
||||
|
|
|
|||
|
|
@ -31,10 +31,8 @@ int ListConfigPackets(int argc, char *argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
auto in_bytes = absl::Span<uint8_t>(
|
||||
static_cast<uint8_t*>(in_file->data()),
|
||||
in_file->size());
|
||||
auto reader = xc7series::BitstreamReader::InitWithBytes(in_bytes);
|
||||
auto reader = xc7series::BitstreamReader::InitWithBytes(
|
||||
in_file->as_bytes());
|
||||
if (!reader) {
|
||||
std::cerr << "Input doesn't look like a bitstream"
|
||||
<< std::endl;
|
||||
|
|
|
|||
|
|
@ -31,10 +31,8 @@ int main(int argc, char *argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
auto in_bytes = absl::Span<uint8_t>(
|
||||
static_cast<uint8_t*>(in_file->data()),
|
||||
in_file->size());
|
||||
auto reader = xc7series::BitstreamReader::InitWithBytes(in_bytes);
|
||||
auto reader = xc7series::BitstreamReader::InitWithBytes(
|
||||
in_file->as_bytes());
|
||||
if (!reader) {
|
||||
std::cerr << "Input doesn't look like a bitstream"
|
||||
<< std::endl;
|
||||
|
|
|
|||
Loading…
Reference in New Issue