2013-03-01 17:41:30 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-09-14 23:20:26 +04:00
|
|
|
|
|
|
|
// This file provides substitutes for the basic stdio routines used by hyphen.c
|
|
|
|
// to read its dictionary files. We #define the stdio names to these versions
|
|
|
|
// in hnjalloc.h, so that we can use nsIURI and nsIInputStream to specify and
|
|
|
|
// access the dictionary resources.
|
|
|
|
|
|
|
|
#include "hnjalloc.h"
|
2014-05-06 06:02:04 +04:00
|
|
|
#undef FILE // Undo the damage done in hnjalloc.h
|
2011-09-14 23:20:26 +04:00
|
|
|
#include "nsNetUtil.h"
|
2014-09-21 20:43:27 +04:00
|
|
|
#include "nsContentUtils.h"
|
2011-09-14 23:20:26 +04:00
|
|
|
|
|
|
|
#define BUFSIZE 1024
|
|
|
|
|
|
|
|
struct hnjFile_ {
|
|
|
|
nsCOMPtr<nsIInputStream> mStream;
|
|
|
|
char mBuffer[BUFSIZE];
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mCurPos;
|
|
|
|
uint32_t mLimit;
|
2011-09-14 23:20:26 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// replacement for fopen()
|
|
|
|
// (not a full substitute: only supports read access)
|
|
|
|
hnjFile*
|
|
|
|
hnjFopen(const char* aURISpec, const char* aMode)
|
|
|
|
{
|
|
|
|
// this override only needs to support "r"
|
|
|
|
NS_ASSERTION(!strcmp(aMode, "r"), "unsupported fopen() mode in hnjFopen");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURISpec);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-09-14 23:20:26 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 07:26:40 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
|
|
|
nsContentUtils::GetSystemPrincipal(),
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_OTHER);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-09-21 20:43:27 +04:00
|
|
|
|
2015-01-12 07:26:40 +03:00
|
|
|
nsCOMPtr<nsIInputStream> instream;
|
|
|
|
rv = channel->Open(getter_AddRefs(instream));
|
2011-09-14 23:20:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-09-14 23:20:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
hnjFile *f = new hnjFile;
|
|
|
|
f->mStream = instream;
|
|
|
|
f->mCurPos = 0;
|
|
|
|
f->mLimit = 0;
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// replacement for fclose()
|
|
|
|
int
|
|
|
|
hnjFclose(hnjFile* f)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(f && f->mStream, "bad argument to hnjFclose");
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
nsresult rv = f->mStream->Close();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
result = EOF;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
f->mStream = nullptr;
|
2011-09-14 23:20:26 +04:00
|
|
|
|
|
|
|
delete f;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// replacement for fgets()
|
|
|
|
// (not a full reimplementation, but sufficient for libhyphen's needs)
|
|
|
|
char*
|
|
|
|
hnjFgets(char* s, int n, hnjFile* f)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(s && f, "bad argument to hnjFgets");
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
while (i < n - 1) {
|
|
|
|
if (f->mCurPos < f->mLimit) {
|
|
|
|
char c = f->mBuffer[f->mCurPos++];
|
|
|
|
s[i++] = c;
|
|
|
|
if (c == '\n' || c == '\r') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->mCurPos = 0;
|
|
|
|
|
|
|
|
nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
f->mLimit = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-09-14 23:20:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f->mLimit == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 0) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr; // end of file
|
2011-09-14 23:20:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
s[i] = '\0'; // null-terminate the returned string
|
|
|
|
return s;
|
|
|
|
}
|