Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
1.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1994 - 1999
  6. //
  7. // File: string.cxx
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. //
  12. // Filename: String.cxx
  13. //
  14. // Description: contains class functions for a basic
  15. // string class
  16. //
  17. // Author: Scott Holden (t-scotth)
  18. //
  19. //
  20. #include "String.hxx"
  21. String::String( const char *str )
  22. {
  23. _length = strlen( str );
  24. _string = new char[ _length + 1 ];
  25. strcpy( _string, str );
  26. }
  27. String::String( const String& str )
  28. {
  29. _length = str._length;
  30. _string = new char[ _length + 1 ];
  31. strcpy( _string, str._string );
  32. }
  33. String&
  34. String::operator=( const String& str )
  35. {
  36. if ( this != &str ) { //bad things happen if s=s
  37. if ( _string ) {
  38. delete[] _string;
  39. }
  40. _length = str._length;
  41. _string = new char[ _length + 1 ];
  42. strcpy( _string, str._string );
  43. }
  44. return *this;
  45. }
  46. String&
  47. String::operator=( const char* str )
  48. {
  49. if ( _string ) {
  50. delete[] _string;
  51. }
  52. _length = strlen( str );
  53. _string = new char[ _length + 1 ];
  54. strcpy( _string, str );
  55. return *this;
  56. }
  57. ostream&
  58. operator<<( ostream& s, const String &x )
  59. {
  60. return s << x._string;
  61. }
  62. istream&
  63. operator>>( istream& s, String &x )
  64. {
  65. char buf[100];
  66. s >> buf;
  67. x = buf;
  68. //cout << x._string << " \t[" << x._length << "]" << endl;
  69. return s;
  70. }
  71. char&
  72. String::operator[]( int i )
  73. {
  74. if ( ( i < 0 ) || ( _length <= i ) ) {
  75. cerr << "Error: index out of range" << endl;
  76. }
  77. return _string[i];
  78. }
  79. const char&
  80. String::operator[]( int i ) const
  81. {
  82. if ( ( i < 0 ) || ( _length <= i ) ) {
  83. cerr << "Error: index out of range" << endl;
  84. }
  85. return _string[i];
  86. }