From 3058bcd49c607d2fea83ecffaf736aa2bc28d25d Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Wed, 28 Jul 2021 18:19:57 +0200 Subject: [PATCH] change: remove global variables --- discspan.c | 23 +++++++++++++---------- readfile.c | 18 ++++++++++-------- readfile.h | 4 +++- splitter.c | 12 +++++------- splitter.h | 2 +- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/discspan.c b/discspan.c index e3d5547..209826c 100644 --- a/discspan.c +++ b/discspan.c @@ -12,10 +12,7 @@ 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 struct_array_length = 0; -unsigned long long split = 0; + unsigned long long splitgroesse, int *arraylength); int cmpfunc(const void *filea, const void *fileb) { @@ -32,8 +29,10 @@ int cmpfunc(const void *filea, const void *fileb) return -2; } -void readArgument(int argc, char *argv[]) +const unsigned long long readArgument(int argc, char *argv[]) { + unsigned long long split = 0; + // Medien groessen const unsigned long long bluray = 24159191040; const unsigned long long dvd9 = 8500000000; @@ -63,6 +62,8 @@ void readArgument(int argc, char *argv[]) break; } } + + return split; } void printHumanReadSize(const char *output, unsigned long long splitgroesse) @@ -95,16 +96,18 @@ int main(int argc, char *argv[]) const char *input = argv[1]; const char *output = argv[2]; - readArgument(argc, argv); - file_size_t *fs = read_input_file(input); + int arraylength = 0; + + const unsigned long long split = readArgument(argc, argv); + file_size_t *fs = read_input_file(input, split, &arraylength); - qsort(fs, struct_array_length, sizeof(file_size_t), cmpfunc); + qsort(fs, arraylength, sizeof(file_size_t), cmpfunc); char outname[strlen(output) + 3]; int num = 1; - while (struct_array_length > 0) { + while (arraylength > 0) { sprintf(outname, "%s%03d", output, num++); - fs = splitter(fs, outname, split); + fs = splitter(fs, outname, split, &arraylength); } free(fs); diff --git a/readfile.c b/readfile.c index 6f0a496..3c8eb10 100644 --- a/readfile.c +++ b/readfile.c @@ -6,7 +6,6 @@ #include "discspan.h" #include "readfile.h" -extern int struct_array_length; extern unsigned long long split; /** @@ -39,7 +38,8 @@ int get_array_length(FILE *in) * @return Arraylaenge */ file_size_t * fill_array_from_file(FILE *in, file_size_t *fs, - const unsigned long long split) + const unsigned long long split, + int *length) { FILE *ignore = NULL; char pfad[255]; @@ -65,21 +65,23 @@ file_size_t * fill_array_from_file(FILE *in, file_size_t *fs, if (ignore != NULL) fclose(ignore); - struct_array_length = lines; + length = &lines; return fs; } -file_size_t * read_input_file(const char *input) +file_size_t * read_input_file(const char *input, + const unsigned long long split, + int *arraylength) { FILE *in = fopen(input, "r"); - struct_array_length = get_array_length(in); + *arraylength = get_array_length(in); file_size_t *fs = (file_size_t *) - malloc(struct_array_length * sizeof(file_size_t)); + malloc(*arraylength * sizeof(file_size_t)); - fs = fill_array_from_file(in, fs, split); + fs = fill_array_from_file(in, fs, split, arraylength); fs = (file_size_t *) - realloc(fs, struct_array_length * sizeof(file_size_t)); + realloc(fs, *arraylength * sizeof(file_size_t)); fclose(in); diff --git a/readfile.h b/readfile.h index d7237d6..927da68 100644 --- a/readfile.h +++ b/readfile.h @@ -8,6 +8,8 @@ * * @return file_size struct array */ -file_size_t * read_input_file(const char *input); +file_size_t * read_input_file(const char *input, + const unsigned long long split, + int *arraylength); #endif diff --git a/splitter.c b/splitter.c index f2eb235..85f1e8e 100644 --- a/splitter.c +++ b/splitter.c @@ -6,8 +6,6 @@ #include "discspan.h" #include "splitter.h" -extern int struct_array_length; - void writeOutputFile(const char *outputPath, file_size_t *disc, int length) { FILE *out = fopen(outputPath, "w"); @@ -26,18 +24,18 @@ void onPrintEvent(const char *output, unsigned long long splitgroesse) } file_size_t * splitter(file_size_t *input, const char *output, - unsigned long long splitgroesse) + unsigned long long splitgroesse, int *arraylength) { file_size_t *rest = (file_size_t *) - malloc(struct_array_length * sizeof(file_size_t)); + malloc(*arraylength * sizeof(file_size_t)); file_size_t *disc = (file_size_t *) - malloc(struct_array_length * sizeof(file_size_t)); + malloc(*arraylength * sizeof(file_size_t)); unsigned long long filesize = 0; int restcount = 0; int disccount = 0; - for (int i = 0; i < struct_array_length; i++) { + for (int i = 0; i < *arraylength; i++) { // ISO9660 filesystem overhead filesize = ceil(input[i].fsize / 2048.0) * 2048; @@ -61,6 +59,6 @@ file_size_t * splitter(file_size_t *input, const char *output, free(rest); free(disc); - struct_array_length = restcount; + *arraylength = restcount; return input; } diff --git a/splitter.h b/splitter.h index 77614a4..37c4e27 100644 --- a/splitter.h +++ b/splitter.h @@ -22,6 +22,6 @@ void (*printEvent)(const char *output, unsigned long long splitgroesse); * @return Restlaenge des neuen Arrays */ file_size_t * splitter(file_size_t *input, const char *output, - unsigned long long splitgroesse); + unsigned long long splitgroesse, int *arraylength); #endif -- 2.39.5