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

files.cpp

00001 // files.cpp - written and placed in the public domain by Wei Dai
00002 
00003 #include "pch.h"
00004 #include "files.h"
00005 
00006 NAMESPACE_BEGIN(CryptoPP)
00007 
00008 using namespace std;
00009 
00010 static const unsigned int BUFFER_SIZE = 1024;
00011 
00012 FileStore::FileStore(istream &i)
00013         : m_in(i)
00014 {
00015 }
00016 
00017 FileStore::FileStore(const char *filename)
00018         : m_file(filename, ios::in | ios::binary), m_in(m_file)
00019 {
00020         if (!m_file)
00021                 throw OpenErr(filename);
00022 }
00023 
00024 unsigned long FileStore::MaxRetrievable() const
00025 {
00026         streampos current = m_in.tellg();
00027         streampos end = m_in.rdbuf()->pubseekoff(0, ios::end, ios::in);
00028         m_in.rdbuf()->pubseekpos(current);
00029         return end-current;
00030 }
00031 
00032 unsigned int FileStore::Peek(byte &outByte) const
00033 {
00034         int result = m_in.peek();
00035         if (result == EOF)      // GCC workaround: 2.95.2 doesn't have char_traits<char>::eof()
00036                 return 0;
00037         else
00038         {
00039                 outByte = byte(result);
00040                 return 1;
00041         }
00042 }
00043 
00044 unsigned long FileStore::TransferTo(BufferedTransformation &target, unsigned long size)
00045 {
00046         m_buffer.Resize(BUFFER_SIZE);
00047         unsigned long total=0;
00048 
00049         while (size && m_in.good())
00050         {
00051                 m_in.read((char *)m_buffer.ptr, STDMIN(size, (unsigned long)BUFFER_SIZE));
00052                 unsigned int l = m_in.gcount();
00053                 target.Put(m_buffer, l);
00054                 size -= l;
00055                 total += l;
00056         }
00057 
00058         if (!m_in.good() && !m_in.eof())
00059                 throw ReadErr();
00060 
00061         return total;
00062 }
00063 
00064 unsigned long FileStore::CopyTo(BufferedTransformation &target, unsigned long copyMax) const
00065 {
00066         unsigned long total = const_cast<FileStore *>(this)->TransferTo(target, copyMax);
00067         m_in.clear();
00068         m_in.seekg(-std::streamoff(total), ios::cur);   // GCC workaround: 2.95.2 doesn't have istream::off_type
00069         return total;
00070 }
00071 
00072 FileSource::FileSource (istream &i, bool pumpAll, BufferedTransformation *outQueue)
00073         : Source(outQueue), m_store(i)
00074 {
00075         if (pumpAll)
00076                 PumpAll();
00077 }
00078 
00079 FileSource::FileSource (const char *filename, bool pumpAll, BufferedTransformation *outQueue)
00080         : Source(outQueue), m_store(filename)
00081 {
00082         if (pumpAll)
00083                 PumpAll();
00084 }
00085 
00086 FileSink::FileSink(ostream &o)
00087         : out(o)
00088 {
00089 }
00090 
00091 FileSink::FileSink(const char *filename, bool binary)
00092         : file(filename, ios::out | (binary ? ios::binary : ios::openmode(0)) | ios::trunc), out(file)
00093 {
00094         if (!file)
00095                 throw OpenErr(filename);
00096 }
00097 
00098 void FileSink::Flush(bool, int)
00099 {
00100         out.flush();
00101         if (!out.good())
00102           throw WriteErr();
00103 }
00104 
00105 void FileSink::MessageEnd(int)
00106 {
00107         Flush(true);
00108 }
00109 
00110 void FileSink::Put(const byte *inString, unsigned int length)
00111 {
00112         out.write((const char *)inString, length);
00113         if (!out.good())
00114           throw WriteErr();
00115 }
00116 
00117 NAMESPACE_END

Generated at Mon Jan 15 01:16:32 2001 for Crypto++ by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000