home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP10-8.ZIP / CLASSSRC.ZIP / STRNG.CPP < prev   
C/C++ Source or Header  |  1990-09-26  |  6KB  |  256 lines

  1. //
  2. // This file contains proprietary information of Borland International.
  3. // Copying or reproduction without prior written approval is prohibited.
  4. //
  5. // Copyright (c) 1990
  6. // Borland International
  7. // 1800 Green Hills Road.
  8. // Scotts Valley, CA 95066
  9. // (408) 438-8400
  10. //
  11.  
  12. // Contents ----------------------------------------------------------------
  13. //
  14. //      String::String                          copy constructor
  15. //      String::~String
  16. //      String::isEqual
  17. //      String::isLessThan
  18. //      String::isA
  19. //      String::nameOf
  20. //      String::hashValue
  21. //      String::printOn
  22. //         String::operator =
  23. //
  24. // Description
  25. //
  26. //      Contains the implementation of class String member functions.
  27. //
  28. // End ---------------------------------------------------------------------
  29.  
  30. // Interface Dependencies ---------------------------------------------------
  31.  
  32. #ifndef __CLSTYPES_H
  33. #include <clstypes.h>
  34. #endif
  35.  
  36. #ifndef __STRNG_H
  37. #include <strng.h>
  38. #endif
  39.  
  40. // End Interface Dependencies ------------------------------------------------
  41.  
  42. // Implementation Dependencies ----------------------------------------------
  43.  
  44. #ifndef __STDLIB_H
  45. #include <stdlib.h>
  46. #define __STDLIB_H
  47. #endif
  48.  
  49. #ifndef __STRING_H
  50. #include <string.h>
  51. #define __STRING_H
  52. #endif
  53.  
  54. // End Implementation Dependencies -------------------------------------------
  55.  
  56.  
  57. // Member Function //
  58.  
  59. String::String( const String& sourceString )
  60.  
  61. // Summary -----------------------------------------------------------------
  62. //
  63. //      Copy constructor
  64. //
  65. // Parameters
  66. //
  67. //     sourceString
  68. //
  69. //     The string we are to assign to this.
  70. //
  71. // End ---------------------------------------------------------------------
  72. {
  73.         len = sourceString.len;
  74.         theString = new char[ len ];
  75.         strcpy( theString, sourceString.theString );
  76. }
  77. // End Member Function String::operator = //
  78.  
  79. // Destructor //
  80.  
  81. String::~String()
  82.  
  83. // Summary -----------------------------------------------------------------
  84. //
  85. //      Destructor for a String object.  Deletes the storage for the
  86. //      character string.
  87. //
  88. // End ---------------------------------------------------------------------
  89. {
  90.     delete theString;
  91. }
  92. // End Destructor //
  93.  
  94.  
  95. // Member Function //
  96.  
  97. String::isEqual( const Object& testString ) const
  98.  
  99. // Summary -----------------------------------------------------------------
  100. //
  101. //     Determines whether two strings are equal.  Strings
  102. //      are equal if they are the same length and contain the same
  103. //      characters.
  104. //
  105. // Parameters
  106. //
  107. //      testString
  108. //
  109. //      The string we are testing against this.
  110. //
  111. // End ---------------------------------------------------------------------
  112. {
  113.     return ( len == ((String &)testString).len &&
  114.              !strcmp( theString, ((String &)testString).theString ) );
  115. }
  116. // End Member Function String::isEqual //
  117.  
  118.  
  119. // Member Function //
  120.  
  121. int String::isLessThan( const Object& testString ) const
  122.  
  123. // Summary -----------------------------------------------------------------
  124. //
  125. //      Determines whether the given string is less than this.  The strings
  126. //      need not be the same length.
  127. //
  128. // Parameters
  129. //
  130. //      testString
  131. //
  132. //      The string we are testing against this.
  133. //
  134. // End ---------------------------------------------------------------------
  135. {
  136.     return ( strcmp ( theString, ((String &)testString).theString ) < 0 );
  137. }
  138. // End Member Function String::isLessThan //
  139.  
  140.  
  141. // Member Function //
  142.  
  143. classType String::isA() const
  144.  
  145. // Summary -----------------------------------------------------------------
  146. //
  147. //      Returns a predefined value for the class String.
  148. //
  149. // Parameters
  150. //
  151. //      none
  152. //
  153. // End ---------------------------------------------------------------------
  154. {
  155.     return stringClass;
  156. }
  157. // End Member Function String::isA //
  158.  
  159.  
  160. // Member Function //
  161.  
  162. char *String::nameOf() const
  163.  
  164. // Summary -----------------------------------------------------------------
  165. //
  166. //      Returns the string "String".
  167. //
  168. // Parameters
  169. //
  170. //      none
  171. //
  172. // End ---------------------------------------------------------------------
  173. {
  174.     return "String";
  175. }
  176. // End Member Function String::nameOf //
  177.  
  178.  
  179. // Member Function //
  180.  
  181. hashValueType String::hashValue() const
  182.  
  183. // Summary -----------------------------------------------------------------
  184. //
  185. //      Returns the hash value of a string object.
  186. //
  187. // End ---------------------------------------------------------------------
  188. {
  189.     hashValueType    value = hashValueType(0);
  190.  
  191.     for( int i = 0; i < len; i++ )
  192.     {
  193.         value ^= theString[i];
  194.         value = _rotl( value, 1 );
  195.     }
  196.     return value;
  197. }
  198. // End Member Function String::hashValue //
  199.  
  200.  
  201. // Member Function //
  202.  
  203. void    String::printOn( ostream& outputStream ) const
  204.  
  205. // Summary -----------------------------------------------------------------
  206. //
  207. //      Displays this object on the given stream.
  208. //
  209. // Parameters
  210. //
  211. //      outputStream
  212. //
  213. //      The stream where we are to display the object.
  214. //
  215. // End ---------------------------------------------------------------------
  216. {
  217.     outputStream << theString;
  218. }
  219. // End Member Function String::printOn //
  220.  
  221.  
  222. // Member Function //
  223.  
  224. String& String::operator =( const String& sourceString )
  225.  
  226. // Summary -----------------------------------------------------------------
  227. //
  228. //      Assignment operator for strings.
  229. //
  230. // Parameters
  231. //
  232. //     sourceString
  233. //
  234. //     The string we are to assign to this.
  235. //
  236. // Functional Description
  237. //
  238. //     We check that we are doing s = s, then copy the contents of
  239. //     the source string into this string.
  240. //
  241. // End ---------------------------------------------------------------------
  242. {
  243.     if ( *this != sourceString )
  244.     {
  245.         if ( len != sourceString.len )
  246.         {
  247.             delete theString;
  248.             len = sourceString.len;
  249.             theString = new char[ len ];
  250.         }
  251.         (void)strcpy( theString, sourceString.theString );
  252.     }
  253.     return *this;
  254. }
  255. // End Member Function String::operator = //
  256.