Skip to content

Commit

Permalink
Changed signature of str_from_file() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim2266 committed Jul 1, 2020
1 parent 62c891d commit 985672e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ Sets the target object to `str_null` after freeing any memory owned by the targe
`void str_swap(str* const s1, str* const s2)`<br>
Swaps two string objects.
`int str_from_file(str* const dest, const str file_name)`<br>
Reads the entire file (of up to 1Gb in size) into the destination string. Returns 0 on success,
or the value of `errno` on error.
`int str_from_file(str* const dest, const char* const file_name)`<br>
Reads the entire file (of up to 1Gb by default, configuarable via `STR_MAX_FILE_SIZE`) into
the destination string. Returns 0 on success, or the value of `errno` on error.
#### String Comparison
Expand Down
6 changes: 3 additions & 3 deletions str.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ void _str_dup(str* const dest, const str s)
#define STR_MAX_FILE_SIZE (1024 * 1024 * 1024)
#endif

int str_from_file(str* const dest, const str file_name)
int str_from_file(str* const dest, const char* const file_name)
{
// stat the file
struct stat info;

if(stat(str_ptr(file_name), &info))
if(stat(file_name, &info))
return errno;

// only regular files are allowed
Expand All @@ -228,7 +228,7 @@ int str_from_file(str* const dest, const str file_name)
}

// open the file
const int fd = open(str_ptr(file_name), O_CLOEXEC | O_RDONLY);
const int fd = open(file_name, O_CLOEXEC | O_RDONLY);

if(fd == -1)
return errno;
Expand Down
2 changes: 1 addition & 1 deletion str.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ str str_acquire_chars(const char* const s, const size_t n);
str str_acquire(const char* const s);

// string from file
int str_from_file(str* const dest, const str file_name);
int str_from_file(str* const dest, const char* const file_name);

// sorting and searching --------------------------------------------------------------------
// comparison functions
Expand Down
11 changes: 6 additions & 5 deletions str_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,14 +637,15 @@ void test_from_file(void)

str res = str_null;

assert(str_from_file(&res, str_ref(tmp)) == 0);
assert(str_from_file(&res, tmp) == 0);
unlink(tmp);
assert(str_eq(res, str_lit("aaa bbb ccc")));
assert(str_is_owner(res));

// errors
assert(str_from_file(&res, str_lit(".")) == EISDIR);
assert(str_from_file(&res, str_lit("/dev/null")) == EOPNOTSUPP);
assert(str_from_file(&res, str_lit("does-not-exist")) == ENOENT);
// test errors
assert(str_from_file(&res, ".") == EISDIR);
assert(str_from_file(&res, "/dev/null") == EOPNOTSUPP);
assert(str_from_file(&res, "does-not-exist") == ENOENT);

str_free(res);
passed;
Expand Down

0 comments on commit 985672e

Please sign in to comment.