aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-08-19 00:35:09 +0000
committerlloyd <[email protected]>2010-08-19 00:35:09 +0000
commit02620c3c59e0773ba9a89035dd6ebe33127aba16 (patch)
treeb91f226ee499bb90e539db3b8e83c43e9367df3f /src
parent34fe34b27e0b6a2799078879b4566707a8622e81 (diff)
Use a different idiom for handling the different cases between being
passed a ref and having to allocate a new stream object, a little bit cleaner I think.
Diffstat (limited to 'src')
-rw-r--r--src/filters/data_snk.cpp29
-rw-r--r--src/filters/data_snk.h6
-rw-r--r--src/filters/data_src.cpp51
-rw-r--r--src/filters/data_src.h7
4 files changed, 48 insertions, 45 deletions
diff --git a/src/filters/data_snk.cpp b/src/filters/data_snk.cpp
index f8ee9f86e..d61fcc61f 100644
--- a/src/filters/data_snk.cpp
+++ b/src/filters/data_snk.cpp
@@ -17,8 +17,8 @@ namespace Botan {
*/
void DataSink_Stream::write(const byte out[], u32bit length)
{
- sink->write(reinterpret_cast<const char*>(out), length);
- if(!sink->good())
+ sink.write(reinterpret_cast<const char*>(out), length);
+ if(!sink.good())
throw Stream_IO_Error("DataSink_Stream: Failure writing to " +
identifier);
}
@@ -28,9 +28,10 @@ void DataSink_Stream::write(const byte out[], u32bit length)
*/
DataSink_Stream::DataSink_Stream(std::ostream& out,
const std::string& name) :
- identifier(name != "" ? name : "<std::ostream>"), owner(false)
+ identifier(name),
+ sink_p(0),
+ sink(out)
{
- sink = &out;
}
/*
@@ -38,15 +39,17 @@ DataSink_Stream::DataSink_Stream(std::ostream& out,
*/
DataSink_Stream::DataSink_Stream(const std::string& path,
bool use_binary) :
- identifier(path), owner(true)
+ identifier(path),
+ sink_p(use_binary ?
+ new std::ofstream(path.c_str(), std::ios::binary) :
+ new std::ofstream(path.c_str())),
+ sink(*sink_p)
{
- if(use_binary)
- sink = new std::ofstream(path.c_str(), std::ios::binary);
- else
- sink = new std::ofstream(path.c_str());
-
- if(!sink->good())
+ if(!sink.good())
+ {
+ delete sink_p;
throw Stream_IO_Error("DataSink_Stream: Failure opening " + path);
+ }
}
/*
@@ -54,9 +57,7 @@ DataSink_Stream::DataSink_Stream(const std::string& path,
*/
DataSink_Stream::~DataSink_Stream()
{
- if(owner)
- delete sink;
- sink = 0;
+ delete sink_p;
}
}
diff --git a/src/filters/data_snk.h b/src/filters/data_snk.h
index 509fdae20..def9facbb 100644
--- a/src/filters/data_snk.h
+++ b/src/filters/data_snk.h
@@ -43,7 +43,7 @@ class BOTAN_DLL DataSink_Stream : public DataSink
* @param name identifier
*/
DataSink_Stream(std::ostream& stream,
- const std::string& name = "");
+ const std::string& name = "<std::ostream>");
/**
* Construct a DataSink_Stream from a stream.
@@ -57,9 +57,9 @@ class BOTAN_DLL DataSink_Stream : public DataSink
~DataSink_Stream();
private:
const std::string identifier;
- const bool owner;
- std::ostream* sink;
+ std::ostream* sink_p;
+ std::ostream& sink;
};
}
diff --git a/src/filters/data_src.cpp b/src/filters/data_src.cpp
index e6387c4ba..522ce09d0 100644
--- a/src/filters/data_src.cpp
+++ b/src/filters/data_src.cpp
@@ -8,7 +8,6 @@
#include <botan/data_src.h>
#include <botan/exceptn.h>
-
#include <fstream>
#include <algorithm>
@@ -107,11 +106,11 @@ DataSource_Memory::DataSource_Memory(const std::string& in)
*/
u32bit DataSource_Stream::read(byte out[], u32bit length)
{
- source->read(reinterpret_cast<char*>(out), length);
- if(source->bad())
+ source.read(reinterpret_cast<char*>(out), length);
+ if(source.bad())
throw Stream_IO_Error("DataSource_Stream::read: Source failure");
- u32bit got = source->gcount();
+ u32bit got = source.gcount();
total_read += got;
return got;
}
@@ -129,23 +128,23 @@ u32bit DataSource_Stream::peek(byte out[], u32bit length, u32bit offset) const
if(offset)
{
SecureVector<byte> buf(offset);
- source->read(reinterpret_cast<char*>(buf.begin()), buf.size());
- if(source->bad())
+ source.read(reinterpret_cast<char*>(buf.begin()), buf.size());
+ if(source.bad())
throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
- got = source->gcount();
+ got = source.gcount();
}
if(got == offset)
{
- source->read(reinterpret_cast<char*>(out), length);
- if(source->bad())
+ source.read(reinterpret_cast<char*>(out), length);
+ if(source.bad())
throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
- got = source->gcount();
+ got = source.gcount();
}
- if(source->eof())
- source->clear();
- source->seekg(total_read, std::ios::beg);
+ if(source.eof())
+ source.clear();
+ source.seekg(total_read, std::ios::beg);
return got;
}
@@ -155,7 +154,7 @@ u32bit DataSource_Stream::peek(byte out[], u32bit length, u32bit offset) const
*/
bool DataSource_Stream::end_of_data() const
{
- return (!source->good());
+ return (!source.good());
}
/*
@@ -171,15 +170,17 @@ std::string DataSource_Stream::id() const
*/
DataSource_Stream::DataSource_Stream(const std::string& path,
bool use_binary) :
- identifier(path), owner(true)
+ identifier(path),
+ source_p(use_binary ?
+ new std::ifstream(path.c_str()) :
+ new std::ifstream(path.c_str(), std::ios::binary)),
+ source(*source_p)
{
- if(use_binary)
- source = new std::ifstream(path.c_str(), std::ios::binary);
- else
- source = new std::ifstream(path.c_str());
-
- if(!source->good())
+ if(!source.good())
+ {
+ delete source_p;
throw Stream_IO_Error("DataSource: Failure opening file " + path);
+ }
total_read = 0;
}
@@ -189,9 +190,10 @@ DataSource_Stream::DataSource_Stream(const std::string& path,
*/
DataSource_Stream::DataSource_Stream(std::istream& in,
const std::string& name) :
- identifier(name), owner(false)
+ identifier(name),
+ source_p(0),
+ source(in)
{
- source = &in;
total_read = 0;
}
@@ -200,8 +202,7 @@ DataSource_Stream::DataSource_Stream(std::istream& in,
*/
DataSource_Stream::~DataSource_Stream()
{
- if(owner)
- delete source;
+ delete source_p;
}
}
diff --git a/src/filters/data_src.h b/src/filters/data_src.h
index bf3bfa99b..26238928d 100644
--- a/src/filters/data_src.h
+++ b/src/filters/data_src.h
@@ -130,7 +130,8 @@ class BOTAN_DLL DataSource_Stream : public DataSource
bool end_of_data() const;
std::string id() const;
- DataSource_Stream(std::istream&, const std::string& id = "");
+ DataSource_Stream(std::istream&,
+ const std::string& id = "<std::istream>");
/**
* Construct a Stream-Based DataSource from file
@@ -142,9 +143,9 @@ class BOTAN_DLL DataSource_Stream : public DataSource
~DataSource_Stream();
private:
const std::string identifier;
- const bool owner;
- std::istream* source;
+ std::istream* source_p;
+ std::istream& source;
u32bit total_read;
};