/branches/ucsim/ucsim-0_6-pre/gstring.cc

# · C++ · 519 lines · 240 code · 100 blank · 179 comment · 48 complexity · df2a0c9d3c8108a93eb6d1bf0a1a2941 MD5 · raw file

  1. /**
  2. * Copyright 1999, 2000 by PC Drew
  3. *
  4. * All rights reserved.
  5. *
  6. * This file is a part of the gstring class - a string class for
  7. * C++ programs.
  8. *
  9. * The gstring class, including all files distributed with it,
  10. * is free software; you can redistribute it and/or use it and/or modify it
  11. * under the terms of the Python License (http://www.python.org/doc/Copyright.html)
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. */
  18. #include "gstring.h"
  19. /**
  20. * Precondition: none.
  21. * Postcondition: a new gstring is created, with an empty string.
  22. */
  23. gstring::gstring(void)
  24. {
  25. create("");
  26. }
  27. /**
  28. * Precondition: _s is a char.
  29. * Postcondition: a new gstring is returned, with the value of _s.
  30. */
  31. /*gstring::gstring(char _s)
  32. {
  33. create((char*)_s);
  34. }*/
  35. /**
  36. * Precondition: _str is a char*.
  37. * Postcondition: a new gstring is returned, with the value of _str.
  38. */
  39. gstring::gstring(char* _str)
  40. {
  41. create(_str);
  42. }
  43. /**
  44. * Precondition: _x is an integer.
  45. * Postcondition: a new gstring is returned, with the string value of _x.
  46. */
  47. gstring::gstring(int _x)
  48. {
  49. char* str = new char;
  50. sprintf(str, "%i", _x);
  51. create(str);
  52. delete [] str;
  53. }
  54. /**
  55. * Precondition: _y is an double.
  56. * Postcondition: a new gstring is returned, with the string value of _y.
  57. */
  58. gstring::gstring(double _y)
  59. {
  60. char* str = new char;
  61. sprintf(str, "%f", _y);
  62. create(str);
  63. delete [] str;
  64. }
  65. /**
  66. * Precondition: _gstr is a gstring.
  67. * Postcondition: a copy of the existing gstring is returned.
  68. */
  69. gstring::gstring(const gstring& _gstr)
  70. {
  71. create((char*)_gstr);
  72. }
  73. /**
  74. * Precondition: none.
  75. * Postcondition: the current gstring is destroyed.
  76. */
  77. gstring::~gstring(void)
  78. {
  79. destroy();
  80. }
  81. /**
  82. * Precondition: _str is a char*.
  83. * Postcondition: _str is copied into the new gstring.
  84. */
  85. int gstring::create(char* _str)
  86. {
  87. assert(_str != NULL);
  88. // We want to create our own char*, instead of using
  89. // strdup() because new will never return NULL, and
  90. // malloc() (used by strdup()) might.
  91. str = new char[strlen(_str)];
  92. strcpy(str, _str);
  93. return 0;
  94. }
  95. /**
  96. * Precondition: none.
  97. * Postcondition: the current gstring is deleted.
  98. */
  99. int gstring::destroy(void)
  100. {
  101. delete [] str;
  102. return 0;
  103. }
  104. /**
  105. * Precondition: _index is the first char in the substring, starting at 0,
  106. * _num is the number of chars in the substring.
  107. * Postcondition: the substring starting at _index and going _num chars
  108. * in length is returned, as a gstring.
  109. */
  110. gstring gstring::at(int _index, int _num)
  111. {
  112. int len = length();
  113. assert(_index >= 0 && _num >= 1 && _index <= len - _num);
  114. char* temp_str = new char[len - _index];
  115. char* begin = str;
  116. char* end = temp_str;
  117. // Go to the character that is at _index and copy the
  118. // rest of the char* to temp_str.
  119. begin += _index;
  120. strcpy(temp_str, begin);
  121. // Go to the character that is at _index + _num and set
  122. // it equal to the null terminator.
  123. end += _num;
  124. *end = '\0';
  125. // Create a new gstring from the substring that we extracted.
  126. gstring gstr(temp_str);
  127. // Clean up.
  128. delete [] temp_str;
  129. return gstr;
  130. }
  131. /**
  132. * Precondition: _num is either an integer >= 1 or it is not specified.
  133. * If the function is called without any args (i.e. first()), then
  134. * _num = 1 by default.
  135. * Postcondition: the substring from the beginning of the string, going
  136. * _num chars in length is returned.
  137. */
  138. gstring gstring::first(int _num)
  139. {
  140. return (at(0, _num));
  141. }
  142. /**
  143. * Precondition: _num is either an integer >= 1 or it is not specified.
  144. * If the function is called without any args (i.e. first()), then
  145. * _num = 1 by default.
  146. * Postcondition: the substring _num chars from the end of the string, going
  147. * to the end of the string is to returned.
  148. */
  149. gstring gstring::last(int _num)
  150. {
  151. return (at(length() - _num, _num));
  152. }
  153. /**
  154. * Precondition: _num is the number of times you want the string repeated.
  155. * Postcondition: the current string is changed to be the current string,
  156. * repeated _num times.
  157. */
  158. gstring& gstring::repeatme(int _num)
  159. {
  160. assert(str != NULL && _num >= 1);
  161. char* temp_str = new char[length() * _num];
  162. // Tack str onto the end of temp_str, _num times.
  163. for (int i = 0; i < _num; i++) {
  164. strcat(temp_str, str);
  165. }
  166. destroy();
  167. create(temp_str);
  168. delete [] temp_str;
  169. return *this;
  170. }
  171. /**
  172. * Precondition: _token is a char*.
  173. * Postcondition: returns the number of occurences of _token in the string.
  174. */
  175. int gstring::ntokens(char* _token)
  176. {
  177. char* temp_str = str;
  178. int len = strlen(_token);
  179. int i = 0;
  180. assert(_token != NULL && len >= 1);
  181. // Iterate through the string...
  182. for ( ; *temp_str != '\0'; temp_str++) {
  183. if (*temp_str == *_token && strncmp(_token, temp_str, len) == 0) {
  184. i++;
  185. }
  186. }
  187. return i;
  188. }
  189. /**
  190. * Precondition: _token is a char*.
  191. * Postcondition: an array of gstrings is returned. The contents of each
  192. * gstring in the array is the value either from the beginning of the
  193. * original string to the first occurance of _token or from one occurance
  194. * of _token to the next. _token will not be returned in any of the strings.
  195. *
  196. * **NOTE**
  197. * Don't initialize you're array (i.e. call "new") before calling
  198. * this function...this function will do that for you. You do, however,
  199. * need to call "delete [] array" in your own code.
  200. */
  201. gstring* gstring::explode(char* _token)
  202. {
  203. assert(_token != NULL && strlen(_token) >= 1);
  204. int i;
  205. int n = nfields(_token);
  206. char* ptr;
  207. char* temp_str = new char[length()];
  208. gstring* arr = new gstring[n];
  209. strcpy(temp_str, str);
  210. for (i = 0, ptr = strtok(temp_str, _token); ptr != NULL;
  211. i++, ptr = strtok(NULL, _token)) {
  212. arr[i] = ptr;
  213. }
  214. delete [] temp_str;
  215. return arr;
  216. }
  217. /**
  218. * Precondition: _arr is an array of char*'s and _token is a char*, one or more characters
  219. * in length.
  220. * Postcondition: value returned is each char* in the array joined
  221. * together by _token.
  222. */
  223. gstring implode(char** _arr, char* _token, int _num)
  224. {
  225. assert(_arr != NULL && _token != NULL && strlen(_token) >= 1);
  226. gstring s = _arr[0];
  227. for (int i = 1; i < _num; i++) {
  228. s.append(_token);
  229. s.append(_arr[i]);
  230. }
  231. return s;
  232. }
  233. /**
  234. * Precondition: _arr is an array of gstrings and _token is a char*, one or more characters
  235. * in length.
  236. * Postcondition: value returned is each gstring in the array joined
  237. * together by _token.
  238. */
  239. gstring implode(gstring* _arr, char* _token, int _num)
  240. {
  241. assert(_arr != NULL && _token != NULL);
  242. gstring s = _arr[0];
  243. for (int i = 1; i < _num; i++) {
  244. s.append(_token + _arr[i]);
  245. }
  246. return s;
  247. }
  248. /**
  249. * Precondition: _x is the number of characters to chop off the end of the string.
  250. * The default value (chop called without any parameters) is 1 -- the last
  251. * character will be removed.
  252. * Postcondition: the current gstring has the last _x characters removed from the string.
  253. * together by _token.
  254. */
  255. gstring& gstring::chop(int _x)
  256. {
  257. int len = length() - _x;
  258. char* temp_str = new char[len];
  259. assert(_x >= 0);
  260. // This allows implode to join the strings together with a "" string (nothing).
  261. if (_x > 0) {
  262. strcpy(temp_str, str);
  263. temp_str[len] = '\0';
  264. destroy();
  265. create(temp_str);
  266. }
  267. delete [] temp_str;
  268. return *this;
  269. }
  270. /**
  271. * Precondition: _str is not NULL.
  272. * Postcondition: _str is appended to current gstring.
  273. */
  274. gstring& gstring::append(char* _str)
  275. {
  276. assert(_str != NULL);
  277. char* temp_str = new char[length() + strlen(_str)];
  278. strcpy(temp_str, str);
  279. strcat(temp_str, _str);
  280. destroy();
  281. create(temp_str);
  282. delete [] temp_str;
  283. return *this;
  284. }
  285. /**
  286. * Precondition: _str is not NULL.
  287. * Postcondition: _str is prepended to current gstring.
  288. */
  289. gstring& gstring::prepend(char* _str)
  290. {
  291. assert(_str != NULL);
  292. char* temp_str = new char[strlen(_str) + length()];
  293. strcpy(temp_str, _str);
  294. strcat(temp_str, str);
  295. destroy();
  296. create(temp_str);
  297. delete [] temp_str;
  298. return *this;
  299. }
  300. /**
  301. * Precondition: current gstring is not NULL.
  302. * Postcondition: current gstring is converted to uppercase.
  303. */
  304. gstring& gstring::upcaseme(void)
  305. {
  306. char* ptr = str;
  307. int len = length();
  308. assert(str != NULL);
  309. for (int i = 0; i < len; i++, ptr++) {
  310. *ptr = (char)toupper((int) * ptr);
  311. }
  312. return *this;
  313. }
  314. /**
  315. * Precondition: current gstring is not equal to NULL.
  316. * Postcondition: current gstring is converted to lowercase.
  317. */
  318. gstring& gstring::downcaseme(void)
  319. {
  320. char* ptr = str;
  321. int len = length();
  322. assert(str != NULL);
  323. for (int i = 0; i < len; i++, ptr++) {
  324. *ptr = (char)tolower((int) * ptr);
  325. }
  326. return *this;
  327. }
  328. /**
  329. * Precondition: _gstrarr is an array of gstrings and _num is the number of
  330. * gstrings in the array.
  331. * Postcondition: the value returned is an exact copy of _gstrarr, only it
  332. * it is an array of char*'s instead.
  333. */
  334. char** tochararray(gstring* _gstrarr, int _num)
  335. {
  336. char** strarr = new char * [_num];
  337. for (int i = 0; i < _num; i++) {
  338. strarr[i] = (char*)_gstrarr[i];
  339. }
  340. return strarr;
  341. }
  342. /**
  343. * Precondition: _strarr is an array of char*s and _num is the number of
  344. * char*'s in the array.
  345. * Postcondition: the value returned is an exact copy of _strarr, only it
  346. * it is an array of gstrings instead.
  347. */
  348. gstring* togstringarray(char** _strarr, int _num)
  349. {
  350. gstring* gstrarr = new gstring[_num];
  351. for (int i = 0; i < _num; i++) {
  352. gstrarr[i] = _strarr[i];
  353. }
  354. return gstrarr;
  355. }
  356. /*gstring& gstring::operator =(char _s)
  357. {
  358. assert(_s != NULL);
  359. if (str != NULL) {
  360. destroy();
  361. }
  362. create((char*)_s);
  363. return *this;
  364. }*/
  365. gstring& gstring::operator =(char* _str)
  366. {
  367. assert(_str != NULL);
  368. if (str != NULL) {
  369. destroy();
  370. }
  371. create(_str);
  372. return *this;
  373. }
  374. gstring& gstring::operator =(int _x)
  375. {
  376. assert(_x != 0/*NULL*/);
  377. char* temp_str = new char;
  378. if (str != NULL) {
  379. destroy();
  380. }
  381. sprintf(temp_str, "%i", _x);
  382. create(temp_str);
  383. delete [] temp_str;
  384. return *this;
  385. }
  386. gstring& gstring::operator =(double _y)
  387. {
  388. assert(_y != 0/*NULL*/);
  389. char* temp_str = new char;
  390. if (str != NULL) {
  391. destroy();
  392. }
  393. sprintf(temp_str, "%f", _y);
  394. create(temp_str);
  395. delete [] temp_str;
  396. return *this;
  397. }
  398. gstring& gstring::operator =(const gstring& _gstr)
  399. {
  400. assert((char*)_gstr != NULL);
  401. if (str != NULL) {
  402. destroy();
  403. }
  404. create((char*)_gstr);
  405. return *this;
  406. }
  407. /*istream& operator >>(istream& in, gstring& _gstr)
  408. {
  409. char** temp_str = new char * [1];
  410. in.gets(temp_str);
  411. _gstr = temp_str[0];
  412. delete [] temp_str[0];
  413. delete [] temp_str;
  414. return in;
  415. }*/