Use o3tl::convert in Math
[LibreOffice.git] / l10ntools / source / propmerge.cxx
blob8c6da8e93b544682f7363a897ae6dc5dd53eb067
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_sSource( rInputFile )
92 , m_sLang( rLang )
93 , m_bIsInitialized( false )
95 std::ifstream aIfstream( m_sSource.getStr() );
96 if( aIfstream.is_open() )
98 std::string s;
99 std::getline( aIfstream, s );
100 while( !aIfstream.eof() )
102 OString sLine( s.data(), s.length() );
103 if( bMergeMode ||
104 ( !sLine.startsWith(" *") && !sLine.startsWith("/*") ) )
106 m_vLines.push_back( sLine );
108 std::getline( aIfstream, s );
111 else
113 std::cerr
114 << "Propex error: Cannot open source file: "
115 << m_sSource << std::endl;
116 return;
118 m_bIsInitialized = true;
121 PropParser::~PropParser()
125 //Extract strings form source file
126 void PropParser::Extract( const OString& rPOFile )
128 assert( m_bIsInitialized );
129 PoOfstream aPOStream( rPOFile, PoOfstream::APP );
130 if( !aPOStream.isOpen() )
132 std::cerr
133 << "Propex error: Cannot open pofile for extract: "
134 << rPOFile << std::endl;
135 return;
138 for( size_t nIndex = 0; nIndex < m_vLines.size(); ++nIndex )
140 const OString sLine = m_vLines[nIndex];
141 const sal_Int32 nEqualSign = sLine.indexOf('=');
142 if( nEqualSign != -1 )
144 OString sID = sLine.copy( 0, nEqualSign ).trim();
145 OString sText = lcl_ConvertToUTF8( sLine.copy( nEqualSign + 1 ).trim() );
147 common::writePoEntry(
148 "Propex", aPOStream, m_sSource, "property",
149 sID, OString(), OString(), sText);
153 aPOStream.close();
156 //Merge strings to source file
157 void PropParser::Merge( const OString &rMergeSrc, const OString &rDestinationFile )
159 assert( m_bIsInitialized );
160 std::ofstream aDestination(
161 rDestinationFile.getStr(), std::ios_base::out | std::ios_base::trunc );
162 if( !aDestination.is_open() ) {
163 std::cerr
164 << "Propex error: Cannot open source file for merge: "
165 << rDestinationFile << std::endl;
166 return;
169 std::unique_ptr<MergeDataFile> pMergeDataFile;
170 if( m_sLang != "qtz" )
172 pMergeDataFile.reset( new MergeDataFile( rMergeSrc, m_sSource, false, false ) );
174 const std::vector<OString> vLanguages = pMergeDataFile->GetLanguages();
175 if( !vLanguages.empty() && vLanguages[0] != m_sLang )
177 std::cerr
178 << ("Propex error: given language conflicts with language of"
179 " Mergedata file: ")
180 << m_sLang << " - "
181 << vLanguages[0] << std::endl;
182 return;
186 for( size_t nIndex = 0; nIndex < m_vLines.size(); ++nIndex )
188 const OString sLine = m_vLines[nIndex];
189 const sal_Int32 nEqualSign = sLine.indexOf('=');
190 if( !sLine.startsWith(" *") && !sLine.startsWith("/*") &&
191 nEqualSign != -1 )
193 const OString sID( sLine.copy( 0, sLine.indexOf('=') ).trim() );
194 ResData aResData( sID, m_sSource );
195 aResData.sResTyp = "property";
196 OString sNewText;
197 if( m_sLang == "qtz" )
199 const OString sOriginText = lcl_ConvertToUTF8(sLine.copy( nEqualSign + 1 ).trim());
200 sNewText = MergeEntrys::GetQTZText(aResData, sOriginText);
202 else if( pMergeDataFile )
204 MergeEntrys* pEntrys = pMergeDataFile->GetMergeEntrys( &aResData );
205 if( pEntrys )
207 pEntrys->GetText( sNewText, m_sLang );
210 if( !sNewText.isEmpty() )
212 aDestination << OString(sID + "=");
213 lcl_PrintJavaStyle( sNewText, aDestination );
214 aDestination << std::endl;
216 else
218 aDestination << sLine << std::endl;
221 else
223 aDestination << sLine << std::endl;
226 aDestination.close();
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */