mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-14 09:48:01 +08:00
Add sizep param to mg_file_read()
This commit is contained in:
parent
381d7cce54
commit
4528cc28ef
@ -143,8 +143,8 @@ static void broadcast_mjpeg_frame(struct mg_mgr *mgr) {
|
|||||||
size_t nfiles = sizeof(files) / sizeof(files[0]);
|
size_t nfiles = sizeof(files) / sizeof(files[0]);
|
||||||
static size_t i;
|
static size_t i;
|
||||||
const char *path = files[i++ % nfiles];
|
const char *path = files[i++ % nfiles];
|
||||||
size_t size = mg_file_size(path);
|
size_t size = 0;
|
||||||
char *data = mg_file_read(path); // Read next file
|
char *data = mg_file_read(path, &size); // Read next file
|
||||||
struct mg_connection *c;
|
struct mg_connection *c;
|
||||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||||
if (c->label[0] != 'S') continue; // Skip non-stream connections
|
if (c->label[0] != 'S') continue; // Skip non-stream connections
|
||||||
|
@ -32,8 +32,8 @@ static void broadcast_mjpeg_frame(struct mg_mgr *mgr) {
|
|||||||
size_t nfiles = sizeof(files) / sizeof(files[0]);
|
size_t nfiles = sizeof(files) / sizeof(files[0]);
|
||||||
static size_t i;
|
static size_t i;
|
||||||
const char *path = files[i++ % nfiles];
|
const char *path = files[i++ % nfiles];
|
||||||
size_t size = mg_file_size(path);
|
size_t size = 0;
|
||||||
char *data = mg_file_read(path); // Read next file
|
char *data = mg_file_read(path, &size); // Read next file
|
||||||
struct mg_connection *c;
|
struct mg_connection *c;
|
||||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||||
if (c->label[0] != 'S') continue; // Skip non-stream connections
|
if (c->label[0] != 'S') continue; // Skip non-stream connections
|
||||||
|
@ -3902,7 +3902,7 @@ int64_t mg_file_size(const char *path) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
char *mg_file_read(const char *path) {
|
char *mg_file_read(const char *path, size_t *sizep) {
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
char *data = NULL;
|
char *data = NULL;
|
||||||
size_t size = (size_t) mg_file_size(path);
|
size_t size = (size_t) mg_file_size(path);
|
||||||
@ -3914,6 +3914,7 @@ char *mg_file_read(const char *path) {
|
|||||||
data = NULL;
|
data = NULL;
|
||||||
} else {
|
} else {
|
||||||
data[size] = '\0';
|
data[size] = '\0';
|
||||||
|
if (sizep != NULL) *sizep = size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
@ -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);
|
int64_t mg_file_size(const char *path);
|
||||||
bool mg_file_write(const char *path, const void *buf, size_t len);
|
bool mg_file_write(const char *path, const void *buf, size_t len);
|
||||||
bool mg_file_printf(const char *path, const char *fmt, ...);
|
bool mg_file_printf(const char *path, const char *fmt, ...);
|
||||||
|
@ -33,7 +33,7 @@ int64_t mg_file_size(const char *path) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
char *mg_file_read(const char *path) {
|
char *mg_file_read(const char *path, size_t *sizep) {
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
char *data = NULL;
|
char *data = NULL;
|
||||||
size_t size = (size_t) mg_file_size(path);
|
size_t size = (size_t) mg_file_size(path);
|
||||||
@ -45,6 +45,7 @@ char *mg_file_read(const char *path) {
|
|||||||
data = NULL;
|
data = NULL;
|
||||||
} else {
|
} else {
|
||||||
data[size] = '\0';
|
data[size] = '\0';
|
||||||
|
if (sizep != NULL) *sizep = size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#include "arch.h"
|
#include "arch.h"
|
||||||
#include "str.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);
|
int64_t mg_file_size(const char *path);
|
||||||
bool mg_file_write(const char *path, const void *buf, size_t len);
|
bool mg_file_write(const char *path, const void *buf, size_t len);
|
||||||
bool mg_file_printf(const char *path, const char *fmt, ...);
|
bool mg_file_printf(const char *path, const char *fmt, ...);
|
||||||
|
@ -503,7 +503,7 @@ static void test_http_server(void) {
|
|||||||
#endif
|
#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(fetch(&mgr, buf, url, "GET /ca.pem HTTP/1.0\r\n\n") == 200);
|
||||||
ASSERT(cmpbody(data, buf) == 0);
|
ASSERT(cmpbody(data, buf) == 0);
|
||||||
free(data);
|
free(data);
|
||||||
@ -564,7 +564,7 @@ static void test_http_server(void) {
|
|||||||
// Test upload
|
// Test upload
|
||||||
char *p;
|
char *p;
|
||||||
remove("uploaded.txt");
|
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,
|
ASSERT(fetch(&mgr, buf, url,
|
||||||
"POST /upload HTTP/1.0\n"
|
"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"
|
"POST /upload?name=uploaded.txt&offset=5 HTTP/1.0\r\n"
|
||||||
"Content-Length: 6\r\n"
|
"Content-Length: 6\r\n"
|
||||||
"\r\n\nworld") == 200);
|
"\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);
|
ASSERT(strcmp(p, "hello\nworld") == 0);
|
||||||
free(p);
|
free(p);
|
||||||
remove("uploaded.txt");
|
remove("uploaded.txt");
|
||||||
@ -1069,7 +1069,7 @@ static void test_util(void) {
|
|||||||
free(s);
|
free(s);
|
||||||
memset(&a, 0, sizeof(a));
|
memset(&a, 0, sizeof(a));
|
||||||
ASSERT(mg_file_printf("data.txt", "%s", "hi") == true);
|
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);
|
ASSERT(strcmp(p, "hi") == 0);
|
||||||
free(p);
|
free(p);
|
||||||
remove("data.txt");
|
remove("data.txt");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user