Commit a03e9b2e authored by Davis King's avatar Davis King

Added something to the base64 object to allow the user to

control how it writes out end of lines.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402621
parent fafa738f
...@@ -11,6 +11,25 @@ ...@@ -11,6 +11,25 @@
namespace dlib namespace dlib
{ {
// ----------------------------------------------------------------------------------------
base64_kernel_1::line_ending_type base64_kernel_1::
line_ending (
) const
{
return eol_style;
}
// ----------------------------------------------------------------------------------------
void base64_kernel_1::
set_line_ending (
line_ending_type eol_style_
)
{
eol_style = eol_style_;
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
base64_kernel_1:: base64_kernel_1::
...@@ -18,7 +37,8 @@ namespace dlib ...@@ -18,7 +37,8 @@ namespace dlib
) : ) :
encode_table(0), encode_table(0),
decode_table(0), decode_table(0),
bad_value(100) bad_value(100),
eol_style(LF)
{ {
try try
{ {
...@@ -156,7 +176,6 @@ namespace dlib ...@@ -156,7 +176,6 @@ namespace dlib
unsigned char c1, c2, c3, c4, c5, c6; unsigned char c1, c2, c3, c4, c5, c6;
int counter = 19; int counter = 19;
const char newline = '\n';
// while we haven't hit the end of the input stream // while we haven't hit the end of the input stream
while (status != 0) while (status != 0)
...@@ -165,9 +184,29 @@ namespace dlib ...@@ -165,9 +184,29 @@ namespace dlib
{ {
counter = 19; counter = 19;
// write a newline // write a newline
if (out.sputn(reinterpret_cast<const char*>(&newline),1)!=1) char ch;
switch (eol_style)
{ {
throw std::ios_base::failure("error occured in the base64 object"); case CR:
ch = '\r';
if (out.sputn(&ch,1)!=1)
throw std::ios_base::failure("error occured in the base64 object");
break;
case LF:
ch = '\n';
if (out.sputn(&ch,1)!=1)
throw std::ios_base::failure("error occured in the base64 object");
break;
case CRLF:
ch = '\r';
if (out.sputn(&ch,1)!=1)
throw std::ios_base::failure("error occured in the base64 object");
ch = '\n';
if (out.sputn(&ch,1)!=1)
throw std::ios_base::failure("error occured in the base64 object");
break;
default:
DLIB_CASSERT(false,"this should never happen");
} }
} }
--counter; --counter;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#define DLIB_BASE64_KERNEl_1_ #define DLIB_BASE64_KERNEl_1_
#include "../algs.h" #include "../algs.h"
#include "base64_kernel_abstract.h"
#include <iosfwd> #include <iosfwd>
namespace dlib namespace dlib
...@@ -42,6 +43,20 @@ namespace dlib ...@@ -42,6 +43,20 @@ namespace dlib
virtual ~base64_kernel_1 ( virtual ~base64_kernel_1 (
); );
enum line_ending_type
{
CR, // i.e. "\r"
LF, // i.e. "\n"
CRLF // i.e. "\r\n"
};
line_ending_type line_ending (
) const;
void set_line_ending (
line_ending_type eol_style_
);
void encode ( void encode (
std::istream& in, std::istream& in,
std::ostream& out std::ostream& out
...@@ -57,6 +72,7 @@ namespace dlib ...@@ -57,6 +72,7 @@ namespace dlib
char* encode_table; char* encode_table;
unsigned char* decode_table; unsigned char* decode_table;
const unsigned char bad_value; const unsigned char bad_value;
line_ending_type eol_style;
// restricted functions // restricted functions
base64_kernel_1(base64_kernel_1&); // copy constructor base64_kernel_1(base64_kernel_1&); // copy constructor
......
...@@ -13,7 +13,7 @@ namespace dlib ...@@ -13,7 +13,7 @@ namespace dlib
{ {
/*! /*!
INITIAL VALUE INITIAL VALUE
This object does not have any state associated with it. - line_ending() == LF
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
This object consists of the two functions encode and decode. This object consists of the two functions encode and decode.
...@@ -42,6 +42,34 @@ namespace dlib ...@@ -42,6 +42,34 @@ namespace dlib
- all memory associated with *this has been released - all memory associated with *this has been released
!*/ !*/
enum line_ending_type
{
CR, // i.e. "\r"
LF, // i.e. "\n"
CRLF // i.e. "\r\n"
};
line_ending_type line_ending (
) const;
/*!
ensures
- returns the type of end of line bytes the encoder
will use when encoding data to base64 blocks. Note that
the ostream object you use might apply some sort of transform
to line endings as well. For example, C++ ofstream objects
usually convert '\n' into whatever a normal newline is for
your platform unless you open a file in binary mode. But
aside from file streams the ostream objects usually don't
modify the data you pass to them.
!*/
void set_line_ending (
line_ending_type eol_style
);
/*!
ensures
- #line_ending() == eol_style
!*/
void encode ( void encode (
std::istream& in, std::istream& in,
...@@ -59,7 +87,6 @@ namespace dlib ...@@ -59,7 +87,6 @@ namespace dlib
this exception may be thrown if there is any other problem this exception may be thrown if there is any other problem
!*/ !*/
void decode ( void decode (
std::istream& in, std::istream& in,
std::ostream& out std::ostream& out
...@@ -80,7 +107,6 @@ namespace dlib ...@@ -80,7 +107,6 @@ namespace dlib
this exception may be thrown if there is any other problem this exception may be thrown if there is any other problem
!*/ !*/
private: private:
// restricted functions // restricted functions
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment