1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
24 #include <rtl/strbuf.hxx>
26 #include <tools/stream.hxx>
27 #include <tools/globname.hxx>
29 // ImpSvGlobalName ------------------------------------------------------------
31 ImpSvGlobalName::ImpSvGlobalName( const ImpSvGlobalName
& rObj
)
34 memcpy( szData
, rObj
.szData
, sizeof( szData
) );
37 ImpSvGlobalName::ImpSvGlobalName( Empty
)
40 memset( szData
, 0, sizeof( szData
) );
43 sal_Bool
ImpSvGlobalName::operator == ( const ImpSvGlobalName
& rObj
) const
45 return !memcmp( szData
, rObj
.szData
, sizeof( szData
) );
48 // SvGlobalName ----------------------------------------------------------------
50 SvGlobalName::SvGlobalName()
52 static ImpSvGlobalName
aNoName( ImpSvGlobalName::EMPTY
);
69 SvGlobalName::SvGlobalName( const CLSID
& rId
)
71 pImp
= new ImpSvGlobalName();
73 memcpy( pImp
->szData
, &rId
, sizeof( pImp
->szData
) );
76 SvGlobalName::SvGlobalName( sal_uInt32 n1
, sal_uInt16 n2
, sal_uInt16 n3
,
77 sal_uInt8 b8
, sal_uInt8 b9
, sal_uInt8 b10
, sal_uInt8 b11
,
78 sal_uInt8 b12
, sal_uInt8 b13
, sal_uInt8 b14
, sal_uInt8 b15
)
80 pImp
= new ImpSvGlobalName();
83 memcpy(pImp
->szData
, &n1
, sizeof(n1
));
84 memcpy(pImp
->szData
+4, &n2
, sizeof(n2
));
85 memcpy(pImp
->szData
+6, &n3
, sizeof(n3
));
86 pImp
->szData
[ 8 ] = b8
;
87 pImp
->szData
[ 9 ] = b9
;
88 pImp
->szData
[ 10 ] = b10
;
89 pImp
->szData
[ 11 ] = b11
;
90 pImp
->szData
[ 12 ] = b12
;
91 pImp
->szData
[ 13 ] = b13
;
92 pImp
->szData
[ 14 ] = b14
;
93 pImp
->szData
[ 15 ] = b15
;
96 SvGlobalName::~SvGlobalName()
99 if( !pImp
->nRefCount
)
103 SvGlobalName
& SvGlobalName::operator = ( const SvGlobalName
& rObj
)
105 rObj
.pImp
->nRefCount
++;
107 if( !pImp
->nRefCount
)
113 void SvGlobalName::NewImp()
115 if( pImp
->nRefCount
> 1 )
118 pImp
= new ImpSvGlobalName( *pImp
);
123 SvStream
& operator << ( SvStream
& rOStr
, const SvGlobalName
& rObj
)
126 memcpy(&a
, rObj
.pImp
->szData
, sizeof(sal_uInt32
));
130 memcpy(&b
, rObj
.pImp
->szData
+4, sizeof(sal_uInt16
));
133 memcpy(&b
, rObj
.pImp
->szData
+6, sizeof(sal_uInt16
));
136 rOStr
.Write( (sal_Char
*)&rObj
.pImp
->szData
[ 8 ], 8 );
140 SvStream
& operator >> ( SvStream
& rStr
, SvGlobalName
& rObj
)
142 rObj
.NewImp(); // copy if necessary
146 memcpy(rObj
.pImp
->szData
, &a
, sizeof(sal_uInt32
));
150 memcpy(rObj
.pImp
->szData
+4, &b
, sizeof(sal_uInt16
));
153 memcpy(rObj
.pImp
->szData
+6, &b
, sizeof(sal_uInt16
));
155 rStr
.Read( (sal_Char
*)&rObj
.pImp
->szData
[ 8 ], 8 );
160 sal_Bool
SvGlobalName::operator < ( const SvGlobalName
& rObj
) const
162 int n
= memcmp( pImp
->szData
+6, rObj
.pImp
->szData
+6,
163 sizeof( pImp
->szData
) -6);
170 memcpy(&Data2_a
, pImp
->szData
+4, sizeof(sal_uInt16
));
173 memcpy(&Data2_b
, rObj
.pImp
->szData
+4, sizeof(sal_uInt16
));
175 if( Data2_a
< Data2_b
)
177 else if( Data2_a
== Data2_b
)
180 memcpy(&Data1_a
, pImp
->szData
, sizeof(sal_uInt32
));
183 memcpy(&Data1_b
, rObj
.pImp
->szData
, sizeof(sal_uInt32
));
185 return Data1_a
< Data1_b
;
192 SvGlobalName
& SvGlobalName::operator += ( sal_uInt32 n
)
197 memcpy(&nOld
, pImp
->szData
, sizeof(sal_uInt32
));
198 sal_uInt32 nNew
= nOld
+ n
;
199 memcpy(pImp
->szData
, &nNew
, sizeof(sal_uInt32
));
205 memcpy(&Data2
, pImp
->szData
+ 4, sizeof(sal_uInt16
));
207 memcpy(pImp
->szData
+ 4, &Data2
, sizeof(sal_uInt16
));
212 sal_Bool
SvGlobalName::operator == ( const SvGlobalName
& rObj
) const
214 return *pImp
== *rObj
.pImp
;
217 void SvGlobalName::MakeFromMemory( void * pData
)
220 memcpy( pImp
->szData
, pData
, sizeof( pImp
->szData
) );
223 sal_Bool
SvGlobalName::MakeId( const String
& rIdStr
)
225 OString
aStr(OUStringToOString(rIdStr
,
226 RTL_TEXTENCODING_ASCII_US
));
227 const sal_Char
*pStr
= aStr
.getStr();
228 if( rIdStr
.Len() == 36
229 && '-' == pStr
[ 8 ] && '-' == pStr
[ 13 ]
230 && '-' == pStr
[ 18 ] && '-' == pStr
[ 23 ] )
232 sal_uInt32 nFirst
= 0;
234 for( i
= 0; i
< 8; i
++ )
236 if( isxdigit( *pStr
) )
237 if( isdigit( *pStr
) )
238 nFirst
= nFirst
* 16 + (*pStr
- '0');
240 nFirst
= nFirst
* 16 + (toupper( *pStr
) - 'A' + 10 );
248 for( i
= 0; i
< 4; i
++ )
250 if( isxdigit( *pStr
) )
251 if( isdigit( *pStr
) )
252 nSec
= nSec
* 16 + (*pStr
- '0');
254 nSec
= nSec
* 16 + (sal_uInt16
)(toupper( *pStr
) - 'A' + 10 );
260 sal_uInt16 nThird
= 0;
262 for( i
= 0; i
< 4; i
++ )
264 if( isxdigit( *pStr
) )
265 if( isdigit( *pStr
) )
266 nThird
= nThird
* 16 + (*pStr
- '0');
268 nThird
= nThird
* 16 + (sal_uInt16
)(toupper( *pStr
) - 'A' + 10 );
274 sal_Int8 szRemain
[ 8 ];
275 memset( szRemain
, 0, sizeof( szRemain
) );
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');
283 szRemain
[i
/2] = szRemain
[i
/2] * 16 + (sal_Int8
)(toupper( *pStr
) - 'A' + 10 );
292 memcpy(&pImp
->szData
[0], &nFirst
, sizeof(nFirst
));
293 memcpy(&pImp
->szData
[4], &nSec
, sizeof(nSec
));
294 memcpy(&pImp
->szData
[6], &nThird
, sizeof(nThird
));
295 memcpy(&pImp
->szData
[ 8 ], szRemain
, 8);
301 String
SvGlobalName::GetHexName() const
303 OStringBuffer aHexBuffer
;
307 memcpy(&Data1
, pImp
->szData
, sizeof(sal_uInt32
));
308 sprintf( buf
, "%8.8" SAL_PRIXUINT32
, Data1
);
309 aHexBuffer
.append(buf
);
310 aHexBuffer
.append('-');
312 for( i
= 4; i
< 8; i
+= 2 )
315 memcpy(&Data2
, pImp
->szData
+i
, sizeof(sal_uInt16
));
316 sprintf( buf
, "%4.4X", Data2
);
317 aHexBuffer
.append(buf
);
318 aHexBuffer
.append('-');
320 for( i
= 8; i
< 10; i
++ )
322 sprintf( buf
, "%2.2x", pImp
->szData
[ i
] );
323 aHexBuffer
.append(buf
);
325 aHexBuffer
.append('-');
326 for( i
= 10; i
< 16; i
++ )
328 sprintf( buf
, "%2.2x", pImp
->szData
[ i
] );
329 aHexBuffer
.append(buf
);
331 return OStringToOUString(aHexBuffer
.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US
);
334 com::sun::star::uno::Sequence
< sal_Int8
> SvGlobalName::GetByteSequence() const
336 // platform independent representation of a "GlobalName"
337 // maybe transported remotely
338 com::sun::star::uno::Sequence
< sal_Int8
> aResult( 16 );
341 memcpy(&Data1
, pImp
->szData
, sizeof(sal_uInt32
));
342 aResult
[0] = (sal_Int8
) (Data1
>> 24);
343 aResult
[1] = (sal_Int8
) ((Data1
<< 8 ) >> 24);
344 aResult
[2] = (sal_Int8
) ((Data1
<< 16 ) >> 24);
345 aResult
[3] = (sal_Int8
) ((Data1
<< 24 ) >> 24);
347 memcpy(&Data2
, pImp
->szData
+4, sizeof(sal_uInt16
));
348 aResult
[4] = (sal_Int8
) (Data2
>> 8);
349 aResult
[5] = (sal_Int8
) ((Data2
<< 8 ) >> 8);
351 memcpy(&Data3
, pImp
->szData
+6, sizeof(sal_uInt16
));
352 aResult
[6] = (sal_Int8
) (Data3
>> 8);
353 aResult
[7] = (sal_Int8
) ((Data3
<< 8 ) >> 8);
354 aResult
[8] = pImp
->szData
[ 8 ];
355 aResult
[9] = pImp
->szData
[ 9 ];
356 aResult
[10] = pImp
->szData
[ 10 ];
357 aResult
[11] = pImp
->szData
[ 11 ];
358 aResult
[12] = pImp
->szData
[ 12 ];
359 aResult
[13] = pImp
->szData
[ 13 ];
360 aResult
[14] = pImp
->szData
[ 14 ];
361 aResult
[15] = pImp
->szData
[ 15 ];
366 SvGlobalName::SvGlobalName( const com::sun::star::uno::Sequence
< sal_Int8
>& aSeq
)
368 // create SvGlobalName from a platform independent representation
370 memset( &aResult
, 0, sizeof( aResult
) );
371 if ( aSeq
.getLength() == 16 )
373 aResult
.Data1
= ( ( ( ( ( ( sal_uInt8
)aSeq
[0] << 8 ) + ( sal_uInt8
)aSeq
[1] ) << 8 ) + ( sal_uInt8
)aSeq
[2] ) << 8 ) + ( sal_uInt8
)aSeq
[3];
374 aResult
.Data2
= ( ( sal_uInt8
)aSeq
[4] << 8 ) + ( sal_uInt8
)aSeq
[5];
375 aResult
.Data3
= ( ( sal_uInt8
)aSeq
[6] << 8 ) + ( sal_uInt8
)aSeq
[7];
376 for( int nInd
= 0; nInd
< 8; nInd
++ )
377 aResult
.Data4
[nInd
] = ( sal_uInt8
)aSeq
[nInd
+8];
380 pImp
= new ImpSvGlobalName();
382 memcpy( pImp
->szData
, &aResult
, sizeof( pImp
->szData
) );
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */