MFEM  v4.3.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
binaryio.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2021, 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 
20 namespace mfem
21 {
22 
23 // binary I/O helpers
24 
25 namespace bin_io
26 {
27 
28 /// Write 'value' to stream.
29 template<typename T>
30 inline 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.
36 template<typename T>
37 inline 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.
45 template <typename T>
46 inline 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.
54 template <typename T>
55 void 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 /// Given a buffer @a buf of length @a nbytes, encode the data in base-64
62 /// format, and write the encoded data to the output stream @a out.
63 void WriteBase64(std::ostream &out, const void *bytes, size_t nbytes);
64 
65 /// Decode @a len base-64 encoded characters in the buffer @a src, and store the
66 /// resulting decoded data in @a buf. @a buf will be resized as needed.
67 void DecodeBase64(const char *src, size_t len, std::vector<char> &buf);
68 
69 /// Return the number of characters needed to encode @a nbytes in base-64. This
70 /// is equal to 4*nbytes/3, rounded up to the nearest multiple of 4.
71 size_t NumBase64Chars(size_t nbytes);
72 
73 } // namespace mfem::bin_io
74 
75 } // namespace mfem
76 
77 #endif
void DecodeBase64(const char *src, size_t len, std::vector< char > &buf)
Definition: binaryio.cpp:74
void write(std::ostream &os, T value)
Write &#39;value&#39; to stream.
Definition: binaryio.hpp:30
void WriteBase64(std::ostream &out, const void *bytes, size_t nbytes)
Definition: binaryio.cpp:25
T read(std::istream &is)
Read a value from the stream and return it.
Definition: binaryio.hpp:37
size_t NumBase64Chars(size_t nbytes)
Definition: binaryio.cpp:103
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