home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cslio205.zip / INCLUDE / CSSTR.H < prev    next >
C/C++ Source or Header  |  1997-01-21  |  5KB  |  146 lines

  1. /***********************************************************************
  2.  
  3.                        CSA Library, Free Evaluation Version 2.0.5 
  4.                                        Release: January 22th 1997 
  5.  
  6.        String class.
  7.  
  8.                                            Copyright(c) 1994-1997 
  9.                                                           ComBits 
  10.                                                   The Netherlands 
  11. ***********************************************************************/
  12.  
  13. #ifndef __csSTR_H
  14. #define __csSTR_H
  15.  
  16.  
  17. #include "stdlib.h"
  18. #include "string.h"
  19. #include "cstools.h"
  20.  
  21.  
  22. ////////////////////////////////////////////////////////////////
  23. ////////////////////////////////////////////////////////////////
  24. // It is valid to manipulate the string by optaining a pointer
  25. // to the allocated space. E.g.: you can write a '0' (a zero)  somewhere
  26. // in the string to make it shorter. The length() function and the
  27. // allocation functions won't go astray.
  28. //
  29. // Of course, you cannot extend string in this way.
  30.  
  31. class csSTR
  32. {
  33. protected:
  34.  
  35.    csCHAR *s;
  36.    int al_len;             // Number of bytes allocated (including the 0 )
  37.  
  38. public:
  39.    int  length(void)        { return strlen(s); }
  40.    int  xstricmp(csCHAR *s1,csCHAR *s2);
  41.  
  42. //////////////////// String manipulation ///////////////////////
  43.  
  44.    void trim(void)          { trim_string(s);     }
  45.    void upper(void)         { str_upper(s);       }
  46.    void lower(void)         { str_lower(s);       }
  47.    void strip(csCHAR *f)    { str_strip(s,f);     }
  48.    void filter(csCHAR *f)   { filter_string(s,f); }
  49.    void replace_one(csCHAR *d,csCHAR *r) { string_replace_ones(s,d,r); }
  50.    int  replace_all(csCHAR *d,csCHAR *r) { return string_replace(s,d,r); }
  51.    csCHAR *find_sub(csCHAR *sub)
  52.         {
  53.            return strstr((csCHAR *)s,(csCHAR *)sub);
  54.         }
  55.    int  find_pos(csCHAR *sub)
  56.         {
  57.            csCHAR *p=strstr((csCHAR *)s,(csCHAR *)sub);
  58.            return (p==NULL) ? -1: (int)(p-s);
  59.         }
  60.  
  61. ///////////////////// Constructors  ///////////////////////////
  62.    csSTR( csSTR&);
  63.    csSTR(uchar *);
  64.    csSTR(csCHAR *);
  65.    csSTR(int len);
  66.    csSTR(void);
  67.  
  68.  
  69. ///////////////////// Destructor  /////////////////////////////
  70.    virtual ~csSTR(void);
  71.  
  72.  
  73. ////////////////////// Operator overloading /////////////////////
  74.    friend csSTR operator+( csSTR &s1, csSTR &s2);
  75.  
  76.    csSTR operator+( csSTR &s1);
  77.  
  78.    csCHAR & operator[](int i);  // First byte is at position 0.
  79.  
  80.    int operator!=(csCHAR *str){ return xstricmp(str,s);   }
  81.    int operator!=( csSTR &str)  { return xstricmp(str.s,s); }
  82.    int operator==(csCHAR *str){ return !xstricmp(str,s);   }
  83.    int operator==( csSTR &str)  { return !xstricmp(str.s,s); }
  84.    int operator<( csSTR &str)   { return (xstricmp(s,str.s)<0); }
  85.    int operator<=( csSTR &str)  { return (xstricmp(s,str.s)<=0); }
  86.    int operator>( csSTR &str)   { return (xstricmp(s,str.s)>0); }
  87.    int operator>=( csSTR &str)  { return (xstricmp(s,str.s)>=0); }
  88.  
  89.  
  90.  
  91.    csSTR&  operator+=( csSTR &);
  92.    csSTR&  operator+=( csCHAR *);
  93.    csSTR&  operator+=( csCHAR c);
  94.    csSTR&  operator=(uchar *);
  95.    csSTR&  operator=(csCHAR *p)  { return operator=((uchar *)p); }
  96.    csSTR&  operator=(uchar &);
  97.    csSTR&  operator=(int &);
  98.    csSTR&  operator=(long &);
  99.    csSTR&  operator=(float &);
  100.    csSTR&  operator=(double &);
  101.    csSTR&  operator=( csSTR &);
  102.  
  103.  
  104. ////////////////////// Type casting  /////////////////////////////
  105.  
  106.    operator uchar*()   { return (uchar *)s; }
  107.    operator csCHAR*()  { return (csCHAR *)s; }
  108.    operator uchar()    { return *s; }
  109.    operator int()      { return atoi(s); }
  110.    operator long()     { return atol(s); }
  111.    operator float()    { return (float)atof(s); }
  112.    operator double()   { return atof(s); }
  113.  
  114.  
  115.  
  116. ////////////////////// Allocation ///////////////////////////////
  117.  
  118. protected:
  119.     void m_alloc(int n);      // Allocating
  120.     void m_free(void);        // Freeing allocated memory
  121.  
  122. public:
  123.     void alloc_min(void);     // Minimizes the amount of allocated
  124.                               // RAM (with respect to the string length).
  125.                               // No data is lost.
  126.     void alloc_adjust(int l); // Adjust the allocated amount of
  127.                               // RAM to amount l, or to the (string length +1)
  128.                               // whatever is more.
  129.                               // No data is lost.
  130.     void alloc_new(int n);    // Throws away the old string and the
  131.                               // allocated memory.
  132.                               // Allocates n new bytes without
  133.                               // initializing!
  134.                               // ALL DATA IS LOST.
  135.     void alloc_max(int n);    // Allocates n bytes if not already
  136.                               // available.
  137.                               // No data is lost.
  138.     void alloc_new_max(int n);// Allocates n bytes if not already
  139.                               // available.
  140.                               // ALL DATA IS LOST.
  141.  
  142. };
  143.  
  144.  
  145. #endif
  146.