home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / token.cpp < prev    next >
C/C++ Source or Header  |  1997-10-25  |  3KB  |  181 lines

  1. #include "CkyPch.h"
  2.  
  3. #include "debug.h"
  4. #include "token.h"
  5. #include "utils.h"
  6.  
  7.  
  8. // Base class for keywords
  9.  
  10. CToken::CToken(
  11.     const string& rstr,
  12.     const BOUNDARY bndPrefix /* =IRRELEVANT */,
  13.     const BOUNDARY bndSuffix /* =IRRELEVANT */)
  14.     : m_str(rstr),
  15.       m_bndPrefix(bndPrefix),
  16.       m_bndSuffix(bndSuffix)
  17. {
  18. }
  19.  
  20.  
  21.  
  22. CToken::~CToken()
  23. {
  24. }
  25.  
  26.  
  27.  
  28. BOOL
  29. CToken::MatchesBoundaryClass(
  30.     const TCHAR tch,
  31.     const BOUNDARY bnd)
  32. {
  33.     switch (bnd)
  34.     {
  35.     case IRRELEVANT:
  36.         return TRUE;
  37.         
  38.     case WHITESPACE:
  39.         return _istspace(tch);
  40.         
  41.     case ALPHA:
  42.         return _istalpha(tch);
  43.         
  44.     case NUMERIC:
  45.         return _istdigit(tch);
  46.  
  47.     case ALPHANUMERIC:
  48.         return _istalnum(tch);
  49.  
  50.     case NEWLINE:
  51.         return tch == _T('\n')  ||  tch == _T('\r');
  52.  
  53.     default:
  54.         ASSERT(FALSE);
  55.         return FALSE;
  56.     }
  57. }
  58.  
  59.  
  60.  
  61. // DoFilter: default implementation is to update the state stack
  62. // and copy the text that matched the token
  63.  
  64. UINT
  65. CToken::DoFilter(
  66.     CStateStack& rss,
  67.     LPCTSTR&     rptszData,
  68.     UINT         cchData,
  69.     LPTSTR&      rptszOutBuf) const
  70. {
  71.     const UINT cb  = CountBytes(rss, rptszData, cchData);
  72.     const UINT cch = m_str.length();
  73.  
  74.     // for (UINT i = 0; i < cch; ++i)
  75.     //     TRACE("%c", rptszOutBuf[i]);
  76.  
  77.     memcpy(rptszOutBuf, rptszData, cch);
  78.     rptszData += cch;
  79.     rptszOutBuf += cch;
  80.  
  81.     return cb;
  82. }
  83.  
  84.  
  85.  
  86. #ifdef _DEBUG
  87.  
  88. void
  89. CToken::AssertValid() const
  90. {
  91. }
  92.  
  93.  
  94.  
  95. void
  96. CToken::Dump() const
  97. {
  98.     TRACE("\t%d %d", (int) m_bndPrefix, (int) m_bndSuffix);
  99. }
  100.  
  101. #endif // _DEBUG
  102.  
  103.  
  104.  
  105. //----------------------------------------------------------------
  106.  
  107.  
  108. bool
  109. CTokenTrie::AddToken(
  110.     const CToken* ptok)
  111. {
  112.     return CTrie<CToken, true, true>::AddToken(ptok->m_str.c_str(), ptok);
  113. }
  114.  
  115.  
  116.  
  117. inline bool
  118. CTokenTrie::_LastCharPresent(
  119.     CHAR ch) const
  120. {
  121.     ASSERT(CHAR_MIN <= ch  &&  ch <= CHAR_MAX);
  122.     const UINT i = ch - CHAR_MIN;   // CHAR_MIN is -128 for `signed char'
  123.  
  124.     return m_afLastChar[i >> 3] & (1 << (i & 7))  ?  true  :  false;
  125. }
  126.  
  127.  
  128.  
  129. inline void
  130. CTokenTrie::_SetLastCharPresent(
  131.     CHAR ch,
  132.     bool f)
  133. {
  134.     ASSERT(CHAR_MIN <= ch  &&  ch <= CHAR_MAX);
  135.     const UINT i = ch - CHAR_MIN;
  136.  
  137.     if (f)
  138.         m_afLastChar[i >> 3] |=  (1 << (i & 7));
  139.     else
  140.         m_afLastChar[i >> 3] &= ~(1 << (i & 7));
  141. }
  142.  
  143.  
  144. // ctor
  145.  
  146. CTokenTrie::CTokenTrie()
  147. {
  148.     memset(m_afCharPresent, 0, sizeof(m_afCharPresent));
  149.  
  150.     static const CHAR achEndTokens[] = {
  151.         ' ', '\t', '\f', '\b', '\r', '\n', '>',
  152.     };
  153.  
  154.     for (int i = ARRAYSIZE(achEndTokens);  --i >= 0; )
  155.         _SetLastCharPresent(achEndTokens[i], true);
  156. }
  157.  
  158.  
  159.  
  160. // Returns 1 past the last character which is a valid token-ending char,
  161. // or < 0 if no such char
  162. int
  163. CTokenTrie::EndOfBuffer(
  164.     PHTTP_FILTER_RAW_DATA pRawData,
  165.     int iStart)
  166. {
  167.     LPSTR pszData = (LPSTR) pRawData->pvInData;
  168.  
  169.     // Empty interval?
  170.     if (pRawData->cbInData == iStart)
  171.         return iStart;
  172.  
  173.     for (int i = pRawData->cbInData;  --i >= iStart; )
  174.     {
  175.         if (_LastCharPresent(pszData[i]))
  176.             return i+1;
  177.     }
  178.  
  179.     return -1;
  180. }
  181.