1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/hyphenation/src/hnjstdio.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +// This file provides substitutes for the basic stdio routines used by hyphen.c 1.10 +// to read its dictionary files. We #define the stdio names to these versions 1.11 +// in hnjalloc.h, so that we can use nsIURI and nsIInputStream to specify and 1.12 +// access the dictionary resources. 1.13 + 1.14 +#include "hnjalloc.h" 1.15 +#include "nsNetUtil.h" 1.16 + 1.17 +#define BUFSIZE 1024 1.18 + 1.19 +struct hnjFile_ { 1.20 + nsCOMPtr<nsIInputStream> mStream; 1.21 + char mBuffer[BUFSIZE]; 1.22 + uint32_t mCurPos; 1.23 + uint32_t mLimit; 1.24 +}; 1.25 + 1.26 +// replacement for fopen() 1.27 +// (not a full substitute: only supports read access) 1.28 +hnjFile* 1.29 +hnjFopen(const char* aURISpec, const char* aMode) 1.30 +{ 1.31 + // this override only needs to support "r" 1.32 + NS_ASSERTION(!strcmp(aMode, "r"), "unsupported fopen() mode in hnjFopen"); 1.33 + 1.34 + nsCOMPtr<nsIURI> uri; 1.35 + nsresult rv = NS_NewURI(getter_AddRefs(uri), aURISpec); 1.36 + if (NS_FAILED(rv)) { 1.37 + return nullptr; 1.38 + } 1.39 + 1.40 + nsCOMPtr<nsIInputStream> instream; 1.41 + rv = NS_OpenURI(getter_AddRefs(instream), uri); 1.42 + if (NS_FAILED(rv)) { 1.43 + return nullptr; 1.44 + } 1.45 + 1.46 + hnjFile *f = new hnjFile; 1.47 + f->mStream = instream; 1.48 + f->mCurPos = 0; 1.49 + f->mLimit = 0; 1.50 + 1.51 + return f; 1.52 +} 1.53 + 1.54 +// replacement for fclose() 1.55 +int 1.56 +hnjFclose(hnjFile* f) 1.57 +{ 1.58 + NS_ASSERTION(f && f->mStream, "bad argument to hnjFclose"); 1.59 + 1.60 + int result = 0; 1.61 + nsresult rv = f->mStream->Close(); 1.62 + if (NS_FAILED(rv)) { 1.63 + result = EOF; 1.64 + } 1.65 + f->mStream = nullptr; 1.66 + 1.67 + delete f; 1.68 + return result; 1.69 +} 1.70 + 1.71 +// replacement for fgets() 1.72 +// (not a full reimplementation, but sufficient for libhyphen's needs) 1.73 +char* 1.74 +hnjFgets(char* s, int n, hnjFile* f) 1.75 +{ 1.76 + NS_ASSERTION(s && f, "bad argument to hnjFgets"); 1.77 + 1.78 + int i = 0; 1.79 + while (i < n - 1) { 1.80 + if (f->mCurPos < f->mLimit) { 1.81 + char c = f->mBuffer[f->mCurPos++]; 1.82 + s[i++] = c; 1.83 + if (c == '\n' || c == '\r') { 1.84 + break; 1.85 + } 1.86 + continue; 1.87 + } 1.88 + 1.89 + f->mCurPos = 0; 1.90 + 1.91 + nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit); 1.92 + if (NS_FAILED(rv)) { 1.93 + f->mLimit = 0; 1.94 + return nullptr; 1.95 + } 1.96 + 1.97 + if (f->mLimit == 0) { 1.98 + break; 1.99 + } 1.100 + } 1.101 + 1.102 + if (i == 0) { 1.103 + return nullptr; // end of file 1.104 + } 1.105 + 1.106 + s[i] = '\0'; // null-terminate the returned string 1.107 + return s; 1.108 +}