diff options
author | Zolnai Tamás <zolnaitamas2000g@gmail.com> | 2012-11-26 15:14:11 +0100 |
---|---|---|
committer | Zolnai Tamás <zolnaitamas2000g@gmail.com> | 2012-11-26 15:49:56 +0100 |
commit | 54f2fad47b002c498b66b899a3cbcdad4bd11197 (patch) | |
tree | c79614eb7d7a117659fe94f8780ba8d93332c0de /l10ntools | |
parent | 2981921b49c9bc5748a01dec9ee039aace0bc80e (diff) |
Varnisch localize.cxx
Use getenv method instead of osl_getEnvironment.
Delete rtl and std namespace identifier
Use OString every possible place to decrease
OString<->OUString conversions.
Add some block to minimize scope of local variables
and add comments to make clear each block task.
Move propex "en_US exist in file name" check to
localize to save some extra process time.
Change-Id: I9a0455f7524f71e34efe4d32998d2d21601f14e1
Diffstat (limited to 'l10ntools')
-rw-r--r-- | l10ntools/source/localize.cxx | 352 | ||||
-rw-r--r-- | l10ntools/source/propex.cxx | 29 |
2 files changed, 176 insertions, 205 deletions
diff --git a/l10ntools/source/localize.cxx b/l10ntools/source/localize.cxx index cf211930631d..2a4ec00ec899 100644 --- a/l10ntools/source/localize.cxx +++ b/l10ntools/source/localize.cxx @@ -28,12 +28,11 @@ #include "boost/noncopyable.hpp" #include "osl/file.h" #include "osl/file.hxx" -#include "osl/process.h" #include "osl/thread.h" #include "rtl/string.h" #include "rtl/string.hxx" #include "rtl/textcvt.h" -#include "rtl/ustrbuf.hxx" +#include "rtl/strbuf.hxx" #include "rtl/ustring.h" #include "rtl/ustring.hxx" #include "sal/macros.h" @@ -46,37 +45,26 @@ using namespace std; namespace { -rtl::OUString getEnvironment(rtl::OUString const & variable) { - rtl::OUString value; - if (osl_getEnvironment(variable.pData, &value.pData) != osl_Process_E_None) - { - std::cerr - << "Error: cannot get environment variable " << variable << '\n'; - throw false; //TODO - } - return value; -} - class TempFile: private boost::noncopyable { public: TempFile() { if (osl::FileBase::createTempFile(0, 0, &url_) != osl::FileBase::E_None) { - std::cerr << "osl::FileBase::createTempFile() failed\n"; + cerr << "osl::FileBase::createTempFile() failed\n"; throw false; //TODO } } ~TempFile() { if (osl::File::remove(url_) != osl::FileBase::E_None) { - std::cerr << "Warning: failure removing temporary " << url_ << '\n'; + cerr << "Warning: failure removing temporary " << url_ << '\n'; } } - rtl::OUString getUrl() const { return url_; } + OUString getUrl() const { return url_; } private: - rtl::OUString url_; + OUString url_; }; struct AsciiString { @@ -85,9 +73,9 @@ struct AsciiString { }; bool matchList( - rtl::OUString const & url, AsciiString const * list, std::size_t length) + OUString const & url, AsciiString const * list, size_t length) { - for (std::size_t i = 0; i != length; ++i) { + for (size_t i = 0; i != length; ++i) { if (url.endsWithAsciiL(list[i].string, list[i].length)) { return true; } @@ -95,7 +83,7 @@ bool matchList( return false; } -bool passesNegativeList(rtl::OUString const & url) { +bool passesNegativeList(OUString const & url) { static AsciiString const list[] = { { RTL_CONSTASCII_STRINGPARAM("/dictionaries.xcu") }, { RTL_CONSTASCII_STRINGPARAM( @@ -119,7 +107,7 @@ bool passesNegativeList(rtl::OUString const & url) { return !matchList(url, list, SAL_N_ELEMENTS(list)); } -bool passesPositiveList(rtl::OUString const & url) { +bool passesPositiveList(OUString const & url) { static AsciiString const list[] = { { RTL_CONSTASCII_STRINGPARAM( "/chart2/source/controller/dialogs/res_DataLabel_tmpl.hrc") }, @@ -159,118 +147,112 @@ bool passesPositiveList(rtl::OUString const & url) { } void handleCommand( - rtl::OUString const & project, rtl::OUString const & projectRoot, - rtl::OUString const & url, rtl::OUString const & actualDir, - PoOfstream & rPoOutPut, rtl::OUString const & executable, bool positive) + OString const & project, OString const & projectRoot, + OUString const & url, OString const & actualPotDir, + PoOfstream & rPoOutPut, OString const & executable, bool positive) { if (positive ? passesPositiveList(url) : passesNegativeList(url)) { - rtl::OUString inPath; - if (osl::FileBase::getSystemPathFromFileURL(url, inPath) != - osl::FileBase::E_None) + + //Get input file path + OString inPath; { - std::cerr - << "osl::FileBase::getSystemPathFromFileURL(" << url - << ") failed\n"; - throw false; //TODO + OUString inPathTmp; + if (osl::FileBase::getSystemPathFromFileURL(url, inPathTmp) != + osl::FileBase::E_None) + { + cerr + << "osl::FileBase::getSystemPathFromFileURL(" << url + << ") failed\n"; + throw false; //TODO + } + inPath = OUStringToOString( inPathTmp, RTL_TEXTENCODING_UTF8 ); } + + //Get output file path TempFile temp; - rtl::OUString outPath; - if (osl::FileBase::getSystemPathFromFileURL(temp.getUrl(), outPath) - != osl::FileBase::E_None) + OString outPath; { - std::cerr - << "osl::FileBase::getSystemPathFromFileURL(" << temp.getUrl() - << ") failed\n"; - throw false; //TODO - } - rtl::OUStringBuffer buf( - getEnvironment( - rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SOLARVER")))); - buf.append('/'); - buf.append( - getEnvironment( - rtl::OUString( - RTL_CONSTASCII_USTRINGPARAM("INPATH_FOR_BUILD")))); - buf.appendAscii(RTL_CONSTASCII_STRINGPARAM("/bin/")); - buf.append(executable); - buf.appendAscii(RTL_CONSTASCII_STRINGPARAM(" -p ")); - buf.append(project); - buf.appendAscii(RTL_CONSTASCII_STRINGPARAM(" -r ")); - buf.append(projectRoot); - buf.appendAscii(RTL_CONSTASCII_STRINGPARAM(" -i ")); - buf.append(inPath); - buf.appendAscii(RTL_CONSTASCII_STRINGPARAM(" -o ")); - buf.append(outPath); - buf.appendAscii(RTL_CONSTASCII_STRINGPARAM(" -l en-US")); - rtl::OString cmd; - if (!buf.makeStringAndClear().convertToString( - &cmd, osl_getThreadTextEncoding(), - (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR - | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))) - { - std::cerr << "Error: Cannot convert command from UTF-16\n"; - throw false; //TODO - } - if (system(cmd.getStr()) != 0) { - std::cerr << "Error: Failed to execute " << cmd.getStr() << '\n'; - throw false; //TODO + OUString outPathTmp; + if (osl::FileBase::getSystemPathFromFileURL(temp.getUrl(),outPathTmp) + != osl::FileBase::E_None) + { + cerr + << "osl::FileBase::getSystemPathFromFileURL(" + << temp.getUrl() << ") failed\n"; + throw false; //TODO + } + outPath = OUStringToOString( outPathTmp, RTL_TEXTENCODING_UTF8 ); } - rtl::OString outPath8; - if (!outPath.convertToString( - &outPath8, osl_getThreadTextEncoding(), - (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR - | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))) + + //Call the executable { - std::cerr << "Error: Cannot convert pathname from UTF-16\n"; - throw false; //TODO + OStringBuffer buf(OString(getenv("SOLARVER"))); + buf.append('/'); + buf.append(OString(getenv("INPATH_FOR_BUILD"))); + buf.append("/bin/"); + buf.append(executable); + buf.append(" -p "); + buf.append(project); + buf.append(" -r "); + buf.append(projectRoot); + buf.append(" -i "); + buf.append(inPath); + buf.append(" -o "); + buf.append(outPath); + buf.append(" -l en-US"); + + const OString cmd = buf.makeStringAndClear(); + if (system(cmd.getStr()) != 0) { + cerr << "Error: Failed to execute " << cmd.getStr() << '\n'; + throw false; //TODO + } } - std::ifstream in(outPath8.getStr()); + + ifstream in(outPath.getStr()); if (!in.is_open()) { - std::cerr << "Error: Cannot open " << outPath8.getStr() << "\n"; + cerr << "Error: Cannot open " << outPath.getStr() << "\n"; throw false; //TODO } - std::string s; - std::getline(in, s); + string s; + getline(in, s); if (!in.eof() && !rPoOutPut.isOpen()) { - rtl::OUString outDirUrl; - if (osl::FileBase::getFileURLFromSystemPath(actualDir. - copy(0,actualDir.lastIndexOf('/')), outDirUrl) - != osl::FileBase::E_None) + //Create directory for po file { - std::cerr << "Error: Cannot convert pathname to URL\n"; - throw false; //TODO + OUString outDir = + OStringToOUString( + actualPotDir.copy(0,actualPotDir.lastIndexOf('/')), + RTL_TEXTENCODING_UTF8); + OUString outDirUrl; + if (osl::FileBase::getFileURLFromSystemPath(outDir, outDirUrl) + != osl::FileBase::E_None) + { + cerr << "Error: Cannot convert pathname to URL\n"; + throw false; //TODO + } + osl::Directory::createPath(outDirUrl); } - osl::Directory::createPath(outDirUrl); - rtl::OString outFilePath; - if (!actualDir.concat(".pot"). - convertToString( - &outFilePath, osl_getThreadTextEncoding(), - (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR - | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))) - { - std::cerr << "Error: Cannot convert pathname from UTF-16\n"; - throw false; //TODO - } - rPoOutPut.open(outFilePath.getStr()); - if (!rPoOutPut.isOpen()) + //Open po file { - std::cerr - << "Error: Cannot open po file " - << outFilePath.getStr() << "\n"; - throw false; //TODO + OString outFilePath = actualPotDir.concat(".pot"); + rPoOutPut.open(outFilePath.getStr()); + if (!rPoOutPut.isOpen()) + { + cerr + << "Error: Cannot open po file " + << outFilePath.getStr() << "\n"; + throw false; //TODO + } } - //Add header to actual po file + //Add header to po file { const sal_Int32 nProjectInd = inPath.indexOf(project); const OString relativPath = - OUStringToOString( - inPath.copy( nProjectInd, - inPath.lastIndexOf('/')- nProjectInd), - RTL_TEXTENCODING_UTF8 ); + inPath.copy( + nProjectInd, inPath.lastIndexOf('/')- nProjectInd); PoHeader aTmp(relativPath); rPoOutPut.writeHeader(aTmp); @@ -292,29 +274,29 @@ void handleCommand( { if(aException == PoEntry::INVALIDSDFLINE) { - std::cerr - << executable + cerr + << executable.getStr() << "'s output is invalid:\n" << sLine.replaceAll("\t","\\t").getStr() - << std::endl; + << endl; throw false; //TODO } } - std::getline(in, s); + getline(in, s); }; in.close(); } } void handleFile( - rtl::OUString const & project, rtl::OUString const & projectRoot, - rtl::OUString const & url, rtl::OUString const & actualDir, + OString const & project, OString const & projectRoot, + OUString const & url, OString const & actualPotDir, PoOfstream & rPoOutPut) { struct Command { char const * extension; sal_Int32 extensionLength; - char const * executable; + OString executable; bool positive; }; static Command const commands[] = { @@ -328,21 +310,23 @@ void handleFile( { RTL_CONSTASCII_STRINGPARAM(".properties"), "propex", false }, { RTL_CONSTASCII_STRINGPARAM(".ui"), "uiex", false }, { RTL_CONSTASCII_STRINGPARAM(".tree"), "treex", false } }; - for (std::size_t i = 0; i != SAL_N_ELEMENTS(commands); ++i) { + for (size_t i = 0; i != SAL_N_ELEMENTS(commands); ++i) + { if (url.endsWithAsciiL( - commands[i].extension, commands[i].extensionLength)) + commands[i].extension, commands[i].extensionLength) && + (commands[i].executable != "propex" || url.indexOf("en_US") != -1) +) { handleCommand( - project, projectRoot, url, actualDir, rPoOutPut, - rtl::OUString::createFromAscii(commands[i].executable), - commands[i].positive); + project, projectRoot, url, actualPotDir, rPoOutPut, + commands[i].executable, commands[i].positive); break; } } } -bool includeProject(rtl::OUString const & project) { - static char const * projects[] = { +bool includeProject(OString const & project) { + static OString projects[] = { "accessibility", "avmedia", "basctl", @@ -392,34 +376,34 @@ bool includeProject(rtl::OUString const & project) { "vcl", "wizards", "xmlsecurity" }; - for (std::size_t i = 0; i != SAL_N_ELEMENTS(projects); ++i) { - if (project.equalsAscii(projects[i])) { + for (size_t i = 0; i != SAL_N_ELEMENTS(projects); ++i) { + if (project == projects[i]) { return true; } } return false; } -bool excludeDirectory(rtl::OUString const & directory) { +bool excludeDirectory(OString const & directory) { // Cf. OUTPATH=* in configure.in: - static AsciiString const excluded[] = { - { RTL_CONSTASCII_STRINGPARAM("callcatcher") }, - { RTL_CONSTASCII_STRINGPARAM("unxaig") }, - { RTL_CONSTASCII_STRINGPARAM("unxand") }, - { RTL_CONSTASCII_STRINGPARAM("unxdfly") }, - { RTL_CONSTASCII_STRINGPARAM("unxfbsd") }, - { RTL_CONSTASCII_STRINGPARAM("unxios") }, - { RTL_CONSTASCII_STRINGPARAM("unxkfg") }, - { RTL_CONSTASCII_STRINGPARAM("unxlng") }, - { RTL_CONSTASCII_STRINGPARAM("unxmac") }, - { RTL_CONSTASCII_STRINGPARAM("unxnbsd") }, - { RTL_CONSTASCII_STRINGPARAM("unxobsd") }, - { RTL_CONSTASCII_STRINGPARAM("unxsog") }, - { RTL_CONSTASCII_STRINGPARAM("unxsol") }, - { RTL_CONSTASCII_STRINGPARAM("unxubt") }, - { RTL_CONSTASCII_STRINGPARAM("wntmsc") } }; - for (std::size_t i = 0; i != SAL_N_ELEMENTS(excluded); ++i) { - if (directory.matchAsciiL(excluded[i].string, excluded[i].length)) { + static OString const excluded[] = { + "callcatcher", + "unxaig", + "unxand", + "unxdfly", + "unxfbsd", + "unxios", + "unxkfg", + "unxlng", + "unxmac", + "unxnbsd", + "unxobsd", + "unxsog", + "unxsol", + "unxubt", + "wntmsc" }; + for (size_t i = 0; i != SAL_N_ELEMENTS(excluded); ++i) { + if (directory == excluded[i]) { return true; } } @@ -442,17 +426,16 @@ bool excludeDirectory(rtl::OUString const & directory) { /// /// @param the relative path back to the project root (empty and ignored if /// level <= 0) +/// @param actualPotDir the path of pot directory void handleDirectory( - rtl::OUString const & url, int level, rtl::OUString const & project, - rtl::OUString const & projectRoot, rtl::OUString const & actualDir) + OUString const & url, int level, OString const & project, + OString const & projectRoot, OString const & actualPotDir) { PoOfstream aPoOutPut; osl::Directory dir(url); if (dir.open() != osl::FileBase::E_None) { - std::cerr - << "Error: Cannot open directory: " - << rtl::OUStringToOString(url, osl_getThreadTextEncoding()).getStr() - << '\n'; + cerr + << "Error: Cannot open directory: " << url << '\n'; throw false; //TODO } for (;;) { @@ -462,55 +445,56 @@ void handleDirectory( break; } if (e != osl::FileBase::E_None) { - std::cerr << "Error: Cannot read directory\n"; + cerr << "Error: Cannot read directory\n"; throw false; //TODO } osl::FileStatus stat( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_FileURL); if (item.getFileStatus(stat) != osl::FileBase::E_None) { - std::cerr << "Error: Cannot get file status\n"; + cerr << "Error: Cannot get file status\n"; throw false; //TODO } + const OString sFileName = + OUStringToOString(stat.getFileName(),RTL_TEXTENCODING_UTF8); switch (level) { case -1: // the clone or src directory if (stat.getFileType() == osl::FileStatus::Directory) { handleDirectory( - stat.getFileURL(), 0, rtl::OUString(), - rtl::OUString(), actualDir); + stat.getFileURL(), 0, OString(), + OString(), actualPotDir); } break; case 0: // a root directory if (stat.getFileType() == osl::FileStatus::Directory) { - if (includeProject(stat.getFileName())) { + if (includeProject(sFileName)) { handleDirectory( - stat.getFileURL(), 1, stat.getFileName(), - rtl::OUString(), actualDir.concat("/"). - concat(stat.getFileName())); - } else if ( stat.getFileName() == "clone" || - stat.getFileName() == "src" ) + stat.getFileURL(), 1, sFileName, + OString(), actualPotDir.concat("/"). + concat(sFileName)); + } else if ( sFileName == "clone" || + sFileName == "src" ) { handleDirectory( - stat.getFileURL(), -1, rtl::OUString(), - rtl::OUString(), actualDir); + stat.getFileURL(), -1, OString(), + OString(), actualPotDir); } } break; default: if (stat.getFileType() == osl::FileStatus::Directory) { - if (level == 2 || !excludeDirectory(stat.getFileName())) { - rtl::OUString pr(projectRoot); + if (level == 2 || !excludeDirectory(sFileName)) { + OString pr(projectRoot); if (!pr.isEmpty()) { - pr += rtl::OUString('/'); + pr += OString('/'); } - pr += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("..")); + pr += OString(".."); handleDirectory(stat.getFileURL(), 2, project, pr, - actualDir.concat("/"). - concat(stat.getFileName())); + actualPotDir.concat("/").concat(sFileName)); } } else { handleFile(project, projectRoot, - stat.getFileURL(), actualDir, aPoOutPut); + stat.getFileURL(), actualPotDir, aPoOutPut); } break; } @@ -518,13 +502,14 @@ void handleDirectory( if (aPoOutPut.isOpen()) aPoOutPut.close(); if (dir.close() != osl::FileBase::E_None) { - std::cerr << "Error: Cannot close directory\n"; + cerr << "Error: Cannot close directory\n"; throw false; //TODO } } -void handleProjects(char const * sourceRoot, char const * destRoot) { - rtl::OUString root16; +void handleProjects(char * sourceRoot, char const * destRoot) +{ + OUString root16; if (!rtl_convertStringToUString( &root16.pData, sourceRoot, rtl_str_getLength(sourceRoot), osl_getThreadTextEncoding(), @@ -532,40 +517,29 @@ void handleProjects(char const * sourceRoot, char const * destRoot) { | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))) { - std::cerr << "Error: Cannot convert pathname to UTF-16\n"; + cerr << "Error: Cannot convert pathname to UTF-16\n"; throw false; //TODO } - rtl::OUString rootUrl; + OUString rootUrl; if (osl::FileBase::getFileURLFromSystemPath(root16, rootUrl) != osl::FileBase::E_None) { - std::cerr << "Error: Cannot convert pathname to URL\n"; - throw false; //TODO - } - rtl::OUString outPutRoot; - if (!rtl_convertStringToUString( - &outPutRoot.pData, destRoot, rtl_str_getLength(destRoot), - osl_getThreadTextEncoding(), - (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR - | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR - | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))) - { - std::cerr << "Error: Cannot convert pathname to UTF-16\n"; + cerr << "Error: Cannot convert pathname to URL\n"; throw false; //TODO } - handleDirectory(rootUrl, 0, rtl::OUString(), rtl::OUString(), outPutRoot); + handleDirectory(rootUrl, 0, OString(), OString(), OString(destRoot)); } } SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv) { if (argc != 3) { - std::cerr + cerr << ("localize (c)2001 by Sun Microsystems\n\n" "As part of the L10N framework, localize extracts en-US\n" "strings for translation out of the toplevel modules defined\n" "in projects array in l10ntools/source/localize.cxx.\n\n" "Syntax: localize <source-root> <outfile>\n"); - std::exit(EXIT_FAILURE); + exit(EXIT_FAILURE); } try { handleProjects(argv[1],argv[2]); diff --git a/l10ntools/source/propex.cxx b/l10ntools/source/propex.cxx index 2977cb158874..053858048d56 100644 --- a/l10ntools/source/propex.cxx +++ b/l10ntools/source/propex.cxx @@ -21,23 +21,20 @@ SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv) return 1; } - if( aArgs.m_sInputFile.indexOf("en_US") != -1 ) + PropParser aParser( + aArgs.m_sInputFile, Export::sLanguages, aArgs.m_bMergeMode ); + if( !aParser.isInitialized() ) { - PropParser aParser( - aArgs.m_sInputFile, Export::sLanguages, aArgs.m_bMergeMode ); - if( !aParser.isInitialized() ) - { - return 1; - } - if( aArgs.m_bMergeMode ) - { - aParser.Merge(aArgs.m_sMergeSrc, aArgs.m_sOutputFile); - } - else - { - aParser.Extract( - aArgs.m_sOutputFile, aArgs.m_sPrj, aArgs.m_sPrjRoot ); - } + return 1; + } + if( aArgs.m_bMergeMode ) + { + aParser.Merge(aArgs.m_sMergeSrc, aArgs.m_sOutputFile); + } + else + { + aParser.Extract( + aArgs.m_sOutputFile, aArgs.m_sPrj, aArgs.m_sPrjRoot ); } return 0; } |