]> gitweb.hhaalo.de Git - discspan.git/commitdiff
change: refactor splitter write file function
authorBastian Dehn <hhaalo@arcor.de>
Sun, 12 Jan 2020 20:30:32 +0000 (21:30 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 12 Jan 2020 20:30:32 +0000 (21:30 +0100)
splitter.c

index 49bf94faef2b400a198b77819a5513c5e219aa99..2e8ff456a9859f56712a58c21214c4c22865b0d6 100644 (file)
@@ -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;
 }