Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <stdlib.h>
22 : #include <stdio.h>
23 : #include <string.h>
24 :
25 : #include <rscdb.hxx>
26 : #include <rscstr.hxx>
27 :
28 : #include <rtl/textcvt.h>
29 : #include <rtl/textenc.h>
30 :
31 902 : RscString::RscString( Atom nId, sal_uInt32 nTypeId )
32 902 : : RscTop( nId, nTypeId )
33 : {
34 902 : nSize = ALIGNED_SIZE( sizeof( RscStringInst ) );
35 902 : pRefClass = NULL;
36 902 : }
37 :
38 0 : RSCCLASS_TYPE RscString::GetClassType() const
39 : {
40 0 : return RSCCLASS_STRING;
41 : }
42 :
43 103256 : ERRTYPE RscString::SetString( const RSCINST & rInst, const char * pStr )
44 : {
45 : char * pTmp;
46 103256 : ERRTYPE aError;
47 :
48 103256 : if( aError.IsOk() )
49 : {
50 103256 : ((RscStringInst *)rInst.pData)->bDflt = false;
51 :
52 103256 : pTmp = ((RscStringInst *)rInst.pData)->pStr;
53 103256 : if( pTmp )
54 : {
55 16730 : rtl_freeMemory( pTmp );
56 16730 : pTmp = NULL;
57 : }
58 :
59 103256 : if( pStr )
60 : {
61 86496 : sal_uInt32 nLen = strlen( pStr ) +1;
62 86496 : pTmp = (char *)rtl_allocateMemory( nLen );
63 86496 : memcpy( pTmp, pStr, nLen );
64 : };
65 :
66 103256 : ((RscStringInst *)rInst.pData)->pStr = pTmp;
67 : }
68 :
69 103256 : return aError;
70 : }
71 :
72 814 : ERRTYPE RscString::GetString( const RSCINST & rInst, char ** ppStr )
73 : {
74 814 : *ppStr = ((RscStringInst *)rInst.pData)->pStr;
75 814 : return ERR_OK;
76 : }
77 :
78 4408 : ERRTYPE RscString::GetRef( const RSCINST & rInst, RscId * pRscId )
79 : {
80 4408 : *pRscId = ((RscStringInst *)rInst.pData)->aRefId;
81 4408 : return ERR_OK;
82 : }
83 :
84 0 : ERRTYPE RscString::SetRef( const RSCINST & rInst, const RscId & rRefId )
85 : {
86 0 : if( pRefClass )
87 : {
88 0 : ((RscStringInst *)rInst.pData)->aRefId = rRefId;
89 0 : ((RscStringInst *)rInst.pData)->bDflt = false;
90 : }
91 : else
92 0 : return( ERR_REFNOTALLOWED );
93 :
94 0 : return ERR_OK;
95 : }
96 :
97 91520 : RSCINST RscString::Create( RSCINST * pInst, const RSCINST & rDflt,
98 : bool bOwnClass )
99 : {
100 91520 : RSCINST aInst;
101 :
102 91520 : if( !pInst )
103 : {
104 51416 : aInst.pClass = this;
105 : aInst.pData = (CLASS_DATA)
106 51416 : rtl_allocateMemory( sizeof( RscStringInst ) );
107 : }
108 : else
109 40104 : aInst = *pInst;
110 :
111 91520 : if( !bOwnClass && rDflt.IsInst() )
112 33486 : bOwnClass = rDflt.pClass->InHierarchy( this );
113 :
114 91520 : ((RscStringInst *)aInst.pData)->aRefId.Create();
115 91520 : ((RscStringInst *)aInst.pData)->pStr = NULL;
116 91520 : ((RscStringInst *)aInst.pData)->bDflt = true;
117 :
118 91520 : if( bOwnClass )
119 : {
120 33486 : ((RscStringInst *)aInst.pData)->aRefId =
121 33486 : ((RscStringInst *)rDflt.pData)->aRefId;
122 33486 : SetString( aInst, ((RscStringInst *)rDflt.pData)->pStr );
123 : ((RscStringInst *)aInst.pData)->bDflt =
124 33486 : ((RscStringInst *)rDflt.pData)->bDflt ;
125 : }
126 :
127 91520 : return aInst;
128 : }
129 :
130 90971 : void RscString::Destroy( const RSCINST & rInst )
131 : {
132 90971 : if( ((RscStringInst *)rInst.pData)->pStr )
133 69766 : rtl_freeMemory( ((RscStringInst *)rInst.pData)->pStr );
134 90971 : ((RscStringInst *)rInst.pData)->aRefId.Destroy();
135 90971 : }
136 :
137 59860 : bool RscString::IsValueDefault( const RSCINST & rInst, CLASS_DATA pDef )
138 : {
139 59860 : RscStringInst * pData = (RscStringInst*)rInst.pData;
140 59860 : RscStringInst * pDefData = (RscStringInst*)pDef;
141 :
142 59860 : if( pDef )
143 : {
144 59860 : if( pData->aRefId.IsId() || pDefData->aRefId.IsId() )
145 : {
146 0 : if( pData->aRefId.aExp.IsNumber() &&
147 0 : pDefData->aRefId.aExp.IsNumber() )
148 : {
149 : // Sind die Referenzidentifier gleich
150 0 : if( pData->aRefId.GetNumber() == pDefData->aRefId.GetNumber() )
151 : {
152 0 : return true;
153 : }
154 : }
155 : }
156 : else
157 : {
158 59860 : bool bStrEmpty = false;
159 59860 : bool bDefStrEmpty = false;
160 :
161 59860 : if( pData->pStr )
162 : {
163 43155 : bStrEmpty = ('\0' == *pData->pStr);
164 : }
165 : else
166 16705 : bStrEmpty = true;
167 :
168 59860 : if( pDefData->pStr )
169 : {
170 1 : bDefStrEmpty = ('\0' == *pDefData->pStr);
171 : }
172 : else
173 59859 : bDefStrEmpty = true;
174 :
175 59860 : if( !bStrEmpty || !bDefStrEmpty )
176 : {
177 43093 : return false;
178 : }
179 16767 : return true;
180 : }
181 : }
182 :
183 0 : return false;
184 : }
185 :
186 42846 : void RscString::WriteSrc( const RSCINST & rInst, FILE * fOutput,
187 : RscTypCont *, sal_uInt32, const char * )
188 : {
189 42846 : if ( ((RscStringInst *)rInst.pData)->aRefId.IsId() )
190 : {
191 : fprintf( fOutput, "%s",
192 0 : ((RscStringInst *)rInst.pData)->aRefId.GetName().getStr() );
193 : }
194 : else
195 : {
196 42846 : RscStringInst * pStrI = ((RscStringInst *)rInst.pData);
197 42846 : if( pStrI->pStr ){
198 : //char * pChangeTab = RscChar::GetChangeTab();
199 26175 : sal_uInt32 n = 0;
200 : sal_uInt32 nPos, nSlashPos;
201 :
202 27275 : do
203 : {
204 27275 : fputc( '\"', fOutput );
205 27275 : nSlashPos = nPos = 0;
206 :
207 594753 : while( pStrI->pStr[ n ] && (nPos < 72 || nPos - nSlashPos <= 3) )
208 : { // nach \ mindesten 3 Zeichen wegeb \xa7
209 540203 : fputc( pStrI->pStr[ n ], fOutput );
210 540203 : if( pStrI->pStr[ n ] == '\\' )
211 759 : nSlashPos = nPos;
212 540203 : n++;
213 540203 : nPos++;
214 : }
215 :
216 27275 : fputc( '\"', fOutput );
217 27275 : if( pStrI->pStr[ n ] ) //nocht nicht zu ende
218 : {
219 1100 : fputc( '\n', fOutput );
220 : }
221 : }
222 27275 : while( pStrI->pStr[ n ] );
223 : }
224 : else
225 16671 : fprintf( fOutput, "\"\"" );
226 : }
227 42846 : }
228 :
229 28293 : ERRTYPE RscString::WriteRc( const RSCINST & rInst, RscWriteRc & rMem,
230 : RscTypCont * pTC, sal_uInt32 nDeep, bool bExtra )
231 : {
232 28293 : ERRTYPE aError;
233 28293 : ObjNode * pObjNode = NULL;
234 :
235 :
236 28293 : if( ((RscStringInst *)rInst.pData)->aRefId.IsId() )
237 : {
238 0 : RscId aId( ((RscStringInst *)rInst.pData)->aRefId );
239 0 : RSCINST aTmpI;
240 :
241 0 : aTmpI.pClass = pRefClass;
242 :
243 0 : while( aError.IsOk() && aId.IsId() )
244 : {
245 : //Erhoehen und abfragen um Endlosrekusion zu vermeiden
246 0 : nDeep++;
247 0 : if( nDeep > nRefDeep )
248 0 : aError = ERR_REFTODEEP;
249 : else
250 : {
251 0 : pObjNode = pRefClass->GetObjNode( aId );
252 0 : if( pObjNode )
253 : {
254 0 : aTmpI.pData = pObjNode->GetRscObj();
255 0 : aError = pRefClass->GetRef( aTmpI, &aId );
256 : }
257 : else
258 : {
259 0 : if( pTC )
260 : {
261 : OStringBuffer aMsg(pHS->getString(
262 0 : pRefClass->GetId()));
263 0 : aMsg.append(' ').append(aId.GetName());
264 0 : aError = WRN_STR_REFNOTFOUND;
265 : pTC->pEH->Error( aError, rInst.pClass,
266 0 : RscId(), aMsg.getStr() );
267 : }
268 0 : break;
269 : }
270 : }
271 0 : }
272 : }
273 :
274 28293 : if( aError.IsOk() )
275 : {
276 28293 : if( pObjNode )
277 : {
278 0 : RSCINST aRefI;
279 :
280 0 : aRefI = RSCINST( pRefClass, pObjNode->GetRscObj() );
281 0 : aError = aRefI.pClass->WriteRc( aRefI, rMem, pTC, nDeep, bExtra );
282 : }
283 : else
284 : {
285 28293 : if( ((RscStringInst *)rInst.pData)->pStr && pTC )
286 : {
287 : char * pStr = RscChar::MakeUTF8( ((RscStringInst *)rInst.pData)->pStr,
288 27861 : pTC->GetSourceCharSet() );
289 27861 : rMem.PutUTF8( pStr );
290 27861 : rtl_freeMemory( pStr );
291 : }
292 : else
293 432 : rMem.PutUTF8( ((RscStringInst *)rInst.pData)->pStr );
294 : };
295 : };
296 28293 : return aError;
297 : }
298 :
299 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|