home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / doom_i / program / dmreject.exe / SOURCE.ZIP / XSTRING.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-31  |  10.0 KB  |  419 lines

  1. //**********************************************************************************
  2. //    REJECT.EXE - Reject data table builder for DOOM
  3. //    Copyright (C) 1994 L.M.WITEK 
  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 1, or (at your option)
  8. //    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, write to the Free Software
  17. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. //**********************************************************************************
  19.  
  20. #include <string.h>
  21. #include "xstring.hpp"
  22.  
  23.  
  24. /*********************************************************************************
  25. **
  26. **  CONSTRUCTORS:   
  27. **
  28. *********************************************************************************/
  29.  
  30. XString::XString (const char *str)
  31.         : s(strlen (str)+1), len (strlen (str))
  32. {
  33.      strcpy (s, str);
  34. }
  35.  
  36. XString::XString (const XString& str)
  37.         : s (str.s), len (str.len)
  38. {
  39. }
  40.  
  41. XString::XString (const MemHandle& m)
  42.         : s (m), len (strlen (m))
  43. {
  44. }
  45.  
  46. /*********************************************************************************
  47. **
  48. **  DESTRUCTOR:   
  49. **
  50. *********************************************************************************/
  51.  
  52. XString::~XString ()
  53. {
  54. }
  55.  
  56.  
  57. /*********************************************************************************
  58. **
  59. **  FUNCTIONS:   
  60. **
  61. **  RETURNS:   
  62. **
  63. *********************************************************************************/
  64.  
  65. XString& XString::operator= (const XString& str)
  66. {
  67.      s = str.s;
  68.      len = str.len;
  69.      return *this;
  70. }
  71.  
  72. XString& XString::operator= (const char* str)
  73. {
  74.      len = strlen (str);
  75.      s = MemHandle (len+1);
  76.      strcpy (s, str);
  77.      return *this;
  78. }
  79.  
  80.  
  81. /*********************************************************************************
  82. **
  83. **  FUNCTION:   
  84. **
  85. **  RETURNS:   
  86. **
  87. *********************************************************************************/
  88.  
  89. XString::operator const char * () const
  90. {
  91.      return (s);
  92. }
  93.  
  94. /*********************************************************************************
  95. **
  96. **  FUNCTION:   
  97. **
  98. **  RETURNS:   
  99. **
  100. *********************************************************************************/
  101.  
  102. size_t XString::Len () const
  103. {
  104.      return len;
  105. }
  106.  
  107. /*********************************************************************************
  108. **
  109. **  FUNCTIONS: Comparison Operators.  
  110. **
  111. **             Implementations of the following comparison operators:
  112. **
  113. **             ==, !=, >, <, >=, <= 
  114. **
  115. **  RETURNS:  1 if the comparison is true else 0 
  116. **
  117. *********************************************************************************/
  118.  
  119. int XString::operator== (const XString& str) const
  120. {
  121.      return (_stricmp (s, str.s) == 0);
  122. }
  123.  
  124. int XString::operator!= (const XString& str) const 
  125. {
  126.      return (_stricmp (s, str.s) != 0);
  127. }
  128.  
  129. int XString::operator> (const XString& str) const 
  130. {
  131.      return (_stricmp (s, str.s) > 0);
  132. }
  133.  
  134. int XString::operator< (const XString& str) const 
  135. {
  136.      return (_stricmp (s, str.s) < 0);
  137. }
  138.  
  139. int XString::operator>= (const XString& str) const 
  140. {            
  141.      return (_stricmp (s, str.s) >= 0);
  142. }
  143.  
  144. int XString::operator<= (const XString& str) const 
  145. {
  146.      return (_stricmp (s, str.s) <= 0);
  147. }
  148.  
  149.  
  150. /*********************************************************************************
  151. **
  152. **  FUNCTION:   
  153. **
  154. **  RETURNS:   
  155. **
  156. *********************************************************************************/
  157.  
  158. const char* XString::Buffer() const
  159. {
  160.      return (s);
  161. }
  162.                                    
  163.  
  164. /*********************************************************************************
  165. **
  166. **  FUNCTIONS:   
  167. **
  168. **  RETURNS:   
  169. **
  170. *********************************************************************************/
  171.  
  172. XString  XString::operator+ (const XString& str) const 
  173. {                                              
  174.      // -=- Get memory for new string -=-
  175.      MemHandle temp (len + str.len + 1);
  176.                          
  177.      // -=- join the 2 strings -=-                    
  178.      strcpy (temp, s);
  179.      strcat (temp, str.s); 
  180.                     
  181.      // -=- make new string to return -=- 
  182.      return XString (temp);     
  183. }
  184.      
  185.  
  186. XString& XString::operator+=(const XString& str)       
  187. {
  188.      // -=- extend the length of this string -=-
  189.      len += str.len;
  190.  
  191.      // -=- Get memory for new string -=-
  192.      MemHandle temp (len + 1);
  193.      
  194.      // -=- join the 2 strings -=-                    
  195.      strcpy (temp, s);
  196.      strcat (temp, str.s); 
  197.  
  198.      s = temp;
  199.  
  200.      return *this;
  201. }
  202.  
  203.  
  204. /*********************************************************************************
  205. **
  206. **  FUNCTION: XString XString::operator() (int from, int to) const
  207. **            return a substring from a string.
  208. **
  209. **  ENTRY: from = index of starting character of substring
  210. **           to = index of ending character 
  211. **
  212. **  RETURNS:  the substring 
  213. **
  214. *********************************************************************************/
  215.  
  216. XString XString::operator() (int from, int to) const
  217. {
  218.      // -=- check for indexex out of range -=-
  219.      if (((unsigned)from >= len) || (from > to))
  220.           return XString();
  221.                           
  222.      // -=- if 'to' is past the end of the string then set to end -=-
  223.      if ((unsigned)to >= len)
  224.           to = len - 1;
  225.      
  226.      // -=- create buffer to hold string -=-
  227.      MemHandle mh (to - from + 2);    
  228.      
  229.      // -=- copy substring into buffer -=-
  230.      strncpy (mh, &(*this)[from], to - from+1);
  231.      mh[to - from + 1] = 0;
  232.      
  233.      // -=- return substring -=- 
  234.      return XString (mh);
  235. }
  236.  
  237. /*********************************************************************************
  238. **
  239. **  FUNCTION:   
  240. **
  241. **  RETURNS:   
  242. **
  243. *********************************************************************************/
  244.                   
  245. const char& XString::operator[] (int idx) const
  246. {
  247.      static char dummy = 0;
  248.  
  249.      if ((unsigned)idx >= len)
  250.           return dummy;
  251.      else
  252.           return s[idx];
  253. }
  254.  
  255. char& XString::operator[] (int idx)
  256. {
  257.      static char dummy;
  258.  
  259.      if ((unsigned)idx >= len)
  260.      {
  261.           dummy = 0;
  262.           return dummy;
  263.      }
  264.      else
  265.      {
  266.           if (s.RefCount() > 1)
  267.                s = s.Clone();
  268.           return s[idx];
  269.      }
  270. }
  271.  
  272. /*********************************************************************************
  273. **
  274. **  FUNCTION:   
  275. **
  276. **  RETURNS:   
  277. **
  278. *********************************************************************************/
  279.  
  280. void XString::DelWS ()
  281. {
  282.      if (s.RefCount() > 1)
  283.           s = s.Clone();    
  284.      
  285.      char *start = s;
  286.      for(char *x=start+len; (*x<=' ')&&(x >= start) ; *x-- ='\0')
  287.           ;
  288.         
  289.      len = strlen (s);
  290. }
  291.  
  292.  
  293. void XString::ToUpper ()
  294. {
  295.      if (s.RefCount() > 1)
  296.           s = s.Clone();    
  297.      _strupr (s);
  298. }
  299.  
  300. void XString::ToLower ()
  301. {
  302.      if (s.RefCount() > 1)
  303.           s = s.Clone();    
  304.      _strlwr (s);
  305. }
  306.  
  307. int XString::InString (const XString& sub) const
  308. {
  309.      char *x = strstr (s, sub.s);     
  310.  
  311.      if (x == 0)
  312.           return (-1);
  313.      else
  314.           return (x - s);
  315. }
  316.  
  317.  
  318.  
  319.  
  320. //*********************************************************************************
  321. //*********************************************************************************
  322. //**
  323. //**      Test rig for the XString class
  324. //**
  325. //*********************************************************************************
  326. //*********************************************************************************
  327. // #define _TEST_XSTRING_
  328.  
  329. #ifdef _TEST_XSTRING_
  330.  
  331. #include <stdio.h>
  332.  
  333. //*********************************************************************************
  334. // -=- Over loaded new & delete to track free store leeks during testing -=-
  335. //*********************************************************************************
  336.  
  337. void * operator new (size_t s)
  338. {    
  339.      void *p = malloc (s);               
  340.           
  341.      printf ("New: Allocate %04u bytes @%p\n", s, p);
  342.      return p;
  343. }
  344.  
  345. void operator delete (void *s)
  346. {
  347.      printf ("Delete @%p\n", s);
  348.      free (s);
  349. }
  350.  
  351.  
  352. void main ()
  353. {
  354.      XString s1 ("aaaaaaaaaa");
  355.      XString s2 ("bbbbbbbbbb");
  356.      XString s3 (s1);
  357.  
  358.      printf (" s1 %s\n", s1.Buffer());
  359.      printf (" s2 %s\n", s2.Buffer());
  360.      printf (" s3 %s\n", s3.Buffer());
  361.  
  362.  
  363.      printf (" s3 == s2 %d\n", s3 == s2);
  364.      printf (" s2 != s3 %d\n", s2 != s3);
  365.  
  366.      printf (" s1 > s2 %d\n", s1 > s2);
  367.      printf (" s2 > s1 %d\n", s2 > s1);
  368.  
  369.      printf (" s1 < s2 %d\n", s1 < s2);
  370.      printf (" s2 < s1 %d\n", s2 < s1);
  371.  
  372.      printf (" s3 >= s1 %d\n", s3 >= s1);
  373.      printf (" s3 <= s1 %d\n", s3 <= s1);
  374.      
  375.      XString s4;
  376.      
  377.      s4 = s1 + s2;
  378.      printf (" s4 %s\n", s4.Buffer());
  379.      s4 += s2;
  380.      printf (" s4 %s\n", s4.Buffer());
  381.  
  382.      s4 = "wibble ";
  383.      s4 = s4 + s4;
  384.      printf (" s4 %s\n", s4.Buffer());
  385.      s4 += s4;
  386.      printf (" s4 %s\n", s4.Buffer()); 
  387.      
  388.      s4 = s4;
  389.  
  390.      XString s5 = "0123456789";
  391.      XString s6 = s5 (10, 60);
  392.      puts (s6);
  393.  
  394.      XString s7 = s6 = "HELLO WORLD!!";
  395.  
  396.      for (int n = 0; n < 53; n++)
  397.           s6[n] = 'x';
  398.  
  399.      puts (s7);
  400.  
  401.      char a = s7[4]; 
  402.      
  403.      
  404.      XString s8 = "HeLlO 2 yOu                ";
  405.      
  406.      s8.ToUpper ();
  407.      puts (s8);    
  408.      s8.ToLower ();
  409.      puts (s8);
  410.      
  411.      s8.DelWS();
  412.      printf (">%s<\n", s8.Buffer()); 
  413.  
  414.      printf ("%d\n", s8.InString ("you"));
  415.      printf ("%d\n", s8.InString ("wibble"));
  416. }
  417.  
  418. #endif                                    
  419.