69 inline i32 read_fortran_4byte(std::basic_stringstream<byte> &
buffer) {
76 std::basic_stringstream<byte> data;
88 inline void _write(T
arg) {
89 stream_write(data,
arg);
99 inline void _read(T &
arg) {
100 stream_read(data,
arg);
110 template<
class T,
int N>
111 inline void _read(std::array<T, N> &vec) {
112 for (
u32 i = 0;
i < N;
i++) {
113 stream_read(data, vec[
i]);
124 template<
class T,
int N>
125 inline void _write(std::array<T, N> &vec) {
126 for (
u32 i = 0;
i < N;
i++) {
127 stream_write(data, vec[
i]);
175 template<
class...
Args>
179 ((_write(args)), ...);
194 template<
class...
Args>
197 i32 check = read_fortran_4byte(data);
201 ((_read(args)), ...);
202 check_fortran_4byte(data,
check);
218 i32 check = read_fortran_4byte(data);
222 data.read(
reinterpret_cast<byte *
>(s.data()),
len *
sizeof(
char));
223 check_fortran_4byte(data,
check);
238 stream_write(data,
len);
239 data.write(
reinterpret_cast<byte *
>(s.data()),
len *
sizeof(
char));
240 stream_write(data,
len);
258 i32 check = read_fortran_4byte(data);
267 data.read(
reinterpret_cast<byte *
>(
svec[
i].data()),
strlen *
sizeof(
char));
270 check_fortran_4byte(data,
check);
289 stream_write(data,
totlen);
293 data.write(
reinterpret_cast<byte *
>(
svec[
i].data()),
strlen *
sizeof(
char));
296 stream_write(data,
totlen);
315 i32 check = read_fortran_4byte(data);
319 "the byte count is not correct");
322 vec.resize(val_count);
324 for (
u32 i = 0;
i < val_count;
i++) {
325 stream_read(data, vec[
i]);
328 check_fortran_4byte(data,
check);
347 if (val_count > vec.size()) {
349 "val count is higher than vec size");
352 stream_write(data,
totlen);
354 for (
u32 i = 0;
i < val_count;
i++) {
355 stream_write(data, vec[
i]);
358 stream_write(data,
totlen);
379 std::ofstream
out_f(fname, std::ios::binary);
383 out_f << data.rdbuf();
390 "unimplemented case : could not open file " + fname +
" for writing");
405 std::ifstream
in_f(fname, std::ios::binary);
407 std::basic_stringstream<byte>
buffer;
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
char byte
byte type similar to std::byte
std::int32_t i32
32 bit integer
Class for reading and writing Fortran-style binary files.
void write(Args &...args)
Write a list of arguments to the internal buffer.
void write_string_array(std::vector< std::string > &svec, u32 strlen, u32 str_count)
Write a fixed-length string array to the buffer.
void write_val_array(std::vector< T > &vec, u32 val_count)
Write an array of values to the buffer.
void read_fixed_string(std::string &s, u32 len)
Read a fixed-length string from the buffer.
int fort_int
Fortran int type.
void read(Args &...args)
Read a list of arguments from the internal buffer.
void read_string_array(std::vector< std::string > &svec, u32 strlen, u32 str_count)
Read a fixed-length string array from the buffer.
void read_val_array(std::vector< T > &vec, u32 val_count)
Read an array of values from the buffer.
void write_fixed_string(std::string &s, u32 len)
Write a fixed-length string to the buffer.
std::basic_stringstream< byte > & get_internal_buf()
Get a reference to the internal buffer.
f64 fort_real
Fortran real type.
FortranIOFile(std::basic_stringstream< byte > &&data_in, u64 length)
Construct a new FortranIOFile object.
bool finished_read()
Check if the end of the file has been reached.
void write_to_file(std::string fname)
Write the Fortran formatted file to disk.
This header file contains utility functions related to exception handling in the code.
namespace for basic c++ utilities
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
FortranIOFile load_fortran_file(const std::string &fname)
Load a Fortran formatted file from disk.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.