2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
gb_buffer_temp.h
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2022-09-12 15:13:13 +02:00
|
|
|
(c) 2000-2017 Benoît Minisini <benoit.minisini@gambas-basic.org>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-03-18 14:14:17 +01: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
|
|
|
|
2012-03-18 14:14:17 +01: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
|
|
|
|
2012-03-18 14:14:17 +01: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., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __BUFFER_C
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
#include "gb_buffer.h"
|
|
|
|
|
|
|
|
|
2008-01-05 15:07:21 +01:00
|
|
|
void BUFFER_create(void *p_data)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-03-18 14:14:17 +01:00
|
|
|
BUFFER *buf;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(&buf, sizeof(BUFFER));
|
2012-03-18 14:14:17 +01:00
|
|
|
buf->max = 0;
|
|
|
|
buf->length = 0;
|
|
|
|
|
|
|
|
*((void **)p_data) = BUFFER_TO_DATA(buf);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-05 15:07:21 +01:00
|
|
|
void BUFFER_delete(void *p_data)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-03-18 14:14:17 +01:00
|
|
|
void **data = (void **)p_data;
|
|
|
|
BUFFER *buf = DATA_TO_BUFFER(*data);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&buf);
|
2012-03-18 14:14:17 +01:00
|
|
|
*data = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-05 15:07:21 +01:00
|
|
|
bool BUFFER_need(void *p_data, size_t size)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-03-18 14:14:17 +01:00
|
|
|
void **data = (void **)p_data;
|
|
|
|
BUFFER *buffer = DATA_TO_BUFFER(*data);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
buffer->length += size;
|
2010-06-24 03:14:12 +02:00
|
|
|
//fprintf(stderr, "BUFFER_need: %ld (%ld / %ld)\n", size, buffer->length, buffer->max);
|
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
if (buffer->length > buffer->max)
|
|
|
|
{
|
|
|
|
while (buffer->length >= buffer->max)
|
|
|
|
buffer->max += BUFFER_INC;
|
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
REALLOC(&buffer, sizeof(char) * buffer->max + sizeof(BUFFER));
|
2012-03-18 14:14:17 +01:00
|
|
|
*data = BUFFER_TO_DATA(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-19 15:15:24 +02:00
|
|
|
offset_t BUFFER_add(void *p_data, const void *string, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-03-18 14:14:17 +01:00
|
|
|
void **data = (void **)p_data;
|
|
|
|
BUFFER *buffer = DATA_TO_BUFFER(*data);
|
|
|
|
size_t pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-04-19 15:15:24 +02:00
|
|
|
if (len < 0)
|
|
|
|
len = strlen((const char *)string);
|
2010-06-24 03:14:12 +02:00
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
pos = buffer->length;
|
|
|
|
BUFFER_need(p_data, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
memcpy(*data + pos, string, len);
|
2010-06-24 03:14:12 +02:00
|
|
|
|
|
|
|
//fprintf(stderr, ">> BUFFER_add\n");
|
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
return pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
void BUFFER_add_char(void *p_data, char c)
|
|
|
|
{
|
|
|
|
void **data = (void **)p_data;
|
|
|
|
BUFFER *buffer = DATA_TO_BUFFER(*data);
|
|
|
|
size_t pos;
|
|
|
|
|
|
|
|
pos = buffer->length;
|
|
|
|
BUFFER_need(p_data, 1);
|
|
|
|
*(char *)(*data + pos) = c;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-05 15:07:21 +01:00
|
|
|
bool BUFFER_load_file(void *p_data, const char *name)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-03-18 14:14:17 +01:00
|
|
|
void **data;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
int fd;
|
|
|
|
struct stat info;
|
2011-04-16 15:25:46 +02:00
|
|
|
int old_len;
|
|
|
|
int len, lenr;
|
2012-03-18 14:14:17 +01:00
|
|
|
void *p;
|
|
|
|
|
|
|
|
fd = open(name, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (fstat(fd, &info))
|
2013-01-05 00:24:37 +01:00
|
|
|
{
|
|
|
|
close(fd);
|
2011-04-16 15:25:46 +02:00
|
|
|
return TRUE;
|
2013-01-05 00:24:37 +01:00
|
|
|
}
|
2011-04-16 15:25:46 +02:00
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
len = info.st_size;
|
|
|
|
|
|
|
|
data = (void **)p_data;
|
2011-04-16 15:25:46 +02:00
|
|
|
old_len = DATA_TO_BUFFER(*data)->length;
|
|
|
|
|
|
|
|
BUFFER_need(p_data, len);
|
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
data = (void **)p_data;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-04-16 15:25:46 +02:00
|
|
|
p = *data + old_len;
|
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
lenr = read(fd, p, len);
|
|
|
|
if (lenr < 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lenr == len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
p += lenr;
|
|
|
|
len -= lenr;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|