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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
|
/**
* Copyright 1999, 2000 by PC Drew
*
* All rights reserved.
*
* This file is a part of the gstring class - a string class for
* C++ programs.
*
* The gstring class, including all files distributed with it,
* is free software; you can redistribute it and/or use it and/or modify it
* under the terms of the Python License (http://www.python.org/doc/Copyright.html)
*
* 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.
*
*/
#include "gstring.h"
/**
* Precondition: none.
* Postcondition: a new gstring is created, with an empty string.
*/
gstring::gstring(void)
{
create("");
}
/**
* Precondition: _s is a char.
* Postcondition: a new gstring is returned, with the value of _s.
*/
/*gstring::gstring(char _s)
{
create((char*)_s);
}*/
/**
* Precondition: _str is a char*.
* Postcondition: a new gstring is returned, with the value of _str.
*/
gstring::gstring(char* _str)
{
create(_str);
}
/**
* Precondition: _x is an integer.
* Postcondition: a new gstring is returned, with the string value of _x.
*/
gstring::gstring(int _x)
{
char* str = new char;
sprintf(str, "%i", _x);
create(str);
delete [] str;
}
/**
* Precondition: _y is an double.
* Postcondition: a new gstring is returned, with the string value of _y.
*/
gstring::gstring(double _y)
{
char* str = new char;
sprintf(str, "%f", _y);
create(str);
delete [] str;
}
/**
* Precondition: _gstr is a gstring.
* Postcondition: a copy of the existing gstring is returned.
*/
gstring::gstring(const gstring& _gstr)
{
create((char*)_gstr);
}
/**
* Precondition: none.
* Postcondition: the current gstring is destroyed.
*/
gstring::~gstring(void)
{
destroy();
}
/**
* Precondition: _str is a char*.
* Postcondition: _str is copied into the new gstring.
*/
int gstring::create(char* _str)
{
assert(_str != NULL);
// We want to create our own char*, instead of using
// strdup() because new will never return NULL, and
// malloc() (used by strdup()) might.
str = new char[strlen(_str)];
strcpy(str, _str);
return 0;
}
/**
* Precondition: none.
* Postcondition: the current gstring is deleted.
*/
int gstring::destroy(void)
{
delete [] str;
return 0;
}
/**
* Precondition: _index is the first char in the substring, starting at 0,
* _num is the number of chars in the substring.
* Postcondition: the substring starting at _index and going _num chars
* in length is returned, as a gstring.
*/
gstring gstring::at(int _index, int _num)
{
int len = length();
assert(_index >= 0 && _num >= 1 && _index <= len - _num);
char* temp_str = new char[len - _index];
char* begin = str;
char* end = temp_str;
// Go to the character that is at _index and copy the
// rest of the char* to temp_str.
begin += _index;
strcpy(temp_str, begin);
// Go to the character that is at _index + _num and set
// it equal to the null terminator.
end += _num;
*end = '\0';
// Create a new gstring from the substring that we extracted.
gstring gstr(temp_str);
// Clean up.
delete [] temp_str;
return gstr;
}
/**
* Precondition: _num is either an integer >= 1 or it is not specified.
* If the function is called without any args (i.e. first()), then
* _num = 1 by default.
* Postcondition: the substring from the beginning of the string, going
* _num chars in length is returned.
*/
gstring gstring::first(int _num)
{
return (at(0, _num));
}
/**
* Precondition: _num is either an integer >= 1 or it is not specified.
* If the function is called without any args (i.e. first()), then
* _num = 1 by default.
* Postcondition: the substring _num chars from the end of the string, going
* to the end of the string is to returned.
*/
gstring gstring::last(int _num)
{
return (at(length() - _num, _num));
}
/**
* Precondition: _num is the number of times you want the string repeated.
* Postcondition: the current string is changed to be the current string,
* repeated _num times.
*/
gstring& gstring::repeatme(int _num)
{
assert(str != NULL && _num >= 1);
char* temp_str = new char[length() * _num];
// Tack str onto the end of temp_str, _num times.
for (int i = 0; i < _num; i++) {
strcat(temp_str, str);
}
destroy();
create(temp_str);
delete [] temp_str;
return *this;
}
/**
* Precondition: _token is a char*.
* Postcondition: returns the number of occurences of _token in the string.
*/
int gstring::ntokens(char* _token)
{
char* temp_str = str;
int len = strlen(_token);
int i = 0;
assert(_token != NULL && len >= 1);
// Iterate through the string...
for ( ; *temp_str != '\0'; temp_str++) {
if (*temp_str == *_token && strncmp(_token, temp_str, len) == 0) {
i++;
}
}
return i;
}
/**
* Precondition: _token is a char*.
* Postcondition: an array of gstrings is returned. The contents of each
* gstring in the array is the value either from the beginning of the
* original string to the first occurance of _token or from one occurance
* of _token to the next. _token will not be returned in any of the strings.
*
* **NOTE**
* Don't initialize you're array (i.e. call "new") before calling
* this function...this function will do that for you. You do, however,
* need to call "delete [] array" in your own code.
*/
gstring* gstring::explode(char* _token)
{
assert(_token != NULL && strlen(_token) >= 1);
int i;
int n = nfields(_token);
char* ptr;
char* temp_str = new char[length()];
gstring* arr = new gstring[n];
strcpy(temp_str, str);
for (i = 0, ptr = strtok(temp_str, _token); ptr != NULL;
i++, ptr = strtok(NULL, _token)) {
arr[i] = ptr;
}
delete [] temp_str;
return arr;
}
/**
* Precondition: _arr is an array of char*'s and _token is a char*, one or more characters
* in length.
* Postcondition: value returned is each char* in the array joined
* together by _token.
*/
gstring implode(char** _arr, char* _token, int _num)
{
assert(_arr != NULL && _token != NULL && strlen(_token) >= 1);
gstring s = _arr[0];
for (int i = 1; i < _num; i++) {
s.append(_token);
s.append(_arr[i]);
}
return s;
}
/**
* Precondition: _arr is an array of gstrings and _token is a char*, one or more characters
* in length.
* Postcondition: value returned is each gstring in the array joined
* together by _token.
*/
gstring implode(gstring* _arr, char* _token, int _num)
{
assert(_arr != NULL && _token != NULL);
gstring s = _arr[0];
for (int i = 1; i < _num; i++) {
s.append(_token + _arr[i]);
}
return s;
}
/**
* Precondition: _x is the number of characters to chop off the end of the string.
* The default value (chop called without any parameters) is 1 -- the last
* character will be removed.
* Postcondition: the current gstring has the last _x characters removed from the string.
* together by _token.
*/
gstring& gstring::chop(int _x)
{
int len = length() - _x;
char* temp_str = new char[len];
assert(_x >= 0);
// This allows implode to join the strings together with a "" string (nothing).
if (_x > 0) {
strcpy(temp_str, str);
temp_str[len] = '\0';
destroy();
create(temp_str);
}
delete [] temp_str;
return *this;
}
/**
* Precondition: _str is not NULL.
* Postcondition: _str is appended to current gstring.
*/
gstring& gstring::append(char* _str)
{
assert(_str != NULL);
char* temp_str = new char[length() + strlen(_str)];
strcpy(temp_str, str);
strcat(temp_str, _str);
destroy();
create(temp_str);
delete [] temp_str;
return *this;
}
/**
* Precondition: _str is not NULL.
* Postcondition: _str is prepended to current gstring.
*/
gstring& gstring::prepend(char* _str)
{
assert(_str != NULL);
char* temp_str = new char[strlen(_str) + length()];
strcpy(temp_str, _str);
strcat(temp_str, str);
destroy();
create(temp_str);
delete [] temp_str;
return *this;
}
/**
* Precondition: current gstring is not NULL.
* Postcondition: current gstring is converted to uppercase.
*/
gstring& gstring::upcaseme(void)
{
char* ptr = str;
int len = length();
assert(str != NULL);
for (int i = 0; i < len; i++, ptr++) {
*ptr = (char)toupper((int) * ptr);
}
return *this;
}
/**
* Precondition: current gstring is not equal to NULL.
* Postcondition: current gstring is converted to lowercase.
*/
gstring& gstring::downcaseme(void)
{
char* ptr = str;
int len = length();
assert(str != NULL);
for (int i = 0; i < len; i++, ptr++) {
*ptr = (char)tolower((int) * ptr);
}
return *this;
}
/**
* Precondition: _gstrarr is an array of gstrings and _num is the number of
* gstrings in the array.
* Postcondition: the value returned is an exact copy of _gstrarr, only it
* it is an array of char*'s instead.
*/
char** tochararray(gstring* _gstrarr, int _num)
{
char** strarr = new char * [_num];
for (int i = 0; i < _num; i++) {
strarr[i] = (char*)_gstrarr[i];
}
return strarr;
}
/**
* Precondition: _strarr is an array of char*s and _num is the number of
* char*'s in the array.
* Postcondition: the value returned is an exact copy of _strarr, only it
* it is an array of gstrings instead.
*/
gstring* togstringarray(char** _strarr, int _num)
{
gstring* gstrarr = new gstring[_num];
for (int i = 0; i < _num; i++) {
gstrarr[i] = _strarr[i];
}
return gstrarr;
}
/*gstring& gstring::operator =(char _s)
{
assert(_s != NULL);
if (str != NULL) {
destroy();
}
create((char*)_s);
return *this;
}*/
gstring& gstring::operator =(char* _str)
{
assert(_str != NULL);
if (str != NULL) {
destroy();
}
create(_str);
return *this;
}
gstring& gstring::operator =(int _x)
{
assert(_x != 0/*NULL*/);
char* temp_str = new char;
if (str != NULL) {
destroy();
}
sprintf(temp_str, "%i", _x);
create(temp_str);
delete [] temp_str;
return *this;
}
gstring& gstring::operator =(double _y)
{
assert(_y != 0/*NULL*/);
char* temp_str = new char;
if (str != NULL) {
destroy();
}
sprintf(temp_str, "%f", _y);
create(temp_str);
delete [] temp_str;
return *this;
}
gstring& gstring::operator =(const gstring& _gstr)
{
assert((char*)_gstr != NULL);
if (str != NULL) {
destroy();
}
create((char*)_gstr);
return *this;
}
/*istream& operator >>(istream& in, gstring& _gstr)
{
char** temp_str = new char * [1];
in.gets(temp_str);
_gstr = temp_str[0];
delete [] temp_str[0];
delete [] temp_str;
return in;
}*/
|