-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamClasses.cpp
More file actions
161 lines (138 loc) · 2.54 KB
/
StreamClasses.cpp
File metadata and controls
161 lines (138 loc) · 2.54 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#define _CRT_SECURE_NO_WARNINGS
#include "StreamClasses.h"
#include <iostream>
#include <cstring>
#include <cassert>
#include <fstream>
#include <exception>
Stream::Stream(int capacity)
{
this->capacity = capacity;
this->array = new char[capacity];
this->size = 0;
}
Stream::Stream()
{
this->capacity = 10;
this->array = new char[capacity];
this->size = 0;
*(this->array + size++) = '\0';
}
Stream::Stream(const char* str) : Stream()
{
setData(str);
}
Stream::Stream(const Stream& other)
{
this->capacity = other.capacity;
array = new char[capacity];
this->size = other.size;
for(int i = 0; i < size; ++i)
{
*(array + i) = other.array[i];
}
}
Stream::~Stream()
{
delete[] array;
}
void Stream::setData(const char* str)
{
if (str == nullptr)
{
str = "";
}
int strLength = strlen(str) + 1;
while (strLength > capacity)
{
expandArray();
}
strcpy(array, str);
size = strLength;
}
char* Stream::readInput()
{
char ch;
while (std::cin.get(ch))
{
if (size == capacity)
{
expandArray();
}
array[size++] = ch;
}
if (capacity <= size)
{
expandArray();
}
array[size] = '\0';
return array;
}
char* Stream::getArray()
{
return array;
}
char* Stream::getArray() const
{
return array;
}
int Stream::sizeOfArray() const
{
return this->size;
}
void Stream::incrementSize()
{
size++;
}
int Stream::capacityOfArray()
{
return this->capacity;
}
void Stream::addElement(const char value)
{
if(size == capacity)
{
expandArray();
}
*(array + size) = value;
size++;
}
void Stream::expandArray()
{
char* tempArray = new char[capacity * 2];
capacity = capacity * 2;
for (int i = 0; i < size; i++)
{
*(tempArray + i) = *(array + i);
}
delete[] array;
array = tempArray;
}
char Stream::operator[] (int index) const
{
assert(index >= 0 && index < size);
return *(array + index);
}
Stream& Stream::operator=(const Stream& other)
{
if (this != &other)
{
setData(other.array);
}
return *this;
}
FileStream::FileStream(const char* filename) : Stream(0)
{
std::ifstream file(filename);
if (!file)
{
std::cerr << "Could not open the file you've chose: " << filename << std::endl;
return;
}
char ch;
while (file.get(ch))
{
this->addElement(ch);
}
file.close();
}