From: Bastian Dehn Date: Sat, 31 Jul 2021 06:56:06 +0000 (+0200) Subject: change: file write in filehandler module X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=57aae332db84070a323b8bbc2ab9614fad33fe0a;p=discspan.git change: file write in filehandler module --- 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/filehandler.c b/filehandler.c new file mode 100644 index 0000000..fa7b0b6 --- /dev/null +++ b/filehandler.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include + +#include "discspan.h" +#include "filehandler.h" + +void malloc_array_length(FILE *in, file_size_handler_t *handler) +{ + int lines = 0; + char pfad[255]; + + while (fscanf(in, "%[^\n]\n", pfad) == 1) { + lines++; + } + rewind(in); + + handler->length = lines; + handler->filearray = (file_size_t *) + malloc(handler->length * sizeof(file_size_t)); +} + +void fill_array_from_file(FILE *in, const unsigned long long split, + file_size_handler_t *handler) +{ + FILE *ignore = NULL; + char pfad[255]; + struct stat st; + int lines = 0; + + while (fscanf(in, "%[^\n]\n", pfad) == 1) { + // Lese Dateieigenschaften in st struct + stat(pfad, &st); + + if (st.st_size <= split) { + strcpy(handler->filearray[lines].name, pfad); + handler->filearray[lines++].fsize = st.st_size; + } else { + // irgnore List erstellen + if (ignore == NULL) + ignore = fopen("ignore", "w"); + + fprintf(ignore, "%s\n", pfad); + } + } + + if (ignore != NULL) + fclose(ignore); + + handler->length = lines; + handler->filearray = (file_size_t *) + realloc(handler->filearray, + handler->length * sizeof(file_size_t)); +} + +void read_input_file(const char *input, + const unsigned long long split, + file_size_handler_t *handler) +{ + FILE *in = fopen(input, "r"); + + malloc_array_length(in, handler); + fill_array_from_file(in, split, handler); + + 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/filehandler.h b/filehandler.h new file mode 100644 index 0000000..a2c254f --- /dev/null +++ b/filehandler.h @@ -0,0 +1,10 @@ +#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/readfile.c b/readfile.c deleted file mode 100644 index b303121..0000000 --- a/readfile.c +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include - -#include "discspan.h" -#include "readfile.h" - -void malloc_array_length(FILE *in, file_size_handler_t *handler) -{ - int lines = 0; - char pfad[255]; - - while (fscanf(in, "%[^\n]\n", pfad) == 1) { - lines++; - } - rewind(in); - - handler->length = lines; - handler->filearray = (file_size_t *) - malloc(handler->length * sizeof(file_size_t)); -} - -void fill_array_from_file(FILE *in, const unsigned long long split, - file_size_handler_t *handler) -{ - FILE *ignore = NULL; - char pfad[255]; - struct stat st; - int lines = 0; - - while (fscanf(in, "%[^\n]\n", pfad) == 1) { - // Lese Dateieigenschaften in st struct - stat(pfad, &st); - - if (st.st_size <= split) { - strcpy(handler->filearray[lines].name, pfad); - handler->filearray[lines++].fsize = st.st_size; - } else { - // irgnore List erstellen - if (ignore == NULL) - ignore = fopen("ignore", "w"); - - fprintf(ignore, "%s\n", pfad); - } - } - - if (ignore != NULL) - fclose(ignore); - - handler->length = lines; - handler->filearray = (file_size_t *) - realloc(handler->filearray, - handler->length * sizeof(file_size_t)); -} - -void read_input_file(const char *input, - const unsigned long long split, - file_size_handler_t *handler) -{ - FILE *in = fopen(input, "r"); - - malloc_array_length(in, handler); - fill_array_from_file(in, split, handler); - - fclose(in); -} diff --git a/readfile.h b/readfile.h deleted file mode 100644 index 54c8483..0000000 --- a/readfile.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef _READFILE_H -#define _READFILE_H - -void read_input_file(const char *input, - const unsigned long long split, - file_size_handler_t *handler); - -#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