net: data reception system, fixed bugs in rtl8139
[quarnos.git] / libs / string.cpp
blobd7dc79087615313056f5f9a701739801efb54774
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"
25 #define assert(x,y)
27 string::string(const char *text) : error('\0') {
28 data = new char[strlen(text) + 1];
29 strcpy(data, text);
30 used = 1;
33 void string::save(const char *text) {
34 data = new char[strlen(text) + 1];
35 strcpy(data, text);
38 /* Consider copy on write strategy */
39 string::string(const string &x) : error('\0') {
40 assert("string: copying null string", x.null());
41 data = new char[x.length() + 1];
42 if (x.data)
43 strcpy(data, x.data);
46 string::string() : data((char*)0) {}
48 string::string(int _val) {
49 unsigned int val = _val;
50 data = new char[20];
52 for (int i = 0; i < 20; i++)
53 data[i] = 0;
55 int i = 0;
56 do {
57 data[i++] = val % 10 + '0';
58 } while ((val /= 10) > 0);
60 data[i] = 0;
62 reverse();
65 string::string(const unsigned int _val) {
66 unsigned int val = _val;
67 data = new char[20];
69 for (int i = 0; i < 20; i++)
70 data[i] = 0;
72 int i = 0;
73 do {
74 data[i++] = val % 10 + '0';
75 } while ((val /= 10) > 0);
77 data[i] = 0;
79 reverse();
83 string::string(const unsigned int _val, bool) {
84 const char translate[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
85 unsigned int val = _val;
87 data = new char[20];
89 for (int i = 0; i < 20; i++)
90 data[i] = 0;
92 int i = 0;
93 do {
94 data[i++] = translate[val & 0xf];
95 } while ((val >>= 4) != 0);
97 data[i] = 0;
99 reverse();
103 string::~string() {
104 if (data)
105 delete data;
108 int string::length() const {
109 if (!data)
110 return 0;
112 return strlen(data);
115 void string::reverse() {
116 assert("string: operations on a null string", null());
117 char temp;
118 int len = length();
120 for (int i = 0, j = length() - 1; i < j; i++, j--) {
121 temp = data[i];
122 data[i] = data[j];
123 data[j] = temp;
126 data[len] = 0;
129 char &string::operator[](const int index) {
130 assert("string: operations on a null string", null());
131 if (index < length() + 1)
132 return data[index];
133 else
134 return error;
137 const char &string::operator[](const int index) const {
138 assert("string: operations on a null string", null());
139 if (index < length() + 1)
140 return data[index];
141 else
142 return error;
145 bool string::operator==(const char *x) const {
146 assert("string: operations on a null string", null());
147 assert("string: operations on a null string", x == 0 || strlen(x) == 0);
149 if (!strcmp(data, x))
150 return true;
151 else
152 return false;
155 bool string::operator==(const string &x) const {
156 assert("string: operations on a null string", null());
157 // assert("string: operations on a null string", x.null());
159 if (!strcmp(data, x.data))
160 return true;
161 else
162 return false;
165 string string::operator+(const int x) const {
166 assert("string: operations on a null string", null());
167 string str(x);
168 return operator+(str);
171 string string::operator+(const string &x) const {
172 assert("string: operations on a null string", null());
174 char *temp = new char[length() + x.length() + 1];
175 if (data)
176 strcpy(temp, data);
177 if (x && data)
178 strcat(temp, x);
179 else if (x)
180 strcpy(temp, x);
182 temp[length() + x.length()] = 0;
184 string val;
185 val.data = temp;
186 return val;
189 void string::operator+=(const string &x) {
190 char *temp = new char[length() + x.length() + 1];
192 if (data)
193 strcpy(temp, data);
194 if (x && data)
195 strcat(temp, x);
196 else if (x)
197 strcpy(temp, x);
199 temp[length() + x.length()] = 0;
201 if (data)
202 delete data;
204 data = temp;
207 string::operator const char*() const {
208 assert("string: operations on a null string", null());
209 return data;
212 bool string::null() const {
213 return length() == 0;
216 void string::operator=(const string &x) {
217 if (data)
218 delete data;
220 assert("string: copying null string", x.null());
221 data = new char[x.length() + 1];
223 strcpy(data, x.data);
226 buffer string::to_mem() const {
227 return buffer(data, strlen(data));
230 bool string::is_digit(char x) {
231 return (x >= '0' && x <= '9');
234 int strlen(const char *a) {
235 int i;
236 for (i = 0; a[i]; i++);
237 return i;
240 int strcmp(const char *a,const char *b) {
241 int i;
242 for (i = 0; a[i] && b[i] && a[i] == b[i]; i++);
243 if (a[i] != b[i]) return 1;
244 return 0;
247 int strncmp(const char *a,const char *b, int n) {
248 int i;
249 for (i = 0; a[i] && b[i] && a[i] == b[i] && i < n; i++);
250 if (a[i] != b[i] && i != n) return 1;
251 return 0;
254 char *strcat(char *a, const char *b) {
255 return strcpy(&(a[strlen(a)]), b);
258 char *strcpy(char *a, const char *b) {
259 return (char*)memcpy((void*)a, (const void *)b, strlen(b) + 1);
262 char *strncpy(char *a, const char *b, int n) {
263 return (char*)memcpy((void*)a, (const void *)b, n);
266 void *memcpy(void * a, const void * b, int count) {
267 for (int i = 0; i < count; i++)
268 ((char *)a)[i] = ((const char *)b)[i];
269 return a;
272 void *memset(void *a, int sign, int count) {
273 for (int i = 0; i < count; i++)
274 ((unsigned char *)a)[i] = (unsigned char)sign;
275 return a;