Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# files.h

    00001 #ifndef CRYPTOPP_FILES_H
    00002 #define CRYPTOPP_FILES_H
    00003
    00004 #include "cryptlib.h"
    00005 #include "filters.h"
    00006
    00007 #include <iostream>
    00008 #include <fstream>
    00009
    00010 NAMESPACE_BEGIN(CryptoPP)
    00011
    00012 //! .
    00013 class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
    00014 {
    00015 public:
    00016         class Err : public Exception
    00017         {
    00018         public:
    00019                 Err(const std::string &s) : Exception(IO_ERROR, s) {}
    00020         };
    00021         class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
    00022         class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
    00023
    00024         FileStore() : m_stream(NULL) {}
    00025         FileStore(std::istream &in)
    00026                 {StoreInitialize(MakeParameters("InputStreamPointer", &in));}
    00027         FileStore(const char *filename)
    00028                 {StoreInitialize(MakeParameters("InputFileName", filename));}
    00029
    00030         std::istream* GetStream() {return m_stream;}
    00031
    00032         unsigned long MaxRetrievable() const;
    00033         unsigned int Peek(byte &outByte) const;
    00034         unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
    00035         unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
    00036         unsigned long Skip(unsigned long skipMax);
    00037
    00038 private:
    00039         void StoreInitialize(const NameValuePairs &parameters);
    00040
    00041         member_ptr<std::ifstream> m_file;
    00042         std::istream *m_stream;
    00043         byte *m_space;
    00044         unsigned int m_len;
    00045         bool m_waiting;
    00046 };
    00047
    00048 //! .
    00049 class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
    00050 {
    00051 public:
    00052         typedef FileStore::Err Err;
    00053         typedef FileStore::OpenErr OpenErr;
    00054         typedef FileStore::ReadErr ReadErr;
    00055
    00056         FileSource(BufferedTransformation *attachment = NULL)
    00057                 : SourceTemplate<FileStore>(attachment) {}
    00058         FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULL)
    00059                 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputStreamPointer", &in));}
    00060         FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
    00061                 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputFileName", filename)("InputBinaryMode", binary));}
    00062
    00063         std::istream* GetStream() {return m_store.GetStream();}
    00064 };
    00065
    00066 //! .
    00067 class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
    00068 {
    00069 public:
    00070         class Err : public Exception
    00071         {
    00072         public:
    00073                 Err(const std::string &s) : Exception(IO_ERROR, s) {}
    00074         };
    00075         class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
    00076         class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
    00077
    00078         FileSink() : m_stream(NULL) {}
    00079         FileSink(std::ostream &out)
    00080                 {IsolatedInitialize(MakeParameters("OutputStreamPointer", &out));}
    00081         FileSink(const char *filename, bool binary=true)
    00082                 {IsolatedInitialize(MakeParameters("OutputFileName", filename)("OutputBinaryMode", binary));}
    00083
    00084         std::ostream* GetStream() {return m_stream;}
    00085
    00086         void IsolatedInitialize(const NameValuePairs &parameters);
    00087         unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking);
    00088         bool IsolatedFlush(bool hardFlush, bool blocking);
    00089
    00090 private:
    00091         member_ptr<std::ofstream> m_file;
    00092         std::ostream *m_stream;
    00093 };
    00094
    00095 NAMESPACE_END
    00096
    00097 #endif