home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / c / WLIB.ZIP / WSTR.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-23  |  11.6 KB  |  293 lines

  1. #ifndef WStrIncluded
  2. #define WStrIncluded
  3.  
  4. // copyright (c) 1992, 1993 by Paul Wheaton
  5. // 1916 Brooks #205, Missoula, MT  59801
  6. //
  7. // voice phone:  (406)543-7543
  8. // modem phone:  (406)543-1144 (2400N81)
  9. //  CompuServe:  72707,207
  10. //    Internet:  72707.207@CompuServe.com
  11.  
  12. #include <string.h>
  13. #include <WMisc.h>
  14.  
  15. #ifdef MAJORBBS
  16.   #include <stdlib.h>
  17. #endif
  18.  
  19. #define DefaultStringExtra    15
  20.   /*  used internally by "String". Since most strings are very small yet
  21.   will have a little bit more added to them, this extra allocation should
  22.   save quite a bit of time.  The only time it would be a hendrance is when
  23.   an array of strings is created */
  24.  
  25. class String;
  26. class StackString;
  27. class String40;
  28. class String120;
  29.  
  30. class BaseString
  31.   {
  32.     protected:
  33.       char* P;   // character string
  34.       int Len;   // Length of string, excluding null character
  35.       int Alloc; // amount of actual storage allocated (including null char)
  36.       friend String;
  37.       friend StackString;
  38.       friend String40;
  39.       friend String120;
  40.     public:
  41.       operator const char*() const {return P;}
  42.       char& operator[](int Index);
  43.       Bool operator<(const BaseString& S) const { return strcmp(P, S.P) < 0; }
  44.       Bool operator>(const BaseString& S) const { return strcmp(P, S.P) > 0; }
  45.       Bool operator<=(const BaseString& S) const { return strcmp(P, S.P) <= 0; }
  46.       Bool operator>=(const BaseString& S) const { return strcmp(P, S.P) >= 0; }
  47.       Bool operator==(const BaseString& S) const;
  48.       Bool operator!=(const BaseString& S) const { return !(*this==S); }
  49.  
  50.       Bool operator<(const char* CS)  const { return strcmp(P,CS) < 0; }
  51.       Bool operator>(const char* CS)  const { return strcmp(P,CS) > 0; }
  52.       Bool operator<=(const char* CS) const { return strcmp(P,CS) <= 0; }
  53.       Bool operator>=(const char* CS) const { return strcmp(P,CS) >= 0; }
  54.       Bool operator==(const char* CS) const { return strcmp(P,CS) == 0; }
  55.       Bool operator!=(const char* CS) const { return strcmp(P,CS) != 0; }
  56.  
  57.       friend Bool operator<(const char* cs, const BaseString& S)
  58.         { return strcmp(cs, (S.P)) < 0; }
  59.       friend Bool operator>(const char* cs, const BaseString& S)
  60.         { return strcmp(cs, (S.P)) > 0; }
  61.       friend Bool operator<=(const char* cs, const BaseString& S)
  62.         { return strcmp(cs, (S.P)) <= 0; }
  63.       friend Bool operator>=(const char* cs, const BaseString& S)
  64.         { return strcmp(cs, (S.P)) >= 0; }
  65.       friend Bool operator==(const char* cs, const BaseString& S)
  66.         { return strcmp(cs, (S.P)) == 0; }
  67.       friend Bool operator!=(const char* cs, const BaseString& S)
  68.         { return strcmp(cs, (S.P)) != 0; }
  69.  
  70.       int Length() const { return Len; }
  71.       void ToLower();  // force all of StackString to lower case
  72.       void ToUpper();  // force all of StackString to upper case
  73.       int Capacity() const { return Alloc - 1; }
  74.       int Size() const { return (Len+2); } // the current size to store
  75.       int Index(char SearchChar, int StartIndex=0) const;
  76.       int Index(const char* SearchStr, int StartIndex=0) const;
  77.       Bool Find(char SearchChar, int StartIndex=0) const
  78.           {return (Index(SearchChar,StartIndex)!=NotFound);}
  79.       Bool Find(const char* SearchStr, int StartIndex=0) const
  80.           {return (Index(SearchStr,StartIndex)!=NotFound);}
  81.       int Count(char C) const;  // how many of this character occur
  82.  
  83.       void Delete(int Index=0,int Length=1);
  84.       void DeleteLast();
  85.       void Trim();  //   chop off leading and trailing spaces
  86.       void TrimLead();
  87.       void TrimTrail();
  88.       void TrimTrailTo(char C);
  89.         // remove last chars until C is found, then remove C too!
  90.  
  91.       char At(int Index) const;
  92.       char operator()(int Index) const {return At(Index);}
  93.       char Last() const; // the last char in the string
  94.       void Clip(int NewLen);  // chop off the right end of the string if there
  95.  
  96.       String40 Word(int Num=0);
  97.         // using spaces as delimiters, pull out word number 0, 1, 2, etc.
  98.       String40 XWord(int Num=0);
  99.         // same as Word except that the Word is extracted from the original string
  100.  
  101.       #ifdef MAJORBBS
  102.         void* operator new(size_t size){return malloc(size);}
  103.         void  operator delete(void* p) {free(p);}
  104.       #endif
  105.   };
  106.  
  107. class String: public BaseString
  108.   {
  109.     protected:
  110.       void ReNew(int NewCapacity);
  111.       void New(); // Uses Alloc
  112.     public:
  113.       String(const char& C, int L=1, int Extra=DefaultStringExtra);
  114.         // String S(' ',20) constructs a string consisting of 20 spaces
  115.       String(int Extra=DefaultStringExtra);
  116.         // constructor based on size desired or size not yet known
  117.       String(const char*, int Extra=DefaultStringExtra);
  118.         // a char* type string is used to construct a String type string
  119.       String(const BaseString&, int Extra=DefaultStringExtra);
  120.         // creating one String from another
  121.       String(const String&);
  122.       #ifdef MAJORBBS
  123.         ~String() {free(P);}
  124.       #else
  125.         ~String() {delete P;}
  126.       #endif
  127.  
  128.       // assignment operators
  129.       void operator=(const BaseString&);
  130.       void operator=(const char*);
  131.       void operator=(const char);
  132.  
  133.       // String concatination (S1=S2+S3)
  134.       // String operator+(const StackString& S) const;
  135.       String operator+(const String40& S) const;
  136.       String operator+(const String120& S) const;
  137.       String operator+(const String&) const;
  138.       String operator+(const char*) const;
  139.       String operator+(char C) const;
  140.       friend String operator+(const String40&, const String&);
  141.       friend String operator+(const String120&, const String&);
  142.       friend String operator+(const char* CS, const String& S);
  143.       friend String operator+(char C, const String& S);
  144.  
  145.       // Appending stuff to a String
  146.       void operator+=(const BaseString&);
  147.       void operator+=(const char*);
  148.       void operator+=(char);
  149.  
  150.       void Left(int NewSize);  //  resize string and left justify text
  151.       void Right(int NewSize);
  152.       void Center(int NewSize);
  153.       void Just(int Type, int NewSize);
  154.         //  parses out to Left, Right or Center
  155.       void Tail(int NewSize); // works like "Right" cept left chars may be chopped
  156.  
  157.       int ReAlloc(int NewCapacity);
  158.       String At(int Index, int Length) const;
  159.       String operator()(int Index, int Length) const {return At(Index,Length);}
  160.       char   At(int Index) const {return BaseString::At(Index);}
  161.       char   operator()(int Index) const {return BaseString::At(Index);}
  162.       String Before(int Index) const {return At(0,Index);}
  163.       String Through(int Index) const {return At(0,Index+1);}
  164.       String From(int Index) const {return At(Index,Len-Index);}
  165.       String After(int Index) const {return At(Index+1,Len-Index-1);}
  166.  
  167.       void Insert(char C,int Index=0);
  168.       void Insert(const char* St,int Index=0);
  169.       void Replace(const char* SearchStr, const char* ReplaceStr);
  170.       void Replace(const char SearchChar, const char* ReplaceStr);
  171.       void Replace(const char* SearchStr, const char ReplaceChar);
  172.       void Replace(const char SearchChar, const char ReplaceChar);
  173.   };
  174.  
  175. class StackString: public BaseString
  176.   {
  177.     protected:
  178.       friend String;
  179.     public:
  180.  
  181.       void operator=(const BaseString&);
  182.       void operator=(const char*);
  183.       void operator=(const char);
  184.  
  185.       void operator+=(const BaseString&);
  186.       void operator+=(const char*);
  187.       void operator+=(char);
  188.  
  189.       void Left(int NewSize);  //  resize string and left justify text
  190.       void Right(int NewSize);
  191.       void Center(int NewSize);
  192.       void Just(int Type, int NewSize);
  193.       void Tail(int NewSize); // works like "Right" cept left chars may be chopped
  194.  
  195.       void Sub(StackString& D, int Index, int Length);
  196.  
  197.       void Insert(char C,int Index=0);
  198.       void Insert(const char* St,int Index=0);
  199.   };
  200.  
  201. class String40: public StackString
  202.   {
  203.     protected:
  204.       char Buf[41];
  205.       friend String;
  206.     public:
  207.       String40();
  208.       String40(const char& C, int L=1);
  209.       String40(const char*);
  210.       String40(const BaseString&);
  211.       String40(const String40&);
  212.  
  213.       String40 operator+(const String40& S) const;
  214.       String40 operator+(const char* S) const;
  215.       String40 operator+(char C) const;
  216.       friend String40 operator+(const char* S1, const String40& S2);
  217.       friend String40 operator+(char C, const String40& S);
  218.  
  219.       String40 At(int Index, int Length) const;
  220.       String40 operator()(int Index, int Length) const {return At(Index,Length);}
  221.       char   At(int Index) const {return BaseString::At(Index);}
  222.       char   operator()(int Index) const {return BaseString::At(Index);}
  223.       String40 Before(int Index) const {return At(0,Index);}
  224.       String40 Through(int Index) const {return At(0,Index+1);}
  225.       String40 From(int Index) const {return At(Index,Len-Index);}
  226.       String40 After(int Index) const {return At(Index+1,Len-Index-1);}
  227.       void Replace(const char* SearchStr, const char* ReplaceStr);
  228.       void Replace(const char SearchChar, const char* ReplaceStr);
  229.       void Replace(const char* SearchStr, const char ReplaceChar);
  230.       void Replace(const char SearchChar, const char ReplaceChar);
  231.   };
  232.  
  233. class String120: public StackString
  234.   {
  235.     protected:
  236.       char Buf[121];
  237.       friend String;
  238.     public:
  239.       String120();
  240.       String120(const char& C, int L=1);
  241.       String120(const char*);
  242.       String120(const BaseString&);
  243.       String120(const String120&);
  244.  
  245.       String120 operator+(const String40& S) const;
  246.       String120 operator+(const String120& S) const;
  247.       String120 operator+(const char* S) const;
  248.       String120 operator+(char C) const;
  249.       friend String120 operator+(const String40& S1, const String120& S2);
  250.       friend String120 operator+(const char* S1, const String120& S2);
  251.       friend String120 operator+(char C, const String120& S);
  252.  
  253.       String120 At(int Index, int Length) const;
  254.       String120 operator()(int Index, int Length) const {return At(Index,Length);}
  255.       char   At(int Index) const {return BaseString::At(Index);}
  256.       char   operator()(int Index) const {return BaseString::At(Index);}
  257.       String120 Before(int Index) const {return At(0,Index);}
  258.       String120 Through(int Index) const {return At(0,Index+1);}
  259.       String120 From(int Index) const {return At(Index,Len-Index);}
  260.       String120 After(int Index) const {return At(Index+1,Len-Index-1);}
  261.       void Replace(const char* SearchStr, const char* ReplaceStr);
  262.       void Replace(const char SearchChar, const char* ReplaceStr);
  263.       void Replace(const char* SearchStr, const char ReplaceChar);
  264.       void Replace(const char SearchChar, const char ReplaceChar);
  265.   };
  266.  
  267. String Str(long);  //  convert integers to plain, unformatted strings
  268. String40 CommaStr(long);
  269. String DStr(double Num); // very slow yet works outside of SLong range
  270. String HexStr(Long Num);
  271. String OctalStr(Long Num);
  272. String Form(const char* Format,double Val);
  273. String Form(int FLen,SLong Val);
  274.   // will someday be much faster than using double
  275. String Form(int FLen,const double& Val);
  276. String Form(int FLen,const Long& Val);
  277. String Form(int FLen,const float& Val);
  278. inline String Form(int FLen,const int& Val){return Form(FLen,double(Val));}
  279.  
  280. String StringOf(int HowMany, char What);
  281. String Spaces(int HowMany);
  282.  
  283. String LeftText(const char* CS, int NewSize);
  284. String RightText(const char* CS, int NewSize);
  285. String CenterText(const char* CS, int NewSize);
  286. String JustText(const char* CS, int Type, int NewSize);
  287. String TailText(const char* CS, int NewSize);
  288. String Trim(const char* S);
  289. String TrimLead(const char* S);
  290. String TrimTrail(const char* S);
  291.  
  292. #endif
  293.