-
Notifications
You must be signed in to change notification settings - Fork 0
лексем комментариев #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #include "String.h" | ||
|
|
||
| char *getString(size_t * const len, FILE * file, char const endOfLine) | ||
| { | ||
| *len = 0; | ||
| size_t capacity = 1; | ||
| char *s = (char *)malloc(sizeof(char)); | ||
| if (s == NULL) | ||
| { | ||
| return NULL; | ||
| } | ||
|
|
||
| for (char c = fgetc(file); c != endOfLine && c != EOF; c = fgetc(file)) | ||
| { | ||
| s[(*len)++] = c; | ||
|
|
||
| if (*len >= capacity) | ||
| { | ||
| capacity *= 2; | ||
| char *tmp = (char *)realloc(s, capacity * sizeof(char)); | ||
| if (tmp == NULL) | ||
| { | ||
| free(s); | ||
| return NULL; | ||
| } | ||
| s = tmp; | ||
| } | ||
| } | ||
|
|
||
| s[*len] = '\0'; | ||
|
|
||
| return s; | ||
| } | ||
|
|
||
| char *copyString(char const * const string) | ||
| { | ||
| const size_t len = strlen(string); | ||
| char *copy = (char *)malloc(len * sizeof(char)); | ||
| if (copy != NULL) | ||
| { | ||
| strcpy(copy, string); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. len должна быть на 1 больше strlen, чтобы ещё нулевой байт вместить |
||
| } | ||
| return copy; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdbool.h> | ||
| #include <string.h> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Думаю, что этот файл в хедере не нужен |
||
| #include <stdio.h> | ||
|
|
||
| char *getString(size_t * const len, FILE * file, char const endOfLine); | ||
| char *copyString(char const * const string); | ||
|
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо комментарии |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #include <stdio.h> | ||
|
|
||
| int main(void) /* dfghdshs */ | ||
| /* 56436rteyuhny65 */{ | ||
| printf("Hello world!\n"); | ||
| /* dfghhgf */ /* fdhgjbvfcdxvbmnvb */t | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #pragma once | ||
|
|
||
| typedef enum ErrorCode | ||
| { | ||
| ok, | ||
| memoryError, | ||
| fileReadingError | ||
| } ErrorCode; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
|
|
||
| #include "findComments.h" | ||
| #include "readFile.h" | ||
| #include "list.h" | ||
| #include "../../String/String.h" | ||
|
|
||
| typedef enum State | ||
| { | ||
| start, | ||
| firstSlash, | ||
| firstStar, | ||
| text, | ||
| secondStar, | ||
| finish | ||
| } State; | ||
|
|
||
| State move(State state, char c) | ||
| { | ||
| switch (state) | ||
| { | ||
| case start: | ||
| { | ||
| if (c == '/') | ||
| { | ||
| return firstSlash; | ||
| } | ||
| return start; | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишний пробел тут и ниже |
||
| case firstSlash: | ||
| { | ||
| if (c == '*') | ||
| { | ||
| return firstStar; | ||
| } | ||
| return start; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не-а. |
||
| } | ||
|
|
||
| case firstStar: | ||
| { | ||
| if (c == '*') | ||
| { | ||
| return secondStar; | ||
| } | ||
| return text; | ||
| } | ||
|
|
||
| case text: | ||
| { | ||
| if (c == '*') | ||
| { | ||
| return secondStar; | ||
| } | ||
| return text; | ||
| } | ||
|
|
||
| case secondStar: | ||
| { | ||
| if (c == '/') | ||
| { | ||
| return finish; | ||
| } | ||
| return text; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тоже нет, |
||
| } | ||
|
|
||
| default: | ||
| return start; | ||
| break; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. break не нужен, до него никогда управление не дойдёт |
||
| } | ||
| } | ||
|
|
||
| ErrorCode addComment(List *list, size_t start, size_t end, const char *const text) | ||
| { | ||
| char *string = malloc((start + end + 1) * sizeof(char)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (string == NULL) | ||
| { | ||
| return memoryError; | ||
| } | ||
|
|
||
| for (size_t i = start; i < end + 1; ++i) | ||
| { | ||
| string[i - start] = text[i]; | ||
| } | ||
|
|
||
| ErrorCode ec = addElement(list, string); | ||
| if (ec != ok) | ||
| { | ||
| free(string); | ||
| } | ||
|
|
||
| return ec; | ||
| } | ||
|
|
||
| char **findComments(const char *const fileName, size_t *outputLen) | ||
| { | ||
| List *list = createList(); | ||
| if (list == NULL) | ||
| { | ||
| return NULL; | ||
| } | ||
|
|
||
| size_t len = 0; | ||
| char *text = readFile(fileName, &len); | ||
| if (text == NULL) | ||
| { | ||
| free(list); | ||
| return NULL; | ||
| } | ||
|
|
||
| State state = start; | ||
| size_t currentCommentStart = 0; | ||
| size_t currentCommentEnd = 0; | ||
| for (size_t i = 0; i < len; i++) | ||
| { | ||
| state = move(state, text[i]); | ||
| if (state == firstSlash) | ||
| { | ||
| currentCommentStart = i; | ||
| } | ||
| if (state == finish) | ||
| { | ||
| currentCommentEnd = i; | ||
| if (addComment(list, currentCommentStart, currentCommentEnd, text) != ok) | ||
| { | ||
| free(text); | ||
| deleteList(&list); | ||
| return NULL; | ||
| } | ||
| } | ||
| } | ||
| free(text); | ||
| char **output = writeToDefaultArray(list, outputLen); | ||
| deleteList(&list); | ||
| return output; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdlib.h> | ||
| #include "errorCode.h" | ||
|
|
||
| char ** findComments(const char * const fileName, size_t *outputLen); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #include <stdlib.h> | ||
|
|
||
| #include "list.h" | ||
|
|
||
| typedef struct Node | ||
| { | ||
| struct Node *next; | ||
| char *value; | ||
| } Node; | ||
|
|
||
| typedef struct List | ||
| { | ||
| size_t length; | ||
| Node *head; | ||
| Node *back; | ||
| } List; | ||
|
|
||
| List *createList(void) | ||
| { | ||
| return calloc(1, sizeof(List)); | ||
| } | ||
|
|
||
| ErrorCode addElement(List *list, char *const value) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| Node *newNode = calloc(1, sizeof(Node)); | ||
| if (newNode == NULL) | ||
| { | ||
| return memoryError; | ||
| } | ||
|
|
||
| newNode->value = value; | ||
|
|
||
| if (list->head == NULL) | ||
| { | ||
| list->head = newNode; | ||
| list->back = newNode; | ||
| ++list->length; | ||
| return ok; | ||
| } | ||
|
|
||
| list->back->next = newNode; | ||
| list->back = newNode; | ||
| ++list->length; | ||
| return ok; | ||
| } | ||
|
|
||
| char **writeToDefaultArray(List const *const list, size_t *length) | ||
| { | ||
| char **output = calloc(list->length, sizeof(char *)); | ||
| if (output == NULL) | ||
| { | ||
| return NULL; | ||
| } | ||
|
|
||
| size_t i = 0; | ||
| for (Node *node = list->head; node != NULL; node = node->next) | ||
| { | ||
| output[i] = node->value; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. переименовать функцию в convertToArray и внутри удалять список |
||
| ++i; | ||
| } | ||
|
|
||
| *length = list->length; | ||
| return output; | ||
| } | ||
|
|
||
| void deleteList(List **const list) | ||
| { | ||
| Node *currentNode = (*list)->head; | ||
| while (currentNode != NULL) | ||
| { | ||
| Node *temp = currentNode; | ||
| currentNode = currentNode->next; | ||
| // free(temp->value); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. раскомментировать |
||
| free(temp); | ||
| } | ||
| free(*list); | ||
| *list = NULL; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #pragma once | ||
|
|
||
| #include "errorCode.h" | ||
|
|
||
| typedef struct List List; | ||
|
|
||
| List * createList(void); | ||
| ErrorCode addElement(List * list, char * const value); | ||
| char ** writeToDefaultArray(List const * const list, size_t *length); | ||
| void deleteList(List ** const list); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include <stdio.h> | ||
|
|
||
| #include "readFile.h" | ||
| #include "errorCode.h" | ||
| #include "../../String/String.h" | ||
|
|
||
| char * readFile(const char * const fileName, size_t *len) | ||
| { | ||
| FILE *file = fopen(fileName, "r"); | ||
| if (file == NULL) | ||
| { | ||
| return NULL; | ||
| } | ||
| char * text = getString(len, file, EOF); | ||
| fclose(file); | ||
| return text; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #pragma once | ||
|
|
||
| char * readFile(const char * const fileName, size_t *len); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||
| #include <stdio.h> | ||||||
| #include <locale.h> | ||||||
|
|
||||||
| #include "findComments/findComments.h" | ||||||
| #include "tests/test.h" | ||||||
|
|
||||||
| #define FILENAME "../file.txt" | ||||||
|
|
||||||
| typedef enum exitCode | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| programFinishedCorrectly, | ||||||
| programFailedTests, | ||||||
| notEnoughMemory | ||||||
| } exitCode; | ||||||
|
|
||||||
| int main(void) | ||||||
| { | ||||||
| setlocale(LC_ALL, "Russian"); | ||||||
|
|
||||||
| if (!test()) | ||||||
| { | ||||||
| printf("Программа сейчас не работает\n"); | ||||||
| return programFailedTests; | ||||||
| } | ||||||
|
|
||||||
| size_t len = 0; | ||||||
| char **comments = findComments(FILENAME, &len); | ||||||
| if (comments == NULL) | ||||||
| { | ||||||
| printf("Недостаточно памяти\n"); | ||||||
| return notEnoughMemory; | ||||||
| } | ||||||
|
|
||||||
| printf("Комментарии из файла:\n"); | ||||||
| for (size_t i = 0; i < len; ++i) | ||||||
| { | ||||||
| printf("%s\n", comments[i]); | ||||||
| free(comments[i]); | ||||||
| } | ||||||
| free(comments); | ||||||
| return programFinishedCorrectly; | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
У Вас в параметрах функций
*пишется с пробелом, при объявлении локальных переменных — нет, а лучше бы единообразно (правильнее*писать слитно с именем переменной, чтобы не путать читателя относительно того, кто именно указатель).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А ещё s как имя переменной — не очень