From 57aae332db84070a323b8bbc2ab9614fad33fe0a Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 31 Jul 2021 08:56:06 +0200 Subject: [PATCH] change: file write in filehandler module --- CMakeLists.txt | 2 +- Makefile | 8 ++++---- discspan.c | 12 ++++++++---- readfile.c => filehandler.c | 13 ++++++++++++- readfile.h => filehandler.h | 6 ++++-- splitter.c | 36 ++++++++++++++---------------------- splitter.h | 2 +- 7 files changed, 44 insertions(+), 35 deletions(-) rename readfile.c => filehandler.c (85%) rename readfile.h => filehandler.h (52%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 187140c..3c6ebf5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ endif() project(discspan) -set(SOURCES discspan.c readfile.c splitter.c help.c) +set(SOURCES discspan.c filehandler.c splitter.c help.c) add_executable(discspan ${SOURCES}) target_link_libraries(discspan m) diff --git a/Makefile b/Makefile index 4be349b..ebb1458 100644 --- a/Makefile +++ b/Makefile @@ -5,13 +5,13 @@ BINPATH=/usr/bin all: discspan -discspan: discspan.o readfile.o readfile.h splitter.o splitter.h help.o help.h - $(CC) $(CFLAGS) -o discspan discspan.o readfile.o splitter.o help.o +discspan: discspan.o filehandler.o filehandler.h splitter.o splitter.h help.o help.h + $(CC) $(CFLAGS) -o discspan discspan.o filehandler.o splitter.o help.o discspan.o: discspan.h discspan.c $(CC) $(CFLAGS) -c discspan.c -readfile.o: discspan.h readfile.h readfile.c - $(CC) $(CFLAGS) -c readfile.c +filehandler.o: discspan.h filehandler.h filehandler.c + $(CC) $(CFLAGS) -c filehandler.c splitter.o: splitter.h splitter.c $(CC) $(CFLAGS) -c splitter.c help.o: help.h help.c diff --git a/discspan.c b/discspan.c index 6e1d42f..b4fe792 100644 --- a/discspan.c +++ b/discspan.c @@ -5,15 +5,16 @@ #include "discspan.h" #include "splitter.h" -#include "readfile.h" +#include "filehandler.h" #include "help.h" extern void (*printEvent)(const char *output, unsigned long long splitgroesse); extern void useage(); 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); +extern void write_output_file(const char *output, file_size_handler_t disc); +extern file_size_handler_t splitter(file_size_handler_t *handler, + const char *output, unsigned long long splitgroesse); int cmpfunc(const void *filea, const void *fileb) { @@ -108,9 +109,12 @@ int main(int argc, char *argv[]) char outname[strlen(output) + 3]; int num = 1; + file_size_handler_t disc; while (arrayhandler.length > 0) { sprintf(outname, "%s%03d", output, num++); - splitter(&arrayhandler, outname, split); + disc = splitter(&arrayhandler, outname, split); + write_output_file(outname, disc); + free(disc.filearray); } free(arrayhandler.filearray); diff --git a/readfile.c b/filehandler.c similarity index 85% rename from readfile.c rename to filehandler.c index b303121..fa7b0b6 100644 --- a/readfile.c +++ b/filehandler.c @@ -4,7 +4,7 @@ #include #include "discspan.h" -#include "readfile.h" +#include "filehandler.h" void malloc_array_length(FILE *in, file_size_handler_t *handler) { @@ -65,3 +65,14 @@ void read_input_file(const char *input, fclose(in); } + +void write_output_file(const char *output, file_size_handler_t disc) +{ + FILE *out = fopen(output, "w"); + + for (int i = 0; i < disc.length; i++) { + fprintf(out, "%s\n", disc.filearray[i].name); + } + + fclose(out); +} diff --git a/readfile.h b/filehandler.h similarity index 52% rename from readfile.h rename to filehandler.h index 54c8483..a2c254f 100644 --- a/readfile.h +++ b/filehandler.h @@ -1,8 +1,10 @@ -#ifndef _READFILE_H -#define _READFILE_H +#ifndef _FILEHANDLER_H +#define _FILEHANDLER_H void read_input_file(const char *input, const unsigned long long split, file_size_handler_t *handler); +void write_output_file(const char *output, file_size_handler_t disc); + #endif diff --git a/splitter.c b/splitter.c index db225d5..10200a5 100644 --- a/splitter.c +++ b/splitter.c @@ -6,27 +6,19 @@ #include "discspan.h" #include "splitter.h" -void writeOutputFile(const char *outputPath, file_size_handler_t disc) -{ - FILE *out = fopen(outputPath, "w"); - - for (int i = 0; i < disc.length; i++) { - fprintf(out, "%s\n", disc.filearray[i].name); - } - - fclose(out); -} - void onPrintEvent(const char *output, unsigned long long splitgroesse) { if (printEvent != NULL) printEvent(output, splitgroesse); } -void splitter(file_size_handler_t *handler, const char *output, unsigned long long splitgroesse) +file_size_handler_t splitter(file_size_handler_t *handler, const char *output, + unsigned long long splitgroesse) { - file_size_t *rest = (file_size_t *) - malloc(handler->length * sizeof(file_size_t)); + file_size_handler_t rest; + rest.length = handler->length; + rest.filearray = (file_size_t *) + malloc(rest.length * sizeof(file_size_t)); file_size_handler_t disc; disc.length = handler->length; @@ -45,7 +37,7 @@ void splitter(file_size_handler_t *handler, const char *output, unsigned long lo splitgroesse -= filesize; disc.filearray[disccount++] = handler->filearray[i]; } else { - rest[restcount++] = handler->filearray[i]; + rest.filearray[restcount++] = handler->filearray[i]; } } @@ -53,15 +45,15 @@ void splitter(file_size_handler_t *handler, const char *output, unsigned long lo disc.filearray = (file_size_t *) realloc(disc.filearray, disc.length * sizeof(file_size_t)); - writeOutputFile(output, disc); - onPrintEvent(output, splitgroesse); - memcpy(handler->filearray, rest, restcount * sizeof(file_size_t)); - handler->filearray = (file_size_t *) realloc(handler->filearray, - restcount * sizeof(file_size_t)); + memcpy(handler->filearray, rest.filearray, + restcount * sizeof(file_size_t)); handler->length = restcount; - free(disc.filearray); - free(rest); + handler->filearray = (file_size_t *) realloc(handler->filearray, + handler->length * sizeof(file_size_t)); + + free(rest.filearray); + return disc; } diff --git a/splitter.h b/splitter.h index a5779c4..da73a44 100644 --- a/splitter.h +++ b/splitter.h @@ -3,7 +3,7 @@ void (*printEvent)(const char *output, unsigned long long splitgroesse); -void splitter(file_size_handler_t *handler, const char *output, +file_size_handler_t splitter(file_size_handler_t *handler, const char *output, unsigned long long splitgroesse); #endif -- 2.39.5