46 inline void check_fortran_4byte(std::basic_stringstream<byte> &buffer,
i32 fortran_byte) {
49 shambase::stream_read(buffer, new_check);
51 if (new_check != fortran_byte) {
55 fortran_byte = new_check;
69 inline i32 read_fortran_4byte(std::basic_stringstream<byte> &buffer) {
71 shambase::stream_read(buffer, check);
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]);
145 : data(std::forward<std::basic_stringstream<
byte>>(data_in)), length(length) {
175 template<
class... Args>
177 i32 linebytecount = ((
sizeof(args)) + ...);
178 stream_write(data, linebytecount);
179 ((_write(args)), ...);
180 stream_write(data, linebytecount);
194 template<
class... Args>
195 inline void read(Args &...args) {
196 u64 linebytecount = ((
sizeof(args)) + ...);
197 i32 check = read_fortran_4byte(data);
198 if (check != linebytecount) {
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);
257 u64 totlen = strlen * str_count;
258 i32 check = read_fortran_4byte(data);
259 if (check != totlen) {
263 svec.resize(str_count);
265 for (
u32 i = 0; i < str_count; i++) {
266 svec[i].resize(strlen);
267 data.read(
reinterpret_cast<byte *
>(svec[i].data()), strlen *
sizeof(
char));
270 check_fortran_4byte(data, check);
287 i32 totlen = strlen * str_count;
289 stream_write(data, totlen);
291 for (
u32 i = 0; i < str_count; i++) {
293 data.write(
reinterpret_cast<byte *
>(svec[i].data()), strlen *
sizeof(
char));
296 stream_write(data, totlen);
314 u64 totlen =
sizeof(T) * val_count;
315 i32 check = read_fortran_4byte(data);
317 if (check != totlen) {
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");
351 i32 totlen =
sizeof(T) * val_count;
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;
409 buffer << in_f.rdbuf();
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.
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.