Add sizep param to mg_file_read()

This commit is contained in:
cpq 2021-03-15 09:16:34 +00:00
parent 381d7cce54
commit 4528cc28ef
7 changed files with 14 additions and 12 deletions

View File

@ -143,8 +143,8 @@ static void broadcast_mjpeg_frame(struct mg_mgr *mgr) {
size_t nfiles = sizeof(files) / sizeof(files[0]);
static size_t i;
const char *path = files[i++ % nfiles];
size_t size = mg_file_size(path);
char *data = mg_file_read(path); // Read next file
size_t size = 0;
char *data = mg_file_read(path, &size); // Read next file
struct mg_connection *c;
for (c = mgr->conns; c != NULL; c = c->next) {
if (c->label[0] != 'S') continue; // Skip non-stream connections

View File

@ -32,8 +32,8 @@ static void broadcast_mjpeg_frame(struct mg_mgr *mgr) {
size_t nfiles = sizeof(files) / sizeof(files[0]);
static size_t i;
const char *path = files[i++ % nfiles];
size_t size = mg_file_size(path);
char *data = mg_file_read(path); // Read next file
size_t size = 0;
char *data = mg_file_read(path, &size); // Read next file
struct mg_connection *c;
for (c = mgr->conns; c != NULL; c = c->next) {
if (c->label[0] != 'S') continue; // Skip non-stream connections

View File

@ -3902,7 +3902,7 @@ int64_t mg_file_size(const char *path) {
#endif
}
char *mg_file_read(const char *path) {
char *mg_file_read(const char *path, size_t *sizep) {
FILE *fp;
char *data = NULL;
size_t size = (size_t) mg_file_size(path);
@ -3914,6 +3914,7 @@ char *mg_file_read(const char *path) {
data = NULL;
} else {
data[size] = '\0';
if (sizep != NULL) *sizep = size;
}
}
fclose(fp);

View File

@ -477,7 +477,7 @@ void mg_timer_poll(unsigned long uptime_ms);
char *mg_file_read(const char *path);
char *mg_file_read(const char *path, size_t *size);
int64_t mg_file_size(const char *path);
bool mg_file_write(const char *path, const void *buf, size_t len);
bool mg_file_printf(const char *path, const char *fmt, ...);

View File

@ -33,7 +33,7 @@ int64_t mg_file_size(const char *path) {
#endif
}
char *mg_file_read(const char *path) {
char *mg_file_read(const char *path, size_t *sizep) {
FILE *fp;
char *data = NULL;
size_t size = (size_t) mg_file_size(path);
@ -45,6 +45,7 @@ char *mg_file_read(const char *path) {
data = NULL;
} else {
data[size] = '\0';
if (sizep != NULL) *sizep = size;
}
}
fclose(fp);

View File

@ -3,7 +3,7 @@
#include "arch.h"
#include "str.h"
char *mg_file_read(const char *path);
char *mg_file_read(const char *path, size_t *size);
int64_t mg_file_size(const char *path);
bool mg_file_write(const char *path, const void *buf, size_t len);
bool mg_file_printf(const char *path, const char *fmt, ...);

View File

@ -503,7 +503,7 @@ static void test_http_server(void) {
#endif
{
char *data = mg_file_read("./test/data/ca.pem");
char *data = mg_file_read("./test/data/ca.pem", NULL);
ASSERT(fetch(&mgr, buf, url, "GET /ca.pem HTTP/1.0\r\n\n") == 200);
ASSERT(cmpbody(data, buf) == 0);
free(data);
@ -564,7 +564,7 @@ static void test_http_server(void) {
// Test upload
char *p;
remove("uploaded.txt");
ASSERT((p = mg_file_read("uploaded.txt")) == NULL);
ASSERT((p = mg_file_read("uploaded.txt", NULL)) == NULL);
ASSERT(fetch(&mgr, buf, url,
"POST /upload HTTP/1.0\n"
@ -578,7 +578,7 @@ static void test_http_server(void) {
"POST /upload?name=uploaded.txt&offset=5 HTTP/1.0\r\n"
"Content-Length: 6\r\n"
"\r\n\nworld") == 200);
ASSERT((p = mg_file_read("uploaded.txt")) != NULL);
ASSERT((p = mg_file_read("uploaded.txt", NULL)) != NULL);
ASSERT(strcmp(p, "hello\nworld") == 0);
free(p);
remove("uploaded.txt");
@ -1069,7 +1069,7 @@ static void test_util(void) {
free(s);
memset(&a, 0, sizeof(a));
ASSERT(mg_file_printf("data.txt", "%s", "hi") == true);
ASSERT((p = mg_file_read("data.txt")) != NULL);
ASSERT((p = mg_file_read("data.txt", NULL)) != NULL);
ASSERT(strcmp(p, "hi") == 0);
free(p);
remove("data.txt");