update credits
[LibreOffice.git] / rsc / source / res / rscstr.cxx
blob79cf3605b502ea061656a80ab621d1bb6461ad94
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 ){
44 char * pTmp;
45 ERRTYPE aError;
47 if( aError.IsOk() ){
48 ((RscStringInst *)rInst.pData)->bDflt = sal_False;
50 pTmp = ((RscStringInst *)rInst.pData)->pStr;
51 if( pTmp ){
52 rtl_freeMemory( pTmp );
53 pTmp = NULL;
56 if( pStr ){
57 sal_uInt32 nLen = strlen( pStr ) +1;
58 pTmp = (char *)rtl_allocateMemory( nLen );
59 memcpy( pTmp, pStr, nLen );
62 ((RscStringInst *)rInst.pData)->pStr = pTmp;
65 return( aError );
68 ERRTYPE RscString::GetString( const RSCINST & rInst, char ** ppStr ){
69 *ppStr = ((RscStringInst *)rInst.pData)->pStr;
70 return( ERR_OK );
73 ERRTYPE RscString::GetRef( const RSCINST & rInst, RscId * pRscId ){
74 *pRscId = ((RscStringInst *)rInst.pData)->aRefId;
75 return( ERR_OK );
78 ERRTYPE RscString::SetRef( const RSCINST & rInst, const RscId & rRefId ){
79 if( pRefClass ){
80 ((RscStringInst *)rInst.pData)->aRefId = rRefId;
81 ((RscStringInst *)rInst.pData)->bDflt = sal_False;
83 else
84 return( ERR_REFNOTALLOWED );
86 return ERR_OK;
89 RSCINST RscString::Create( RSCINST * pInst, const RSCINST & rDflt,
90 sal_Bool bOwnClass )
92 RSCINST aInst;
94 if( !pInst ){
95 aInst.pClass = this;
96 aInst.pData = (CLASS_DATA)
97 rtl_allocateMemory( sizeof( RscStringInst ) );
99 else
100 aInst = *pInst;
101 if( !bOwnClass && rDflt.IsInst() )
102 bOwnClass = rDflt.pClass->InHierarchy( this );
104 ((RscStringInst *)aInst.pData)->aRefId.Create();
105 ((RscStringInst *)aInst.pData)->pStr = NULL;
106 ((RscStringInst *)aInst.pData)->bDflt = sal_True;
108 if( bOwnClass ){
109 ((RscStringInst *)aInst.pData)->aRefId =
110 ((RscStringInst *)rDflt.pData)->aRefId;
111 SetString( aInst, ((RscStringInst *)rDflt.pData)->pStr );
112 ((RscStringInst *)aInst.pData)->bDflt =
113 ((RscStringInst *)rDflt.pData)->bDflt ;
116 return( aInst );
119 void RscString::Destroy( const RSCINST & rInst ){
120 if( ((RscStringInst *)rInst.pData)->pStr )
121 rtl_freeMemory( ((RscStringInst *)rInst.pData)->pStr );
122 ((RscStringInst *)rInst.pData)->aRefId.Destroy();
125 sal_Bool RscString::IsValueDefault( const RSCINST & rInst, CLASS_DATA pDef ){
126 RscStringInst * pData = (RscStringInst*)rInst.pData;
127 RscStringInst * pDefData = (RscStringInst*)pDef;
129 if( pDef ){
130 if( pData->aRefId.IsId() || pDefData->aRefId.IsId() )
132 if( pData->aRefId.aExp.IsNumber()
133 && pDefData->aRefId.aExp.IsNumber() )
135 // Sind die Referenzidentifier gleich
136 if( pData->aRefId.GetNumber() == pDefData->aRefId.GetNumber() )
138 return sal_True;
142 else {
143 sal_Bool bStrEmpty = sal_False;
144 sal_Bool bDefStrEmpty = sal_False;
146 if( pData->pStr ){
147 bStrEmpty = ('\0' == *pData->pStr);
149 else
150 bStrEmpty = sal_True;
152 if( pDefData->pStr ){
153 bDefStrEmpty = ('\0' == *pDefData->pStr);
155 else
156 bDefStrEmpty = sal_True;
158 if( !bStrEmpty || !bDefStrEmpty ){
159 return sal_False;
161 return sal_True;
165 return sal_False;
168 void RscString::WriteSrc( const RSCINST & rInst, FILE * fOutput,
169 RscTypCont *, sal_uInt32, const char * )
171 if ( ((RscStringInst *)rInst.pData)->aRefId.IsId() )
173 fprintf( fOutput, "%s",
174 ((RscStringInst *)rInst.pData)->aRefId.GetName().getStr() );
176 else
178 RscStringInst * pStrI = ((RscStringInst *)rInst.pData);
179 if( pStrI->pStr ){
180 //char * pChangeTab = RscChar::GetChangeTab();
181 sal_uInt32 n = 0;
182 sal_uInt32 nPos, nSlashPos;
184 do {
185 fputc( '\"', fOutput );
186 nSlashPos = nPos = 0;
187 while( pStrI->pStr[ n ]
188 && (nPos < 72 || nPos - nSlashPos <= 3) )
189 { // nach \ mindesten 3 Zeichen wegeb \xa7
190 fputc( pStrI->pStr[ n ], fOutput );
191 if( pStrI->pStr[ n ] == '\\' )
192 nSlashPos = nPos;
193 n++;
194 nPos++;
197 fputc( '\"', fOutput );
198 if( pStrI->pStr[ n ] ) //nocht nicht zu ende
199 fputc( '\n', fOutput );
200 } while( pStrI->pStr[ n ] );
202 else
203 fprintf( fOutput, "\"\"" );
207 ERRTYPE RscString::WriteRc( const RSCINST & rInst, RscWriteRc & rMem,
208 RscTypCont * pTC, sal_uInt32 nDeep, sal_Bool bExtra )
210 ERRTYPE aError;
211 ObjNode * pObjNode = NULL;
214 if( ((RscStringInst *)rInst.pData)->aRefId.IsId() ){
215 RscId aId( ((RscStringInst *)rInst.pData)->aRefId );
216 RSCINST aTmpI;
218 aTmpI.pClass = pRefClass;
220 while( aError.IsOk() && aId.IsId() ){
221 //Erhoehen und abfragen um Endlosrekusion zu vermeiden
222 nDeep++;
223 if( nDeep > nRefDeep )
224 aError = ERR_REFTODEEP;
225 else
227 pObjNode = pRefClass->GetObjNode( aId );
228 if( pObjNode )
230 aTmpI.pData = pObjNode->GetRscObj();
231 aError = pRefClass->GetRef( aTmpI, &aId );
233 else
235 if( pTC )
237 OStringBuffer aMsg(pHS->getString(
238 pRefClass->GetId()));
239 aMsg.append(' ').append(aId.GetName());
240 aError = WRN_STR_REFNOTFOUND;
241 pTC->pEH->Error( aError, rInst.pClass,
242 RscId(), aMsg.getStr() );
244 break;
250 if( aError.IsOk() )
252 if( pObjNode )
254 RSCINST aRefI;
256 aRefI = RSCINST( pRefClass, pObjNode->GetRscObj() );
257 aError = aRefI.pClass->WriteRc( aRefI, rMem, pTC, nDeep, bExtra );
259 else
261 if( ((RscStringInst *)rInst.pData)->pStr && pTC )
263 char * pStr = RscChar::MakeUTF8( ((RscStringInst *)rInst.pData)->pStr,
264 pTC->GetSourceCharSet() );
265 rMem.PutUTF8( pStr );
266 rtl_freeMemory( pStr );
268 else
269 rMem.PutUTF8( ((RscStringInst *)rInst.pData)->pStr );
272 return( aError );
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */