From: Bastian Dehn Date: Sun, 12 Jan 2020 20:30:32 +0000 (+0100) Subject: change: refactor splitter write file function X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=5d1a43837f0a7e24c197a00919970737f84604c9;p=discspan.git change: refactor splitter write file function --- diff --git a/splitter.c b/splitter.c index 49bf94f..2e8ff45 100644 --- a/splitter.c +++ b/splitter.c @@ -25,6 +25,17 @@ void printHumanReadSize(const char *output, unsigned long long splitgroesse) splitgroesse, humanread, einheit[i]); } +void writeOutputFile(const char *outputPath, file_size_t *disc, int length) +{ + FILE *out = fopen(outputPath, "w"); + + for (int i = 0; i <= length; i++) { + fprintf(out, "%s\n", disc[i].name); + } + + fclose(out); +} + /** * Schreibt in eine Datei die Pfade der Dateien, die auf das Medium passen. Wenn * ein Rest uebrig bleibt wird das Array reallociert und die Restlange zurueck- @@ -42,24 +53,28 @@ int splitter(file_size_t *input, const char *output, { file_size_t *rest = (file_size_t *) malloc(length * sizeof(file_size_t)); + file_size_t *disc = (file_size_t *) + malloc(length * sizeof(file_size_t)); unsigned long long filesize = 0; int restcount = 0; - FILE *out; + int disccount = 0; - out = fopen(output, "w"); - for (int i = 0; i <= length; i++) { + for (int i = 0; i < length; i++) { // ISO9660 filesystem overhead filesize = ceil(input[i].fsize / 2048.0) * 2048; if (splitgroesse >= filesize) { splitgroesse -= filesize; - fprintf(out, "%s\n", input[i].name); + disc[disccount++] = input[i]; } else { rest[restcount++] = input[i]; } } - fclose(out); + + disc = (file_size_t *) realloc(disc, disccount * sizeof(file_size_t)); + + writeOutputFile(output, disc, disccount); printHumanReadSize(output, splitgroesse); @@ -70,6 +85,7 @@ int splitter(file_size_t *input, const char *output, } free(rest); + free(disc); restcount--; return restcount; }