win32 support, especially win does not allow file streaming in memory

This commit is contained in:
Vítor Santos Costa 2016-02-24 02:07:46 +00:00
parent 3af377d610
commit e4e59dedf7
4 changed files with 39 additions and 36 deletions

View File

@ -24,7 +24,7 @@ static int GETW(int sno) {
case ENC_ISO_ANSI: { case ENC_ISO_ANSI: {
char buf[8]; char buf[8];
int out; int out;
int wch; wchar_t wch;
mbstate_t mbstate; mbstate_t mbstate;
memset((void *)&(mbstate), 0, sizeof(mbstate_t)); memset((void *)&(mbstate), 0, sizeof(mbstate_t));

View File

@ -196,6 +196,9 @@ void Yap_DefaultStreamOps(StreamDesc *st) {
if (st->status & (Promptable_Stream_f)) { if (st->status & (Promptable_Stream_f)) {
st->stream_wgetc = get_wchar; st->stream_wgetc = get_wchar;
Yap_ConsoleOps(st, true); Yap_ConsoleOps(st, true);
} else if (st->status & (InMemory_Stream_f)) {
st->stream_wgetc = get_wchar;
Yap_ConsoleOps(st, true);
} else if (st->encoding == LOCAL_encoding) { } else if (st->encoding == LOCAL_encoding) {
st->stream_wgetc = get_wchar_from_file; st->stream_wgetc = get_wchar_from_file;
} else } else

View File

@ -1,4 +1,4 @@
/************************************************************************* /*************************************************************************post/////85
* * * *
* YAP Prolog * * YAP Prolog *
* * * *
@ -38,7 +38,7 @@ static char SccsId[] = "%W% %G%";
#if HAVE_IO_H #if HAVE_IO_H
/* Windows */ /* Windows */
#include <io.h> #include <io.h>
#endif #endif
#if HAVE_SOCKET #if HAVE_SOCKET
#include <winsock2.h> #include <winsock2.h>
#endif #endif
@ -55,11 +55,11 @@ static char SccsId[] = "%W% %G%";
FILE * open_memstream (char **buf, size_t *len); FILE * open_memstream (char **buf, size_t *len);
#endif #endif
#if HAVE_FMEMOPEN #if HAVE_FMEMOPEN
#define MAY_READ 1 #define MAY_READ 1
#endif #endif
#if HAVE_OPEN_MEMSTREAM #if HAVE_OPEN_MEMSTREAM
#define MAY_READ 1 #define MAY_READ 1
#define MAY_WRITE 1 #define MAY_WRITE 1
#endif #endif
@ -82,12 +82,12 @@ MemGetc (int sno)
spos = s->u.mem_string.pos; spos = s->u.mem_string.pos;
if (spos == s->u.mem_string.max_size) { if (spos == s->u.mem_string.max_size) {
return post_process_eof(s); return EOF;
} else { } else {
ch = s->u.mem_string.buf[spos]; ch = s->u.mem_string.buf[spos];
s->u.mem_string.pos = ++spos; s->u.mem_string.pos = ++spos;
} }
return post_process_read_char(ch, s); return ch;
} }
#endif #endif
@ -166,7 +166,7 @@ MemPutc(int sno, int ch)
FILE *f; FILE *f;
encoding_t encoding; encoding_t encoding;
stream_flags_t flags; stream_flags_t flags;
sno = GetFreeStreamD(); sno = GetFreeStreamD();
if (sno < 0) if (sno < 0)
return (PlIOError (RESOURCE_ERROR_MAX_STREAMS,TermNil, "new stream not available for open_mem_read_stream/1")); return (PlIOError (RESOURCE_ERROR_MAX_STREAMS,TermNil, "new stream not available for open_mem_read_stream/1"));
@ -254,7 +254,7 @@ Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp, memBufSou
int sno; int sno;
StreamDesc *st; StreamDesc *st;
sno = GetFreeStreamD(); sno = GetFreeStreamD();
if (sno < 0) if (sno < 0)
return -1; return -1;
@ -322,11 +322,11 @@ open_mem_write_stream (USES_REGS1) /* $open_mem_write_stream(-Stream) */
return (Yap_unify (ARG1, t)); return (Yap_unify (ARG1, t));
} }
/** /**
* Yap_PeekMemwriteStream() shows the current buffer for a memory stream. * Yap_PeekMemwriteStream() shows the current buffer for a memory stream.
* *
* @param sno, the in-memory stream * @param sno, the in-memory stream
* *
* @return temporary buffer, discarded by close and may be moved away * @return temporary buffer, discarded by close and may be moved away
* by other writes.. * by other writes..
*/ */
@ -413,13 +413,13 @@ bool Yap_CloseMemoryStream( int sno )
fclose(GLOBAL_Stream[sno].file); fclose(GLOBAL_Stream[sno].file);
if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f) if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f)
free( GLOBAL_Stream[sno].nbuf ); free( GLOBAL_Stream[sno].nbuf );
#else #else
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE) if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE)
Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf); Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf);
else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) { else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
free(GLOBAL_Stream[sno].u.mem_string.buf); free(GLOBAL_Stream[sno].u.mem_string.buf);
} }
#endif #endif
} else { } else {
#if MAY_READ #if MAY_READ
fclose(GLOBAL_Stream[sno].file); fclose(GLOBAL_Stream[sno].file);
@ -430,7 +430,7 @@ bool Yap_CloseMemoryStream( int sno )
else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) { else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
free(GLOBAL_Stream[sno].u.mem_string.buf); free(GLOBAL_Stream[sno].u.mem_string.buf);
} }
#endif #endif
} }
return true; return true;
} }
@ -446,4 +446,3 @@ Yap_InitMems( void )
Yap_InitCPred ("peek_mem_write_stream", 3, peek_mem_write_stream, SyncPredFlag); Yap_InitCPred ("peek_mem_write_stream", 3, peek_mem_write_stream, SyncPredFlag);
CurrentModule = cm; CurrentModule = cm;
} }

