From: Bastian Dehn Date: Fri, 30 Jul 2021 16:58:33 +0000 (+0200) Subject: change: array into arrayhandler X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=bbbe88b33c4cd17fb32d1c84bcc2c265e5f9d0c6;p=discspan.git change: array into arrayhandler --- diff --git a/discspan.c b/discspan.c index 209826c..6e1d42f 100644 --- a/discspan.c +++ b/discspan.c @@ -10,9 +10,10 @@ 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; } diff --git a/discspan.h b/discspan.h index 1614276..6b05345 100644 --- a/discspan.h +++ b/discspan.h @@ -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 diff --git a/readfile.c b/readfile.c index 3c8eb10..b303121 100644 --- a/readfile.c +++ b/readfile.c @@ -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; } diff --git a/readfile.h b/readfile.h index 927da68..54c8483 100644 --- a/readfile.h +++ b/readfile.h @@ -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 diff --git a/splitter.c b/splitter.c index 85f1e8e..091feeb 100644 --- a/splitter.c +++ b/splitter.c @@ -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); } diff --git a/splitter.h b/splitter.h index 37c4e27..a5779c4 100644 --- a/splitter.h +++ b/splitter.h @@ -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