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
13 changes: 13 additions & 0 deletions semester_1/kr1.2/task3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.0.0)
project(task3 VERSION 0.1.0 LANGUAGES C)

include(CTest)
enable_testing()

add_executable(task3 main.c
read/read.c
tests/test.c)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
6 changes: 6 additions & 0 deletions semester_1/kr1.2/task3/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
aafgbbbaaaaiiiiiiefkjg;dfaghpdufdahhghhghhhhhghdghrhiag'ffdgsz
fsdgsyjukhgfretfdsf



hytjhkytresdsxzdfggggghhhhhhrrrrr
40 changes: 40 additions & 0 deletions semester_1/kr1.2/task3/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>

#include "read/read.h"
#include "tests/test.h"

#define PROGRAM_FINISHED_CORRECTLY 0
#define PROGRAM_FAILED_TESTS 1
#define MEMORY_ERROR 2
#define FILE_READING_ERROR 3

#define FILENAME "../file.txt"

int main(void)
{
// if (!test())
// {
// printf("Программа сейчас не работает\n");
// return PROGRAM_FAILED_TESTS;
// }
Comment on lines +16 to +20

Choose a reason for hiding this comment

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

:(


FILE *file = fopen(FILENAME, "r");
if (file == NULL)
{
printf("Не удается получить доступ к файлу\n");
return FILE_READING_ERROR;
}


size_t len = 0;
char *text = getString(&len, file, EOF);
fclose(file);
if (text == NULL)
{
printf("Закончилась память\n");
return MEMORY_ERROR;
}
printf("%s\n", text);
free(text);
}
52 changes: 52 additions & 0 deletions semester_1/kr1.2/task3/read/read.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "read.h"

char *getString(size_t *const len, FILE *file, char const endOfLine);

Choose a reason for hiding this comment

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

Тут точка с запятой лишняя, не компилируется

{
*len = 0;
size_t capacity = 1;
char *s = (char *)malloc(sizeof(char));
if (s == NULL)
{
return NULL;
}
char previousChar = '\0';
for (char c = fgetc(file); c != endOfLine && c != EOF; c = fgetc(file))
{
if (c != previousChar)
{
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;
}
previousChar = c;
}
}

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);
}
return copy;
}

Choose a reason for hiding this comment

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

strdup?

8 changes: 8 additions & 0 deletions semester_1/kr1.2/task3/read/read.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stdbool.h>
#include <string.h>
#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.

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

Empty file.
Empty file.
Empty file.
57 changes: 57 additions & 0 deletions semester_1/kr1.2/task3/tests/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "test.h"
#include "../read/read.h"

const bool testCase(char const *const testFileName, char const *const answerFileName)
{
FILE *file = fopen(testFileName, "r");
if (file == NULL)
{
return false;
}
size_t len = 0;
char *str = getString(&len, file, EOF);
fclose(file);
if (str == NULL)
{
return false;
}
FILE *answerFile = fopen(answerFileName, "r");
if (answerFile == NULL)
{
free(str);
return false;
}
char *str2 = getString(&len, answerFile, EOF);
fclose(answerFile);
if (str2 == NULL)
{
free(str);
return false;
}
bool answer = strcmp(str, str2) == 0;
free(str);
free(str2);
return answer;
}

const bool test(void)
{
const size_t testNumber = 3;
char const *const testFileNames[testNumber] = {"../tests/testFile1.txt", "../tests/testFile2.txt", "../tests/testFile3.txt"};

Choose a reason for hiding this comment

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

Стандарт не позволяет так делать, testNumber должен быть константным литералом, объявленным через #define. Не компилится.

char const *const answerFileNames[testNumber] = {"../tests/answerFile1.txt",
"../tests/answerFile1.txt",
"../tests/answerFile1.txt"};

for (size_t i = 0; i < testNumber; ++i)
{
if (!testCase(testFileNames[i], answerFileNames[i]))
{
return false;
}
}
return true;
}
5 changes: 5 additions & 0 deletions semester_1/kr1.2/task3/tests/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <stdbool.h>

const bool test(void);
Empty file.
Empty file.