-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterClasses.cpp
More file actions
107 lines (89 loc) · 2.3 KB
/
FilterClasses.cpp
File metadata and controls
107 lines (89 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#define _CRT_SECURE_NO_WARNINGS
#include "FilterClasses.h"
#include <cassert>
#include <cstring>
Filter::Filter(Stream& stream, const char* wordToBeFound) : stream(stream), wordToBeFound(wordToBeFound), dataFromStream(stream.
getArray())
{
}
Filter::Filter(Stream& stream) : stream(stream), dataFromStream(stream.getArray())
{
}
char* Filter::searchWordInInput()
{
char* startLine = dataFromStream;
char* endLine = nullptr;
while ((endLine = strchr(startLine, '\n')) != nullptr)
{
int length = endLine - startLine;
char* substring = new char[length + 1];
strncpy(substring, startLine, length);
substring[length] = '\0';
if (strstr(substring, wordToBeFound) != nullptr)
{
return substring;
}
startLine = endLine + 1;
delete[] substring;
}
return nullptr;
}
const Stream& Filter::getStream()
{
return (this->stream);
}
CopyFilter::CopyFilter(Stream& stream) : Filter(stream)
{
}
char* CopyFilter::getOriginalContent()
{
return this->getStream().getArray();
}
CapitaliseFilter::CapitaliseFilter(Stream& stream, const char* wordToBeFound) : Filter(stream, wordToBeFound)
{
capitaliseWords();
}
void CapitaliseFilter::capitaliseWords()
{
char* originalContent = this->getStream().getArray();
bool newWord = true;
for (int i = 0; originalContent[i] != '\0'; ++i)
{
if (std::isalpha(originalContent[i]))
{
if (newWord)
{
std::toupper(originalContent[i]);
newWord = false;
}
else
{
std::tolower(originalContent[i]);
}
}
else
{
if (originalContent[i] == ' ')
{
newWord = true;
}
}
}
}
FunctionFilter::FunctionFilter(Stream& stream, const char* wordToBeFound) : Filter(stream, wordToBeFound)
{
}
char* FunctionFilter::applyEncodingFunction()
{
char* originalContent = this->getStream().getArray();
char* result = encodingFunction(originalContent);
return result;
}
void FunctionFilter::setEncodingFunction(char* (*function)(char*))
{
if(!function)
{
std::cerr << "Encoding function is nullptr!" << std::endl;
}
encodingFunction = function;
}