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/.
16 #include <string_view>
20 #include <propmerge.hxx>
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
);
34 bool bIsUnicode
= true;
35 for( short nDist
= 2; nDist
<= 5; ++nDist
)
37 if( sHexDigits
.indexOf( rSource
[nIndex
+ nDist
] ) == -1 )
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
);
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
];
72 rOfstream
<< static_cast<char>( cUniCode
);
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
)
93 , m_bIsInitialized( false )
95 std::ifstream
aIfstream( m_sSource
.getStr() );
96 if( aIfstream
.is_open() )
99 std::getline( aIfstream
, s
);
100 while( !aIfstream
.eof() )
102 OString
sLine( s
.data(), s
.length() );
104 ( !sLine
.startsWith(" *") && !sLine
.startsWith("/*") ) )
106 m_vLines
.push_back( sLine
);
108 std::getline( aIfstream
, s
);
114 << "Propex error: Cannot open source file: "
115 << m_sSource
<< std::endl
;
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() )
133 << "Propex error: Cannot open pofile for extract: "
134 << rPOFile
<< std::endl
;
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
);
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() ) {
164 << "Propex error: Cannot open source file for merge: "
165 << rDestinationFile
<< std::endl
;
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
)
178 << ("Propex error: given language conflicts with language of"
181 << vLanguages
[0] << std::endl
;
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("/*") &&
193 const OString
sID( sLine
.copy( 0, sLine
.indexOf('=') ).trim() );
194 ResData
aResData( sID
, m_sSource
);
195 aResData
.sResTyp
= "property";
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
);
207 pEntrys
->GetText( sNewText
, m_sLang
);
210 if( !sNewText
.isEmpty() )
212 aDestination
<< OString(sID
+ "=");
213 lcl_PrintJavaStyle( sNewText
, aDestination
);
214 aDestination
<< std::endl
;
218 aDestination
<< sLine
<< std::endl
;
223 aDestination
<< sLine
<< std::endl
;
226 aDestination
.close();
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */