summaryrefslogtreecommitdiff
path: root/XMPCommon/source/UTF8StringImpl.cpp
blob: e36df2eaca81b32cfd31528c7d6f929b62f1a259 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// =================================================================================================
// Copyright 2014 Adobe
// All Rights Reserved.
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it. If you have received this file from a source other 
// than Adobe, then your use, modification, or distribution of it requires the prior written permission
// of Adobe.
// =================================================================================================

#include <algorithm>

#define IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED 1
	#include "XMPCommon/ImplHeaders/UTF8StringImpl.h"
#undef IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED

#include "XMPCommon/Interfaces/IError_I.h"
#include "XMPCommon/XMPCommonErrorCodes_I.h"
#include "XMPCommon/Utilities/TSmartPointers_I.h"
#include "XMPCommon/Interfaces/IObjectFactory.h"

namespace XMP_COMPONENT_INT_NAMESPACE {
	 static sizet ValidateSrcPosParameter( const spcIUTF8String & src, const sizet & srcPos ) {
		sizet srcSize = src->size();
		if ( srcPos > srcSize ) {
			NOTIFY_ERROR( IError_v1::kEDGeneral, kGECIndexOutOfBounds,
				"srcPos is greater than length of source", IError_v1::kESOperationFatal,
				true, srcPos, true, srcSize );
		}
		return srcSize;
	}

	spIUTF8String APICALL UTF8StringImpl::append( const char * buf, sizet count ) {
		if ( buf && count > 0 ) {
			if ( count == npos ) // assume null terminated string
				mString.append( buf );
			else
				mString.append( buf, count );
		}
		return returnSelfSharedPointer();
	}

	spIUTF8String APICALL UTF8StringImpl::append( const spcIUTF8String & src, sizet srcPos, sizet count ) {
		if ( src && count > 0 ) {
			sizet srcSize = ValidateSrcPosParameter( src, srcPos );
			mString.append( src->c_str() + srcPos, std::min( count, srcSize - srcPos ) );
		}
		return returnSelfSharedPointer();
	}

	spIUTF8String APICALL UTF8StringImpl::assign( const char * buf, sizet count ) {
		if ( buf && count > 0 ) {
			if ( count == npos ) // assume null terminated string
				mString.assign( buf );
			else
				mString.assign( buf, count );
		} else {
			mString.clear();
		}
		return returnSelfSharedPointer();
	}

	spIUTF8String APICALL UTF8StringImpl::assign( const spcIUTF8String & src, sizet srcPos, sizet count ) {
		if ( src && count > 0 ) {
			sizet srcSize = ValidateSrcPosParameter( src, srcPos );
			mString.assign( src->c_str() + srcPos, std::min( count, srcSize - srcPos ) );
		} else {
			mString.clear();
		}
		return returnSelfSharedPointer();
	}

	spIUTF8String APICALL UTF8StringImpl::insert( sizet pos, const char * buf, sizet count ) {
		if ( buf && count > 0 ) {
			sizet size = ValidatePosParameter( pos );
			if ( count == npos )
				mString.insert( pos, buf );
			else
				mString.insert( pos, buf, count );
		}
		return returnSelfSharedPointer();
	}

	spIUTF8String APICALL UTF8StringImpl::insert( sizet pos, const spcIUTF8String & src, sizet srcPos, sizet count ) {
		if ( src && count > 0 ) {
			sizet size = ValidatePosParameter( pos );
			sizet srcSize = ValidateSrcPosParameter( src, srcPos );
			mString.insert( pos, src->c_str() + srcPos, std::min( count, srcSize - srcPos ) );
		}
		return returnSelfSharedPointer();
	}

	spIUTF8String APICALL UTF8StringImpl::erase( sizet pos, sizet count ) {
		if ( count > 0 ) {
			sizet size = ValidatePosParameter( pos );
			mString.erase( pos, count );
		}
		return returnSelfSharedPointer();
	}

	void APICALL UTF8StringImpl::resize( sizet n ) {
		if ( n >= mString.max_size() ) {
			NOTIFY_ERROR( IError_v1::kEDGeneral, kGECParametersNotAsExpected,
				"n is equal or greater than max_size", IError_v1::kESOperationFatal,
				true, n, true, mString.max_size() );
		}
		try {
			mString.resize( n );
		} catch( ... ) {
			NOTIFY_ERROR( IError_v1::kEDMemoryManagement, kMMECAllocationFailure, 
				"failed to allocate required memory", IError_v1::kESOperationFatal, false, false );
		}
	}

	spIUTF8String APICALL UTF8StringImpl::replace( sizet pos, sizet count, const char * buf, sizet srcCount ) {
		if ( buf && count > 0 && srcCount > 0 ) {
			sizet size = ValidatePosParameter( pos );
			if ( srcCount == npos )
				mString.replace( pos, count, buf );
			else
				mString.replace( pos, count, buf, srcCount );
		}
		return returnSelfSharedPointer();
	}

	spIUTF8String APICALL UTF8StringImpl::replace( sizet pos, sizet count, const spcIUTF8String & src, sizet srcPos, sizet srcCount ) {
		if ( src && count > 0 && srcCount > 0 ) {
			sizet size = ValidatePosParameter( pos );
			sizet srcSize = ValidateSrcPosParameter( src, srcPos );
			mString.replace( pos, count, src->c_str() + srcPos, std::min( srcCount, srcSize - srcPos ) );
		}
		return returnSelfSharedPointer();
	}

