xref: /trunk/main/rsc/source/res/rscstr.cxx (revision 477794c1)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_rsc.hxx"
26 /****************** I N C L U D E S **************************************/
27 
28 // C and C++ Includes.
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 
33 // Programmabh�ngige Includes.
34 #include <rscdb.hxx>
35 #include <rscstr.hxx>
36 
37 #include <rtl/textcvt.h>
38 #include <rtl/textenc.h>
39 
40 /****************** C O D E **********************************************/
41 /****************** R s c S t r i n g ************************************/
42 /*************************************************************************
43 |*
44 |*	  RscString::RscString()
45 |*
46 |*	  Beschreibung
47 |*	  Ersterstellung	MM 25.04.91
48 |*	  Letzte Aenderung	MM 25.04.91
49 |*
50 *************************************************************************/
RscString(Atom nId,sal_uInt32 nTypeId)51 RscString::RscString( Atom nId, sal_uInt32 nTypeId )
52 				: RscTop( nId, nTypeId )
53 {
54 	nSize = ALIGNED_SIZE( sizeof( RscStringInst ) );
55 	pRefClass = NULL;
56 }
57 
58 /*************************************************************************
59 |*
60 |*	  RscString::GetClassType()
61 |*
62 |*	  Beschreibung
63 |*	  Ersterstellung	MM 25.04.91
64 |*	  Letzte Aenderung	MM 25.04.91
65 |*
66 *************************************************************************/
GetClassType() const67 RSCCLASS_TYPE RscString::GetClassType() const
68 {
69 	return RSCCLASS_STRING;
70 }
71 
72 /*************************************************************************
73 |*
74 |*	  RscString::SetNumber()
75 |*
76 |*	  Beschreibung
77 |*	  Ersterstellung	MM 25.04.91
78 |*	  Letzte Aenderung	MM 25.04.91
79 |*
80 *************************************************************************/
SetString(const RSCINST & rInst,const char * pStr)81 ERRTYPE RscString::SetString( const RSCINST & rInst, const char * pStr ){
82 	char	* pTmp;
83 	ERRTYPE aError;
84 
85 	if( aError.IsOk() ){
86 		((RscStringInst *)rInst.pData)->bDflt = sal_False;
87 
88 		pTmp = ((RscStringInst *)rInst.pData)->pStr;
89 		if( pTmp ){
90 			rtl_freeMemory( pTmp );
91 			pTmp = NULL;
92 		}
93 
94 		if( pStr ){
95 			sal_uInt32	nLen = strlen( pStr ) +1;
96 			pTmp = (char *)rtl_allocateMemory( nLen );
97 			memcpy( pTmp, pStr, nLen );
98 		};
99 
100 		((RscStringInst *)rInst.pData)->pStr = pTmp;
101 	}
102 
103 	return( aError );
104 }
105 
106 /*************************************************************************
107 |*
108 |*	  RscString::GetString()
109 |*
110 |*	  Beschreibung
111 |*	  Ersterstellung	MM 25.04.91
112 |*	  Letzte Aenderung	MM 25.04.91
113 |*
114 *************************************************************************/
GetString(const RSCINST & rInst,char ** ppStr)115 ERRTYPE RscString::GetString( const RSCINST & rInst, char ** ppStr ){
116 	*ppStr = ((RscStringInst *)rInst.pData)->pStr;
117 	return( ERR_OK );
118 }
119 
120 /*************************************************************************
121 |*
122 |*	  RscString::GetRef()
123 |*
124 |*	  Beschreibung
125 |*	  Ersterstellung	MM 22.07.91
126 |*	  Letzte Aenderung	MM 22.07.91
127 |*
128 *************************************************************************/
GetRef(const RSCINST & rInst,RscId * pRscId)129 ERRTYPE RscString::GetRef( const RSCINST & rInst, RscId * pRscId ){
130 	*pRscId = ((RscStringInst *)rInst.pData)->aRefId;
131 	return( ERR_OK );
132 }
133 
134 /*************************************************************************
135 |*
136 |*	  RscString::SetRef()
137 |*
138 |*	  Beschreibung
139 |*	  Ersterstellung	MM 15.05.91
140 |*	  Letzte Aenderung	MM 15.05.91
141 |*
142 *************************************************************************/
SetRef(const RSCINST & rInst,const RscId & rRefId)143 ERRTYPE RscString::SetRef( const RSCINST & rInst, const RscId & rRefId ){
144 	if( pRefClass ){
145 		((RscStringInst *)rInst.pData)->aRefId = rRefId;
146 		((RscStringInst *)rInst.pData)->bDflt  = sal_False;
147 	}
148 	else
149 		return( ERR_REFNOTALLOWED );
150 
151 	return ERR_OK;
152 }
153 
154 /*************************************************************************
155 |*
156 |*	  RscString::Create()
157 |*
158 |*	  Beschreibung
159 |*	  Ersterstellung	MM 25.04.91
160 |*	  Letzte Aenderung	MM 25.04.91
161 |*
162 *************************************************************************/
Create(RSCINST * pInst,const RSCINST & rDflt,sal_Bool bOwnClass)163 RSCINST RscString::Create( RSCINST * pInst, const RSCINST & rDflt,
164 						   sal_Bool bOwnClass )
165 {
166 	RSCINST aInst;
167 
168 	if( !pInst ){
169 		aInst.pClass = this;
170 		aInst.pData = (CLASS_DATA)
171 					  rtl_allocateMemory( sizeof( RscStringInst ) );
172 	}
173 	else
174 		aInst = *pInst;
175 	if( !bOwnClass && rDflt.IsInst() )
176 		bOwnClass = rDflt.pClass->InHierarchy( this );
177 
178 	((RscStringInst *)aInst.pData)->aRefId.Create();
179 	((RscStringInst *)aInst.pData)->pStr = NULL;
180 	((RscStringInst *)aInst.pData)->bDflt = sal_True;
181 
182 	if( bOwnClass ){
183 		((RscStringInst *)aInst.pData)->aRefId =
184 						  ((RscStringInst *)rDflt.pData)->aRefId;
185 		SetString( aInst, ((RscStringInst *)rDflt.pData)->pStr );
186 		((RscStringInst *)aInst.pData)->bDflt =
187 							((RscStringInst *)rDflt.pData)->bDflt ;
188 	}
189 
190 	return( aInst );
191 }
192 
193 /*************************************************************************
194 |*
195 |*	  RscString::Destroy()
196 |*
197 |*	  Beschreibung
198 |*	  Ersterstellung	MM 15.05.91
199 |*	  Letzte Aenderung	MM 15.05.91
200 |*
201 *************************************************************************/
Destroy(const RSCINST & rInst)202 void RscString::Destroy( const RSCINST & rInst ){
203 	if( ((RscStringInst *)rInst.pData)->pStr )
204 		rtl_freeMemory( ((RscStringInst *)rInst.pData)->pStr );
205 	((RscStringInst *)rInst.pData)->aRefId.Destroy();
206 }
207 
208 /*************************************************************************
209 |*
210 |*	  RscString::IsValueDefault()
211 |*
212 |*	  Beschreibung
213 |*	  Ersterstellung	MM 15.01.92
214 |*	  Letzte Aenderung	MM 15.01.92
215 |*
216 *************************************************************************/
IsValueDefault(const RSCINST & rInst,CLASS_DATA pDef)217 sal_Bool RscString::IsValueDefault( const RSCINST & rInst, CLASS_DATA pDef ){
218 	RscStringInst * pData	 = (RscStringInst*)rInst.pData;
219 	RscStringInst * pDefData = (RscStringInst*)pDef;
220 
221 	if( pDef ){
222 		if( pData->aRefId.IsId() || pDefData->aRefId.IsId() )
223 		{
224 			if( pData->aRefId.aExp.IsNumber()
225 			  && pDefData->aRefId.aExp.IsNumber() )
226 			{
227 				// Sind die Referenzidentifier gleich
228 				if( pData->aRefId.GetNumber() == pDefData->aRefId.GetNumber() )
229 				{
230 					return sal_True;
231 				}
232 			}
233 		}
234 		else {
235 			sal_Bool bStrEmpty = sal_False;
236 			sal_Bool bDefStrEmpty = sal_False;
237 
238 			if( pData->pStr ){
239 				bStrEmpty = ('\0' == *pData->pStr);
240 			}
241 			else
242 				bStrEmpty = sal_True;
243 
244 			if( pDefData->pStr ){
245 				bDefStrEmpty = ('\0' == *pDefData->pStr);
246 			}
247 			else
248 				bDefStrEmpty = sal_True;
249 
250 			if( !bStrEmpty || !bDefStrEmpty ){
251 				return sal_False;
252 			}
253 			return sal_True;
254 		}
255 	}
256 
257 	return sal_False;
258 }
259 
260 /*************************************************************************
261 |*
262 |*	  RscString::WriteSrc()
263 |*
264 |*	  Beschreibung
265 |*	  Ersterstellung	MM 25.04.91
266 |*	  Letzte Aenderung	MM 25.04.91
267 |*
268 *************************************************************************/
WriteSrc(const RSCINST & rInst,FILE * fOutput,RscTypCont *,sal_uInt32,const char *)269 void RscString::WriteSrc( const RSCINST & rInst, FILE * fOutput,
270 						  RscTypCont *, sal_uInt32, const char * )
271 {
272 	if ( ((RscStringInst *)rInst.pData)->aRefId.IsId() )
273 	{
274 		fprintf( fOutput, "%s",
275 				 ((RscStringInst *)rInst.pData)->aRefId.GetName().GetBuffer() );
276 	}
277 	else
278 	{
279 		RscStringInst * pStrI = ((RscStringInst *)rInst.pData);
280 		if(  pStrI->pStr ){
281 			//char *  pChangeTab = RscChar::GetChangeTab();
282 			sal_uInt32	n = 0;
283 			sal_uInt32	nPos, nSlashPos;
284 
285 			do {
286 				fputc( '\"', fOutput );
287 				nSlashPos = nPos = 0;
288 				while( pStrI->pStr[ n ]
289 				  && (nPos < 72 || nPos - nSlashPos <= 3) )
290 				{ // nach \ mindesten 3 Zeichen wegeb \xa7
291 					fputc( pStrI->pStr[ n ], fOutput );
292 					if( pStrI->pStr[ n ] == '\\' )
293 						nSlashPos = nPos;
294 					n++;
295 					nPos++;
296 				}
297 
298 				fputc( '\"', fOutput );
299 				if( pStrI->pStr[ n ] ) //nocht nicht zu ende
300 					fputc( '\n', fOutput );
301 			} while( pStrI->pStr[ n ] );
302 		}
303 		else
304 			fprintf( fOutput, "\"\"" );
305 	}
306 }
307 
308 /*************************************************************************
309 |*
310 |*	  RscString::WriteRc()
311 |*
312 |*	  Beschreibung
313 |*	  Ersterstellung	MM 15.04.91
314 |*	  Letzte Aenderung	MM 15.04.91
315 |*
316 *************************************************************************/
WriteRc(const RSCINST & rInst,RscWriteRc & rMem,RscTypCont * pTC,sal_uInt32 nDeep,sal_Bool bExtra)317 ERRTYPE RscString::WriteRc( const RSCINST & rInst, RscWriteRc & rMem,
318 							RscTypCont * pTC, sal_uInt32 nDeep, sal_Bool bExtra )
319 {
320 	ERRTYPE aError;
321 	ObjNode *		pObjNode = NULL;
322 
323 
324 	if( ((RscStringInst *)rInst.pData)->aRefId.IsId() ){
325 		RscId	aId( ((RscStringInst *)rInst.pData)->aRefId );
326 		RSCINST aTmpI;
327 
328 		aTmpI.pClass = pRefClass;
329 
330 		while( aError.IsOk() && aId.IsId() ){
331 			//Erhoehen und abfragen um Endlosrekusion zu vermeiden
332 			nDeep++;
333 			if( nDeep > nRefDeep )
334 				aError = ERR_REFTODEEP;
335 			else
336 			{
337 				pObjNode = pRefClass->GetObjNode( aId );
338 				if( pObjNode )
339 				{
340 					aTmpI.pData = pObjNode->GetRscObj();
341 					aError = pRefClass->GetRef( aTmpI, &aId );
342 				}
343 				else
344 				{
345 					if( pTC )
346 					{
347 						ByteString	aMsg( pHS->getString( pRefClass->GetId() ).getStr() );
348 						aMsg += ' ';
349 						aMsg += aId.GetName();
350 						aError = WRN_STR_REFNOTFOUND;
351 						pTC->pEH->Error( aError, rInst.pClass,
352 										 RscId(), aMsg.GetBuffer() );
353 					}
354 					break;
355 				}
356 			}
357 		}
358 	}
359 
360 	if( aError.IsOk() )
361 	{
362 		if( pObjNode )
363 		{
364 			RSCINST 	aRefI;
365 
366 			aRefI = RSCINST( pRefClass, pObjNode->GetRscObj() );
367 			aError = aRefI.pClass->WriteRc( aRefI, rMem, pTC, nDeep, bExtra );
368 		}
369 		else
370 		{
371 			if( ((RscStringInst *)rInst.pData)->pStr && pTC )
372 			{
373 				char * pStr = RscChar::MakeUTF8( ((RscStringInst *)rInst.pData)->pStr,
374 												pTC->GetSourceCharSet() );
375 				rMem.PutUTF8( pStr );
376 				rtl_freeMemory( pStr );
377 			}
378 			else
379 				rMem.PutUTF8( ((RscStringInst *)rInst.pData)->pStr );
380 		};
381 	};
382 	return( aError );
383 }
384 
385 //==================================================================
WriteRcAccess(FILE * fOutput,RscTypCont *,const char * pName)386 void RscString::WriteRcAccess
387 (
388 	FILE * fOutput,
389 	RscTypCont * /*pTC*/,
390 	const char * pName
391 )
392 {
393 	fprintf( fOutput, "\t\tString aStr( (const char*)(pResData+nOffset) );\n" );
394 	fprintf( fOutput, "\t\tSet%s( aStr );\n", pName );
395 	fprintf( fOutput, "\t\tnOffset += GetStringSizeRes( aStr );\n" );
396 }
397 
398