Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / tools / source / ref / globname.cxx
blobeed9b8fd5a04264e945bc4d55f22fdc75264e5a7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <stdio.h>
21 #include <string.h>
23 #include <rtl/strbuf.hxx>
24 #include <rtl/character.hxx>
26 #include <tools/stream.hxx>
27 #include <tools/globname.hxx>
29 // ImpSvGlobalName ------------------------------------------------------------
30 ImpSvGlobalName::ImpSvGlobalName( const ImpSvGlobalName & rObj )
31 : szData(rObj.szData)
35 ImpSvGlobalName::ImpSvGlobalName(sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
36 sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
37 sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15)
39 szData.Data1 = n1;
40 szData.Data2 = n2;
41 szData.Data3 = n3;
42 szData.Data4[0] = b8;
43 szData.Data4[1] = b9;
44 szData.Data4[2] = b10;
45 szData.Data4[3] = b11;
46 szData.Data4[4] = b12;
47 szData.Data4[5] = b13;
48 szData.Data4[6] = b14;
49 szData.Data4[7] = b15;
52 bool ImpSvGlobalName::operator == ( const ImpSvGlobalName & rObj ) const
54 return !memcmp( &szData, &rObj.szData, sizeof( szData ) );
57 // SvGlobalName ----------------------------------------------------------------
58 SvGlobalName::SvGlobalName() :
59 pImp()
63 SvGlobalName::SvGlobalName( const SvGUID & rId ) :
64 pImp( ImpSvGlobalName( rId ) )
68 SvGlobalName::SvGlobalName( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
69 sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
70 sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 ) :
71 pImp( ImpSvGlobalName(n1, n2, n3, b8, b9, b10, b11, b12, b13, b14, b15) )
75 SvGlobalName::SvGlobalName( const css::uno::Sequence < sal_Int8 >& aSeq )
77 // create SvGlobalName from a platform independent representation
78 SvGUID aResult = {};
79 if ( aSeq.getLength() == 16 )
81 aResult.Data1 = ( ( ( ( ( static_cast<sal_uInt8>(aSeq[0]) << 8 ) + static_cast<sal_uInt8>(aSeq[1]) ) << 8 ) + static_cast<sal_uInt8>(aSeq[2]) ) << 8 ) + static_cast<sal_uInt8>(aSeq[3]);
82 aResult.Data2 = ( static_cast<sal_uInt8>(aSeq[4]) << 8 ) + static_cast<sal_uInt8>(aSeq[5]);
83 aResult.Data3 = ( static_cast<sal_uInt8>(aSeq[6]) << 8 ) + static_cast<sal_uInt8>(aSeq[7]);
84 for( int nInd = 0; nInd < 8; nInd++ )
85 aResult.Data4[nInd] = static_cast<sal_uInt8>(aSeq[nInd+8]);
88 pImp = ::o3tl::cow_wrapper< ImpSvGlobalName >(aResult);
91 SvGlobalName::~SvGlobalName()
95 SvGlobalName & SvGlobalName::operator = ( const SvGlobalName & rObj )
97 pImp = rObj.pImp;
99 return *this;
102 SvGlobalName & SvGlobalName::operator = ( SvGlobalName && rObj ) noexcept
104 pImp = std::move(rObj.pImp);
105 return *this;
108 SvStream& WriteSvGlobalName( SvStream& rOStr, const SvGlobalName & rObj )
110 rOStr.WriteUInt32( rObj.pImp->szData.Data1 );
111 rOStr.WriteUInt16( rObj.pImp->szData.Data2 );
112 rOStr.WriteUInt16( rObj.pImp->szData.Data3 );
113 rOStr.WriteBytes( &rObj.pImp->szData.Data4, 8 );
114 return rOStr;
117 SvStream& operator >> ( SvStream& rStr, SvGlobalName & rObj )
119 // the non-const dereferencing operator
120 // ensures pImp is unique
121 rStr.ReadUInt32( rObj.pImp->szData.Data1 );
122 rStr.ReadUInt16( rObj.pImp->szData.Data2 );
123 rStr.ReadUInt16( rObj.pImp->szData.Data3 );
124 rStr.ReadBytes( &rObj.pImp->szData.Data4, 8 );
125 return rStr;
129 bool SvGlobalName::operator < ( const SvGlobalName & rObj ) const
131 if( pImp->szData.Data3 < rObj.pImp->szData.Data3 )
132 return true;
133 else if( pImp->szData.Data3 > rObj.pImp->szData.Data3 )
134 return false;
136 if( pImp->szData.Data2 < rObj.pImp->szData.Data2 )
137 return true;
138 else if( pImp->szData.Data2 > rObj.pImp->szData.Data2 )
139 return false;
141 return pImp->szData.Data1 < rObj.pImp->szData.Data1;
144 bool SvGlobalName::operator == ( const SvGlobalName & rObj ) const
146 return pImp == rObj.pImp;
149 void SvGlobalName::MakeFromMemory( void const * pData )
151 memcpy( &pImp->szData, pData, sizeof( pImp->szData ) );
154 bool SvGlobalName::MakeId( const OUString & rIdStr )
156 const sal_Unicode *pStr = rIdStr.getStr();
157 if( rIdStr.getLength() == 36
158 && '-' == pStr[ 8 ] && '-' == pStr[ 13 ]
159 && '-' == pStr[ 18 ] && '-' == pStr[ 23 ] )
161 sal_uInt32 nFirst = 0;
162 int i = 0;
163 for( i = 0; i < 8; i++ )
165 if( rtl::isAsciiHexDigit( *pStr ) )
166 if( rtl::isAsciiDigit( *pStr ) )
167 nFirst = nFirst * 16 + (*pStr - '0');
168 else
169 nFirst = nFirst * 16 + (rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 );
170 else
171 return false;
172 pStr++;
175 sal_uInt16 nSec = 0;
176 pStr++;
177 for( i = 0; i < 4; i++ )
179 if( rtl::isAsciiHexDigit( *pStr ) )
180 if( rtl::isAsciiDigit( *pStr ) )
181 nSec = nSec * 16 + (*pStr - '0');
182 else
183 nSec = nSec * 16 + static_cast<sal_uInt16>(rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 );
184 else
185 return false;
186 pStr++;
189 sal_uInt16 nThird = 0;
190 pStr++;
191 for( i = 0; i < 4; i++ )
193 if( rtl::isAsciiHexDigit( *pStr ) )
194 if( rtl::isAsciiDigit( *pStr ) )
195 nThird = nThird * 16 + (*pStr - '0');
196 else
197 nThird = nThird * 16 + static_cast<sal_uInt16>(rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 );
198 else
199 return false;
200 pStr++;
203 sal_Int8 szRemain[ 8 ] = {};
204 pStr++;
205 for( i = 0; i < 16; i++ )
207 if( rtl::isAsciiHexDigit( *pStr ) )
208 if( rtl::isAsciiDigit( *pStr ) )
209 szRemain[i/2] = szRemain[i/2] * 16 + (*pStr - '0');
210 else
211 szRemain[i/2] = szRemain[i/2] * 16 + static_cast<sal_Int8>(rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 );
212 else
213 return false;
214 pStr++;
215 if( i == 3 )
216 pStr++;
219 memcpy(&pImp->szData.Data1, &nFirst, sizeof(nFirst));
220 memcpy(&pImp->szData.Data2, &nSec, sizeof(nSec));
221 memcpy(&pImp->szData.Data3, &nThird, sizeof(nThird));
222 memcpy(&pImp->szData.Data4, szRemain, 8);
223 return true;
225 return false;
228 OUString SvGlobalName::GetHexName() const
230 OStringBuffer aHexBuffer(36);
232 sal_Char buf[ 10 ];
233 sprintf( buf, "%8.8" SAL_PRIXUINT32, pImp->szData.Data1 );
234 aHexBuffer.append(buf);
235 aHexBuffer.append('-');
236 sprintf( buf, "%4.4X", pImp->szData.Data2 );
237 aHexBuffer.append(buf);
238 aHexBuffer.append('-');
239 sprintf( buf, "%4.4X", pImp->szData.Data3 );
240 aHexBuffer.append(buf);
241 aHexBuffer.append('-');
242 for( int i = 0; i < 2; i++ )
244 sprintf( buf, "%2.2x", pImp->szData.Data4[ i ] );
245 aHexBuffer.append(buf);
247 aHexBuffer.append('-');
248 for( int i = 2; i < 8; i++ )
250 sprintf( buf, "%2.2x", pImp->szData.Data4[ i ] );
251 aHexBuffer.append(buf);
253 return OStringToOUString(aHexBuffer.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US);
256 css::uno::Sequence < sal_Int8 > SvGlobalName::GetByteSequence() const
258 // platform independent representation of a "GlobalName"
259 // maybe transported remotely
260 css::uno::Sequence< sal_Int8 > aResult( 16 );
262 aResult[ 0] = static_cast<sal_Int8>(pImp->szData.Data1 >> 24);
263 aResult[ 1] = static_cast<sal_Int8>((pImp->szData.Data1 << 8 ) >> 24);
264 aResult[ 2] = static_cast<sal_Int8>((pImp->szData.Data1 << 16 ) >> 24);
265 aResult[ 3] = static_cast<sal_Int8>((pImp->szData.Data1 << 24 ) >> 24);
266 aResult[ 4] = static_cast<sal_Int8>(pImp->szData.Data2 >> 8);
267 aResult[ 5] = static_cast<sal_Int8>((pImp->szData.Data2 << 8 ) >> 8);
268 aResult[ 6] = static_cast<sal_Int8>(pImp->szData.Data3 >> 8);
269 aResult[ 7] = static_cast<sal_Int8>((pImp->szData.Data3 << 8 ) >> 8);
270 aResult[ 8] = pImp->szData.Data4[ 0 ];
271 aResult[ 9] = pImp->szData.Data4[ 1 ];
272 aResult[10] = pImp->szData.Data4[ 2 ];
273 aResult[11] = pImp->szData.Data4[ 3 ];
274 aResult[12] = pImp->szData.Data4[ 4 ];
275 aResult[13] = pImp->szData.Data4[ 5 ];
276 aResult[14] = pImp->szData.Data4[ 6 ];
277 aResult[15] = pImp->szData.Data4[ 7 ];
279 return aResult;
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */