~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to common/libBuffer/src/CommonCharacterBuffer.cpp

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2009 NetAllied Systems GmbH
 
3
 
 
4
    This file is part of Common libBuffer.
 
5
        
 
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
 
9
*/
 
10
 
 
11
#include "CommonCharacterBuffer.h"
 
12
 
 
13
#include <string.h>
 
14
#include <algorithm>
 
15
#include <Commonftoa.h>
 
16
#include <Commondtoa.h>
 
17
#include <Commonitoa.h>
 
18
#include <ConvertUTF.h>
 
19
 
 
20
namespace Common
 
21
{
 
22
 
 
23
        const char CharacterBuffer::DEFAULT_TRUE_STRING[] = "1";
 
24
        const char CharacterBuffer::DEFAULT_FALSE_STRING[] = "0";
 
25
 
 
26
 
 
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)
 
34
        {
 
35
 
 
36
        }
 
37
 
 
38
        //--------------------------------------------------------------------
 
39
        void CharacterBuffer::setTrueString( const char* trueString )
 
40
        {
 
41
                mTrueString = trueString;
 
42
                mTrueStringLength = strlen( trueString );
 
43
        }
 
44
 
 
45
        //--------------------------------------------------------------------
 
46
        void CharacterBuffer::setFalseString( const char* falseString )
 
47
        {
 
48
                mFalseString = falseString;
 
49
                mFalseStringLength = strlen( falseString );
 
50
        }
 
51
 
 
52
        //--------------------------------------------------------------------
 
53
        bool CharacterBuffer::copyToBufferAsChar( float f )
 
54
        {
 
55
                if ( getBytesAvailable() < FTOA_BUFFERSIZE )
 
56
                {
 
57
                        //The float might not fit into the buffer. We need to flush first.
 
58
                        flushBuffer();
 
59
                }
 
60
                
 
61
                // Check if the buffer size is large enough
 
62
//              assert(getBytesAvailable() >= FTOA_BUFFERSIZE);
 
63
                if ( getBytesAvailable() < FTOA_BUFFERSIZE )
 
64
                {
 
65
                        //No chance to convert the float with this buffer
 
66
                        return false;
 
67
                }
 
68
 
 
69
                size_t bytesWritten = ftoa( f, getCurrentPosition() );
 
70
 
 
71
                increaseCurrentPosition( bytesWritten );
 
72
 
 
73
                return true;
 
74
        }
 
75
 
 
76
        //--------------------------------------------------------------------
 
77
        bool CharacterBuffer::copyToBufferAsChar( double d, bool doublePrecision /*= false*/ )
 
78
        {
 
79
                if ( getBytesAvailable() < DTOA_BUFFERSIZE )
 
80
                {
 
81
                        //The double might not fit into the buffer. We need to flush first.
 
82
                        flushBuffer();
 
83
                }
 
84
 
 
85
                // Check if the buffer size is large enough
 
86
//              assert(getBytesAvailable() >= DTOA_BUFFERSIZE);
 
87
                if ( getBytesAvailable() < DTOA_BUFFERSIZE )
 
88
                {
 
89
                        //No chance to convert the double with this buffer
 
90
                        return false;
 
91
                }
 
92
 
 
93
                size_t bytesWritten = dtoa( d, getCurrentPosition(), doublePrecision );
 
94
 
 
95
                increaseCurrentPosition( bytesWritten );
 
96
 
 
97
                return true;
 
98
        }
 
99
 
 
100
        //--------------------------------------------------------------------
 
101
        bool CharacterBuffer::copyToBufferAsChar( bool v )
 
102
        {
 
103
                const char* string = v ? mTrueString : mFalseString;
 
104
                size_t length = v ? mTrueStringLength : mFalseStringLength;
 
105
 
 
106
                if ( getBytesAvailable() < length )
 
107
                {
 
108
                        //The string might not fit into the buffer. We need to flush first.
 
109
                        flushBuffer();
 
110
                }
 
111
 
 
112
                // Check if the buffer size is large enough
 
113
//              assert(getBytesAvailable() >= length);
 
114
                if ( getBytesAvailable() < length )
 
115
                {
 
116
                        //No chance to convert the bool with this buffer
 
117
                        return false;
 
118
                }
 
119
 
 
120
                copyToBuffer( string, length );
 
121
 
 
122
                return true;
 
123
        }
 
124
 
 
125
        //------------------------------
 
126
        void CharacterBuffer::copyToBufferAsChar( const wchar_t* text, size_t length )
 
127
        {
 
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 )
 
134
                {
 
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;
 
140
                }
 
141
        }
 
142
 
 
143
        //------------------------------
 
144
        size_t CharacterBuffer::convertWideStringToUTF8( const wchar_t* sourceWideText, size_t sourceWideTextLength, char* targetTextBuffer, size_t targetTextBufferLength )
 
