2
Copyright (c) 2009 NetAllied Systems GmbH
4
This file is part of Common libBuffer.
6
Licensed under the MIT Open Source License,
7
for details please see LICENSE file or the website
8
http://www.opensource.org/licenses/mit-license.php
11
#include "CommonCharacterBuffer.h"
15
#include <Commonftoa.h>
16
#include <Commondtoa.h>
17
#include <Commonitoa.h>
18
#include <ConvertUTF.h>
23
const char CharacterBuffer::DEFAULT_TRUE_STRING[] = "1";
24
const char CharacterBuffer::DEFAULT_FALSE_STRING[] = "0";
27
//--------------------------------------------------------------------
28
CharacterBuffer::CharacterBuffer( size_t bufferSize, IBufferFlusher* flusher )
29
: Buffer(bufferSize, flusher)
30
, mTrueString(DEFAULT_TRUE_STRING)
31
, mTrueStringLength(sizeof(DEFAULT_TRUE_STRING)-1)
32
, mFalseString(DEFAULT_FALSE_STRING)
33
, mFalseStringLength(sizeof(DEFAULT_FALSE_STRING)-1)
38
//--------------------------------------------------------------------
39
void CharacterBuffer::setTrueString( const char* trueString )
41
mTrueString = trueString;
42
mTrueStringLength = strlen( trueString );
45
//--------------------------------------------------------------------
46
void CharacterBuffer::setFalseString( const char* falseString )
48
mFalseString = falseString;
49
mFalseStringLength = strlen( falseString );
52
//--------------------------------------------------------------------
53
bool CharacterBuffer::copyToBufferAsChar( float f )
55
if ( getBytesAvailable() < FTOA_BUFFERSIZE )
57
//The float might not fit into the buffer. We need to flush first.
61
// Check if the buffer size is large enough
62
// assert(getBytesAvailable() >= FTOA_BUFFERSIZE);
63
if ( getBytesAvailable() < FTOA_BUFFERSIZE )
65
//No chance to convert the float with this buffer
69
size_t bytesWritten = ftoa( f, getCurrentPosition() );
71
increaseCurrentPosition( bytesWritten );
76
//--------------------------------------------------------------------
77
bool CharacterBuffer::copyToBufferAsChar( double d, bool doublePrecision /*= false*/ )
79
if ( getBytesAvailable() < DTOA_BUFFERSIZE )
81
//The double might not fit into the buffer. We need to flush first.
85
// Check if the buffer size is large enough
86
// assert(getBytesAvailable() >= DTOA_BUFFERSIZE);
87
if ( getBytesAvailable() < DTOA_BUFFERSIZE )
89
//No chance to convert the double with this buffer
93
size_t bytesWritten = dtoa( d, getCurrentPosition(), doublePrecision );
95
increaseCurrentPosition( bytesWritten );
100
//--------------------------------------------------------------------
101
bool CharacterBuffer::copyToBufferAsChar( bool v )
103
const char* string = v ? mTrueString : mFalseString;
104
size_t length = v ? mTrueStringLength : mFalseStringLength;
106
if ( getBytesAvailable() < length )
108
//The string might not fit into the buffer. We need to flush first.
112
// Check if the buffer size is large enough
113
// assert(getBytesAvailable() >= length);
114
if ( getBytesAvailable() < length )
116
//No chance to convert the bool with this buffer
120
copyToBuffer( string, length );
125
//------------------------------
126
void CharacterBuffer::copyToBufferAsChar( const wchar_t* text, size_t length )
128
static const size_t CHARACTERS_IN_BUFFER = 100;
129
static const size_t BUFFER_SIZE = CharacterBuffer::MAX_UTF8_CHAR_LENGTH * CHARACTERS_IN_BUFFER;
130
char buffer[BUFFER_SIZE];
131
size_t charactersLeft = length;
132
const wchar_t* p = text;
133
while ( charactersLeft > 0 )
135
size_t charactersToConvert = std::min(CHARACTERS_IN_BUFFER, charactersLeft);
136
size_t bytesConverted = convertWideStringToUTF8(p, charactersToConvert, buffer, BUFFER_SIZE);
137
copyToBuffer( buffer, bytesConverted );
138
charactersLeft -= charactersToConvert;
139
p += charactersToConvert;
143
//------------------------------
144
size_t CharacterBuffer::convertWideStringToUTF8( const wchar_t* sourceWideText, size_t sourceWideTextLength, char* targetTextBuffer, size_t targetTextBufferLength )
146
// assert((targetTextBufferLength >= MAX_UTF8_CHAR_LENGTH*sourceWideTextLength));
147
if((targetTextBufferLength < MAX_UTF8_CHAR_LENGTH*sourceWideTextLength))
150
if ( !sourceWideText || (sourceWideTextLength==0) || !targetTextBuffer || (targetTextBufferLength < MAX_UTF8_CHAR_LENGTH*sourceWideTextLength))
155
UTF8* targetstart = reinterpret_cast<UTF8*>( targetTextBuffer );
156
UTF8* thisFirstWChar = targetstart;
157
UTF8* targetend = targetstart + targetTextBufferLength;
158
ConversionResult res = conversionOK;
160
if ( sizeof( wchar_t ) == 2 )
162
const UTF16* sourcestart = reinterpret_cast<const UTF16*>( sourceWideText );
163
const UTF16* sourceend = sourcestart + sourceWideTextLength;
164
res = ConvertUTF16toUTF8( &sourcestart, sourceend, &targetstart, targetend, strictConversion );
166
else if ( sizeof( wchar_t ) == 4 )
168
const UTF32* sourcestart = reinterpret_cast<const UTF32*>( sourceWideText );
169
const UTF32* sourceend = sourcestart + sourceWideTextLength;
170
res = ConvertUTF32toUTF8( &sourcestart, sourceend, &targetstart, targetend, strictConversion );
175
//"Could not convert from wide string to UTF8."
178
if ( res != conversionOK )
181
//"Could not convert from wide string to UTF8."
183
return targetstart - thisFirstWChar;
187
//--------------------------------------------------------------------
188
template<class IntegerType>
189
bool CharacterBuffer::copyIntegerToBufferAsChar( IntegerType i)
191
size_t maxIntLength = Itoa<IntegerType>::MINIMUM_BUFFERSIZE_10;
193
if ( getBytesAvailable() < maxIntLength )
195
//The int might not fit into the buffer. We need to flush first.
199
// Check if the buffer size is large enough
200
// assert(getBytesAvailable() >= maxIntLength);
201
if ( getBytesAvailable() < maxIntLength )
203
//No chance to convert the double with this buffer
207
increaseCurrentPosition( itoa( i, getCurrentPosition(), 10) );
212
//------------------------------
213
bool CharacterBuffer::copyToBufferAsChar( char i )
215
return copyIntegerToBufferAsChar(i);
218
//------------------------------
219
bool CharacterBuffer::copyToBufferAsChar( unsigned char i )
221
return copyIntegerToBufferAsChar(i);
224
//------------------------------
225
bool CharacterBuffer::copyToBufferAsChar( short i )
227
return copyIntegerToBufferAsChar(i);
230
//------------------------------
231
bool CharacterBuffer::copyToBufferAsChar( unsigned short i )
233
return copyIntegerToBufferAsChar(i);
236
//------------------------------
237
bool CharacterBuffer::copyToBufferAsChar( long i )
239
return copyIntegerToBufferAsChar(i);
242
//------------------------------
243
bool CharacterBuffer::copyToBufferAsChar( unsigned long i )
245
return copyIntegerToBufferAsChar(i);
248
//------------------------------
249
bool CharacterBuffer::copyToBufferAsChar( int i )
251
return copyIntegerToBufferAsChar(i);
254
//------------------------------
255
bool CharacterBuffer::copyToBufferAsChar( unsigned int i )
257
return copyIntegerToBufferAsChar(i);
260
//------------------------------
261
bool CharacterBuffer::copyToBufferAsChar( long long i )
263
return copyIntegerToBufferAsChar(i);
266
//------------------------------
267
bool CharacterBuffer::copyToBufferAsChar( unsigned long long i )
269
return copyIntegerToBufferAsChar(i);
274
} // namespace Common