home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ctdemo.zip / classes / @CLSLIB1.ZIP / cls / tutorial / string.cpp < prev   
C/C++ Source or Header  |  1995-03-30  |  5KB  |  259 lines

  1. #ifndef INC_STRING
  2. #define INC_STRING
  3.  
  4. #ifndef __FIRST__
  5. #define __FIRST__
  6. #define __IMPL__STRING
  7. #endif
  8.  
  9. /////V String PCM f:\cls_ibm\cls\tutorial 10  PM 30.03.95 10:25:25
  10. /*
  11. /////H
  12. 20.10.94 19:37 PM 0 copied from: String TOS f:\cls_ibm\cls\tutorial 0 PM 19.10.94 00:26:00
  13. 30.03.95 10:34 PM 10 archived: String PCM f:\cls_ibm\cls\tutorial 10  PM 30.03.95 10:25:25
  14. /////
  15. */
  16.  
  17. /////1
  18. #undef inline
  19.  
  20. #include <bsa.h>
  21.  
  22. /////I IOSTREAM.H @ @ @ @ pre 
  23. #include <IOSTREAM.H>
  24.  
  25. /////I STRING.H @ @ @ @ pre 
  26. #include <STRING.H>
  27.  
  28. /////I stdlib.h @ @ @ @ pre 
  29. #include <stdlib.h>
  30.  
  31.  
  32. #ifndef __INLINE__
  33. #define inline
  34. #endif
  35.  
  36. /////C String @ @ app level0 
  37. class String
  38.  
  39. {
  40. /////N >> @ >>
  41. friend istream & operator >> (istream & s, String & x);
  42.  
  43. /////N << @ <<
  44. friend ostream & operator << ( ostream  & s, const String & x );
  45.  
  46. /////N != @ !=
  47. friend int operator != ( const String & x, const String & y );
  48.  
  49. /////N != @ !=
  50. friend int operator != ( const String & x, const char * s );
  51.  
  52. /////N == @ ==
  53. friend int operator == ( const String & x, const String & y );
  54.  
  55. /////N == @ ==
  56. friend int operator == ( const String & x, const char * s );
  57.  
  58. /////T struct srep inside 
  59. struct srep {
  60.     char *    s;    // pointer to data
  61.     int         n;    // reference count
  62.     srep() { n = 1; }
  63. };
  64. /////
  65.  
  66.  
  67. private:
  68. /////D P @  @ instance private 
  69.     srep *  P;
  70.  
  71.  
  72. public:
  73.     String & operator   = (const String&);
  74.     String & operator   = (const char*);
  75.     String ();
  76.     String (const char*);
  77.     String (const String&);
  78.     char & operator   [] (int);
  79.     virtual ~String ();
  80. };
  81.  
  82.     inline int operator   != (const String&,const char*);
  83.     inline int operator   != (const String&,const String&);
  84.     ostream & operator   << (ostream&,const String&);
  85.     inline int operator   == (const String&,const char*);
  86.     inline int operator   == (const String&,const String&);
  87.     istream & operator   >> (istream&,String&);
  88.     void   error (const char*);
  89.  
  90. /////2
  91. #undef inline
  92.  
  93.  
  94. #if (defined __INLINE__) || (defined __IMPL__STRING)
  95.  
  96. #ifndef __INLINE__
  97. #define inline
  98. #endif
  99.  
  100. /////F != @ @ global inline 
  101. inline
  102. int operator != ( const String & x, const String & y )
  103. {
  104.     return strcmp(x.P->s, y.P->s) != 0;
  105. }
  106.  
  107. /////F != @ @ global inline 
  108. inline
  109. int operator != ( const String & x, const char * s )
  110. {
  111.     return strcmp(x.P->s, s) != 0;
  112. }
  113.  
  114. /////F == @ @ global inline 
  115. inline
  116. int operator == ( const String & x, const String & y )
  117. {
  118.     return strcmp(x.P->s, y.P->s) == 0;
  119. }
  120.  
  121. /////F == @ @ global inline 
  122. inline
  123. int operator == ( const String & x, const char * s )
  124. {
  125.     return strcmp(x.P->s, s) == 0;
  126. }
  127.  
  128. /////
  129. #endif
  130.  
  131. /////3
  132. #undef inline
  133.  
  134. #ifdef __IMPL__STRING
  135. /////F << @ @ global 
  136. ostream & operator << ( ostream  & s, const String & x )
  137. {
  138. //    return s << x.P->s << " [" << x.P->n << "] \n";
  139.     return s << x.P->s;
  140. }
  141.  
  142. /////F = @ @ instance public 
  143. String & String:: operator = ( const char * s )
  144. {
  145.     if (P->n > 1) {                        // dissconnect self
  146.         P->n--;
  147.         P = new srep;
  148.     }
  149.     else
  150.         delete[] P->s;                    // free old string
  151.         
  152.     P->s = new char[ strlen(s) + 1 ];
  153.     strcpy(P->s, s);
  154.  
  155.     return * this;
  156. }
  157.  
  158. /////F = @ @ instance public 
  159. String & String:: operator = ( const String & x )
  160. {
  161.     x.P->n++;    // protecct against "st = st "
  162.     if (--P->n == 0) {
  163.         delete[] P->s,
  164.         delete P;
  165.     }
  166.     P = x.P;
  167.     return * this;
  168. }
  169.  
  170. /////F >> @ @ global 
  171. istream & operator >> (istream & s, String & x)
  172. {
  173.     char buf[256];
  174.     s >> buf; // unsafe, might overflow
  175.     x = buf;
  176.     cout << "echo: " << x << "\n";
  177.     return s;
  178. }
  179.  
  180. /////F error @ @ global 
  181. void error ( const char * p )
  182. {
  183.     cerr << p << "\n";
  184.         exit(1);
  185. }
  186.  
  187. /////
  188. #ifdef __APPCLASS__
  189. /////F main @ @ global 
  190. int main ()
  191. {
  192.     String x[5];
  193.     int n;
  194.     
  195.     cout << "here we go (type in a few chars, 'done' to terminate)\n";
  196.     for (n = 0; cin >> x[n]; n++) {
  197.         if (n==4) {
  198.             error("too many strings");
  199.             return 99;
  200.         }
  201.         String y;
  202.         y = x[n];
  203.         cout << y << '\n';
  204.         if (y=="done")
  205.             break;
  206.         cout << "next string please\n";
  207.     }
  208.     cout << "\nhere we go back again\n";
  209.     for (int i=n-1; 0<=i; i--)
  210.         cout << x[i] << '\n';
  211.     return 0;
  212. }
  213. /////
  214. #endif
  215.  
  216. /////F String @ @ instance public 
  217. String:: String (const String & x)
  218. {
  219.     x.P->n++;
  220.     P = x.P;
  221. }
  222.  
  223. /////F String @ @ instance public 
  224. String:: String (const char * s)
  225. {
  226.     P = new srep;
  227.     P->s = new char[ strlen(s)+1 ];
  228.     strcpy(P->s, s);
  229. }
  230.  
  231. /////F String @ @ public instance docu 
  232. String:: String ()
  233. {
  234.     P = new srep;
  235.     P->s = 0;
  236. }
  237.  
  238. /////F [] @ @ instance public 
  239. char & String:: operator [] ( int i )
  240. {
  241.     if (i<0 || strlen(P->s)<i)
  242.         error ("index out of range");
  243.     return P->s[i];
  244. }
  245.  
  246. /////F ~String @ @ terminal public instance 
  247. String:: ~String ()
  248. {
  249.     if (--P->n == 0) {
  250.         delete[] P->s;
  251.         delete P;
  252.     }
  253. }
  254.  
  255. /////
  256. #endif
  257.  
  258. #endif
  259.