-
Notifications
You must be signed in to change notification settings - Fork 1
/
String.cpp
109 lines (95 loc) · 2.26 KB
/
String.cpp
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
/**
* GreenPois0n Arsenic - String.cpp
* Copyright (C) 2010 Chronic-Dev Team
* Copyright (C) 2010 Joshua Hill
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include <cstring>
#include "String.h"
namespace GP {
String::String(const char* data) :
Node(kStringNode) {
mData = strdup(data);
mLength = strlen(data);
}
String::String(const String& data) :
Node(kStringNode) {
mData = strdup(data.get());
mLength = strlen(data.get());
}
String::~String() {
if(mData) {
free(mData);
mData = NULL;
}
}
const char* String::get() {
return mData;
}
void String::set(const char* data) {
if(data) {
if(mData) {
delete mData;
mData = NULL;
}
mData = strdup(data);
}
bool String::compare(const String& what) {
if(!strcmp(mData, what.get())) {
return true;
}
return false;
}
bool String::compare(const char* what) {
if(!strcmp(mData, what)) {
return true;
}
return false;
}
int String::length() {
mLength = strlen(mData);
return mLength;
}
void concat(const char* what) {
unsigned int addSize = strlen(what);
unsigned int oldSize = strlen(mData);
unsigned int newSize = oldSize + addSize;
if(newSize > mLength) {
unsigned char* tmp = malloc(newSize+1);
if(tmp == NULL) {
return NULL;
}
strncpy(tmp, mData, newSize);
strncat(tmp, what, newSize);
free(mData);
mData = tmp;
}
}
void concat(String& what) {
unsigned int addSize = what.length();
unsigned int oldSize = strlen(mData);
unsigned int newSize = oldSize + addSize;
if(newSize > mLength) {
unsigned char* tmp = malloc(newSize+1);
if(tmp == NULL) {
return NULL;
}
strncpy(tmp, mData, newSize);
strncat(tmp, what.get(), newSize);
free(mData);
mData = tmp;
}
}
}