tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / l10ntools / source / propmerge.cxx
blobd3b6314ba0af94ae6c4c62a26160b7634276031a
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 <rtl/ustring.hxx>
11 #include <o3tl/string_view.hxx>
13 #include <memory>
14 #include <cstdlib>
15 #include <cassert>
16 #include <iostream>
17 #include <iomanip>
18 #include <string_view>
20 #include <export.hxx>
21 #include <common.hxx>
22 #include <propmerge.hxx>
23 #include <utility>
25 namespace
27 //Find ascii escaped unicode
28 sal_Int32 lcl_IndexOfUnicode(
29 std::string_view rSource, const sal_Int32 nFrom = 0 )
31 static constexpr std::string_view sHexDigits = "0123456789abcdefABCDEF";
32 size_t nIndex = rSource.find( "\\u", nFrom );
33 if( nIndex == std::string_view::npos )
35 return -1;
37 bool bIsUnicode = true;
38 for( short nDist = 2; nDist <= 5; ++nDist )
40 if( sHexDigits.find( rSource[nIndex + nDist] ) == std::string_view::npos )
42 bIsUnicode = false;
45 return bIsUnicode ? nIndex : -1;
48 //Convert ascii escaped unicode to utf-8
49 OString lcl_ConvertToUTF8( const OString& rText )
51 OString sResult = rText;
52 sal_Int32 nIndex = lcl_IndexOfUnicode( sResult );
53 while( nIndex != -1 && nIndex < rText.getLength() )
55 const OString sHex = sResult.copy( nIndex + 2, 4 );
56 const sal_Unicode cDec =
57 static_cast<sal_Unicode>( strtol( sHex.getStr(), nullptr, 16 ) );
58 const OString sNewChar( &cDec, 1, RTL_TEXTENCODING_UTF8 );
59 sResult = sResult.replaceAll( "\\u" + sHex, sNewChar );
60 nIndex = lcl_IndexOfUnicode( sResult, nIndex );
62 return sResult;
65 //Escape unicode characters
66 void lcl_PrintJavaStyle( std::string_view rText, std::ofstream &rOfstream )
68 const OUString sTemp =
69 OStringToOUString( rText, RTL_TEXTENCODING_UTF8 );
70 for ( sal_Int32 nIndex = 0; nIndex < sTemp.getLength(); ++nIndex )
72 sal_Unicode cUniCode = sTemp[nIndex];
73 if( cUniCode < 128 )
75 rOfstream << static_cast<char>( cUniCode );
77 else
79 rOfstream
80 << "\\u"
81 << std::setfill('0') << std::setw(2) << std::uppercase
82 << std::hex << (cUniCode >> 8)
83 << std::setfill('0') << std::setw(2) << std::uppercase
84 << std::hex << (cUniCode & 0xFF);
90 //Open source file and store its lines
91 PropParser::PropParser(
92 OString _sInputFile, OString _sLang,
93 const bool bMergeMode )
94 : m_sSource(std::move( _sInputFile ))
95 , m_sLang(std::move( _sLang ))
96 , m_bIsInitialized( false )
98 std::ifstream aIfstream( m_sSource.getStr() );
99 if( aIfstream.is_open() )
101 std::string s;
102 while( std::getline( aIfstream, s ) )
104 OString sLine( s.data(), s.length() );
105 if( bMergeMode ||
106 ( !sLine.startsWith(" *") && !sLine.startsWith("/*") ) )
108 m_vLines.push_back( sLine );
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 std::string_view sID = o3tl::trim(sLine.subView( 0, nEqualSign ));
146 OString sText = lcl_ConvertToUTF8( OString(o3tl::trim(sLine.subView( nEqualSign + 1 ))) );
148 common::writePoEntry(
149 "Propex"_ostr, aPOStream, m_sSource, "property",
150 OString(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( o3tl::trim(sLine.subView( 0, sLine.indexOf('=') )) );
195 ResData aResData( sID, m_sSource );
196 aResData.sResTyp = "property"_ostr;
197 OString sNewText;
198 if( m_sLang == "qtz" )
200 const OString sOriginText = lcl_ConvertToUTF8(OString(o3tl::trim(sLine.subView( nEqualSign + 1 ))));
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: */