Magick++  7.1.0
Blob.cpp
Go to the documentation of this file.
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2004
4 //
5 // Copyright @ 2013 ImageMagick Studio LLC, a non-profit organization
6 // dedicated to making software imaging solutions freely available.
7 //
8 // Implementation of Blob
9 //
10 
11 #define MAGICKCORE_IMPLEMENTATION 1
12 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
13 
14 #include "Magick++/Include.h"
15 #include "Magick++/Blob.h"
16 #include "Magick++/BlobRef.h"
17 #include "Magick++/Exception.h"
18 
19 #include <string.h>
20 
22  : _blobRef(new Magick::BlobRef(0,0))
23 {
24 }
25 
26 Magick::Blob::Blob(const void* data_,const size_t length_)
27  : _blobRef(new Magick::BlobRef(data_, length_))
28 {
29 }
30 
32  : _blobRef(blob_._blobRef)
33 {
34  // Increase reference count
35  _blobRef->increase();
36 }
37 
39 {
40  try
41  {
42  if (_blobRef->decrease() == 0)
43  delete _blobRef;
44  }
45  catch(Magick::Exception&)
46  {
47  }
48 
49  _blobRef=(Magick::BlobRef *) NULL;
50 }
51 
53 {
54  if (this != &blob_)
55  {
56  blob_._blobRef->increase();
57  if (_blobRef->decrease() == 0)
58  delete _blobRef;
59 
60  _blobRef=blob_._blobRef;
61  }
62  return(*this);
63 }
64 
65 void Magick::Blob::base64(const std::string base64_)
66 {
67  size_t
68  length;
69 
70  unsigned char
71  *decoded;
72 
73  decoded=Base64Decode(base64_.c_str(),&length);
74 
75  if(decoded)
76  updateNoCopy(static_cast<void*>(decoded),length,
78 }
79 
80 std::string Magick::Blob::base64(void) const
81 {
82  size_t
83  encoded_length;
84 
85  char
86  *encoded;
87 
88  std::string
89  result;
90 
91  encoded_length=0;
92  encoded=Base64Encode(static_cast<const unsigned char*>(data()),length(),
93  &encoded_length);
94 
95  if(encoded)
96  {
97  result=std::string(encoded,encoded_length);
98  encoded=(char *) RelinquishMagickMemory(encoded);
99  return result;
100  }
101 
102  return(std::string());
103 }
104 
105 const void* Magick::Blob::data(void) const
106 {
107  return(_blobRef->data);
108 }
109 
110 size_t Magick::Blob::length(void) const
111 {
112  return(_blobRef->length);
113 }
114 
115 void Magick::Blob::update(const void* data_,size_t length_)
116 {
117  if (_blobRef->decrease() == 0)
118  delete _blobRef;
119 
120  _blobRef=new Magick::BlobRef(data_,length_);
121 }
122 
123 void Magick::Blob::updateNoCopy(void* data_,size_t length_,
124  Magick::Blob::Allocator allocator_)
125 {
126  if (_blobRef->decrease() == 0)
127  delete _blobRef;
128 
129  _blobRef=new Magick::BlobRef((const void*) NULL,0);
130  _blobRef->data=data_;
131  _blobRef->length=length_;
132  _blobRef->allocator=allocator_;
133 }
134 
Blob & operator=(const Blob &blob_)
Definition: Blob.cpp:52
Blob(void)
Definition: Blob.cpp:21
void updateNoCopy(void *data_, const size_t length_, const Allocator allocator_=NewAllocator)
Definition: Blob.cpp:123
virtual ~Blob()
Definition: Blob.cpp:38
void increase()
Definition: BlobRef.cpp:64
std::string base64(void) const
Definition: Blob.cpp:80
const void * data(void) const
Definition: Blob.cpp:105
void update(const void *data_, const size_t length_)
Definition: Blob.cpp:115
Definition: Blob.h:17
size_t length(void) const
Definition: Blob.cpp:110