home *** CD-ROM | disk | FTP | other *** search
/ Resource for Source: C/C++ / Resource for Source - C-C++.iso / misc_src / cslib16b / include / csedstr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-01  |  8.2 KB  |  272 lines

  1. /***********************************************************************
  2.  
  3.                                        CSA Library, Version 1.6.b 
  4.                                          Released: March 2nd 1995 
  5.  
  6.        Elaborate string class.
  7.        Contains functions to edit a string.
  8.        (Assuming a non-graphical screen.)
  9.  
  10.                                            Copyright(c) 1994,1995 
  11.                                                            Combis 
  12.                                                   The Netherlands 
  13. ***********************************************************************/
  14.  
  15. #ifndef __CSEDSTR_H
  16. #define __CSEDSTR_H
  17.  
  18.  
  19. #ifndef __STDLIB_H
  20. #include "stdlib.h"
  21. #endif
  22.  
  23. #ifndef __STRING_H
  24. #include "string.h"
  25. #endif
  26.  
  27. #ifndef __CSTOOLS_H
  28. #include "cstools.h"
  29. #endif
  30.  
  31.  
  32. ////////////////////////////////////////////////////////////////
  33. ////////////////////////////////////////////////////////////////
  34.  
  35. class STR
  36. {
  37. protected:
  38.  
  39.   CSCHAR *s;
  40.   int al_len;          // Number of bytes allocated (including the 0 )
  41.  
  42. public:
  43.   int length(void)      { return strlen(s); }
  44.  
  45. //////////////////// String manipulation ///////////////////////
  46.  
  47.    void trim(void)        { trim_string(s); }
  48.    void upper(void)        { strupr(s);      }
  49.    void lower(void)        { strlwr(s);      }
  50.    void strip(CSCHAR *f)    { str_strip(s,f); }
  51.    void filter(CSCHAR *f)   { filter_string(s,f); }
  52.    void replace_one(CSCHAR *d,CSCHAR *r) { string_replace_ones(s,d,r); }
  53.    int    replace_all(CSCHAR *d,CSCHAR *r) { return string_replace(s,d,r); }
  54.    CSCHAR *find_sub(CSCHAR *sub)
  55.     {
  56.        return strstr((CSCHAR *)s,(CSCHAR *)sub);
  57.     }
  58.    int    find_pos(CSCHAR *sub)
  59.     {
  60.        CSCHAR *p=strstr((CSCHAR *)s,(CSCHAR *)sub);
  61.        return (p==NULL) ? -1: (int)(p-s);
  62.     }
  63.  
  64. ///////////////////// Constructors  ///////////////////////////
  65.    STR( STR&);
  66.    STR(uchar *);
  67.    STR(CSCHAR *);
  68.    STR(int len);
  69.    STR(void);
  70.  
  71.  
  72. ///////////////////// Destructor  /////////////////////////////
  73.    ~STR(void);
  74.  
  75.  
  76. ////////////////////// Operator overloading /////////////////////
  77.    friend STR operator+( STR &s1, STR &s2);
  78.  
  79.    STR operator+( STR &s1);
  80.  
  81.    CSCHAR & operator[](int i);
  82.  
  83.    int operator==( STR &str)  { return !stricmp(str.s,s); }
  84.    int operator<( STR &str)   { return (stricmp(s,str.s)<0); }
  85.    int operator<=( STR &str)  { return (stricmp(s,str.s)<=0); }
  86.    int operator>( STR &str)   { return (stricmp(s,str.s)>0); }
  87.    int operator>=( STR &str)  { return (stricmp(s,str.s)>=0); }
  88.  
  89.    STR&  operator+=( STR &);
  90.    STR&  operator+=( CSCHAR *);
  91.    STR&  operator=(uchar *);
  92.    STR&  operator=(CSCHAR *p)  { return operator=((uchar *)p); }
  93.    STR&  operator=(uchar &);
  94.    STR&  operator=(int &);
  95.    STR&  operator=(long &);
  96.    STR&  operator=(float &);
  97.    STR&  operator=(double &);
  98.    STR&  operator=( STR &);
  99.  
  100.  
  101. ////////////////////// Type casting  /////////////////////////////
  102.  
  103.    operator uchar*()   { return (uchar *)s; }
  104.    operator CSCHAR*()  { return (CSCHAR *)s; }
  105.    operator uchar()    { return *s; }
  106.    operator int()      { return atoi(s); }
  107.    operator long()     { return atol(s); }
  108.    operator float()    { return (float)atof(s); }
  109.    operator double()   { return atof(s); }
  110.  
  111.  
  112.  
  113. ////////////////////// Allocation ///////////////////////////////
  114.  
  115. protected:
  116.     void m_alloc(int n);      // Allocating
  117.     void m_free(void);          // Freeing allocated memory
  118.  
  119. public:
  120.     void alloc_min(void);     // Minimizes the amount of allocated
  121.                   // RAM (with respect to the string length).
  122.                   // No data is lost.
  123.     void alloc_adjust(int l); // Adjust the allocated amount of
  124.                   // RAM to amount l, or to the (string length +1)
  125.                   // whatever is more.
  126.                   // No data is lost.
  127.     void alloc_new(int n);    // Throws away the old string and the
  128.                   // accompaning allocated memory.
  129.                   // And allocates n new bytes without
  130.                   // initializing!
  131.                   // ALL DATA IS LOST.
  132.     void alloc_max(int n);    // Allocates n bytes if not already
  133.                   // available.
  134.                   // No data is lost.
  135.     void alloc_new_max(int n);// Allocates n bytes if not already
  136.                   // available.
  137.                   // ALL DATA IS LOST.
  138.  
  139. };
  140.  
  141.  
  142.  
  143. ////////////////////////////////////////////////////////////////
  144. ////////////////////////////////////////////////////////////////
  145.  
  146.  
  147. class EDSTR: public STR
  148. {
  149. protected:
  150.   int vertical_exit;
  151.   uchar *legs;          // Allowable CSCHARacters
  152.   int esc_allow;
  153.   int change;
  154.   int maxlen;          // Max allowable length during editing
  155.   int displen;          // Max number of uchar displayed during editing.
  156.   int exitkey;
  157.  
  158. private:
  159.  
  160. //////////////////// Template related /////////////////////////////////
  161.  
  162.   int go_left(int &l_pos,int &pos, uchar * tpt);
  163.   int go_right(int &l_pos,int &pos, uchar * tpt);
  164.   int go_home(int &l_pos,int &pos, uchar * tpt);
  165.   int go_end(int &l_pos,int &pos, uchar * tpt);
  166.   int go_bcks(int &l_pos,int &pos, uchar * tpt);
  167.   int go_del(int &l_pos,int &pos, uchar * tpt);
  168.   int ed_tpt(int &l_pos,int &pos,uchar *temp,uchar *eX_tpt,int TF);
  169.  
  170. ////////////////////////////////////////////////////////////////////////
  171.  
  172. protected:
  173.   int editstring(int &insert,int &l_pos,int &pos,uchar *l,int disp_len,int max_len,int edit_allow);
  174.   int edit(int &insert,int &l_pos,int &pos);
  175.   int browse(int &insert,int &l_pos,int &pos);
  176.   void v_exit(int t)      { vertical_exit=t; }
  177.  
  178.   void init(void);
  179.  
  180. public:
  181.  
  182. ///////////////////// Constructores  /////////////////////
  183.  
  184.  
  185.    EDSTR( EDSTR&);
  186.    EDSTR(uchar *);
  187.    EDSTR(int len);
  188.    EDSTR(void);
  189.  
  190.    ~EDSTR(void);
  191.  
  192.   void change_reset(void) { change=FALSE; }
  193.   void show(void);
  194.  
  195.   int ed_template(int &l_pos,int &pos,uchar *tpt);
  196.  
  197. //////////////////// String editing ////////////////////////////
  198.  
  199.  
  200.    void legal(uchar *s)  { legs=s;     }
  201.    void legal(CSCHAR *s) { legal((uchar *)s); }
  202.    void max_len(int l)     { maxlen=l;   }
  203.    void disp_len(int l)  { displen=l;  }
  204.    int    disp_len(void)     { return displen; }
  205.    void exit_key(int ke) { exitkey=ke; }
  206.    int edit(void) ;
  207.    int edit(uchar *l)     { legal(l); return edit(); }
  208.    int edit(uchar *l, int dl,int ml)
  209.              { legal(l); disp_len(dl); max_len(ml); return edit(); }
  210.    int edit(int dl,int ml)
  211.              { disp_len(dl); max_len(ml); return edit(); }
  212.    int browse(void) ;
  213.    int browse(int dl,int ml)
  214.              { disp_len(dl); max_len(ml); return browse(); }
  215.    int changed(void)     { return change; }
  216.    void escape_on(void)  { esc_allow=TRUE; }
  217.    void escape_off(void) { esc_allow=FALSE; }
  218.    void escape(int tf)     { esc_allow=tf; }
  219.  
  220. //////////////////// Converting from EDSTR ////////////////////////
  221.    int      convert(uchar *);
  222.    int      convert(CSCHAR *s) { return convert((uchar *)s); }
  223.    int      convert(uchar &);
  224.    int      convert(CSCHAR &c) { return convert((uchar)c); }
  225.    int      convert(int &);
  226.    int      convert(float &);
  227.    int      convert(long &);
  228.    int      convert(double &);
  229.  
  230.  
  231. ////////////////////// Operator overloading /////////////////////
  232.    friend EDSTR operator+( EDSTR &s1, EDSTR &s2);
  233.    EDSTR operator+( EDSTR &s1);
  234.  
  235.  
  236.    CSCHAR & operator[](int i)    { return s[i];            }
  237.    int operator==( EDSTR &str)    { return !stricmp(str.s,s);    }
  238.    int operator<( EDSTR &str)    { return (stricmp(s,str.s)<0);    }
  239.    int operator<=( EDSTR &str)    { return (stricmp(s,str.s)<=0); }
  240.    int operator>( EDSTR &str)    { return (stricmp(s,str.s)>0);    }
  241.    int operator>=( EDSTR &str)    { return (stricmp(s,str.s)>=0); }
  242.  
  243.    EDSTR&  operator+=(EDSTR &);
  244.    EDSTR&  operator+=(CSCHAR *);
  245.    EDSTR&  operator=(uchar *);
  246.    EDSTR&  operator=(CSCHAR *s) { return operator=((uchar *)s); }
  247.    EDSTR&  operator=(uchar &);
  248.    EDSTR&  operator=(CSCHAR &s) { return operator=((uchar)s); }
  249.    EDSTR&  operator=(int &);
  250.    EDSTR&  operator=(long &);
  251.    EDSTR&  operator=(float &);
  252.    EDSTR&  operator=(double &);
  253.    EDSTR&  operator=(EDSTR &);
  254.  
  255.  
  256. ////////////////////// Type casting  /////////////////////////////
  257.    operator uchar*()   { return (uchar *)s; }
  258.    operator CSCHAR*()  { return (CSCHAR *)s; }
  259.    operator uchar()    { return (uchar)*s; }
  260.    operator CSCHAR()   { return (CSCHAR)*s; }
  261.    operator int()      { return atoi(s); }
  262.    operator long()     { return atol(s); }
  263.    operator float()    { return (float)atof(s); }
  264.    operator double()   { return atof(s); }
  265.  
  266.  
  267.  
  268. };
  269.  
  270.  
  271. #endif
  272.