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 error
|
||||||
returns 0 if everything is ok
|
returns 0 if everything is ok
|
||||||
returns 1 if problem reading file */
|
returns 1 if problem reading file */
|
||||||
int APP_CC
|
static int APP_CC
|
||||||
file_read_sections(int fd, struct list* names)
|
l_file_read_sections(int fd, int max_file_size, struct list* names)
|
||||||
{
|
{
|
||||||
struct stream* s;
|
struct stream* s;
|
||||||
char text[256];
|
char text[256];
|
||||||
@ -51,8 +51,8 @@ file_read_sections(int fd, struct list* names)
|
|||||||
g_memset(text, 0, 256);
|
g_memset(text, 0, 256);
|
||||||
list_clear(names);
|
list_clear(names);
|
||||||
make_stream(s);
|
make_stream(s);
|
||||||
init_stream(s, 8192);
|
init_stream(s, max_file_size);
|
||||||
len = g_file_read(fd, s->data, 8192);
|
len = g_file_read(fd, s->data, max_file_size);
|
||||||
if (len > 0)
|
if (len > 0)
|
||||||
{
|
{
|
||||||
s->end = s->p + len;
|
s->end = s->p + len;
|
||||||
@ -154,6 +154,7 @@ file_read_line(struct stream* s, char* text)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
/* returns error */
|
||||||
static int APP_CC
|
static int APP_CC
|
||||||
file_split_name_value(char* text, char* name, char* value)
|
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 */
|
/* return error */
|
||||||
int APP_CC
|
static int APP_CC
|
||||||
file_read_section(int fd, const char* section, struct list* names,
|
l_file_read_section(int fd, int max_file_size, const char* section,
|
||||||
struct list* values)
|
struct list* names, struct list* values)
|
||||||
{
|
{
|
||||||
struct stream* s;
|
struct stream* s;
|
||||||
char text[512];
|
char text[512];
|
||||||
@ -208,7 +209,9 @@ file_read_section(int fd, const char* section, struct list* names,
|
|||||||
int in_it_index;
|
int in_it_index;
|
||||||
int len;
|
int len;
|
||||||
int index;
|
int index;
|
||||||
|
int file_size;
|
||||||
|
|
||||||
|
file_size = 32 * 1024; /* 32 K file size limit */
|
||||||
g_file_seek(fd, 0);
|
g_file_seek(fd, 0);
|
||||||
in_it_index = 0;
|
in_it_index = 0;
|
||||||
in_it = 0;
|
in_it = 0;
|
||||||
@ -216,8 +219,8 @@ file_read_section(int fd, const char* section, struct list* names,
|
|||||||
list_clear(names);
|
list_clear(names);
|
||||||
list_clear(values);
|
list_clear(values);
|
||||||
make_stream(s);
|
make_stream(s);
|
||||||
init_stream(s, 8192);
|
init_stream(s, file_size);
|
||||||
len = g_file_read(fd, s->data, 8192);
|
len = g_file_read(fd, s->data, file_size);
|
||||||
if (len > 0)
|
if (len > 0)
|
||||||
{
|
{
|
||||||
s->end = s->p + len;
|
s->end = s->p + len;
|
||||||
@ -259,3 +262,77 @@ file_read_section(int fd, const char* section, struct list* names,
|
|||||||
free_stream(s);
|
free_stream(s);
|
||||||
return 1;
|
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
|
int APP_CC
|
||||||
file_read_sections(int fd, struct list* names);
|
file_read_sections(int fd, struct list* names);
|
||||||
int APP_CC
|
int APP_CC
|
||||||
file_read_section(int fd, const char* section, struct list* names,
|
file_by_name_read_sections(const char* file_name, struct list* names);
|
||||||
struct list* values);
|
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
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user