summaryrefslogtreecommitdiffstats
path: root/old/botan/src/filters/data_snk.cpp
blob: f8ee9f86e45318972b576036720fe8f31c23f38f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* DataSink
* (C) 1999-2007 Jack Lloyd
*     2005 Matthew Gregan
*
* Distributed under the terms of the Botan license
*/

#include <botan/data_snk.h>
#include <botan/exceptn.h>
#include <fstream>

namespace Botan {

/*
* Write to a stream
*/
void DataSink_Stream::write(const byte out[], u32bit length)
   {
   sink->write(reinterpret_cast<const char*>(out), length);
   if(!sink->good())
      throw Stream_IO_Error("DataSink_Stream: Failure writing to " +
                            identifier);
   }

/*
* DataSink_Stream Constructor
*/
DataSink_Stream::DataSink_Stream(std::ostream& out,
                                 const std::string& name) :
   identifier(name != "" ? name : "<std::ostream>"), owner(false)
   {
   sink = &out;
   }

/*
* DataSink_Stream Constructor
*/
DataSink_Stream::DataSink_Stream(const std::string& path,
                                 bool use_binary) :
   identifier(path), owner(true)
   {
   if(use_binary)
      sink = new std::ofstream(path.c_str(), std::ios::binary);
   else
      sink = new std::ofstream(path.c_str());

   if(!sink->good())
      throw Stream_IO_Error("DataSink_Stream: Failure opening " + path);
   }

/*
* DataSink_Stream Destructor
*/
DataSink_Stream::~DataSink_Stream()
   {
   if(owner)
      delete sink;
   sink = 0;
   }

}