]> gitweb.hhaalo.de Git - discspan.git/commitdiff
change: refactore discspan
authorBastian Dehn <hhaalo@arcor.de>
Sun, 1 Oct 2017 14:52:11 +0000 (16:52 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 1 Oct 2017 14:52:11 +0000 (16:52 +0200)
discspan.c

index 8985a4daa31c82bbabdced8181c680af6da06747..ab4a99dff2ad7946a479fc261db435643b23effe 100644 (file)
@@ -4,32 +4,40 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-void filecopy(const char *fsource, const char *ftarget)
-{
+struct file_size {
+       char name[255];
+       long long unsigned int fsize;
+};
+
+void filecopy(const char *fsource,
+                               const char *ftarget) {
        FILE *source; FILE *target;
        char ch;
 
        source = fopen(fsource, "r");
        target = fopen(ftarget, "w");
 
-       while((ch = fgetc(source)) != EOF)
+       while((ch = fgetc(source)) != EOF) {
                fputc(ch, target);
+       }
 
        fclose(source);
        fclose(target);
 }
 
-int exists(const char *fname)
-{
+int exists(const char *fname) {
        struct stat buffer;
-       if (stat(fname, &buffer) == 0)
+
+       if (stat(fname, &buffer) == 0) {
                return 0;
-       else
+       } else {
                return 1;
+       }
 }
 
-void splitter(const char *input, char *output, const char *rest, unsigned long long splitgroesse)
-{
+void splitter(const char *input,
+                               char *output, const char *rest,
+                               unsigned long long splitgroesse) {
        struct stat st;
        char pfad[255];
        unsigned long long filesize;
@@ -42,72 +50,76 @@ void splitter(const char *input, char *output, const char *rest, unsigned long l
        in = fopen(input, "r");
        out = fopen(output, "w");
 
-       while (fscanf(in, "%[^\n]\n", pfad) == 1)
-       {
+       while (fscanf(in, "%[^\n]\n", pfad) == 1) {
                stat(pfad, &st);
+
                // filesize inklusive overhead
                filesize = 33 + strlen(pfad) + st.st_size;
-               if (splitgroesse >= filesize)
-               {
+
+               if (splitgroesse >= filesize) {
                        splitgroesse -= filesize;
                        fprintf(out, "%s\n", pfad);
-               }
-               else
-               {
-                       if (exists(rest) == 1)
+               } else {
+                       if (exists(rest) == 1) {
                                re = fopen(rest, "w");
+                       }
 
                        fprintf(re, "%s\n", pfad);
                }
        }
 
-       fclose(in); fclose(out);
-       if(exists(rest) == 0)
+       fclose(in);
+       fclose(out);
+
+       if (exists(rest) == 0) {
                fclose(re);
-       
+       }
+
        humanread = splitgroesse;
-       while (humanread >= 1024)
-       {
+
+       while (humanread >= 1024) {
                i++;
                humanread /= 1024;
-               switch (i)
-               {
+
+               switch (i) {
                        case 1:
                                einheit = 'K';
-                       break;
+                               break;
                        case 2:
                                einheit = 'M';
-                       break;
+                               break;
                        case 3:
                                einheit = 'G';
-                       break;
+                               break;
                }
        }
 
        printf("Restlicher Platz in Bytes(%s): %lld (%lld %c)\n", output, splitgroesse, humanread, einheit);
 }
 
-struct file_size
-{
-       char name[255];
-       long long unsigned int fsize;
-};
 
-int cmpfunc(const void * filea, const void * fileb)
-{
+
+int cmpfunc(const void * filea,
+                       const void * fileb) {
        long long unsigned int a = ((const struct file_size*)filea)->fsize;
        long long unsigned int b = ((const struct file_size*)fileb)->fsize;
-       if(a < b)
+
+       if (a < b) {
                return 1;
-       if(a > b)
+       } else if(a > b) {
                return -1;
-       if(a == b)
+       } else if(a == b) {
                return 0;
-       return -2;
+       } else {
+               return -2;
+       }
 }
 
-void filesize_sort(const char *input, const char *output, const long long unsigned int split)
-{
+void filesize_sort(const char *input,
+                                       const char *output,
+                                       const long long unsigned int split) {
+       // in: input Textdatei; out: output Textdatei;
+       // ignore: output Textdatei; zugrosse Dateien
        FILE *in; FILE *out; FILE *ignore;
        char pfad[255];
        struct stat st;
@@ -116,110 +128,125 @@ void filesize_sort(const char *input, const char *output, const long long unsign
        in = fopen(input, "r");
        out = fopen(output, "w");
 
-       while(fscanf(in, "%[^\n]\n", pfad) == 1)
-       {
-               lines++;                
+       // input-Datei Zeilenanzahl
+       while (fscanf(in, "%[^\n]\n", pfad) == 1) {
+               lines++;
        }
        fseek(in, 0L, SEEK_SET);
+
+       // struct Array with Dateienanzahl
        struct file_size fs[lines -1];
        lines = 0;
-       while(fscanf(in, "%[^\n]\n", pfad) == 1)
-       {
+
+       while (fscanf(in, "%[^\n]\n", pfad) == 1) {
+               // Lese Dateieigenschaften in st struct
                stat(pfad, &st);
-               if(st.st_size <= split)
-               {
+
+               if (st.st_size <= split) {
+                       // kopiert den pfad String ins struct
                        strcpy(fs[lines].name, pfad);
-                       fs[lines++].fsize = st.st_size;
-               }
-               else
-               {
-                       if(exists("ignore") == 1)
+                       // Filesize in struct
+                       fs[lines].fsize = st.st_size;
+                       lines++;
+               } else {
+                       // irgnore List erstellen
+                       if (exists("ignore") == 1) {
                                ignore = fopen("ignore", "w");
+                       }
+
                        fprintf(ignore, "%s\n", pfad);
                }
        }
+
+       // sortiere das array fs mit stucts
        qsort(fs, lines, sizeof(struct file_size), cmpfunc);
-       int i;
-       for(i = 0; i < lines; i++)
-       {
+
+       // schreibe in out die sortieren liste
+       for (int i = 0; i < lines; i++) {
                fprintf(out, "%s\n", fs[i].name);
        }
+
+       // schliesse alle Textdateien
        fclose(in);
        fclose(out);
-       if(exists("ignore") == 0)
+       if (exists("ignore") == 0) {
                fclose(ignore);
+       }
 }
 
-void usage()
-{      
+void usage() {
        printf("\nUsage: discspan <input> <output> <option>\n\n");
        printf(" <input>\tTextdatei enthält alle Dateipfade, die auf den Datenträger sollen\n");
        printf(" <output>\tTextdatei enthält alle Dateipfade, die auf den Datenträger passen\n\n");
        printf(" option:\n");
        printf("   -9\t\tEntspricht der Größe einer Double Layer DVD: 8.497.902.848 Bytes\n");
        printf("   -5\t\tEntspricht der Größe einer Single Layer DVD: 4.697.902.848 Bytes\n");
-       printf("   -c\t\tEntspricht der Größe einer CD: 734.003.200 Bytes\n"); 
+       printf("   -c\t\tEntspricht der Größe einer CD: 734.003.200 Bytes\n");
        printf("   -f <Größe>\tOption zur Größenangabe in Bytes\n\n");
 }
 
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
+       // input: Textdatei mit Quellpfaden
        const char *input = argv[1];
+       // output: gesplittete Textdatei mit Quellpfaden
        const char *output = argv[2];
+       // rest: temp restliche  Datein,
+       // die nicht aufs Medium passen
        const char *rest = "rest";
+       // work: temp Datein,
+       // die auf das Medium passen
        const char *work = "work";
 
-       char outname[sizeof output] = "";
-       int num = 1;
-
-       unsigned long long split;
-       int c;
-       extern char *optarg;
-       // Konstanten aus dat.c bzw. dat.h
+       // Medien groessen
        const unsigned long long dvd9 = 8497902848;
        const unsigned long long dvd5 = 4697902848;
        const unsigned long long cd = 734003200;
+       // Default Wert dvd5
+       unsigned long long split = dvd5;
 
-       if(argc <= 3)
-       {
+       // gib die Funktionsübersicht aus bei zu wenig Parameter
+       if (argc <= 3) {
                usage();
                return 1;
        }
 
-       while ((c = getopt(argc, argv, ":59bdcf:")) != -1)
-       {
-               switch(c)
-               {
+       // Lese Parameter ein
+       int c;
+       while ((c = getopt(argc, argv, ":59bdcf:")) != -1) {
+               switch(c) {
                        case '5':
                                split = dvd5;
-                       break;
+                               break;
                        case '9':
                                split = dvd9;
-                       break;
+                               break;
                        case 'c':
                                split = cd;
-                       break;
+                               break;
                        case 'f':
                                split = atoll(optarg);
-                       break;
+                               break;
                        case '?':
                                usage();
                                return 1;
-                       break;
+                               break;
                }
        }
 
+       // schreibt eine soritere Liste in rest
        filesize_sort(input, rest, split);
-       while(exists(rest) == 0)
-       {
-               sprintf(outname, "%s%03d",output , num++);
-               if(exists(rest) == 0)
-               {
+
+       char outname[sizeof output] = "";
+       int num = 1;
+       while (exists(rest) == 0) {
+               sprintf(outname, "%s%03d", output , num++);
+               if (exists(rest) == 0) {
                        filecopy(rest, work);
                        remove(rest);
                }
                splitter(work, outname, rest, split);
-       } 
+       }
+
        remove(work);
 
        return 0;