+ ifstream is(filen.c_str(), ios::in | ios::binary);
+ if (!is.read(&moData[0], buf.st_size)) {
+ LYXERR0("Cannot read file " << filen);
+ return false;
+ }
+
+ MoHeader * header = reinterpret_cast<MoHeader *>(&moData[0]);
+
+ bool doSwap = false;
+ if (header->magic == 0xde120495) {
+ header->swapEnd();
+ doSwap = true;
+ }
+
+ if (header->magic != 0x950412de) {
+ LYXERR0("Wrong magic number for file " << filen
+ << ".\nExpected 0x950412de, got 0x" << std::hex << header->magic << std::dec);
+ return false;
+ }
+
+ StringTable * orig = reinterpret_cast<StringTable *>(&moData[0] + header->O);
+ StringTable * trans = reinterpret_cast<StringTable *>(&moData[0] + header->T);
+ // First the header
+ if (doSwap) {
+ // Handle endiannness change
+ orig[0].swapEnd();
+ trans[0].swapEnd();
+ }
+ string const info = string(&moData[0] + trans[0].offset, trans[0].length);
+ size_t pos = info.find("charset=");
+ if (pos != string::npos) {
+ pos += 8;
+ string charset;
+ size_t pos2 = info.find("\n", pos);
+ if (pos2 == string::npos)
+ charset = info.substr(pos);
+ else
+ charset = info.substr(pos, pos2 - pos);
+ charset = ascii_lowercase(trim(charset));
+ if (charset != "utf-8") {
+ LYXERR0("Wrong encoding " << charset << " for file " << filen);
+ return false;
+ }
+ } else {
+ LYXERR0("Cannot find encoding encoding for file " << filen);
+ return false;
+ }
+
+ for (size_t i = 1; i < header->N; ++i) {
+ if (doSwap) {
+ // Handle endiannness change
+ orig[i].swapEnd();
+ trans[i].swapEnd();
+ }
+ // Note that in theory the strings may contain NUL characters.
+ // This may be the case with plural forms
+ string const ostr(&moData[0] + orig[i].offset, orig[i].length);
+ docstring tstr = from_utf8(string(&moData[0] + trans[i].offset,
+ trans[i].length));
+ cleanTranslation(tstr);
+ trans_map_[ostr] = tstr;
+ //lyxerr << ostr << " ==> " << tstr << endl;
+ }