From f88c3c7ba531f3756ac26a73d1afaaab140ef852 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Mon, 6 Jan 2020 09:34:17 +0100 Subject: [PATCH] change: read input file function --- discspan.c | 59 +++++++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/discspan.c b/discspan.c index 71594a0..fb14474 100644 --- a/discspan.c +++ b/discspan.c @@ -9,6 +9,9 @@ extern void useage(); +int struct_array_length = 0; +unsigned long long int split = 0; + typedef struct file_size { char name[255]; long long unsigned int fsize; @@ -168,7 +171,7 @@ int splitter(struct file_size *input, const char *output, return restcount; } -unsigned long long readArgument(int argc, char *argv[]) +void readArgument(int argc, char *argv[]) { // Medien groessen const unsigned long long bluray = 25000000000; @@ -176,8 +179,6 @@ unsigned long long readArgument(int argc, char *argv[]) const unsigned long long dvd5 = 4700000000; const unsigned long long cd = 734003200; - unsigned long long split = 0; - int c = 0; while ((c = getopt(argc, argv, ":59bcf:")) != -1) { switch (c) { @@ -198,58 +199,48 @@ unsigned long long readArgument(int argc, char *argv[]) break; case 'h': usage(); - return 0; break; } } +} + +file_size * read_input_file(const char *input) +{ + FILE *in = fopen(input, "r"); + + struct_array_length = get_array_length(in); + struct file_size *fs = (struct file_size *) + malloc(struct_array_length * sizeof(struct file_size)); + + struct_array_length = fill_array_from_file(in, fs, split); + fs = (struct file_size *) + realloc(fs, struct_array_length * sizeof(struct file_size)); + + fclose(in); - return split; + return fs; } -/** - * Hauptfunktion - * - * @param Anzahl der Argumente - * @param Argument Array - * - * @return Erfolgreich 0; Fehler 1 - */ int main(int argc, char *argv[]) { const char *input = argv[1]; const char *output = argv[2]; - unsigned long long int split = 0; - // gib die Funktionsübersicht aus bei zu wenig Parameter if (argc <= 3) { usage(); return 1; } - split = readArgument(argc, argv); - - // Liest die Quelldatei in den Ram - FILE *in = fopen(input, "r"); - - int s_length = get_array_length(in); - struct file_size *fs = (struct file_size *) - malloc(s_length * sizeof(struct file_size)); - - s_length = fill_array_from_file(in, fs, split); - fs = (struct file_size *) - realloc(fs, s_length * sizeof(struct file_size)); - - fclose(in); + readArgument(argc, argv); + struct file_size *fs = read_input_file(input); - // sortieren - // s_length is array length 0 bis xy - qsort(fs, (s_length + 1), sizeof(struct file_size), cmpfunc); + qsort(fs, (struct_array_length + 1), sizeof(struct file_size), cmpfunc); char outname[strlen(output) + 3]; int num = 1; - while (s_length > 0) { + while (struct_array_length > 0) { sprintf(outname, "%s%03d", output, num++); - s_length = splitter(fs, outname, split, s_length); + struct_array_length = splitter(fs, outname, split, struct_array_length); } free(fs); -- 2.39.5