machine: file_contents

This commit is contained in:
Alastair Poole 2020-06-20 00:48:15 +01:00
parent 70f7eb5c94
commit 18fa8063e1
3 changed files with 12 additions and 19 deletions

View File

@ -131,7 +131,6 @@ main(int argc, char **argv)
{ {
ecore_main_loop_begin(); ecore_main_loop_begin();
evisum_ui_shutdown(ui); evisum_ui_shutdown(ui);
free(ui);
} }
elm_shutdown(); elm_shutdown();

View File

@ -85,31 +85,23 @@ file_contents(const char *path)
{ {
FILE *f; FILE *f;
char *buf, *tmp; char *buf, *tmp;
char byte[1]; size_t n = 1, len = 0;
size_t count, n, bytes = 0; const size_t block = 4096;
f = fopen(path, "r"); f = fopen(path, "r");
if (!f) return NULL; if (!f) return NULL;
n = 1024; buf = NULL;
buf = malloc(n * sizeof(byte) + 1); while ((!feof(f)) && (!ferror(f)))
if (!buf) return NULL;
while ((count = (fread(byte, sizeof(byte), 1, f))) > 0)
{ {
bytes += sizeof(byte); tmp = realloc(buf, ++n * (sizeof(char) * block) + 1);
if (bytes == (n * sizeof(byte))) if (!tmp) return NULL;
{ buf = tmp;
n *= 2; len += fread(buf + len, sizeof(char), block, f);
tmp = realloc(buf, n * sizeof(byte) + 1);
if (!tmp) return NULL;
buf = tmp;
}
memcpy(&buf[bytes - sizeof(byte)], byte, sizeof(byte));
} }
if (!feof(f)) if (ferror(f))
{ {
free(buf); free(buf);
fclose(f); fclose(f);
@ -117,7 +109,7 @@ file_contents(const char *path)
} }
fclose(f); fclose(f);
buf[bytes] = 0; buf[len] = 0;
return buf; return buf;
} }

View File

@ -1520,6 +1520,8 @@ evisum_ui_shutdown(Ui *ui)
evisum_ui_item_cache_free(ui->cache); evisum_ui_item_cache_free(ui->cache);
eina_lock_free(&_lock); eina_lock_free(&_lock);
free(ui);
} }
static void static void