Branch libreoffice-5-0-4
[LibreOffice.git] / l10ntools / source / propmerge.cxx
bloba41cd154d4c30532618c6c2a6e8cc42dce78ca5e
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 <cstdlib>
11 #include <cassert>
12 #include <iostream>
13 #include <fstream>
14 #include <iomanip>
16 #include "export.hxx"
17 #include "common.hxx"
18 #include "propmerge.hxx"
20 namespace
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 );
28 if( nIndex == -1 )
30 return -1;
32 bool bIsUnicode = true;
33 for( short nDist = 2; nDist <= 5; ++nDist )
35 if( sHexDigits.indexOf( rSource[nIndex + nDist] ) == -1 )
37 bIsUnicode = false;
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 );
58 return sResult;
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];
69 if( cUniCode < 128 )
71 rOfstream << static_cast<char>( cUniCode );
73 else
75 rOfstream
76 << "\\u"
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 )
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.getStr() << 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.getStr() << std::endl;
135 return;
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);
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.getStr() << std::endl;
166 return;
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 )
177 std::cerr
178 << ("Propex error: given language conflicts with language of"
179 " Mergedata file: ")
180 << m_sLang.getStr() << " - "
181 << vLanguages[0].getStr() << std::endl;
182 delete pMergeDataFile;
183 return;
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("/*") &&
192 nEqualSign != -1 )
194 const OString sID( sLine.copy( 0, sLine.indexOf('=') ).trim() );
195 ResData aResData( sID, m_sSource );
196 aResData.sResTyp = "property";
197 OString sNewText;
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 );
206 if( pEntrys )
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;
217 else
219 aDestination << sLine.getStr() << std::endl;
222 else
224 aDestination << sLine.getStr() << std::endl;
227 aDestination.close();
228 delete pMergeDataFile;
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */