From 018fca76f9642279860c55ca847e1cabf9788206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BE=D0=BB=D0=B5=D0=B3=20=D0=B1=D0=B0=D1=80=D0=BA=D0=B0?= =?UTF-8?q?=D0=BB=D0=BE=D0=B2?= Date: Fri, 24 Sep 2021 09:15:21 +0300 Subject: [PATCH 01/10] Added first LAB by Oleg Barkalov M3108 --- lab1/lab1.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lab1/lab1.c diff --git a/lab1/lab1.c b/lab1/lab1.c new file mode 100644 index 0000000..e9969c8 --- /dev/null +++ b/lab1/lab1.c @@ -0,0 +1,73 @@ +#include +#include +//#include + +int main(int argc, char *argv[]) { + + int i = 0, outValue = 0, isWord = 0; // i - value to run on the lines + char option[15], path[100]; + + + strcpy(option, argv[1]); + strcpy(path, argv[2]); + + FILE* ftext = fopen(path,"r"); + + if (strcmp(option, "-l") == 0 | strcmp(option, "--lines") == 0){ + printf("%d",linesCounter(ftext)+1); // ---------------------------------> count of lines (+1 - cause need to count last line) + } + if (strcmp(option, "-c" ) == 0 | strcmp(option, "--bytes") == 0){ + printf("%d",bytesCounter(ftext, path)); // ---------------------------------> count of filesize + } + if (strcmp(option, "-w") == 0 | strcmp(option, "--words") == 0){ + printf("%d",wordsCounter(ftext)); // ---------------------------------> count of words + } + + fclose(ftext); + +} + +int linesCounter(FILE* f){ // f - active file + int outVal=0; + char c; + while ((c = getc(f)) != EOF) { + if (c=='\n') + outVal++; + + } + return outVal; + +} + +int wordsCounter(FILE* f){ // f - active file + int outVal = 0,isWord = 0; + char c = getc(f); + while ((c == ' ') & (c != '\0')) + c = getc(f); + + while ((c = getc(f)) != EOF) { + + //isWord = 0; // boolean value (0 - false/1 - true) + if ((c != ' ') & (isWord == 0)){ + outVal++; + isWord = 1; + } + else if (c == ' ' | c == '\n') + isWord = 0; + } + return outVal; + } + + int bytesCounter(FILE* f, char* p){ // f - active file, p - path + int outVal; + f = fopen(p,"rb"); + + fseek(f, 0, SEEK_END); + outVal = ftell(f); + fseek(f, 0, SEEK_SET); + + fclose(f); + + return outVal; + + } \ No newline at end of file From 98f07bf697a870cc5e5bae31bc9c5d0501689f5c Mon Sep 17 00:00:00 2001 From: Haloz Date: Mon, 27 Sep 2021 09:22:05 +0300 Subject: [PATCH 02/10] fixed --- lab1/lab1.c | 72 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/lab1/lab1.c b/lab1/lab1.c index e9969c8..91f8f8e 100644 --- a/lab1/lab1.c +++ b/lab1/lab1.c @@ -4,70 +4,90 @@ int main(int argc, char *argv[]) { - int i = 0, outValue = 0, isWord = 0; // i - value to run on the lines - char option[15], path[100]; + int i = 0; // i - value to run on the lines + char option[15], path[255]; + if (argc != 3){ + printf("ERROR, insufficient number of parameters"); + return -1; + } strcpy(option, argv[1]); strcpy(path, argv[2]); - FILE* ftext = fopen(path,"r"); + if (fileIsExist(path) == 0){ + printf("ERROR, cannot find a file"); + return -2; + } - if (strcmp(option, "-l") == 0 | strcmp(option, "--lines") == 0){ - printf("%d",linesCounter(ftext)+1); // ---------------------------------> count of lines (+1 - cause need to count last line) + if (strcmp(option, "-l") == 0 || strcmp(option, "--lines") == 0){ + printf("%d",linesCounter(path)+1); // ---------------------------------> count of lines (+1 - cause need to count last line) } - if (strcmp(option, "-c" ) == 0 | strcmp(option, "--bytes") == 0){ - printf("%d",bytesCounter(ftext, path)); // ---------------------------------> count of filesize + if (strcmp(option, "-c" ) == 0 || strcmp(option, "--bytes") == 0){ + printf("%d",bytesCounter(path)); // ---------------------------------> count of filesize } - if (strcmp(option, "-w") == 0 | strcmp(option, "--words") == 0){ - printf("%d",wordsCounter(ftext)); // ---------------------------------> count of words + if (strcmp(option, "-w") == 0 || strcmp(option, "--words") == 0){ + printf("%d",wordsCounter(path)); // ---------------------------------> count of words } - fclose(ftext); - + return 0; } -int linesCounter(FILE* f){ // f - active file +int linesCounter(char* p){ // p - path int outVal=0; char c; + FILE* f = fopen(p,"r"); // f - active file while ((c = getc(f)) != EOF) { if (c=='\n') outVal++; } + fclose(f); return outVal; } -int wordsCounter(FILE* f){ // f - active file +int wordsCounter(char* p){ // p - path int outVal = 0,isWord = 0; - char c = getc(f); - while ((c == ' ') & (c != '\0')) + FILE* f = fopen(p,"r"); // f - active file + + char c = getc(f); + while ((c == ' ') && (c != '\0')) c = getc(f); while ((c = getc(f)) != EOF) { //isWord = 0; // boolean value (0 - false/1 - true) - if ((c != ' ') & (isWord == 0)){ + if ((c != ' ') && (c != '\n') && (c != '\t') && (isWord == 0)){ outVal++; isWord = 1; } - else if (c == ' ' | c == '\n') + else if (c == ' ' || c == '\n') isWord = 0; } + + fclose(f); return outVal; } - int bytesCounter(FILE* f, char* p){ // f - active file, p - path - int outVal; - f = fopen(p,"rb"); +int bytesCounter(char* p){ // f - active file, p - path + int outVal; + FILE* f = fopen(p,"rb"); + + fseek(f, 0, SEEK_END); + outVal = ftell(f); - fseek(f, 0, SEEK_END); - outVal = ftell(f); - fseek(f, 0, SEEK_SET); + fclose(f); - fclose(f); + return outVal; + + } - return outVal; +int fileIsExist(char* p){ // - path + FILE* activeFile = fopen(p,"r"); + if (activeFile == NULL) + return 0; + else + return 1; - } \ No newline at end of file +} \ No newline at end of file From 1a917e73fedf67254884c7f6b021f47c6a43a0be Mon Sep 17 00:00:00 2001 From: Haloz Date: Sat, 9 Oct 2021 11:36:40 +0300 Subject: [PATCH 03/10] add some fix --- lab1/lab1.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lab1/lab1.c b/lab1/lab1.c index 91f8f8e..016f0ca 100644 --- a/lab1/lab1.c +++ b/lab1/lab1.c @@ -4,30 +4,27 @@ int main(int argc, char *argv[]) { - int i = 0; // i - value to run on the lines - char option[15], path[255]; - if (argc != 3){ printf("ERROR, insufficient number of parameters"); return -1; } - strcpy(option, argv[1]); - strcpy(path, argv[2]); + //argv[1] is option + //argv[2] is path - if (fileIsExist(path) == 0){ + if (fileIsExist(argv[2]) == 0){ printf("ERROR, cannot find a file"); return -2; } - if (strcmp(option, "-l") == 0 || strcmp(option, "--lines") == 0){ - printf("%d",linesCounter(path)+1); // ---------------------------------> count of lines (+1 - cause need to count last line) + if (strcmp(argv[1], "-l") == 0 || strcmp(argv[1], "--lines") == 0){ + printf("%d",linesCounter(argv[2])+1); // ---------------------------------> count of lines (+1 - cause need to count last line) } - if (strcmp(option, "-c" ) == 0 || strcmp(option, "--bytes") == 0){ - printf("%d",bytesCounter(path)); // ---------------------------------> count of filesize + if (strcmp(argv[1], "-c" ) == 0 || strcmp(argv[1], "--bytes") == 0){ + printf("%d",bytesCounter(argv[2])); // ---------------------------------> count of filesize } - if (strcmp(option, "-w") == 0 || strcmp(option, "--words") == 0){ - printf("%d",wordsCounter(path)); // ---------------------------------> count of words + if (strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--words") == 0){ + printf("%d",wordsCounter(argv[2])); // ---------------------------------> count of words } return 0; @@ -82,7 +79,7 @@ int bytesCounter(char* p){ // f - active file, p - path return outVal; } - +/* int fileIsExist(char* p){ // - path FILE* activeFile = fopen(p,"r"); if (activeFile == NULL) @@ -90,4 +87,4 @@ int fileIsExist(char* p){ // - path else return 1; -} \ No newline at end of file +}*/ \ No newline at end of file From 6e144e25bfd0ccf3c736e2876b85dd7770ceaa4a Mon Sep 17 00:00:00 2001 From: Haloz Date: Sat, 9 Oct 2021 11:51:57 +0300 Subject: [PATCH 04/10] FINAL FIX --- lab1/lab1.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/lab1/lab1.c b/lab1/lab1.c index 016f0ca..cd0cbf2 100644 --- a/lab1/lab1.c +++ b/lab1/lab1.c @@ -12,20 +12,19 @@ int main(int argc, char *argv[]) { //argv[1] is option //argv[2] is path - if (fileIsExist(argv[2]) == 0){ + if (DoesFileExist(argv[2]) == 0){ printf("ERROR, cannot find a file"); return -2; } - if (strcmp(argv[1], "-l") == 0 || strcmp(argv[1], "--lines") == 0){ - printf("%d",linesCounter(argv[2])+1); // ---------------------------------> count of lines (+1 - cause need to count last line) - } - if (strcmp(argv[1], "-c" ) == 0 || strcmp(argv[1], "--bytes") == 0){ + if (strcmp(argv[1], "-l") == 0 || strcmp(argv[1], "--lines") == 0) + printf("%d",linesCounter(argv[2])); // ---------------------------------> count of lines + else if (strcmp(argv[1], "-c" ) == 0 || strcmp(argv[1], "--bytes") == 0) printf("%d",bytesCounter(argv[2])); // ---------------------------------> count of filesize - } - if (strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--words") == 0){ + else if (strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--words") == 0) printf("%d",wordsCounter(argv[2])); // ---------------------------------> count of words - } + else + printf("ERROR, incorrect command"); return 0; } @@ -40,7 +39,7 @@ int linesCounter(char* p){ // p - path } fclose(f); - return outVal; + return outVal + 1; // +1 - cause need to count last line } @@ -63,9 +62,9 @@ int wordsCounter(char* p){ // p - path isWord = 0; } - fclose(f); - return outVal; - } + fclose(f); + return outVal; +} int bytesCounter(char* p){ // f - active file, p - path int outVal; @@ -78,13 +77,13 @@ int bytesCounter(char* p){ // f - active file, p - path return outVal; - } -/* -int fileIsExist(char* p){ // - path +} + +int DoesFileExist(char* p){ // - path FILE* activeFile = fopen(p,"r"); if (activeFile == NULL) return 0; else return 1; -}*/ \ No newline at end of file +} \ No newline at end of file From 55ea1521922e890bd8fe5d869bd51b72d57515f4 Mon Sep 17 00:00:00 2001 From: Ha1oz Date: Fri, 7 Jan 2022 13:28:40 +0300 Subject: [PATCH 05/10] added lab3.c --- lab3/lab3.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 lab3/lab3.c diff --git a/lab3/lab3.c b/lab3/lab3.c new file mode 100644 index 0000000..780b61e --- /dev/null +++ b/lab3/lab3.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include + + +#define SIZE 300//размер строки запроса +long timestamp[1891714]; int n = 0; + + +long utime(char *stime); +long get_time(char[]); +int get_error(char[]); +void get_window(int); + + +int main (int argc, char* argv[]) { + + FILE* f = fopen("access_log_Jul95.txt", "r"); + + int errorCount = 0; + int wsize = atoi(argv[1]); + + char str[SIZE]; + + if (argc != 2) { + printf("ERROR: not enough arguments"); + return -1; + } + + + while (fgets(str, SIZE, f)) { + timestamp[n++] = get_time(str); + if ((get_error(str) / 100) == 5){ + ++errorCount; + } + } + + get_window(wsize); + + + printf("\n5XX errors: %d\n", errorCount); + + return 0; +} + + +void get_window(int width) { + long m = 0;//макс. окно + long mp1 = 0; long mp2 = 0;//индексы границ макс. окна + long p1 = 0; long p2 = 0;//индексы границ текущего окна + while (p2 < n - 1) { + + if (timestamp[p2] - timestamp[p1] <= width) { + ++p2; + } else { + if (p2 - p1 > m) { + m = p2 - p1; + mp1 = p1 + 1; + mp2 = p2; + } + p1++; + } + } + if ((timestamp[p2] - timestamp[p1] <= width) && (p2 - p1 > m)) { + m = p2 - p1 + 1; + mp1 = p1 + 1; + mp2 = p2 + 1; + } + printf("count = %d, from %d to %d", m, mp1, mp2); +} + +long get_time(char str[]) { + + char buff[40];//хранит строку со временем + int ptr = 0;//индекс для времени + int i = 0;//индекс строки + while (str[i++] != '['); + + while (str[i] != ']') + buff[ptr++] = str[i++]; + + return utime(buff); +} + + +int get_error(char str[]) { + char buff[4];//хранит код ошибки + int ptr = 2;//индекс для кода ошибки + int i = strlen(str) - 1;//индекс строки + while (str[i--] != ' '); + + while (str[i] != ' ') + buff[ptr--] = str[i--]; + return atof(buff); +} + + +long utime(char* stime) { + char time_format[10] = "//::: ";//разделители строки времени + + char buff[7][6] = {""};//хранит разбиение строки stime + int i = 0;//индекс строки stime + int bp = 0; int wp = 0;//индексы buff + int fp = 0; //индекс time_format + while (stime[i]) { + if (stime[i] == time_format[fp]) { + ++bp; + ++fp; + wp = 0; + } else + buff[bp][wp++] = stime[i]; + ++i; + } + + struct tm tdate = { + .tm_mday = atof(buff[0]), + .tm_mon = atof(buff[1]) - 1, + .tm_year = atof(buff[2]) - 1900, + .tm_hour = atof(buff[3]), + .tm_min = atof(buff[4]), + .tm_sec = atof(buff[5]), + .tm_isdst = 0 + }; + + time_t time = mktime(&tdate); + return time; +} \ No newline at end of file From 62b0bad17767444fdad12b93b27d2d5834f66a56 Mon Sep 17 00:00:00 2001 From: Ha1oz Date: Fri, 7 Jan 2022 13:36:48 +0300 Subject: [PATCH 06/10] updated lab3.c --- lab3/lab3.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lab3/lab3.c b/lab3/lab3.c index 780b61e..f6504f9 100644 --- a/lab3/lab3.c +++ b/lab3/lab3.c @@ -3,7 +3,6 @@ #include #include - #define SIZE 300//размер строки запроса long timestamp[1891714]; int n = 0; @@ -16,14 +15,14 @@ void get_window(int); int main (int argc, char* argv[]) { - FILE* f = fopen("access_log_Jul95.txt", "r"); - + //FILE* f = fopen("access_log_Jul95.txt", "r"); + + FILE* f = fopen(argv[1], "r"); int errorCount = 0; - int wsize = atoi(argv[1]); - + int wsize = atoi(argv[2]); char str[SIZE]; - if (argc != 2) { + if (argc != 3) { printf("ERROR: not enough arguments"); return -1; } From 306525609b169ce5e710f3aaedad73e8c67e6315 Mon Sep 17 00:00:00 2001 From: Ha1oz Date: Fri, 7 Jan 2022 14:18:09 +0300 Subject: [PATCH 07/10] added lab6.c --- lab6/lab6.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 lab6/lab6.c diff --git a/lab6/lab6.c b/lab6/lab6.c new file mode 100644 index 0000000..2859afa --- /dev/null +++ b/lab6/lab6.c @@ -0,0 +1,126 @@ +#include +#include +#include + + +char *get_file_name(char *path) { + char *name; + int len = strlen(path); + int i = len - 1; + while (i >= 0 && path[i] != '/' && path[i] != '\\') + i--; + + name = (char *) malloc(len - i); + name[len - i - 1] = '\0'; + memcpy(name, path + i + 1, len - 1 - i); + return name; +} + +int get_size(FILE *f){ + fseek(f, 0L, SEEK_END); + int size= ftell(f); + fseek(f, 0L, SEEK_SET); + return size; +} + + +void get_archive(char *name) { + FILE *fin, *fout; + char n, nsize; + int fsize; + char *tname, *text; + + fin = fopen(name, "rb"); + fread(&n, 1, 1, fin); + + for (int i = 0; i < n; i++) { + fread(&nsize, 1, 1, fin); + fread(&fsize, 4, 1, fin); + + tname = (char *) malloc(nsize + 1); + tname[nsize] = '\0'; + fread(tname, 1, nsize, fin); + + fout = fopen(tname, "wb"); + + text = (char *) malloc(fsize); + fread(text, 1, fsize, fin); + + fwrite(text, 1, fsize, fout); + fclose(fout); + free(tname); + free(text); + } +} + +void list(char *name) { + FILE *fin; + char n, nsize; + int fsize; + char *tname, *text; + + fin = fopen(name, "rb"); + fread(&n, 1, 1, fin); + + for (int i = 0; i < n; i++) { + fread(&nsize, 1, 1, fin); + fread(&fsize, 4, 1, fin); + + tname = (char *) malloc(nsize + 1); + tname[nsize] = '\0'; + fread(tname, 1, nsize, fin); + + fseek(fin, fsize, SEEK_CUR); + printf("%s ", tname); + free(tname); + } +} + +void pack(int argc, char *argv[], char *name) { + char n, nsize; + int fsize; + char *tname, *text; + FILE *fout, *fin; + + fout = fopen(name, "wb"); + n = argc - 4; + fputc(n, fout); + + for (int i = 4; i < argc; i++) { + fin = fopen(argv[i], "rb"); + tname = get_file_name(argv[i]); + + nsize = strlen(tname); + fsize = get_size(fin); + fputc(nsize, fout); + fwrite(&fsize, sizeof(int), 1, fout); + fwrite(tname, sizeof(char), nsize, fout); + + text = (char*) malloc(fsize); + fread(text, 1, fsize, fin); + fwrite(text, 1, fsize, fout); + fclose(fin); + free(text); + } + fclose(fout); +} + + + +int main(int argc, char *argv[]) { + char filename[100]; + + // filename = argv[2] + + if (!strcmp("--create", argv[3])) { + pack(argc, argv, argv[2]); + } else if (!strcmp("--extract", argv[3])) { + get_archive(argv[2]); + } else if (!strcmp("--list", argv[3])) { + list(argv[2]); + } else { + printf("ERROR: unknown command"); + return -1; + } + return 0; +} \ No newline at end of file From 68973886be2c5611f15c32ae2ccf41190dfc5933 Mon Sep 17 00:00:00 2001 From: Ha1oz Date: Sat, 15 Jan 2022 13:33:03 +0300 Subject: [PATCH 08/10] added lab2, deleted useless files --- lab1/.gitkeep | 0 lab1/lab1.c | 89 ------------------------------ lab2/lab2.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++ lab3/.gitkeep | 0 lab3/lab3.c | 127 ------------------------------------------- lab4/.gitkeep | 0 lab5/.gitkeep | 0 lab6/.gitkeep | 0 lab6/lab6.c | 126 ------------------------------------------- 9 files changed, 146 insertions(+), 342 deletions(-) delete mode 100644 lab1/.gitkeep delete mode 100644 lab1/lab1.c create mode 100644 lab2/lab2.c delete mode 100644 lab3/.gitkeep delete mode 100644 lab3/lab3.c delete mode 100644 lab4/.gitkeep delete mode 100644 lab5/.gitkeep delete mode 100644 lab6/.gitkeep delete mode 100644 lab6/lab6.c diff --git a/lab1/.gitkeep b/lab1/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/lab1/lab1.c b/lab1/lab1.c deleted file mode 100644 index cd0cbf2..0000000 --- a/lab1/lab1.c +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include -//#include - -int main(int argc, char *argv[]) { - - if (argc != 3){ - printf("ERROR, insufficient number of parameters"); - return -1; - } - - //argv[1] is option - //argv[2] is path - - if (DoesFileExist(argv[2]) == 0){ - printf("ERROR, cannot find a file"); - return -2; - } - - if (strcmp(argv[1], "-l") == 0 || strcmp(argv[1], "--lines") == 0) - printf("%d",linesCounter(argv[2])); // ---------------------------------> count of lines - else if (strcmp(argv[1], "-c" ) == 0 || strcmp(argv[1], "--bytes") == 0) - printf("%d",bytesCounter(argv[2])); // ---------------------------------> count of filesize - else if (strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--words") == 0) - printf("%d",wordsCounter(argv[2])); // ---------------------------------> count of words - else - printf("ERROR, incorrect command"); - - return 0; -} - -int linesCounter(char* p){ // p - path - int outVal=0; - char c; - FILE* f = fopen(p,"r"); // f - active file - while ((c = getc(f)) != EOF) { - if (c=='\n') - outVal++; - - } - fclose(f); - return outVal + 1; // +1 - cause need to count last line - -} - -int wordsCounter(char* p){ // p - path - int outVal = 0,isWord = 0; - FILE* f = fopen(p,"r"); // f - active file - - char c = getc(f); - while ((c == ' ') && (c != '\0')) - c = getc(f); - - while ((c = getc(f)) != EOF) { - - //isWord = 0; // boolean value (0 - false/1 - true) - if ((c != ' ') && (c != '\n') && (c != '\t') && (isWord == 0)){ - outVal++; - isWord = 1; - } - else if (c == ' ' || c == '\n') - isWord = 0; - } - - fclose(f); - return outVal; -} - -int bytesCounter(char* p){ // f - active file, p - path - int outVal; - FILE* f = fopen(p,"rb"); - - fseek(f, 0, SEEK_END); - outVal = ftell(f); - - fclose(f); - - return outVal; - -} - -int DoesFileExist(char* p){ // - path - FILE* activeFile = fopen(p,"r"); - if (activeFile == NULL) - return 0; - else - return 1; - -} \ No newline at end of file diff --git a/lab2/lab2.c b/lab2/lab2.c new file mode 100644 index 0000000..df9cf38 --- /dev/null +++ b/lab2/lab2.c @@ -0,0 +1,146 @@ +#define _CRT_SECURE_NO_WARNINGS + +#include +#include +#include +#include + +#define uint1024 unsigned long* +#define FIELD_SIZE 9//ширина поля вывода в printf_value() +#define SIZE 35//размер массива long 310/9 +#define SIZE_CHAR 310//размер строки в scanf_val() длина str(2**1024) + +unsigned long long max = 1000000000;//макс. значение ячеек массива + + +uint1024 init(); +uint1024 from_uint(unsigned int); +uint1024 add_op(uint1024, uint1024); +uint1024 subtr_op(uint1024, uint1024); +void printf_value(uint1024); +void prt(unsigned long, int);//вспом. функция для printf_value() +uint1024 multp_op(uint1024, uint1024); +void scanf_val(uint1024*); + + +int main() { + uint1024 a = init(); + uint1024 b = init(); + uint1024 c = from_uint(1); + + scanf_val(&a); + scanf_val(&b); + + + + printf("--OUTPUT--\n"); + printf_value(add_op(a, b)); + printf("\n"); + + printf_value(subtr_op(a, b)); + printf("\n"); + + printf_value(multp_op(a, b)); + return 0; +} + + +void scanf_val(uint1024 *a) { + char str[SIZE_CHAR]; + int k = 0; + int i = 0; + for (i; i < SIZE_CHAR; ++i) { + str[i] = '\0'; + } + + scanf("%s", &str); + + while(str[i] == '\0' ) {i -= 1;} + + while (i >= 0) { + *(*a + (k / FIELD_SIZE)) += (str[i] - '0') * pow(10, k % FIELD_SIZE); + ++k; + --i; + } +} + +void prt(unsigned long x, int i) { // вывод одной цифры + if (i == FIELD_SIZE) + return ; + prt(x / 10, ++i); + printf("%c", x % 10 + '0'); +} + +void printf_value(uint1024 x) { + int i = SIZE - 1; + for (i; *(x + i) == 0; --i) ; + + printf("%d", *(x + i--)); + + for (i;i >= 0; --i) + prt(*(x + i), 0); +} + +uint1024 init() { + uint1024 buff = malloc(SIZE * sizeof(unsigned long)); + for (int i = 0; i < SIZE; ++i) + *(buff + i) = 0; + return buff; +} + +uint1024 from_uint(unsigned int x) { + uint1024 buff = init(); + int i = 0; + while (x != 0) { + *(buff + i) = x % max; + x /= max; + i++; + } + return buff; +} + +uint1024 add_op(uint1024 a, uint1024 b) { + uint1024 buff = init(); + int o = 0; + unsigned long long sm; + for (int i = 0; i < SIZE; ++i) { + sm = *(a + i) + *(b + i) + o; + *(buff + i)= sm % max; + o = sm / max; + } + return buff; +} + +uint1024 subtr_op(uint1024 a, uint1024 b) { + uint1024 buff = init(); + signed long long o = 0; + signed long long sm; + for (int i = 0; i < SIZE; ++i) { + sm = (long long )*(a + i) - (long long) * (b + i) - o; + + if (sm >= 0) { + *(buff + i) = sm; + o = 0; + } + else { + *(buff + i) = max + sm; + o = 1; + } + } + return buff; +} + +uint1024 multp_op(uint1024 a, uint1024 b) { + uint1024 buff = init(); + unsigned long long pr_buff = 0; + unsigned long long o = 0; + + for (int pa = 0; pa < SIZE; ++pa) { + for (int pb = 0; pa + pb < SIZE; ++pb) { + pr_buff = ((long long)*(a + pa)) * ((long long)*(b + pb)) + o; + buff[pa + pb] += pr_buff % max; + o = pr_buff / max; + } + } + return buff; +} \ No newline at end of file diff --git a/lab3/.gitkeep b/lab3/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/lab3/lab3.c b/lab3/lab3.c deleted file mode 100644 index f6504f9..0000000 --- a/lab3/lab3.c +++ /dev/null @@ -1,127 +0,0 @@ -#include -#include -#include -#include - -#define SIZE 300//размер строки запроса -long timestamp[1891714]; int n = 0; - - -long utime(char *stime); -long get_time(char[]); -int get_error(char[]); -void get_window(int); - - -int main (int argc, char* argv[]) { - - //FILE* f = fopen("access_log_Jul95.txt", "r"); - - FILE* f = fopen(argv[1], "r"); - int errorCount = 0; - int wsize = atoi(argv[2]); - char str[SIZE]; - - if (argc != 3) { - printf("ERROR: not enough arguments"); - return -1; - } - - - while (fgets(str, SIZE, f)) { - timestamp[n++] = get_time(str); - if ((get_error(str) / 100) == 5){ - ++errorCount; - } - } - - get_window(wsize); - - - printf("\n5XX errors: %d\n", errorCount); - - return 0; -} - - -void get_window(int width) { - long m = 0;//макс. окно - long mp1 = 0; long mp2 = 0;//индексы границ макс. окна - long p1 = 0; long p2 = 0;//индексы границ текущего окна - while (p2 < n - 1) { - - if (timestamp[p2] - timestamp[p1] <= width) { - ++p2; - } else { - if (p2 - p1 > m) { - m = p2 - p1; - mp1 = p1 + 1; - mp2 = p2; - } - p1++; - } - } - if ((timestamp[p2] - timestamp[p1] <= width) && (p2 - p1 > m)) { - m = p2 - p1 + 1; - mp1 = p1 + 1; - mp2 = p2 + 1; - } - printf("count = %d, from %d to %d", m, mp1, mp2); -} - -long get_time(char str[]) { - - char buff[40];//хранит строку со временем - int ptr = 0;//индекс для времени - int i = 0;//индекс строки - while (str[i++] != '['); - - while (str[i] != ']') - buff[ptr++] = str[i++]; - - return utime(buff); -} - - -int get_error(char str[]) { - char buff[4];//хранит код ошибки - int ptr = 2;//индекс для кода ошибки - int i = strlen(str) - 1;//индекс строки - while (str[i--] != ' '); - - while (str[i] != ' ') - buff[ptr--] = str[i--]; - return atof(buff); -} - - -long utime(char* stime) { - char time_format[10] = "//::: ";//разделители строки времени - - char buff[7][6] = {""};//хранит разбиение строки stime - int i = 0;//индекс строки stime - int bp = 0; int wp = 0;//индексы buff - int fp = 0; //индекс time_format - while (stime[i]) { - if (stime[i] == time_format[fp]) { - ++bp; - ++fp; - wp = 0; - } else - buff[bp][wp++] = stime[i]; - ++i; - } - - struct tm tdate = { - .tm_mday = atof(buff[0]), - .tm_mon = atof(buff[1]) - 1, - .tm_year = atof(buff[2]) - 1900, - .tm_hour = atof(buff[3]), - .tm_min = atof(buff[4]), - .tm_sec = atof(buff[5]), - .tm_isdst = 0 - }; - - time_t time = mktime(&tdate); - return time; -} \ No newline at end of file diff --git a/lab4/.gitkeep b/lab4/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/lab5/.gitkeep b/lab5/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/lab6/.gitkeep b/lab6/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/lab6/lab6.c b/lab6/lab6.c deleted file mode 100644 index 2859afa..0000000 --- a/lab6/lab6.c +++ /dev/null @@ -1,126 +0,0 @@ -#include -#include -#include - - -char *get_file_name(char *path) { - char *name; - int len = strlen(path); - int i = len - 1; - while (i >= 0 && path[i] != '/' && path[i] != '\\') - i--; - - name = (char *) malloc(len - i); - name[len - i - 1] = '\0'; - memcpy(name, path + i + 1, len - 1 - i); - return name; -} - -int get_size(FILE *f){ - fseek(f, 0L, SEEK_END); - int size= ftell(f); - fseek(f, 0L, SEEK_SET); - return size; -} - - -void get_archive(char *name) { - FILE *fin, *fout; - char n, nsize; - int fsize; - char *tname, *text; - - fin = fopen(name, "rb"); - fread(&n, 1, 1, fin); - - for (int i = 0; i < n; i++) { - fread(&nsize, 1, 1, fin); - fread(&fsize, 4, 1, fin); - - tname = (char *) malloc(nsize + 1); - tname[nsize] = '\0'; - fread(tname, 1, nsize, fin); - - fout = fopen(tname, "wb"); - - text = (char *) malloc(fsize); - fread(text, 1, fsize, fin); - - fwrite(text, 1, fsize, fout); - fclose(fout); - free(tname); - free(text); - } -} - -void list(char *name) { - FILE *fin; - char n, nsize; - int fsize; - char *tname, *text; - - fin = fopen(name, "rb"); - fread(&n, 1, 1, fin); - - for (int i = 0; i < n; i++) { - fread(&nsize, 1, 1, fin); - fread(&fsize, 4, 1, fin); - - tname = (char *) malloc(nsize + 1); - tname[nsize] = '\0'; - fread(tname, 1, nsize, fin); - - fseek(fin, fsize, SEEK_CUR); - printf("%s ", tname); - free(tname); - } -} - -void pack(int argc, char *argv[], char *name) { - char n, nsize; - int fsize; - char *tname, *text; - FILE *fout, *fin; - - fout = fopen(name, "wb"); - n = argc - 4; - fputc(n, fout); - - for (int i = 4; i < argc; i++) { - fin = fopen(argv[i], "rb"); - tname = get_file_name(argv[i]); - - nsize = strlen(tname); - fsize = get_size(fin); - fputc(nsize, fout); - fwrite(&fsize, sizeof(int), 1, fout); - fwrite(tname, sizeof(char), nsize, fout); - - text = (char*) malloc(fsize); - fread(text, 1, fsize, fin); - fwrite(text, 1, fsize, fout); - fclose(fin); - free(text); - } - fclose(fout); -} - - - -int main(int argc, char *argv[]) { - char filename[100]; - - // filename = argv[2] - - if (!strcmp("--create", argv[3])) { - pack(argc, argv, argv[2]); - } else if (!strcmp("--extract", argv[3])) { - get_archive(argv[2]); - } else if (!strcmp("--list", argv[3])) { - list(argv[2]); - } else { - printf("ERROR: unknown command"); - return -1; - } - return 0; -} \ No newline at end of file From 1124e24bcec180a097ab55baffef39870191117f Mon Sep 17 00:00:00 2001 From: Ha1oz Date: Sat, 15 Jan 2022 13:46:13 +0300 Subject: [PATCH 09/10] Added lab3, deleted useless files --- lab2/.gitkeep | 0 lab2/lab2.c | 146 -------------------------------------------------- lab3/lab3.c | 127 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 146 deletions(-) delete mode 100644 lab2/.gitkeep delete mode 100644 lab2/lab2.c create mode 100644 lab3/lab3.c diff --git a/lab2/.gitkeep b/lab2/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/lab2/lab2.c b/lab2/lab2.c deleted file mode 100644 index df9cf38..0000000 --- a/lab2/lab2.c +++ /dev/null @@ -1,146 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS - -#include -#include -#include -#include - -#define uint1024 unsigned long* -#define FIELD_SIZE 9//ширина поля вывода в printf_value() -#define SIZE 35//размер массива long 310/9 -#define SIZE_CHAR 310//размер строки в scanf_val() длина str(2**1024) - -unsigned long long max = 1000000000;//макс. значение ячеек массива - - -uint1024 init(); -uint1024 from_uint(unsigned int); -uint1024 add_op(uint1024, uint1024); -uint1024 subtr_op(uint1024, uint1024); -void printf_value(uint1024); -void prt(unsigned long, int);//вспом. функция для printf_value() -uint1024 multp_op(uint1024, uint1024); -void scanf_val(uint1024*); - - -int main() { - uint1024 a = init(); - uint1024 b = init(); - uint1024 c = from_uint(1); - - scanf_val(&a); - scanf_val(&b); - - - - printf("--OUTPUT--\n"); - printf_value(add_op(a, b)); - printf("\n"); - - printf_value(subtr_op(a, b)); - printf("\n"); - - printf_value(multp_op(a, b)); - return 0; -} - - -void scanf_val(uint1024 *a) { - char str[SIZE_CHAR]; - int k = 0; - int i = 0; - for (i; i < SIZE_CHAR; ++i) { - str[i] = '\0'; - } - - scanf("%s", &str); - - while(str[i] == '\0' ) {i -= 1;} - - while (i >= 0) { - *(*a + (k / FIELD_SIZE)) += (str[i] - '0') * pow(10, k % FIELD_SIZE); - ++k; - --i; - } -} - -void prt(unsigned long x, int i) { // вывод одной цифры - if (i == FIELD_SIZE) - return ; - prt(x / 10, ++i); - printf("%c", x % 10 + '0'); -} - -void printf_value(uint1024 x) { - int i = SIZE - 1; - for (i; *(x + i) == 0; --i) ; - - printf("%d", *(x + i--)); - - for (i;i >= 0; --i) - prt(*(x + i), 0); -} - -uint1024 init() { - uint1024 buff = malloc(SIZE * sizeof(unsigned long)); - for (int i = 0; i < SIZE; ++i) - *(buff + i) = 0; - return buff; -} - -uint1024 from_uint(unsigned int x) { - uint1024 buff = init(); - int i = 0; - while (x != 0) { - *(buff + i) = x % max; - x /= max; - i++; - } - return buff; -} - -uint1024 add_op(uint1024 a, uint1024 b) { - uint1024 buff = init(); - int o = 0; - unsigned long long sm; - for (int i = 0; i < SIZE; ++i) { - sm = *(a + i) + *(b + i) + o; - *(buff + i)= sm % max; - o = sm / max; - } - return buff; -} - -uint1024 subtr_op(uint1024 a, uint1024 b) { - uint1024 buff = init(); - signed long long o = 0; - signed long long sm; - for (int i = 0; i < SIZE; ++i) { - sm = (long long )*(a + i) - (long long) * (b + i) - o; - - if (sm >= 0) { - *(buff + i) = sm; - o = 0; - } - else { - *(buff + i) = max + sm; - o = 1; - } - } - return buff; -} - -uint1024 multp_op(uint1024 a, uint1024 b) { - uint1024 buff = init(); - unsigned long long pr_buff = 0; - unsigned long long o = 0; - - for (int pa = 0; pa < SIZE; ++pa) { - for (int pb = 0; pa + pb < SIZE; ++pb) { - pr_buff = ((long long)*(a + pa)) * ((long long)*(b + pb)) + o; - buff[pa + pb] += pr_buff % max; - o = pr_buff / max; - } - } - return buff; -} \ No newline at end of file diff --git a/lab3/lab3.c b/lab3/lab3.c new file mode 100644 index 0000000..e863bf5 --- /dev/null +++ b/lab3/lab3.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include + +#define SIZE 300 //размер строки запроса +long timestamp[1891714]; int n = 0; +// 1891714 - кол-во строк в файле + +long utime(char *stime); +long get_time(char[]); +int get_error(char[]); +void get_window(int); + + +int main (int argc, char* argv[]) { + + //FILE* f = fopen("access_log_Jul95.txt", "r"); + + FILE* f = fopen(argv[1], "r"); + int errorCount = 0; + int wsize = atoi(argv[2]); + char str[SIZE]; + + if (argc != 3) { + printf("ERROR: not enough arguments"); + return -1; + } + + + while (fgets(str, SIZE, f)) { + timestamp[n++] = get_time(str); + if ((get_error(str) / 100) == 5){ + ++errorCount; + } + } + + get_window(wsize); + + + printf("\n5XX errors: %d\n", errorCount); + + return 0; +} + + +void get_window(int width) { + long m = 0;//макс. окно + long mp1 = 0; long mp2 = 0;//индексы границ макс. окна + long p1 = 0; long p2 = 0;//индексы границ текущего окна + while (p2 < n - 1) { + + if (timestamp[p2] - timestamp[p1] <= width) { + ++p2; + } else { + if (p2 - p1 > m) { + m = p2 - p1; + mp1 = p1 + 1; + mp2 = p2; + } + p1++; + } + } + if ((timestamp[p2] - timestamp[p1] <= width) && (p2 - p1 > m)) { + m = p2 - p1 + 1; + mp1 = p1 + 1; + mp2 = p2 + 1; + } + printf("count = %d, from %d to %d", m, mp1, mp2); +} + +long get_time(char str[]) { + + char buff[40];//хранит строку со временем + int ptr = 0;//индекс для времени + int i = 0;//индекс строки + while (str[i++] != '['); + + while (str[i] != ']') + buff[ptr++] = str[i++]; + + return utime(buff); +} + + +int get_error(char str[]) { + char buff[4];//хранит код ошибки + int ptr = 2;//индекс для кода ошибки + int i = strlen(str) - 1;//индекс строки + while (str[i--] != ' '); + + while (str[i] != ' ') + buff[ptr--] = str[i--]; + return atof(buff); +} + + +long utime(char* stime) { + char time_format[10] = "//::: ";//разделители строки времени + + char buff[7][6] = {""};//хранит разбиение строки stime + int i = 0;//индекс строки stime + int bp = 0; int wp = 0;//индексы buff + int fp = 0; //индекс time_format + while (stime[i]) { + if (stime[i] == time_format[fp]) { + ++bp; + ++fp; + wp = 0; + } else + buff[bp][wp++] = stime[i]; + ++i; + } + + struct tm tdate = { + .tm_mday = atof(buff[0]), + .tm_mon = atof(buff[1]) - 1, + .tm_year = atof(buff[2]) - 1900, + .tm_hour = atof(buff[3]), + .tm_min = atof(buff[4]), + .tm_sec = atof(buff[5]), + .tm_isdst = 0 + }; + + time_t time = mktime(&tdate); + return time; +} \ No newline at end of file From 979777440098e4b2d4b2f538a0448e52bd8f439d Mon Sep 17 00:00:00 2001 From: Ha1oz Date: Sat, 15 Jan 2022 14:37:04 +0300 Subject: [PATCH 10/10] Added lab4, deleted useless files --- lab3/lab3.c | 127 -------------------- lab4/lab4.c | 329 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 329 insertions(+), 127 deletions(-) delete mode 100644 lab3/lab3.c create mode 100644 lab4/lab4.c diff --git a/lab3/lab3.c b/lab3/lab3.c deleted file mode 100644 index e863bf5..0000000 --- a/lab3/lab3.c +++ /dev/null @@ -1,127 +0,0 @@ -#include -#include -#include -#include - -#define SIZE 300 //размер строки запроса -long timestamp[1891714]; int n = 0; -// 1891714 - кол-во строк в файле - -long utime(char *stime); -long get_time(char[]); -int get_error(char[]); -void get_window(int); - - -int main (int argc, char* argv[]) { - - //FILE* f = fopen("access_log_Jul95.txt", "r"); - - FILE* f = fopen(argv[1], "r"); - int errorCount = 0; - int wsize = atoi(argv[2]); - char str[SIZE]; - - if (argc != 3) { - printf("ERROR: not enough arguments"); - return -1; - } - - - while (fgets(str, SIZE, f)) { - timestamp[n++] = get_time(str); - if ((get_error(str) / 100) == 5){ - ++errorCount; - } - } - - get_window(wsize); - - - printf("\n5XX errors: %d\n", errorCount); - - return 0; -} - - -void get_window(int width) { - long m = 0;//макс. окно - long mp1 = 0; long mp2 = 0;//индексы границ макс. окна - long p1 = 0; long p2 = 0;//индексы границ текущего окна - while (p2 < n - 1) { - - if (timestamp[p2] - timestamp[p1] <= width) { - ++p2; - } else { - if (p2 - p1 > m) { - m = p2 - p1; - mp1 = p1 + 1; - mp2 = p2; - } - p1++; - } - } - if ((timestamp[p2] - timestamp[p1] <= width) && (p2 - p1 > m)) { - m = p2 - p1 + 1; - mp1 = p1 + 1; - mp2 = p2 + 1; - } - printf("count = %d, from %d to %d", m, mp1, mp2); -} - -long get_time(char str[]) { - - char buff[40];//хранит строку со временем - int ptr = 0;//индекс для времени - int i = 0;//индекс строки - while (str[i++] != '['); - - while (str[i] != ']') - buff[ptr++] = str[i++]; - - return utime(buff); -} - - -int get_error(char str[]) { - char buff[4];//хранит код ошибки - int ptr = 2;//индекс для кода ошибки - int i = strlen(str) - 1;//индекс строки - while (str[i--] != ' '); - - while (str[i] != ' ') - buff[ptr--] = str[i--]; - return atof(buff); -} - - -long utime(char* stime) { - char time_format[10] = "//::: ";//разделители строки времени - - char buff[7][6] = {""};//хранит разбиение строки stime - int i = 0;//индекс строки stime - int bp = 0; int wp = 0;//индексы buff - int fp = 0; //индекс time_format - while (stime[i]) { - if (stime[i] == time_format[fp]) { - ++bp; - ++fp; - wp = 0; - } else - buff[bp][wp++] = stime[i]; - ++i; - } - - struct tm tdate = { - .tm_mday = atof(buff[0]), - .tm_mon = atof(buff[1]) - 1, - .tm_year = atof(buff[2]) - 1900, - .tm_hour = atof(buff[3]), - .tm_min = atof(buff[4]), - .tm_sec = atof(buff[5]), - .tm_isdst = 0 - }; - - time_t time = mktime(&tdate); - return time; -} \ No newline at end of file diff --git a/lab4/lab4.c b/lab4/lab4.c new file mode 100644 index 0000000..808bee2 --- /dev/null +++ b/lab4/lab4.c @@ -0,0 +1,329 @@ +#include +#include +#include +#include + +typedef struct +{ + unsigned char id[3]; + unsigned char version[2]; + unsigned char flags; + unsigned char size[4]; +} Header; + +typedef struct +{ + unsigned char *id; + unsigned char *size; + unsigned char *flags; + unsigned char *content; +} Frame; + +void clear(Frame *frame); +long header_size(unsigned char sizearr[]); +long frame_size(unsigned char sizearr[]); +void read_(unsigned char *str, long n); +void get_header(); +Frame *get_frame(); +Frame *init_frame(); +void print(char *p, int n); +void show(); +int null(char *p, int n); +void get(const char *q); +Header *header; +long pointer; +FILE *fin; + +char *separate(char *str); + +void copyf(char buff[], char origin[]); +void set(const char id[], char val[]); + +void print_header(); +void print_frame(Frame *); + +char filepath[100]; + +int main(int argc, char *argv[]) { + if (argc > 2) { + char * p = separate(argv[1]); + if (strcmp(argv[1], "--filepath") == 0) { + strcpy(filepath, p); + } else { + printf("ERROR: no path"); + return -1; + } + } + fin = fopen(filepath, "r+"); + + if (argc < 3) + { + fprintf(fin, "ERROR: not enough arguments"); + return -2; + } + else if (argc == 3) + { + char *val = separate(argv[2]); + if (strcmp(argv[2], "--show") == 0) + show(); + else if (strcmp(argv[2], "--get") == 0) + get(val); + else { + fprintf(fin, "ERROR: 1 arg, not supported"); + return -3; + } + } + else if (argc == 4) + { + char * id = separate(argv[2]); + char * value = separate(argv[3]); + if (strcmp(argv[2], "--set") != 0) { + printf("ERROR: no tag selected"); + return -4; + } + + if (strcmp(argv[3], "--value") != 0) { + printf("ERROR: no value selected"); + return -5; + } + + set(id, value); + } + else + { + fprintf(fin, "ERROR: too many arguments"); + return -6; + } + + fclose(fin); + return 0; +} + +void set(const char id[], char val[]) { + Frame * frames[100]; + get_header(); + Frame * frame; Frame *q; + int i = 0; + int f = 0; + while (pointer < header_size(header->size) && (frame = get_frame())) { + if (strcmp(frame->id, id) == 0) { + q = frame; + f = 1; + long fsize = strlen(val) + 1; + + for (int i = 0; i < 4; ++i) { + frame->size[3 - i] = fsize % (long)pow(2, 8); + fsize /= pow(2, 8); + } + char * buff = calloc(strlen(val) + 1, sizeof(char)); + buff[0] = 0; + for (int i = 1; i < strlen(val) + 1; ++i) { + buff[i] = val[i - 1]; + } + frame->content = buff; + } + frames[i++] = frame; + } + + if (!f) { + printf("tag not found"); + return ; + } + + long size1 = frame_size(q->size); + long size2 = strlen(val) + 1; + long dsize = size2 - size1; + long size = header_size(header->size) + dsize; + + + fseek(fin, 0, SEEK_SET); + + for (int i = 0; i < 4; ++i) { + header->size[3 - i] = size % (long)pow(2, 7); + size /= pow(2, 7); + } + print_header(); + + for (int j = 0; j < i; ++j) + print_frame(frames[j]); + fprintf(fin, "\0"); + } + + +Frame *get_frame() { + Frame *frame = init_frame(); + read_(frame->id, 4); + if (null(frame->id, 4)) + { + return 0; + } + read_(frame->size, 4); + read_(frame->flags, 2); + + long size = frame_size(frame->size); + + unsigned char *str = malloc((size + 1L) * sizeof(unsigned char)); + read_(str, size); + frame->content = str; + + return frame; +} + + +void show() { + get_header(); + Frame *frame; + while (pointer < header_size(header->size) && (frame = get_frame())) { + print(frame->id, 4); + printf(" %d ", frame_size(frame->size)); + + print(frame->content, frame_size(frame->size)); + printf("\n"); + clear(frame); + } +} + +void print(char *p, int n) +{ + while (n-- > 0) + { + if (*p) + printf("%c", *p); + *p++; + } +} + +void get(const char *q) { + get_header(); + Frame *frame; + while (pointer < header_size(header->size) && (frame = get_frame())) { + if (strcmp(frame->id, q) == 0) { + print(frame->id, 4); + printf(" %d ", frame_size(frame->size)); + + print(frame->content, frame_size(frame->size)); + printf("\n"); + } + clear(frame); + } +} + +void get_header() +{ + header = malloc(sizeof(Header)); + int i; + for (i = 0; i < 3; ++i) + header->id[i] = fgetc(fin); + + for (i = 0; i < 2; ++i) + header->version[i] = fgetc(fin); + + header->flags = fgetc(fin); + + for (i = 0; i < 4; ++i) + { + header->size[i] = fgetc(fin); + } +} + +void read_(unsigned char *str, long n) +{ + while (n-- > 0) + { + *str++ = fgetc(fin); + ++pointer; + } + *str = '\0'; +} + +void print_header() +{ + int i; + for (i = 0; i < 3; ++i) + fprintf(fin, "%c", header->id[i]); + + for (i = 0; i < 2; ++i) + fprintf(fin, "%c", header->version[i]); + + fprintf(fin, "%c", header->flags); + + for (i = 0; i < 4; ++i) + fprintf(fin, "%c", header->size[i]); +} + +void print_frame(Frame *frame) +{ + int i; + for (i = 0; i < 4; ++i) + fprintf(fin, "%c", frame->id[i]); + + for (i = 0; i < 4; ++i) + { + fprintf(fin, "%c", frame->size[i]); + } + for (i = 0; i < 2; ++i) + fprintf(fin, "%c", frame->flags[i]); + + for (i = 0; i < frame_size(frame->size); ++i) + fprintf(fin, "%c", frame->content[i]); +} + +int null(char *p, int n) +{ + int f = 1; + while (n-- > 0) + if (*p++ != 0) + f = 0; + return f; +} + + +char *separate(char *str) +{ //первая часть - str, вторая - separate + while (*str != '=' && *str++ != '\0') + ; + + if (*str == '\0') + return str; + else + *str = '\0'; + return ++str; +} + +Frame *init_frame() { + Frame *frame = malloc(sizeof(Frame)); + frame->id = malloc(5 * sizeof(unsigned char)); + frame->size = malloc(5 * sizeof(unsigned char)); + frame->flags = malloc(3 * sizeof(unsigned char)); + frame->content = malloc(sizeof(unsigned char *)); + return frame; +} + +void clear(Frame *frame) +{ + free(frame->id); + free(frame->size); + free(frame->flags); + free(frame->content); + free(frame); +} + +long header_size(unsigned char sizearr[]) +{ + long size = 0; + for (int i = 0; i < 4; ++i) + { + size += ((sizearr[4 - i - 1]) << 7 * i); + } + return size; +} + +long frame_size(unsigned char arr[]) +{ + long size = 0; + for (int i = 0; i < 4; ++i) + { + size += ((arr[4 - i - 1]) << 8 * i); + } + return size; +} \ No newline at end of file