145
        {
 
146
//              assert((targetTextBufferLength >= MAX_UTF8_CHAR_LENGTH*sourceWideTextLength));
 
147
                if((targetTextBufferLength < MAX_UTF8_CHAR_LENGTH*sourceWideTextLength))
 
148
                        return 0;
 
149
 
 
150
                if ( !sourceWideText || (sourceWideTextLength==0) || !targetTextBuffer || (targetTextBufferLength < MAX_UTF8_CHAR_LENGTH*sourceWideTextLength))
 
151
                {
 
152
                        return 0;
 
153
                }
 
154
 
 
155
                UTF8* targetstart = reinterpret_cast<UTF8*>( targetTextBuffer );
 
156
                UTF8* thisFirstWChar = targetstart;
 
157
                UTF8* targetend = targetstart + targetTextBufferLength;
 
158
                ConversionResult res = conversionOK;
 
159
 
 
160
                if ( sizeof( wchar_t ) == 2 )
 
161
                {
 
162
                        const UTF16* sourcestart = reinterpret_cast<const UTF16*>( sourceWideText );
 
163
                        const UTF16* sourceend = sourcestart + sourceWideTextLength;
 
164
                        res = ConvertUTF16toUTF8( &sourcestart, sourceend, &targetstart, targetend, strictConversion );
 
165
                }
 
166
                else if ( sizeof( wchar_t ) == 4 )
 
167
                {
 
168
                        const UTF32* sourcestart = reinterpret_cast<const UTF32*>( sourceWideText );
 
169
                        const UTF32* sourceend = sourcestart + sourceWideTextLength;
 
170
                        res = ConvertUTF32toUTF8( &sourcestart, sourceend, &targetstart, targetend, strictConversion );
 
171
                }
 
172
                else
 
173
                {
 
174
                        return 0;
 
175
                        //"Could not convert from wide string to UTF8."
 
176
                }
 
177
 
 
178
                if ( res != conversionOK )
 
179
                {
 
180
                        return 0;
 
181
                        //"Could not convert from wide string to UTF8."
 
182
                }
 
183
                return targetstart - thisFirstWChar;
 
184
        }
 
185
 
 
186
 
 
187
        //--------------------------------------------------------------------
 
188
        template<class IntegerType>
 
189
        bool CharacterBuffer::copyIntegerToBufferAsChar( IntegerType i)
 
190
        {
 
191
                size_t maxIntLength = Itoa<IntegerType>::MINIMUM_BUFFERSIZE_10; 
 
192
 
 
193
                if ( getBytesAvailable() < maxIntLength )
 
194
                {
 
195
                        //The int might not fit into the buffer. We need to flush first.
 
196
                        flushBuffer();
 
197
                }
 
198
 
 
199
                // Check if the buffer size is large enough
 
200
//              assert(getBytesAvailable() >= maxIntLength);
 
201
                if ( getBytesAvailable() < maxIntLength )
 
202
                {
 
203
                        //No chance to convert the double with this buffer
 
204
                        return false;
 
205
                }
 
206
 
 
207
                increaseCurrentPosition( itoa( i, getCurrentPosition(), 10) );
 
208
 
 
209
                return true;
 
210
        }
 
211
 
 
212
        //------------------------------
 
213
        bool CharacterBuffer::copyToBufferAsChar( char i )
 
214
        {
 
215
                return copyIntegerToBufferAsChar(i);
 
216
        }
 
217
 
 
218
        //------------------------------
 
219
        bool CharacterBuffer::copyToBufferAsChar( unsigned char i )
 
220
        {
 
221
                return copyIntegerToBufferAsChar(i);
 
222
        }
 
223
 
 
224
        //------------------------------
 
225
        bool CharacterBuffer::copyToBufferAsChar( short i )
 
226
        {
 
227
                return copyIntegerToBufferAsChar(i);
 
228
        }
 
229
 
 
230
        //------------------------------
 
231
        bool CharacterBuffer::copyToBufferAsChar( unsigned short i )
 
232
        {
 
233
                return copyIntegerToBufferAsChar(i);
 
234
        }
 
235
 
 
236
        //------------------------------
 
237
        bool CharacterBuffer::copyToBufferAsChar( long i )
 
238
        {
 
239
                return copyIntegerToBufferAsChar(i);
 
240
        }
 
241
 
 
242
        //------------------------------
 
243
        bool CharacterBuffer::copyToBufferAsChar( unsigned long i )
 
244
        {
 
245
                return copyIntegerToBufferAsChar(i);
 
246
        }
 
247
 
 
248
        //------------------------------
 
249
        bool CharacterBuffer::copyToBufferAsChar( int i )
 
250
        {
 
251
                return copyIntegerToBufferAsChar(i);
 
252
        }
 
253
 
 
254
        //------------------------------
 
255
        bool CharacterBuffer::copyToBufferAsChar( unsigned int i )
 
256
        {
 
257
                return copyIntegerToBufferAsChar(i);
 
258
        }
 
259
 
 
260
        //------------------------------
 
261
        bool CharacterBuffer::copyToBufferAsChar( long long i )
 
262
        {
 
263
                return copyIntegerToBufferAsChar(i);
 
264
        }
 
265
 
 
266
        //------------------------------
 
267
        bool CharacterBuffer::copyToBufferAsChar( unsigned long long i )
 
268
        {
 
269
                return copyIntegerToBufferAsChar(i);
 
270
        }
 
271
 
 
272
 
 
273
 
 
274
} // namespace Common