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
|
/* -*- 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/.
*/
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <fstream>
#include <iomanip>
#include "export.hxx"
#include "common.hxx"
#include "propmerge.hxx"
namespace
{
//Write out an sdf line
static void lcl_WriteSDF(
std::ofstream &aSDFStream, const OString& rText, const OString& rPrj,
const OString& rActFileName, const OString& rID )
{
OString sOutput( rPrj ); sOutput += "\t";
sOutput += rActFileName;
sOutput += "\t0\tproperty\t";
sOutput += rID; sOutput += "\t\t\t\t0\ten-US\t";
sOutput += rText; sOutput += "\t\t\t\t";
aSDFStream << sOutput.getStr() << std::endl;
}
//Find ascii escaped unicode
static sal_Int32 lcl_IndexOfUnicode(
const OString& rSource, const sal_Int32 nFrom = 0 )
{
const OString sHexDigits = "0123456789abcdefABCDEF";
sal_Int32 nIndex = rSource.indexOf( "\\u", nFrom );
if( nIndex == -1 )
{
return -1;
}
bool bIsUnicode = true;
for( short nDist = 2; nDist <= 5; ++nDist )
{
if( sHexDigits.indexOf( rSource[nIndex + nDist] ) == -1 )
{
bIsUnicode = false;
}
}
return bIsUnicode ? nIndex : -1;
}
//Convert ascii escaped unicode to utf-8
static OString lcl_ConvertToUTF8( const OString& rText )
{
OString sResult = rText;
sal_Int32 nIndex = lcl_IndexOfUnicode( sResult );
while( nIndex != -1 && nIndex < rText.getLength() )
{
const OString sHex = sResult.copy( nIndex + 2, 4 );
const sal_Unicode cDec =
static_cast<sal_Unicode>( strtol( sHex.getStr(), NULL, 16 ) );
const OString sNewChar =
OString( &cDec, 1, RTL_TEXTENCODING_UTF8 );
sResult = sResult.replaceAll( "\\u" + sHex, sNewChar );
nIndex = lcl_IndexOfUnicode( sResult, nIndex );
}
return sResult;
}
//Escape unicode characters
static void lcl_PrintJavaStyle( const OString& rText, std::ofstream &rOfstream )
{
const OUString sTemp =
OStringToOUString( rText, RTL_TEXTENCODING_UTF8 );
for ( sal_Int32 nIndex = 0; nIndex < sTemp.getLength(); ++nIndex )
{
sal_Unicode cUniCode = sTemp[nIndex];
if( cUniCode < 128 )
{
rOfstream << static_cast<char>( cUniCode );
}
else
{
rOfstream
<< "\\u"
<< std::setfill('0') << std::setw(2) << std::uppercase
<< std::hex << (cUniCode >> 8) << (cUniCode & 0xFF);
}
}
}
}
//Open source file and store its lines
PropParser::PropParser(
const OString& rInputFile, const OString& rLang,
const bool bMergeMode )
: m_vLines( std::vector<OString>() )
, m_sSource( rInputFile )
, m_sLang( rLang )
, m_bIsInitialized( false )
{
std::ifstream aIfstream( m_sSource.getStr() );
if( aIfstream.is_open() )
{
std::string s;
std::getline( aIfstream, s );
while( !aIfstream.eof() )
{
OString sLine( s.data(), s.length() );
if( bMergeMode ||
( !sLine.startsWith(" *") && !sLine.startsWith("/*") ) )
{
m_vLines.push_back( sLine );
}
std::getline( aIfstream, s );
}
}
else
{
std::cerr
<< "Propex error: Cannot open source file: "
<< m_sSource.getStr() << std::endl;
return;
}
m_bIsInitialized = true;
}
PropParser::~PropParser()
{
}
//Extract strings form source file
void PropParser::Extract(
const OString& rSDFFile, const OString& rPrj, const OString& rRoot )
{
assert( m_bIsInitialized );
std::ofstream aSDFStream(
rSDFFile.getStr(), std::ios_base::out | std::ios_base::trunc );
if( !aSDFStream.is_open() )
{
std::cerr
<< "Propex error: Cannot open sdffile for extract: "
<< rSDFFile.getStr() << std::endl;
return;
}
for( unsigned nIndex = 0; nIndex < m_vLines.size(); ++nIndex )
{
const OString sLine = m_vLines[nIndex];
const sal_Int32 nEqualSign = sLine.indexOf('=');
if( nEqualSign != -1 )
{
lcl_WriteSDF(
aSDFStream,
lcl_ConvertToUTF8( sLine.copy( nEqualSign + 1 ).trim() ),//Text
rPrj,
common::pathnameToken(
m_sSource.getStr(), rRoot.getStr()), //FileName
sLine.copy( 0, nEqualSign ).trim() ); //ID
}
}
aSDFStream.close();
}
//Merge strings to source file
void PropParser::Merge( const OString &rMergeSrc, const OString &rDestinationFile )
{
assert( m_bIsInitialized );
std::ofstream aDestination(
rDestinationFile.getStr(), std::ios_base::out | std::ios_base::trunc );
if( !aDestination.is_open() ) {
std::cerr
<< "Propex error: Cannot open source file for merge: "
<< rDestinationFile.getStr() << std::endl;
return;
}
MergeDataFile aMergeDataFile(
rMergeSrc, m_sSource, false, m_sLang == "qtz" );
const std::vector<OString> vLanguages = aMergeDataFile.GetLanguages();
if( m_sLang != "qtz" && vLanguages.size()>=2 &&
vLanguages[vLanguages[0]!="qtz" ? 0 : 1] != m_sLang )
{
std::cerr
<< "Propex error: given language conflicts with "
<< "language of Mergedata file: "
<< m_sLang.getStr() << " - "
<< vLanguages[vLanguages[0]!="qtz" ? 0 : 1].getStr() << std::endl;
return;
}
for( unsigned nIndex = 0; nIndex < m_vLines.size(); ++nIndex )
{
const OString sLine = m_vLines[nIndex];
const sal_Int32 nEqualSign = sLine.indexOf('=');
if( !sLine.startsWith(" *") && !sLine.startsWith("/*") &&
nEqualSign != -1 )
{
const OString sID( sLine.copy( 0, sLine.indexOf("=") ).trim() );
ResData aResData( "", sID , m_sSource );
aResData.sResTyp = "property";
PFormEntrys* pEntrys = aMergeDataFile.GetPFormEntrys( &aResData );
if( pEntrys )
{
OString sNewText;
pEntrys->GetText( sNewText, STRING_TYP_TEXT, m_sLang );
aDestination << (sID + OString("=")).getStr();
lcl_PrintJavaStyle( sNewText, aDestination );
aDestination << std::endl;
}
else
{
aDestination << sLine.getStr() << std::endl;
}
}
else
{
aDestination << sLine.getStr() << std::endl;
}
}
aDestination.close();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|