WSTRING.CPP 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /****************************************************************************\
  19. * C O N F I D E N T I A L --- W E S T W O O D S T U D I O S *
  20. ******************************************************************************
  21. Project Name: Carpenter (The RedAlert ladder creator)
  22. File Name : string.cpp
  23. Author : Neal Kettler
  24. Start Date : June 1, 1997
  25. Last Update : June 17, 1997
  26. A fairly typical string class. This string class always copies any input
  27. string to it's own memory (for assignment or construction).
  28. \***************************************************************************/
  29. #include <ctype.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "wstring.h"
  34. Wstring::Wstring():str(NULL)
  35. { }
  36. Wstring::Wstring(IN char *string):str(NULL)
  37. { set(string); }
  38. Wstring::Wstring(IN const Wstring &other):str(NULL)
  39. {
  40. if (other.str!=NULL)
  41. {
  42. str=new char[strlen(other.str)+1];
  43. strcpy(str,other.str);
  44. }
  45. }
  46. Wstring::~Wstring()
  47. { clear(); }
  48. bit8 Wstring::operator==(IN char *other)
  49. {
  50. if ((str==NULL)&&(other==NULL))
  51. return(TRUE);
  52. if(strcmp(str, other) != 0)
  53. return(FALSE);
  54. else
  55. return(TRUE);
  56. }
  57. bit8 Wstring::operator==(IN Wstring &other)
  58. {
  59. if((str == NULL) && (other.str == NULL))
  60. return(TRUE);
  61. if((str == NULL) || (other.str == NULL))
  62. return(FALSE);
  63. if(strcmp(str, other.str) != 0)
  64. return(FALSE);
  65. else
  66. return(TRUE);
  67. }
  68. bit8 Wstring::operator!=(IN char *other)
  69. {
  70. if(strcmp(str, other) != 0)
  71. return(TRUE);
  72. else
  73. return(FALSE);
  74. }
  75. bit8 Wstring::operator!=(IN Wstring &other)
  76. {
  77. if((str == NULL) && (other.str == NULL))
  78. return(FALSE);
  79. if((str == NULL) || (other.str == NULL))
  80. return(TRUE);
  81. if(strcmp(str, other.str) != 0)
  82. return(TRUE);
  83. else
  84. return(FALSE);
  85. }
  86. Wstring &Wstring::operator=(char *other)
  87. {
  88. set(other);
  89. return(*this);
  90. }
  91. Wstring &Wstring::operator=(IN Wstring &other)
  92. {
  93. if(*this == other)
  94. return(*this);
  95. set(other.get());
  96. return(*this);
  97. }
  98. bit8 Wstring::cat(IN char *s)
  99. {
  100. char *oldStr;
  101. uint32 len;
  102. if (s==NULL) // it's OK to cat nothing
  103. return(TRUE);
  104. // Save the contents of the string.
  105. oldStr = str;
  106. // Determine the length of the resultant string.
  107. len = strlen(s) + 1;
  108. if(oldStr)
  109. len += strlen(oldStr);
  110. // Allocate memory for the new string.
  111. if(!(str = new char[(len * sizeof(char))]))
  112. {
  113. str = oldStr;
  114. return(FALSE);
  115. }
  116. // Copy the contents of the old string and concatenate the passed
  117. // string.
  118. if(oldStr) strcpy(str, oldStr);
  119. else str[0] = 0;
  120. strcat(str, s);
  121. // delete the old string.
  122. if(oldStr)
  123. delete[](oldStr);
  124. return(TRUE);
  125. }
  126. bit8 Wstring::cat(uint32 size, IN char *s)
  127. {
  128. char *oldStr;
  129. uint32 len;
  130. // Save the contents of the string.
  131. oldStr = str;
  132. // Determine the length of the resultant string.
  133. len = size + 1;
  134. if(oldStr)
  135. len += strlen(oldStr);
  136. // Allocate memory for the new string.
  137. if(!(str = new char[(len * sizeof(char))]))
  138. {
  139. str = oldStr;
  140. return(FALSE);
  141. }
  142. // Copy the contents of the old string and concatenate the passed
  143. // string.
  144. if(oldStr)
  145. strcpy(str, oldStr);
  146. else
  147. str[0] = 0;
  148. strncat(str, s, size);
  149. // delete the old string.
  150. if(oldStr)
  151. delete[](oldStr);
  152. return(TRUE);
  153. }
  154. bit8 Wstring::cat(IN Wstring &other)
  155. {
  156. return cat(other.get());
  157. }
  158. Wstring &Wstring::operator+=(IN char *string)
  159. {
  160. cat(string);
  161. return(*this);
  162. }
  163. Wstring &Wstring::operator+=(IN Wstring &other)
  164. {
  165. cat(other.get());
  166. return(*this);
  167. }
  168. Wstring Wstring::operator+(IN char *string)
  169. {
  170. Wstring temp = *this;
  171. temp.cat(string);
  172. return(temp);
  173. }
  174. Wstring Wstring::operator+(IN Wstring &s)
  175. {
  176. Wstring temp = *this;
  177. temp.cat(s);
  178. return(temp);
  179. }
  180. //
  181. // This function deletes 'count' characters indexed by `pos' from the Wstring.
  182. // If `pos'+'count' is > the length of the array, the last 'count' characters
  183. // of the string are removed. If an error occurs, FALSE is returned.
  184. // Otherwise, TRUE is returned. Note: count has a default value of 1.
  185. //
  186. //
  187. char Wstring::remove(sint32 pos,sint32 count)
  188. {
  189. char *s;
  190. sint32 len;
  191. if (!str)
  192. return(FALSE);
  193. len = (sint32)strlen(str);
  194. if(pos+count > len)
  195. pos = len - count;
  196. if (pos < 0)
  197. {
  198. count+=pos; // If they remove before 0, ignore up till beginning
  199. pos=0;
  200. }
  201. if (count<=0)
  202. return(FALSE);
  203. if(!(s = new char[len-count+1]))
  204. {
  205. //ErrorMessage(SET_EM, "Insufficient memory to modify Wstring.");
  206. return(FALSE);
  207. }
  208. ///////DBGMSG("Wstring::remove POS: "<<pos<<" LEN: "<<len);
  209. // put nulls on both ends of substring to be removed
  210. str[pos] = 0;
  211. str[pos+count-1] = 0;
  212. strcpy(s, str);
  213. strcat(s, &(str[pos + count]));
  214. delete[](str);
  215. str = s;
  216. return(TRUE);
  217. }
  218. // Remove all instances of a char from the string
  219. bit8 Wstring::removeChar(char c)
  220. {
  221. int len=0;
  222. char *cptr=NULL;
  223. bit8 removed=FALSE;
  224. if (str==NULL)
  225. return(FALSE);
  226. len=strlen(str);
  227. while ((cptr=strchr(str,c)) !=NULL)
  228. {
  229. memmove(cptr,cptr+1,len-1-((int)(cptr-str)));
  230. len--;
  231. str[len]=0;
  232. removed=TRUE;
  233. }
  234. if (removed)
  235. {
  236. char *newStr=new char[strlen(str)+1];
  237. strcpy(newStr,str);
  238. delete[](str);
  239. str=newStr;
  240. }
  241. return(removed);
  242. }
  243. void Wstring::removeSpaces(void)
  244. {
  245. removeChar(' ');
  246. removeChar('\t');
  247. }
  248. void Wstring::clear(void)
  249. {
  250. if(str)
  251. delete[](str);
  252. str=NULL;
  253. }
  254. void Wstring::setSize(sint32 size)
  255. {
  256. clear();
  257. if (size<0)
  258. return;
  259. str=new char[size];
  260. memset(str,0,size);
  261. }
  262. void Wstring::cellCopy(char *dest, uint32 len)
  263. {
  264. uint32 i;
  265. strncpy(dest, str, len);
  266. for(i = (uint32)strlen(str); i < len; i++)
  267. dest[i] = ' ';
  268. dest[len] = 0;
  269. }
  270. char *Wstring::get(void)
  271. {
  272. if(!str)
  273. return "";
  274. return str;
  275. }
  276. char Wstring::get(uint32 index)
  277. {
  278. if(index < strlen(str))
  279. return str[index];
  280. return(0);
  281. }
  282. uint32 Wstring::length(void)
  283. {
  284. if(str == NULL)
  285. return(0);
  286. return((uint32)strlen(str));
  287. }
  288. // Insert at given position and shift old stuff to right
  289. bit8 Wstring::insert(char *instring, uint32 pos)
  290. {
  291. if (str==NULL)
  292. return(set(instring));
  293. if (pos>strlen(str))
  294. pos=strlen(str);
  295. char *newstr=new char[strlen(str)+strlen(instring)+1];
  296. memset(newstr,0,strlen(str)+strlen(instring)+1);
  297. strcpy(newstr,str);
  298. // move the old data out of the way
  299. int bytes=strlen(str)+1-pos;
  300. memmove(newstr+pos+strlen(instring),newstr+pos,bytes);
  301. // move the new data into place
  302. memmove(newstr+pos,instring,strlen(instring));
  303. delete[](str);
  304. str=newstr;
  305. return(TRUE);
  306. }
  307. // This function inserts the character specified by `k' into the string at the
  308. // position indexed by `pos'. If `pos' is >= the length of the string, it is
  309. // appended to the string. If an error occurs, FALSE is returned. Otherwise,
  310. // TRUE is returned.
  311. bit8 Wstring::insert(char k, uint32 pos)
  312. {
  313. char *s;
  314. uint32 len;
  315. char c[2];
  316. if(!str)
  317. {
  318. c[0] = k;
  319. c[1] = 0;
  320. return(set(c));
  321. }
  322. len = (uint32)strlen(str);
  323. if(pos > len)
  324. pos = len;
  325. if(!(s = (char *)new char[(len + 2)]))
  326. {
  327. //ErrorMessage(SET_EM, "Insufficient memory to modify Wstring.");
  328. return(FALSE);
  329. }
  330. c[0] = str[pos];
  331. str[pos] = 0;
  332. c[1] = 0;
  333. strcpy(s, str);
  334. str[pos] = c[0];
  335. c[0] = k;
  336. strcat(s, c);
  337. strcat(s, &(str[pos]));
  338. delete[](str);
  339. str = s;
  340. return(TRUE);
  341. }
  342. // This function replaces any occurences of the string pointed to by
  343. // `replaceThis' with the string pointed to by `withThis'. If an error
  344. // occurs, FALSE is returned. Otherwise, TRUE is returned.
  345. bit8 Wstring::replace(char *replaceThis, char *withThis)
  346. {
  347. Wstring dest;
  348. char *foundStr, *src;
  349. uint32 len;
  350. src=get();
  351. while(src && src[0])
  352. {
  353. foundStr = strstr(src, replaceThis);
  354. if(foundStr)
  355. {
  356. len = (uint32)foundStr - (uint32)src;
  357. if(len)
  358. {
  359. if(!dest.cat(len, src))
  360. return(FALSE);
  361. }
  362. if(!dest.cat(withThis))
  363. return(FALSE);
  364. src = foundStr + strlen(replaceThis);
  365. }
  366. else
  367. {
  368. if(!dest.cat(src))
  369. return(FALSE);
  370. src=NULL;
  371. }
  372. }
  373. return(set(dest.get()));
  374. }
  375. bit8 Wstring::set(IN char *s)
  376. {
  377. uint32 len;
  378. clear();
  379. len = (uint32)strlen(s) + 1;
  380. if(!(str = new char[len]))
  381. {
  382. //ErrorMessage(SET_EM, "Insufficient memory to set Wstring.");
  383. return(FALSE);
  384. }
  385. strcpy(str, s);
  386. return(TRUE);
  387. }
  388. bit8 Wstring::set(char c, uint32 index)
  389. {
  390. if(index >= (uint32)strlen(str))
  391. return FALSE;
  392. str[index] = c;
  393. return TRUE;
  394. }
  395. char Wstring::set(uint32 size, IN char *string)
  396. {
  397. uint32 len;
  398. clear();
  399. len = size + 1;
  400. if(!(str = new char[len]))
  401. {
  402. //ErrorMessage(SET_EM, "Insufficient memory to set Wstring.");
  403. return(FALSE);
  404. }
  405. // Copy the bytes in the string, and NULL-terminate it.
  406. strncpy(str, string, size);
  407. str[size] = 0;
  408. return(TRUE);
  409. }
  410. // This function converts all alphabetical characters in the string to lower
  411. // case.
  412. void Wstring::toLower(void)
  413. {
  414. uint32 i;
  415. for(i = 0; i < length(); i++)
  416. {
  417. if((str[i] >= 'A') && (str[i] <= 'Z'))
  418. str[i] = tolower(str[i]);
  419. }
  420. }
  421. // This function converts all alphabetical characters in the string to upper
  422. // case.
  423. void Wstring::toUpper(void)
  424. {
  425. uint32 i;
  426. for(i = 0; i < length(); i++)
  427. {
  428. if((str[i] >= 'a') && (str[i] <= 'z'))
  429. str[i] = toupper(str[i]);
  430. }
  431. }
  432. // This function truncates the string so its length will match the specified
  433. // `len'. If an error occurs, FALSE is returned. Otherwise, TRUE is returned.
  434. bit8 Wstring::truncate(uint32 len)
  435. {
  436. Wstring tmp;
  437. if(!tmp.set(len, get()) || !set(tmp.get()))
  438. return(FALSE);
  439. return(TRUE);
  440. }
  441. // Truncate the string after the character 'c' (gets rid of 'c' as well)
  442. // Do nothing if 'c' isn't in the string
  443. bit8 Wstring::truncate(char c)
  444. {
  445. sint32 len;
  446. if (str==NULL)
  447. return(FALSE);
  448. char *cptr=strchr(str,c);
  449. if (cptr==NULL)
  450. return(FALSE);
  451. len=(sint32)(cptr-str);
  452. truncate((uint32)len);
  453. return(TRUE);
  454. }
  455. // Get a token from this string that's seperated by one or more
  456. // chars from the 'delim' string , start at offset & return offset
  457. sint32 Wstring::getToken(int offset,char *delim,Wstring &out)
  458. {
  459. int i;
  460. sint32 start;
  461. sint32 stop;
  462. for (i=offset; i<length(); i++) {
  463. if(strchr(delim,str[i])==NULL)
  464. break;
  465. }
  466. if (i>=length())
  467. return(-1);
  468. start=i;
  469. for (; i<length(); i++) {
  470. if(strchr(delim,str[i])!=NULL)
  471. break;
  472. }
  473. stop=i-1;
  474. out.set(str+start);
  475. out.truncate((uint32)stop-start+1);
  476. return(stop+1);
  477. }
  478. // Get the first line of text after offset. Lines are terminated by '\r\n' or '\n'
  479. sint32 Wstring::getLine(int offset, Wstring &out)
  480. {
  481. int i;
  482. sint32 start;
  483. sint32 stop;
  484. start=i=offset;
  485. if (start >= length())
  486. return(-1);
  487. for (; i<length(); i++) {
  488. if(strchr("\r\n",str[i])!=NULL)
  489. break;
  490. }
  491. stop=i;
  492. if ((str[stop]=='\r')&&(str[stop+1]=='\n'))
  493. stop++;
  494. out.set(str+start);
  495. out.truncate((uint32)stop-start+1);
  496. return(stop+1);
  497. }