	sizet APICALL UTF8StringImpl::copy( char * buf, sizet len, sizet pos ) const {
		if ( buf && len > 0 ) {
			ValidatePosParameter( pos );
			return mString.copy( buf, len, pos );
		}
		return 0;
	}

	sizet APICALL UTF8StringImpl::find( const char * buf, sizet pos, sizet count ) const {
		if ( buf && count > 0 ) {
			return mString.find( buf, pos, count );
		}
		return npos;
	}

	sizet APICALL UTF8StringImpl::find( const spcIUTF8String & src, sizet pos, sizet count ) const {
		if ( src && src->size() > 0 && count > 0 ) {
			return mString.find( src->c_str(), pos, count );
		}
		return npos;
	}

	sizet APICALL UTF8StringImpl::rfind( const char * buf, sizet pos, sizet count ) const {
		if ( buf && count > 0 ) {
			return mString.rfind( buf, pos, count );
		}
		return npos;
	}

	sizet APICALL UTF8StringImpl::rfind( const spcIUTF8String & src, sizet pos, sizet count ) const {
		if ( src && src->size() > 0 && count > 0 ) {
			return mString.rfind( src->c_str(), pos, count );
		}
		return npos;
	}

	int32 APICALL UTF8StringImpl::compare( sizet pos, sizet len, const char * buf, sizet count ) const {
		if ( buf && (len > 0||len ==0) && (count > 0 || count==0) ) {
			sizet size = ValidatePosParameter( pos );
			if ( count == npos )
				return mString.compare( pos, len, buf );
			else
				return mString.compare( pos, len, buf, count );
		}
		return -1;
	}

	int32 APICALL UTF8StringImpl::compare( sizet pos, sizet len, const spcIUTF8String & str, sizet strPos, sizet strLen ) const {
		if ( str && (len > 0||len ==0) && (strLen > 0||strLen ==0) ) {
			ValidatePosParameter( pos );
			ValidateSrcPosParameter( str, strPos );
			return mString.compare( pos, len, str->c_str() + strPos, strLen );
		}
		return -1;
	}

	spIUTF8String APICALL UTF8StringImpl::substr( sizet pos, sizet count ) const {
		ValidatePosParameter( pos );
		try {
			return CreateUTF8String( this->c_str() + pos, std::min( count, this->size() - pos ) );
		} catch ( ... ) {
			NOTIFY_ERROR( IError_v1::kEDMemoryManagement, kMMECAllocationFailure,
				"failed to allocate required memory", IError_v1::kESOperationFatal, false, false );
		}
		return spIUTF8String();
	}

	bool APICALL UTF8StringImpl::empty() const {
		return mString.empty();
	}

	const char * APICALL UTF8StringImpl::c_str() const __NOTHROW__ {
		return mString.c_str();
	}

	void APICALL UTF8StringImpl::clear() __NOTHROW__ {
		mString.clear();
	}

	sizet APICALL UTF8StringImpl::size() const __NOTHROW__ {
		return mString.size();
	}

	spIUTF8String UTF8StringImpl::returnSelfSharedPointer() {
		try {
			return shared_from_this();
		} catch ( bad_weak_ptr & ) {
			return spIUTF8String();
		}
	}

	sizet UTF8StringImpl::ValidatePosParameter( const sizet & pos ) const {
		sizet size = this->size();
		if ( pos > size ) {
			NOTIFY_ERROR( IError_v1::kEDGeneral, kGECIndexOutOfBounds,
				"pos mentioned is out of bounds", IError_v1::kESOperationFatal,
				true, pos, true, size );
		}
		return size;
	}

	spIUTF8String IUTF8String_I::CreateUTF8String( const char * buf /* = NULL */, sizet count /* = npos */ ) {
		auto sp = MakeUncheckedSharedPointer( new UTF8StringImpl(), __FILE__, __LINE__, true );
		sp->assign( buf, count );
		return sp;
	}

	TAllocator< spIUTF8String > gTAllocatorUTF8String;

}

#if BUILDING_XMPCOMMON_LIB || SOURCE_COMPILING_XMP_ALL
namespace AdobeXMPCommon {
	spIUTF8String IUTF8String_v1::MakeShared( pIUTF8String_base ptr ) {
		if ( !ptr ) return spIUTF8String();
		pIUTF8String p = IUTF8String::GetInterfaceVersion() > 1 ? ptr->GetInterfacePointer< IUTF8String >() : ptr;
		return XMP_COMPONENT_INT_NAMESPACE::MakeUncheckedSharedPointer( p, __FILE__, __LINE__, true );
	}

	spIUTF8String IUTF8String_v1::CreateUTF8String( pIObjectFactory objFactory ) {
		pcIError error( NULL );
		auto retValue = MakeShared( objFactory->CreateUTF8String( NULL, AdobeXMPCommon::npos, error ) );
		if ( error ) throw IError::MakeShared( error );
		return retValue;
	}

	spIUTF8String IUTF8String_v1::CreateUTF8String( pIObjectFactory objFactory, const char * buf, sizet count ) {
		pcIError error( NULL );
		auto retValue = MakeShared( objFactory->CreateUTF8String( buf, count, error ) );
		if ( error ) throw IError::MakeShared( error );
		return retValue;
	}

}
#endif  // BUILDING_XMPCOMMON_LIB || SOURCE_COMPILING_XMP_ALL