home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 14 / MA_Cover_14.iso / source / c / pegase_src / stringclass.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-14  |  14.6 KB  |  601 lines

  1. /*
  2. **
  3. ** StringClass.cpp
  4. **
  5. ** (c) 1998 Didier Levet
  6. **
  7. ** String classes
  8. **
  9. ** $Revision: 1.4 $
  10. ** $State: Exp $
  11. ** $Date: 1998/11/23 14:38:53 $
  12. **
  13. ** $Log: StringClass.cpp $
  14. ** Revision 1.4  1998/11/23 14:38:53  kakace
  15. ** Reworked CString and CStrArray classes
  16. **
  17. ** Revision 1.3  1998/11/22 14:57:17  kakace
  18. ** Class CStrArray now uses class CString and seems to work fine
  19. **
  20. ** Revision 1.2  1998/11/21 20:58:24  kakace
  21. ** Class CString seems to be okay now
  22. **
  23. ** Revision 1.1  1998/11/11 16:35:49  kakace
  24. ** Initial revision
  25. **
  26. **
  27. */
  28.  
  29.  
  30. #ifndef  _STRING_CLASS_HPP
  31. #include "StringClass.hpp"
  32. #endif
  33.  
  34. #ifndef  _INCLUDE_STDLIB_H
  35. #include <stdlib.h>
  36. #endif
  37.  
  38. #ifndef  _INCLUDE_STRING_H
  39. #include <string.h>
  40. #endif
  41.  
  42.  
  43. //----------------------------------------------------------------------------------------------------
  44. //========================================== Class CString ===========================================
  45. //----------------------------------------------------------------------------------------------------
  46.  
  47. //==================================================================================================
  48. //
  49. //   SYNOPSIS                                                                       CString::CString
  50. //   CString::CString(str, minLength = 0)
  51. //   CString::CString(STRING, ULONG);
  52. //
  53. //   FUNCTION
  54. //      Initialize the instance with the string given. The string buffer is allocated on the heap,
  55. //      and its size is equal to MAX(minLength, strlen(str)).
  56. //
  57. //   INPUTS
  58. //   str       - String to copy into a new buffer, or NULL.
  59. //   minLength - Minimum buffer size.
  60. //
  61. //   NOTES
  62. //
  63. //   SEE ALSO
  64. //
  65. //   HISTORY
  66. //
  67. //==================================================================================================
  68. /// CString::CONSTRUCTOR
  69.  
  70.  
  71. CString::CString(STRING str, ULONG minLength)
  72. {
  73.     ULONG c = stringlen(str);
  74.  
  75.     if (c < minLength) c = minLength;
  76.  
  77.     release();                          // Initialize the instance to a NULL state.
  78.     newstring(str, c);
  79. }
  80. ///
  81.  
  82. //==================================================================================================
  83. //
  84. //   SYNOPSIS                                                                        CString::extend
  85. //   buffsize = CString::extend(newSize)
  86. //   ULONG CString::extend(ULONG);
  87. //
  88. //   FUNCTION
  89. //      Extend the string buffer to be newSize bytes wide, unless it is currently larger than that.
  90. //
  91. //   INPUTS
  92. //   newSize - New buffer size.
  93. //
  94. //   RESULT
  95. //   buffsize - Current buffer size.
  96. //
  97. //   NOTES
  98. //
  99. //   SEE ALSO
  100. //
  101. //   HISTORY
  102. //
  103. //==================================================================================================
  104. /// CString::extend()
  105.  
  106.  
  107. ULONG CString::extend(ULONG newSize)
  108. {
  109.     if (iBuffLen < newSize)
  110.     {
  111.         newstring(pcString, newSize);
  112.     }
  113.  
  114.     return iBuffLen;
  115. }
  116. ///
  117.  
  118. //==================================================================================================
  119. //
  120. //   SYNOPSIS                                                                              operator>
  121. //   bool = CString::operator>(str)
  122. //   int CString::operator>(const CString&);
  123. //
  124. //   FUNCTION
  125. //      Compare two strings.
  126. //
  127. //   INPUTS
  128. //   str - String to compare with (can be NULL).
  129. //
  130. //   RESULT
  131. //   bool - TRUE if this string is "greater" than the string given in parameter, FALSE otherwise.
  132. //      The result is also valid when one (or both) string is undefined (NULL).
  133. //
  134. //   NOTES
  135. //
  136. //   SEE ALSO
  137. //
  138. //   HISTORY
  139. //
  140. //==================================================================================================
  141. /// CString::OPERATOR >
  142.  
  143.  
  144. int CString::operator > (const CString& str) const
  145. {
  146.     int result;
  147.  
  148.     if (pcString == NULL)
  149.     {
  150.         result = FALSE;
  151.     }
  152.     else if (str == EMPTY || strcmp(pcString, str) > 0)
  153.     {
  154.         result = TRUE;
  155.     }
  156.  
  157.     return result;
  158. }
  159. ///
  160.  
  161. //==================================================================================================
  162. //
  163. //   SYNOPSIS                                                                             operator!=
  164. //   bool = CString::operator!=(str)
  165. //   int CString::operator!=(const CString&);
  166. //
  167. //   FUNCTION
  168. //      Compare two strings.
  169. //
  170. //   INPUTS
  171. //   str - String to compare with (can be NULL)
  172. //
  173. //   RESULT
  174. //   bool - TRUE if the strings are different, FALSE otherwise. The result is also valid when one
  175. //      (or both) string is undefined (NULL).
  176. //
  177. //   NOTES
  178. //
  179. //   SEE ALSO
  180. //
  181. //   HISTORY
  182. //
  183. //==================================================================================================
  184. /// CString::OPERATOR !=
  185.  
  186.  
  187. int CString::operator !=(const CString& str) const
  188. {
  189.     int result = (pcString != str.pcString);    // FALSE if pcString == str.pcString == NULL
  190.                                                 // (a string can't be pointed to by more than one pointer).
  191.  
  192.     if (iLength == str.iLength && pcString != NULL && str.pcString != NULL && strcmp(pcString, str) == 0)
  193.         result = FALSE;
  194.  
  195.     return result;
  196. }
  197. ///
  198.  
  199. //==================================================================================================
  200. //
  201. //   SYNOPSIS                                                                    CString::stringcopy
  202. //   len = CString::stringcopy(source, dest)
  203. //   ULONG CString::stringcopy(STRING, char *);
  204. //
  205. //   FUNCTION
  206. //      Copy the source NUL terminated string to the destination buffer.
  207. //
  208. //   INPUTS
  209. //   source - Source string, or NULL, or empty string.
  210. //   dest   - Destination buffer. It must be large enough to hold the whole string.
  211. //
  212. //   RESULT
  213. //   len - Length of the string copied.
  214. //
  215. //   PRECONDITIONS
  216. //      dest != NULL
  217. //
  218. //   POSTCONDITIONS
  219. //      The destination buffer holds a NUL terminated string that is either an empty string or the
  220. //      copy of the source string.
  221. //
  222. //   NOTES
  223. //
  224. //   SEE ALSO
  225. //
  226. //   HISTORY
  227. //
  228. //==================================================================================================
  229. ///CString::stringcopy()           [PROTECTED]
  230.  
  231.  
  232. ULONG CString::stringcopy(STRING source, char *dest)
  233. {
  234.     ULONG  len = 0;
  235.     STRING start = source;
  236.  
  237.     if (source != NULL)
  238.     {
  239.         while ( (*dest++ = *source++) != '\0')
  240.             ;
  241.         len = source - start - 1;
  242.     }
  243.     else *dest = '\0';
  244.  
  245.     return len;
  246. }
  247. ///
  248.  
  249. //==================================================================================================
  250. //
  251. //   SYNOPSIS                                                                     CString::newstring
  252. //   CString::newstring(str, size)
  253. //   void CString::newstring(STRING, ULONG);
  254. //
  255. //   FUNCTION
  256. //      Allocate a new buffer then copy the string given into it.
  257. //
  258. //   INPUTS
  259. //   str  - String to copy. Can be an empty string, or NULL.
  260. //   size - Minimum buffer size or string's length. If size is equal to ZERO, this function
  261. //      releases the buffer currently held.
  262. //
  263. //   PRECONDITIONS
  264. //      size >= strlen(str)
  265. //
  266. //   POSTCONDITIONS
  267. //      pcString == NULL : iLength = iBuffLen = 0
  268. //      pcString != NULL : iLength = string's length, iBuffLen = buffer's size.
  269. //
  270. //   NOTES
  271. //
  272. //   SEE ALSO
  273. //
  274. //   HISTORY
  275. //
  276. //==================================================================================================
  277. /// CString::newstring()            [PROTECTED]
  278.  
  279.  
  280. void CString::newstring(STRING str, ULONG size)
  281. {
  282.     char  *p = NULL;
  283.  
  284.     iBuffLen = iLength = 0;
  285.  
  286.     // Allocate a new buffer.
  287.  
  288.     if (size > 0 && (p = new char[size + 1]) != NULL)
  289.     {
  290.         iLength = stringcopy(str, p);
  291.         iBuffLen = size + 1;
  292.     }
  293.  
  294.     freebuff();                     // Free the old buffer if necessary.
  295.     pcString = p;
  296. }
  297. ///
  298.  
  299. //==================================================================================================
  300. //
  301. //   SYNOPSIS                                                                     CString::stringlen
  302. //   len = CString::stringlen(s)
  303. //   ULONG CString::stringlen(const char []);
  304. //
  305. //   FUNCTION
  306. //      Returns the length of the string passed in parameter.
  307. //
  308. //   INPUTS
  309. //   s - Pointer to a NUL terminated string, or NULL.
  310. //
  311. //   RESULT
  312. //   len - Length of the given string, or ZERO if the string is empty or undefined (NULL).
  313. //
  314. //   NOTES
  315. //
  316. //   SEE ALSO
  317. //
  318. //   HISTORY
  319. //
  320. //==================================================================================================
  321. /// CString::stringlen()            [PROTECTED]
  322.  
  323.  
  324. ULONG CString::stringlen(const char s[])
  325. {
  326.     STRING str = s;
  327.     ULONG len = 0;
  328.  
  329.     if (str != NULL)
  330.     {
  331.         while (*str++ != '\0')
  332.             ;
  333.         len = str - s - 1;
  334.     }
  335.  
  336.     return len;
  337. }
  338. ///
  339.  
  340. //==================================================================================================
  341. //
  342. //   SYNOPSIS                                                                        CString::concat
  343. //   CString::concat(str, len)
  344. //   void CString::concat(STRING, ULONG);
  345. //
  346. //   FUNCTION
  347. //      Concatenate the string with the current object. The buffer is extended when necessary.
  348. //
  349. //   INPUTS
  350. //   str - String to add (or NULL).
  351. //   len - Length of the string to add.
  352. //
  353. //   PRECONDITIONS
  354. //      len >= strlen(str)
  355. //
  356. //   NOTES
  357. //
  358. //   SEE ALSO
  359. //
  360. //   HISTORY
  361. //
  362. //==================================================================================================
  363. /// CString::concat()               [PROTECTED]
  364.  
  365.  
  366. void CString::concat(STRING str, ULONG len)
  367. {
  368.     if (pcString == NULL)
  369.     {
  370.         newstring(str, len);
  371.     }
  372.     else
  373.     {
  374.         ULONG total_len = iLength + len;
  375.  
  376.         if(extend(total_len) >= total_len)
  377.         {
  378.             iLength += stringcopy(str, pcString + iLength);
  379.         }
  380.     }
  381. }
  382. ///
  383.  
  384. //==================================================================================================
  385. //
  386. //   SYNOPSIS                                                                     CString::duplicate
  387. //   CString::duplicate(str, len)
  388. //   void CString::duplicate(STRING, ULONG);
  389. //
  390. //   FUNCTION
  391. //      Duplicate the string passed in.
  392. //
  393. //   INPUTS
  394. //   str - String to copy.
  395. //   len - Buffer size
  396. //
  397. //   PRECONDITIONS
  398. //      len >= strlen(str)
  399. //
  400. //   NOTES
  401. //
  402. //   SEE ALSO
  403. //
  404. //   HISTORY
  405. //
  406. //==================================================================================================
  407. /// CString::duplicate()            [PROTECTED]
  408.  
  409.  
  410. void CString::duplicate(STRING str, ULONG len)
  411. {
  412.     if (iBuffLen > len)                         // <==> pcString != NULL
  413.     {
  414.         iLength = stringcopy(str, pcString);
  415.     }
  416.     else
  417.     {
  418.         newstring(str, len);
  419.     }
  420. }
  421. ///
  422.  
  423.  
  424. //----------------------------------------------------------------------------------------------------
  425. //========================================= Class CStrArray ==========================================
  426. //----------------------------------------------------------------------------------------------------
  427.  
  428. //==================================================================================================
  429. //
  430. //   SYNOPSIS                                                                   CStrArray::operator=
  431. //   object = CStrArray::operator=(array)
  432. //   CStrArray& CStrArray::operator=(CStrArray&);
  433. //
  434. //   FUNCTION
  435. //      Make the current object to adopt array's buffers.
  436. //
  437. //   INPUTS
  438. //   array - Source object.
  439. //
  440. //   RESULT
  441. //   object - Destination object.
  442. //
  443. //   NOTES
  444. //      All buffers held by the current object (*this) are freed before adopting the new ones.
  445. //
  446. //   SEE ALSO
  447. //      CStrArray::freebuff(), CStrArray::adopt()
  448. //
  449. //   HISTORY
  450. //
  451. //==================================================================================================
  452. /// CStrArray::operator = (array)
  453.  
  454.  
  455. CStrArray& CStrArray::operator =(CStrArray& array)
  456. {
  457.     if (&array != this)
  458.     {
  459.         freebuff();             // Free the current array.
  460.         AdoptArray(array);
  461.     }
  462.  
  463.     return *this;
  464. }
  465. ///
  466.  
  467. //==================================================================================================
  468. //
  469. //   SYNOPSIS                                                                   CStrArray::operator=
  470. //   object = CStrArray::operator=(array)
  471. //   CStrArray& CStrArray::operator=(STRING *);
  472. //
  473. //   FUNCTION
  474. //      Initialize the current object with the string array. All strings are copied into new
  475. //      buffers.
  476. //
  477. //   INPUTS
  478. //   array - String array to copy.
  479. //
  480. //   RESULT
  481. //   object - Current object.
  482. //
  483. //   NOTES
  484. //
  485. //   SEE ALSO
  486. //      CStrArray::Add()
  487. //
  488. //   HISTORY
  489. //
  490. //==================================================================================================
  491. /// CStrArray::operator = (STRING *)
  492.  
  493.  
  494. CStrArray& CStrArray::operator =(STRING *array)
  495. {
  496.     if (array != NULL)
  497.     {
  498.         while (*array)
  499.         {
  500.             if (Add(*array) == FALSE)
  501.             {
  502.                 cleanup();
  503.                 break;
  504.             }
  505.             ++array;
  506.         }
  507.     }
  508.  
  509.     return *this;
  510. }
  511. ///
  512.  
  513. //==================================================================================================
  514. //
  515. //   SYNOPSIS                                                                       CStrArray::adopt
  516. //   result = CStrArray::adopt(str)
  517. //   bool CStrArray::adopt(CString&);
  518. //
  519. //   FUNCTION
  520. //      Add a new string into the array. This array is extended when required.
  521. //      The string is adopted by the string array. This means that it goes in an empty state.
  522. //
  523. //   INPUTS
  524. //   str - String object to add.
  525. //
  526. //   RESULT
  527. //   result - FALSE if an error as occured (not enough memory).
  528. //
  529. //   NOTES
  530. //
  531. //   SEE ALSO
  532. //      CStrArray::extend(), CStrArray::newstring()
  533. //
  534. //   HISTORY
  535. //
  536. //==================================================================================================
  537. /// CStrArray::adopt()
  538.  
  539.  
  540. int CStrArray::adopt(CString& str)
  541. {
  542.     if (extend() != FALSE)
  543.     {
  544.         if (str != CString::EMPTY) newstring(str);
  545.         return TRUE;
  546.     }
  547.  
  548.     return FALSE;
  549. }
  550. ///
  551.  
  552. //==================================================================================================
  553. //
  554. //   SYNOPSIS                                                                      CStrArray::extend
  555. //   result = CStrArray::extend(size = granularity())
  556. //   bool CStrArray::extend(ULONG);
  557. //
  558. //   FUNCTION
  559. //      Extend the buffer when necessary.
  560. //
  561. //   RESULT
  562. //   result - FALSE if an error has occured (not enough memory).
  563. //
  564. //   NOTES
  565. //
  566. //   SEE ALSO
  567. //
  568. //   HISTORY
  569. //
  570. //==================================================================================================
  571. ///CStrArray::extend()
  572.  
  573.  
  574. int CStrArray::extend(ULONG size)
  575. {
  576.     int result = TRUE;
  577.  
  578.     if (iCurrentIndex == iNumEntries)
  579.     {
  580.         ULONG num_entries  = iNumEntries + size;
  581.         CString *array = new CString[num_entries];
  582.  
  583.         if (array != NULL)
  584.         {
  585.             for (int i = 0; i < iCurrentIndex; ++i)
  586.             {
  587.                 array[i].adopt(voStringArray[i]);
  588.             }
  589.  
  590.             freebuff();
  591.             voStringArray = array;
  592.             iNumEntries   = num_entries;
  593.         }
  594.         else result = FALSE;
  595.     }
  596.  
  597.     return result;
  598. }
  599. ///
  600.  
  601.