home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWLINC.PAK / VALIDATE.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  6KB  |  242 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //----------------------------------------------------------------------------
  6. #if !defined(OWL_VALIDATE_H)
  7. #define OWL_VALIDATE_H
  8.  
  9. #if !defined(OWL_WINDOW_H)
  10. # include <owl/window.h>
  11. #endif
  12. #if !defined(CLASSLIB_ARRAYS_H)
  13. # include <classlib/arrays.h>
  14. #endif
  15. #if !defined(OWL_BITSET_H)
  16. # include <owl/bitset.h>
  17. #endif
  18. #include <owl/validate.rh>
  19.  
  20. //
  21. // Validator option flags
  22. //
  23. enum TValidatorOptions {
  24.   voFill     =  0x0001,   // option to fill in chars on IsValidInput checks
  25.   voTransfer =  0x0002,   // option to perform conversion & transfer
  26.   voOnAppend =  0x0004,   // option to only validate input on appending
  27.   voReserved =  0x00F8    // reserved for future use
  28. };
  29.  
  30. //
  31. // class TValidator
  32. // ----- ----------
  33. //
  34. class _OWLCLASS TValidator : public TStreamableBase {
  35.   public:
  36.     class _OWLCLASS_RTL TXValidator : public TXOwl {
  37.       public:
  38.         TXValidator(uint resId = IDS_VALIDATORSYNTAX);
  39.     };
  40.  
  41.     TValidator();
  42.     virtual ~TValidator();
  43.  
  44.     virtual void Error();
  45.     virtual bool IsValidInput(char far* str, bool suppressFill);
  46.     virtual bool IsValid(const char far* str);
  47.     virtual uint Transfer(char far* str, void* buffer, TTransferDirection direction);
  48.  
  49.     // Checks input against validator for completeness. Never modifies input.
  50.     // Calls error if not valid.
  51.     //
  52.     bool         Valid(const char far* str)
  53.                   {if (!IsValid(str)) {Error(); return false;} return true;}
  54.  
  55.     bool         HasOption(int option) {return Options & option;}
  56.     void         SetOption(int option) {Options |= uint16(option);}
  57.     void         UnsetOption(int option) {Options &= uint16(~option);}
  58.  
  59.   protected:
  60.     uint16   Options;
  61.     
  62.   DECLARE_STREAMABLE(_OWLCLASS, TValidator, 1);
  63. };
  64.  
  65. //
  66. // TPXPictureValidator result type
  67. //
  68. enum TPicResult {
  69.   prComplete,
  70.   prIncomplete,
  71.   prEmpty,
  72.   prError,
  73.   prSyntax,
  74.   prAmbiguous,
  75.   prIncompNoFill
  76. };
  77.  
  78. //
  79. // class TPXPictureValidator
  80. // ----- -------------------
  81. //
  82. class _OWLCLASS TPXPictureValidator : public TValidator {
  83.   public:
  84.     TPXPictureValidator(const char far* pic, bool autoFill=false);
  85.  
  86.     //
  87.     // override TValidator's virtuals
  88.     //
  89.     void         Error();
  90.     bool         IsValidInput(char far* str, bool suppressFill);
  91.     bool         IsValid(const char far* str);
  92.  
  93.     virtual TPicResult Picture(char far* input, bool autoFill=false);
  94.  
  95.   protected:
  96.     string       Pic;
  97.  
  98.   private:
  99.     bool         IsComplete(TPicResult rslt);
  100.     bool         IsIncomplete(TPicResult rslt);
  101.     void         ToGroupEnd(uint termCh, uint& i);
  102.     bool         SkipToComma(uint termCh, uint& i);
  103.     uint         CalcTerm(uint termCh, uint i);
  104.     TPicResult   Iteration(char far* input, uint termCh, uint& i, uint& j);
  105.     TPicResult   Group(char far* input, uint termCh, uint& i, uint& j);
  106.     TPicResult   CheckComplete(uint termCh, uint& i, TPicResult rslt);
  107.  
  108.     TPicResult   Scan(char far* input, uint termCh, uint& i, uint& j);
  109.     TPicResult   Process(char far* input, uint termCh, uint& i, uint& j);
  110.     bool         SyntaxCheck();
  111.  
  112.   DECLARE_STREAMABLE(_OWLCLASS, TPXPictureValidator, 1);
  113. };
  114.  
  115. //
  116. // class TFilterValidator
  117. // ----- ----------------
  118. //
  119. class _OWLCLASS TFilterValidator : public TValidator {
  120.   public:
  121.     TFilterValidator(const TCharSet& validChars);
  122.  
  123.     //
  124.     // override TValidator's virtuals
  125.     //
  126.     void         Error();
  127.     bool         IsValid(const char far* str);
  128.     bool         IsValidInput(char far* str, bool suppressFill);
  129.  
  130.   protected:
  131.     TCharSet     ValidChars;
  132.  
  133.   DECLARE_STREAMABLE(_OWLCLASS, TFilterValidator, 1);
  134. };
  135.  
  136. //
  137. // class TRangeValidator
  138. // ----- ---------------
  139. //
  140. class _OWLCLASS TRangeValidator : public TFilterValidator {
  141.   public:
  142.     TRangeValidator(long min, long max);
  143.  
  144.     //
  145.     // override TValidator's virtuals
  146.     //
  147.     void         Error();
  148.     bool         IsValid(const char far* str);
  149.     uint         Transfer(char far* str, void* buffer, TTransferDirection direction);
  150.  
  151.   protected:
  152.     long   Min;
  153.     long   Max;
  154.  
  155.   DECLARE_STREAMABLE(_OWLCLASS, TRangeValidator, 1);
  156. };
  157.  
  158. //
  159. // class TLookupValidator
  160. // ----- ----------------
  161. //
  162. class _OWLCLASS TLookupValidator : public TValidator {
  163.   public:
  164.     TLookupValidator();
  165.  
  166.     //
  167.     // override TValidator's virtuals
  168.     //
  169.     bool         IsValid(const char far* str);
  170.  
  171.     // virtual lookup of a string
  172.     virtual bool Lookup(const char far* str);
  173.  
  174.   DECLARE_STREAMABLE(_OWLCLASS, TLookupValidator, 1);
  175. };
  176.  
  177. //
  178. // class TSortedStringArray
  179. // ----- ------------------
  180. //
  181. class _OWLCLASS TSortedStringArray
  182. {
  183.   public:
  184.     typedef void (*IterFunc)(string&, void*);
  185.     typedef int  (*CondFunc)(const string&, void*);
  186.  
  187.     TSortedStringArray(int upper, int lower, int delta);
  188.     int LowerBound() const;
  189.     int UpperBound() const;
  190.     unsigned ArraySize() const;
  191.     int IsFull() const;
  192.     int IsEmpty() const;
  193.     unsigned GetItemsInContainer() const;
  194.     int Add(const string& t);
  195.     int Detach(const string& t);
  196.     int Detach(int loc);
  197.     int Destroy(const string& t);
  198.     int Destroy(int loc);
  199.     int HasMember(const string& t) const;
  200.     int Find(const string& t) const;
  201.     string& operator [](int loc);
  202.     string& operator [](int loc) const;
  203.     void ForEach(IterFunc iter, void* args);
  204.     string* FirstThat(CondFunc cond, void* args) const;
  205.     string* LastThat(CondFunc cond, void* args) const;
  206.     void Flush();
  207.  
  208.   private:
  209.     TSArrayAsVector<string> Data;
  210.  
  211.   friend class TSortedStringArrayIterator;
  212. };
  213.  
  214. //
  215. // class TStringLookupValidator
  216. // ----- ----------------------
  217. //
  218. class _OWLCLASS TStringLookupValidator : public TLookupValidator {
  219.   public:
  220.     TStringLookupValidator(TSortedStringArray* strings);
  221.    ~TStringLookupValidator();
  222.  
  223.     //
  224.     // override TValidator's virtuals
  225.     //
  226.     void         Error();
  227.  
  228.     //
  229.     // override TLookupValidator's virtuals
  230.     //
  231.     bool         Lookup(const char far* str);
  232.  
  233.     void         NewStringList(TSortedStringArray* strings);
  234.  
  235.   protected:
  236.     TSortedStringArray* Strings;
  237.  
  238.   DECLARE_STREAMABLE(_OWLCLASS, TStringLookupValidator, 1);
  239. };
  240.  
  241. #endif  // OWL_VALIDATE_H
  242.