Repository navigation
Expand file tree
/
Copy pathStr.cpp
More file actions
209 lines (144 loc) · 4.58 KB
/
Copy pathStr.cpp
File metadata and controls
209 lines (144 loc) · 4.58 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include "str.h"
#include <string>
using namespace std;
int str::len_local(const char* String) const {
int length = 0;
if(String != NULL)
for (int i = 0; String[i] != '\0'; ++i) {
length++;
}
return length;
}
str::str() {
ptr = nullptr;
}
str::str(const char* String) {
int length = len_local(String);
ptr = new char[sizeof(char) * length + 1];
if (String != NULL)
for (int i = 0; i <= length; ++i) { // <= to include '\0'
ptr[i] = String[i];
}
else
ptr[0] = '\0';
}
str::str(const str& obj) {
int length = len_local(obj.ptr);
ptr = new char[sizeof(char) * length + 1];
for (int i = 0; i <= length; ++i) { // <= to include '\0'
ptr[i] = obj.ptr[i];
}
}
str::str(const string& String) {
int length = String.size();
ptr = new char[sizeof(char) * length + 1];
for (int i = 0; i <= length; ++i) { // <= to include '\0'
ptr[i] = String[i];
}
}
int str::len() const {
return len_local(ptr);
}
string str::str2string() const {
return string(ptr);
}
str::~str() {
delete[] ptr; //to delete the whole array of char, "delete ptr" will delete the first location in the array;
}
str::operator const char* () const {
return ptr;
}
void str::operator = (const str& obj) {
if (this == &obj) return; //to avoid self assignment
delete[] ptr; // to prevent memory leaks, if we left that, the old
int length = len_local(obj.ptr);
ptr = new char[sizeof(char) * length + 1];
for (int i = 0; i <= length; ++i) { // <= to include '\0'
ptr[i] = obj.ptr[i];
}
}
void str::operator = (const char* String) {
delete[] ptr; // to prevent memory leaks, if we left that, the old
int length = len_local(String);
ptr = new char[sizeof(char) * length + 1];
for (int i = 0; i <= length; ++i) { // <= to include '\0'
ptr[i] = String[i];
}
}
void str::operator = (const string& String) {
delete[] ptr; // to prevent memory leaks, if we left that, the old
int length = String.size();
ptr = new char[sizeof(char) * length + 1];
for (int i = 0; i <= length; ++i) { // <= to include '\0'
ptr[i] = String[i];
}
}
str str::operator + (const str& obj) const {
int length1 = len_local(this->ptr);
int length2 = len_local(obj.ptr);
str result;
result.ptr = new char[sizeof(char) * (length1 + length2) + 1];
for (int i = 0; i < length1; ++i) {
result.ptr[i] = this->ptr[i];
}
for (int i = 0; i <= length2; ++i) {
result.ptr[length1 + i] = obj.ptr[i];
}
return result;
}
str str::operator + (const char* string) const {
str temp = str(string);
return *this + temp;
}
str str::operator + (const string& String) const {
str result = str(String);
return *this + result;
}
str str::operator + (const int& num) const {
str result = to_string(num).c_str();
result += ptr;
return result;
}
void str::operator += (const str& obj) {
int length1 = len_local(this->ptr);
int length2 = len_local(obj.ptr);
str result;
result.ptr = new char[sizeof(char) * (length1 + length2) + 1];
for (int i = 0; i < length1; ++i) {
result.ptr[i] = this->ptr[i];
}
for (int i = 0; i <= length2; ++i) {
result.ptr[length1 + i] = obj.ptr[i];
}
*this = result;
}
void str::operator += (const string& String) {
str result = str(String);
*this = *this + String;
}
void str::operator += (const int& num) {
str result = str();
result = result + num;
*this = *this + result;
}
bool str::operator == (const str& obj) const {
if (len_local(obj.ptr) != len_local(this->ptr)) {
return false;
}
for (int i = 0; *(obj.ptr + i) != '\0'; i++) {
if (*(obj.ptr + i) != *(this->ptr + i))
return false;
}
return true;
}
void str::operator += (const char* String) {
str result = str(String);
*this = *this + String;
}
std::istream& operator>>(std::istream& is, str& obj){
char temp[1024];
is >> temp; //store in temp
obj = temp; // put temp in obj, why not "is >> obj.ptr" because we do not know the length of the entered data and storing that in obj.ptr, can write the data in places byond the places we have access to. and that will cause problems
return is; // Return the input stream to allow chaining of input operations
}