MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
binaryio.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, 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/// Write 'value' to stream.
29template<typename T>
30inline void write(std::ostream& os, T value)
31{
32 os.write((char*) &value, sizeof(T));
33}
34
35/// Read a value from the stream and return it.
36template<typename T>
37inline T read(std::istream& is)
38{
39 T value;
40 is.read((char*) &value, sizeof(T));
41 return value;
42}
43
44/// Read a value of type @a T from a binary buffer and return it.
45template <typename T>
46inline T read(const char *buf)
47{
48 T value;
49 std::copy(buf, buf + sizeof(T), reinterpret_cast<char*>(&value));
50 return value;
51}
52
53/// Append the binary representation of @a val to the byte buffer @a vec.
54template <typename T>
55void AppendBytes(std::vector<char> &vec, const T &val)
56{
57 const char *ptr = reinterpret_cast<const char*>(&val);
58 vec.insert(vec.end(), ptr, ptr + sizeof(T));
59}
60
61/// @brief Given a buffer @a bytes of length @a nbytes, encode the data in
62/// base-64 format, and write the encoded data to the output stream @a out.
63void WriteBase64(std::ostream &out, const void *bytes, size_t nbytes);
64
65/// @brief Decode @a len base-64 encoded characters in the buffer @a src, and
66/// store the resulting decoded data in @a buf. @a buf will be resized as
67/// needed.
68void DecodeBase64(const char *src, size_t len, std::vector<char> &buf);
69
70/// @brief Return the number of characters needed to encode @a nbytes in
71/// base-64.
72///
73/// This is equal to 4*nbytes/3, rounded up to the nearest multiple of 4.
74size_t NumBase64Chars(size_t nbytes);
75
76} // namespace mfem::bin_io
77
78} // namespace mfem
79
80#endif
void write(std::ostream &os, T value)
Write 'value' to stream.
Definition binaryio.hpp:30
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
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:37
void AppendBytes(std::vector< char > &vec, const T &val)
Append the binary representation of val to the byte buffer vec.
Definition binaryio.hpp:55
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