libdballe  7.29
file.h
1 #ifndef DBALLE_FILE_H
2 #define DBALLE_FILE_H
3 
4 #include <dballe/types.h>
5 #include <memory>
6 #include <string>
7 #include <functional>
8 
9 namespace dballe {
10 
11 struct BinaryMessage;
12 
18 struct File
19 {
21  typedef enum {
22  BUFR = 0,
23  CREX = 1,
24  AOF = 2,
25  } Encoding;
26 
27  virtual ~File();
28 
30  virtual std::string pathname() const = 0;
31 
33  virtual Encoding encoding() const = 0;
34 
42  virtual BinaryMessage read() = 0;
43 
54  virtual bool foreach(std::function<bool(const BinaryMessage&)> dest) = 0;
55 
57  virtual void write(const std::string& msg) = 0;
58 
69  static std::unique_ptr<File> create(const std::string& pathname, const char* mode);
70 
83  static std::unique_ptr<File> create(Encoding type, const std::string& pathname, const char* mode);
84 
100  static std::unique_ptr<File> create(FILE* file, bool close_on_exit, const std::string& name="(fp)");
101 
118  static std::unique_ptr<File> create(Encoding type, FILE* file, bool close_on_exit, const std::string& name="(fp)");
119 
121  static const char* encoding_name(Encoding enc);
122 
124  static Encoding parse_encoding(const char* s);
125 
127  static Encoding parse_encoding(const std::string& s);
128 
129 };
130 
133 {
136 
138  std::string data;
139 
145  std::string pathname;
146 
148  off_t offset = (off_t)-1;
149 
151  int index = MISSING_INT;
152 
154  : encoding(encoding) {}
155 
157  operator bool() const;
158 };
159 
160 }
161 
162 #endif
Encoding
Supported encodings.
Definition: file.h:21
virtual BinaryMessage read()=0
Read a message from the file.
Common base types used by most of DB-All.e code.
int index
Index of the message from the beginning of the file.
Definition: file.h:151
off_t offset
Start offset of this message inside the file.
Definition: file.h:148
virtual Encoding encoding() const =0
Get the file encoding.
virtual void write(const std::string &msg)=0
Append the binary message to the file.
static Encoding parse_encoding(const char *s)
Return the Encoding corresponding to the given name.
static std::unique_ptr< File > create(const std::string &pathname, const char *mode)
Open a file from the filesystem, autodetecting the encoding type.
std::string data
Binary message data.
Definition: file.h:138
Binary message.
Definition: file.h:132
static const char * encoding_name(Encoding enc)
Return a string with the name of this encoding.
File::Encoding encoding
Format of the binary data.
Definition: file.h:135
std::string pathname
Pathname of the file from where the BinaryMessage has been read.
Definition: file.h:145
virtual std::string pathname() const =0
Get the file pathname.
File object for doing I/O on binary message streams.
Definition: file.h:18