mirror of https://github.com/davisking/dlib.git
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
This commit is contained in:
parent
fafa738f61
commit
a03e9b2e1a
|
@ -11,6 +11,25 @@
|
|||
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::
|
||||
|
@ -18,7 +37,8 @@ namespace dlib
|
|||
) :
|
||||
encode_table(0),
|
||||
decode_table(0),
|
||||
bad_value(100)
|
||||
bad_value(100),
|
||||
eol_style(LF)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -156,7 +176,6 @@ namespace dlib
|
|||
unsigned char c1, c2, c3, c4, c5, c6;
|
||||
|
||||
int counter = 19;
|
||||
const char newline = '\n';
|
||||
|
||||
// while we haven't hit the end of the input stream
|
||||
while (status != 0)
|
||||
|
@ -165,9 +184,29 @@ namespace dlib
|
|||
{
|
||||
counter = 19;
|
||||
// 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;
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#define DLIB_BASE64_KERNEl_1_
|
||||
|
||||
#include "../algs.h"
|
||||
#include "base64_kernel_abstract.h"
|
||||
#include <iosfwd>
|
||||
|
||||
namespace dlib
|
||||
|
@ -42,6 +43,20 @@ namespace dlib
|
|||
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 (
|
||||
std::istream& in,
|
||||
std::ostream& out
|
||||
|
@ -57,6 +72,7 @@ namespace dlib
|
|||
char* encode_table;
|
||||
unsigned char* decode_table;
|
||||
const unsigned char bad_value;
|
||||
line_ending_type eol_style;
|
||||
|
||||
// restricted functions
|
||||
base64_kernel_1(base64_kernel_1&); // copy constructor
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace dlib
|
|||
{
|
||||
/*!
|
||||
INITIAL VALUE
|
||||
This object does not have any state associated with it.
|
||||
- line_ending() == LF
|
||||
|
||||
WHAT THIS OBJECT REPRESENTS
|
||||
This object consists of the two functions encode and decode.
|
||||
|
@ -42,6 +42,34 @@ namespace dlib
|
|||
- 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 (
|
||||
std::istream& in,
|
||||
|
@ -59,7 +87,6 @@ namespace dlib
|
|||
this exception may be thrown if there is any other problem
|
||||
!*/
|
||||
|
||||
|
||||
void decode (
|
||||
std::istream& in,
|
||||
std::ostream& out
|
||||
|
@ -80,7 +107,6 @@ namespace dlib
|
|||
this exception may be thrown if there is any other problem
|
||||
!*/
|
||||
|
||||
|
||||
private:
|
||||
|
||||
// restricted functions
|
||||
|
|
Loading…
Reference in New Issue