intl/hyphenation/src/hnjstdio.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 }

mercurial