summaryrefslogtreecommitdiffstats
path: root/intl/hyphenation/glue/hnjstdio.cpp
diff options
context:
space:
mode:
authorwolfbeast <mcwerewolf@gmail.com>2018-05-19 21:25:26 +0200
committerwolfbeast <mcwerewolf@gmail.com>2018-05-19 21:30:57 +0200
commita2c339417025a8cb68e99aa28a416f95c5fec465 (patch)
tree12eeacbd4a8b85b759771187e728bb54645cdb4a /intl/hyphenation/glue/hnjstdio.cpp
parentb3ebf5ea2fc031edaf5e2a006fd3c33d95caadb8 (diff)
downloadUXP-a2c339417025a8cb68e99aa28a416f95c5fec465.tar
UXP-a2c339417025a8cb68e99aa28a416f95c5fec465.tar.gz
UXP-a2c339417025a8cb68e99aa28a416f95c5fec465.tar.lz
UXP-a2c339417025a8cb68e99aa28a416f95c5fec465.tar.xz
UXP-a2c339417025a8cb68e99aa28a416f95c5fec465.zip
Update libhyphen's glue code to include overloads for fgetc() and feof().
Follow-up to 41cbe2d931d2742bb8dd6240eae9599e3bf3a512 to fix a crash regression. This resolves #374.
Diffstat (limited to 'intl/hyphenation/glue/hnjstdio.cpp')
-rw-r--r--intl/hyphenation/glue/hnjstdio.cpp51
1 files changed, 36 insertions, 15 deletions
diff --git a/intl/hyphenation/glue/hnjstdio.cpp b/intl/hyphenation/glue/hnjstdio.cpp
index 660ebaf13..5be8b7c1d 100644
--- a/intl/hyphenation/glue/hnjstdio.cpp
+++ b/intl/hyphenation/glue/hnjstdio.cpp
@@ -22,6 +22,7 @@ struct hnjFile_ {
char mBuffer[BUFSIZE];
uint32_t mCurPos;
uint32_t mLimit;
+ bool mEOF;
};
// replacement for fopen()
@@ -58,6 +59,7 @@ hnjFopen(const char* aURISpec, const char* aMode)
f->mStream = instream;
f->mCurPos = 0;
f->mLimit = 0;
+ f->mEOF = false;
return f;
}
@@ -79,6 +81,27 @@ hnjFclose(hnjFile* 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*
@@ -88,24 +111,15 @@ hnjFgets(char* s, int n, hnjFile* f)
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;
+ int c = hnjFgetc(f);
- nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit);
- if (NS_FAILED(rv)) {
- f->mLimit = 0;
- return nullptr;
+ if (c == EOF) {
+ break;
}
- if (f->mLimit == 0) {
+ s[i++] = c;
+
+ if (c == '\n' || c == '\r') {
break;
}
}
@@ -117,3 +131,10 @@ hnjFgets(char* s, int n, hnjFile* f)
s[i] = '\0'; // null-terminate the returned string
return s;
}
+
+
+int
+hnjFeof(hnjFile* f)
+{
+ return f->mEOF ? EOF : 0;
+} \ No newline at end of file