00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "src/common/shared.hpp"
00019 #include "src/common/string.hpp"
00020
00021
00022
00023 #include "src/memory/align.hpp"
00024 #include "src/memory/memset.hpp"
00025 #include "src/memory/heap.hpp"
00026 #include "src/common/string.hpp"
00027
00028 const int DEF_ERROR = 1;
00029 const int DEF_SUCCESS = 0;
00030
00031 #define kmalloc(size) ((char*)Memory::Heap::heap0.malloc(size, 1,"n/a",NULL))
00032 #define kfree(ptr) (Memory::Heap::heap0.free(ptr))
00033
00034 char *krealloc_old(char *s, int ss)
00035 {
00036 char *buf = kmalloc(ss);
00037 if (buf==NULL)
00038 return NULL;
00039 memmove(buf,s,ss);
00040 kfree(s);
00041 return buf;
00042 }
00043
00044
00045
00046 string::string()
00047 {
00048
00049 asciiz = (char*) 0;
00050 mx_size = 0;
00051 }
00052
00053
00054 string::string(char *str)
00055 {
00056
00057 asciiz = str;
00058 mx_size = length();
00059 }
00060
00061
00062
00063 string::string(char *str, uint32 size)
00064 {
00065
00066 asciiz = kmalloc(size + 1);
00067 mx_size = size;
00068
00069 copy(str, size);
00070 }
00071
00072
00073 string::string (uint32 size)
00074 {
00075 asciiz = kmalloc(size + 1);
00076 mx_size = size;
00077
00078
00079 asciiz[0] = 0;
00080 }
00081
00082
00083 string::~string()
00084 {
00085 if (mx_size != 0)
00086 {
00087
00088 kfree(asciiz);
00089 asciiz = 0; mx_size = 0;
00090 }
00091 }
00092
00093
00094 void string::inval()
00095 {
00096
00097 asciiz = 0;
00098
00099 mx_size = 0;
00100 }
00101
00102
00103 char *string::v()
00104 {
00105 return asciiz;
00106 }
00107
00108
00109 uint32 string::verify(uint32 size)
00110 {
00111 char *new_buf;
00112
00113
00114 if (mx_size == 0)
00115 {
00116 nw (size+1);
00117 return DEF_SUCCESS;
00118 }
00119
00120
00121 if (mx_size < size)
00122 {
00123
00124 size+=0x20;
00125
00126 new_buf = krealloc_old (asciiz, size + 1);
00127 if (new_buf == 0)
00128
00129 return DEF_ERROR;
00130
00131 kfree( asciiz );
00132 asciiz = new_buf;
00133 mx_size = size;
00134 }
00135 return DEF_SUCCESS;
00136 }
00137
00138
00139 void string::set (char *str)
00140 {
00141 asciiz = str;
00142 mx_size = length();
00143 }
00144
00145
00146 void string::set (char *buff, uint32 size)
00147 {
00148 asciiz=buff;mx_size = size;asciiz[0]=0;
00149 }
00150
00151
00152 void string::nw (uint32 size)
00153 {
00154 asciiz = kmalloc(size + 1);
00155 mx_size = size;
00156
00157
00158 asciiz[0] = 0;
00159 }
00160
00161
00162 void string::done()
00163 {
00164
00165 if (mx_size == 0)
00166 return;
00167
00168 kfree( asciiz );
00169 asciiz = (char*) NULL;
00170 mx_size = 0;
00171 }
00172
00173
00174 void string::copy (char *str)
00175 {
00176
00177 verify(lgth(str));
00178
00179
00180 memmove (asciiz,str, lgth(str) + 1);
00181 }
00182
00183
00184 void string::copy (char *str, uint32 size)
00185 {
00186
00187 verify(size);
00188
00189 if (lgth(str) > size)
00190 {
00191
00192 memmove (asciiz,str, size);
00193 asciiz[size+1] = 0;
00194 }
00195 else
00196
00197 memmove (asciiz,str, lgth(str) + 1);
00198 }
00199
00200
00201 char *string::strncat(const char *str)
00202 {
00203
00204 verify(length() + lgth(str) + 1);
00205
00206
00207 memmove(asciiz+length(),str, lgth(str)+1);
00208
00209
00210 return asciiz;
00211 }
00212
00213
00214 char *string::add_char(char ch)
00215 {
00216
00217 verify(length() + 1);
00218
00219 asciiz[length()+1]=0;
00220
00221 asciiz[length()] = ch;
00222
00223
00224 return asciiz;
00225 }
00226
00227
00228 char *string::cut(uint32 n)
00229 {
00230 uint32 i;
00231
00232 if (n < length())
00233 {
00234
00235 asciiz[n]=0;
00236
00237 return asciiz;
00238 }
00239
00240 verify(n);
00241
00242 for (i=length(); i<n; i++)
00243 asciiz[i] = ' ';
00244
00245 asciiz[n] = 0;
00246
00247 return asciiz;
00248 }
00249
00250
00251 uint32 string::length()
00252 {
00253
00254 if (asciiz == 0) return 0;
00255
00256 return lgth (asciiz);
00257 }
00258
00259
00260 uint32 string::strcmp(char *s)
00261 {
00262 uint32 i;
00263
00264 if (lgth(s) != length())
00265 return 0;
00266
00267
00268 for (i=0; i<lgth(s); i++)
00269 if (asciiz[i] != s[i]) return 0;
00270
00271
00272 return 1;
00273 }
00274
00275
00276 uint32 lgth (const char *str)
00277 {
00278 const char *s;
00279
00280 for (s = str; *s; s++);
00281
00282
00283 return (uint32) s - (uint32) str;
00284 }
00285
00286
00287 char *string::formatText(char *text, char *tokens)
00288 {
00289
00290 const uint32 el_size = 0x20;
00291
00292 string str (0x20);
00293
00294 uint32 count = 0;
00295 int flag;
00296 uint32 pad_size;
00297
00298 verify(el_size);
00299
00300
00301 asciiz[0] = 0;
00302
00303
00304 for (text = text; *text; text++)
00305 {
00306 flag=0;pad_size=0;
00307
00308 if (*text == '%') {
00309 do {
00310 text++;
00311 switch (*text)
00312 {
00313
00314 case 'c':
00315 asciiz [count] = *(tokens + 3);count++;
00316 tokens += 4;
00317 flag=0;break;
00318
00319 case '%':
00320 asciiz [count] = '%';count++;
00321 flag=0;break;
00322
00323 case 's':
00324 asciiz[count] = 0;
00325 strncat((char*)(*(uint32*)(tokens + 3)));
00326 count += lgth((char*)(*(uint32*)(tokens + 3))); tokens+=4;
00327 flag=0;break;
00328
00329 case 'd':
00330 asciiz[count] = 0;
00331 strncat(str.decim( *(uint32*)(tokens + 3) ));
00332 count += str.length();tokens+=4;
00333 flag=0;break;
00334 case 'x':
00335 asciiz[count] = 0;
00336 strncat(str.hex( *(uint32*)(tokens + 3), '0', 8-pad_size));
00337 count += str.length();tokens+=4;
00338 flag=0;break;
00339
00340
00341
00342
00343
00344 case '0':
00345 if (!(flag & 4)) {
00346 flag|= 1|4;
00347 break; }
00348 case'1':case'2':case'3':case'4':case'5':
00349 case'6':case'7':case'8':case'9':
00350 flag|=2;
00351 pad_size*=10;
00352 pad_size+=*text - '0';
00353 break;
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 default: flag=0;break;
00367 }
00368 } while(flag);} else
00369 {asciiz [count] = *text;count++;}
00370
00371
00372 if (count > mx_size)
00373 verify (mx_size + el_size);
00374 }
00375
00376 asciiz [count] = 0;
00377 str.done();
00378
00379 return asciiz;
00380 }
00381
00382
00383 uint32 string::token_count(char div)
00384 {
00385 char *ptr;
00386 char last_char = div;
00387 uint32 count = 0, flag = 0;
00388
00389
00390 for (ptr = asciiz;*ptr;ptr++)
00391 {
00392
00393 if ((*ptr != div) && (last_char == div)) {
00394 if (!flag) {count++;flag=1;} else flag=0;
00395 }
00396
00397
00398 if ((*ptr == div) && (last_char != div)) {
00399 if (!flag) {count++;flag=1;} else flag=0;
00400 }
00401
00402
00403 last_char = *ptr;
00404 }
00405
00406
00407 return count;
00408 }
00409
00410
00411 char *string::token(uint32 i, char div)
00412 {
00413 uint32 count;
00414 char *ptr = asciiz;
00415 string str;
00416 char *ret_val;
00417
00418
00419 if (i >= token_count(div))
00420 return 0;
00421
00422
00423 while (*ptr == div)
00424 ptr++;
00425
00426 for (count=0; count < i; count++)
00427 {
00428
00429 while (*ptr != div) ptr++;
00430
00431 while (*ptr == div) ptr++;
00432 }
00433
00434 while ((*ptr) && (*ptr != div))
00435 {
00436 str.add_char(*ptr);
00437 ptr++;
00438 }
00439
00440
00441 ret_val = str.v();
00442
00443 str.inval();
00444
00445 return ret_val;
00446 }
00447
00448
00449 char *string::decim(uint32 n)
00450 {
00451 number (n,
00452 10,
00453 1000000000,
00454 0,
00455 0,30);
00456
00457
00458 return asciiz;
00459 }
00460
00461
00462 char *string::decim(uint32 n, char zero)
00463 {
00464 number (n,
00465 10,
00466 1000000000,
00467 1,
00468 zero, 0);
00469
00470
00471 return asciiz;
00472 }
00473
00474
00475 char *string::hex(uint32 n)
00476 {
00477 number (n,
00478 0x10,
00479 0x10000000,
00480 0,
00481 0,40);
00482
00483
00484 return asciiz;
00485 }
00486
00487
00488 char *string::hex(uint32 n, char zero) {
00489 return hex(n,zero,0);
00490 }
00491 char *string::hex(uint32 n, char zero, uint8 pad_size)
00492 {
00493 number (n,
00494 0x10,
00495 0x10000000,
00496 1,
00497 zero,pad_size);
00498
00499
00500 return asciiz;
00501 }
00502
00503
00504 char *string::number (uint32 n, uint32 radix, uint32 n_pos, uint32 zeroes, char zero, uint8 nps)
00505 {
00506 union
00507 {
00508 uint32 u32[2];
00509 uint64 u64;
00510 };
00511 u32[0]=n; u32[1]=0;
00512 return num64(u64,radix,n_pos,zeroes,zero,nps);
00513 }
00514
00515
00516
00517 char *string::num64 (uint64 n, uint32 radix, uint64 n_pos, uint32 zeroes, char zero, uint8 nps)
00518 {
00519 char first_letter = 'A';
00520 uint64 cipher;
00521 char fl = 0;
00522 uint32 count = 0;
00523 verify (10);
00524 uint32 count2=0;
00525
00526 while (n_pos)
00527 {
00528 count2++;
00529 cipher = n / n_pos;
00530 if (cipher <= 9)
00531 {
00532
00533 if (cipher||fl){
00534 fl = 1;
00535 asciiz[count] = cipher + (char) '0';
00536 count++;}
00537
00538 if ((!fl)&&((!cipher)&&(zeroes)&&(count2>nps))){
00539 asciiz[count] = zero;
00540 count++;};
00541 }
00542 else
00543 {
00544 fl = 1;
00545 asciiz[count]=cipher + first_letter - 0xA;
00546 count++;
00547 }
00548
00549 n -= cipher * n_pos;
00550 n_pos = n_pos / radix;
00551 }
00552
00553
00554 if (count == 0)
00555 {
00556 asciiz[count] = '0';
00557 count++;
00558 }
00559
00560
00561 asciiz[count] = 0;
00562
00563
00564 return asciiz;
00565 }
00566
00567
00568 uint32 string::int_val(uint32 radix, uint32 *error)
00569 {
00570 uint32 n;
00571 uint32 value;
00572 uint32 pos = 1;
00573 char *ch;
00574
00575 *error = 0; value=0;
00576
00577 n = length() - 1;
00578 while ((int)n>=0)
00579 {
00580
00581 ch = asciiz + n;
00582
00583 if (cipher(*ch, radix) == (uint32)DEF_ERROR)
00584 {*error=n+1;return value;}
00585
00586 value += pos * (cipher(*ch, radix));
00587
00588 pos = pos * radix;
00589 n--;
00590 }
00591
00592
00593 return value;
00594 }
00595
00596
00597 uint32 cipher(char ch, uint32 radix)
00598 {
00599 uint32 val;
00600
00601
00602 if (ch >= '0')
00603 val = ch - '0';
00604
00605 if (ch >= 'A')
00606 val = ch - 'A' + 10;
00607
00608 if (ch >= 'a')
00609 val = ch - 'a' + 10;
00610
00611
00612 if (val < radix)
00613 return val;
00614 else
00615 return DEF_ERROR;
00616 }
00617
00618
00619 uint32 all_chars(char *ch, char c)
00620 {
00621 char *ptr;
00622
00623 if (lgth(ch)==0)
00624 return 0;
00625
00626 for (ptr=ch; *ptr; ptr++)
00627 if (*ptr!=c) return 0;
00628
00629
00630 return 1;
00631 }
00632
00633
00634 uint32 str_dif(char *ch1, char *ch2)
00635 {
00636 uint32 n = 0;
00637
00638 while (*ch1 == *ch2)
00639 {
00640
00641 if (*ch1 == 0)
00642 return DEF_ERROR;
00643
00644 ch1++;ch2++;n++;
00645 }
00646
00647
00648 return n;
00649 }
00650
00651
00652
00653
00654
00655
00656