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/.
18 #include "propmerge.hxx"
22 //Find ascii escaped unicode
23 static sal_Int32
lcl_IndexOfUnicode(
24 const OString
& rSource
, const sal_Int32 nFrom
= 0 )
26 const OString sHexDigits
= "0123456789abcdefABCDEF";
27 sal_Int32 nIndex
= rSource
.indexOf( "\\u", nFrom
);
32 bool bIsUnicode
= true;
33 for( short nDist
= 2; nDist
<= 5; ++nDist
)
35 if( sHexDigits
.indexOf( rSource
[nIndex
+ nDist
] ) == -1 )
40 return bIsUnicode
? nIndex
: -1;
43 //Convert ascii escaped unicode to utf-8
44 static OString
lcl_ConvertToUTF8( const OString
& rText
)
46 OString sResult
= rText
;
47 sal_Int32 nIndex
= lcl_IndexOfUnicode( sResult
);
48 while( nIndex
!= -1 && nIndex
< rText
.getLength() )
50 const OString sHex
= sResult
.copy( nIndex
+ 2, 4 );
51 const sal_Unicode cDec
=
52 static_cast<sal_Unicode
>( strtol( sHex
.getStr(), NULL
, 16 ) );
53 const OString sNewChar
=
54 OString( &cDec
, 1, RTL_TEXTENCODING_UTF8
);
55 sResult
= sResult
.replaceAll( "\\u" + sHex
, sNewChar
);
56 nIndex
= lcl_IndexOfUnicode( sResult
, nIndex
);
61 //Escape unicode characters
62 static void lcl_PrintJavaStyle( const OString
& rText
, std::ofstream
&rOfstream
)
64 const OUString sTemp
=
65 OStringToOUString( rText
, RTL_TEXTENCODING_UTF8
);
66 for ( sal_Int32 nIndex
= 0; nIndex
< sTemp
.getLength(); ++nIndex
)
68 sal_Unicode cUniCode
= sTemp
[nIndex
];
71 rOfstream
<< static_cast<char>( cUniCode
);
77 << std::setfill('0') << std::setw(2) << std::uppercase
78 << std::hex
<< (cUniCode
>> 8)
79 << std::setfill('0') << std::setw(2) << std::uppercase
80 << std::hex
<< (cUniCode
& 0xFF);
86 //Open source file and store its lines
87 PropParser::PropParser(
88 const OString
& rInputFile
, const OString
& rLang
,
89 const bool bMergeMode
)
90 : m_vLines( std::vector
<OString
>() )
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
.getStr() << 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
.getStr() << std::endl
;
138 for( unsigned 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
.getStr() << std::endl
;
169 MergeDataFile
* pMergeDataFile
= 0;
170 if( m_sLang
!= "qtz" )
172 pMergeDataFile
= new MergeDataFile( rMergeSrc
, m_sSource
, false, false );
174 const std::vector
<OString
> vLanguages
= pMergeDataFile
->GetLanguages();
175 if( vLanguages
.size()>=1 && vLanguages
[0] != m_sLang
)
178 << ("Propex error: given language conflicts with language of"
180 << m_sLang
.getStr() << " - "
181 << vLanguages
[0].getStr() << std::endl
;
182 delete pMergeDataFile
;
187 for( unsigned 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( sLine
.copy( 0, sLine
.indexOf('=') ).trim() );
195 ResData
aResData( sID
, m_sSource
);
196 aResData
.sResTyp
= "property";
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
);
208 pEntrys
->GetText( sNewText
, STRING_TYP_TEXT
, m_sLang
);
211 if( !sNewText
.isEmpty() )
213 aDestination
<< OString(sID
+ "=").getStr();
214 lcl_PrintJavaStyle( sNewText
, aDestination
);
215 aDestination
<< std::endl
;
219 aDestination
<< sLine
.getStr() << std::endl
;
224 aDestination
<< sLine
.getStr() << std::endl
;
227 aDestination
.close();
228 delete pMergeDataFile
;
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */