From aa4e175c9095386b973951743bf5c21b25f410ac Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 16 Dec 2022 23:43:47 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82=D0=BC,=20?= =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D1=87=D1=82=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D0=B7=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lexerOfCommentsByFile.sln | 31 ++++ .../lexerOfCommentsByFile/lexerOfComments.c | 54 +++++++ .../lexerOfCommentsByFile/lexerOfComments.h | 10 ++ .../lexerOfCommentsByFile.vcxproj | 150 ++++++++++++++++++ .../lexerOfCommentsByFile.vcxproj.filters | 17 ++ 5 files changed, 262 insertions(+) create mode 100644 lexerOfCommentsByFile/lexerOfCommentsByFile.sln create mode 100644 lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c create mode 100644 lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h create mode 100644 lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj create mode 100644 lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile.sln b/lexerOfCommentsByFile/lexerOfCommentsByFile.sln new file mode 100644 index 0000000..ff58d98 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lexerOfCommentsByFile", "lexerOfCommentsByFile\lexerOfCommentsByFile.vcxproj", "{B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Debug|x64.ActiveCfg = Debug|x64 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Debug|x64.Build.0 = Debug|x64 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Debug|x86.ActiveCfg = Debug|Win32 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Debug|x86.Build.0 = Debug|Win32 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Release|x64.ActiveCfg = Release|x64 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Release|x64.Build.0 = Release|x64 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Release|x86.ActiveCfg = Release|Win32 + {B8D1BF09-6F07-4E71-BA65-C7B78E7FE91A}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0F1031D9-8477-45F8-876F-09D22FF8001E} + EndGlobalSection +EndGlobal diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c new file mode 100644 index 0000000..ee8c1be --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c @@ -0,0 +1,54 @@ +#include "lexerOfComments.h" + +/* + + : +0 / -> 1. +1 * -> 2. / 1. 0. +2 . * -> 3. *, / 1. 0. +? 1 / ? +3 / . 0. + + + stage * / + 0 0 1 + 1 2 1 + 2 3 1 + 3 0 0 +*/ + +char* workWithStage(int statusTable[4][3], const char* fileNameWithStrings, Error* errorCheck) { + FILE* file = fopen(fileNameWithStrings, "r"); + if (file == NULL) { + *errorCheck = fileProblem; + return NULL; + } + char symbol = 0; + char bufferWithOneComment[10000] = { '\0' }; + char bufferWithAllComments[100000] = { '\0' }; + int stage = 0; + while (fscanf_s(file, '%c', &symbol) == 1) { + if () + } + + return bufferWithAllComments; +} + +char* lexerOfComments(const char* fileNameTable, const char* fileNameWithStrings, Error* errorCheck) { + FILE* file = fopen(fileNameTable, "r"); + if (file == NULL) { + *errorCheck = fileProblem; + return NULL; + } + int statusTable[4][3] = { 0 }; + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 3; ++j) { + int changeStage = 0; + if (fscanf_s(file, '%d', &changeStage) == 1) { + statusTable[i][j] = changeStage; + } + } + } + + return workWithStage(, fileNameWithStrings, errorCheck); +} \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h new file mode 100644 index 0000000..2a86621 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h @@ -0,0 +1,10 @@ +#pragma once +#include +#include + +typedef enum Error { + ok, + fileProblem +}Error; + +char* lexerOfComments(const char* fileName); \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj new file mode 100644 index 0000000..e83844c --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj @@ -0,0 +1,150 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + 16.0 + Win32Proj + {b8d1bf09-6f07-4e71-ba65-c7b78e7fe91a} + lexerOfCommentsByFile + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters new file mode 100644 index 0000000..153170c --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters @@ -0,0 +1,17 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + \ No newline at end of file From d37917d159043811b2575b69092bc4c5169dca7c Mon Sep 17 00:00:00 2001 From: Artem Date: Sat, 17 Dec 2022 11:37:33 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B,=20=D0=BD=D0=B5=D0=BC=D0=BD?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D1=80=20=D1=82=D0=B5?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B5=D0=B3=D0=BE=20=D0=BB=D0=B5?= =?UTF-8?q?=D0=B3=D1=87=D0=B5=20=D1=87=D0=B8=D1=82=D0=B0=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lexerOfCommentsByFile/lexerOfComments.c | 111 +++++++++++++----- .../lexerOfCommentsByFile/lexerOfComments.h | 3 +- .../lexerOfCommentsByFile.vcxproj | 49 ++++---- .../lexerOfCommentsByFile.vcxproj.filters | 27 +++++ .../lexerOfCommentsByFile/main.c | 30 +++++ .../lexerOfCommentsByFile/table.txt | 5 + .../lexerOfCommentsByFile/test.c | 9 ++ .../lexerOfCommentsByFile/test.h | 4 + .../lexerOfCommentsByFile/test.txt | 1 + 9 files changed, 187 insertions(+), 52 deletions(-) create mode 100644 lexerOfCommentsByFile/lexerOfCommentsByFile/main.c create mode 100644 lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt create mode 100644 lexerOfCommentsByFile/lexerOfCommentsByFile/test.c create mode 100644 lexerOfCommentsByFile/lexerOfCommentsByFile/test.h create mode 100644 lexerOfCommentsByFile/lexerOfCommentsByFile/test.txt diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c index ee8c1be..75726ca 100644 --- a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c @@ -1,36 +1,82 @@ #include "lexerOfComments.h" -/* - - : -0 / -> 1. -1 * -> 2. / 1. 0. -2 . * -> 3. *, / 1. 0. -? 1 / ? -3 / . 0. - +int changeStage(int statusTable[5][2], int stage, int symbol) { + int posSymbol = symbol % 42 == 0 ? 0 : 1; + return statusTable[stage][posSymbol]; +} - stage * / - 0 0 1 - 1 2 1 - 2 3 1 - 3 0 0 -*/ +void clearBuffer(char buffer[], int* index) { + memset(buffer, 0, strlen(buffer)); + *index = 0; +} -char* workWithStage(int statusTable[4][3], const char* fileNameWithStrings, Error* errorCheck) { +char* workWithStage(int statusTable[5][2], const char* fileNameWithStrings, Error* errorCheck) { FILE* file = fopen(fileNameWithStrings, "r"); if (file == NULL) { *errorCheck = fileProblem; return NULL; } char symbol = 0; - char bufferWithOneComment[10000] = { '\0' }; - char bufferWithAllComments[100000] = { '\0' }; - int stage = 0; - while (fscanf_s(file, '%c', &symbol) == 1) { - if () + char bufferWithOneComment[1000] = { '\0' }; + char bufferWithAllComments[10000] = { '\0' }; + int stage = 1; + int indexBuffer = 0; + while (fscanf(file, "%c", &symbol) == 1) { + if (symbol == '/') { + switch (stage) { + case(1): + if (indexBuffer == 0) { + bufferWithOneComment[indexBuffer] = '/'; + ++indexBuffer; + } + break; + case(2): + clearBuffer(bufferWithOneComment, &indexBuffer); + bufferWithOneComment[0] = '/'; + indexBuffer = 1; + break; + case(3): + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + break; + case(4): + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + strcat(bufferWithAllComments, bufferWithOneComment); + memset(bufferWithOneComment, 0, strlen(bufferWithOneComment)); + indexBuffer = 0; + break; + } + stage = changeStage(statusTable, stage, (int)symbol); + } else if (symbol == '*') { + switch (stage) { + case(1): + clearBuffer(bufferWithOneComment, &indexBuffer); + break; + case(2): + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + break; + case(3): + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + break; + case(4): + clearBuffer(bufferWithOneComment, &indexBuffer); + break; + } + stage = changeStage(statusTable, stage, (int)symbol); + } else { + if (stage == 3) { + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + } else { + clearBuffer(bufferWithOneComment, &indexBuffer); + stage = 1; + } + } } - + fclose(file); return bufferWithAllComments; } @@ -40,15 +86,20 @@ char* lexerOfComments(const char* fileNameTable, const char* fileNameWithStrings *errorCheck = fileProblem; return NULL; } - int statusTable[4][3] = { 0 }; - for (int i = 0; i < 4; ++i) { - for (int j = 0; j < 3; ++j) { - int changeStage = 0; - if (fscanf_s(file, '%d', &changeStage) == 1) { - statusTable[i][j] = changeStage; - } + int statusTable[5][2] = { 0 }; + char expression[3] = { '\0' }; + if (fscanf(file, "%s", expression) == 1) { + statusTable[0][0] = expression[0]; + statusTable[0][1] = expression[1]; + } + + for (int i = 1; i < 5; ++i) { + if (fscanf(file, "%s", expression) == 1) { + statusTable[i][0] = expression[0] - 48; + statusTable[i][1] = expression[1] - 48; } } + fclose(file); - return workWithStage(, fileNameWithStrings, errorCheck); + return workWithStage(statusTable, fileNameWithStrings, errorCheck); } \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h index 2a86621..bf2fb22 100644 --- a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h @@ -1,10 +1,11 @@ #pragma once #include #include +#include typedef enum Error { ok, fileProblem }Error; -char* lexerOfComments(const char* fileName); \ No newline at end of file +char* lexerOfComments(const char* fileNameTable, const char* fileNameWithStrings, Error* errorCheck); \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj index e83844c..7f4f391 100644 --- a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj @@ -17,7 +17,6 @@ Release x64 - 16.0 @@ -53,25 +52,23 @@ true Unicode - - + + + + + + + + + + + + + - - - - - - - - - - - - - true @@ -85,7 +82,6 @@ false - Level3 @@ -117,7 +113,7 @@ Level3 - true + false _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true @@ -142,9 +138,20 @@ true - - + + + + + + + + + + + + + - + \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters index 153170c..f17d6a6 100644 --- a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfCommentsByFile.vcxproj.filters @@ -14,4 +14,31 @@ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Исходные файлы + + + Исходные файлы + + + + + Исходные файлы + + + Исходные файлы + + \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/main.c b/lexerOfCommentsByFile/lexerOfCommentsByFile/main.c new file mode 100644 index 0000000..80d8b58 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/main.c @@ -0,0 +1,30 @@ +#include "lexerOfComments.h" +#include +#include +#include "test.h" + + +int main() { + if (test()) { + printf("Tests correct!\n"); + } else { + printf("Error\n"); + return -1; + } + Error errorCheck = ok; + printf("Input your file with extension. No more than 100 symbols\n"); + char nameFile[101] = { '\0' }; + int checkScanf = scanf_s("%s", nameFile, 100); + while (checkScanf != 1) { + while (getchar() != '\n') { + } + printf("Try again\n"); + checkScanf = scanf_s("%s", nameFile, 100); + } + char* buffer = lexerOfComments("table.txt", nameFile, &errorCheck); + if (errorCheck != ok) { + printf("Error\n"); + return -1; + } + printf("%s", buffer); +} \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt b/lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt new file mode 100644 index 0000000..a031efe --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt @@ -0,0 +1,5 @@ +*/ +12 +32 +43 +11 \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/test.c b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.c new file mode 100644 index 0000000..03327b1 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.c @@ -0,0 +1,9 @@ +#include "test.h" +#include "lexerOfComments.h" + +bool test() { + Error errorCheck = ok; + char* stringForCheck = "/*fdfdfdfd*//**//*dfsdfsff*/"; + char* result = lexerOfComments("table.txt", "test.txt", &errorCheck); + return errorCheck == ok && strcmp(result, stringForCheck) == 0; +} \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/test.h b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.h new file mode 100644 index 0000000..c4546c4 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.h @@ -0,0 +1,4 @@ +#pragma once +#include + +bool test(); \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/test.txt b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.txt new file mode 100644 index 0000000..2cc66a2 --- /dev/null +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.txt @@ -0,0 +1 @@ +dslfdsof'dksfodsf/*fdfdfdfd*/dfdfsdfdsf/**/dfssf/dfjdjf*/fdfdfd//*dfsdfsff*/dfdf*fdfd* \ No newline at end of file From 6f64ef010cd836367429fd3a8c5ecb53d6622dd1 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 21 Dec 2022 10:53:41 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D0=BE=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lexerOfCommentsByFile/lexerOfComments.c | 184 +++++++++--------- .../lexerOfCommentsByFile/lexerOfComments.h | 4 +- .../lexerOfCommentsByFile/main.c | 45 +++-- .../lexerOfCommentsByFile/table.txt | 10 +- .../lexerOfCommentsByFile/test.c | 8 +- .../lexerOfCommentsByFile/test.h | 2 + 6 files changed, 128 insertions(+), 125 deletions(-) diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c index 75726ca..4d9812f 100644 --- a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.c @@ -1,105 +1,105 @@ #include "lexerOfComments.h" -int changeStage(int statusTable[5][2], int stage, int symbol) { - int posSymbol = symbol % 42 == 0 ? 0 : 1; - return statusTable[stage][posSymbol]; +int changeStage(int statusTable[5][3], int stage, int symbol) { + int posSymbol = symbol % 42 == 0 ? 0 : symbol % 47 == 0 ? 1 : 2; + return statusTable[stage][posSymbol]; } void clearBuffer(char buffer[], int* index) { - memset(buffer, 0, strlen(buffer)); - *index = 0; + memset(buffer, 0, strlen(buffer)); + *index = 0; } -char* workWithStage(int statusTable[5][2], const char* fileNameWithStrings, Error* errorCheck) { - FILE* file = fopen(fileNameWithStrings, "r"); - if (file == NULL) { - *errorCheck = fileProblem; - return NULL; - } - char symbol = 0; - char bufferWithOneComment[1000] = { '\0' }; - char bufferWithAllComments[10000] = { '\0' }; - int stage = 1; - int indexBuffer = 0; - while (fscanf(file, "%c", &symbol) == 1) { - if (symbol == '/') { - switch (stage) { - case(1): - if (indexBuffer == 0) { - bufferWithOneComment[indexBuffer] = '/'; - ++indexBuffer; - } - break; - case(2): - clearBuffer(bufferWithOneComment, &indexBuffer); - bufferWithOneComment[0] = '/'; - indexBuffer = 1; - break; - case(3): - bufferWithOneComment[indexBuffer] = symbol; - ++indexBuffer; - break; - case(4): - bufferWithOneComment[indexBuffer] = symbol; - ++indexBuffer; - strcat(bufferWithAllComments, bufferWithOneComment); - memset(bufferWithOneComment, 0, strlen(bufferWithOneComment)); - indexBuffer = 0; - break; - } - stage = changeStage(statusTable, stage, (int)symbol); - } else if (symbol == '*') { - switch (stage) { - case(1): - clearBuffer(bufferWithOneComment, &indexBuffer); - break; - case(2): - bufferWithOneComment[indexBuffer] = symbol; - ++indexBuffer; - break; - case(3): - bufferWithOneComment[indexBuffer] = symbol; - ++indexBuffer; - break; - case(4): - clearBuffer(bufferWithOneComment, &indexBuffer); - break; - } - stage = changeStage(statusTable, stage, (int)symbol); - } else { - if (stage == 3) { - bufferWithOneComment[indexBuffer] = symbol; - ++indexBuffer; - } else { - clearBuffer(bufferWithOneComment, &indexBuffer); - stage = 1; - } - } - } - fclose(file); - return bufferWithAllComments; +char* workWithStage(int statusTable[5][3], const char* fileNameWithStrings, Error* errorCheck) { + FILE* file = fopen(fileNameWithStrings, "r"); + if (file == NULL) { + *errorCheck = fileProblem; + return NULL; + } + char symbol = 0; + char bufferWithOneComment[1000] = { '\0' }; + char bufferWithAllComments[10000] = { '\0' }; + int stage = 1; + int indexBuffer = 0; + while (fscanf(file, "%c", &symbol) == 1) { + if (symbol == '/') { + switch (stage) { + case 1: + if (indexBuffer == 0) { + bufferWithOneComment[indexBuffer] = '/'; + ++indexBuffer; + } + break; + case 2: + clearBuffer(bufferWithOneComment, &indexBuffer); + bufferWithOneComment[0] = '/'; + indexBuffer = 1; + break; + case 3: + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + break; + case 4: + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + strcat(bufferWithAllComments, bufferWithOneComment); + memset(bufferWithOneComment, 0, strlen(bufferWithOneComment)); + indexBuffer = 0; + break; + } + } else if (symbol == '*') { + switch (stage) { + case 1: + clearBuffer(bufferWithOneComment, &indexBuffer); + break; + case 2: + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + break; + case 3: + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + break; + case 4: + clearBuffer(bufferWithOneComment, &indexBuffer); + break; + } + } else { + if (stage == 3) { + bufferWithOneComment[indexBuffer] = symbol; + ++indexBuffer; + } else { + clearBuffer(bufferWithOneComment, &indexBuffer); + } + } + stage = changeStage(statusTable, stage, symbol); + } + fclose(file); + return bufferWithAllComments; } char* lexerOfComments(const char* fileNameTable, const char* fileNameWithStrings, Error* errorCheck) { - FILE* file = fopen(fileNameTable, "r"); - if (file == NULL) { - *errorCheck = fileProblem; - return NULL; - } - int statusTable[5][2] = { 0 }; - char expression[3] = { '\0' }; - if (fscanf(file, "%s", expression) == 1) { - statusTable[0][0] = expression[0]; - statusTable[0][1] = expression[1]; - } + FILE* file = fopen(fileNameTable, "r"); + if (file == NULL) { + *errorCheck = fileProblem; + return NULL; + } + int statusTable[5][3] = { 0 }; + char expression[4] = { '\0' }; + if (fscanf(file, "%s", expression) == 1) { + statusTable[0][0] = expression[0]; + statusTable[0][1] = expression[1]; + statusTable[0][2] = expression[2]; + } - for (int i = 1; i < 5; ++i) { - if (fscanf(file, "%s", expression) == 1) { - statusTable[i][0] = expression[0] - 48; - statusTable[i][1] = expression[1] - 48; - } - } - fclose(file); + for (int i = 1; i < 5; ++i) { + if (fscanf(file, "%s", expression) == 1) { + statusTable[i][0] = expression[0] - 48; + statusTable[i][1] = expression[1] - 48; + statusTable[i][2] = expression[2] - 48; + } + } + fclose(file); - return workWithStage(statusTable, fileNameWithStrings, errorCheck); + return workWithStage(statusTable, fileNameWithStrings, errorCheck); } \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h index bf2fb22..cf184d1 100644 --- a/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/lexerOfComments.h @@ -1,4 +1,5 @@ #pragma once + #include #include #include @@ -6,6 +7,7 @@ typedef enum Error { ok, fileProblem -}Error; +} Error; +//Lexer of comments. Return string with all comments. char* lexerOfComments(const char* fileNameTable, const char* fileNameWithStrings, Error* errorCheck); \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/main.c b/lexerOfCommentsByFile/lexerOfCommentsByFile/main.c index 80d8b58..04817ae 100644 --- a/lexerOfCommentsByFile/lexerOfCommentsByFile/main.c +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/main.c @@ -3,28 +3,27 @@ #include #include "test.h" - int main() { - if (test()) { - printf("Tests correct!\n"); - } else { - printf("Error\n"); - return -1; - } - Error errorCheck = ok; - printf("Input your file with extension. No more than 100 symbols\n"); - char nameFile[101] = { '\0' }; - int checkScanf = scanf_s("%s", nameFile, 100); - while (checkScanf != 1) { - while (getchar() != '\n') { - } - printf("Try again\n"); - checkScanf = scanf_s("%s", nameFile, 100); - } - char* buffer = lexerOfComments("table.txt", nameFile, &errorCheck); - if (errorCheck != ok) { - printf("Error\n"); - return -1; - } - printf("%s", buffer); + if (test()) { + printf("Tests correct!\n"); + } else { + printf("Error\n"); + return -1; + } + Error errorCheck = ok; + printf("Input your file with extension. No more than 100 symbols\n"); + char nameFile[101] = { '\0' }; + int checkScanf = scanf_s("%s", nameFile, 100); + while (checkScanf != 1) { + while (getchar() != '\n') { + } + printf("Try again\n"); + checkScanf = scanf_s("%s", nameFile, 100); + } + char* buffer = lexerOfComments("table.txt", nameFile, &errorCheck); + if (errorCheck != ok) { + printf("Error\n"); + return -1; + } + printf("%s", buffer); } \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt b/lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt index a031efe..8c552c6 100644 --- a/lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/table.txt @@ -1,5 +1,5 @@ -*/ -12 -32 -43 -11 \ No newline at end of file +*/a +121 +321 +433 +111 \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/test.c b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.c index 03327b1..cc3fdc5 100644 --- a/lexerOfCommentsByFile/lexerOfCommentsByFile/test.c +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.c @@ -2,8 +2,8 @@ #include "lexerOfComments.h" bool test() { - Error errorCheck = ok; - char* stringForCheck = "/*fdfdfdfd*//**//*dfsdfsff*/"; - char* result = lexerOfComments("table.txt", "test.txt", &errorCheck); - return errorCheck == ok && strcmp(result, stringForCheck) == 0; + Error errorCheck = ok; + char* stringForCheck = "/*fdfdfdfd*//**//*dfsdfsff*/"; + char* result = lexerOfComments("table.txt", "test.txt", &errorCheck); + return errorCheck == ok && strcmp(result, stringForCheck) == 0; } \ No newline at end of file diff --git a/lexerOfCommentsByFile/lexerOfCommentsByFile/test.h b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.h index c4546c4..0ca2603 100644 --- a/lexerOfCommentsByFile/lexerOfCommentsByFile/test.h +++ b/lexerOfCommentsByFile/lexerOfCommentsByFile/test.h @@ -1,4 +1,6 @@ #pragma once + #include +//Test for lexer of comments bool test(); \ No newline at end of file