Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | // This file provides substitutes for the basic stdio routines used by hyphen.c |
michael@0 | 7 | // to read its dictionary files. We #define the stdio names to these versions |
michael@0 | 8 | // in hnjalloc.h, so that we can use nsIURI and nsIInputStream to specify and |
michael@0 | 9 | // access the dictionary resources. |
michael@0 | 10 | |
michael@0 | 11 | #include "hnjalloc.h" |
michael@0 | 12 | #include "nsNetUtil.h" |
michael@0 | 13 | |
michael@0 | 14 | #define BUFSIZE 1024 |
michael@0 | 15 | |
michael@0 | 16 | struct hnjFile_ { |
michael@0 | 17 | nsCOMPtr<nsIInputStream> mStream; |
michael@0 | 18 | char mBuffer[BUFSIZE]; |
michael@0 | 19 | uint32_t mCurPos; |
michael@0 | 20 | uint32_t mLimit; |
michael@0 | 21 | }; |
michael@0 | 22 | |
michael@0 | 23 | // replacement for fopen() |
michael@0 | 24 | // (not a full substitute: only supports read access) |
michael@0 | 25 | hnjFile* |
michael@0 | 26 | hnjFopen(const char* aURISpec, const char* aMode) |
michael@0 | 27 | { |
michael@0 | 28 | // this override only needs to support "r" |
michael@0 | 29 | NS_ASSERTION(!strcmp(aMode, "r"), "unsupported fopen() mode in hnjFopen"); |
michael@0 | 30 | |
michael@0 | 31 | nsCOMPtr<nsIURI> uri; |
michael@0 | 32 | nsresult rv = NS_NewURI(getter_AddRefs(uri), aURISpec); |
michael@0 | 33 | if (NS_FAILED(rv)) { |
michael@0 | 34 | return nullptr; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | nsCOMPtr<nsIInputStream> instream; |
michael@0 | 38 | rv = NS_OpenURI(getter_AddRefs(instream), uri); |
michael@0 | 39 | if (NS_FAILED(rv)) { |
michael@0 | 40 | return nullptr; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | hnjFile *f = new hnjFile; |
michael@0 | 44 | f->mStream = instream; |
michael@0 | 45 | f->mCurPos = 0; |
michael@0 | 46 | f->mLimit = 0; |
michael@0 | 47 | |
michael@0 | 48 | return f; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | // replacement for fclose() |
michael@0 | 52 | int |
michael@0 | 53 | hnjFclose(hnjFile* f) |
michael@0 | 54 | { |
michael@0 | 55 | NS_ASSERTION(f && f->mStream, "bad argument to hnjFclose"); |
michael@0 | 56 | |
michael@0 | 57 | int result = 0; |
michael@0 | 58 | nsresult rv = f->mStream->Close(); |
michael@0 | 59 | if (NS_FAILED(rv)) { |
michael@0 | 60 | result = EOF; |
michael@0 | 61 | } |
michael@0 | 62 | f->mStream = nullptr; |
michael@0 | 63 | |
michael@0 | 64 | delete f; |
michael@0 | 65 | return result; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | // replacement for fgets() |
michael@0 | 69 | // (not a full reimplementation, but sufficient for libhyphen's needs) |
michael@0 | 70 | char* |
michael@0 | 71 | hnjFgets(char* s, int n, hnjFile* f) |
michael@0 | 72 | { |
michael@0 | 73 | NS_ASSERTION(s && f, "bad argument to hnjFgets"); |
michael@0 | 74 | |
michael@0 | 75 | int i = 0; |
michael@0 | 76 | while (i < n - 1) { |
michael@0 | 77 | if (f->mCurPos < f->mLimit) { |
michael@0 | 78 | char c = f->mBuffer[f->mCurPos++]; |
michael@0 | 79 | s[i++] = c; |
michael@0 | 80 | if (c == '\n' || c == '\r') { |
michael@0 | 81 | break; |
michael@0 | 82 | } |
michael@0 | 83 | continue; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | f->mCurPos = 0; |
michael@0 | 87 | |
michael@0 | 88 | nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit); |
michael@0 | 89 | if (NS_FAILED(rv)) { |
michael@0 | 90 | f->mLimit = 0; |
michael@0 | 91 | return nullptr; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | if (f->mLimit == 0) { |
michael@0 | 95 | break; |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | if (i == 0) { |
michael@0 | 100 | return nullptr; // end of file |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | s[i] = '\0'; // null-terminate the returned string |
michael@0 | 104 | return s; |
michael@0 | 105 | } |