aboutsummaryrefslogtreecommitdiff
path: root/src/loadfiles.c
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2020-03-20 23:06:46 +0300
committerAleksey Veresov <aleksey@veresov.pro>2020-03-20 23:06:46 +0300
commit8acbd33a82d2c13e70eb17447bc6abfd86cf9512 (patch)
treef89d1c6ec04e3ba418b63c37202d6abc4f08138e /src/loadfiles.c
parentfcffb003f36a4357b6ba88e6b5e2239d7d111a6a (diff)
downloadmagi-8acbd33a82d2c13e70eb17447bc6abfd86cf9512.tar
magi-8acbd33a82d2c13e70eb17447bc6abfd86cf9512.tar.xz
magi-8acbd33a82d2c13e70eb17447bc6abfd86cf9512.zip
[magi]
Diffstat (limited to 'src/loadfiles.c')
-rw-r--r--src/loadfiles.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/loadfiles.c b/src/loadfiles.c
new file mode 100644
index 0000000..b3efff7
--- /dev/null
+++ b/src/loadfiles.c
@@ -0,0 +1,78 @@
+#include "loadfiles.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+void magi_loadfiles_add(magi_loadfiles *table,
+ const char *name,
+ const char *path,
+ int max)
+{
+ static const int size = sizeof(*table->files);
+ if (!table) {
+ return;
+ }
+ if (table->count) {
+ table->files = realloc(table->files, size * table->count + size);
+ } else {
+ table->files = malloc(size);
+ }
+ table->files[table->count].name = name;
+ table->files[table->count].path = path;
+ table->files[table->count].max = max;
+ table->count++;
+}
+
+void magi_loadfiles_free(magi_loadfiles *table)
+{
+ if (!table) {
+ return;
+ }
+ free(table->files);
+ table->count = 0;
+}
+
+static void loadfiles(void *userdata,
+ magi_file *file,
+ char *addon,
+ int addon_len)
+{
+ magi_loadfiles *table = userdata;
+ int pos;
+ if (!file->filename || !strcmp(file->filename, "")) {
+ return;
+ }
+ for (pos = 0; pos != table->count; ++pos) {
+ if (!strcmp(table->files[pos].name, file->field)) {
+ static FILE *f = 0;
+ static int unlimited;
+ static int left;
+ if (!f) {
+ const char *path = table->files[pos].path;
+ f = fopen(path, "wb");
+ left = table->files[pos].max;
+ unlimited = !left;
+ }
+ if (unlimited) {
+ fwrite(addon, 1, addon_len, f);
+ } else {
+ int min = left < addon_len ? left : addon_len;
+ fwrite(addon, 1, min, f);
+ left -= min;
+ }
+ if (!addon_len) {
+ fclose(f);
+ f = 0;
+ }
+ return;
+ }
+ }
+}
+
+void magi_loadfiles_set(magi_request *request, magi_loadfiles *table)
+{
+ request->callback.act = loadfiles;
+ request->callback.userdata = table;
+}