View File

@ -541,14 +541,14 @@ DirName(const char *X) {
} }
#endif #endif
static const char *myrealpath( const char *path) static const char *myrealpath( const char *path, char *out)
{ {
#if _WIN32 || defined(__MINGW32__) #if _WIN32 || defined(__MINGW32__)
DWORD retval=0; DWORD retval=0;
// notice that the file does not need to exist // notice that the file does not need to exist
retval = GetFullPathName(path, retval = GetFullPathName(path,
YAP_FILENAME_MAX, MAX_PATH-1,
out, out,
NULL); NULL);
@ -594,9 +594,10 @@ static const char *myrealpath( const char *path)
} }
} }
#endif #endif
char *out = malloc(strlen(path)+1); char *rc = malloc(strlen(path)+1);
strcpy( out, path); strcpy( rc, path);
return out; const char * f = rc;
return f;
} }
static const char * static const char *
@ -605,9 +606,10 @@ expandVars(const char *spec)
CACHE_REGS CACHE_REGS
#if _WIN32 || defined(__MINGW32__) #if _WIN32 || defined(__MINGW32__)
char u[YAP_FILENAME_MAX+1]; char u[YAP_FILENAME_MAX+1];
char *out;
// first pass, remove Unix style stuff // first pass, remove Unix style stuff
if ((ou=unix2win(spec, YAP_FILENAME_MAX)) == NULL) if ((out=unix2win(spec, u, YAP_FILENAME_MAX)) == NULL)
return NULL; return NULL;
spec = u; spec = u;
#endif #endif
@ -619,7 +621,7 @@ expandVars(const char *spec)
if (IsPairTerm(t)) if (IsPairTerm(t))
return RepAtom(AtomOfTerm(HeadOfTerm(t)))->StrOfAE; return RepAtom(AtomOfTerm(HeadOfTerm(t)))->StrOfAE;
return NULL; return NULL;
} }
return spec; return spec;
} }
@ -639,7 +641,7 @@ Yap_AbsoluteFile(const char *spec, bool ok)
rc = PlExpandVars(spec, NULL, NULL); rc = PlExpandVars(spec, NULL, NULL);
if (!rc) if (!rc)
rc = spec; rc = spec;
if ((p = myrealpath(rc) ) ) { if ((p = myrealpath(rc, NULL )) ) {
return p; return p;
} else { } else {
return NULL; return NULL;
@ -648,7 +650,7 @@ Yap_AbsoluteFile(const char *spec, bool ok)
} }
/** /**
* generate absolute path and stores path in an user given buffer. If * generate absolute path and stores path in an user given buffer. If
* NULL, uses a temporary buffer that must be quickly released. * NULL, uses a temporary buffer that must be quickly released.
* *
* if ok first expand variable names and do globbing * if ok first expand variable names and do globbing
@ -671,8 +673,8 @@ Yap_AbsoluteFileInBuffer(const char *spec, char *out, size_t sz, bool ok)
} else { } else {
rc = spec; rc = spec;
} }
if ((p = myrealpath(rc) ) ) { if ((p = myrealpath(rc, out) ) ) {
if (!out) { if (!out) {
out = LOCAL_FileNameBuf; out = LOCAL_FileNameBuf;
sz = YAP_FILENAME_MAX-1; sz = YAP_FILENAME_MAX-1;
@ -702,15 +704,14 @@ do_glob(const char *spec, bool glob_vs_wordexp)
WIN32_FIND_DATA find; WIN32_FIND_DATA find;
HANDLE hFind; HANDLE hFind;
CELL *dest; CELL *dest;
char *espec;
Term tf;
// first pass, remove Unix style stuff // first pass, remove Unix style stuff
if (unix2win(espec, u, YAP_FILENAME_MAX) == NULL) if ((espec =unix2win(spec, u, YAP_FILENAME_MAX)) == NULL)
return TermNil; return TermNil;
espec = (const char *)u; espec = (const char *)u;
if (!use_system_expansion) {
return MkPairTerm(MkAtomTerm(Yap_LookupAtom(espec)), TermNil);
}
hFind = FindFirstFile(espec, &find); hFind = FindFirstFile(espec, &find);
if (hFind == INVALID_HANDLE_VALUE) if (hFind == INVALID_HANDLE_VALUE)
@ -860,6 +861,7 @@ prolog_realpath( USES_REGS1 )
{ {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
const char *cmd; const char *cmd;
char out[YAP_FILENAME_MAX];
if (IsAtomTerm(t1)) { if (IsAtomTerm(t1)) {
cmd = RepAtom(AtomOfTerm(t1))->StrOfAE; cmd = RepAtom(AtomOfTerm(t1))->StrOfAE;
@ -868,7 +870,7 @@ prolog_realpath( USES_REGS1 )
} else { } else {
return false; return false;
} }
const char *rc = myrealpath( cmd ); const char *rc = myrealpath( cmd , out);
if (!rc) { if (!rc) {
PlIOError( SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, strerror(errno)); PlIOError( SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, strerror(errno));
return false; return false;
@ -901,7 +903,7 @@ static const param_t expand_filename_defs[] = {EXPAND_FILENAME_DEFS()};
static Term static Term
do_expand_file_name(Term t1, Term opts USES_REGS) do_expand_file_name(Term t1, Term opts USES_REGS)
{ {
xarg *args; xarg *args;
expand_filename_enum_choices_t i; expand_filename_enum_choices_t i;
bool use_system_expansion = true; bool use_system_expansion = true;
@ -969,7 +971,7 @@ do_expand_file_name(Term t1, Term opts USES_REGS)
/* @pred expand_file_name( +Pattern, -ListOfPaths) is det /* @pred expand_file_name( +Pattern, -ListOfPaths) is det
This builtin receives a pattern and expands it into a list of files. This builtin receives a pattern and expands it into a list of files.
In Unix-like systems, YAP applies glob to expand patterns such as '*', '.', and '?'. Further variable expansion In Unix-like systems, YAP applies glob to expand patterns such as '*', '.', and '?'. Further variable expansion
may also happen. glob is shell-dependent: som Yap_InitCPred ("absolute_file_system_path", 2, absolute_file_system_path, 0); may also happen. glob is shell-dependent: som Yap_InitCPred ("absolute_file_system_path", 2, absolute_file_system_path, 0);
Yap_InitCPred ("real_path", 2, prolog_realpath, 0); Yap_InitCPred ("real_path", 2, prolog_realpath, 0);
Yap_InitCPred ("true_file_name", 2, Yap_InitCPred ("true_file_name", 2,
@ -1786,7 +1788,7 @@ p_mv ( USES_REGS1 )
Yap_Error(TYPE_ERROR_ATOM, t2, "second argument to rename/2 not atom"); Yap_Error(TYPE_ERROR_ATOM, t2, "second argument to rename/2 not atom");
} else { } else {
oldname = (RepAtom(AtomOfTerm(t1)))->StrOfAE; oldname = (RepAtom(AtomOfTerm(t1)))->StrOfAE;
newname = (RepAtom(AtomOfTerm(t2)))->StrOfAE; newname = (RepAtom(AtomOfTerm(t2)))->StrOfAE;
if ((r = link (oldname, newname)) == 0 && (r = unlink (oldname)) != 0) if ((r = link (oldname, newname)) == 0 && (r = unlink (oldname)) != 0)
unlink (newname); unlink (newname);
if (r != 0) { if (r != 0) {
@ -2297,4 +2299,3 @@ Yap_InitSysPreds(void)
Yap_InitCPred ("rmdir", 2, p_rmdir, SyncPredFlag); Yap_InitCPred ("rmdir", 2, p_rmdir, SyncPredFlag);
Yap_InitCPred ("make_directory", 1, make_directory, SyncPredFlag); Yap_InitCPred ("make_directory", 1, make_directory, SyncPredFlag);
} }