Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions semester_1/homework12/String/String.c
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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У Вас в параметрах функций * пишется с пробелом, при объявлении локальных переменных ­— нет, а лучше бы единообразно (правильнее * писать слитно с именем переменной, чтобы не путать читателя относительно того, кто именно указатель).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А ещё s как имя переменной — не очень

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len должна быть на 1 больше strlen, чтобы ещё нулевой байт вместить

}
return copy;
}
8 changes: 8 additions & 0 deletions semester_1/homework12/String/String.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stdbool.h>
#include <string.h>

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо комментарии

7 changes: 7 additions & 0 deletions semester_1/homework12/comments/file.txt
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
}
8 changes: 8 additions & 0 deletions semester_1/homework12/comments/findComments/errorCode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

typedef enum ErrorCode
{
ok,
memoryError,
fileReadingError
} ErrorCode;
135 changes: 135 additions & 0 deletions semester_1/homework12/comments/findComments/findComments.c
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;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лишний пробел тут и ниже

case firstSlash:
{
if (c == '*')
{
return firstStar;
}
return start;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//*

Choose a reason for hiding this comment

The 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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тоже нет, /* текст **/ — валидный комментарий, просто * является его частью

}

default:
return start;
break;

Choose a reason for hiding this comment

The 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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start + end, не end - start?

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;
}
6 changes: 6 additions & 0 deletions semester_1/homework12/comments/findComments/findComments.h
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);
78 changes: 78 additions & 0 deletions semester_1/homework12/comments/findComments/list.c
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* const в .h тоже

{
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

переименовать функцию в convertToArray и внутри удалять список
указатель на value занулять

++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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

раскомментировать

free(temp);
}
free(*list);
*list = NULL;
}
10 changes: 10 additions & 0 deletions semester_1/homework12/comments/findComments/list.h
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);
17 changes: 17 additions & 0 deletions semester_1/homework12/comments/findComments/readFile.c
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;
}
3 changes: 3 additions & 0 deletions semester_1/homework12/comments/findComments/readFile.h
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);
42 changes: 42 additions & 0 deletions semester_1/homework12/comments/main.c
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
typedef enum exitCode
typedef enum ExitCode

{
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;
}
Loading