mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-15 10:29:47 +00:00
Change mg_file_read() signature: return mg_str
This commit is contained in:
1 parent
d128d2146a
commit
0e2f3de154
24 files changed
+95
-1052
No files matched your search
@@ -22,25 +22,21 @@ void mg_fs_close(struct mg_fd *fd) {
|
||||
}
|
||||
}
|
||||
|
||||
char *mg_file_read(struct mg_fs *fs, const char *path, size_t *sizep) {
|
||||
struct mg_fd *fd;
|
||||
char *data = NULL;
|
||||
size_t size = 0;
|
||||
fs->st(path, &size, NULL);
|
||||
if ((fd = mg_fs_open(fs, path, MG_FS_READ)) != NULL) {
|
||||
data = (char *) calloc(1, size + 1);
|
||||
if (data != NULL) {
|
||||
if (fs->rd(fd->fd, data, size) != size) {
|
||||
free(data);
|
||||
data = NULL;
|
||||
} else {
|
||||
data[size] = '\0';
|
||||
if (sizep != NULL) *sizep = size;
|
||||
}
|
||||
struct mg_str mg_file_read(struct mg_fs *fs, const char *path) {
|
||||
struct mg_str result = {NULL, 0};
|
||||
void *fp;
|
||||
fs->st(path, &result.len, NULL);
|
||||
if ((fp = fs->op(path, MG_FS_READ)) != NULL) {
|
||||
result.ptr = (char *) calloc(1, result.len + 1);
|
||||
if (result.ptr != NULL &&
|
||||
fs->rd(fp, (void *) result.ptr, result.len) != result.len) {
|
||||
free((void *) result.ptr);
|
||||
result.ptr = NULL;
|
||||
}
|
||||
mg_fs_close(fd);
|
||||
fs->cl(fp);
|
||||
}
|
||||
return data;
|
||||
if (result.ptr == NULL) result.len = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool mg_file_write(struct mg_fs *fs, const char *path, const void *buf,
|
||||
|
||||
@@ -40,7 +40,7 @@ struct mg_fd {
|
||||
struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags);
|
||||
void mg_fs_close(struct mg_fd *fd);
|
||||
bool mg_fs_ls(struct mg_fs *fs, const char *path, char *buf, size_t len);
|
||||
char *mg_file_read(struct mg_fs *fs, const char *path, size_t *size);
|
||||
struct mg_str mg_file_read(struct mg_fs *fs, const char *path);
|
||||
bool mg_file_write(struct mg_fs *fs, const char *path, const void *, size_t);
|
||||
bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...);
|
||||
|
||||
|
||||
Reference in new issue
Block a user