LibCppBinary is a cross-platform library for reading and writing binary data through strongly typed fields, structures, and streams.
It provides a small set of composable primitives that make binary file work predictable and explicit:
- Field types for raw bytes, strings, and integers of multiple sizes
- Stream abstractions for files and in-memory buffers
- Data structure support for ordered field serialization
- Chunk-header support for tagged binary formats
- String formatting helpers for text output (raw, printable, bin, hex, dec)
Features
- DataField base abstraction for binary field types
- RawField for fixed-size byte data
- StringField for fixed-size text fields
- IntField template with specializations:
- UInt8Field, Int8Field
- UInt16Field, Int16Field
- UInt24Field, Int24Field
- UInt32Field, Int32Field
- UInt64Field, Int64Field
- Configurable integer endianness (FieldEndianness)
- Stream interface for position-based I/O
- StandardFileStream for binary file I/O
- BufferStream for in-memory binary I/O
- DataStructure interface for grouping ordered fields
- ChunkHeader structure (id + dataSize) and chunk scanning helpers
Requirements
- C++17 or greater compatible compiler
- CMake 3.11 or greater
Test dependencies are fetched automatically by CMake:
- GoogleTest / GoogleMock via FetchContent
Build
From the repository root:
cmake -S . -B build
cmake --build build
The static library target is LibCppBinary.
Run Tests
ctest --test-dir build --output-on-failure
Or run the test binary directly:
./build/bin/libcppbinarytests
Generate API Documentation
Doxygen configuration is included.
Generated docs are written under docs/.
Quick Start
Include the Library
Example: Define a Binary Record Structure
{
std::vector<Binary::DataField*>
Fields()
override
{
return { &magic, &version };
}
std::vector<const Binary::DataField*>
Fields()
const override
{
return { &magic, &version };
}
size_t Size()
const override
{
}
};
size_t Size() const override
Gets the size of the data in the field.
Definition RawField.h:58
Represents a string field in a binary file.
Definition StringField.h:33
IntField< uint32_t, 4 > UInt32Field
Represents an unsigned 32-bit integer field in a binary file.
Definition IntField.h:519
Abstract base struct representing data structure in binary file.
Definition DataStructure.h:42
virtual size_t Size() const =0
Gets the total size of all fields in the structure.
virtual std::vector< DataField * > Fields()=0
Provides a vector of raw pointers to the structure's fields.
Example: Write and Read a Record from a File
int main()
{
ExampleRecord out;
out.magic.SetValue("LBIN");
out.version.SetValue(1);
ExampleRecord in;
return 0;
}
Provides a standard file stream for manipulating binary files.
Definition StandardFileStream.h:43
virtual void Open(FileMode mode) override
Opens the file in the specified mode.
virtual void Write(const DataField *field) override
Writes data to the stream from the specified field.
virtual void Read(DataField *field) const override
Reads data from the stream into the specified field.
virtual void Close() override
Closes the file.
Definition StandardFileStream.h:77
@ Write
File should be opened for writing.
Definition FileMode.h:29
@ Read
File should be opened for reading.
Definition FileMode.h:26
Example: Work with In-Memory Binary Data
int main()
{
return 0;
}
Represents a memory buffer that can be used as a stream.
Definition BufferStream.h:39
void Read(DataField *field) const override
Reads data from the stream into the specified field.
void Write(const DataField *field) override
Writes data to the stream from the specified field.
void SetPosition(size_t position) const override
Sets the current position in the stream.
void SetValue(IntType value)
Sets the value of the field.
Definition IntField.h:213
IntField< uint16_t, 2 > UInt16Field
Represents an unsigned 16-bit integer field in a binary file.
Definition IntField.h:513
CMake Integration
If LibCppBinary is included as a subdirectory in another CMake project:
add_subdirectory(path/to/LibCppBinary)
target_link_libraries(your_target PRIVATE LibCppBinary)
Repository Layout
- include/ public headers
- src/ library implementation and library target definition
- tests/ GoogleTest suite
- docs/ requirements and generated Doxygen output
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.