update PLStream to more recent version of SWI.

This commit is contained in:
Vitor Santos Costa
2010-02-22 09:35:47 +00:00
parent 4fda6b7488
commit 4a53759fc1
12 changed files with 545 additions and 292 deletions

View File

@@ -351,21 +351,25 @@ MarkExecutable(const char *name)
* FIND FILES FROM C *
*********************************/
int
static int
unifyTime(term_t t, time_t time)
{ return PL_unify_float(t, (double)time);
}
static void
static int
add_option(term_t options, functor_t f, atom_t val)
{ GET_LD
term_t head = PL_new_term_ref();
term_t head;
PL_unify_list(options, head, options);
PL_unify_term(head, PL_FUNCTOR, f, PL_ATOM, val);
if ( (head=PL_new_term_ref()) &&
PL_unify_list(options, head, options) &&
PL_unify_term(head, PL_FUNCTOR, f, PL_ATOM, val) )
{ PL_reset_term_refs(head);
return TRUE;
}
PL_reset_term_refs(head);
return FALSE;
}
#define CVT_FILENAME (CVT_ATOM|CVT_STRING|CVT_LIST)
@@ -378,29 +382,36 @@ PL_get_file_name(term_t n, char **namep, int flags)
char ospath[MAXPATHLEN];
if ( flags & PL_FILE_SEARCH )
{ predicate_t pred = PL_predicate("absolute_file_name", 3, "system");
term_t av = PL_new_term_refs(3);
term_t options = PL_copy_term_ref(av+2);
int cflags = ((flags&PL_FILE_NOERRORS) ? PL_Q_CATCH_EXCEPTION
: PL_Q_PASS_EXCEPTION);
{ fid_t fid;
PL_put_term(av+0, n);
if ( (fid = PL_open_foreign_frame()) )
{ predicate_t pred = PL_predicate("absolute_file_name", 3, "system");
term_t av = PL_new_term_refs(3);
term_t options = PL_copy_term_ref(av+2);
int rc = TRUE;
int cflags = ((flags&PL_FILE_NOERRORS) ? PL_Q_CATCH_EXCEPTION
: PL_Q_PASS_EXCEPTION);
if ( flags & PL_FILE_EXIST )
add_option(options, FUNCTOR_access1, ATOM_exist);
if ( flags & PL_FILE_READ )
add_option(options, FUNCTOR_access1, ATOM_read);
if ( flags & PL_FILE_WRITE )
add_option(options, FUNCTOR_access1, ATOM_write);
if ( flags & PL_FILE_EXECUTE )
add_option(options, FUNCTOR_access1, ATOM_execute);
PL_put_term(av+0, n);
PL_unify_nil(options);
if ( rc && flags & PL_FILE_EXIST )
rc = add_option(options, FUNCTOR_access1, ATOM_exist);
if ( rc && flags & PL_FILE_READ )
rc = add_option(options, FUNCTOR_access1, ATOM_read);
if ( rc && flags & PL_FILE_WRITE )
rc = add_option(options, FUNCTOR_access1, ATOM_write);
if ( rc && flags & PL_FILE_EXECUTE )
rc = add_option(options, FUNCTOR_access1, ATOM_execute);
if ( !PL_call_predicate(NULL, cflags, pred, av) )
return FALSE;
if ( rc ) rc = PL_unify_nil(options);
if ( rc ) rc = PL_call_predicate(NULL, cflags, pred, av);
if ( rc ) rc = PL_get_chars_ex(av+1, namep, CVT_ATOMIC|BUF_RING|REP_FN);
return PL_get_chars_ex(av+1, namep, CVT_ATOMIC|BUF_RING|REP_FN);
PL_discard_foreign_frame(fid);
return rc;
}
return FALSE;
}
if ( flags & PL_FILE_NOERRORS )
@@ -642,9 +653,51 @@ PRED_IMPL("tmp_file", 2, tmp_file, 0)
if ( !PL_get_chars(base, &n, CVT_ALL) )
return PL_error("tmp_file", 2, NULL, ERR_TYPE, ATOM_atom, base);
return PL_unify_atom(name, TemporaryFile(n));
return PL_unify_atom(name, TemporaryFile(n, NULL));
}
/** tmp_file_stream(+Mode, -File, -Stream)
*/
static
PRED_IMPL("tmp_file_stream", 3, tmp_file_stream, 0)
{ PRED_LD
atom_t fn;
int fd;
IOENC enc;
atom_t encoding;
const char *mode;
if ( !PL_get_atom_ex(A1, &encoding) )
return FALSE;
if ( (enc = atom_to_encoding(encoding)) == ENC_UNKNOWN )
{ if ( encoding == ATOM_binary )
{ enc = ENC_OCTET;
mode = "wb";
} else
{ return PL_error(NULL, 0, NULL, ERR_DOMAIN, ATOM_encoding, A1);
}
} else
{ mode = "w";
}
if ( (fn=TemporaryFile("", &fd)) )
{ IOSTREAM *s;
if ( !PL_unify_atom(A2, fn) )
{ close(fd);
return PL_error(NULL, 0, NULL, ERR_MUST_BE_VAR, 2);
}
s = Sfdopen(fd, mode);
s->encoding = enc;
return PL_unify_stream(A3, s);
} else
{ return PL_error(NULL, 0, NULL, ERR_RESOURCE, ATOM_temporary_files);
}
}
/*******************************
* CHANGE FILESYSTEM *
@@ -653,7 +706,13 @@ PRED_IMPL("tmp_file", 2, tmp_file, 0)
static
PRED_IMPL("delete_file", 1, delete_file, 0)
{ char *n;
{ PRED_LD
char *n;
atom_t aname;
if ( PL_get_atom(A1, &aname) &&
DeleteTemporaryFile(aname) )
return TRUE;
if ( !PL_get_file_name(A1, &n, 0) )
return FALSE;
@@ -662,7 +721,7 @@ PRED_IMPL("delete_file", 1, delete_file, 0)
return TRUE;
return PL_error(NULL, 0, MSG_ERRNO, ERR_FILE_OPERATION,
ATOM_delete, ATOM_file, A1);
ATOM_delete, ATOM_file, A1);
}
@@ -799,7 +858,7 @@ has_extension(const char *name, const char *ext)
static int
name_too_long()
name_too_long(void)
{ return PL_error(NULL, 0, NULL, ERR_REPRESENTATION, ATOM_max_path_length);
}
@@ -941,6 +1000,7 @@ BeginPredDefs(files)
PRED_DEF("exists_file", 1, exists_file, 0)
PRED_DEF("exists_directory", 1, exists_directory, 0)
PRED_DEF("tmp_file", 2, tmp_file, 0)
PRED_DEF("tmp_file_stream", 3, tmp_file_stream, 0)
PRED_DEF("delete_file", 1, delete_file, 0)
PRED_DEF("delete_directory", 1, delete_directory, 0)
PRED_DEF("make_directory", 1, make_directory, 0)