]> gitweb.hhaalo.de Git - discspan.git/commitdiff
change: file write in filehandler module
authorBastian Dehn <hhaalo@arcor.de>
Sat, 31 Jul 2021 06:56:06 +0000 (08:56 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 31 Jul 2021 06:56:06 +0000 (08:56 +0200)
CMakeLists.txt
Makefile
discspan.c
filehandler.c [new file with mode: 0644]
filehandler.h [new file with mode: 0644]
readfile.c [deleted file]
readfile.h [deleted file]
splitter.c
splitter.h

index 187140ce9b66afbb4a438d1f97d03c2d1dde5624..3c6ebf525017a3bfc2a6e6fcd7f032ee390a19f4 100644 (file)
@@ -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)
index 4be349b89775a9fa7a33210e7bc10b2f7d87df1d..ebb14588d0b529ea33b4db16bdb54116c60ef50e 100644 (file)
--- 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
index 6e1d42ff2bd9f40e85f90ce2ceb2ad114b560110..b4fe792b1b28926965ccd13bc38aadd9b27bae6d 100644 (file)
@@ -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 (file)
index 0000000..fa7b0b6
--- /dev/null
@@ -0,0 +1,78 @@
+#include <sys/stat.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#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 (file)
index 0000000..a2c254f
--- /dev/null
@@ -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 (file)
index b303121..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-#include <sys/stat.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-#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 (file)
index 54c8483..0000000
+++ /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
index db225d5c96aca96fae8b1d9184d1d378f0b63580..10200a58788284901be653e4da27509c03e63ff9 100644 (file)
@@ -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;
 }
index a5779c426853ddae077fd88f75193964d25c93c7..da73a4431a3d1df98d04f44f6e7a0757b10aee77 100644 (file)
@@ -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