net: partial implementation of tcp
[quarnos.git] / libs / string.cpp
blob6cd13792d7d6dfd2c8627fdd8037d518a36dbbde
1 /* Quarn OS
3 * String
5 * Copyright (C) 2008-2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "string.h"
24 #include "buffer.h"
26 const buffer buffer::empty = buffer((void*)0, 0);
28 #define assert(x,y)
30 string::string(const char *text) : error('\0') {
31 data = new char[strlen(text) + 1];
32 strcpy(data, text);
33 used = 1;
36 void string::save(const char *text) {
37 data = new char[strlen(text) + 1];
38 strcpy(data, text);
41 /* Consider copy on write strategy */
42 string::string(const string &x) : error('\0') {
43 assert("string: copying null string", x.null());
44 data = new char[x.length() + 1];
45 if (x.data)
46 strcpy(data, x.data);
49 string::string() : data((char*)0) {}
51 string::string(int _val) {
52 unsigned int val = _val;
53 data = new char[20];
55 for (int i = 0; i < 20; i++)
56 data[i] = 0;
58 int i = 0;
59 do {
60 data[i++] = val % 10 + '0';
61 } while ((val /= 10) > 0);
63 data[i] = 0;
65 reverse();
68 string::string(const unsigned int _val) {
69 unsigned int val = _val;
70 data = new char[20];
72 for (int i = 0; i < 20; i++)
73 data[i] = 0;
75 int i = 0;
76 do {
77 data[i++] = val % 10 + '0';
78 } while ((val /= 10) > 0);
80 data[i] = 0;
82 reverse();
86 string::string(const unsigned int _val, bool) {
87 const char translate[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
88 unsigned int val = _val;
90 data = new char[20];
92 for (int i = 0; i < 20; i++)
93 data[i] = 0;
95 int i = 0;
96 do {
97 data[i++] = translate[val & 0xf];
98 } while ((val >>= 4) != 0);
100 data[i] = 0;
102 reverse();
106 string::~string() {
107 if (data)
108 delete data;
111 int string::length() const {
112 if (!data)
113 return 0;
115 return strlen(data);
118 void string::reverse() {
119 assert("string: operations on a null string", null());
120 char temp;
121 int len = length();
123 for (int i = 0, j = length() - 1; i < j; i++, j--) {
124 temp = data[i];
125 data[i] = data[j];
126 data[j] = temp;
129 data[len] = 0;
132 char &string::operator[](const int index) {
133 assert("string: operations on a null string", null());
134 if (index < length() + 1)
135 return data[index];
136 else
137 return error;
140 const char &string::operator[](const int index) const {
141 assert("string: operations on a null string", null());
142 if (index < length() + 1)
143 return data[index];
144 else
145 return error;
148 bool string::operator==(const char *x) const {
149 assert("string: operations on a null string", null());
150 assert("string: operations on a null string", x == 0 || strlen(x) == 0);
152 if (!strcmp(data, x))
153 return true;
154 else
155 return false;
158 bool string::operator==(const string &x) const {
159 assert("string: operations on a null string", null());
160 // assert("string: operations on a null string", x.null());
162 if (!strcmp(data, x.data))
163 return true;
164 else
165 return false;
168 string string::operator+(const int x) const {
169 assert("string: operations on a null string", null());
170 string str(x);
171 return operator+(str);
174 string string::operator+(const string &x) const {
175 assert("string: operations on a null string", null());
177 char *temp = new char[length() + x.length() + 1];
178 if (data)
179 strcpy(temp, data);
180 if (x && data)
181 strcat(temp, x);
182 else if (x)
183 strcpy(temp, x);
185 temp[length() + x.length()] = 0;
187 string val;
188 val.data = temp;
189 return val;
192 void string::operator+=(const string &x) {
193 char *temp = new char[length() + x.length() + 1];
195 if (data)
196 strcpy(temp, data);
197 if (x && data)
198 strcat(temp, x);
199 else if (x)
200 strcpy(temp, x);
202 temp[length() + x.length()] = 0;
204 if (data)
205 delete data;
207 data = temp;
210 string::operator const char*() const {
211 assert("string: operations on a null string", null());
212 return data;
215 bool string::null() const {
216 return length() == 0;
219 void string::operator=(const string &x) {
220 if (data)
221 delete data;
223 assert("string: copying null string", x.null());
224 data = new char[x.length() + 1];
226 strcpy(data, x.data);
229 buffer string::to_mem() const {
230 return buffer(data, strlen(data));
233 bool string::is_digit(char x) {
234 return (x >= '0' && x <= '9');
237 int strlen(const char *a) {
238 int i;
239 for (i = 0; a[i]; i++);
240 return i;
243 int strcmp(const char *a,const char *b) {
244 int i;
245 for (i = 0; a[i] && b[i] && a[i] == b[i]; i++);
246 if (a[i] != b[i]) return 1;
247 return 0;
250 int strncmp(const char *a,const char *b, int n) {
251 int i;
252 for (i = 0; a[i] && b[i] && a[i] == b[i] && i < n; i++);
253 if (a[i] != b[i] && i != n) return 1;
254 return 0;
257 char *strcat(char *a, const char *b) {
258 return strcpy(&(a[strlen(a)]), b);
261 char *strcpy(char *a, const char *b) {
262 return (char*)memcpy((void*)a, (const void *)b, strlen(b) + 1);
265 char *strncpy(char *a, const char *b, int n) {
266 return (char*)memcpy((void*)a, (const void *)b, n);
269 void *memcpy(void * a, const void * b, int count) {
270 for (int i = 0; i < count; i++)
271 ((char *)a)[i] = ((const char *)b)[i];
272 return a;
275 void *memset(void *a, int sign, int count) {
276 for (int i = 0; i < count; i++)
277 ((unsigned char *)a)[i] = (unsigned char)sign;
278 return a;