libdballe  7.29
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  bool foreach(std::function<bool(const BinaryMessage&)> dest) override;
33 
39  static std::string resolve_test_data_file(const std::string& name);
40 
46  static std::unique_ptr<dballe::File> open_test_data_file(Encoding type, const std::string& name);
47 };
48 
50 {
51 public:
52  BufrFile(const std::string& name, FILE* fd, bool close_on_exit=true)
53  : File(name, fd, close_on_exit) {}
54 
55  Encoding encoding() const override { return BUFR; }
56  BinaryMessage read() override;
57  void write(const std::string& msg) override;
58 };
59 
61 {
62 public:
63  CrexFile(const std::string& name, FILE* fd, bool close_on_exit=true)
64  : File(name, fd, close_on_exit) {}
65 
66  Encoding encoding() const override { return CREX; }
67  BinaryMessage read() override;
68  void write(const std::string& msg) override;
69 };
70 
71 }
72 }
73 #endif
Encoding
Supported encodings.
Definition: file.h:21
static std::unique_ptr< dballe::File > open_test_data_file(Encoding type, const std::string &name)
Open a test data file.
void write(const std::string &msg) override
Append the binary message to the file.
BinaryMessage read() override
Read a message from the file.
std::string m_name
Name of the file.
Definition: core/file.h:19
Definition: core/file.h:60
Encoding encoding() const override
Get the file encoding.
Definition: core/file.h:66
FILE * fd
FILE structure used to read or write to the file.
Definition: core/file.h:21
std::string pathname() const override
Get the file pathname.
Definition: core/file.h:31
int idx
Index of the last message read from the file or written to the file.
Definition: core/file.h:25
static std::string resolve_test_data_file(const std::string &name)
Resolve the location of a test data file.
void write(const std::string &msg) override
Append the binary message to the file.
BinaryMessage read() override
Read a message from the file.
Binary message.
Definition: file.h:132
Definition: core/file.h:49
Common definitions.
bool close_on_exit
True if fd should be closed on destruction.
Definition: core/file.h:23
Base for dballe::File implementations.
Definition: core/file.h:15
Encoding encoding() const override
Get the file encoding.
Definition: core/file.h:55
File object for doing I/O on binary message streams.
Definition: file.h:18