1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/spellcheck/hunspell/src/hunspell_fopen_hooks.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef fopen_hooks_h__ 1.9 +#define fopen_hooks_h__ 1.10 + 1.11 +/** 1.12 + * This file is force-included in hunspell code. Its purpose is to add 1.13 + * readahead to fopen() calls in hunspell without modifying its code, in order 1.14 + * to ease future upgrades. 1.15 + * 1.16 + * This file is force-included through mozilla-config.h which is generated 1.17 + * during the configure step. 1.18 + */ 1.19 + 1.20 +#include "mozilla/FileUtils.h" 1.21 +#include <stdio.h> 1.22 +#include <string.h> 1.23 + 1.24 +#if defined(XP_WIN) 1.25 +#include "nsNativeCharsetUtils.h" 1.26 +#include "nsString.h" 1.27 + 1.28 +#include <fcntl.h> 1.29 +#include <windows.h> 1.30 +// Hunspell defines a function named near. Windef.h #defines near. 1.31 +#undef near 1.32 +// mozHunspell defines a function named RemoveDirectory. 1.33 +#undef RemoveDirectory 1.34 +#endif /* defined(XP_WIN) */ 1.35 + 1.36 +inline FILE* 1.37 +hunspell_fopen_readahead(const char* filename, const char* mode) 1.38 +{ 1.39 + if (!filename || !mode) { 1.40 + return nullptr; 1.41 + } 1.42 + // Fall back to libc's fopen for modes not supported by ReadAheadFile 1.43 + if (!strchr(mode, 'r') || strchr(mode, '+')) { 1.44 + return fopen(filename, mode); 1.45 + } 1.46 + int fd = -1; 1.47 +#if defined(XP_WIN) 1.48 + // filename is obtained via the nsIFile::nativePath attribute, so 1.49 + // it is using the Windows ANSI code page, NOT UTF-8! 1.50 + nsAutoString utf16Filename; 1.51 + nsresult rv = NS_CopyNativeToUnicode(nsDependentCString(filename), 1.52 + utf16Filename); 1.53 + if (NS_FAILED(rv)) { 1.54 + return nullptr; 1.55 + } 1.56 + HANDLE handle = INVALID_HANDLE_VALUE; 1.57 + mozilla::ReadAheadFile(utf16Filename.get(), 0, SIZE_MAX, &handle); 1.58 + if (handle == INVALID_HANDLE_VALUE) { 1.59 + return nullptr; 1.60 + } 1.61 + int flags = _O_RDONLY; 1.62 + // MSVC CRT's _open_osfhandle only supports adding _O_TEXT, not _O_BINARY 1.63 + if (strchr(mode, 't')) { 1.64 + // Force translated mode 1.65 + flags |= _O_TEXT; 1.66 + } 1.67 + // Import the Win32 fd into the CRT 1.68 + fd = _open_osfhandle((intptr_t)handle, flags); 1.69 + if (fd < 0) { 1.70 + CloseHandle(handle); 1.71 + return nullptr; 1.72 + } 1.73 +#else 1.74 + mozilla::ReadAheadFile(filename, 0, SIZE_MAX, &fd); 1.75 + if (fd < 0) { 1.76 + return nullptr; 1.77 + } 1.78 +#endif /* defined(XP_WIN) */ 1.79 + 1.80 + FILE* file = fdopen(fd, mode); 1.81 + if (!file) { 1.82 + close(fd); 1.83 + } 1.84 + return file; 1.85 +} 1.86 + 1.87 +#define fopen(filename, mode) hunspell_fopen_readahead(filename, mode) 1.88 + 1.89 +#endif /* fopen_hooks_h__ */ 1.90 +