PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/util_code/stringc.cpp

https://github.com/w601sxs/OpenVSP
C++ | 1074 lines | 753 code | 199 blank | 122 comment | 252 complexity | 7dc2556867c401cb585abd4e39987d15 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. //
  2. // This file is released under the terms of the NASA Open Source Agreement (NOSA)
  3. // version 1.3 as detailed in the LICENSE file which accompanies this software.
  4. //
  5. //********************************
  6. // String Class
  7. //
  8. // Cathy D. Roberts
  9. // Sterling Software
  10. //
  11. //********************************
  12. #include "stringc.h"
  13. //-------------------------- Constructors -----------------------------------
  14. //----------------------------------------------------------------------------
  15. Stringc::Stringc()
  16. {
  17. chunk_size = STRINGC_CHUNK_SIZE;
  18. total_size = 0;
  19. num_chars = 0;
  20. make_space();
  21. char_array[num_chars] = '\0';
  22. }
  23. //----------------------------------------------------------------------------
  24. Stringc::Stringc(const char ch)
  25. {
  26. chunk_size = STRINGC_CHUNK_SIZE;
  27. num_chars = 1;
  28. make_space();
  29. (*this)[0] = ch;
  30. (*this)[num_chars] = '\0';
  31. }
  32. //----------------------------------------------------------------------------
  33. Stringc::Stringc(const char *ch_array)
  34. {
  35. chunk_size = STRINGC_CHUNK_SIZE;
  36. if (ch_array)
  37. num_chars = strlen(ch_array);
  38. else
  39. num_chars = 0;
  40. make_space();
  41. for (int i=0;i<num_chars;i++)
  42. (*this)[i] = ch_array[i];
  43. (*this)[num_chars] = '\0';
  44. }
  45. //----------------------------------------------------------------------------
  46. Stringc::Stringc(int ichar)
  47. {
  48. chunk_size = STRINGC_CHUNK_SIZE;
  49. num_chars = ichar;
  50. make_space();
  51. for (int i=0; i<num_chars; i++)
  52. char_array[i] = ' ';
  53. char_array[num_chars] = '\0';
  54. }
  55. //----------------------------------------------------------------------------
  56. Stringc::Stringc(const Stringc& str)
  57. {
  58. num_chars = str.num_chars;
  59. chunk_size = str.chunk_size;
  60. make_space();
  61. for (int i=0; i< num_chars; i++)
  62. (*this)[i] = str[i];
  63. (*this)[num_chars] = '\0';
  64. }
  65. //----------------------------- Destructor -----------------------------------
  66. //----------------------------------------------------------------------------
  67. Stringc::~Stringc()
  68. {
  69. clear_space();
  70. }
  71. //--------------------------- Private functions ------------------------------
  72. //----------------------------------------------------------------------------
  73. void Stringc::make_space()
  74. {
  75. int size_mult = (num_chars+1)/chunk_size+1;
  76. total_size = size_mult*chunk_size;
  77. char_array = new char[total_size];
  78. }
  79. //----------------------------------------------------------------------------
  80. void Stringc::clear_space()
  81. {
  82. // if ( num_chars >= 0 ) delete [] char_array;
  83. if ( total_size >= 0 ) delete [] char_array;
  84. }
  85. //----------------------------------------------------------------------------
  86. void Stringc::append(char ch)
  87. {
  88. num_chars++;
  89. if ( (num_chars+1) >= total_size )
  90. {
  91. char* old_char_array = char_array;
  92. make_space();
  93. if ( old_char_array )
  94. {
  95. memcpy(char_array, old_char_array, num_chars*sizeof(char));
  96. delete [] old_char_array;
  97. }
  98. }
  99. char_array[num_chars-1] = ch;
  100. char_array[num_chars] = '\0';
  101. }
  102. //----------------------------------------------------------------------------
  103. void Stringc::prepend(char ch)
  104. {
  105. num_chars++;
  106. if ( (num_chars+1) >= total_size )
  107. {
  108. char* old_char_array = char_array;
  109. make_space();
  110. if ( old_char_array )
  111. {
  112. memcpy(char_array+1, old_char_array, num_chars*sizeof(char));
  113. delete [] old_char_array;
  114. }
  115. }
  116. else
  117. {
  118. // shift chars
  119. for (int i = num_chars-1; i > 0; i--)
  120. char_array[i] = char_array[i-1];
  121. }
  122. char_array[0] = ch;
  123. char_array[num_chars] = '\0';
  124. }
  125. //----------------------------------------------------------------------------
  126. void Stringc::rdelete(int istart, int iend)
  127. {
  128. int i=0;
  129. if ((istart > -1)&&(istart < num_chars)&&(iend > -1)&&(iend < num_chars)
  130. &&(istart <= iend))
  131. {
  132. int new_num = num_chars - (iend-istart+1);
  133. int new_size = ((new_num+1)/chunk_size)*chunk_size;
  134. if (new_size == total_size)
  135. {
  136. for( i=istart; i<new_num; i++)
  137. char_array[i] = char_array[i+(iend-istart+1)];
  138. num_chars = new_num;
  139. char_array[num_chars] = '\0';
  140. }
  141. else
  142. {
  143. int new_length = num_chars - (iend-istart+1);
  144. char *new_char = new char[new_length+1];
  145. for (i=0; i<istart; i++)
  146. new_char[i] = char_array[i];
  147. int j=istart;
  148. for (i=iend+1; i<num_chars; i++)
  149. {
  150. new_char[j] = char_array[i];
  151. j++;
  152. }
  153. clear_space();
  154. num_chars = new_length;
  155. make_space();
  156. for ( i=0; i<num_chars; i++)
  157. char_array[i] = new_char[i];
  158. char_array[num_chars] = '\0';
  159. delete [] new_char;
  160. }
  161. }
  162. else
  163. {
  164. cout << "num_chars = " << num_chars << "\n";
  165. cout << "Error in Stringc::delete routine.\n";
  166. cout << " Bad range " << istart << " to " << iend;
  167. cout << "... Nothing will be deleted.\n";
  168. }
  169. }
  170. //---------------------------- Public functions ------------------------------
  171. //----------------------------------------------------------------------------
  172. void Stringc::print()
  173. {
  174. cout << "\n<" << char_array << ">\n";
  175. cout << "Num_chars: " << num_chars << "\n";
  176. cout << "Chunk: " << chunk_size << "\n";
  177. cout << "Total_Size: " << total_size << "\n\n";
  178. }
  179. //----------------------------------------------------------------------------
  180. double Stringc::convert_to_double()
  181. {
  182. double data=0.0;
  183. data = atof(char_array);
  184. return data;
  185. }
  186. //----------------------------------------------------------------------------
  187. int Stringc::count_words()
  188. {
  189. int i = 0;
  190. int iword = 0;
  191. while ( i < num_chars )
  192. {
  193. // Step through spaces
  194. while ( i< num_chars &&
  195. (char_array[i] == ' ' || char_array[i] == '\t')) i++;
  196. // Step through the word and increment the word counter, iword.
  197. if ( i < num_chars )
  198. {
  199. while ( i < num_chars && char_array[i] != ' '
  200. && char_array[i] != '\t') i++;
  201. iword++;
  202. }
  203. }
  204. return iword;
  205. }
  206. //----------------------------------------------------------------------------
  207. Stringc Stringc::get_word(int iword)
  208. {
  209. int i = 0;
  210. int iw = 0;
  211. int istart = 0;
  212. int iend = 0;
  213. // Find the start and end location of the desired iword'th word.
  214. while ( i < num_chars && iw <= iword)
  215. {
  216. // Step through spaces
  217. while ( i< num_chars &&
  218. (char_array[i] == ' ' || char_array[i] == '\t')) i++;
  219. // Step through a word and save its start and end location if it
  220. // is the desired iword.
  221. if ( i < num_chars )
  222. {
  223. if ( iw == iword) istart = i;
  224. while ( i < num_chars && char_array[i] != ' '
  225. && char_array[i] != '\t')
  226. {
  227. if ( iw == iword) iend = i;
  228. i++;
  229. }
  230. iw++;
  231. }
  232. }
  233. // Extract the word string from the character array if iword exists.
  234. if ( iword < iw && iword >= 0)
  235. {
  236. int word_size = iend-istart+1;
  237. Stringc sword(word_size);
  238. for ( i=0; i<word_size; i++)
  239. {
  240. sword[i] = char_array[istart+i];
  241. }
  242. sword[word_size] = '\0';
  243. return sword;
  244. }
  245. else
  246. {
  247. cout << "\nError in Stringc::get_word routine.\n";
  248. if ( iword < 0 )
  249. cout << " Negative word index does not make sense.\n";
  250. else
  251. cout << " Word index is greater than the number of words.\n";
  252. cout << " Returning an empty string as word\n";
  253. Stringc sword(1);
  254. sword[0] = '\0';
  255. return sword;
  256. }
  257. }
  258. //----------------------------------------------------------------------------
  259. void Stringc::delete_word(int iword)
  260. {
  261. int i = 0;
  262. int iw = 0;
  263. int istart = 0;
  264. int iend = 0;
  265. // Find the start and end location of the desired iword word.
  266. while ( i < num_chars && iw <= iword)
  267. {
  268. // Step through spaces
  269. while ( i< num_chars &&
  270. (char_array[i] == ' ' || char_array[i] == '\t')) i++;
  271. // Step through a word and save its start and end location if it
  272. // is the desired iword.
  273. if ( i < num_chars )
  274. {
  275. if ( iw == iword) istart = i;
  276. while ( i < num_chars && char_array[i] != ' '
  277. && char_array[i] != '\t')
  278. {
  279. if ( iw == iword) iend = i;
  280. i++;
  281. }
  282. iw++;
  283. }
  284. }
  285. // If iword exists delete it from the character array.
  286. if ( iword < iw && iword >= 0)
  287. rdelete(istart,iend);
  288. else
  289. {
  290. cout << "Error in Stringc::delete_word routine.\n";
  291. if ( iword < 0 )
  292. cout << " Negative word index does not make sense.\n";
  293. else
  294. cout << " Word index is greater than the number of words.\n";
  295. }
  296. }
  297. //----------------------------------------------------------------------------
  298. int Stringc::count_lines()
  299. {
  300. int i = 0;
  301. int iline = 0;
  302. while ( i < num_chars )
  303. {
  304. // Step through empty lines
  305. while ( i< num_chars &&
  306. (char_array[i] == '\n')) i++;
  307. // Step through the lines and increment the line counter, iline.
  308. if ( i < num_chars )
  309. {
  310. while ( i < num_chars && char_array[i] != '\n') i++;
  311. iline++;
  312. }
  313. }
  314. return iline;
  315. }
  316. //----------------------------------------------------------------------------
  317. Stringc Stringc::get_line(int iline)
  318. {
  319. int i = 0;
  320. int il = 0;
  321. int istart = 0;
  322. int iend = 0;
  323. // Find the start and end location of the desired iline'th line.
  324. while ( i < num_chars && il <= iline)
  325. {
  326. // Step through spaces
  327. while ( i< num_chars && (char_array[i] == '\n')) i++;
  328. // Step through a line and save its start and end location if it
  329. // is the desired iline.
  330. if ( i < num_chars )
  331. {
  332. if ( il == iline) istart = i;
  333. while ( i < num_chars && char_array[i] != '\n' )
  334. {
  335. if ( il == iline) iend = i;
  336. i++;
  337. }
  338. il++;
  339. }
  340. }
  341. // Extract the line string from the character array if iline exists.
  342. if ( iline < il && iline >= 0)
  343. {
  344. int line_size = iend-istart+1;
  345. Stringc sline(line_size);
  346. for ( i=0; i<line_size; i++)
  347. {
  348. sline[i] = char_array[istart+i];
  349. }
  350. sline[line_size] = '\0';
  351. return sline;
  352. }
  353. else
  354. {
  355. cout << "\nError in Stringc::get_line routine.\n";
  356. if ( iline < 0 )
  357. cout << " Negative line index does not make sense.\n";
  358. else
  359. cout << " Line index is greater than the number of lines.\n";
  360. cout << " Returning an empty string as line\n";
  361. Stringc sline(1);
  362. sline[0] = '\0';
  363. return sline;
  364. }
  365. }
  366. //----------------------------------------------------------------------------
  367. void Stringc::delete_line(int iline)
  368. {
  369. int i = 0;
  370. int il = 0;
  371. int istart = 0;
  372. int iend = 0;
  373. // Find the start and end location of the desired iline line.
  374. while ( i < num_chars && il <= iline)
  375. {
  376. // Step through spaces
  377. while ( i< num_chars && (char_array[i] == '\n')) i++;
  378. // Step through a line and save its start and end location if it
  379. // is the desired iline.
  380. if ( i < num_chars )
  381. {
  382. if ( il == iline) istart = i;
  383. while ( i < num_chars && char_array[i] != '\n')
  384. {
  385. if ( il == iline) iend = i;
  386. i++;
  387. }
  388. il++;
  389. }
  390. }
  391. // If iline exists delete it from the character array.
  392. if ( iline < il && iline >= 0)
  393. rdelete(istart,iend);
  394. else
  395. {
  396. cout << "Error in Stringc::delete_line routine.\n";
  397. if ( iline< 0 )
  398. cout << " Negative line index does not make sense.\n";
  399. else
  400. cout << " Line index is greater than the number of lines.\n";
  401. }
  402. }
  403. //----------------------------------------------------------------------------
  404. void Stringc::get_rid_of_comments()
  405. {
  406. int iend=-1;
  407. int i = 0;
  408. // Find location of comment
  409. while ( iend < 0 && i < num_chars-1 )
  410. {
  411. if ((char_array[i] == '/' ) && (char_array[i+1] == '/'))
  412. iend = i;
  413. i++;
  414. }
  415. // Delete comment.
  416. if ( iend >= 0 )
  417. rdelete(iend,num_chars-1);
  418. }
  419. //----------------------------------------------------------------------------
  420. void Stringc::concatenate(const char ch)
  421. {
  422. append(ch);
  423. }
  424. //----------------------------------------------------------------------------
  425. void Stringc::concatenate(const char *cstring)
  426. {
  427. for(int i=0; i < (int)strlen(cstring); i++)
  428. append(cstring[i]);
  429. }
  430. //----------------------------------------------------------------------------
  431. void Stringc::concatenate(const Stringc cstring)
  432. {
  433. for(int i=0; i<cstring.num_chars; i++)
  434. append(cstring[i]);
  435. }
  436. //----------------------------------------------------------------------------
  437. void Stringc::underscore_to_space()
  438. {
  439. for(int i=0; i<num_chars; i++)
  440. if (char_array[i] == '_') char_array[i] = ' ';
  441. }
  442. //----------------------------------------------------------------------------
  443. void Stringc::space_to_underscore()
  444. {
  445. for(int i=0; i<num_chars; i++)
  446. if (char_array[i] == ' ') char_array[i] = '_';
  447. }
  448. //----------------------------------------------------------------------------
  449. void Stringc::change_from_to( const char f, const char t)
  450. {
  451. for(int i=0; i<num_chars; i++)
  452. if (char_array[i] == f) char_array[i] = t;
  453. }
  454. //----------------------------------------------------------------------------
  455. int Stringc::search_for_substring( const char *substring )
  456. {
  457. // Use strstr to find out if substring is in char_array.
  458. char *character = strstr( char_array, substring );
  459. // if a match was found, return the element position of the match
  460. if ( character != NULL )
  461. {
  462. int i = 0;
  463. while ( &char_array[i] != character ) i++;
  464. return i;
  465. }
  466. // if no match was found, return minus 1
  467. return -1;
  468. }
  469. //----------------------------------------------------------------------------
  470. void Stringc::insert_string_at (int position, const Stringc cstring)
  471. {
  472. // check to see if position to insert string at makes sense
  473. if (position > num_chars || position < 0)
  474. {
  475. cout << "Error in Stringc::insert_string_at routine.\n";
  476. cout << " Bad position = " << position << "\n";
  477. cout << " Postion should be >= 0 and < " << num_chars+1 <<"\n";
  478. return;
  479. }
  480. int len_char = cstring.num_chars;
  481. int new_size = num_chars+len_char;
  482. int num_save = num_chars;
  483. int i = 0;
  484. // If more room is needed in the char_array for the new string,
  485. // append spaces.
  486. for ( i=0; i<len_char; i++)
  487. append(' ');
  488. // Shift characters after position to make room for inserted string
  489. for (i=0; i<num_save-position; i++) {
  490. char_array[new_size-i-1] = char_array[num_save-i-1];
  491. }
  492. // Insert string
  493. for (i=position; i<position+len_char; i++)
  494. char_array[i] = cstring[i-position];
  495. num_chars = new_size;
  496. char_array[num_chars] = '\0';
  497. }
  498. //----------------------------------------------------------------------------
  499. void Stringc::insert_string_at (int position, const char *cstring)
  500. {
  501. // check to see if position to insert string at makes sense
  502. if (position > num_chars || position < 0)
  503. {
  504. cout << "Error in Stringc::insert_string_at routine.\n";
  505. cout << " Bad position = " << position << "\n";
  506. cout << " Postion should be >= 0 and < " << num_chars+1 <<"\n";
  507. return;
  508. }
  509. int len_char = strlen(cstring);
  510. int new_size = num_chars+len_char;
  511. int num_save = num_chars;
  512. int i = 0;
  513. // Make room for character string.
  514. for ( i=0; i<len_char; i++)
  515. append(' ');
  516. // Shift characters after position to make room for inserted string
  517. for (i=0; i<num_save-position; i++) {
  518. char_array[new_size-i-1] = char_array[num_save-i-1];
  519. }
  520. // Insert string
  521. for (i=position; i<position+len_char; i++)
  522. char_array[i] = cstring[i-position];
  523. // num_chars = new_size;
  524. // char_array[num_chars] = '\0';
  525. }
  526. //----------------------------------------------------------------------------
  527. void Stringc::insert_string_at (int position, const char character)
  528. {
  529. int num_save = num_chars;
  530. int new_size = num_chars+1;
  531. // Add room for a character.
  532. append(' ');
  533. // Shift characters after position to make room for inserted string
  534. for (int i=0; i<num_save-position; i++)
  535. char_array[new_size-i-1] = char_array[num_save-i-1];
  536. // Insert character
  537. char_array[position] = character;
  538. num_chars = new_size;
  539. char_array[num_chars] = '\0';
  540. }
  541. //----------------------------------------------------------------------------
  542. int Stringc::search_for_substring( int istart, char character )
  543. {
  544. // search for the character
  545. for ( int i = istart; i < num_chars; i++ )
  546. {
  547. if ( char_array[i] == character )
  548. {
  549. return i;
  550. }
  551. }
  552. // if no match was found, return minus 1
  553. return -1;
  554. }
  555. //----------------------------------------------------------------------------
  556. int Stringc::search_for_substring( char character )
  557. {
  558. return search_for_substring( 0, character );
  559. }
  560. //----------------------------------------------------------------------------
  561. int Stringc::matchwild(Stringc wildcardstring)
  562. {
  563. char *wild = wildcardstring();
  564. char *string = get_char_star();
  565. char *cp, *mp;
  566. while ((*string) && (*wild != '*')) {
  567. if ((*wild != *string) && (*wild != '?')) {
  568. return 0;
  569. }
  570. wild++;
  571. string++;
  572. }
  573. while (*string) {
  574. if (*wild == '*') {
  575. if (!*++wild) {
  576. return 1;
  577. }
  578. mp = wild;
  579. cp = string+1;
  580. } else if ((*wild == *string) || (*wild == '?')) {
  581. wild++;
  582. string++;
  583. } else {
  584. wild = mp;
  585. string = cp++;
  586. }
  587. }
  588. while (*wild == '*') {
  589. wild++;
  590. }
  591. return !*wild;
  592. }
  593. //----------------------------------------------------------------------------
  594. void Stringc::delete_range(int istart, int iend)
  595. {
  596. rdelete(istart,iend);
  597. }
  598. //----------------------------------------------------------------------------
  599. Stringc Stringc::get_range(int istart, int iend)
  600. {
  601. int str_len = iend-istart+2;
  602. char *new_char = new char[str_len];
  603. for (int i=istart; i<=iend; i++)
  604. new_char[i-istart] = char_array[i];
  605. new_char[iend+1-istart] = '\0';
  606. Stringc new_string(new_char);
  607. delete [] new_char;
  608. return new_string;
  609. }
  610. //----------------------------------------------------------------------------
  611. void Stringc::overwrite_at_position(int position, const char *cstring)
  612. {
  613. int len_char = strlen(cstring);
  614. if (position < 0)
  615. {
  616. cout << "Error in Stringc::overwrite_at_position routine.\n";
  617. cout << " String: " << char_array;
  618. cout << " Overwrite string: " << cstring << "\n";
  619. cout << " Position cannot be negative it can run from 0 to ";
  620. cout << num_chars-len_char << "\n";
  621. }
  622. else if (position+len_char > num_chars )
  623. {
  624. cout << "Error in Stringc::overwrite_at_position routine.\n";
  625. cout << " String: " << char_array;
  626. cout << " Overwrite string: " << cstring << "\n";
  627. cout << " Position, " << position;
  628. cout << ", is out of range it can run from 0 to ";
  629. cout << num_chars-len_char << "\n";
  630. }
  631. else
  632. for (int i=0; i < len_char; i++)
  633. char_array[i+position] = cstring[i];
  634. }
  635. //----------------------------------------------------------------------------
  636. void Stringc::remove_leading_blanks()
  637. {
  638. if ( num_chars > 0 )
  639. {
  640. int i = 0;
  641. while ( (char_array[i] == ' ' || char_array[i] == '\t') && i<num_chars)
  642. i++;
  643. int iend = i;
  644. if (iend != 0 ) rdelete(0,iend-1);
  645. }
  646. }
  647. //----------------------------------------------------------------------------
  648. void Stringc::remove_trailing_blanks()
  649. {
  650. if ( num_chars > 0 )
  651. {
  652. int i=num_chars-1;
  653. while ( (char_array[i] == ' ' || char_array[i] == '\t') && i>=0 ) i--;
  654. int istart = i+1;
  655. if ( istart != num_chars ) rdelete(istart,num_chars-1);
  656. }
  657. }
  658. //----------------------------------------------------------------------------
  659. void Stringc::remove_leading(char c)
  660. {
  661. if ( num_chars > 0 )
  662. {
  663. int i = 0;
  664. while ( (char_array[i] == c) && i<num_chars)
  665. i++;
  666. int iend = i;
  667. if (iend != 0 ) rdelete(0,iend-1);
  668. }
  669. }
  670. //----------------------------------------------------------------------------
  671. void Stringc::remove_trailing(char c)
  672. {
  673. if ( num_chars > 0 )
  674. {
  675. int i=num_chars-1;
  676. while ( (char_array[i] == c) && i>=0 ) i--;
  677. int istart = i+1;
  678. if ( istart != num_chars ) rdelete(istart,num_chars-1);
  679. }
  680. }
  681. //----------------------------------------------------------------------------
  682. int Stringc::count_substrings(const char *substring)
  683. {
  684. int j = 0;
  685. int len_sub = strlen(substring);
  686. int icount = 0;
  687. for (int i=0; i <= num_chars-len_sub; i++)
  688. {
  689. j = 0;
  690. int matches = 1;
  691. while ( j < len_sub && matches )
  692. {
  693. if ( char_array[i+j] != substring[j] ) matches = 0;
  694. j++;
  695. }
  696. if ( matches ) icount++;
  697. }
  698. return icount;
  699. }
  700. //----------------------------------------------------------------------------
  701. void Stringc::remove_substring(const char *substring, int istring)
  702. {
  703. int j = 0;
  704. int len_sub = strlen(substring);
  705. int icount = 0;
  706. int i=0;
  707. int notfound = 1;
  708. while (notfound && i<num_chars)
  709. {
  710. j = 0;
  711. int matches = 1;
  712. while ( j < len_sub && matches )
  713. {
  714. if ( char_array[i+j] != substring[j] ) matches = 0;
  715. j++;
  716. }
  717. if ( matches ) icount++;
  718. if ( matches && icount == istring)
  719. {
  720. rdelete(i,i+len_sub-1);
  721. notfound = 0;
  722. }
  723. i++;
  724. }
  725. }
  726. //----------------------------------------------------------------------------
  727. void Stringc::remove_all_substrings(const char *substring)
  728. {
  729. int j = 0;
  730. int len_sub = strlen(substring);
  731. int i=0;
  732. while (i < num_chars-len_sub)
  733. {
  734. j = 0;
  735. int matches = 1;
  736. while ( j < len_sub && matches )
  737. {
  738. if ( char_array[i+j] != substring[j] ) matches = 0;
  739. j++;
  740. }
  741. if ( matches ) rdelete(i,i+len_sub-1);
  742. i++;
  743. }
  744. }
  745. //----------------------------------------------------------------------------
  746. void Stringc::remove_substring(char character, int ichar)
  747. {
  748. int j = 0;
  749. int icount = 0;
  750. int i=0;
  751. int notfound = 1;
  752. while (notfound && i<num_chars)
  753. {
  754. j = 0;
  755. int matches = 1;
  756. while ( j < 1 && matches )
  757. {
  758. if ( char_array[i+j] != character ) matches = 0;
  759. j++;
  760. }
  761. if ( matches ) icount++;
  762. if ( matches && icount == ichar)
  763. {
  764. rdelete(i,i);
  765. notfound = 0;
  766. }
  767. i++;
  768. }
  769. }
  770. //----------------------------------------------------------------------------
  771. void Stringc::remove_all_substrings(char character)
  772. {
  773. int j = 0;
  774. int i=0;
  775. while (i < num_chars)
  776. {
  777. j = 0;
  778. int matches = 1;
  779. while ( j < 1 && matches )
  780. {
  781. if ( char_array[i+j] != character ) matches = 0;
  782. j++;
  783. }
  784. if ( matches ) rdelete(i,i);
  785. i++;
  786. }
  787. }
  788. //----------------------------------------------------------------------------
  789. int Stringc::is_double( double* ret )
  790. {
  791. for ( int i = 0 ; i < num_chars ; i++ )
  792. {
  793. if ( char_array[i] == '+' || char_array[i] == '-' || char_array[i] == '.' ||
  794. (( char_array[i] >= '0') && ( char_array[i] <= '9' )) )
  795. continue;
  796. else
  797. return 0;
  798. }
  799. *ret = atof( get_char_star() );
  800. return 1;
  801. }
  802. //----------------------------- OPERATORS ------------------------------------
  803. //----------------------------------------------------------------------------
  804. Stringc& Stringc::operator=( const char ch)
  805. {
  806. (*this).clear_space();
  807. (*this).num_chars = 1;
  808. (*this).chunk_size = STRINGC_CHUNK_SIZE;
  809. (*this).make_space();
  810. (*this).char_array[0] = ch;
  811. (*this).char_array[1] = '\0';
  812. return *this;
  813. }
  814. //----------------------------------------------------------------------------
  815. Stringc& Stringc::operator=( const Stringc& cstring )
  816. {
  817. (*this).clear_space();
  818. (*this).num_chars = cstring.num_chars;
  819. (*this).chunk_size = cstring.chunk_size;
  820. (*this).make_space();
  821. for (int i=0; i<=cstring.num_chars; i++)
  822. (*this).char_array[i] = cstring.char_array[i];
  823. (*this).char_array[num_chars] = '\0';
  824. return *this;
  825. }
  826. //----------------------------------------------------------------------------
  827. Stringc& Stringc::operator=( const char *cstring )
  828. {
  829. (*this).clear_space();
  830. (*this).num_chars = strlen(cstring);
  831. (*this).chunk_size = STRINGC_CHUNK_SIZE;
  832. (*this).make_space();
  833. for (int i=0; i < (int)strlen(cstring); i++)
  834. (*this).char_array[i] = cstring[i];
  835. (*this)[num_chars] = '\0';
  836. return *this;
  837. }
  838. //----------------------------------------------------------------------------
  839. int Stringc::operator==( const Stringc& cstring ) const
  840. {
  841. if ( strcmp( char_array, cstring.char_array ) )
  842. return FALSE;
  843. else
  844. return TRUE;
  845. }
  846. //----------------------------------------------------------------------------
  847. int Stringc::operator==( const char *cstring ) const
  848. {
  849. if ( strcmp( char_array, cstring ) )
  850. return FALSE;
  851. else
  852. return TRUE;
  853. }
  854. //----------------------------------------------------------------------------
  855. int Stringc::operator!=( const Stringc& cstring ) const
  856. {
  857. if ( strcmp( char_array, cstring.char_array ) )
  858. return TRUE;
  859. else
  860. return FALSE;
  861. }
  862. //----------------------------------------------------------------------------
  863. int Stringc::operator!=( const char *cstring ) const
  864. {
  865. if ( strcmp( char_array, cstring ) )
  866. return TRUE;
  867. else
  868. return FALSE;
  869. }
  870. //----------------------------------------------------------------------------
  871. //ostream& operator<<( ostream &os, const Stringc &stringc )
  872. // {
  873. // os << stringc.char_array;
  874. // return os;
  875. // }