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 1252 : RscString::RscString( Atom nId, sal_uInt32 nTypeId )
32 1252 : : RscTop( nId, nTypeId )
33 : {
34 1252 : nSize = ALIGNED_SIZE( sizeof( RscStringInst ) );
35 1252 : pRefClass = NULL;
36 1252 : }
37 :
38 0 : RSCCLASS_TYPE RscString::GetClassType() const
39 : {
40 0 : return RSCCLASS_STRING;
41 : }
42 :
43 120566 : ERRTYPE RscString::SetString( const RSCINST & rInst, const char * pStr )
44 : {
45 : char * pTmp;
46 120566 : ERRTYPE aError;
47 :
48 120566 : if( aError.IsOk() )
49 : {
50 120566 : ((RscStringInst *)rInst.pData)->bDflt = false;
51 :
52 120566 : pTmp = ((RscStringInst *)rInst.pData)->pStr;
53 120566 : if( pTmp )
54 : {
55 19168 : rtl_freeMemory( pTmp );
56 19168 : pTmp = NULL;
57 : }
58 :
59 120566 : if( pStr )
60 : {
61 101370 : sal_uInt32 nLen = strlen( pStr ) +1;
62 101370 : pTmp = (char *)rtl_allocateMemory( nLen );
63 101370 : memcpy( pTmp, pStr, nLen );
64 : };
65 :
66 120566 : ((RscStringInst *)rInst.pData)->pStr = pTmp;
67 : }
68 :
69 120566 : return aError;
70 : }
71 :
72 698 : ERRTYPE RscString::GetString( const RSCINST & rInst, char ** ppStr )
73 : {
74 698 : *ppStr = ((RscStringInst *)rInst.pData)->pStr;
75 698 : return ERR_OK;
76 : }
77 :
78 3638 : ERRTYPE RscString::GetRef( const RSCINST & rInst, RscId * pRscId )
79 : {
80 3638 : *pRscId = ((RscStringInst *)rInst.pData)->aRefId;
81 3638 : 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 106022 : RSCINST RscString::Create( RSCINST * pInst, const RSCINST & rDflt,
98 : bool bOwnClass )
99 : {
100 106022 : RSCINST aInst;
101 :
102 106022 : if( !pInst )
103 : {
104 61507 : aInst.pClass = this;
105 : aInst.pData = (CLASS_DATA)
106 61507 : rtl_allocateMemory( sizeof( RscStringInst ) );
107 : }
108 : else
109 44515 : aInst = *pInst;
110 :
111 106022 : if( !bOwnClass && rDflt.IsInst() )
112 38435 : bOwnClass = rDflt.pClass->InHierarchy( this );
113 :
114 106022 : ((RscStringInst *)aInst.pData)->aRefId.Create();
115 106022 : ((RscStringInst *)aInst.pData)->pStr = NULL;
116 106022 : ((RscStringInst *)aInst.pData)->bDflt = true;
117 :
118 106022 : if( bOwnClass )
119 : {
120 38444 : ((RscStringInst *)aInst.pData)->aRefId =
121 38444 : ((RscStringInst *)rDflt.pData)->aRefId;
122 38444 : SetString( aInst, ((RscStringInst *)rDflt.pData)->pStr );
123 : ((RscStringInst *)aInst.pData)->bDflt =
124 38444 : ((RscStringInst *)rDflt.pData)->bDflt ;
125 : }
126 :
127 106022 : return aInst;
128 : }
129 :
130 105118 : void RscString::Destroy( const RSCINST & rInst )
131 : {
132 105118 : if( ((RscStringInst *)rInst.pData)->pStr )
133 82202 : rtl_freeMemory( ((RscStringInst *)rInst.pData)->pStr );
134 105118 : ((RscStringInst *)rInst.pData)->aRefId.Destroy();
135 105118 : }
136 :
137 69891 : bool RscString::IsValueDefault( const RSCINST & rInst, CLASS_DATA pDef )
138 : {
139 69891 : RscStringInst * pData = (RscStringInst*)rInst.pData;
140 69891 : RscStringInst * pDefData = (RscStringInst*)pDef;
141 :
142 69891 : if( pDef )
143 : {
144 69891 : 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 69891 : bool bStrEmpty = false;
159 69891 : bool bDefStrEmpty = false;
160 :
161 69891 : if( pData->pStr )
162 : {
163 50749 : bStrEmpty = ('\0' == *pData->pStr);
164 : }
165 : else
166 19142 : bStrEmpty = true;
167 :
168 69891 : if( pDefData->pStr )
169 : {
170 1 : bDefStrEmpty = ('\0' == *pDefData->pStr);
171 : }
172 : else
173 69890 : bDefStrEmpty = true;
174 :
175 69891 : if( !bStrEmpty || !bDefStrEmpty )
176 : {
177 50684 : return false;
178 : }
179 19207 : return true;
180 : }
181 : }
182 :
183 0 : return false;
184 : }
185 :
186 50253 : void RscString::WriteSrc( const RSCINST & rInst, FILE * fOutput,
187 : RscTypCont *, sal_uInt32, const char * )
188 : {
189 50253 : if ( ((RscStringInst *)rInst.pData)->aRefId.IsId() )
190 : {
191 : fprintf( fOutput, "%s",
192 0 : ((RscStringInst *)rInst.pData)->aRefId.GetName().getStr() );
193 : }
194 : else
195 : {
196 50253 : RscStringInst * pStrI = ((RscStringInst *)rInst.pData);
197 50253 : if( pStrI->pStr ){
198 : //char * pChangeTab = RscChar::GetChangeTab();
199 31147 : sal_uInt32 n = 0;
200 : sal_uInt32 nPos, nSlashPos;
201 :
202 32326 : do
203 : {
204 32326 : fputc( '\"', fOutput );
205 32326 : nSlashPos = nPos = 0;
206 :
207 734192 : while( pStrI->pStr[ n ] && (nPos < 72 || nPos - nSlashPos <= 3) )
208 : { // nach \ mindesten 3 Zeichen wegeb \xa7
209 669540 : fputc( pStrI->pStr[ n ], fOutput );
210 669540 : if( pStrI->pStr[ n ] == '\\' )
211 886 : nSlashPos = nPos;
212 669540 : n++;
213 669540 : nPos++;
214 : }
215 :
216 32326 : fputc( '\"', fOutput );
217 32326 : if( pStrI->pStr[ n ] ) //nocht nicht zu ende
218 : {
219 1179 : fputc( '\n', fOutput );
220 : }
221 : }
222 32326 : while( pStrI->pStr[ n ] );
223 : }
224 : else
225 19106 : fprintf( fOutput, "\"\"" );
226 : }
227 50253 : }
228 :
229 33423 : ERRTYPE RscString::WriteRc( const RSCINST & rInst, RscWriteRc & rMem,
230 : RscTypCont * pTC, sal_uInt32 nDeep, bool bExtra )
231 : {
232 33423 : ERRTYPE aError;
233 33423 : ObjNode * pObjNode = NULL;
234 :
235 :
236 33423 : 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 33423 : if( aError.IsOk() )
275 : {
276 33423 : 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 33423 : if( ((RscStringInst *)rInst.pData)->pStr && pTC )
286 : {
287 : char * pStr = RscChar::MakeUTF8( ((RscStringInst *)rInst.pData)->pStr,
288 32915 : pTC->GetSourceCharSet() );
289 32915 : rMem.PutUTF8( pStr );
290 32915 : rtl_freeMemory( pStr );
291 : }
292 : else
293 508 : rMem.PutUTF8( ((RscStringInst *)rInst.pData)->pStr );
294 : };
295 : };
296 33423 : return aError;
297 : }
298 :
299 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|