1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rscdb.hxx>
#include <rscstr.hxx>
#include <rtl/textcvt.h>
#include <rtl/textenc.h>
RscString::RscString( Atom nId, RESOURCE_TYPE nTypeId )
: RscTop( nId, nTypeId )
, pRefClass(nullptr)
{
}
RSCCLASS_TYPE RscString::GetClassType() const
{
return RSCCLASS_STRING;
}
ERRTYPE RscString::SetString( const RSCINST & rInst, const char * pStr )
{
char * pTmp;
ERRTYPE aError;
if( aError.IsOk() )
{
reinterpret_cast<RscStringInst *>(rInst.pData)->bDflt = false;
pTmp = reinterpret_cast<RscStringInst *>(rInst.pData)->pStr;
if( pTmp )
{
rtl_freeMemory( pTmp );
pTmp = nullptr;
}
if( pStr )
{
sal_uInt32 nLen = strlen( pStr ) +1;
pTmp = static_cast<char *>(rtl_allocateMemory( nLen ));
memcpy( pTmp, pStr, nLen );
}
reinterpret_cast<RscStringInst *>(rInst.pData)->pStr = pTmp;
}
return aError;
}
ERRTYPE RscString::GetString( const RSCINST & rInst, char ** ppStr )
{
*ppStr = reinterpret_cast<RscStringInst *>(rInst.pData)->pStr;
return ERR_OK;
}
ERRTYPE RscString::GetRef( const RSCINST & rInst, RscId * pRscId )
{
*pRscId = reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId;
return ERR_OK;
}
ERRTYPE RscString::SetRef( const RSCINST & rInst, const RscId & rRefId )
{
if( pRefClass )
{
reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId = rRefId;
reinterpret_cast<RscStringInst *>(rInst.pData)->bDflt = false;
}
else
return ERR_REFNOTALLOWED;
return ERR_OK;
}
RSCINST RscString::Create( RSCINST * pInst, const RSCINST & rDflt,
bool bOwnClass )
{
RSCINST aInst;
if( !pInst )
{
aInst.pClass = this;
aInst.pData = static_cast<CLASS_DATA>(
rtl_allocateMemory( sizeof( RscStringInst ) ));
}
else
aInst = *pInst;
if( !bOwnClass && rDflt.IsInst() )
bOwnClass = rDflt.pClass->InHierarchy( this );
reinterpret_cast<RscStringInst *>(aInst.pData)->aRefId.Create();
reinterpret_cast<RscStringInst *>(aInst.pData)->pStr = nullptr;
reinterpret_cast<RscStringInst *>(aInst.pData)->bDflt = true;
if( bOwnClass )
{
reinterpret_cast<RscStringInst *>(aInst.pData)->aRefId =
reinterpret_cast<RscStringInst *>(rDflt.pData)->aRefId;
SetString( aInst, reinterpret_cast<RscStringInst *>(rDflt.pData)->pStr );
reinterpret_cast<RscStringInst *>(aInst.pData)->bDflt =
reinterpret_cast<RscStringInst *>(rDflt.pData)->bDflt;
}
return aInst;
}
void RscString::Destroy( const RSCINST & rInst )
{
if( reinterpret_cast<RscStringInst *>(rInst.pData)->pStr )
rtl_freeMemory( reinterpret_cast<RscStringInst *>(rInst.pData)->pStr );
reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId.Destroy();
}
bool RscString::IsValueDefault( const RSCINST & rInst, CLASS_DATA pDef )
{
RscStringInst * pData = reinterpret_cast<RscStringInst*>(rInst.pData);
RscStringInst * pDefData = reinterpret_cast<RscStringInst*>(pDef);
if( pDef )
{
if( pData->aRefId.IsId() || pDefData->aRefId.IsId() )
{
if( pData->aRefId.aExp.IsNumber() &&
pDefData->aRefId.aExp.IsNumber() )
{
// check whether reference identifiers are equal
if( pData->aRefId.GetNumber() == pDefData->aRefId.GetNumber() )
{
return true;
}
}
}
else
{
bool bStrEmpty = false;
bool bDefStrEmpty = false;
if( pData->pStr )
{
bStrEmpty = ('\0' == *pData->pStr);
}
else
bStrEmpty = true;
if( pDefData->pStr )
{
bDefStrEmpty = ('\0' == *pDefData->pStr);
}
else
bDefStrEmpty = true;
if( !bStrEmpty || !bDefStrEmpty )
{
return false;
}
return true;
}
}
return false;
}
void RscString::WriteSrc( const RSCINST & rInst, FILE * fOutput,
RscTypCont *, sal_uInt32, const char * )
{
if ( reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId.IsId() )
{
fprintf( fOutput, "%s",
reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId.GetName().getStr() );
}
else
{
RscStringInst * pStrI = reinterpret_cast<RscStringInst *>(rInst.pData);
if( pStrI->pStr ){
//char * pChangeTab = RscChar::GetChangeTab();
sal_uInt32 n = 0;
sal_uInt32 nPos, nSlashPos;
do
{
fputc( '\"', fOutput );
nSlashPos = nPos = 0;
while( pStrI->pStr[ n ] && (nPos < 72 || nPos - nSlashPos <= 3) )
{ // after \ drop at least 3 characters \xa7
fputc( pStrI->pStr[ n ], fOutput );
if( pStrI->pStr[ n ] == '\\' )
nSlashPos = nPos;
n++;
nPos++;
}
fputc( '\"', fOutput );
if( pStrI->pStr[ n ] ) // end not reached yet
{
fputc( '\n', fOutput );
}
}
while( pStrI->pStr[ n ] );
}
else
fprintf( fOutput, "\"\"" );
}
}
ERRTYPE RscString::WriteRc( const RSCINST & rInst, RscWriteRc & rMem,
RscTypCont * pTC, sal_uInt32 nDeep, bool bExtra )
{
ERRTYPE aError;
ObjNode * pObjNode = nullptr;
if( reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId.IsId() )
{
RscId aId( reinterpret_cast<RscStringInst *>(rInst.pData)->aRefId );
RSCINST aTmpI;
aTmpI.pClass = pRefClass;
while( aError.IsOk() && aId.IsId() )
{
// increment and test to avoid endless recursion
nDeep++;
if( nDeep > nRefDeep )
aError = ERR_REFTODEEP;
else
{
pObjNode = pRefClass->GetObjNode( aId );
if( pObjNode )
{
aTmpI.pData = pObjNode->GetRscObj();
aError = pRefClass->GetRef( aTmpI, &aId );
}
else
{
if( pTC )
{
OStringBuffer aMsg(pHS->getString(
pRefClass->GetId()));
aMsg.append(' ').append(aId.GetName());
aError = WRN_STR_REFNOTFOUND;
pTC->pEH->Error( aError, rInst.pClass,
RscId(), aMsg.getStr() );
}
break;
}
}
}
}
if( aError.IsOk() )
{
if( pObjNode )
{
RSCINST aRefI;
aRefI = RSCINST( pRefClass, pObjNode->GetRscObj() );
aError = aRefI.pClass->WriteRc( aRefI, rMem, pTC, nDeep, bExtra );
}
else
{
if( reinterpret_cast<RscStringInst *>(rInst.pData)->pStr && pTC )
{
char * pStr = RscChar::MakeUTF8( reinterpret_cast<RscStringInst *>(rInst.pData)->pStr,
pTC->GetSourceCharSet() );
rMem.PutUTF8( pStr );
rtl_freeMemory( pStr );
}
else
rMem.PutUTF8( reinterpret_cast<RscStringInst *>(rInst.pData)->pStr );
}
}
return aError;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|