home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_40.arc / DAIMS.ARC / STRINGS.CPP < prev    next >
Text File  |  1988-02-10  |  4KB  |  236 lines

  1. #include <stdio.h>
  2. #include "strings.hxx"
  3. #undef TEST
  4. /* 
  5. -*++ string::string(): un-initialized string object
  6. ** 
  7. ** (*++ history: 
  8. **      7 Dec 87    Bruce Eckel    Creation date
  9. ** ++*)
  10. ** 
  11. ** (*++ detailed: 
  12. ** ++*)
  13. */
  14.  
  15.  string::string()
  16. {
  17.   p = new srep;
  18.   p->s = 0;
  19.   p->n = 1;
  20. }
  21.  
  22. /* 
  23. -*++ string::string(): build a string object from a string of characters
  24. ** 
  25. ** (*++ history: 
  26. **      7 Dec 87    Bruce Eckel    Creation date
  27. ** ++*)
  28. ** 
  29. ** (*++ detailed: 
  30. ** ++*)
  31. */
  32.  
  33.  string::string(char* s)
  34. {
  35.   p = new srep;
  36.   p->s = new char[strlen(s)+1];
  37.   strcpy(p->s,s);
  38.   p->n = 1;
  39. }
  40.  
  41.  
  42. /* 
  43. -*++ string::string(): build a string object from a string reference
  44. ** 
  45. ** (*++ history: 
  46. **      7 Dec 87    Bruce Eckel    Creation date
  47. ** ++*)
  48. ** 
  49. ** (*++ detailed: 
  50. ** ++*)
  51. */
  52.  
  53.  string::string(string& x)
  54. {
  55.   x.p->n++;
  56.   p = x.p;
  57. }
  58.  
  59.  
  60. /* 
  61. -*++ string::~string(): destructor for string objects
  62. ** 
  63. ** (*++ history: 
  64. **      7 Dec 87    Bruce Eckel    Creation date
  65. ** ++*)
  66. ** 
  67. ** (*++ detailed: 
  68. ** ++*)
  69. */
  70.  
  71.  string::~string()
  72. {
  73.   if (--p->n == 0){
  74.     delete p->s;
  75.     delete p;
  76.   }
  77. }
  78.  
  79.  
  80. /* 
  81. -*++ string::operator=(): string object assignment to character string
  82. ** 
  83. ** (*++ history: 
  84. **      7 Dec 87    Bruce Eckel    Creation date
  85. ** ++*)
  86. ** 
  87. ** (*++ detailed: 
  88. ** ++*)
  89. */
  90.  
  91. string & string::operator=(char * s)
  92. {
  93.   if (p->n > 1) {
  94.     p->n--;
  95.     p = new srep;
  96.   }
  97.   else if (p->n == 1)
  98.     delete p->s;
  99.  
  100.   p->s = new char[strlen(s) + 1 ];
  101.   strcpy(p->s,s);
  102.   p->n = 1;
  103.   return *this;
  104. }
  105.  
  106.  
  107. /* 
  108. -*++ string::operator=(): string object assignment to string object
  109. ** 
  110. ** (*++ history: 
  111. **      7 Dec 87    Bruce Eckel    Creation date
  112. ** ++*)
  113. ** 
  114. ** (*++ detailed: 
  115. ** ++*)
  116. */
  117.  
  118. string& string::operator=(string& x)
  119. {
  120.   x.p->n++;
  121.   if (--p->n == 0) {
  122.     delete p->s;
  123.     delete p;
  124.   }
  125.   p = x.p;
  126.   return *this;
  127. }
  128.  
  129.  
  130.  
  131. /* 
  132. -*++ string::operator<<(): stream output of a string
  133. ** 
  134. ** (*++ history: 
  135. **      7 Dec 87    Bruce Eckel    Creation date
  136. ** ++*)
  137. ** 
  138. ** (*++ detailed: 
  139. ** ++*)
  140. */
  141.  
  142. ostream& operator<<(ostream& s, string& x)
  143. {
  144.   return s << x.p->s << " [" << x.p->n << "]\n";
  145. }
  146.  
  147.  
  148. /* 
  149. -*++ string::operator>>(): stream input to a string
  150. ** 
  151. ** (*++ history: 
  152. **      7 Dec 87    Bruce Eckel    Creation date
  153. ** ++*)
  154. ** 
  155. ** (*++ detailed: 
  156. ** ++*)
  157. */
  158.  
  159. istream & operator>>(istream& s, string& x)
  160. {
  161.   char buf[256];
  162.   s >> buf;
  163.   x = buf;
  164.   cout << "echo: " << x << "\n";
  165.   return s;
  166. }
  167.  
  168.  
  169. /* 
  170. -*++ string::operator[](): index into a string
  171. ** 
  172. ** (*++ history: 
  173. **      7 Dec 87    Bruce Eckel    Creation date
  174. ** ++*)
  175. ** 
  176. ** (*++ detailed: 
  177. ** ++*)
  178. */
  179.  
  180. char & string::operator[](int i)
  181. {
  182.   if (i<0 || strlen(p->s) < i) error("index out of range");
  183.   return p->s[i];
  184. }
  185.  
  186.  
  187. /* 
  188. -*++ string::operator+(): add two string objects
  189. ** 
  190. ** (*++ history: 
  191. **      7 Dec 87    Bruce Eckel    Creation date
  192. ** ++*)
  193. ** 
  194. ** (*++ detailed: 
  195. ** ++*)
  196. */
  197.  
  198. string& operator+(string& x, string& y)
  199. {
  200.   char buf[256];
  201.   strcpy(buf,x.p->s);
  202.   strcat(buf,y.p->s);
  203.   string & tmp =  *new string(buf);
  204.   return tmp;
  205. }
  206.  
  207. #ifdef TEST
  208. main()
  209. {
  210.   string x = "this is string x";
  211.   string y = "this is string y";
  212.   cout << x << y << "\n";
  213.   cout << x + y << "\n";
  214.   string z = "this is string z ";
  215.   string q = "this is string q ";
  216.   cout << z + q << "\n" << q + x << "\n";
  217.  
  218.  
  219.  
  220. /*  string x[100];
  221.   int n;
  222.  
  223.   cout << "here we go\n";
  224.   for (n = 0; cin >> x[n]; n++) {
  225.     string y;
  226.     if (n == 100) error("too many strings");
  227.     cout << (y = x[n]);
  228.     if (y == "done") break;
  229.   }
  230.   cout << "here we go back again\n";
  231.   for (int i=n-1; 0 <= i; i--) cout << x[i];
  232. */
  233. }
  234.  
  235. #endif TEST  /* use test for testing string class, undef otherwise */
  236.