]> gitweb.hhaalo.de Git - discspan.git/commitdiff
change: array into arrayhandler
authorBastian Dehn <hhaalo@arcor.de>
Fri, 30 Jul 2021 16:58:33 +0000 (18:58 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Fri, 30 Jul 2021 16:58:33 +0000 (18:58 +0200)
discspan.c
discspan.h
readfile.c
readfile.h
splitter.c
splitter.h

index 209826c6cd037bb1887d48d66185fbd5573544b3..6e1d42ff2bd9f40e85f90ce2ceb2ad114b560110 100644 (file)
 
 extern void (*printEvent)(const char *output, unsigned long long splitgroesse);
 extern void useage();
-extern file_size_t * read_input_file();
-extern file_size_t * splitter(file_size_t *input, const char *output,
-                       unsigned long long splitgroesse, int *arraylength);
+extern void read_input_file(const char *input, const unsigned long long split,
+               file_size_handler_t *handler);
+extern void splitter(file_size_handler_t *handler, const char *output,
+               unsigned long long splitgroesse);
 
 int cmpfunc(const void *filea, const void *fileb)
 {
@@ -93,23 +94,25 @@ int main(int argc, char *argv[])
        }
 
        printEvent = printHumanReadSize;
+
        const char *input = argv[1];
        const char *output = argv[2];
 
-       int arraylength = 0;
+       file_size_handler_t arrayhandler = { NULL, 0 };
 
        const unsigned long long split = readArgument(argc, argv);
-       file_size_t *fs = read_input_file(input, split, &arraylength);
+       read_input_file(input, split, &arrayhandler);
 
-       qsort(fs, arraylength, sizeof(file_size_t), cmpfunc);
+       qsort(arrayhandler.filearray, arrayhandler.length, sizeof(file_size_t),
+                       cmpfunc);
 
        char outname[strlen(output) + 3];
        int num = 1;
-       while (arraylength > 0) {
+       while (arrayhandler.length > 0) {
                sprintf(outname, "%s%03d", output, num++);
-               fs = splitter(fs, outname, split, &arraylength);
+               splitter(&arrayhandler, outname, split);
        }
 
-       free(fs);
+       free(arrayhandler.filearray);
        return 0;
 }
index 16142764acc378ce0cfab9f60ea59dcc1fd216c9..6b0534533b2c0122d42a9033665c99ca47f9c6cc 100644 (file)
@@ -6,4 +6,9 @@ typedef struct {
        long long unsigned fsize;
 } file_size_t;
 
+typedef struct {
+       file_size_t *filearray;
+       int length;
+} file_size_handler_t;
+
 #endif
index 3c8eb10a448bb12f86d64ca3323ed4929a0e9fe2..b3031216db59875b97d948bc51be646a93ff5c2b 100644 (file)
@@ -6,16 +6,7 @@
 #include "discspan.h"
 #include "readfile.h"
 
-extern unsigned long long split;
-
-/**
- * Ermittelt die Zeilen der Textdatei
- *
- * @param Datei pointer
- *
- * @return Anzahl von Zeilen
- */
-int get_array_length(FILE *in)
+void malloc_array_length(FILE *in, file_size_handler_t *handler)
 {
        int lines = 0;
        char pfad[255];
@@ -25,21 +16,13 @@ int get_array_length(FILE *in)
        }
        rewind(in);
 
-       return lines;
+       handler->length = lines;
+       handler->filearray = (file_size_t *)
+               malloc(handler->length * sizeof(file_size_t));
 }
 
