Fix typos
[LibreOffice.git] / l10ntools / source / propmerge.cxx
blob3922d172398ee49463b90cf1e6fb81ee01c9a034
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <memory>
11 #include <cstdlib>
12 #include <cassert>
13 #include <iostream>
14 #include <fstream>
15 #include <iomanip>
16 #include <string_view>
18 #include <export.hxx>
19 #include <common.hxx>
20 #include <propmerge.hxx>
22 namespace
24 //Find ascii escaped unicode
25 sal_Int32 lcl_IndexOfUnicode(
26 const OString& rSource, const sal_Int32 nFrom = 0 )
28 const OString sHexDigits = "0123456789abcdefABCDEF";
29 sal_Int32 nIndex = rSource.indexOf( "\\u", nFrom );
30 if( nIndex == -1 )
32 return -1;
34 bool bIsUnicode = true;
35 for( short nDist = 2; nDist <= 5; ++nDist )
37 if( sHexDigits.indexOf( rSource[nIndex + nDist] ) == -1 )
39 bIsUnicode = false;
42 return bIsUnicode ? nIndex : -1;
45 //Convert ascii escaped unicode to utf-8
46 OString lcl_ConvertToUTF8( const OString& rText )
48 OString sResult = rText;
49 sal_Int32 nIndex = lcl_IndexOfUnicode( sResult );
50 while( nIndex != -1 && nIndex < rText.getLength() )
52 const OString sHex = sResult.copy( nIndex + 2, 4 );
53 const sal_Unicode cDec =
54 static_cast<sal_Unicode>( strtol( sHex.getStr(), nullptr, 16 ) );
55 const OString sNewChar( &cDec, 1, RTL_TEXTENCODING_UTF8 );
56 sResult = sResult.replaceAll( "\\u" + sHex, sNewChar );
57 nIndex = lcl_IndexOfUnicode( sResult, nIndex );
59 return sResult;
62 //Escape unicode characters
63 void lcl_PrintJavaStyle( std::string_view rText, std::ofstream &rOfstream )
65 const OUString sTemp =
66 OStringToOUString( rText, RTL_TEXTENCODING_UTF8 );
67 for ( sal_Int32 nIndex = 0; nIndex < sTemp.getLength(); ++nIndex )
69 sal_Unicode cUniCode = sTemp[nIndex];
70 if( cUniCode < 128 )
72 rOfstream << static_cast<char>( cUniCode );
74 else
76 rOfstream
77 << "\\u"
78 << std::setfill('0') << std::setw(2) << std::uppercase
79 << std::hex << (cUniCode >> 8)
80 << std::setfill('0') << std::setw(2) << std::uppercase
81 << std::hex << (cUniCode & 0xFF);
87 //Open source file and store its lines
88 PropParser::PropParser(
89 const OString& rInputFile, const OString& rLang,
90 const bool bMergeMode )
91 : m_vLines( std::vector<OString>() )
92 , m_sSource( rInputFile )
93 , m_sLang( rLang )
94 , m_bIsInitialized( false )
96 std::ifstream aIfstream( m_sSource.getStr() );
97 if( aIfstream.is_open() )
99 std::string s;
100 std::getline( aIfstream, s );
101 while( !aIfstream.eof() )
103 OString sLine( s.data(), s.length() );
104 if( bMergeMode ||
105 ( !sLine.startsWith(" *") && !sLine.startsWith("/*") ) )
107 m_vLines.push_back( sLine );
109 std::getline( aIfstream, s );
112 else
114 std::cerr
115 << "Propex error: Cannot open source file: "
116 << m_sSource << std::endl;
117 return;
119 m_bIsInitialized = true;
122 PropParser::~PropParser()
126 //Extract strings form source file
127 void PropParser::Extract( const OString& rPOFile )
129 assert( m_bIsInitialized );
130 PoOfstream aPOStream( rPOFile, PoOfstream::APP );
131 if( !aPOStream.isOpen() )
133 std::cerr
134 << "Propex error: Cannot open pofile for extract: "
135 << rPOFile << std::endl;
136 return;
139 for( size_t nIndex = 0; nIndex < m_vLines.size(); ++nIndex )
141 const OString sLine = m_vLines[nIndex];
142 const sal_Int32 nEqualSign = sLine.indexOf('=');
143 if( nEqualSign != -1 )
145 OString sID = sLine.copy( 0, nEqualSign ).trim();
146 OString sText = lcl_ConvertToUTF8( sLine.copy( nEqualSign + 1 ).trim() );
148 common::writePoEntry(
149 "Propex", aPOStream, m_sSource, "property",
150 sID, OString(), OString(), sText);
154 aPOStream.close();
157 //Merge strings to source file
158 void PropParser::Merge( const OString &rMergeSrc, const OString &rDestinationFile )
160 assert( m_bIsInitialized );
161 std::ofstream aDestination(
162 rDestinationFile.getStr(), std::ios_base::out | std::ios_base::trunc );
163 if( !aDestination.is_open() ) {
164 std::cerr
165 << "Propex error: Cannot open source file for merge: "
166 << rDestinationFile << std::endl;
167 return;
170 std::unique_ptr<MergeDataFile> pMergeDataFile;
171 if( m_sLang != "qtz" )
173 pMergeDataFile.reset( new MergeDataFile( rMergeSrc, m_sSource, false, false ) );
175 const std::vector<OString> vLanguages = pMergeDataFile->GetLanguages();
176 if( !vLanguages.empty() && vLanguages[0] != m_sLang )
178 std::cerr
179 << ("Propex error: given language conflicts with language of"
180 " Mergedata file: ")
181 << m_sLang << " - "
182 << vLanguages[0] << std::endl;
183 return;
187 for( size_t nIndex = 0; nIndex < m_vLines.size(); ++nIndex )
189 const OString sLine = m_vLines[nIndex];
190 const sal_Int32 nEqualSign = sLine.indexOf('=');
191 if( !sLine.startsWith(" *") && !sLine.startsWith("/*") &&
192 nEqualSign != -1 )
194 const OString sID( sLine.copy( 0, sLine.indexOf('=') ).trim() );
195 ResData aResData( sID, m_sSource );
196 aResData.sResTyp = "property";
197 OString sNewText;
198 if( m_sLang == "qtz" )
200 const OString sOriginText = lcl_ConvertToUTF8(sLine.copy( nEqualSign + 1 ).trim());
201 sNewText = MergeEntrys::GetQTZText(aResData, sOriginText);
203 else if( pMergeDataFile )
205 MergeEntrys* pEntrys = pMergeDataFile->GetMergeEntrys( &aResData );
206 if( pEntrys )
208 pEntrys->GetText( sNewText, m_sLang );
211 if( !sNewText.isEmpty() )
213 aDestination << OString(sID + "=");
214 lcl_PrintJavaStyle( sNewText, aDestination );
215 aDestination << std::endl;
217 else
219 aDestination << sLine << std::endl;
222 else
224 aDestination << sLine << std::endl;
227 aDestination.close();
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */