libdballe 9.13
core/file.h
1#ifndef DBA_CORE_FILE_H
2#define DBA_CORE_FILE_H
3
4#include <cstdio>
5#include <dballe/core/defs.h>
6#include <dballe/file.h>
7#include <functional>
8#include <memory>
9#include <string>
10
11namespace dballe {
12namespace core {
13
15class File : public dballe::File
16{
17protected:
19 std::string m_name;
21 FILE* fd;
25 int idx;
26
27public:
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>
48 open_test_data_file(Encoding type, const std::string& name);
49};
50
51class BufrFile : public dballe::core::File
52{
53public:
54 BufrFile(const std::string& name, FILE* fd, bool close_on_exit = true)
55 : File(name, fd, close_on_exit)
56 {
57 }
58
59 Encoding encoding() const override { return Encoding::BUFR; }
60 BinaryMessage read() override;
61 void write(const std::string& msg) override;
62};
63
64class CrexFile : public dballe::core::File
65{
66public:
67 CrexFile(const std::string& name, FILE* fd, bool close_on_exit = true)
68 : File(name, fd, close_on_exit)
69 {
70 }
71
72 Encoding encoding() const override { return Encoding::CREX; }
73 BinaryMessage read() override;
74 void write(const std::string& msg) override;
75};
76
77class JsonFile : public dballe::core::File
78{
79public:
80 JsonFile(const std::string& name, FILE* fd, bool close_on_exit = true)
81 : File(name, fd, close_on_exit)
82 {
83 }
84
85 Encoding encoding() const override { return Encoding::JSON; }
86 BinaryMessage read() override;
87 void write(const std::string& msg) override;
88};
89
90} // namespace core
91} // namespace dballe
92#endif
Binary message.
Definition file.h:135
File object for doing I/O on binary message streams.
Definition file.h:18
BinaryMessage read() override
Read a message from the file.
Encoding encoding() const override
Get the file encoding.
Definition core/file.h:59
void write(const std::string &msg) override
Append the binary message to the file.
BinaryMessage read() override
Read a message from the file.
void write(const std::string &msg) override
Append the binary message to the file.
Encoding encoding() const override
Get the file encoding.
Definition core/file.h:72
Base for dballe::File implementations.
Definition core/file.h:16
static std::unique_ptr< dballe::File > open_test_data_file(Encoding type, const std::string &name)
Open a test data file.
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
std::string m_name
Name of the file.
Definition core/file.h:19
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.
bool close_on_exit
True if fd should be closed on destruction.
Definition core/file.h:23
void close() override
Close the underlying file.
void write(const std::string &msg) override
Append the binary message to the file.
BinaryMessage read() override
Read a message from the file.
Encoding encoding() const override
Get the file encoding.
Definition core/file.h:85
Common definitions.