home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / kwclass.zip / KVALID.CPP < prev    next >
C/C++ Source or Header  |  1994-04-18  |  5KB  |  262 lines

  1. #define INCL_PM
  2. #include <os2.h>
  3. #include <kvalid.hpp>
  4. #include <ikeyevt.hpp>
  5. #include <kentryfd.hpp>
  6. #include <istring.hpp>
  7. #include <istrtest.hpp>
  8. #include <ctype.h>
  9. #include <iexcept.hpp>
  10.  
  11. KValidator::KValidator(Boolean val) 
  12.   : validateContentOnChar(val)
  13. {
  14. }
  15.  
  16. KValidator::~KValidator() 
  17. {
  18. }
  19.  
  20. Boolean KValidator::characterKeyPress(IKeyboardEvent& event)
  21. {
  22.  
  23.    // first check that the character itself is valid.  If any 
  24.    // character not in the set allowed by the test member function
  25.    // is found, the character is invalid.
  26.    IString character(event.mixedCharacter());
  27.    if (character.indexOfAnyBut(IStringTestMemberFn<KValidator>(*this, test)))
  28.    {
  29.       error();
  30.       return true;
  31.    }
  32.  
  33.    // simple validation doesn't require checking the format of 
  34.    // field content on each character 
  35.    if (!validateContentOnChar)
  36.       return false;
  37.  
  38.    // save the current content of the entry field
  39.    KEntryField *field = (KEntryField*)event.window();
  40.    IString originalContent = field->text();
  41.  
  42.    // pass the event on to the control
  43.    field->disableUpdate();
  44.    defaultProcedure(event);
  45.  
  46.    // get the new field content and validate it
  47.    Boolean invalid;
  48.    IString newContent = field->text();
  49.    if (!isValid(newContent))
  50.    {
  51.       error();
  52.       invalid = true;
  53.    }
  54.    else
  55.    {
  56.       invalid = false;
  57.    }
  58.    
  59.    // reset the content to allow other handlers to filter the
  60.    // event if necessary
  61.    field->setText(originalContent);
  62.    field->enableUpdate();
  63.  
  64.    return invalid;
  65. }
  66.  
  67. Boolean KValidator::virtualKeyPress(IKeyboardEvent& event)
  68. {
  69.    // simple validation doesn't require checking the format of 
  70.    // field content on each character 
  71.    if (!validateContentOnChar)
  72.       return false;
  73.  
  74.    KEntryField *field = (KEntryField*)event.window();
  75.    unsigned long ulKey = event.virtualKey();
  76.    Boolean filtered = false;
  77.  
  78.    if (ulKey == IKeyboardEvent::backSpace || ulKey == IKeyboardEvent::deleteKey)
  79.    {
  80.       IString originalContent = field->text();
  81.       field->disableUpdate();
  82.       defaultProcedure(event);
  83.       IString newContent = field->text();
  84.       if (!isValid(newContent))
  85.       {
  86.          filtered = true;
  87.          error();
  88.       }
  89.       field->setText(originalContent);
  90.       field->enableUpdate();
  91.    }
  92.    return filtered;
  93. }
  94.  
  95. void KValidator::error()
  96. {
  97.    WinAlarm(HWND_DESKTOP, WA_ERROR);
  98. }
  99.  
  100. Boolean KValidator::isValid(const char *text, Boolean) const
  101. {
  102.    return !IString(text).indexOfAnyBut(
  103.                  IStringTestMemberFn<KValidator>(*this, test));
  104. }
  105.  
  106. Boolean KValidator::test(int) const
  107. {
  108.    return true;
  109. }
  110.  
  111. IString &KValidator::fill(IString &text)
  112. {
  113.   return text;
  114. }
  115.  
  116. IString &KValidator::strip(IString &text)
  117. {
  118.   return text;
  119. }
  120.  
  121. KAlphaValidator::KAlphaValidator()
  122.   : KValidator(false)
  123. {
  124. }
  125.  
  126. Boolean KAlphaValidator::test(int c) const
  127. {
  128.    return(isalpha(c) || c == ' ');
  129. }
  130.  
  131.  
  132. KNumericValidator::KNumericValidator()
  133.   : KValidator(true)
  134.   , allowSign(true)
  135. {
  136. }
  137.  
  138. void KNumericValidator::enableSign(Boolean val)
  139. {
  140.    allowSign = val;
  141. }
  142.  
  143. void KNumericValidator::disableSign()
  144. {
  145.    enableSign(false);
  146. }
  147.  
  148. Boolean KNumericValidator::isValid(const char *text, Boolean fill) const
  149. {
  150.    IString string(text);
  151.    Boolean valid = false;
  152.  
  153.    valid = Inherited::isValid(string, fill);
  154.    if (allowSign)
  155.    {
  156.       if (valid && string.indexOf('-') > 1)
  157.           valid = false;
  158.    }
  159.    return valid;
  160. }
  161.  
  162. Boolean KNumericValidator::test(int c) const
  163. {
  164.    if (isdigit(c))
  165.       return true;
  166.    if (allowSign && c == '-')
  167.       return true;
  168.    return false;
  169. }
  170.  
  171.  
  172. KAlphaNumericValidator::KAlphaNumericValidator()
  173.   : KValidator(false)
  174. {
  175. }
  176.  
  177. Boolean KAlphaNumericValidator::test(int c) const
  178. {
  179.    return(isalnum(c));
  180. }
  181.  
  182. KRealValidator::KRealValidator()
  183.   : KValidator(true)
  184. {
  185. }
  186.  
  187. Boolean KRealValidator::isValid(const char *text, Boolean fill) const
  188. {
  189.    // check for valid characters
  190.    if (!Inherited::isValid(text,fill))
  191.       return false;
  192.  
  193.    // then check the format
  194.    int nCount = 0, nDigitFound = 0;
  195.    if(text[nCount] == '+' || text[nCount] == '-')
  196.       nCount++;
  197.  
  198.    while((text[nCount] != 0x00) &&
  199.          (text[nCount] != '.') &&
  200.          (toupper(text[nCount]) != 'E') &&
  201.          (toupper(text[nCount]) != 'D'))
  202.    {
  203.       if(isdigit(text[nCount]))
  204.       {
  205.          nCount++;
  206.          nDigitFound = 1;
  207.       }
  208.       else
  209.          return false;       
  210.    }
  211.  
  212.    if(text[nCount] == '.')
  213.    {
  214.       nCount++;
  215.       while(text[nCount] != 0x00 &&
  216.             toupper(text[nCount]) != 'E' &&
  217.             toupper(text[nCount]) != 'D')
  218.       {
  219.          if(isdigit(text[nCount]))
  220.          {
  221.             nCount++;
  222.             nDigitFound = 1;
  223.          }
  224.          else
  225.             return false;       
  226.       }
  227.    }
  228.  
  229.    if(text[nCount] == 0x00)
  230.       return true;
  231.    if(!nDigitFound)
  232.       return false;
  233.  
  234.    nCount++;
  235.  
  236.    if(text[nCount] == '+' || text[nCount] == '-')
  237.       nCount++;
  238.  
  239.    while(text[nCount] != 0x00)
  240.    {
  241.       if(isdigit(text[nCount]))
  242.          nCount++;
  243.       else
  244.          return false;       
  245.    }
  246.  
  247.    return true;
  248. }
  249.  
  250. Boolean KRealValidator::test(int c) const
  251. {
  252.    return (isdigit(c) 
  253.            || c == '.'
  254.            || c == '-'
  255.            || c == '+'
  256.            || toupper(c) == 'E'
  257.            || toupper(c) == 'D');
  258. }
  259.  
  260.  
  261.  
  262.