1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <rtl/ustring.hxx>
11 #include <o3tl/string_view.hxx>
18 #include <string_view>
22 #include <propmerge.hxx>
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
)
37 bool bIsUnicode
= true;
38 for( short nDist
= 2; nDist
<= 5; ++nDist
)
40 if( sHexDigits
.find( rSource
[nIndex
+ nDist
] ) == std::string_view::npos
)
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
);
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
];
75 rOfstream
<< static_cast<char>( cUniCode
);
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() )
102 while( std::getline( aIfstream
, s
) )
104 OString
sLine( s
.data(), s
.length() );
106 ( !sLine
.startsWith(" *") && !sLine
.startsWith("/*") ) )
108 m_vLines
.push_back( sLine
);
115 << "Propex error: Cannot open source file: "
116 << m_sSource
<< std::endl
;
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() )
134 << "Propex error: Cannot open pofile for extract: "
135 << rPOFile
<< std::endl
;
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
);
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() ) {
165 << "Propex error: Cannot open source file for merge: "
166 << rDestinationFile
<< std::endl
;
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
)
179 << ("Propex error: given language conflicts with language of"
182 << vLanguages
[0] << std::endl
;
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("/*") &&
194 const OString
sID( o3tl::trim(sLine
.subView( 0, sLine
.indexOf('=') )) );
195 ResData
aResData( sID
, m_sSource
);
196 aResData
.sResTyp
= "property"_ostr
;
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
);
208 pEntrys
->GetText( sNewText
, m_sLang
);
211 if( !sNewText
.isEmpty() )
213 aDestination
<< OString(sID
+ "=");
214 lcl_PrintJavaStyle( sNewText
, aDestination
);
215 aDestination
<< std::endl
;
219 aDestination
<< sLine
<< std::endl
;
224 aDestination
<< sLine
<< std::endl
;
227 aDestination
.close();
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */