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.

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

mercurial