home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games / INFESPGAMES.mdf / os2 / ribble / support / cstrng.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-09  |  7.0 KB  |  375 lines

  1. // -------------------------------
  2. //  Name:           CString.cpp
  3. //  Author:         J.R.Shannon
  4. //  Language:       C++
  5. //  Date:           6/11/92
  6. //  Revison:        2.1
  7. //  Last revision:  9/7/93
  8. //  Licence:        Public Domain
  9. //  Purpose:        String handling package
  10. // -------------------------------
  11.  
  12. #include <CStrng.h>
  13. #include <stdio.h>
  14. #include <ctype.h>
  15.  
  16. // Make a CString from nothing.
  17.  
  18. CString::CString()
  19. {
  20.   length = 0;
  21.   chunk = chunkRound(length + 1);
  22.   data = new char[chunk];
  23.   CAssert(data);
  24.   data[0] = '\0';
  25. }
  26.  
  27. // Make a CString from another CString.
  28.  
  29. CString::CString(const CString& from)
  30. {
  31.   length = from.length;
  32.   chunk = from.chunk;
  33.   data = new char[chunk];
  34.   CAssert(data);
  35.   memcpy(data, from.data, length + 1);
  36. }
  37.  
  38. // Make a CString from a '\0' terminated char* string.
  39.  
  40. CString::CString(const char* from)
  41. {
  42.   length = strlen(from);
  43.   chunk = chunkRound(length + 1);
  44.   data = new char[chunk];
  45.   CAssert(data);
  46.   memcpy(data, from, length + 1);
  47. }
  48.  
  49. // Make a CString from a character.
  50.  
  51. CString::CString(const char from)
  52. {
  53.   length = sizeof(from);
  54.   chunk = chunkRound(length + 1);
  55.   data = new char[chunk];
  56.   CAssert(data);
  57.   data[0] = from;
  58.   data[1] = '\0';
  59. }
  60.  
  61. // Make a CString from a decimal number.
  62.  
  63. CString::CString(const unsigned int from)
  64. {
  65.   length = 16;
  66.   chunk = chunkRound(length + 1);
  67.   data = new char[chunk];
  68.   CAssert(data);
  69.   sprintf(data, "%u", from);
  70.   length = strlen(data);
  71. }
  72.  
  73. // Append another CString.
  74.  
  75. CString& CString::operator+=(const CString& from)
  76. {
  77.   if (chunk < length + from.length + 1)
  78.     {
  79.       chunk = chunkRound(length + from.length + 1);
  80.       char* newdata = new char[chunk];
  81.       CAssert(newdata);
  82.       memcpy(newdata, data, length);
  83.       memcpy(newdata+length, from.data, from.length+1);
  84.       delete [] data;
  85.       data = newdata;
  86.     }
  87.   else
  88.     memcpy(data+length, from.data, from.length + 1);
  89.  
  90.   length += from.length;
  91.   return *this;
  92. }
  93.  
  94. // Append a char* '\0' terminated string.
  95.  
  96. CString& CString::operator+=(const char* from)
  97. {
  98.   const size_t fromLen = strlen(from);
  99.  
  100.   if (chunk < length + fromLen + 1)
  101.     {
  102.       chunk = chunkRound(length + fromLen + 1);
  103.       char* newdata = new char[chunk];
  104.       CAssert(newdata);
  105.       memcpy(newdata, data, length);
  106.       memcpy(newdata+length, from, fromLen+1);
  107.       delete [] data;
  108.       data = newdata;
  109.     }
  110.   else
  111.     memcpy(data+length, from, fromLen + 1);
  112.  
  113.   length += fromLen;
  114.   return *this;
  115. }
  116.  
  117. // Append a character to the end of the CString.
  118.  
  119. CString& CString::operator+=(const char from)
  120. {
  121.   const size_t fromLen = sizeof(from);
  122.  
  123.   if (chunk < length + fromLen + 1)
  124.     {
  125.       chunk = chunkRound(length + fromLen + 1);
  126.       char* newdata = new char[chunk];
  127.       CAssert(newdata);
  128.       memcpy(newdata, data, length);
  129.       newdata[length] = from;
  130.       newdata[length+1] = '\0';
  131.       delete [] data;
  132.       data = newdata;
  133.     }
  134.   else
  135.     {
  136.       data[length] = from;
  137.       data[length+1] = '\0';
  138.     }
  139.   length += fromLen;
  140.  
  141.   return *this;
  142. }
  143.  
  144. // Assignment from a CString.
  145.  
  146. CString& CString::operator=(const CString& from)
  147. {
  148.   if (this != &from)
  149.     {
  150.       if (from.length+1 > chunk)
  151.         {
  152.           delete [] data;
  153.           chunk = from.chunk;
  154.           data = new char[chunk];
  155.           CAssert(data);
  156.         }
  157.       length = from.length;
  158.       memcpy(data, from.data, length + 1);
  159.     }
  160.   return *this;
  161. }
  162.  
  163. // Assignment from a '\0' char* string.
  164.  
  165. CString& CString::operator=(const char* from)
  166. {
  167.   delete [] data;
  168.   length = strlen(from);
  169.   chunk = chunkRound(length + 1);
  170.   data = new char[chunk];
  171.   CAssert(data);
  172.   memcpy(data, from, length + 1);
  173.   return *this;
  174. }
  175.  
  176. // Assignment from a character.
  177.  
  178. CString& CString::operator=(const char from)
  179. {
  180.   delete [] data;
  181.   length = sizeof(from);
  182.   chunk = chunkRound(length + 1);
  183.   data = new char[chunk];
  184.   CAssert(data);
  185.   data[0] = from;
  186.   data[1] = '\0';
  187.   return *this;
  188. }
  189.  
  190. // Convert the CString to upper case.
  191.  
  192. void CString::makeUpper(void)
  193. {
  194.   size_t ind = length;
  195.  
  196.   while (ind--)
  197.     data[ind] = (char)toupper(data[ind]);
  198. }
  199.  
  200. // Convert the CString to lower case.
  201.  
  202. void CString::makeLower(void)
  203. {
  204.   size_t ind = length;
  205.  
  206.   while (ind--)
  207.     data[ind] = (char)tolower(data[ind]);
  208. }
  209.  
  210. void
  211. CString::stripTrailing(char _ch)
  212. {
  213.   size_t l = length;
  214.   while (l && data[l - 1] == _ch)
  215.     l--;
  216.   data[l] = '\0';
  217.   length = l;
  218. }
  219.  
  220. //
  221. // IMPLEMENTATION (see CString.cpp as well)
  222. //
  223.  
  224. // Round a string length up to the next highest chunk boundary
  225.  
  226.  
  227. size_t CString::chunkRound(size_t from) const
  228. {
  229.   return ((from / chunk_size) + 1) * chunk_size;
  230. }
  231.  
  232. // Destroy a CString
  233.  
  234.  
  235. CString::~CString()
  236. {
  237.   delete [] data;
  238. }
  239.  
  240. // Add two CString/s together and return the result.
  241.  
  242.  
  243. CString CString::operator+(const CString& from) const
  244. {
  245.   CString temp = *this;
  246.   temp += from;
  247.   return temp;
  248. }
  249.  
  250.  
  251. // Return a char* representation to the data in the CString.
  252. // CAVEAT: trashing the contents is not wise....
  253.  
  254.  
  255. CString::operator char*() const
  256. {
  257.   return data;
  258. }
  259.  
  260. //
  261. //CString::operator size_t() const
  262. //{
  263. //  return length;
  264. //}
  265.  
  266. // Return the length of the CString (NOT including
  267. // the terminal '\0').
  268.  
  269.  
  270. size_t CString::len() const
  271. {
  272.   return strlen(data);
  273. }
  274.  
  275. // Compare two CString/s for equality etc.  Returns same
  276. // values as strcmp.
  277.  
  278.  
  279. int CString::operator==(const CString& other) const
  280. {
  281.   return strcmp(data, other.data) == 0;
  282. }
  283.  
  284.  
  285. int CString::operator!=(const CString& other) const
  286. {
  287.   return strcmp(data, other.data) != 0;
  288. }
  289.  
  290.  
  291. int CString::operator<=(const CString& other) const
  292. {
  293.   return strcmp(data, other.data) <= 0;
  294. }
  295.  
  296.  
  297. int CString::operator>=(const CString& other) const
  298. {
  299.   return strcmp(data, other.data) >= 0;
  300. }
  301.  
  302.  
  303. int CString::operator<(const CString& other) const
  304. {
  305.   return strcmp(data, other.data) < 0;
  306. }
  307.  
  308.  
  309. int CString::operator>(const CString& other) const
  310. {
  311.   return strcmp(data, other.data) > 0;
  312. }
  313.  
  314.  
  315. int CString::operator==(const char* other) const
  316. {
  317.   return strcmp(data, other) == 0;
  318. }
  319.  
  320.  
  321. int CString::operator!=(const char* other) const
  322. {
  323.   return strcmp(data, other) != 0;
  324. }
  325.  
  326.  
  327. int CString::operator<=(const char* other) const
  328. {
  329.   return strcmp(data, other) <= 0;
  330. }
  331.  
  332.  
  333. int CString::operator>=(const char* other) const
  334. {
  335.   return strcmp(data, other) >= 0;
  336. }
  337.  
  338.  
  339. int CString::operator<(const char* other) const
  340. {
  341.   return strcmp(data, other) < 0;
  342. }
  343.  
  344.  
  345. int CString::operator>(const char* other) const
  346. {
  347.   return strcmp(data, other) > 0;
  348. }
  349.  
  350. // Return a reference to the character at the specified
  351. // index in the CString.
  352. // CAVEAT:  Trashing the referred character is not advised.
  353. //          No bounds checking is performed.
  354.  
  355.  
  356. const char& CString::operator[](size_t ind)
  357. {
  358.   return data[ind];
  359. }
  360.  
  361. // strchr and strrchr facilities.
  362.  
  363.  
  364. char* CString::index(const int ch)
  365. {
  366.   return strchr(data, ch);
  367. }
  368.  
  369.  
  370. char* CString::rindex(const int ch)
  371. {
  372.   return strrchr(data, ch);
  373. }
  374.  
  375.