MFEM v4.10.0
Finite element discretization library
Loading...
Searching...
No Matches
binaryio.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2026, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_BINARYIO
13#define MFEM_BINARYIO
14
15#include "../config/config.hpp"
16
17#include <iostream>
18#include <vector>
19
20namespace mfem
21{
22
23// binary I/O helpers
24
25namespace bin_io
26{
27
28/// Enum to specify if values should be read in binary or ASCII format.
29enum BinaryOrASCII : bool
30{
31 ASCII = false,
32 BINARY = true
33};
34
35/// Write 'value' to stream.
36template<typename T>
37inline void write(std::ostream &os, T value)
38{
39 os.write((char*) &value, sizeof(T));
40}
41
42/// Read a value from the stream and return it.
43template<typename T>
44inline T read(std::istream &is)
45{
46 T value;
47 is.read((char*) &value, sizeof(T));
48 return value;
49}
50
51/// Read a value of type @a T from a binary buffer and return it.
52template <typename T>
53inline T read(const char *buf)
54{
55 T value;
56 std::copy(buf, buf + sizeof(T), reinterpret_cast<char*>(&value));
57 return value;
58}
59
60/// Append the binary representation of @a val to the byte buffer @a vec.
61template <typename T>
62void AppendBytes(std::vector<char> &vec, const T &val)
63{
64 const char *ptr = reinterpret_cast<const char*>(&val);
65 vec.insert(vec.end(), ptr, ptr + sizeof(T));
66}
67
68/// @brief Given a buffer @a bytes of length @a nbytes, encode the data in
69/// base-64 format, and write the encoded data to the output stream @a out.
70void WriteBase64(std::ostream &out, const void *bytes, size_t nbytes);
71
72/// @brief Decode @a len base-64 encoded characters in the buffer @a src, and
73/// store the resulting decoded data in @a buf. @a buf will be resized as
74/// needed.
75void DecodeBase64(const char *src, size_t len, std::vector<char> &buf);
76
77/// @brief Return the number of characters needed to encode @a nbytes in
78/// base-64.
79///
80/// This is equal to 4*nbytes/3, rounded up to the nearest multiple of 4.
81size_t NumBase64Chars(size_t nbytes);
82
83/// @brief Read and return a value of type @a T from the input stream, in either
84/// binary or ASCII format, depending on the value of @a binary.
85template <typename T>
86T ReadBinaryOrASCII(std::istream &input, BinaryOrASCII binary)
87{
88 if (binary)
89 {
90 return read<T>(input);
91 }
92 else
93 {
94 T val;
95 input >> val;
96 return val;
97 }
98}
99
100/// @brief Skip @a num values of type @a T from the input stream, in either
101/// binary or ASCII format, depending on the value of @a binary.
102template <typename T>
103void Skip(std::istream &input, int num, BinaryOrASCII binary)
104{
105 if (binary)
106 {
107 input.ignore(sizeof(T) * num);
108 }
109 else
110 {
111 for (int i = 0; i < num; ++i) { ReadBinaryOrASCII<T>(input, ASCII); }
112 }
113}
114
115} // namespace mfem::bin_io
116
117} // namespace mfem
118
119#endif
void write(std::ostream &os, T value)
Write 'value' to stream.
Definition binaryio.hpp:37
void Skip(std::istream &input, int num, BinaryOrASCII binary)
Skip num values of type T from the input stream, in either binary or ASCII format,...
Definition binaryio.hpp:103
void WriteBase64(std::ostream &out, const void *bytes, size_t nbytes)
Given a buffer bytes of length nbytes, encode the data in base-64 format, and write the encoded data ...
Definition binaryio.cpp:25
BinaryOrASCII
Enum to specify if values should be read in binary or ASCII format.
Definition binaryio.hpp:30
T ReadBinaryOrASCII(std::istream &input, BinaryOrASCII binary)
Read and return a value of type T from the input stream, in either binary or ASCII format,...
Definition binaryio.hpp:86
void DecodeBase64(const char *src, size_t len, std::vector< char > &buf)
Decode len base-64 encoded characters in the buffer src, and store the resulting decoded data in buf....
Definition binaryio.cpp:74
size_t NumBase64Chars(size_t nbytes)
Return the number of characters needed to encode nbytes in base-64.
Definition binaryio.cpp:103
T read(std::istream &is)
Read a value from the stream and return it.
Definition binaryio.hpp:44
void AppendBytes(std::vector< char > &vec, const T &val)
Append the binary representation of val to the byte buffer vec.
Definition binaryio.hpp:62
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition globals.hpp:66