Bug 1448771 - Update hnjstdio to handle additional functions from stdio.h that libhyphen wants to use. r=glandium, a=RyanVM
--- a/intl/hyphenation/glue/hnjalloc.h
+++ b/intl/hyphenation/glue/hnjalloc.h
@@ -26,26 +26,32 @@
#include <stdio.h> /* ensure stdio.h is loaded before our macros */
#undef FILE
#define FILE hnjFile
#define fopen(path,mode) hnjFopen(path,mode)
#define fclose(file) hnjFclose(file)
#define fgets(buf,count,file) hnjFgets(buf,count,file)
+#define feof(file) hnjFeof(file)
+#define fgetc(file) hnjFgetc(file)
typedef struct hnjFile_ hnjFile;
#ifdef __cplusplus
extern "C" {
#endif
hnjFile* hnjFopen(const char* aURISpec, const char* aMode);
int hnjFclose(hnjFile* f);
char* hnjFgets(char* s, int n, hnjFile* f);
+int hnjFeof(hnjFile* f);
+
+int hnjFgetc(hnjFile* f);
+
#ifdef __cplusplus
}
#endif
--- a/intl/hyphenation/glue/hnjstdio.cpp
+++ b/intl/hyphenation/glue/hnjstdio.cpp
@@ -17,16 +17,17 @@
#define BUFSIZE 1024
struct hnjFile_ {
nsCOMPtr<nsIInputStream> mStream;
char mBuffer[BUFSIZE];
uint32_t mCurPos;
uint32_t mLimit;
+ bool mEOF;
};
// 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"
@@ -53,16 +54,17 @@ hnjFopen(const char* aURISpec, const cha
if (NS_FAILED(rv)) {
return nullptr;
}
hnjFile *f = new hnjFile;
f->mStream = instream;
f->mCurPos = 0;
f->mLimit = 0;
+ f->mEOF = false;
return f;
}
// replacement for fclose()
int
hnjFclose(hnjFile* f)
{
@@ -74,46 +76,64 @@ hnjFclose(hnjFile* f)
result = EOF;
}
f->mStream = nullptr;
delete f;
return result;
}
+// replacement for fgetc()
+int
+hnjFgetc(hnjFile* f)
+{
+ if (f->mCurPos >= f->mLimit) {
+ f->mCurPos = 0;
+
+ nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit);
+ if (NS_FAILED(rv)) {
+ f->mLimit = 0;
+ }
+
+ if (f->mLimit == 0) {
+ f->mEOF = true;
+ return EOF;
+ }
+ }
+
+ return f->mBuffer[f->mCurPos++];
+}
+
// 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;
+ int c = hnjFgetc(f);
+
+ if (c == EOF) {
+ break;
}
- f->mCurPos = 0;
+ s[i++] = c;
- nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit);
- if (NS_FAILED(rv)) {
- f->mLimit = 0;
- return nullptr;
- }
-
- if (f->mLimit == 0) {
+ if (c == '\n' || c == '\r') {
break;
}
}
if (i == 0) {
return nullptr; // end of file
}
s[i] = '\0'; // null-terminate the returned string
return s;
}
+
+int
+hnjFeof(hnjFile* f)
+{
+ return f->mEOF ? EOF : 0;
+}