added by_name functions that can read any file size
This commit is contained in:
parent
d0c70a89b2
commit
30492c0988
@ -32,8 +32,8 @@
|
||||
/* returns error
|
||||
returns 0 if everything is ok
|
||||
returns 1 if problem reading file */
|
||||
int APP_CC
|
||||
file_read_sections(int fd, struct list* names)
|
||||
static int APP_CC
|
||||
l_file_read_sections(int fd, int max_file_size, struct list* names)
|
||||
{
|
||||
struct stream* s;
|
||||
char text[256];
|
||||
@ -51,8 +51,8 @@ file_read_sections(int fd, struct list* names)
|
||||
g_memset(text, 0, 256);
|
||||
list_clear(names);
|
||||
make_stream(s);
|
||||
init_stream(s, 8192);
|
||||
len = g_file_read(fd, s->data, 8192);
|
||||
init_stream(s, max_file_size);
|
||||
len = g_file_read(fd, s->data, max_file_size);
|
||||
if (len > 0)
|
||||
{
|
||||
s->end = s->p + len;
|
||||
@ -154,6 +154,7 @@ file_read_line(struct stream* s, char* text)
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* returns error */
|
||||
static int APP_CC
|
||||
file_split_name_value(char* text, char* name, char* value)
|
||||
{
|
||||
@ -195,9 +196,9 @@ file_split_name_value(char* text, char* name, char* value)
|
||||
|
||||
/*****************************************************************************/
|
||||
/* return error */
|
||||
int APP_CC
|
||||
file_read_section(int fd, const char* section, struct list* names,
|
||||
struct list* values)
|
||||
static int APP_CC
|
||||
l_file_read_section(int fd, int max_file_size, const char* section,
|
||||
struct list* names, struct list* values)
|
||||
{
|
||||
struct stream* s;
|
||||
char text[512];
|
||||
@ -208,7 +209,9 @@ file_read_section(int fd, const char* section, struct list* names,
|
||||
int in_it_index;
|
||||
int len;
|
||||
int index;
|
||||
int file_size;
|
||||
|
||||
file_size = 32 * 1024; /* 32 K file size limit */
|
||||
g_file_seek(fd, 0);
|
||||
in_it_index = 0;
|
||||
in_it = 0;
|
||||
@ -216,8 +219,8 @@ file_read_section(int fd, const char* section, struct list* names,
|
||||
list_clear(names);
|
||||
list_clear(values);
|
||||
make_stream(s);
|
||||
init_stream(s, 8192);
|
||||
len = g_file_read(fd, s->data, 8192);
|
||||
init_stream(s, file_size);
|
||||
len = g_file_read(fd, s->data, file_size);
|
||||
if (len > 0)
|
||||
{
|
||||
s->end = s->p + len;
|
||||
@ -259,3 +262,77 @@ file_read_section(int fd, const char* section, struct list* names,
|
||||
free_stream(s);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* returns error
|
||||
returns 0 if everything is ok
|
||||
returns 1 if problem reading file */
|
||||
/* 32 K file size limit */
|
||||
int APP_CC
|
||||
file_read_sections(int fd, struct list* names)
|
||||
{
|
||||
return l_file_read_sections(fd, 32 * 1024, names);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* return error */
|
||||
/* this function should be prefered over file_read_sections because it can
|
||||
read any file size */
|
||||
int APP_CC
|
||||
file_by_name_read_sections(const char* file_name, struct list* names)
|
||||
{
|
||||
int fd;
|
||||
int file_size;
|
||||
int rv;
|
||||
|
||||
file_size = g_file_get_size(file_name);
|
||||
if (file_size < 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
fd = g_file_open(file_name);
|
||||
if (fd < 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
rv = l_file_read_sections(fd, file_size, names);
|
||||
g_file_close(fd);
|
||||
return rv;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* return error */
|
||||
/* 32 K file size limit */
|
||||
int APP_CC
|
||||
file_read_section(int fd, const char* section,
|
||||
struct list* names, struct list* values)
|
||||
{
|
||||
return l_file_read_section(fd, 32 * 1024, section, names, values);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* return error */
|
||||
/* this function should be prefered over file_read_section because it can
|
||||
read any file size */
|
||||
int APP_CC
|
||||
file_by_name_read_section(const char* file_name, const char* section,
|
||||
struct list* names, struct list* values)
|
||||
{
|
||||
int fd;
|
||||
int file_size;
|
||||
int rv;
|
||||
|
||||
file_size = g_file_get_size(file_name);
|
||||
if (file_size < 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
fd = g_file_open(file_name);
|
||||
if (fd < 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
rv = l_file_read_section(fd, file_size, section, names, values);
|
||||
g_file_close(fd);
|
||||
return rv;
|
||||
}
|
||||
|
@ -30,7 +30,12 @@
|
||||
int APP_CC
|
||||
file_read_sections(int fd, struct list* names);
|
||||
int APP_CC
|
||||
file_read_section(int fd, const char* section, struct list* names,
|
||||
struct list* values);
|
||||
file_by_name_read_sections(const char* file_name, struct list* names);
|
||||
int APP_CC
|
||||
file_read_section(int fd, const char* section,
|
||||
struct list* names, struct list* values);
|
||||
int APP_CC
|
||||
file_by_name_read_section(const char* file_name, const char* section,
|
||||
struct list* names, struct list* values);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user