add extra file functions and change sl_read_file
This commit is contained in:
parent
deb02231af
commit
28b948912e
1 changed files with 16 additions and 15 deletions
31
slibs.h
31
slibs.h
|
@ -198,45 +198,46 @@ void sl_append_c_str(sl_string *sl_str, const char *c_str)
|
|||
sl_ptr_release(smart_ptr); \
|
||||
});
|
||||
|
||||
void sl_read_file(const char *filename, sl_string *buffer);
|
||||
FILE *sl_open_file(const char *filename, const char *mode);
|
||||
sl_string *sl_read_file(const char *filename);
|
||||
void sl_write_file(const char *filename, const sl_string buffer);
|
||||
|
||||
#ifdef SL_IMPLEMENTATION
|
||||
void sl_read_file(const char *filename, sl_string *buffer)
|
||||
FILE *sl_open_file(const char *filename, const char *mode)
|
||||
{
|
||||
FILE *file = fopen(filename, "r");
|
||||
FILE *file = fopen(filename, mode);
|
||||
if (!file)
|
||||
{
|
||||
fprintf(stderr, "Error: could not open file %s\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
sl_string *sl_read_file(const char *filename)
|
||||
{
|
||||
FILE *file = sl_open_file(filename, "r");
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
size_t file_size = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
sl_string *buffer = sl_new(sl_string, 0);
|
||||
|
||||
for (size_t i = 0; i < file_size; i++)
|
||||
{
|
||||
sl_vec_push(*buffer, fgetc(file));
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void sl_write_file(const char *filename, const sl_string buffer)
|
||||
void sl_write_file(const char *filename, sl_string buffer)
|
||||
{
|
||||
FILE *file = fopen(filename, "w");
|
||||
if (!file)
|
||||
{
|
||||
fprintf(stderr, "Error: could not open file %s\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (sl_vec_it(c, buffer))
|
||||
{
|
||||
fputc(*c, file);
|
||||
}
|
||||
FILE *file = sl_open_file(filename, "w");
|
||||
|
||||
fputs(sl_c_str(buffer), file);
|
||||
fclose(file);
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue