This commit is contained in:
Sergey Lyubka committed 2022-06-04 08:45:34 +01:00
1 parent 35b517b434
commit f4fa1e097d
1 file changed
-76
-76
View File
@@ -2882,82 +2882,6 @@ char buf[128];
mg_base64_decode("Q2VzYW50YQ==", 12, buf); // buf is now "Cesanta"
```
### mg\_file\_read()
```c
char *mg_file_read(struct mg_fs *fs, const char *path, size_t *sizep);
```
Read file contents into a nul-terminated malloc-ed string. It is a caller's
responsibility to free() a returned pointer. If `sizep` is not NULL, it will
return a file size in bytes. Return `NULL` on error.
Parameters:
- `fs` - Filesystem to use
- `path` - Path to file to read
- `sizep` - Pointer to `size_t` to receive file size
Return value: File contents, see function description
Usage example:
```c
size_t file_size;
char* data = mg_file_read(&mg_fs_posix, "myfile.txt", &file_size);
if (data != NULL) {
// `data` is now pointer to information readen from file and `file_size` is it size.
}
free(data);
```
### mg\_file\_write()
```c
bool mg_file_write(struct mg_fs *fs, const char *path, const void *buf, size_t len);
```
Write data to a file.
The write is atomic, i.e. data gets written to a temporary file first, then `rename()-ed` to a destination file name.
Parameters:
- `fs` - Filesystem to use
- `path` - Path to file
- `buf` - Data to write
- `len` - Data length
Return value: `true` if written, `false` otherwise
Usage example:
```c
char data[] = "Hello, world!";
if(mg_file_write(&mg_fs_posix, "my_file.txt", data, sizeof(data) - 1)) {
// File contains "Hello, world!" string
}
```
### mg\_file\_printf()
```c
bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...);
```
Write into a file `path` using `printf()` semantics.
This function prints data to a
temporary in-memory buffer first, then calls `mg_file_write()`.
Parameters:
- `fs` - Filesystem to use
- `path`- path to file
- `fmt` - Format string in `printf()` semantics
Return value: `true` on success, `false` otherwise
```c
if (mg_file_printf(&mg_fs_posix, "my_file.txt", "Hello, %s!", "world") {
// File contains "Hello, world!" string
}
```
### mg\_random()
```c