SatCat5
file_io.h
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // File I/O wrappers
6 
7 #pragma once
8 
9 #include <cstdio>
10 #include <string>
11 #include <satcat5/io_core.h>
12 
13 namespace satcat5 {
14  namespace io {
17  public:
22  explicit FileWriter(
23  const char* filename = 0,
24  bool close_on_finalize = false);
25  virtual ~FileWriter();
26 
29  void open(const char* filename);
30  void close();
31 
33  void seek(unsigned offset);
34 
35  // Required and optional function overrides.
36  unsigned get_write_space() const override;
37  void write_bytes(unsigned nbytes, const void* src);
38  bool write_finalize() override;
39  void write_abort() override;
40  protected:
41  void write_next(u8 data) override;
42  const bool m_close_on_finalize;
43  FILE* m_file; // Current file object
44  unsigned m_last_commit; // Position of last commit
45  };
46 
49  public:
54  explicit FileReader(
55  const char* filename = 0,
56  bool close_on_finalize = false);
57  virtual ~FileReader();
58 
63  void open(const char* filename, unsigned len = 0);
64  void close();
65 
66  // Required and optional function overrides.
67  unsigned get_read_ready() const override;
68  bool read_bytes(unsigned nbytes, void* dst);
69  bool read_consume(unsigned nbytes);
70  void read_finalize() override;
71  protected:
72  u8 read_next() override;
73  const bool m_close_on_finalize;
74  FILE* m_file; // Current file object
75  unsigned m_rem; // Remaining readable bytes
76  };
77  }
78 }
Read bytes or packets from a file.
Definition: file_io.h:48
FileReader(const char *filename=0, bool close_on_finalize=false)
Create the FileWriter object.
Definition: file_io.cc:89
void read_finalize() override
Consume any remaining bytes in this frame, if applicable.
Definition: file_io.cc:153
void open(const char *filename, unsigned len=0)
Open the specified file to read the next frame.
Definition: file_io.cc:103
bool read_bytes(unsigned nbytes, void *dst)
Read 0 or more bytes into a buffer.
Definition: file_io.cc:132
unsigned get_read_ready() const override
How many bytes can be read without blocking?
Definition: file_io.cc:127
bool read_consume(unsigned nbytes)
Read and discard 0 or more bytes.
Definition: file_io.cc:142
u8 read_next() override
Read the next byte from the underlying buffer or device.
Definition: file_io.cc:159
Write bytes or packets to a file.
Definition: file_io.h:16
unsigned get_write_space() const override
How many bytes can be written without blocking?
Definition: file_io.cc:53
void write_bytes(unsigned nbytes, const void *src)
Write 0 or more bytes from a buffer.
Definition: file_io.cc:59
void write_abort() override
If possible, abort the current partially-written packet.
Definition: file_io.cc:72
FileWriter(const char *filename=0, bool close_on_finalize=false)
Create the FileWriter object.
Definition: file_io.cc:19
bool write_finalize() override
Mark end of frame and release temporary working data.
Definition: file_io.cc:64
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
Definition: file_io.cc:84
void open(const char *filename)
Open the specified file.
Definition: file_io.cc:33
void seek(unsigned offset)
Move write cursor to the specified absolute offset.
Definition: file_io.cc:48
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
I/O interface core definitions.