-/**
- * Liest die Datei in den Ram ein und gibt die Laenge des Arrays zurueck
- *
- * @param Datei pointer
- * @param file_size pointer
- * @param Splitgroesse
- *
- * @return Arraylaenge
- */
-file_size_t * fill_array_from_file(FILE *in, file_size_t *fs,
-               const unsigned long long split,
-               int *length)
+void fill_array_from_file(FILE *in, const unsigned long long split,
+               file_size_handler_t *handler)
 {
        FILE *ignore = NULL;
        char pfad[255];
@@ -51,8 +34,8 @@ file_size_t * fill_array_from_file(FILE *in, file_size_t *fs,
                stat(pfad, &st);
 
                if (st.st_size <= split) {
-                       strcpy(fs[lines].name, pfad);
-                       fs[lines++].fsize = st.st_size;
+                       strcpy(handler->filearray[lines].name, pfad);
+                       handler->filearray[lines++].fsize = st.st_size;
                } else {
                        // irgnore List erstellen
                        if (ignore == NULL)
@@ -65,25 +48,20 @@ file_size_t * fill_array_from_file(FILE *in, file_size_t *fs,
        if (ignore != NULL)
                fclose(ignore);
 
-       length = &lines;
-       return fs;
+       handler->length = lines;
+       handler->filearray = (file_size_t *)
+               realloc(handler->filearray,
+                               handler->length * sizeof(file_size_t));
 }
 
-file_size_t * read_input_file(const char *input,
+void read_input_file(const char *input,
                const unsigned long long split,
-               int *arraylength)
+               file_size_handler_t *handler)
 {
        FILE *in = fopen(input, "r");
 
-       *arraylength = get_array_length(in);
-       file_size_t *fs = (file_size_t *)
-               malloc(*arraylength * sizeof(file_size_t));
-
-       fs = fill_array_from_file(in, fs, split, arraylength);
-       fs = (file_size_t *)
-               realloc(fs, *arraylength * sizeof(file_size_t));
+       malloc_array_length(in, handler);
+       fill_array_from_file(in, split, handler);
 
        fclose(in);
-
-       return fs;
 }
index 927da6846318fd3719144bf2a07c8af4c72997f3..54c84839e992ec14368aa6fccb85351eec1c2479 100644 (file)
@@ -1,15 +1,8 @@
 #ifndef _READFILE_H
 #define _READFILE_H
 
-/**
- * Liest die Input Datei in den RAM ein
- *
- * @input Pfad zur Textdatei mit dem Dateipfaden
- *
- * @return file_size struct array
- */
-file_size_t * read_input_file(const char *input,
+void read_input_file(const char *input,
               const unsigned long long split,
-              int *arraylength);
+              file_size_handler_t *handler);
 
 #endif
index 85f1e8e9481303dce25115d535d7af01cdbe70a9..091feebb6376626fdc87e74eb97aa6d81a3be86d 100644 (file)
@@ -23,27 +23,26 @@ void onPrintEvent(const char *output, unsigned long long splitgroesse)
                printEvent(output, splitgroesse);
 }
 
-file_size_t * splitter(file_size_t *input, const char *output,
-               unsigned long long splitgroesse, int *arraylength)
+void splitter(file_size_handler_t *handler, const char *output, unsigned long long splitgroesse)
 {
        file_size_t *rest = (file_size_t *)
-               malloc(*arraylength * sizeof(file_size_t));
+               malloc(handler->length * sizeof(file_size_t));
        file_size_t *disc = (file_size_t *)
-               malloc(*arraylength * sizeof(file_size_t));
+               malloc(handler->length * sizeof(file_size_t));
 
        unsigned long long filesize = 0;
        int restcount = 0;
        int disccount = 0;
 
-       for (int i = 0; i < *arraylength; i++) {
+       for (int i = 0; i < handler->length; i++) {
                // ISO9660 filesystem overhead
-               filesize = ceil(input[i].fsize / 2048.0) * 2048;
+               filesize = ceil(handler->filearray[i].fsize / 2048.0) * 2048;
 
                if (splitgroesse >= filesize) {
                        splitgroesse -= filesize;
-                       disc[disccount++] = input[i];
+                       disc[disccount++] = handler->filearray[i];
                } else {
-                       rest[restcount++] = input[i];
+                       rest[restcount++] = handler->filearray[i];
                }
        }
 
@@ -51,14 +50,11 @@ file_size_t * splitter(file_size_t *input, const char *output,
 
        onPrintEvent(output, splitgroesse);
 
-       if (restcount > 0) {
-               memcpy(input, rest, restcount * sizeof(file_size_t));
-               input = (file_size_t *)
-                       realloc(input, restcount * sizeof(file_size_t));
-       }
+       memcpy(handler->filearray, rest, restcount * sizeof(file_size_t));
+       handler->filearray = (file_size_t *) realloc(handler->filearray,
+                                       restcount * sizeof(file_size_t));
 
-       free(rest);
+       handler->length = restcount;
        free(disc);
-       *arraylength = restcount;
-       return input;
+       free(rest);
 }
index 37c4e276851caae1b93ce5400abd1ccf0aa14dab..a5779c426853ddae077fd88f75193964d25c93c7 100644 (file)
@@ -1,27 +1,9 @@
 #ifndef _SPLITTER_H
 #define _SPLITTER_H
 
-/**
- * PrintEvent umd Informationen Anzeigen zur laufzeit Anzeigen zu lassen.
- *
- * @param output Ausgabedateinamen
- * @param splitgroesse die Bytes, die uebrig geblieben sind
- */
 void (*printEvent)(const char *output, unsigned long long splitgroesse);
 
-/**
- * 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-
- * gegeben.
- *
- * @param pointer auf das Array mit file_size struct
- * @param pointer auf den string fuer den Ausgabepfad der Datei
- * @param Splitgroesse
- * @param maximale Laenge des neuen Arrays
- *
- * @return Restlaenge des neuen Arrays
- */
-file_size_t * splitter(file_size_t *input, const char *output,
-               unsigned long long splitgroesse, int *arraylength);
+void splitter(file_size_handler_t *handler, const char *output,
+               unsigned long long splitgroesse);
 
 #endif