Fabcoin Core  0.16.2
P2P Digital Currency
files.h
Go to the documentation of this file.
1 // files.h - written and placed in the public domain by Wei Dai
2 
6 
7 #ifndef CRYPTOPP_FILES_H
8 #define CRYPTOPP_FILES_H
9 
10 #include "cryptlib.h"
11 #include "filters.h"
12 #include "argnames.h"
13 #include "smartptr.h"
14 
15 #include <iostream>
16 #include <fstream>
17 
19 
20 class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
24 {
25 public:
27  class Err : public Exception
28  {
29  public:
30  Err(const std::string &s) : Exception(IO_ERROR, s) {}
31  };
33  class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
35  class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
36 
38  FileStore() : m_stream(NULL), m_space(NULL), m_len(0), m_waiting(0) {}
39 
42  FileStore(std::istream &in) : m_stream(NULL), m_space(NULL), m_len(0), m_waiting(0)
43  {StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
44 
47  FileStore(const char *filename) : m_stream(NULL), m_space(NULL), m_len(0), m_waiting(0)
48  {StoreInitialize(MakeParameters(Name::InputFileName(), filename ? filename : ""));}
49 
50 #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || _MSC_VER >= 1400
51  FileStore(const wchar_t *filename)
55  {StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
56 #endif
57 
60  std::istream* GetStream() {return m_stream;}
61 
62  lword MaxRetrievable() const;
63  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
64  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
65  lword Skip(lword skipMax=ULONG_MAX);
66 
67 private:
68  void StoreInitialize(const NameValuePairs &parameters);
69 
71  std::istream *m_stream;
73  size_t m_len;
74  bool m_waiting;
75 };
76 
80 class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
81 {
82 public:
86 
88  FileSource(BufferedTransformation *attachment = NULL)
89  : SourceTemplate<FileStore>(attachment) {}
90 
95  FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULL)
96  : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
97 
103  FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
104  : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
105 
106 #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || _MSC_VER >= 1400
107  FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
114  : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
115 #endif
116 
119  std::istream* GetStream() {return m_store.GetStream();}
120 };
121 
125 class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
126 {
127 public:
129  class Err : public Exception
130  {
131  public:
132  Err(const std::string &s) : Exception(IO_ERROR, s) {}
133  };
135  class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
137  class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
138 
140  FileSink() : m_stream(NULL) {}
141 
144  FileSink(std::ostream &out)
145  {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
146 
150  FileSink(const char *filename, bool binary=true)
151  {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
152 
153 #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
154  FileSink(const wchar_t *filename, bool binary=true)
158  {IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
159 #endif
160 
163  std::ostream* GetStream() {return m_stream;}
164 
165  void IsolatedInitialize(const NameValuePairs &parameters);
166  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
167  bool IsolatedFlush(bool hardFlush, bool blocking);
168 
169 private:
171  std::ostream *m_stream;
172 };
173 
175 
176 #endif
Create a working space in a BufferedTransformation.
Definition: filters.h:160
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
Standard names for retrieving values by name when working with NameValuePairs.
FileStore::OpenErr OpenErr
Definition: files.h:84
uint8_t byte
Definition: Common.h:57
std::istream * GetStream()
Retrieves the internal stream.
Definition: files.h:60
const lword LWORD_MAX
Definition: config.h:246
member_ptr< std::ofstream > m_file
Definition: files.h:170
size_t m_len
Definition: files.h:73
OpenErr(const std::string &filename)
Definition: files.h:33
Implementation of Store interface.
Definition: files.h:80
FileStore()
Construct a FileStore.
Definition: files.h:38
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
FileStore::Err Err
Definition: files.h:83
Abstract base classes that provide a uniform interface to this library.
byte * m_space
Definition: files.h:72
Classes for automatic resource management.
Acts as a Source for pre-existing, static data.
Definition: simple.h:299
FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment=NULL, bool binary=true)
Construct a FileSource.
Definition: files.h:103
Interface for buffered transformations.
Definition: cryptlib.h:1352
FileSink(std::ostream &out)
Construct a FileSink.
Definition: files.h:144
std::ostream * m_stream
Definition: files.h:171
FileSink(const char *filename, bool binary=true)
Construct a FileSink.
Definition: files.h:150
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.cpp:59
FileSource(BufferedTransformation *attachment=NULL)
Construct a FileSource.
Definition: files.h:88
Transform a Store into a Source.
Definition: filters.h:1312
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:498
FileStore(std::istream &in)
Construct a FileStore.
Definition: files.h:42
Exception thrown when file-based write error is encountered.
Definition: files.h:137
std::istream * m_stream
Definition: files.h:71
FileStore(const char *filename)
Construct a FileStore.
Definition: files.h:47
Exception thrown when file-based error is encountered.
Definition: files.h:129
OpenErr(const std::string &filename)
Definition: files.h:135
member_ptr< std::ifstream > m_file
Definition: files.h:70
Implementation of BufferedTransformation&#39;s attachment interface.
Implementation of Store interface.
Definition: files.h:23
Err(const std::string &s)
Definition: files.h:30
std::istream * GetStream()
Retrieves the internal stream.
Definition: files.h:119
Exception thrown when file-based open error is encountered.
Definition: files.h:135
std::ostream * GetStream()
Retrieves the internal stream.
Definition: files.h:163
FileSink()
Construct a FileSink.
Definition: files.h:140
Err(const std::string &s)
Definition: files.h:132
Implementation of BufferedTransformation&#39;s attachment interface.
Definition: simple.h:331
FileStore::ReadErr ReadErr
Definition: files.h:85
#define NAMESPACE_END
Definition: config.h:201
std::vector< char * > parameters
Definition: boostTest.cpp:46
FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment=NULL)
Construct a FileSource.
Definition: files.h:95
word64 lword
Definition: config.h:245
#define CRYPTOPP_DLL
Definition: config.h:704
Implementation of Store interface.
Definition: files.h:125
Exception thrown when file-based read error is encountered.
Definition: files.h:35
Ensures an object is not copyable.
Definition: misc.h:217
bool m_waiting
Definition: files.h:74
Exception thrown when file-based error is encountered.
Definition: files.h:27
Interface for retrieving values given their names.
Definition: cryptlib.h:279
Exception thrown when file-based open error is encountered.
Definition: files.h:33