bump product version to 5.0.4.1
[LibreOffice.git] / rsc / source / res / rscstr.cxx
blob2e7ffee915c8570346ef5ad1b63e832c754a7658
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 .
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include <rscdb.hxx>
26 #include <rscstr.hxx>
28 #include <rtl/textcvt.h>
29 #include <rtl/textenc.h>
31 RscString::RscString( Atom nId, sal_uInt32 nTypeId )
32 : RscTop( nId, nTypeId )
34 nSize = ALIGNED_SIZE( sizeof( RscStringInst ) );
35 pRefClass = NULL;
38 RSCCLASS_TYPE RscString::GetClassType() const
40 return RSCCLASS_STRING;
43 ERRTYPE RscString::SetString( const RSCINST & rInst, const char * pStr )
45 char * pTmp;
46 ERRTYPE aError;
48 if( aError.IsOk() )
50 reinterpret_cast<RscStringInst *>(rInst.pData)->bDflt = false;
52 pTmp = reinterpret_cast<RscStringInst *>(rInst.pData)->pStr;
53 if( pTmp )
55 rtl_freeMemory( pTmp );
56 pTmp = NULL;
59 if( pStr )
61 sal_uInt32 nLen = strlen( pStr ) +1;
62 pTmp = static_cast<char *>(rtl_allocateMemory( nLen ));
63 memcpy( pTmp, pStr, nLen );
66 reinterpret_cast<RscStringInst *>(rInst.pData)->pStr = pTmp;
69 return aError;
72 ERRTYPE RscString::GetString( const RSCINST & rInst, char ** ppStr )
74 *ppStr = reinterpret_cast<RscStringInst *>(rInst.pData)->pStr;
75 return ERR_OK;
78 ERRTYPE RscString::GetRef( const RSCINST & rInst, RscId * pRscId )
80 *pRscId = reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId;
81 return ERR_OK;
84 ERRTYPE RscString::SetRef( const RSCINST & rInst, const RscId & rRefId )
86 if( pRefClass )
88 reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId = rRefId;
89 reinterpret_cast<RscStringInst *>(rInst.pData)->bDflt = false;
91 else
92 return ERR_REFNOTALLOWED;
94 return ERR_OK;
97 RSCINST RscString::Create( RSCINST * pInst, const RSCINST & rDflt,
98 bool bOwnClass )
100 RSCINST aInst;
102 if( !pInst )
104 aInst.pClass = this;
105 aInst.pData = static_cast<CLASS_DATA>(
106 rtl_allocateMemory( sizeof( RscStringInst ) ));
108 else
109 aInst = *pInst;
111 if( !bOwnClass && rDflt.IsInst() )
112 bOwnClass = rDflt.pClass->InHierarchy( this );
114 reinterpret_cast<RscStringInst *>(aInst.pData)->aRefId.Create();
115 reinterpret_cast<RscStringInst *>(aInst.pData)->pStr = NULL;
116 reinterpret_cast<RscStringInst *>(aInst.pData)->bDflt = true;
118 if( bOwnClass )
120 reinterpret_cast<RscStringInst *>(aInst.pData)->aRefId =
121 reinterpret_cast<RscStringInst *>(rDflt.pData)->aRefId;
122 SetString( aInst, reinterpret_cast<RscStringInst *>(rDflt.pData)->pStr );
123 reinterpret_cast<RscStringInst *>(aInst.pData)->bDflt =
124 reinterpret_cast<RscStringInst *>(rDflt.pData)->bDflt;
127 return aInst;
130 void RscString::Destroy( const RSCINST & rInst )
132 if( reinterpret_cast<RscStringInst *>(rInst.pData)->pStr )
133 rtl_freeMemory( reinterpret_cast<RscStringInst *>(rInst.pData)->pStr );
134 reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId.Destroy();
137 bool RscString::IsValueDefault( const RSCINST & rInst, CLASS_DATA pDef )
139 RscStringInst * pData = reinterpret_cast<RscStringInst*>(rInst.pData);
140 RscStringInst * pDefData = reinterpret_cast<RscStringInst*>(pDef);
142 if( pDef )
144 if( pData->aRefId.IsId() || pDefData->aRefId.IsId() )
146 if( pData->aRefId.aExp.IsNumber() &&
147 pDefData->aRefId.aExp.IsNumber() )
149 // Sind die Referenzidentifier gleich
150 if( pData->aRefId.GetNumber() == pDefData->aRefId.GetNumber() )
152 return true;
156 else
158 bool bStrEmpty = false;
159 bool bDefStrEmpty = false;
161 if( pData->pStr )
163 bStrEmpty = ('\0' == *pData->pStr);
165 else
166 bStrEmpty = true;
168 if( pDefData->pStr )
170 bDefStrEmpty = ('\0' == *pDefData->pStr);
172 else
173 bDefStrEmpty = true;
175 if( !bStrEmpty || !bDefStrEmpty )
177 return false;
179 return true;
183 return false;
186 void RscString::WriteSrc( const RSCINST & rInst, FILE * fOutput,
187 RscTypCont *, sal_uInt32, const char * )
189 if ( reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId.IsId() )
191 fprintf( fOutput, "%s",
192 reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId.GetName().getStr() );
194 else
196 RscStringInst * pStrI = reinterpret_cast<RscStringInst *>(rInst.pData);
197 if( pStrI->pStr ){
198 //char * pChangeTab = RscChar::GetChangeTab();
199 sal_uInt32 n = 0;
200 sal_uInt32 nPos, nSlashPos;
204 fputc( '\"', fOutput );
205 nSlashPos = nPos = 0;
207 while( pStrI->pStr[ n ] && (nPos < 72 || nPos - nSlashPos <= 3) )
208 { // nach \ mindesten 3 Zeichen wegeb \xa7
209 fputc( pStrI->pStr[ n ], fOutput );
210 if( pStrI->pStr[ n ] == '\\' )
211 nSlashPos = nPos;
212 n++;
213 nPos++;
216 fputc( '\"', fOutput );
217 if( pStrI->pStr[ n ] ) //nocht nicht zu ende
219 fputc( '\n', fOutput );
222 while( pStrI->pStr[ n ] );
224 else
225 fprintf( fOutput, "\"\"" );
229 ERRTYPE RscString::WriteRc( const RSCINST & rInst, RscWriteRc & rMem,
230 RscTypCont * pTC, sal_uInt32 nDeep, bool bExtra )
232 ERRTYPE aError;
233 ObjNode * pObjNode = NULL;
236 if( reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId.IsId() )
238 RscId aId( reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId );
239 RSCINST aTmpI;
241 aTmpI.pClass = pRefClass;
243 while( aError.IsOk() && aId.IsId() )
245 //Erhoehen und abfragen um Endlosrekusion zu vermeiden
246 nDeep++;
247 if( nDeep > nRefDeep )
248 aError = ERR_REFTODEEP;
249 else
251 pObjNode = pRefClass->GetObjNode( aId );
252 if( pObjNode )
254 aTmpI.pData = pObjNode->GetRscObj();
255 aError = pRefClass->GetRef( aTmpI, &aId );
257 else
259 if( pTC )
261 OStringBuffer aMsg(pHS->getString(
262 pRefClass->GetId()));
263 aMsg.append(' ').append(aId.GetName());
264 aError = WRN_STR_REFNOTFOUND;
265 pTC->pEH->Error( aError, rInst.pClass,
266 RscId(), aMsg.getStr() );
268 break;
274 if( aError.IsOk() )
276 if( pObjNode )
278 RSCINST aRefI;
280 aRefI = RSCINST( pRefClass, pObjNode->GetRscObj() );
281 aError = aRefI.pClass->WriteRc( aRefI, rMem, pTC, nDeep, bExtra );
283 else
285 if( reinterpret_cast<RscStringInst *>(rInst.pData)->pStr && pTC )
287 char * pStr = RscChar::MakeUTF8( reinterpret_cast<RscStringInst *>(rInst.pData)->pStr,
288 pTC->GetSourceCharSet() );
289 rMem.PutUTF8( pStr );
290 rtl_freeMemory( pStr );
292 else
293 rMem.PutUTF8( reinterpret_cast<RscStringInst *>(rInst.pData)->pStr );
296 return aError;
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */