xref: /trunk/main/tools/source/ref/globname.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_tools.hxx"
30 
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include <tools/stream.hxx>
36 #include <tools/globname.hxx>
37 
38 /************** class ImpSvGlobalName ************************************/
39 ImpSvGlobalName::ImpSvGlobalName( const ImpSvGlobalName & rObj )
40 {
41     nRefCount = 0;
42     memcpy( szData, rObj.szData, sizeof( szData ) );
43 }
44 
45 /************** class ImpSvGlobalName ************************************/
46 ImpSvGlobalName::ImpSvGlobalName( int )
47 {
48     nRefCount = 1;
49     memset( szData, 0, sizeof( szData ) );
50 }
51 
52 /*************************************************************************
53 |*    ImpSvGlobalName::operator ==()
54 *************************************************************************/
55 sal_Bool ImpSvGlobalName::operator == ( const ImpSvGlobalName & rObj ) const
56 {
57     return !memcmp( szData, rObj.szData, sizeof( szData ) );
58 }
59 
60 /*************************************************************************
61 |*    SvGlobalName::SvGlobalName()
62 *************************************************************************/
63 SvGlobalName::SvGlobalName()
64 {
65     static ImpSvGlobalName aNoName( 0 );
66 
67     pImp = &aNoName;
68     pImp->nRefCount++;
69 }
70 
71 // locker die Struktur von Windows kopiert
72 #ifdef WNT
73 struct _GUID
74 #else
75 struct GUID
76 #endif
77 {
78     sal_uInt32 Data1;
79     sal_uInt16 Data2;
80     sal_uInt16 Data3;
81     sal_uInt8  Data4[8];
82 };
83 SvGlobalName::SvGlobalName( const CLSID & rId )
84 {
85     pImp = new ImpSvGlobalName();
86     pImp->nRefCount++;
87     memcpy( pImp->szData, &rId, sizeof( pImp->szData ) );
88 }
89 
90 SvGlobalName::SvGlobalName( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
91                             sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
92                             sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 )
93 {
94     pImp = new ImpSvGlobalName();
95     pImp->nRefCount++;
96 
97     *(sal_uInt32 *)pImp->szData       = n1;
98     *(sal_uInt16 *)&pImp->szData[ 4 ] = n2;
99     *(sal_uInt16 *)&pImp->szData[ 6 ] = n3;
100     pImp->szData[ 8  ] = b8;
101     pImp->szData[ 9  ] = b9;
102     pImp->szData[ 10 ] = b10;
103     pImp->szData[ 11 ] = b11;
104     pImp->szData[ 12 ] = b12;
105     pImp->szData[ 13 ] = b13;
106     pImp->szData[ 14 ] = b14;
107     pImp->szData[ 15 ] = b15;
108 }
109 
110 /*************************************************************************
111 |*    SvGlobalName::~SvGlobalName()
112 *************************************************************************/
113 SvGlobalName::~SvGlobalName()
114 {
115     pImp->nRefCount--;
116     if( !pImp->nRefCount )
117         delete pImp;
118 }
119 
120 /*************************************************************************
121 |*    SvGlobalName::operator = ()
122 *************************************************************************/
123 SvGlobalName & SvGlobalName::operator = ( const SvGlobalName & rObj )
124 {
125     rObj.pImp->nRefCount++;
126     pImp->nRefCount--;
127     if( !pImp->nRefCount )
128         delete pImp;
129     pImp = rObj.pImp;
130     return *this;
131 }
132 
133 /*************************************************************************
134 |*    SvGlobalName::NewImp()
135 *************************************************************************/
136 void SvGlobalName::NewImp()
137 {
138     if( pImp->nRefCount > 1 )
139     {
140         pImp->nRefCount--;
141         pImp = new ImpSvGlobalName( *pImp );
142         pImp->nRefCount++;
143     }
144 }
145 
146 /*************************************************************************
147 |*    SvGlobalName::operator << ()
148 |*    SvGlobalName::operator >> ()
149 *************************************************************************/
150 SvStream& operator << ( SvStream& rOStr, const SvGlobalName & rObj )
151 {
152     rOStr << *(sal_uInt32 *)rObj.pImp->szData;
153     rOStr << *(sal_uInt16 *)&rObj.pImp->szData[ 4 ];
154     rOStr << *(sal_uInt16 *)&rObj.pImp->szData[ 6 ];
155     rOStr.Write( (sal_Char *)&rObj.pImp->szData[ 8 ], 8 );
156     return rOStr;
157 }
158 
159 SvStream& operator >> ( SvStream& rStr, SvGlobalName & rObj )
160 {
161     rObj.NewImp(); // kopieren, falls noetig
162     rStr >> *(sal_uInt32 *)rObj.pImp->szData;
163     rStr >> *(sal_uInt16 *)&rObj.pImp->szData[ 4 ];
164     rStr >> *(sal_uInt16 *)&rObj.pImp->szData[ 6 ];
165     rStr.Read( (sal_Char *)&rObj.pImp->szData[ 8 ], 8 );
166     return rStr;
167 }
168 
169 
170 /*************************************************************************
171 |*    SvGlobalName::operator < ()
172 *************************************************************************/
173 sal_Bool SvGlobalName::operator < ( const SvGlobalName & rObj ) const
174 {
175     int n = memcmp( pImp->szData +6, rObj.pImp->szData +6,
176                     sizeof( pImp->szData ) -6);
177     if( n < 0 )
178         return sal_True;
179     else if( n > 0 )
180         return sal_False;
181     else if( *(sal_uInt16 *)&pImp->szData[ 4 ] < *(sal_uInt16 *)&rObj.pImp->szData[ 4 ] )
182         return sal_True;
183     else if( *(sal_uInt16 *)&pImp->szData[ 4 ] == *(sal_uInt16 *)&rObj.pImp->szData[ 4 ] )
184         return *(sal_uInt32 *)pImp->szData  < *(sal_uInt32 *)rObj.pImp->szData;
185     else
186         return sal_False;
187 
188 }
189 
190 /*************************************************************************
191 |*    SvGlobalName::operator +=()
192 *************************************************************************/
193 SvGlobalName & SvGlobalName::operator += ( sal_uInt32 n )
194 {
195     NewImp();
196     sal_uInt32 nOld = (*(sal_uInt32 *)pImp->szData);
197     (*(sal_uInt32 *)pImp->szData) += n;
198     if( nOld > *(sal_uInt32 *)pImp->szData )
199         // ueberlauf
200         (*(sal_uInt16 *)&pImp->szData[ 4 ])++;
201     return *this;
202 }
203 
204 /*************************************************************************
205 |*    SvGlobalName::operator ==()
206 *************************************************************************/
207 sal_Bool SvGlobalName::operator == ( const SvGlobalName & rObj ) const
208 {
209     return *pImp == *rObj.pImp;
210 }
211 
212 void SvGlobalName::MakeFromMemory( void * pData )
213 {
214     NewImp();
215     memcpy( pImp->szData, pData, sizeof( pImp->szData ) );
216 }
217 
218 /*************************************************************************
219 |*    SvGlobalName::MakeId()
220 *************************************************************************/
221 sal_Bool SvGlobalName::MakeId( const String & rIdStr )
222 {
223     ByteString  aStr( rIdStr, RTL_TEXTENCODING_ASCII_US );
224     sal_Char * pStr = (sal_Char *)aStr.GetBuffer();
225     if( rIdStr.Len() == 36
226       && '-' == pStr[ 8 ]  && '-' == pStr[ 13 ]
227       && '-' == pStr[ 18 ] && '-' == pStr[ 23 ] )
228     {
229         sal_uInt32 nFirst = 0;
230         int i = 0;
231         for( i = 0; i < 8; i++ )
232         {
233             if( isxdigit( *pStr ) )
234                 if( isdigit( *pStr ) )
235                     nFirst = nFirst * 16 + (*pStr - '0');
236                 else
237                     nFirst = nFirst * 16 + (toupper( *pStr ) - 'A' + 10 );
238             else
239                 return sal_False;
240             pStr++;
241         }
242 
243         sal_uInt16 nSec = 0;
244         pStr++;
245         for( i = 0; i < 4; i++ )
246         {
247             if( isxdigit( *pStr ) )
248                 if( isdigit( *pStr ) )
249                     nSec = nSec * 16 + (*pStr - '0');
250                 else
251                     nSec = nSec * 16 + (sal_uInt16)(toupper( *pStr ) - 'A' + 10 );
252             else
253                 return sal_False;
254             pStr++;
255         }
256 
257         sal_uInt16 nThird = 0;
258         pStr++;
259         for( i = 0; i < 4; i++ )
260         {
261             if( isxdigit( *pStr ) )
262                 if( isdigit( *pStr ) )
263                     nThird = nThird * 16 + (*pStr - '0');
264                 else
265                     nThird = nThird * 16 + (sal_uInt16)(toupper( *pStr ) - 'A' + 10 );
266             else
267                 return sal_False;
268             pStr++;
269         }
270 
271         sal_Int8 szRemain[ 8 ];
272         memset( szRemain, 0, sizeof( szRemain ) );
273         pStr++;
274         for( i = 0; i < 16; i++ )
275         {
276             if( isxdigit( *pStr ) )
277                 if( isdigit( *pStr ) )
278                     szRemain[i/2] = szRemain[i/2] * 16 + (*pStr - '0');
279                 else
280                     szRemain[i/2] = szRemain[i/2] * 16 + (sal_Int8)(toupper( *pStr ) - 'A' + 10 );
281             else
282                 return sal_False;
283             pStr++;
284             if( i == 3 )
285                 pStr++;
286         }
287 
288         NewImp();
289         *(sal_uInt32 *)pImp->szData       = nFirst;
290         *(sal_uInt16 *)&pImp->szData[ 4 ] = nSec;
291         *(sal_uInt16 *)&pImp->szData[ 6 ] = nThird;
292         memcpy( &pImp->szData[ 8 ], szRemain, 8 );
293         return sal_True;
294     }
295     return sal_False;
296 }
297 
298 /*************************************************************************
299 |*    SvGlobalName::GetctorName()
300 *************************************************************************/
301 String SvGlobalName::GetctorName() const
302 {
303     ByteString aRet;
304 
305     sal_Char buf[ 20 ];
306     sprintf( buf, "0x%8.8lX", (sal_uIntPtr)*(sal_uInt32 *)pImp->szData );
307     aRet += buf;
308     sal_uInt16 i;
309     for( i = 4; i < 8; i += 2 )
310     {
311         aRet += ',';
312         sprintf( buf, "0x%4.4X", *(sal_uInt16 *)&pImp->szData[ i ] );
313         aRet += buf;
314     }
315     for( i = 8; i < 16; i++ )
316     {
317         aRet += ',';
318         sprintf( buf, "0x%2.2x", pImp->szData[ i ] );
319         aRet += buf;
320     }
321     return String( aRet, RTL_TEXTENCODING_ASCII_US );
322 }
323 
324 /*************************************************************************
325 |*    SvGlobalName::GetHexName()
326 *************************************************************************/
327 String SvGlobalName::GetHexName() const
328 {
329     ByteString aRet;
330 
331     sal_Char buf[ 10 ];
332     sprintf( buf, "%8.8lX", (sal_uIntPtr)*(sal_uInt32 *)pImp->szData );
333     aRet += buf;
334     aRet += '-';
335     sal_uInt16 i ;
336     for( i = 4; i < 8; i += 2 )
337     {
338         sprintf( buf, "%4.4X", *(sal_uInt16 *)&pImp->szData[ i ] );
339         aRet += buf;
340         aRet += '-';
341     }
342     for( i = 8; i < 10; i++ )
343     {
344         sprintf( buf, "%2.2x", pImp->szData[ i ] );
345         aRet += buf;
346     }
347     aRet += '-';
348     for( i = 10; i < 16; i++ )
349     {
350         sprintf( buf, "%2.2x", pImp->szData[ i ] );
351         aRet += buf;
352     }
353     return String( aRet, RTL_TEXTENCODING_ASCII_US );
354 }
355 
356 /************** SvGlobalNameList ****************************************/
357 /************************************************************************/
358 /*************************************************************************
359 |*    SvGlobalNameList::SvGlobalNameList()
360 *************************************************************************/
361 SvGlobalNameList::SvGlobalNameList()
362     : aList( 1, 1 )
363 {
364 }
365 
366 /*************************************************************************
367 |*    SvGlobalNameList::~SvGlobalNameList()
368 *************************************************************************/
369 SvGlobalNameList::~SvGlobalNameList()
370 {
371     for( sal_uIntPtr i = Count(); i > 0; i-- )
372     {
373         ImpSvGlobalName * pImp = (ImpSvGlobalName *)aList.GetObject( i -1 );
374         pImp->nRefCount--;
375         if( !pImp->nRefCount )
376             delete pImp;
377     }
378 }
379 
380 /*************************************************************************
381 |*    SvGlobalNameList::Append()
382 *************************************************************************/
383 void SvGlobalNameList::Append( const SvGlobalName & rName )
384 {
385     rName.pImp->nRefCount++;
386     aList.Insert( rName.pImp, LIST_APPEND );
387 }
388 
389 /*************************************************************************
390 |*    SvGlobalNameList::GetObject()
391 *************************************************************************/
392 SvGlobalName SvGlobalNameList::GetObject( sal_uLong nPos )
393 {
394     return SvGlobalName( (ImpSvGlobalName *)aList.GetObject( nPos ) );
395 }
396 
397 /*************************************************************************
398 |*    SvGlobalNameList::IsEntry()
399 *************************************************************************/
400 sal_Bool SvGlobalNameList::IsEntry( const SvGlobalName & rName )
401 {
402     for( sal_uIntPtr i = Count(); i > 0; i-- )
403     {
404         if( *rName.pImp == *(ImpSvGlobalName *)aList.GetObject( i -1 ) )
405             return sal_True;
406     }
407     return sal_False;
408 }
409 
410 com::sun::star::uno::Sequence < sal_Int8 > SvGlobalName::GetByteSequence() const
411 {
412     // platform independent representation of a "GlobalName"
413     // maybe transported remotely
414     com::sun::star::uno::Sequence< sal_Int8 > aResult( 16 );
415 
416     aResult[0] = (sal_Int8) (*(sal_uInt32 *)pImp->szData >> 24);
417     aResult[1] = (sal_Int8) ((*(sal_uInt32 *)pImp->szData << 8 ) >> 24);
418     aResult[2] = (sal_Int8) ((*(sal_uInt32 *)pImp->szData << 16 ) >> 24);
419     aResult[3] = (sal_Int8) ((*(sal_uInt32 *)pImp->szData << 24 ) >> 24);
420     aResult[4] = (sal_Int8) (*(sal_uInt16 *)&pImp->szData[ 4 ] >> 8);
421     aResult[5] = (sal_Int8) ((*(sal_uInt16 *)&pImp->szData[ 4 ] << 8 ) >> 8);
422     aResult[6] = (sal_Int8) (*(sal_uInt16 *)&pImp->szData[ 6 ] >> 8);
423     aResult[7] = (sal_Int8) ((*(sal_uInt16 *)&pImp->szData[ 6 ] << 8 ) >> 8);
424     aResult[8] = pImp->szData[ 8 ];
425     aResult[9] = pImp->szData[ 9 ];
426     aResult[10] = pImp->szData[ 10 ];
427     aResult[11] = pImp->szData[ 11 ];
428     aResult[12] = pImp->szData[ 12 ];
429     aResult[13] = pImp->szData[ 13 ];
430     aResult[14] = pImp->szData[ 14 ];
431     aResult[15] = pImp->szData[ 15 ];
432 
433     return aResult;
434 }
435 
436 SvGlobalName::SvGlobalName( const com::sun::star::uno::Sequence < sal_Int8 >& aSeq )
437 {
438     // create SvGlobalName from a platform independent representation
439     GUID aResult;
440     memset( &aResult, 0, sizeof( aResult ) );
441     if ( aSeq.getLength() == 16 )
442     {
443         aResult.Data1 = ( ( ( ( ( ( sal_uInt8 )aSeq[0] << 8 ) + ( sal_uInt8 )aSeq[1] ) << 8 ) + ( sal_uInt8 )aSeq[2] ) << 8 ) + ( sal_uInt8 )aSeq[3];
444         aResult.Data2 = ( ( sal_uInt8 )aSeq[4] << 8 ) + ( sal_uInt8 )aSeq[5];
445         aResult.Data3 = ( ( sal_uInt8 )aSeq[6] << 8 ) + ( sal_uInt8 )aSeq[7];
446         for( int nInd = 0; nInd < 8; nInd++ )
447             aResult.Data4[nInd] = ( sal_uInt8 )aSeq[nInd+8];
448     }
449 
450     pImp = new ImpSvGlobalName();
451     pImp->nRefCount++;
452     memcpy( pImp->szData, &aResult, sizeof( pImp->szData ) );
453 }
454