From: Bastian Dehn Date: Sun, 1 Oct 2017 14:52:11 +0000 (+0200) Subject: change: refactore discspan X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=1aef1f6b611f7d2be31c5404ead98febbbc124ba;p=discspan.git change: refactore discspan --- diff --git a/discspan.c b/discspan.c index 8985a4d..ab4a99d 100644 --- a/discspan.c +++ b/discspan.c @@ -4,32 +4,40 @@ #include #include -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