]> gitweb.hhaalo.de Git - discspan.git/commitdiff
change: split read file in seperate c file
authorBastian Dehn <hhaalo@arcor.de>
Mon, 6 Jan 2020 10:42:17 +0000 (11:42 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Mon, 6 Jan 2020 10:42:17 +0000 (11:42 +0100)
Makefile
discspan.c
discspan.h [new file with mode: 0644]
readfile.c [new file with mode: 0644]
readfile.h [new file with mode: 0644]

index df711dfda6453547ece292173d7059f2b11bc807..bcc9f91da0238fd829dd90fdb94999cac9683c2d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,11 +5,13 @@ BINPATH=/usr/bin
 
 all: discspan
 
-discspan: discspan.o help.o help.h
-       $(CC) $(CFLAGS) -o discspan discspan.o help.o
+discspan: discspan.o readfile.o readfile.h help.o help.h
+       $(CC) $(CFLAGS) -o discspan discspan.o readfile.o help.o
 
-discspan.o: discspan.c
+discspan.o: discspan.h discspan.c
        $(CC) $(CFLAGS) -c discspan.c
+readfile.o: discspan.h readfile.h readfile.c
+       $(CC) $(CFLAGS) -c readfile.c
 help.o: help.h help.c
        $(CC) $(CFLAGS) -c help.c
 
index adf2e8025b8642b52f3d45c57118491780d14650..2b30cd88b268022f74babb787633b627d2e79c56 100644 (file)
@@ -1,10 +1,11 @@
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
 #include <math.h>
 #include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
 
+#include "discspan.h"
+#include "readfile.h"
 #include "help.h"
 
 extern void useage();
@@ -12,14 +13,6 @@ extern void useage();
 int struct_array_length = 0;
 unsigned long long split = 0;
 
-typedef struct file_size {
-       char name[255];
-       long long unsigned fsize;
-} file_size;
-
-/**
- * Gibt die Groesse in einer lesbaren Darstellung aus
- */
 void printHumanReadSize(const char *output, unsigned long long splitgroesse)
 {
        unsigned long long humanread;
@@ -39,14 +32,6 @@ void printHumanReadSize(const char *output, unsigned long long splitgroesse)
                        splitgroesse, humanread, einheit[i]);
 }
 
-/**
- * Vergleicht zwei Dateien mit ihrer Groesse
- *
- * @param filea erste Datei
- * @param fileb zweite Datei
- *
- * @return integer
- */
 int cmpfunc(const void *filea, const void *fileb)
 {
        unsigned long long a = ((const struct file_size*)filea)->fsize;
@@ -62,66 +47,6 @@ int cmpfunc(const void *filea, const void *fileb)
                return -2;
 }
 
-/**
- * Ermittelt die Zeilen der Textdatei
- *
- * @param Datei pointer
- *
- * @return Anzahl von Zeilen
- */
-int get_array_length(FILE *in)
-{
-       int lines = 0;
-       char pfad[255];
-
-       while (fscanf(in, "%[^\n]\n", pfad) == 1) {
-               lines++;
-       }
-       rewind(in);
-
-       return lines;
-}
-
-/**
- * Liest die Datei in den Ram ein und gibt die Laenge des Arrays zurueck
- *
- * @param Datei pointer
- * @param file_size pointer
- * @param Splitgroesse
- *
- * @return Arraylaenge
- */
-int fill_array_from_file(FILE *in, struct file_size *fs,
-               const unsigned long long split)
-{
-       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(fs[lines].name, pfad);
-                       fs[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);
-
-       lines--;
-       return lines;
-}
-
 /**
  * Schreibt in eine Datei die Pfade der Dateien, die auf das Medium passen. Wenn
  * ein Rest uebrig bleibt wird das Array reallociert und die Restlange zurueck-
@@ -204,23 +129,6 @@ void readArgument(int argc, char *argv[])
        }
 }
 
-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 fs;
-}
-
 int main(int argc, char *argv[])
 {
        const char *input = argv[1];
diff --git a/discspan.h b/discspan.h
new file mode 100644 (file)
index 0000000..fdcaf79
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef _DISCSPAN_H
+#define _DISCSPAN_H
+
+typedef struct file_size {
+       char name[255];
+       long long unsigned fsize;
+} file_size;
+
+#endif
diff --git a/readfile.c b/readfile.c
new file mode 100644 (file)
index 0000000..c03bf60
--- /dev/null
@@ -0,0 +1,93 @@
+#include <sys/stat.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "readfile.h"
+
+extern int struct_array_length;
+extern unsigned long long split;
+extern struct file_size *fs;
+
+/**
+ * Ermittelt die Zeilen der Textdatei
+ *
+ * @param Datei pointer
+ *
+ * @return Anzahl von Zeilen
+ */
+int get_array_length(FILE *in)
+{
+       int lines = 0;
+       char pfad[255];
+
+       while (fscanf(in, "%[^\n]\n", pfad) == 1) {
+               lines++;
+       }
+       rewind(in);
+
+       return lines;
+}
+
+/**
+ * Liest die Datei in den Ram ein und gibt die Laenge des Arrays zurueck
+ *
+ * @param Datei pointer
+ * @param file_size pointer
+ * @param Splitgroesse
+ *
+ * @return Arraylaenge
+ */
+int fill_array_from_file(FILE *in, struct file_size *fs,
+               const unsigned long long split)
+{
+       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(fs[lines].name, pfad);
+                       fs[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);
+
+       lines--;
+       return lines;
+}
+
+/**
+ * Liest die Input Datei in den RAM ein
+ * 
+ * @input Pfad zur Textdatei mit dem Dateipfaden
+ *
+ * @return file_size struct array
+ */
+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 fs;
+}
diff --git a/readfile.h b/readfile.h
new file mode 100644 (file)
index 0000000..ec8dd5d
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef _READFILE_H
+#define _READFILE_H
+
+#include "stdio.h"
+
+#include "discspan.h"
+
+file_size * read_input_file(const char *input);
+
+#endif