libdballe  8.6
core/file.h
1 #ifndef DBA_CORE_FILE_H
2 #define DBA_CORE_FILE_H
3 
4 #include <dballe/file.h>
5 #include <dballe/core/defs.h>
6 #include <memory>
7 #include <string>
8 #include <cstdio>
9 #include <functional>
10 
11 namespace dballe {
12 namespace core {
13 
15 class File : public dballe::File
16 {
17 protected:
19  std::string m_name;
21  FILE* fd;
25  int idx;
26 
27 public:
28  File(const std::string& name, FILE* fd, bool close_on_exit=true);
29  virtual ~File();
30 
31  std::string pathname() const override { return m_name; }
32  void close() override;
33  bool foreach(std::function<bool(const BinaryMessage&)> dest) override;
34 
40  static std::string resolve_test_data_file(const std::string& name);
41 
47  static std::unique_ptr<dballe::File> open_test_data_file(Encoding type, const std::string& name);
48 };
49 
51 {
52 public:
53  BufrFile(const std::string& name, FILE* fd, bool close_on_exit=true)
54  : File(name, fd, close_on_exit) {}
55 
56  Encoding encoding() const override { return Encoding::BUFR; }
57  BinaryMessage read() override;
58  void write(const std::string& msg) override;
59 };
60 
62 {
63 public:
64  CrexFile(const std::string& name, FILE* fd, bool close_on_exit=true)
65  : File(name, fd, close_on_exit) {}
66 
67  Encoding encoding() const override { return Encoding::CREX; }
68  BinaryMessage read() override;
69  void write(const std::string& msg) override;
70 };
71 
72 }
73 }
74 #endif
dballe::core::File
Base for dballe::File implementations.
Definition: core/file.h:15
dballe::File
File object for doing I/O on binary message streams.
Definition: file.h:17
dballe::core::CrexFile
Definition: core/file.h:61
dballe::core::BufrFile::encoding
Encoding encoding() const override
Get the file encoding.
Definition: core/file.h:56
dballe::core::File::idx
int idx
Index of the last message read from the file or written to the file.
Definition: core/file.h:25
dballe::core::File::close_on_exit
bool close_on_exit
True if fd should be closed on destruction.
Definition: core/file.h:23
dballe::core::BufrFile::write
void write(const std::string &msg) override
Append the binary message to the file.
dballe::core::CrexFile::write
void write(const std::string &msg) override
Append the binary message to the file.
dballe::core::File::fd
FILE * fd
FILE structure used to read or write to the file.
Definition: core/file.h:21
defs.h
dballe::core::File::m_name
std::string m_name
Name of the file.
Definition: core/file.h:19
dballe::core::CrexFile::read
BinaryMessage read() override
Read a message from the file.
dballe::core::File::open_test_data_file
static std::unique_ptr< dballe::File > open_test_data_file(Encoding type, const std::string &name)
Open a test data file.
dballe::core::File::close
void close() override
Close the underlying file.
dballe::core::BufrFile::read
BinaryMessage read() override
Read a message from the file.
dballe::core::CrexFile::encoding
Encoding encoding() const override
Get the file encoding.
Definition: core/file.h:67
dballe::BinaryMessage
Binary message.
Definition: file.h:130
dballe::core::File::resolve_test_data_file
static std::string resolve_test_data_file(const std::string &name)
Resolve the location of a test data file.
dballe::core::BufrFile
Definition: core/file.h:50
dballe::core::File::pathname
std::string pathname() const override
Get the file pathname.
Definition: core/file.h:31