usb: getting string descriptors, minor improvements
[quarnos.git] / libs / string.cpp
blobbafa37b207443abc7c81362c0e2b528d7346c1a9
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 "arch/low/general.h"
25 #include "string.h"
26 #include "buffer.h"
28 #include "list.h"
30 const buffer buffer::empty = buffer((void*)0, 0);
32 #define assert(x,y)
34 string::string(const char *text) : error('\0') {
35 data = new char[strlen(text) + 1];
36 strcpy(data, text);
37 used = 1;
40 void string::save(const char *text) {
41 data = new char[strlen(text) + 1];
42 strcpy(data, text);
45 /* Consider copy on write strategy */
46 string::string(const string &x) : error('\0') {
47 assert("string: copying null string", x.null());
48 data = new char[x.length() + 1];
49 if (x.data)
50 strcpy(data, x.data);
53 string::string() : data((char*)0) {}
55 string::string(int _val) {
56 unsigned int val = _val;
57 data = new char[20];
59 for (int i = 0; i < 20; i++)
60 data[i] = 0;
62 int i = 0;
63 do {
64 data[i++] = val % 10 + '0';
65 } while ((val /= 10) > 0);
67 data[i] = 0;
69 reverse();
72 string::string(const unsigned int _val) {
73 unsigned int val = _val;
74 data = new char[20];
76 for (int i = 0; i < 20; i++)
77 data[i] = 0;
79 int i = 0;
80 do {
81 data[i++] = val % 10 + '0';
82 } while ((val /= 10) > 0);
84 data[i] = 0;
86 reverse();
90 string::string(const unsigned int _val, bool) {
91 const char translate[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
92 unsigned int val = _val;
94 data = new char[20];
96 for (int i = 0; i < 20; i++)
97 data[i] = 0;
99 int i = 0;
100 do {
101 data[i++] = translate[val & 0xf];
102 } while ((val >>= 4) != 0);
104 data[i] = 0;
106 reverse();
110 string::~string() {
111 if (data)
112 delete data;
115 int string::length() const {
116 if (!data)
117 return 0;
119 return strlen(data);
122 void string::reverse() {
123 assert("string: operations on a null string", null());
124 char temp;
125 int len = length();
127 for (int i = 0, j = length() - 1; i < j; i++, j--) {
128 temp = data[i];
129 data[i] = data[j];
130 data[j] = temp;
133 data[len] = 0;
136 char &string::operator[](const int index) {
137 assert("string: operations on a null string", null());
138 if (index < length() + 1)
139 return data[index];
140 else
141 return error;
144 const char &string::operator[](const int index) const {
145 assert("string: operations on a null string", null());
146 if (index < length() + 1)
147 return data[index];
148 else
149 return error;
152 list<string> string::split(char sign) const {
153 char *text = new char[strlen(data) + 1];
154 strcpy(text, data);
156 list<string> subs;
158 int i = 0, j = 0;
159 while (true) {
160 for (; text[i] && text[i] != sign; i++);
161 if (!text[i]) {
162 subs.add(&text[j]);
163 break;
164 } else {
165 text[i] = 0;
166 subs.add(&text[j]);
167 i++;
168 j = i;
172 delete []text;
174 return subs;
177 bool string::operator==(const char *x) const {
178 assert("string: operations on a null string", null());
179 assert("string: operations on a null string", x == 0 || strlen(x) == 0);
181 if (!strcmp(data, x))
182 return true;
183 else
184 return false;
187 bool string::operator==(const string &x) const {
188 assert("string: operations on a null string", null());
189 // assert("string: operations on a null string", x.null());
191 if (!strcmp(data, x.data))
192 return true;
193 else
194 return false;
197 string string::operator+(const int x) const {
198 assert("string: operations on a null string", null());
199 string str(x);
200 return operator+(str);
203 string string::operator+(const string &x) const {
204 assert("string: operations on a null string", null());
206 char *temp = new char[length() + x.length() + 1];
207 if (data)
208 strcpy(temp, data);
209 if (x && data)
210 strcat(temp, x);
211 else if (x)
212 strcpy(temp, x);
214 temp[length() + x.length()] = 0;
216 string val;
217 val.data = temp;
218 return val;
221 void string::operator+=(const string &x) {
222 char *temp = new char[length() + x.length() + 1];
224 if (data)
225 strcpy(temp, data);
226 if (x && data)
227 strcat(temp, x);
228 else if (x)
229 strcpy(temp, x);
231 temp[length() + x.length()] = 0;
233 if (data)
234 delete []data;
236 data = temp;
239 string::operator const char*() const {
240 assert("string: operations on a null string", null());
241 return data;
244 const char *string::to_ascii() const {
245 assert("string: operations on a null string", null());
246 return data;
249 string string::from_utf16(char *_data) {
250 u16 *utf_data = (u16*)_data;
251 int size;
252 for (size = 0; utf_data[size]; size++);
254 u8 *data = new u8[size];
255 for (int i = 0; i < size; i++)
256 data[i] = utf_data[i];
258 return string((char*)data);
261 bool string::null() const {
262 return length() == 0;
265 void string::operator=(const string &x) {
266 if (data)
267 delete data;
269 assert("string: copying null string", x.null());
270 data = new char[x.length() + 1];
272 strcpy(data, x.data);
275 buffer string::to_mem() const {
276 return buffer(data, strlen(data) + 1);
279 bool string::is_digit(char x) {
280 return (x >= '0' && x <= '9');
283 int strlen(const char *a) {
284 int i;
285 for (i = 0; a[i]; i++);
286 return i;
289 int strcmp(const char *a,const char *b) {
290 int i;
291 for (i = 0; a[i] && b[i] && a[i] == b[i]; i++);
292 if (a[i] != b[i]) return 1;
293 return 0;
296 int strncmp(const char *a,const char *b, int n) {
297 int i;
298 for (i = 0; a[i] && b[i] && a[i] == b[i] && i < n; i++);
299 if (a[i] != b[i] && i != n) return 1;
300 return 0;
303 char *strcat(char *a, const char *b) {
304 return strcpy(&(a[strlen(a)]), b);
307 char *strcpy(char *a, const char *b) {
308 return (char*)memcpy((void*)a, (const void *)b, strlen(b) + 1);
311 char *strncpy(char *a, const char *b, int n) {
312 return (char*)memcpy((void*)a, (const void *)b, n);
315 void *memcpy(void * a, const void * b, int count) {
316 for (int i = 0; i < count; i++)
317 ((char *)a)[i] = ((const char *)b)[i];
318 return a;
321 void *memset(void *a, int sign, int count) {
322 for (int i = 0; i < count; i++)
323 ((unsigned char *)a)[i] = (unsigned char)sign;
324 return a;