michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This file provides substitutes for the basic stdio routines used by hyphen.c michael@0: // to read its dictionary files. We #define the stdio names to these versions michael@0: // in hnjalloc.h, so that we can use nsIURI and nsIInputStream to specify and michael@0: // access the dictionary resources. michael@0: michael@0: #include "hnjalloc.h" michael@0: #include "nsNetUtil.h" michael@0: michael@0: #define BUFSIZE 1024 michael@0: michael@0: struct hnjFile_ { michael@0: nsCOMPtr mStream; michael@0: char mBuffer[BUFSIZE]; michael@0: uint32_t mCurPos; michael@0: uint32_t mLimit; michael@0: }; michael@0: michael@0: // replacement for fopen() michael@0: // (not a full substitute: only supports read access) michael@0: hnjFile* michael@0: hnjFopen(const char* aURISpec, const char* aMode) michael@0: { michael@0: // this override only needs to support "r" michael@0: NS_ASSERTION(!strcmp(aMode, "r"), "unsupported fopen() mode in hnjFopen"); michael@0: michael@0: nsCOMPtr uri; michael@0: nsresult rv = NS_NewURI(getter_AddRefs(uri), aURISpec); michael@0: if (NS_FAILED(rv)) { michael@0: return nullptr; michael@0: } michael@0: michael@0: nsCOMPtr instream; michael@0: rv = NS_OpenURI(getter_AddRefs(instream), uri); michael@0: if (NS_FAILED(rv)) { michael@0: return nullptr; michael@0: } michael@0: michael@0: hnjFile *f = new hnjFile; michael@0: f->mStream = instream; michael@0: f->mCurPos = 0; michael@0: f->mLimit = 0; michael@0: michael@0: return f; michael@0: } michael@0: michael@0: // replacement for fclose() michael@0: int michael@0: hnjFclose(hnjFile* f) michael@0: { michael@0: NS_ASSERTION(f && f->mStream, "bad argument to hnjFclose"); michael@0: michael@0: int result = 0; michael@0: nsresult rv = f->mStream->Close(); michael@0: if (NS_FAILED(rv)) { michael@0: result = EOF; michael@0: } michael@0: f->mStream = nullptr; michael@0: michael@0: delete f; michael@0: return result; michael@0: } michael@0: michael@0: // replacement for fgets() michael@0: // (not a full reimplementation, but sufficient for libhyphen's needs) michael@0: char* michael@0: hnjFgets(char* s, int n, hnjFile* f) michael@0: { michael@0: NS_ASSERTION(s && f, "bad argument to hnjFgets"); michael@0: michael@0: int i = 0; michael@0: while (i < n - 1) { michael@0: if (f->mCurPos < f->mLimit) { michael@0: char c = f->mBuffer[f->mCurPos++]; michael@0: s[i++] = c; michael@0: if (c == '\n' || c == '\r') { michael@0: break; michael@0: } michael@0: continue; michael@0: } michael@0: michael@0: f->mCurPos = 0; michael@0: michael@0: nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit); michael@0: if (NS_FAILED(rv)) { michael@0: f->mLimit = 0; michael@0: return nullptr; michael@0: } michael@0: michael@0: if (f->mLimit == 0) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (i == 0) { michael@0: return nullptr; // end of file michael@0: } michael@0: michael@0: s[i] = '\0'; // null-terminate the returned string michael@0: return s; michael@0: }