2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_stream.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_STREAM_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
#include "gb_common_swap.h"
|
|
|
|
#include "gb_common_check.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbx_value.h"
|
|
|
|
#include "gb_limit.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_project.h"
|
|
|
|
#include "gb_file.h"
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "gbx_regexp.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gbx_string.h"
|
2008-10-30 01:54:18 +01:00
|
|
|
#include "gbx_watch.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include "gbx_stream.h"
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int STREAM_eff_read;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
#if DEBUG_STREAM
|
|
|
|
static unsigned char _tag = 0;
|
|
|
|
static int _nopen = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DEBUG_STREAM
|
|
|
|
void STREAM_exit(void)
|
|
|
|
{
|
|
|
|
if (_nopen)
|
|
|
|
fprintf(stderr, "WARNING: %d streams yet opened\n", _nopen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-04-07 20:39:45 +02:00
|
|
|
#define wait_for_fd_ready_to_read(_fd) WATCH_process(_fd, -1)
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool STREAM_in_archive(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ARCHIVE *arch;
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
{
|
|
|
|
ARCHIVE_get_current(&arch);
|
|
|
|
if (arch->name || EXEC_arch) // || !FILE_exist_real(path)) - Why that test ?
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
static int STREAM_get_readable(STREAM *stream, int *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-03-11 14:29:47 +01:00
|
|
|
int fd;
|
2008-09-22 01:22:07 +02:00
|
|
|
off_t off;
|
|
|
|
off_t end;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
fd = STREAM_handle(stream);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
//_IOCTL:
|
|
|
|
|
2008-03-21 01:55:43 +01:00
|
|
|
#ifdef FIONREAD
|
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
if (!stream->common.no_fionread)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-03-06 04:17:47 +01:00
|
|
|
if (ioctl(fd, FIONREAD, len) >= 0)
|
2008-03-11 14:29:47 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
stream->common.no_fionread = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-03-21 01:55:43 +01:00
|
|
|
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
//_LSEEK:
|
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
if (!stream->common.no_lseek)
|
|
|
|
{
|
|
|
|
off = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (off >= 0)
|
|
|
|
{
|
|
|
|
end = lseek(fd, 0, SEEK_END);
|
|
|
|
if (end >= 0)
|
|
|
|
{
|
|
|
|
*len = (int)(end - off);
|
|
|
|
|
|
|
|
off = lseek(fd, off, SEEK_SET);
|
|
|
|
if (off >= 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->common.no_lseek = TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
//fprintf(stderr, "STREAM_get_readable: lseek: %d\n", *len);
|
2008-03-11 14:29:47 +01:00
|
|
|
return (-1);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-27 15:00:04 +01:00
|
|
|
static int default_eof(STREAM *stream)
|
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
int fd;
|
|
|
|
int ilen;
|
2008-01-27 15:00:04 +01:00
|
|
|
|
|
|
|
fd = STREAM_handle(stream);
|
2010-05-10 13:20:47 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return TRUE;
|
|
|
|
|
2010-05-14 23:17:04 +02:00
|
|
|
if (STREAM_is_blocking(stream) && !stream->common.available_now)
|
2010-05-08 22:17:29 +02:00
|
|
|
wait_for_fd_ready_to_read(STREAM_handle(stream));
|
|
|
|
|
2010-05-10 13:20:47 +02:00
|
|
|
if (STREAM_get_readable(stream, &ilen))
|
2008-09-22 01:22:07 +02:00
|
|
|
return TRUE;
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
return (ilen == 0);
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-02-05 15:53:16 +01:00
|
|
|
// STREAM_open *MUST* initialize completely the stream structure
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_open(STREAM *stream, const char *path, int mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (mode & ST_PIPE)
|
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
stream->type = &STREAM_pipe;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2009-09-17 22:58:27 +02:00
|
|
|
else if (mode & ST_MEMORY)
|
|
|
|
{
|
|
|
|
stream->type = &STREAM_memory;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
|
|
|
{
|
2009-09-17 22:58:27 +02:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
{
|
|
|
|
ARCHIVE *arch = NULL;
|
|
|
|
|
|
|
|
if (strncmp(path, "../", 3))
|
|
|
|
ARCHIVE_get_current(&arch);
|
|
|
|
else if (!EXEC_arch)
|
|
|
|
path += 3;
|
|
|
|
|
|
|
|
if ((arch && arch->name) || EXEC_arch) // || !FILE_exist_real(path)) - Why that test ?
|
|
|
|
{
|
|
|
|
stream->type = &STREAM_arch;
|
|
|
|
goto _OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((mode & ST_ACCESS) != ST_READ || mode & ST_PIPE)
|
|
|
|
THROW(E_ACCESS);
|
|
|
|
}
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (mode & ST_DIRECT)
|
|
|
|
stream->type = &STREAM_direct;
|
|
|
|
else
|
|
|
|
stream->type = &STREAM_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_OPEN:
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
stream->common.mode = mode;
|
|
|
|
stream->common.swap = FALSE;
|
|
|
|
stream->common.eol = 0;
|
2010-03-31 01:50:34 +02:00
|
|
|
stream->common.eof = FALSE;
|
2008-09-22 01:22:07 +02:00
|
|
|
stream->common.buffer = NULL;
|
|
|
|
stream->common.buffer_pos = 0;
|
|
|
|
stream->common.buffer_len = 0;
|
|
|
|
stream->common.no_fionread = FALSE;
|
|
|
|
stream->common.no_lseek = FALSE;
|
2009-02-05 15:53:16 +01:00
|
|
|
stream->common.standard = FALSE;
|
2010-03-31 01:50:34 +02:00
|
|
|
stream->common.blocking = TRUE;
|
2010-05-08 22:17:29 +02:00
|
|
|
stream->common.available_now = FALSE;
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
if ((*(stream->type->open))(stream, path, mode, NULL))
|
|
|
|
{
|
|
|
|
stream->type = NULL;
|
|
|
|
THROW_SYSTEM(errno, path);
|
|
|
|
}
|
2008-12-31 16:55:40 +01:00
|
|
|
|
|
|
|
#if DEBUG_STREAM
|
2009-02-04 23:51:20 +01:00
|
|
|
_tag++;
|
2008-12-31 16:55:40 +01:00
|
|
|
stream->common.tag = _tag;
|
|
|
|
_nopen++;
|
2009-02-04 23:51:20 +01:00
|
|
|
fprintf(stderr, "Open %p [%d] (%d)\n", stream, _tag, _nopen);
|
2008-12-31 16:55:40 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-27 15:00:04 +01:00
|
|
|
void STREAM_release(STREAM *stream)
|
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.buffer)
|
|
|
|
{
|
2009-02-04 23:51:20 +01:00
|
|
|
#if DEBUG_STREAM
|
|
|
|
fprintf(stderr, "Stream %p [%d]: Free buffer\n", stream, stream->common.tag);
|
|
|
|
#endif
|
2008-09-22 01:22:07 +02:00
|
|
|
FREE(&stream->common.buffer, "STREAM_close");
|
|
|
|
stream->common.buffer_pos = 0;
|
|
|
|
stream->common.buffer_len = 0;
|
|
|
|
}
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_close(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
int fd;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-02-04 23:51:20 +01:00
|
|
|
STREAM_release(stream);
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
fd = STREAM_handle(stream);
|
|
|
|
if (fd >= 0)
|
|
|
|
GB_Watch(fd, GB_WATCH_NONE, NULL, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-02-04 23:51:20 +01:00
|
|
|
if (stream->common.standard || !(*(stream->type->close))(stream))
|
2008-09-22 01:22:07 +02:00
|
|
|
stream->type = NULL;
|
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
#if DEBUG_STREAM
|
|
|
|
_nopen--;
|
2009-02-04 23:51:20 +01:00
|
|
|
fprintf(stderr, "Close %p [%d] (%d)\n", stream, stream->common.tag, _nopen);
|
2008-12-31 16:55:40 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_flush(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
|
|
|
(*(stream->type->flush))(stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-27 15:00:04 +01:00
|
|
|
static void read_buffer(STREAM *stream, void **addr, int *len)
|
|
|
|
{
|
|
|
|
int l = stream->common.buffer_len - stream->common.buffer_pos;
|
|
|
|
if (l > *len)
|
|
|
|
l = *len;
|
|
|
|
if (l > 0)
|
|
|
|
{
|
|
|
|
memcpy(*addr, stream->common.buffer + stream->common.buffer_pos, l);
|
|
|
|
*addr = (char *)*addr + l;
|
|
|
|
*len -= l;
|
2008-10-21 15:03:03 +02:00
|
|
|
STREAM_eff_read += l;
|
2008-01-27 15:00:04 +01:00
|
|
|
stream->common.buffer_pos += l;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_read(STREAM *stream, void *addr, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_eff_read = 0;
|
|
|
|
|
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
|
|
|
if (stream->common.buffer)
|
|
|
|
read_buffer(stream, &addr, &len);
|
|
|
|
|
2008-11-24 13:09:40 +01:00
|
|
|
while ((*(stream->type->read))(stream, addr, len))
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
|
|
|
switch(errno)
|
|
|
|
{
|
2008-11-24 13:09:40 +01:00
|
|
|
case EINTR:
|
|
|
|
break;
|
2008-09-22 01:22:07 +02:00
|
|
|
case 0:
|
|
|
|
case EAGAIN:
|
|
|
|
THROW(E_EOF);
|
|
|
|
case EIO:
|
|
|
|
THROW(E_READ);
|
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
char STREAM_getchar(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
char c = 0;
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
|
|
|
if (stream->common.buffer && stream->common.buffer_pos < stream->common.buffer_len)
|
|
|
|
return stream->common.buffer[stream->common.buffer_pos++];
|
|
|
|
|
2008-11-24 13:09:40 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (stream->type->getchar)
|
|
|
|
ret = (*(stream->type->getchar))(stream, &c);
|
|
|
|
else
|
|
|
|
ret = (*(stream->type->read))(stream, &c, 1);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
break;
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
switch(errno)
|
|
|
|
{
|
2008-11-24 13:09:40 +01:00
|
|
|
case EINTR:
|
|
|
|
continue;
|
2008-09-22 01:22:07 +02:00
|
|
|
case 0:
|
|
|
|
case EAGAIN:
|
|
|
|
THROW(E_EOF);
|
|
|
|
case EIO:
|
|
|
|
THROW(E_READ);
|
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-05 19:02:16 +02:00
|
|
|
int STREAM_read_max(STREAM *stream, void *addr, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-09 13:03:47 +02:00
|
|
|
bool err;
|
2008-09-22 01:22:07 +02:00
|
|
|
int flags, handle;
|
2010-04-06 19:00:25 +02:00
|
|
|
int save_errno;
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
2010-04-05 19:02:16 +02:00
|
|
|
STREAM_eff_read = 0;
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.buffer)
|
|
|
|
read_buffer(stream, &addr, &len);
|
|
|
|
|
2008-10-21 15:03:03 +02:00
|
|
|
if (len > 0)
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
2008-11-24 13:09:40 +01:00
|
|
|
for(;;)
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
2008-11-24 13:09:40 +01:00
|
|
|
if (stream->common.available_now)
|
|
|
|
{
|
|
|
|
err = (*(stream->type->read))(stream, addr, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handle = STREAM_handle(stream);
|
|
|
|
flags = fcntl(handle, F_GETFL);
|
2010-04-06 19:00:25 +02:00
|
|
|
if ((flags & O_NONBLOCK) == 0)
|
|
|
|
{
|
2010-04-07 20:39:45 +02:00
|
|
|
wait_for_fd_ready_to_read(handle);
|
2010-04-06 19:00:25 +02:00
|
|
|
fcntl(handle, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
}
|
2008-11-24 13:09:40 +01:00
|
|
|
|
2010-04-06 19:00:25 +02:00
|
|
|
errno = 0;
|
2008-11-24 13:09:40 +01:00
|
|
|
err = (*(stream->type->read))(stream, addr, len);
|
2010-04-06 19:00:25 +02:00
|
|
|
save_errno = errno;
|
2008-11-24 13:09:40 +01:00
|
|
|
|
2010-04-06 19:00:25 +02:00
|
|
|
if ((flags & O_NONBLOCK) == 0)
|
|
|
|
fcntl(handle, F_SETFL, flags);
|
|
|
|
|
|
|
|
errno = save_errno;
|
2008-11-24 13:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!err)
|
|
|
|
break;
|
|
|
|
|
2008-10-21 15:03:03 +02:00
|
|
|
switch(errno)
|
|
|
|
{
|
2008-11-24 13:09:40 +01:00
|
|
|
case EINTR:
|
|
|
|
continue;
|
2008-10-21 15:03:03 +02:00
|
|
|
case 0:
|
|
|
|
case EAGAIN:
|
2010-04-06 19:00:25 +02:00
|
|
|
return STREAM_eff_read;
|
2008-10-21 15:03:03 +02:00
|
|
|
case EIO:
|
2010-04-05 19:02:16 +02:00
|
|
|
return -1; //THROW(E_READ);
|
2008-10-21 15:03:03 +02:00
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-05 19:02:16 +02:00
|
|
|
|
|
|
|
return STREAM_eff_read;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_write(STREAM *stream, void *addr, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
2008-11-24 13:09:40 +01:00
|
|
|
while ((*(stream->type->write))(stream, addr, len))
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
|
|
|
switch(errno)
|
|
|
|
{
|
2008-11-24 13:09:40 +01:00
|
|
|
case EINTR:
|
|
|
|
break;
|
2008-09-22 01:22:07 +02:00
|
|
|
case EIO:
|
|
|
|
THROW(E_WRITE);
|
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_write_eol(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
switch(stream->common.eol)
|
|
|
|
{
|
|
|
|
case ST_EOL_UNIX: STREAM_write(stream, "\n", 1); break;
|
|
|
|
case ST_EOL_WINDOWS: STREAM_write(stream, "\r\n", 2); break;
|
|
|
|
case ST_EOL_MAC: STREAM_write(stream, "\r", 1); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int64_t STREAM_tell(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
int64_t pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
|
|
|
if (stream->type->tell(stream, &pos))
|
|
|
|
/*THROW(E_SEEK, ERROR_get());*/
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
return pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_seek(STREAM *stream, int64_t pos, int whence)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
|
|
|
if (stream->type->seek(stream, pos, whence))
|
|
|
|
{
|
|
|
|
switch(errno)
|
|
|
|
{
|
|
|
|
case EINVAL:
|
|
|
|
THROW(E_ARG);
|
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
static void fill_buffer(STREAM *stream, char *addr)
|
|
|
|
{
|
|
|
|
bool err;
|
|
|
|
int flags, fd;
|
|
|
|
|
2008-10-30 01:54:18 +01:00
|
|
|
STREAM_eff_read = 0;
|
2008-10-29 19:26:56 +01:00
|
|
|
fd = STREAM_handle(stream);
|
|
|
|
|
2008-11-24 13:09:40 +01:00
|
|
|
for(;;)
|
2008-10-29 19:26:56 +01:00
|
|
|
{
|
2008-11-24 13:09:40 +01:00
|
|
|
if (stream->common.available_now)
|
|
|
|
err = (*(stream->type->read))(stream, addr, STREAM_BUFFER_SIZE);
|
|
|
|
else
|
|
|
|
{
|
2010-04-07 20:39:45 +02:00
|
|
|
wait_for_fd_ready_to_read(fd);
|
|
|
|
|
2008-11-24 13:09:40 +01:00
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
if ((flags & O_NONBLOCK) == 0)
|
|
|
|
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
|
|
|
err = (*(stream->type->read))(stream, addr, STREAM_BUFFER_SIZE);
|
|
|
|
|
|
|
|
if ((flags & O_NONBLOCK) == 0)
|
|
|
|
fcntl(fd, F_SETFL, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!err)
|
|
|
|
break;
|
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
switch(errno)
|
|
|
|
{
|
2008-11-24 13:09:40 +01:00
|
|
|
case EINTR:
|
|
|
|
continue;
|
2008-10-29 19:26:56 +01:00
|
|
|
case 0:
|
|
|
|
case EAGAIN:
|
2008-10-30 01:54:18 +01:00
|
|
|
case EINPROGRESS:
|
2008-11-24 13:09:40 +01:00
|
|
|
return;
|
2008-10-29 19:26:56 +01:00
|
|
|
case EIO:
|
2008-11-24 13:09:40 +01:00
|
|
|
return; //THROW(E_READ);
|
2008-10-29 19:26:56 +01:00
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-09 01:08:04 +02:00
|
|
|
static char *input(STREAM *stream, bool line)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-09 13:03:47 +02:00
|
|
|
int len = 0;
|
|
|
|
int start;
|
|
|
|
unsigned char c, lc = 0;
|
|
|
|
void *test;
|
|
|
|
bool eol;
|
|
|
|
char *buffer;
|
2010-06-09 01:08:04 +02:00
|
|
|
int buffer_len;
|
|
|
|
int buffer_pos;
|
|
|
|
char *addr;
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2010-06-09 01:08:04 +02:00
|
|
|
addr = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-09 13:03:47 +02:00
|
|
|
stream->common.eof = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!line)
|
|
|
|
test = &&__TEST_INPUT;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch(stream->common.eol)
|
|
|
|
{
|
|
|
|
case 1: test = &&__TEST_WINDOWS; break;
|
|
|
|
case 2: test = &&__TEST_MAC; break;
|
|
|
|
default: test = &&__TEST_UNIX; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
if (!STREAM_is_blocking(stream) && STREAM_eof(stream))
|
2008-10-30 01:54:18 +01:00
|
|
|
{
|
|
|
|
//printf("EOF!\n"); fflush(stdout);
|
2008-01-27 15:00:04 +01:00
|
|
|
THROW(E_EOF);
|
2008-10-30 01:54:18 +01:00
|
|
|
}
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
buffer = stream->common.buffer;
|
|
|
|
buffer_len = stream->common.buffer_len;
|
|
|
|
buffer_pos = stream->common.buffer_pos;
|
|
|
|
|
|
|
|
if (!buffer)
|
2008-01-27 15:00:04 +01:00
|
|
|
{
|
2009-02-04 23:51:20 +01:00
|
|
|
#if DEBUG_STREAM
|
|
|
|
fprintf(stderr, "Stream %p [%d]: Alloc buffer\n", stream, stream->common.tag);
|
|
|
|
#endif
|
2008-03-11 14:29:47 +01:00
|
|
|
ALLOC(&buffer, STREAM_BUFFER_SIZE, "input");
|
|
|
|
buffer_pos = 0;
|
|
|
|
buffer_len = 0;
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*eof_func = stream->type->eof;
|
|
|
|
if (!eof_func)
|
|
|
|
eof_func = default_eof;*/
|
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
start = buffer_pos;
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2008-09-09 13:03:47 +02:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (buffer_pos == buffer_len)
|
|
|
|
{
|
2008-01-27 15:00:04 +01:00
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
//add_string(addr, &len_str, stream->common.buffer + start, len);
|
2010-06-09 01:08:04 +02:00
|
|
|
STRING_add(&addr, buffer + start, len);
|
2008-01-27 15:00:04 +01:00
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
stream->common.buffer = buffer;
|
|
|
|
stream->common.buffer_pos = buffer_pos;
|
|
|
|
stream->common.buffer_len = buffer_len;
|
2008-09-09 13:03:47 +02:00
|
|
|
|
2008-10-30 01:54:18 +01:00
|
|
|
//printf("fill_buffer: (is_file = %d)\n", stream->common.is_file); fflush(stdout);
|
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
fill_buffer(stream, buffer);
|
|
|
|
//STREAM_read_max(stream, buffer, STREAM_BUFFER_SIZE);
|
2008-09-09 13:03:47 +02:00
|
|
|
|
|
|
|
buffer_pos = 0;
|
|
|
|
buffer_len = STREAM_eff_read;
|
|
|
|
|
2008-10-30 01:54:18 +01:00
|
|
|
#if 0
|
|
|
|
printf("-> %d: ", buffer_len);
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < buffer_len; i++)
|
|
|
|
{
|
|
|
|
printf("%02X ", buffer[i]);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-09 13:03:47 +02:00
|
|
|
if (!buffer_len)
|
|
|
|
{
|
2010-05-08 22:17:29 +02:00
|
|
|
//if (stream->common.available_now)
|
|
|
|
//{
|
2008-10-30 01:54:18 +01:00
|
|
|
stream->common.eof = TRUE;
|
|
|
|
break;
|
2010-05-08 22:17:29 +02:00
|
|
|
//}
|
2008-09-09 13:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = buffer[buffer_pos++]; //STREAM_getchar(stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
goto *test;
|
|
|
|
|
|
|
|
__TEST_INPUT:
|
|
|
|
|
|
|
|
eol = (c <= ' ');
|
|
|
|
goto __TEST_OK;
|
|
|
|
|
|
|
|
__TEST_UNIX:
|
|
|
|
|
|
|
|
eol = (c == '\n');
|
|
|
|
if (lc == '\r')
|
|
|
|
len--;
|
|
|
|
lc = c;
|
|
|
|
goto __TEST_OK;
|
|
|
|
|
|
|
|
__TEST_MAC:
|
|
|
|
|
|
|
|
eol = (c == '\r');
|
|
|
|
goto __TEST_OK;
|
|
|
|
|
|
|
|
__TEST_WINDOWS:
|
|
|
|
|
|
|
|
eol = (lc == '\r') && (c == '\n');
|
|
|
|
if (eol)
|
|
|
|
len--;
|
|
|
|
lc = c;
|
|
|
|
goto __TEST_OK;
|
|
|
|
|
|
|
|
__TEST_OK:
|
|
|
|
|
|
|
|
if (eol)
|
|
|
|
break;
|
|
|
|
|
2008-09-09 13:03:47 +02:00
|
|
|
len++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-09 13:03:47 +02:00
|
|
|
if (len > 0)
|
|
|
|
//add_string(addr, &len_str, stream->common.buffer + start, len);
|
2010-06-09 01:08:04 +02:00
|
|
|
STRING_add(&addr, buffer + start, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-09 01:08:04 +02:00
|
|
|
STRING_extend_end(&addr);
|
2008-09-09 13:03:47 +02:00
|
|
|
|
|
|
|
stream->common.buffer = buffer;
|
|
|
|
stream->common.buffer_pos = buffer_pos;
|
|
|
|
stream->common.buffer_len = buffer_len;
|
2010-06-09 01:08:04 +02:00
|
|
|
|
|
|
|
return addr;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-09 01:08:04 +02:00
|
|
|
char *STREAM_line_input(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-06-09 01:08:04 +02:00
|
|
|
return input(stream, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-09 01:08:04 +02:00
|
|
|
char *STREAM_input(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-06-09 01:08:04 +02:00
|
|
|
return input(stream, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_read_type(STREAM *stream, TYPE type, VALUE *value, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-12-07 18:14:32 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char _byte;
|
|
|
|
short _short;
|
|
|
|
int _int;
|
|
|
|
float _single;
|
|
|
|
unsigned char _data[4];
|
|
|
|
}
|
|
|
|
buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (type == T_VARIANT)
|
|
|
|
{
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._byte, 1);
|
|
|
|
type = buffer._byte;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
value->type = type;
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case T_BOOLEAN:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._byte, 1);
|
|
|
|
value->_integer.value = (buffer._byte != 0) ? (-1) : 0;
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_BYTE:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._byte, 1);
|
|
|
|
value->_integer.value = buffer._byte;
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_SHORT:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._short, sizeof(short));
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_short(&buffer._short);
|
|
|
|
value->_integer.value = buffer._short;
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_INTEGER:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_read(stream, &value->_integer.value, sizeof(int));
|
|
|
|
if (stream->common.swap)
|
|
|
|
SWAP_int(&value->_integer.value);
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_LONG:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_read(stream, &value->_long.value, sizeof(int64_t));
|
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_int64(&value->_long.value);
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_SINGLE:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._single, sizeof(float));
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_float(&buffer._single);
|
|
|
|
value->_float.value = buffer._single;
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_FLOAT:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_read(stream, &value->_float.value, sizeof(double));
|
|
|
|
if (stream->common.swap)
|
|
|
|
SWAP_double(&value->_float.value);
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_DATE:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_read(stream, &value->_date.date, sizeof(int));
|
|
|
|
STREAM_read(stream, &value->_date.time, sizeof(int));
|
|
|
|
if (stream->common.swap)
|
|
|
|
{
|
|
|
|
SWAP_int(&value->_date.date);
|
|
|
|
SWAP_int(&value->_date.time);
|
|
|
|
}
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_CSTRING:
|
2007-12-30 17:41:49 +01:00
|
|
|
value->type = T_STRING;
|
|
|
|
// continue
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
if (stream->type == &STREAM_memory)
|
|
|
|
{
|
|
|
|
size_t slen;
|
|
|
|
if (CHECK_strlen(stream->memory.addr + stream->memory.pos, &slen))
|
|
|
|
THROW(E_READ);
|
|
|
|
len = (int)slen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, buffer._data, 1);
|
2008-09-22 01:22:07 +02:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
switch (buffer._data[0] >> 6)
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 1:
|
2009-12-07 18:14:32 +01:00
|
|
|
len = buffer._data[0];
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._data[1], 1);
|
|
|
|
buffer._data[0] &= 0x3F;
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
if (!EXEC_big_endian)
|
2009-12-24 03:02:05 +01:00
|
|
|
SWAP_short(&buffer._short);
|
2008-09-22 01:22:07 +02:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
len = buffer._short;
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._data[1], 3);
|
|
|
|
buffer._data[0] &= 0x3F;
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
if (!EXEC_big_endian)
|
2009-12-24 03:02:05 +01:00
|
|
|
SWAP_int(&buffer._int);
|
2008-09-22 01:22:07 +02:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
len = buffer._int;
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
STRING_new_temp_value(value, NULL, labs(len));
|
|
|
|
STREAM_read(stream, value->_string.addr, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = (-len);
|
|
|
|
|
|
|
|
value->type = T_STRING;
|
2010-06-05 01:48:53 +02:00
|
|
|
value->_string.addr = STRING_new(NULL, len);
|
2008-09-22 01:22:07 +02:00
|
|
|
value->_string.start = 0;
|
|
|
|
value->_string.len = len;
|
|
|
|
|
|
|
|
STREAM_read_max(stream, value->_string.addr, len);
|
|
|
|
|
2008-05-03 17:07:55 +02:00
|
|
|
if (STREAM_eff_read == 0)
|
|
|
|
{
|
|
|
|
value->type = T_NULL;
|
|
|
|
STRING_free(&value->_string.addr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (STREAM_eff_read < len)
|
|
|
|
{
|
2008-05-03 17:07:55 +02:00
|
|
|
STRING_extend(&value->_string.addr, STREAM_eff_read);
|
|
|
|
value->_string.len = STREAM_eff_read;
|
|
|
|
}
|
|
|
|
STRING_extend_end(&value->_string.addr);
|
|
|
|
}
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
default:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_TYPE, "Standard type", TYPE_get_name(type));
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_write_type(STREAM *stream, TYPE type, VALUE *value, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-12-07 18:14:32 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char _byte;
|
|
|
|
short _short;
|
|
|
|
int _int;
|
|
|
|
int64_t _long;
|
|
|
|
float _single;
|
|
|
|
double _float;
|
|
|
|
unsigned char _data[8];
|
|
|
|
}
|
|
|
|
buffer;
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
bool write_zero;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (type == T_VARIANT)
|
|
|
|
{
|
|
|
|
VARIANT_undo(value);
|
|
|
|
type = value->type;
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._byte = (unsigned char)type;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
//value->type = type;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case T_BOOLEAN:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._byte = value->_integer.value ? 0xFF : 0;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BYTE:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._byte = (unsigned char)value->_integer.value;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_SHORT:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._short = (short)value->_integer.value;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_short(&buffer._short);
|
|
|
|
STREAM_write(stream, &buffer._short, sizeof(short));
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_INTEGER:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._int = value->_integer.value;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_int(&buffer._int);
|
|
|
|
STREAM_write(stream, &buffer._int, sizeof(int));
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._long = value->_long.value;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_int64(&buffer._long);
|
|
|
|
STREAM_write(stream, &buffer._long, sizeof(int64_t));
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_SINGLE:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._single = (float)value->_float.value;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_float(&buffer._single);
|
|
|
|
STREAM_write(stream, &buffer._single, sizeof(float));
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._float = value->_float.value;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_double(&buffer._float);
|
|
|
|
STREAM_write(stream, &buffer._float, sizeof(double));
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DATE:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._int = value->_date.date;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_int(&buffer._int);
|
|
|
|
STREAM_write(stream, &buffer._int, sizeof(int));
|
2008-09-22 01:22:07 +02:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._int = value->_date.time;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_int(&buffer._int);
|
|
|
|
STREAM_write(stream, &buffer._int, sizeof(int));
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
case T_CSTRING:
|
|
|
|
|
|
|
|
write_zero = (stream->type == &STREAM_memory) && len == 0;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
len = value->_string.len;
|
|
|
|
|
|
|
|
if (stream->type != &STREAM_memory)
|
|
|
|
{
|
2009-12-07 18:14:32 +01:00
|
|
|
if (len < 0x80)
|
|
|
|
{
|
|
|
|
buffer._byte = (unsigned char)len;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
|
|
|
}
|
|
|
|
else if (len < 0x4000)
|
|
|
|
{
|
|
|
|
buffer._short = (short)len | 0x8000;
|
|
|
|
if (!EXEC_big_endian)
|
|
|
|
SWAP_short(&buffer._short);
|
|
|
|
STREAM_write(stream, &buffer._short, sizeof(short));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer._int = len | 0xC0000000;
|
|
|
|
if (!EXEC_big_endian)
|
|
|
|
SWAP_int(&buffer._int);
|
|
|
|
STREAM_write(stream, &buffer._int, sizeof(int));
|
|
|
|
}
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM_write(stream, value->_string.addr + value->_string.start, Min(len, value->_string.len));
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._byte = 0;
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
while (len > value->_string.len)
|
|
|
|
{
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2008-09-22 01:22:07 +02:00
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write_zero)
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
case T_NULL:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._byte = 0;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2007-12-30 17:41:49 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/*case T_OBJECT:
|
|
|
|
|
|
|
|
class = OBJECT_class(value->_object.object);
|
|
|
|
if (class == CLASS_Array || CLASS_inherits(class, CLASS_Array))
|
|
|
|
{
|
|
|
|
CARRAY_stream_write((CARRAY *)value->_object.object, stream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (class == CLASS_Collection || CLASS_inherits(class, CLASS_Collection))
|
|
|
|
{
|
|
|
|
CCOLLECTION_stream_write((CCOLLECTION *)value->_object.object, stream);
|
|
|
|
break;
|
|
|
|
}*/
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
default:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_TYPE, "Standard type", TYPE_get_name(type));
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_load(const char *path, char **buffer, int *rlen)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM stream;
|
|
|
|
int64_t len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_open(&stream, path, ST_READ);
|
|
|
|
STREAM_lof(&stream, &len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (len >> 31)
|
|
|
|
THROW(E_MEMORY);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
*rlen = len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
ALLOC(buffer, *rlen, "STREAM_load");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_read(&stream, *buffer, *rlen);
|
|
|
|
STREAM_close(&stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
bool STREAM_map(const char *path, char **paddr, int *plen)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM stream;
|
2008-12-28 23:41:55 +01:00
|
|
|
int fd;
|
|
|
|
struct stat info;
|
|
|
|
void *addr;
|
|
|
|
size_t len;
|
2008-12-31 16:55:40 +01:00
|
|
|
bool ret = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
STREAM_open(&stream, path, ST_READ + ST_DIRECT);
|
|
|
|
|
|
|
|
if (stream.type == &STREAM_arch)
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
2008-12-28 23:41:55 +01:00
|
|
|
*paddr = (char *)stream.arch.arch->arch->addr + stream.arch.start;
|
|
|
|
*plen = stream.arch.size;
|
2008-12-31 16:55:40 +01:00
|
|
|
ret = FALSE;
|
|
|
|
goto __RETURN;
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
2008-12-28 23:41:55 +01:00
|
|
|
|
|
|
|
fd = STREAM_handle(&stream);
|
|
|
|
if (fd < 0)
|
2008-12-31 16:55:40 +01:00
|
|
|
goto __RETURN;
|
2008-12-28 23:41:55 +01:00
|
|
|
|
|
|
|
if (fstat(fd, &info) < 0)
|
2008-12-31 16:55:40 +01:00
|
|
|
goto __RETURN;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
len = info.st_size;
|
|
|
|
addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if (addr == MAP_FAILED)
|
2008-12-31 16:55:40 +01:00
|
|
|
goto __RETURN;
|
2008-12-28 23:41:55 +01:00
|
|
|
|
|
|
|
*paddr = addr;
|
|
|
|
*plen = len;
|
2008-12-31 16:55:40 +01:00
|
|
|
ret = FALSE;
|
|
|
|
|
|
|
|
__RETURN:
|
|
|
|
|
|
|
|
STREAM_close(&stream);
|
|
|
|
return ret;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
void STREAM_unmap(char *addr, int len)
|
|
|
|
{
|
|
|
|
if (addr && len > 0 && ARCHIVE_check_addr(addr))
|
2008-12-31 16:55:40 +01:00
|
|
|
{
|
2008-12-28 23:41:55 +01:00
|
|
|
munmap(addr, len);
|
2008-12-31 16:55:40 +01:00
|
|
|
}
|
2008-12-28 23:41:55 +01:00
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int STREAM_handle(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
|
|
|
if (stream->type->handle)
|
|
|
|
return (*stream->type->handle)(stream);
|
|
|
|
else
|
|
|
|
return (-1);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_lock(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
int64_t pos;
|
|
|
|
int fd;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
fd = STREAM_handle(stream);
|
|
|
|
if (fd < 0)
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_LOCK);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (pos < 0)
|
|
|
|
goto __ERROR;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (lseek(fd, 0, SEEK_SET) < 0)
|
|
|
|
goto __ERROR;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-03-21 02:04:18 +01:00
|
|
|
#ifdef F_TLOCK
|
|
|
|
|
|
|
|
if (lockf(fd, F_TLOCK, 0))
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
THROW(E_LOCK);
|
|
|
|
else
|
|
|
|
goto __ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
fprintf(stderr, "locking is not implemented\n");
|
|
|
|
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (lseek(fd, pos, SEEK_SET) < 0)
|
|
|
|
goto __ERROR;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
__ERROR:
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW_SYSTEM(errno, NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_lof(STREAM *stream, int64_t *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
int fd;
|
|
|
|
int ilen;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (stream->type->lof)
|
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!(*(stream->type->lof))(stream, len))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = STREAM_handle(stream);
|
2008-03-11 14:29:47 +01:00
|
|
|
if ((fd >= 0) && (STREAM_get_readable(stream, &ilen) == 0))
|
2007-12-30 17:41:49 +01:00
|
|
|
*len = ilen;
|
2008-01-27 15:00:04 +01:00
|
|
|
|
|
|
|
if (stream->common.buffer)
|
|
|
|
*len += stream->common.buffer_len - stream->common.buffer_pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool STREAM_eof(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
2008-01-27 15:00:04 +01:00
|
|
|
if (stream->common.buffer && stream->common.buffer_pos < stream->common.buffer_len)
|
|
|
|
return FALSE;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (stream->type->eof)
|
2008-09-22 01:22:07 +02:00
|
|
|
return ((*(stream->type->eof))(stream));
|
|
|
|
else
|
|
|
|
return default_eof(stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int STREAM_read_direct(int fd, char *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
ssize_t eff_read;
|
|
|
|
ssize_t len_read;
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
len_read = Min(len, MAX_IO);
|
|
|
|
eff_read = read(fd, buffer, len_read);
|
|
|
|
|
|
|
|
if (eff_read > 0)
|
|
|
|
{
|
|
|
|
STREAM_eff_read += eff_read;
|
|
|
|
len -= eff_read;
|
|
|
|
buffer += eff_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eff_read < len_read)
|
|
|
|
{
|
|
|
|
if (eff_read == 0)
|
|
|
|
errno = 0;
|
|
|
|
if (eff_read <= 0 && errno != EINTR)
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int STREAM_write_direct(int fd, char *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
ssize_t eff_write;
|
|
|
|
ssize_t len_write;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
len_write = Min(len, MAX_IO);
|
|
|
|
eff_write = write(fd, buffer, len_write);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (eff_write < len_write)
|
|
|
|
{
|
|
|
|
if (eff_write <= 0 && errno != EINTR)
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
len -= eff_write;
|
|
|
|
buffer += eff_write;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
void STREAM_blocking(STREAM *stream, bool block)
|
|
|
|
{
|
|
|
|
int fd = STREAM_handle(stream);
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
|
2010-02-27 23:08:04 +01:00
|
|
|
stream->common.blocking = block;
|
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
if (block)
|
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
|
|
|
|
else
|
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-07 20:39:45 +02:00
|
|
|
void STREAM_check_blocking(STREAM *stream)
|
2008-10-29 19:26:56 +01:00
|
|
|
{
|
2010-04-07 20:39:45 +02:00
|
|
|
int fd = STREAM_handle(stream);
|
2008-10-29 19:26:56 +01:00
|
|
|
|
2010-04-07 20:39:45 +02:00
|
|
|
stream->common.blocking = (fd < 0) ? TRUE : ((fcntl(fd, F_GETFL) & O_NONBLOCK) == 0);
|
2008-10-29 19:26:56 +01:00
|
|
|
}
|