Bug 1448771 - Update hnjstdio to handle additional functions from stdio.h that libhyphen wants to use. r=glandium, a=RyanVM
authorJonathan Kew <jkew@mozilla.com>
Wed, 28 Mar 2018 10:17:51 +0100 (2018-03-28)
changeset 460438 2f2e601e1394cbe2965a65f0218708eec9d5b81d
parent 460437 294c100c1a17f2087ad712fab7861521ef4247ef
child 460439 75cefda36f283a32b8b48f5ac7924f4d41b44463
push id8942
push userryanvm@gmail.com
push dateThu, 29 Mar 2018 14:09:55 +0000 (2018-03-29)
treeherdermozilla-beta@75cefda36f28 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium, RyanVM
bugs1448771
milestone60.0
Bug 1448771 - Update hnjstdio to handle additional functions from stdio.h that libhyphen wants to use. r=glandium, a=RyanVM
intl/hyphenation/glue/hnjalloc.h
intl/hyphenation/glue/hnjstdio.cpp
--- 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;
+}