BOM fixes
This commit is contained in:
parent
1a23e47316
commit
784f9a2880
45
os/iopreds.c
45
os/iopreds.c
@ -1256,6 +1256,8 @@ typedef enum open_enum_choices { OPEN_DEFS() } open_choices_t;
|
|||||||
static const param_t open_defs[] = {OPEN_DEFS()};
|
static const param_t open_defs[] = {OPEN_DEFS()};
|
||||||
#undef PAR
|
#undef PAR
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static Int
|
static Int
|
||||||
do_open(Term file_name, Term t2,
|
do_open(Term file_name, Term t2,
|
||||||
Term tlist USES_REGS) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */
|
Term tlist USES_REGS) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */
|
||||||
@ -1264,7 +1266,7 @@ do_open(Term file_name, Term t2,
|
|||||||
SMALLUNSGN s;
|
SMALLUNSGN s;
|
||||||
char io_mode[8];
|
char io_mode[8];
|
||||||
StreamDesc *st;
|
StreamDesc *st;
|
||||||
bool avoid_bom = true, needs_bom = false, bin = false;
|
bool avoid_bom = false, needs_bom = false;
|
||||||
char *fname;
|
char *fname;
|
||||||
stream_flags_t flags;
|
stream_flags_t flags;
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
@ -1361,42 +1363,32 @@ do_open(Term file_name, Term t2,
|
|||||||
flags |= Binary_Stream_f;
|
flags |= Binary_Stream_f;
|
||||||
encoding = ENC_OCTET;
|
encoding = ENC_OCTET;
|
||||||
avoid_bom = true;
|
avoid_bom = true;
|
||||||
|
needs_bom = false;
|
||||||
} else if (t == TermText) {
|
} else if (t == TermText) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
strncat(io_mode, "t", 8);
|
strncat(io_mode, "t", 8);
|
||||||
#endif
|
#endif
|
||||||
/* note that this matters for UNICODE style conversions */
|
/* note that this matters for UNICODE style conversions */
|
||||||
#if MAC
|
|
||||||
if (open_mode == AtomWrite) {
|
|
||||||
Yap_SetTextFile(RepAtom(AtomOfTerm(file_name))->StrOfAE);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} else {
|
} else {
|
||||||
Yap_Error(DOMAIN_ERROR_STREAM, tlist,
|
Yap_Error(DOMAIN_ERROR_STREAM, tlist,
|
||||||
"type is ~a, must be one of binary or text", t);
|
"type is ~a, must be one of binary or text", t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// BOM mess
|
// BOM mess
|
||||||
if ((encoding == ENC_OCTET || encoding == ENC_ISO_ASCII ||
|
if (encoding == ENC_UTF16_BE || encoding == ENC_UTF16_LE ||
|
||||||
encoding == ENC_ISO_LATIN1 || encoding == ENC_ISO_UTF8 || bin)) {
|
encoding == ENC_ISO_UTF32_BE || encoding == ENC_ISO_UTF32_LE ) {
|
||||||
avoid_bom = true;
|
needs_bom = true;
|
||||||
}
|
}
|
||||||
if (args[OPEN_BOM].used) {
|
if (args[OPEN_BOM].used) {
|
||||||
if (args[OPEN_BOM].tvalue == TermTrue) {
|
if (args[OPEN_BOM].tvalue == TermTrue) {
|
||||||
|
avoid_bom = false;
|
||||||
needs_bom = true;
|
needs_bom = true;
|
||||||
if (avoid_bom) {
|
|
||||||
return (PlIOError(SYSTEM_ERROR_INTERNAL, file_name,
|
|
||||||
"BOM not compatible with encoding"));
|
|
||||||
}
|
|
||||||
} else if (args[OPEN_BOM].tvalue == TermFalse) {
|
} else if (args[OPEN_BOM].tvalue == TermFalse) {
|
||||||
needs_bom = false;
|
|
||||||
avoid_bom = true;
|
avoid_bom = true;
|
||||||
} else {
|
needs_bom = false;
|
||||||
Yap_Error(DOMAIN_ERROR_STREAM, tlist,
|
|
||||||
"bom is ~a, should be one of true or false",
|
|
||||||
args[OPEN_BOM].tvalue);
|
|
||||||
}
|
}
|
||||||
} else if (st - GLOBAL_Stream < 3) {
|
}
|
||||||
|
if (st - GLOBAL_Stream < 3) {
|
||||||
flags |= RepError_Prolog_f;
|
flags |= RepError_Prolog_f;
|
||||||
}
|
}
|
||||||
if ((fd = fopen(fname, io_mode)) == NULL ||
|
if ((fd = fopen(fname, io_mode)) == NULL ||
|
||||||
@ -1420,19 +1412,9 @@ do_open(Term file_name, Term t2,
|
|||||||
return false;
|
return false;
|
||||||
if (open_mode == AtomWrite) {
|
if (open_mode == AtomWrite) {
|
||||||
if (needs_bom && !write_bom(sno, st))
|
if (needs_bom && !write_bom(sno, st))
|
||||||
return FALSE;
|
return false;
|
||||||
} else if ((open_mode == AtomRead) && !avoid_bom &&
|
} else if ( open_mode == AtomRead && !avoid_bom ) {
|
||||||
(needs_bom || (flags & Seekable_Stream_f))) {
|
|
||||||
check_bom(sno, st); // can change encoding
|
check_bom(sno, st); // can change encoding
|
||||||
if (st->encoding == ENC_ISO_UTF32_BE) {
|
|
||||||
Yap_Error(DOMAIN_ERROR_STREAM_ENCODING, ARG1,
|
|
||||||
"UTF-32 (BE) stream encoding unsupported");
|
|
||||||
return FALSE;
|
|
||||||
} else if (st->encoding == ENC_ISO_UTF32_LE) {
|
|
||||||
Yap_Error(DOMAIN_ERROR_STREAM_ENCODING, ARG1,
|
|
||||||
"UTF-32 (LE) stream encoding unsupported");
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UNLOCK(st->streamlock);
|
UNLOCK(st->streamlock);
|
||||||
@ -1441,7 +1423,6 @@ do_open(Term file_name, Term t2,
|
|||||||
return (Yap_unify(ARG3, t));
|
return (Yap_unify(ARG3, t));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Int open3(USES_REGS1) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */
|
static Int open3(USES_REGS1) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */
|
||||||
return do_open(Deref(ARG1), Deref(ARG2), TermNil PASS_REGS);
|
return do_open(Deref(ARG1), Deref(ARG2), TermNil PASS_REGS);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user