Coverage Report

Created: 2026-10-19 14:15

src/zserio/FileUtil.h
Line
Count
Source
1
/**
2
 * \file
3
 * File utilities.
4
 *
5
 * These utilities are not used by generated code and they are provided only for user convenience.
6
 *
7
 * \note Please note that file operations allocate memory as needed and are not designed to use allocators.
8
 */
9
10
#ifndef ZSERIO_FILE_UTIL_H_INC
11
#define ZSERIO_FILE_UTIL_H_INC
12
13
#include "zserio/BitBuffer.h"
14
#include "zserio/BitStreamReader.h"
15
#include "zserio/BitStreamWriter.h"
16
#include "zserio/Types.h"
17
18
namespace zserio
19
{
20
21
/**
22
 * Read file to bit buffer object.
23
 *
24
 * \param fileName File to read.
25
 *
26
 * \return Bit buffer representing the file contents.
27
 *
28
 * \throw CppRuntimeException When reading fails.
29
 */
30
BitBuffer readBufferFromFile(const std::string& fileName);
31
32
/**
33
 * Writes given buffer to file.
34
 *
35
 * \param buffer Buffer to write.
36
 * \param bitSize Buffer bit size.
37
 * \param fileName Name of the file to write.
38
 *
39
 * \throw CppRuntimeException When writing fails.
40
 */
41
void writeBufferToFile(const uint8_t* buffer, size_t bitSize, BitsTag, const std::string& fileName);
42
43
/**
44
 * Writes given buffer to file.
45
 *
46
 * Overloaded function provided for convenience.
47
 *
48
 * \param buffer Buffer to write.
49
 * \param byteSize Buffer byte size.
50
 * \param fileName Name of the file to write.
51
 *
52
 * \throw CppRuntimeException When writing fails.
53
 */
54
inline void writeBufferToFile(const uint8_t* buffer, size_t byteSize, const std::string& fileName)
55
1
{
56
1
    writeBufferToFile(buffer, byteSize * 8, BitsTag(), fileName);
57
1
}
58
59
/**
60
 * Writes given bit buffer to file.
61
 *
62
 * Overloaded function provided for convenience.
63
 *
64
 * \param bitBuffer Bit buffer to write.
65
 * \param fileName Name of the file to write.
66
 *
67
 * \throw CppRuntimeException When writing fails.
68
 */
69
template <typename ALLOC>
70
inline void writeBufferToFile(const BasicBitBuffer<ALLOC>& bitBuffer, const std::string& fileName)
71
4
{
72
4
    writeBufferToFile(bitBuffer.getBuffer(), bitBuffer.getBitSize(), BitsTag(), fileName);
73
4
}
74
75
/**
76
 * Writes write-buffer of the given bit stream writer to file.
77
 *
78
 * Overloaded function provided for convenience.
79
 *
80
 * \param writer Bit stream writer to use.
81
 * \param fileName Name of the file to write.
82
 *
83
 * \throw CppRuntimeException When writing fails.
84
 */
85
inline void writeBufferToFile(const BitStreamWriter& writer, const std::string& fileName)
86
1
{
87
1
    writeBufferToFile(writer.getWriteBuffer(), writer.getBitPosition(), BitsTag(), fileName);
88
1
}
89
90
} // namespace zserio
91
92
#endif // ZSERIO_FILE_UTIL_H_INC