Update ooo320-m1
[ooovba.git] / tools / source / ref / globname.cxx
blob1f27b4af95bbec5e4f103fa673e928c3475f305c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: globname.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_tools.hxx"
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <string.h>
38 #include <tools/stream.hxx>
39 #include <tools/globname.hxx>
41 /************** class ImpSvGlobalName ************************************/
42 ImpSvGlobalName::ImpSvGlobalName( const ImpSvGlobalName & rObj )
44 nRefCount = 0;
45 memcpy( szData, rObj.szData, sizeof( szData ) );
48 /************** class ImpSvGlobalName ************************************/
49 ImpSvGlobalName::ImpSvGlobalName( int )
51 nRefCount = 1;
52 memset( szData, 0, sizeof( szData ) );
55 /*************************************************************************
56 |* ImpSvGlobalName::operator ==()
57 *************************************************************************/
58 BOOL ImpSvGlobalName::operator == ( const ImpSvGlobalName & rObj ) const
60 return !memcmp( szData, rObj.szData, sizeof( szData ) );
63 /*************************************************************************
64 |* SvGlobalName::SvGlobalName()
65 *************************************************************************/
66 SvGlobalName::SvGlobalName()
68 static ImpSvGlobalName aNoName( 0 );
70 pImp = &aNoName;
71 pImp->nRefCount++;
74 // locker die Struktur von Windows kopiert
75 #ifdef WNT
76 struct _GUID
77 #else
78 struct GUID
79 #endif
81 UINT32 Data1;
82 UINT16 Data2;
83 UINT16 Data3;
84 BYTE Data4[8];
86 SvGlobalName::SvGlobalName( const CLSID & rId )
88 pImp = new ImpSvGlobalName();
89 pImp->nRefCount++;
90 memcpy( pImp->szData, &rId, sizeof( pImp->szData ) );
93 SvGlobalName::SvGlobalName( UINT32 n1, USHORT n2, USHORT n3,
94 BYTE b8, BYTE b9, BYTE b10, BYTE b11,
95 BYTE b12, BYTE b13, BYTE b14, BYTE b15 )
97 pImp = new ImpSvGlobalName();
98 pImp->nRefCount++;
100 *(UINT32 *)pImp->szData = n1;
101 *(USHORT *)&pImp->szData[ 4 ] = n2;
102 *(USHORT *)&pImp->szData[ 6 ] = n3;
103 pImp->szData[ 8 ] = b8;
104 pImp->szData[ 9 ] = b9;
105 pImp->szData[ 10 ] = b10;
106 pImp->szData[ 11 ] = b11;
107 pImp->szData[ 12 ] = b12;
108 pImp->szData[ 13 ] = b13;
109 pImp->szData[ 14 ] = b14;
110 pImp->szData[ 15 ] = b15;
113 /*************************************************************************
114 |* SvGlobalName::~SvGlobalName()
115 *************************************************************************/
116 SvGlobalName::~SvGlobalName()
118 pImp->nRefCount--;
119 if( !pImp->nRefCount )
120 delete pImp;
123 /*************************************************************************
124 |* SvGlobalName::operator = ()
125 *************************************************************************/
126 SvGlobalName & SvGlobalName::operator = ( const SvGlobalName & rObj )
128 rObj.pImp->nRefCount++;
129 pImp->nRefCount--;
130 if( !pImp->nRefCount )
131 delete pImp;
132 pImp = rObj.pImp;
133 return *this;
136 /*************************************************************************
137 |* SvGlobalName::NewImp()
138 *************************************************************************/
139 void SvGlobalName::NewImp()
141 if( pImp->nRefCount > 1 )
143 pImp->nRefCount--;
144 pImp = new ImpSvGlobalName( *pImp );
145 pImp->nRefCount++;
149 /*************************************************************************
150 |* SvGlobalName::operator << ()
151 |* SvGlobalName::operator >> ()
152 *************************************************************************/
153 SvStream& operator << ( SvStream& rOStr, const SvGlobalName & rObj )
155 rOStr << *(UINT32 *)rObj.pImp->szData;
156 rOStr << *(USHORT *)&rObj.pImp->szData[ 4 ];
157 rOStr << *(USHORT *)&rObj.pImp->szData[ 6 ];
158 rOStr.Write( (sal_Char *)&rObj.pImp->szData[ 8 ], 8 );
159 return rOStr;
162 SvStream& operator >> ( SvStream& rStr, SvGlobalName & rObj )
164 rObj.NewImp(); // kopieren, falls noetig
165 rStr >> *(UINT32 *)rObj.pImp->szData;
166 rStr >> *(USHORT *)&rObj.pImp->szData[ 4 ];
167 rStr >> *(USHORT *)&rObj.pImp->szData[ 6 ];
168 rStr.Read( (sal_Char *)&rObj.pImp->szData[ 8 ], 8 );
169 return rStr;
173 /*************************************************************************
174 |* SvGlobalName::operator < ()
175 *************************************************************************/
176 BOOL SvGlobalName::operator < ( const SvGlobalName & rObj ) const
178 int n = memcmp( pImp->szData +6, rObj.pImp->szData +6,
179 sizeof( pImp->szData ) -6);
180 if( n < 0 )
181 return TRUE;
182 else if( n > 0 )
183 return FALSE;
184 else if( *(USHORT *)&pImp->szData[ 4 ] < *(USHORT *)&rObj.pImp->szData[ 4 ] )
185 return TRUE;
186 else if( *(USHORT *)&pImp->szData[ 4 ] == *(USHORT *)&rObj.pImp->szData[ 4 ] )
187 return *(UINT32 *)pImp->szData < *(UINT32 *)rObj.pImp->szData;
188 else
189 return FALSE;
193 /*************************************************************************
194 |* SvGlobalName::operator +=()
195 *************************************************************************/
196 SvGlobalName & SvGlobalName::operator += ( UINT32 n )
198 NewImp();
199 UINT32 nOld = (*(UINT32 *)pImp->szData);
200 (*(UINT32 *)pImp->szData) += n;
201 if( nOld > *(UINT32 *)pImp->szData )
202 // ueberlauf
203 (*(USHORT *)&pImp->szData[ 4 ])++;
204 return *this;
207 /*************************************************************************
208 |* SvGlobalName::operator ==()
209 *************************************************************************/
210 BOOL SvGlobalName::operator == ( const SvGlobalName & rObj ) const
212 return *pImp == *rObj.pImp;
215 void SvGlobalName::MakeFromMemory( void * pData )
217 NewImp();
218 memcpy( pImp->szData, pData, sizeof( pImp->szData ) );
221 /*************************************************************************
222 |* SvGlobalName::MakeId()
223 *************************************************************************/
224 BOOL SvGlobalName::MakeId( const String & rIdStr )
226 ByteString aStr( rIdStr, RTL_TEXTENCODING_ASCII_US );
227 sal_Char * pStr = (sal_Char *)aStr.GetBuffer();
228 if( rIdStr.Len() == 36
229 && '-' == pStr[ 8 ] && '-' == pStr[ 13 ]
230 && '-' == pStr[ 18 ] && '-' == pStr[ 23 ] )
232 UINT32 nFirst = 0;
233 int i = 0;
234 for( i = 0; i < 8; i++ )
236 if( isxdigit( *pStr ) )
237 if( isdigit( *pStr ) )
238 nFirst = nFirst * 16 + (*pStr - '0');
239 else
240 nFirst = nFirst * 16 + (toupper( *pStr ) - 'A' + 10 );
241 else
242 return FALSE;
243 pStr++;
246 UINT16 nSec = 0;
247 pStr++;
248 for( i = 0; i < 4; i++ )
250 if( isxdigit( *pStr ) )
251 if( isdigit( *pStr ) )
252 nSec = nSec * 16 + (*pStr - '0');
253 else
254 nSec = nSec * 16 + (UINT16)(toupper( *pStr ) - 'A' + 10 );
255 else
256 return FALSE;
257 pStr++;
260 UINT16 nThird = 0;
261 pStr++;
262 for( i = 0; i < 4; i++ )
264 if( isxdigit( *pStr ) )
265 if( isdigit( *pStr ) )
266 nThird = nThird * 16 + (*pStr - '0');
267 else
268 nThird = nThird * 16 + (UINT16)(toupper( *pStr ) - 'A' + 10 );
269 else
270 return FALSE;
271 pStr++;
274 BYTE szRemain[ 8 ];
275 memset( szRemain, 0, sizeof( szRemain ) );
276 pStr++;
277 for( i = 0; i < 16; i++ )
279 if( isxdigit( *pStr ) )
280 if( isdigit( *pStr ) )
281 szRemain[i/2] = szRemain[i/2] * 16 + (*pStr - '0');
282 else
283 szRemain[i/2] = szRemain[i/2] * 16 + (BYTE)(toupper( *pStr ) - 'A' + 10 );
284 else
285 return FALSE;
286 pStr++;
287 if( i == 3 )
288 pStr++;
291 NewImp();
292 *(UINT32 *)pImp->szData = nFirst;
293 *(USHORT *)&pImp->szData[ 4 ] = nSec;
294 *(USHORT *)&pImp->szData[ 6 ] = nThird;
295 memcpy( &pImp->szData[ 8 ], szRemain, 8 );
296 return TRUE;
298 return FALSE;
301 /*************************************************************************
302 |* SvGlobalName::GetctorName()
303 *************************************************************************/
304 String SvGlobalName::GetctorName() const
306 ByteString aRet;
308 sal_Char buf[ 20 ];
309 sprintf( buf, "0x%8.8lX", (ULONG)*(UINT32 *)pImp->szData );
310 aRet += buf;
311 USHORT i;
312 for( i = 4; i < 8; i += 2 )
314 aRet += ',';
315 sprintf( buf, "0x%4.4X", *(USHORT *)&pImp->szData[ i ] );
316 aRet += buf;
318 for( i = 8; i < 16; i++ )
320 aRet += ',';
321 sprintf( buf, "0x%2.2x", pImp->szData[ i ] );
322 aRet += buf;
324 return String( aRet, RTL_TEXTENCODING_ASCII_US );
327 /*************************************************************************
328 |* SvGlobalName::GetHexName()
329 *************************************************************************/
330 String SvGlobalName::GetHexName() const
332 ByteString aRet;
334 sal_Char buf[ 10 ];
335 sprintf( buf, "%8.8lX", (ULONG)*(UINT32 *)pImp->szData );
336 aRet += buf;
337 aRet += '-';
338 USHORT i ;
339 for( i = 4; i < 8; i += 2 )
341 sprintf( buf, "%4.4X", *(USHORT *)&pImp->szData[ i ] );
342 aRet += buf;
343 aRet += '-';
345 for( i = 8; i < 10; i++ )
347 sprintf( buf, "%2.2x", pImp->szData[ i ] );
348 aRet += buf;
350 aRet += '-';
351 for( i = 10; i < 16; i++ )
353 sprintf( buf, "%2.2x", pImp->szData[ i ] );
354 aRet += buf;
356 return String( aRet, RTL_TEXTENCODING_ASCII_US );
359 /************** SvGlobalNameList ****************************************/
360 /************************************************************************/
361 /*************************************************************************
362 |* SvGlobalNameList::SvGlobalNameList()
363 *************************************************************************/
364 SvGlobalNameList::SvGlobalNameList()
365 : aList( 1, 1 )
369 /*************************************************************************
370 |* SvGlobalNameList::~SvGlobalNameList()
371 *************************************************************************/
372 SvGlobalNameList::~SvGlobalNameList()
374 for( ULONG i = Count(); i > 0; i-- )
376 ImpSvGlobalName * pImp = (ImpSvGlobalName *)aList.GetObject( i -1 );
377 pImp->nRefCount--;
378 if( !pImp->nRefCount )
379 delete pImp;
383 /*************************************************************************
384 |* SvGlobalNameList::Append()
385 *************************************************************************/
386 void SvGlobalNameList::Append( const SvGlobalName & rName )
388 rName.pImp->nRefCount++;
389 aList.Insert( rName.pImp, LIST_APPEND );
392 /*************************************************************************
393 |* SvGlobalNameList::GetObject()
394 *************************************************************************/
395 SvGlobalName SvGlobalNameList::GetObject( ULONG nPos )
397 return SvGlobalName( (ImpSvGlobalName *)aList.GetObject( nPos ) );
400 /*************************************************************************
401 |* SvGlobalNameList::IsEntry()
402 *************************************************************************/
403 BOOL SvGlobalNameList::IsEntry( const SvGlobalName & rName )
405 for( ULONG i = Count(); i > 0; i-- )
407 if( *rName.pImp == *(ImpSvGlobalName *)aList.GetObject( i -1 ) )
408 return TRUE;
410 return FALSE;
413 com::sun::star::uno::Sequence < sal_Int8 > SvGlobalName::GetByteSequence() const
415 // platform independent representation of a "GlobalName"
416 // maybe transported remotely
417 com::sun::star::uno::Sequence< sal_Int8 > aResult( 16 );
419 aResult[0] = (sal_Int8) (*(UINT32 *)pImp->szData >> 24);
420 aResult[1] = (sal_Int8) ((*(UINT32 *)pImp->szData << 8 ) >> 24);
421 aResult[2] = (sal_Int8) ((*(UINT32 *)pImp->szData << 16 ) >> 24);
422 aResult[3] = (sal_Int8) ((*(UINT32 *)pImp->szData << 24 ) >> 24);
423 aResult[4] = (sal_Int8) (*(USHORT *)&pImp->szData[ 4 ] >> 8);
424 aResult[5] = (sal_Int8) ((*(USHORT *)&pImp->szData[ 4 ] << 8 ) >> 8);
425 aResult[6] = (sal_Int8) (*(USHORT *)&pImp->szData[ 6 ] >> 8);
426 aResult[7] = (sal_Int8) ((*(USHORT *)&pImp->szData[ 6 ] << 8 ) >> 8);
427 aResult[8] = pImp->szData[ 8 ];
428 aResult[9] = pImp->szData[ 9 ];
429 aResult[10] = pImp->szData[ 10 ];
430 aResult[11] = pImp->szData[ 11 ];
431 aResult[12] = pImp->szData[ 12 ];
432 aResult[13] = pImp->szData[ 13 ];
433 aResult[14] = pImp->szData[ 14 ];
434 aResult[15] = pImp->szData[ 15 ];
436 return aResult;
439 SvGlobalName::SvGlobalName( const com::sun::star::uno::Sequence < sal_Int8 >& aSeq )
441 // create SvGlobalName from a platform independent representation
442 GUID aResult;
443 memset( &aResult, 0, sizeof( aResult ) );
444 if ( aSeq.getLength() == 16 )
446 aResult.Data1 = ( ( ( ( ( ( sal_uInt8 )aSeq[0] << 8 ) + ( sal_uInt8 )aSeq[1] ) << 8 ) + ( sal_uInt8 )aSeq[2] ) << 8 ) + ( sal_uInt8 )aSeq[3];
447 aResult.Data2 = ( ( sal_uInt8 )aSeq[4] << 8 ) + ( sal_uInt8 )aSeq[5];
448 aResult.Data3 = ( ( sal_uInt8 )aSeq[6] << 8 ) + ( sal_uInt8 )aSeq[7];
449 for( int nInd = 0; nInd < 8; nInd++ )
450 aResult.Data4[nInd] = ( sal_uInt8 )aSeq[nInd+8];
453 pImp = new ImpSvGlobalName();
454 pImp->nRefCount++;
455 memcpy( pImp->szData, &aResult, sizeof( pImp->szData ) );