mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-10-25 16:47:29 +09:00
Update libhyphen's glue code to include overloads for fgetc() and feof().
Follow-up to 41cbe2d931d2742bb8dd6240eae9599e3bf3a512 to fix a crash regression. This resolves #374.
This commit is contained in:
parent
2a5df33520
commit
b88143e4be
2 changed files with 42 additions and 15 deletions
|
|
@ -31,6 +31,8 @@
|
||||||
#define fopen(path,mode) hnjFopen(path,mode)
|
#define fopen(path,mode) hnjFopen(path,mode)
|
||||||
#define fclose(file) hnjFclose(file)
|
#define fclose(file) hnjFclose(file)
|
||||||
#define fgets(buf,count,file) hnjFgets(buf,count,file)
|
#define fgets(buf,count,file) hnjFgets(buf,count,file)
|
||||||
|
#define feof(file) hnjFeof(file)
|
||||||
|
#define fgetc(file) hnjFgetc(file)
|
||||||
|
|
||||||
typedef struct hnjFile_ hnjFile;
|
typedef struct hnjFile_ hnjFile;
|
||||||
|
|
||||||
|
|
@ -44,6 +46,10 @@ int hnjFclose(hnjFile* f);
|
||||||
|
|
||||||
char* hnjFgets(char* s, int n, hnjFile* f);
|
char* hnjFgets(char* s, int n, hnjFile* f);
|
||||||
|
|
||||||
|
int hnjFeof(hnjFile* f);
|
||||||
|
|
||||||
|
int hnjFgetc(hnjFile* f);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ struct hnjFile_ {
|
||||||
char mBuffer[BUFSIZE];
|
char mBuffer[BUFSIZE];
|
||||||
uint32_t mCurPos;
|
uint32_t mCurPos;
|
||||||
uint32_t mLimit;
|
uint32_t mLimit;
|
||||||
|
bool mEOF;
|
||||||
};
|
};
|
||||||
|
|
||||||
// replacement for fopen()
|
// replacement for fopen()
|
||||||
|
|
@ -58,6 +59,7 @@ hnjFopen(const char* aURISpec, const char* aMode)
|
||||||
f->mStream = instream;
|
f->mStream = instream;
|
||||||
f->mCurPos = 0;
|
f->mCurPos = 0;
|
||||||
f->mLimit = 0;
|
f->mLimit = 0;
|
||||||
|
f->mEOF = false;
|
||||||
|
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
@ -79,6 +81,27 @@ hnjFclose(hnjFile* f)
|
||||||
return result;
|
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()
|
// replacement for fgets()
|
||||||
// (not a full reimplementation, but sufficient for libhyphen's needs)
|
// (not a full reimplementation, but sufficient for libhyphen's needs)
|
||||||
char*
|
char*
|
||||||
|
|
@ -88,24 +111,15 @@ hnjFgets(char* s, int n, hnjFile* f)
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i < n - 1) {
|
while (i < n - 1) {
|
||||||
if (f->mCurPos < f->mLimit) {
|
int c = hnjFgetc(f);
|
||||||
char c = f->mBuffer[f->mCurPos++];
|
|
||||||
s[i++] = c;
|
if (c == EOF) {
|
||||||
if (c == '\n' || c == '\r') {
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
f->mCurPos = 0;
|
s[i++] = c;
|
||||||
|
|
||||||
nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit);
|
if (c == '\n' || c == '\r') {
|
||||||
if (NS_FAILED(rv)) {
|
|
||||||
f->mLimit = 0;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (f->mLimit == 0) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -117,3 +131,10 @@ hnjFgets(char* s, int n, hnjFile* f)
|
||||||
s[i] = '\0'; // null-terminate the returned string
|
s[i] = '\0'; // null-terminate the returned string
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
hnjFeof(hnjFile* f)
|
||||||
|
{
|
||||||
|
return f->mEOF ? EOF : 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue