extensions/spellcheck/hunspell/src/hunspell_fopen_hooks.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #ifndef fopen_hooks_h__
michael@0 6 #define fopen_hooks_h__
michael@0 7
michael@0 8 /**
michael@0 9 * This file is force-included in hunspell code. Its purpose is to add
michael@0 10 * readahead to fopen() calls in hunspell without modifying its code, in order
michael@0 11 * to ease future upgrades.
michael@0 12 *
michael@0 13 * This file is force-included through mozilla-config.h which is generated
michael@0 14 * during the configure step.
michael@0 15 */
michael@0 16
michael@0 17 #include "mozilla/FileUtils.h"
michael@0 18 #include <stdio.h>
michael@0 19 #include <string.h>
michael@0 20
michael@0 21 #if defined(XP_WIN)
michael@0 22 #include "nsNativeCharsetUtils.h"
michael@0 23 #include "nsString.h"
michael@0 24
michael@0 25 #include <fcntl.h>
michael@0 26 #include <windows.h>
michael@0 27 // Hunspell defines a function named near. Windef.h #defines near.
michael@0 28 #undef near
michael@0 29 // mozHunspell defines a function named RemoveDirectory.
michael@0 30 #undef RemoveDirectory
michael@0 31 #endif /* defined(XP_WIN) */
michael@0 32
michael@0 33 inline FILE*
michael@0 34 hunspell_fopen_readahead(const char* filename, const char* mode)
michael@0 35 {
michael@0 36 if (!filename || !mode) {
michael@0 37 return nullptr;
michael@0 38 }
michael@0 39 // Fall back to libc's fopen for modes not supported by ReadAheadFile
michael@0 40 if (!strchr(mode, 'r') || strchr(mode, '+')) {
michael@0 41 return fopen(filename, mode);
michael@0 42 }
michael@0 43 int fd = -1;
michael@0 44 #if defined(XP_WIN)
michael@0 45 // filename is obtained via the nsIFile::nativePath attribute, so
michael@0 46 // it is using the Windows ANSI code page, NOT UTF-8!
michael@0 47 nsAutoString utf16Filename;
michael@0 48 nsresult rv = NS_CopyNativeToUnicode(nsDependentCString(filename),
michael@0 49 utf16Filename);
michael@0 50 if (NS_FAILED(rv)) {
michael@0 51 return nullptr;
michael@0 52 }
michael@0 53 HANDLE handle = INVALID_HANDLE_VALUE;
michael@0 54 mozilla::ReadAheadFile(utf16Filename.get(), 0, SIZE_MAX, &handle);
michael@0 55 if (handle == INVALID_HANDLE_VALUE) {
michael@0 56 return nullptr;
michael@0 57 }
michael@0 58 int flags = _O_RDONLY;
michael@0 59 // MSVC CRT's _open_osfhandle only supports adding _O_TEXT, not _O_BINARY
michael@0 60 if (strchr(mode, 't')) {
michael@0 61 // Force translated mode
michael@0 62 flags |= _O_TEXT;
michael@0 63 }
michael@0 64 // Import the Win32 fd into the CRT
michael@0 65 fd = _open_osfhandle((intptr_t)handle, flags);
michael@0 66 if (fd < 0) {
michael@0 67 CloseHandle(handle);
michael@0 68 return nullptr;
michael@0 69 }
michael@0 70 #else
michael@0 71 mozilla::ReadAheadFile(filename, 0, SIZE_MAX, &fd);
michael@0 72 if (fd < 0) {
michael@0 73 return nullptr;
michael@0 74 }
michael@0 75 #endif /* defined(XP_WIN) */
michael@0 76
michael@0 77 FILE* file = fdopen(fd, mode);
michael@0 78 if (!file) {
michael@0 79 close(fd);
michael@0 80 }
michael@0 81 return file;
michael@0 82 }
michael@0 83
michael@0 84 #define fopen(filename, mode) hunspell_fopen_readahead(filename, mode)
michael@0 85
michael@0 86 #endif /* fopen_hooks_h__ */
michael@0 87

mercurial