feat(fs): add fs module
This commit is contained in:
parent
69519211ad
commit
0178df85bd
|
@ -15,6 +15,7 @@ parameters and return values).
|
|||
- `cstr`: Functions that expand C's `<string.h>` library for `char *` strings
|
||||
- `debug`: Debug utilities, e.g. debug messages for memory allocations
|
||||
- `enum-helper`: Helper macros for enums (header only)
|
||||
- `fs`: File system handling
|
||||
- `log`: Logging functionality with different log levels and support for
|
||||
printing source file and line information
|
||||
- `str`: A custom string type that stores length and capacity and associated
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
#include "fs.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* read the contents of an entire file into a string. the string containing the
|
||||
* file contents is dynamically allocated.
|
||||
*
|
||||
* @param path: the path that points to the file
|
||||
*
|
||||
* @return the file contents, or nullptr on error
|
||||
*
|
||||
* @note since the returned string is dynamically allocated, its memory
|
||||
* needs to be freed manually.
|
||||
*/
|
||||
char *
|
||||
fs_read_to_str (const char *path)
|
||||
{
|
||||
FILE *fp;
|
||||
char *buf;
|
||||
usize len;
|
||||
|
||||
if (path && (fp = fopen (path, "r"))) {
|
||||
fseek (fp, 0, SEEK_END);
|
||||
len = ftell (fp);
|
||||
fseek (fp, 0, SEEK_SET);
|
||||
|
||||
buf = smalloc (len + 1);
|
||||
|
||||
fread (buf, sizeof *buf, len, fp);
|
||||
buf[len] = '\0';
|
||||
|
||||
fclose (fp);
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* write a string to a file.
|
||||
*
|
||||
* @param path: the path to the file
|
||||
* @param content: the string to write
|
||||
*
|
||||
* @return false on error, true on success
|
||||
*/
|
||||
bool
|
||||
fs_write_file (const char *path, const char *content)
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
if (path && content && (fp = fopen (path, "w+"))) {
|
||||
if (fputs (content, fp) == EOF)
|
||||
return false;
|
||||
fclose (fp);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef FS_H_
|
||||
#define FS_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
char *fs_read_to_str (const char *path); /* read the contents of an entire file into a string */
|
||||
bool fs_write_file (const char *path, const char *content); /* write a string to a file */
|
||||
|
||||
#endif // FS_H_
|
|
@ -0,0 +1 @@
|
|||
Lorem ipsum dolor sit amet.
|
31
test/main.c
31
test/main.c
|
@ -5,14 +5,15 @@
|
|||
|
||||
#include "cstr.h"
|
||||
#define CUTILS_DEBUG_TRACE
|
||||
#include "debug.h"
|
||||
//#include "debug.h"
|
||||
#include "fs.h"
|
||||
#include "log.h"
|
||||
#include "common.h"
|
||||
#include "str.h"
|
||||
#include "test.h"
|
||||
#include "vec.h"
|
||||
|
||||
test_results_t
|
||||
static test_results_t
|
||||
test_cstr (void)
|
||||
{
|
||||
test_group_t group;
|
||||
|
@ -53,7 +54,28 @@ test_cstr (void)
|
|||
return test_group_get_results (&group);
|
||||
}
|
||||
|
||||
test_results_t
|
||||
static test_results_t
|
||||
test_fs (void)
|
||||
{
|
||||
test_group_t group;
|
||||
char *content;
|
||||
|
||||
group = test_group_new ();
|
||||
|
||||
content = fs_read_to_str ("test/lorem.txt");
|
||||
test_add (&group, test_assert (!strcmp (content, "Lorem ipsum dolor sit amet.\n")), "fs_read_to_str correct path");
|
||||
free (content);
|
||||
|
||||
content = fs_read_to_str ("test/this-file-does-not-exist.txt");
|
||||
test_add (&group, test_assert (content == nullptr), "fs_read_to_str incorrect path");
|
||||
|
||||
content = fs_read_to_str (nullptr);
|
||||
test_add (&group, test_assert (content == nullptr), "fs_read_to_str nullptr");
|
||||
|
||||
return test_group_get_results (&group);
|
||||
}
|
||||
|
||||
static test_results_t
|
||||
test_str (void)
|
||||
{
|
||||
test_group_t group;
|
||||
|
@ -106,7 +128,7 @@ test_str (void)
|
|||
return test_group_get_results (&group);
|
||||
}
|
||||
|
||||
test_results_t
|
||||
static test_results_t
|
||||
test_vec (void)
|
||||
{
|
||||
test_group_t group;
|
||||
|
@ -159,6 +181,7 @@ main (void)
|
|||
|
||||
suite = test_suite_new ();
|
||||
test_suite_add (&suite, test_cstr);
|
||||
test_suite_add (&suite, test_fs);
|
||||
test_suite_add (&suite, test_str);
|
||||
test_suite_add (&suite, test_vec);
|
||||
test_suite_run (&suite);
|
||||
|
|
Loading…
Reference in New Issue