aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/src/HexBinaryEncoder.cpp
blob: 728a2bfdfcd7f01767783a452dd7ef274eb470e9 (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// HexBinaryEncoder.cpp
//
// Library: Foundation
// Package: Streams
// Module:  HexBinary
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/HexBinaryEncoder.h"


namespace Poco {


HexBinaryEncoderBuf::HexBinaryEncoderBuf(std::ostream& ostr): 
	_pos(0),
	_lineLength(72),
	_uppercase(0),
	_buf(*ostr.rdbuf())
{
}


HexBinaryEncoderBuf::~HexBinaryEncoderBuf()
{
	try
	{
		close();
	}
	catch (...)
	{
	}
}


void HexBinaryEncoderBuf::setLineLength(int lineLength)
{
	_lineLength = lineLength;
}


int HexBinaryEncoderBuf::getLineLength() const
{
	return _lineLength;
}


void HexBinaryEncoderBuf::setUppercase(bool flag)
{
	_uppercase = flag ? 16 : 0;
}


int HexBinaryEncoderBuf::writeToDevice(char c)
{
	static const int eof = std::char_traits<char>::eof();
	static const char digits[] = "0123456789abcdef0123456789ABCDEF";
	
	if (_buf.sputc(digits[_uppercase + ((c >> 4) & 0xF)]) == eof) return eof;
	++_pos;
	if (_buf.sputc(digits[_uppercase + (c & 0xF)]) == eof) return eof;
	if (++_pos >= _lineLength && _lineLength > 0) 
	{
		if (_buf.sputc('\n') == eof) return eof;
		_pos = 0;
	}
	return charToInt(c);
}


int HexBinaryEncoderBuf::close()
{
	sync();
	return _buf.pubsync();
}


HexBinaryEncoderIOS::HexBinaryEncoderIOS(std::ostream& ostr): _buf(ostr)
{
	poco_ios_init(&_buf);
}


HexBinaryEncoderIOS::~HexBinaryEncoderIOS()
{
}


int HexBinaryEncoderIOS::close()
{
	return _buf.close();
}


HexBinaryEncoderBuf* HexBinaryEncoderIOS::rdbuf()
{
	return &_buf;
}


HexBinaryEncoder::HexBinaryEncoder(std::ostream& ostr): HexBinaryEncoderIOS(ostr), std::ostream(&_buf)
{
}


HexBinaryEncoder::~HexBinaryEncoder()
{
}


} // namespace Poco