libdballe  7.21
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 
8 namespace dballe {
9 
10 struct BinaryMessage;
11 
17 struct File
18 {
20  typedef enum {
21  BUFR = 0,
22  CREX = 1,
23  AOF = 2,
24  } Encoding;
25 
26  virtual ~File();
27 
29  virtual std::string pathname() const = 0;
30 
32  virtual Encoding encoding() const = 0;
33 
41  virtual BinaryMessage read() = 0;
42 
53  virtual bool foreach(std::function<bool(const BinaryMessage&)> dest) = 0;
54 
56  virtual void write(const std::string& msg) = 0;
57 
68  static std::unique_ptr<File> create(const std::string& pathname, const char* mode);
69 
82  static std::unique_ptr<File> create(Encoding type, const std::string& pathname, const char* mode);
83 
99  static std::unique_ptr<File> create(FILE* file, bool close_on_exit, const std::string& name="(fp)");
100 
117  static std::unique_ptr<File> create(Encoding type, FILE* file, bool close_on_exit, const std::string& name="(fp)");
118 
120  static const char* encoding_name(Encoding enc);
121 
123  static Encoding parse_encoding(const char* s);
124 
126  static Encoding parse_encoding(const std::string& s);
127 
128 };
129 
132 {
135 
137  std::string data;
138 
144  std::string pathname;
145 
147  off_t offset = (off_t)-1;
148 
150  int index = MISSING_INT;
151 
152  BinaryMessage(File::Encoding encoding)
153  : encoding(encoding) {}
154 
156  operator bool() const;
157 };
158 
159 }
160 
161 #endif
Encoding
Supported encodings.
Definition: file.h:20
virtual BinaryMessage read()=0
Read a message from the file.
Common base types used by most of DB-All.e code.
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
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:137
Binary message.
Definition: file.h:131
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:134
std::string pathname
Pathname of the file from where the BinaryMessage has been read.
Definition: file.h:144
virtual std::string pathname() const =0
Get the file pathname.
File object for doing I/O on binary message streams.
Definition: file.h:17