QMTranslatorImpl.cpp revision cb702d6d593134e47854c00a7fa1156fe48a14cc
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * VirtualBox API translation handling class
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * Copyright (C) 2014 Oracle Corporation
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * available from http://www.virtualbox.org. This file is free software;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * General Public License (GPL) as published by the Free Software
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* QM File Magic Number */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* Used internally */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const char *m_str;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync virtual const char *what() const throw() { return m_str; }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* Bytes stream. Used by the parser to iterate through the data */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Function stub for transform method */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync QMBytesStream(const uint8_t *const dataStart, size_t cbSize) :
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Sets end pointer
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * Used in message reader to detect the end of message block */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync m_end = m_dataStart + (pos && pos < m_cbSize ? pos : m_cbSize);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t result = *reinterpret_cast<const uint32_t *>(m_iter);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Reads string in UTF16 and converts it into a UTF8 string */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (size & 1) throw QMException("Incorrect string size");
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* We cannot convert to host endianess without copying the data
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * since the file might be mapped to the memory and any memory
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * change will lead to the change of the file. */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync std::transform(reinterpret_cast<const uint16_t *>(m_iter),
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return com::Utf8Str((CBSTR) &wstr.front(), wstr.size());
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Reads string in one-byte encoding
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * The string is assumed to be in ISO-8859-1 encoding */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync com::Utf8Str result(reinterpret_cast<const char *>(m_iter), size);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Checks the magic number
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * Should be called when in the beginning of the data */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline void checkMagic()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (memcmp(&(*m_iter), Magic, MagicLength)) throw QMException("Wrong magic number");
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Has we reached the end pointer? */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline bool hasFinished() { return m_iter == m_end; }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Returns current stream position */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync inline size_t tellPos() { return m_iter - m_dataStart; }
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Moves current pointer to a desired position */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Checks whether stream has enough data to read size bytes */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if (m_end - m_iter < size) throw QMException("Incorrect item size");
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* Internal QMTranslator implementation */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Everything is in UTF-8 */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync HashOffset(uint32_t _hash = 0, uint32_t _offs = 0) : hash(_hash), offset(_offs) {}
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return (hash != obj.hash ? hash < obj.hash : offset < obj.offset);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync typedef QMHashSet::const_iterator QMHashSetConstIter;
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const char *pszDisamb) const
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync uint32_t hash = calculateHash(pszSource, pszDisamb);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync lowerIter = m_hashSet.lower_bound(HashOffset(hash, 0));
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync upperIter = m_hashSet.upper_bound(HashOffset(hash, ~0));
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync const QMMessage &message = m_messageArray[iter->offset];
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync if ((!pszContext || !*pszContext || message.strContext == pszContext) &&
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync ((pszDisamb && !*pszDisamb) || message.strComment == pszDisamb))
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Try without disambiguating comment if it isn't empty */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return (iter != upperIter ? m_messageArray[iter->offset].strTranslation.c_str() : "");
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Load into local variables. If we failed during the load,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * it would allow us to keep the object in a valid (previous) state. */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Hashes and Context sections are ignored. They contain hash tables
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * to speed-up search which is not useful since we recalculate all hashes
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * and don't perform context search by hash */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync parseMessages(stream, &hashSet, &messageArray, sLen);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Only get size information to speed-up vector filling
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * if Hashes section goes in the file before Message section */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* NB! NO BREAK HERE */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Store the data into member variables.
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * The following functions never generate exceptions */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Some QM stuff */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Read messages from the stream. */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync static void parseMessages(QMBytesStream &stream, QMHashSet * const hashSet, QMMessageArray * const messageArray, size_t cbSize)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync message.hash = calculateHash(message.strSource.c_str(), message.strComment.c_str());
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Parse one message from the stream */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync static void parseMessageRecord(QMBytesStream &stream, QMMessage * const message)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Ignored as obsolete */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Ignore unknown block */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync LogRel(("QMTranslator::parseMessageRecord(): Unkown message block %x\n", type));
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* Defines the so called `hashpjw' function by P.J. Weinberger
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync 1986, 1987 Bell Telephone Laboratories, Inc.] */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync static uint32_t calculateHash(const char *pszStr1, const char *pszStr2 = 0)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync for (const char *pszStr = pszStr1; pszStr != pszStr2; pszStr = pszStr2)
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync hash = (hash << 4) + static_cast<uint8_t>(*pszStr);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* Inteface functions implementation */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncQMTranslator::QMTranslator() : _impl(new QMTranslator_Impl) {}
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncconst char *QMTranslator::translate(const char *pszContext, const char *pszSource, const char *pszDisamb) const throw()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync return _impl->translate(pszContext, pszSource, pszDisamb);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync/* The function is noexcept for now but it may be changed
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * to throw exceptions if required to catch them in another
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync * place. */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsyncint QMTranslator::load(const char *pszFilename) throw()
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync /* To free safely the file in case of exception */
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync rc = RTFileReadAll(pszFname, (void**) &data, &cbSize);
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync LogRel(("QMTranslator::load() failed to load file '%s', reason: %s\n", pszFilename, e.what()));
a9a79301a7d78abffc51521473bbb5a1e08028c5vboxsync LogRel(("QMTranslator::load() failed to load file '%s'\n", pszFilename));