strbuf.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 5k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include "priv.h"
  2. #if 0
  3. #include "strbuf.h"
  4. #define USE_ATOM_TABLE
  5. #define ATOM_TABLE_PRIME    101
  6. #define ATOM_TABLE_GROWBY   50
  7. BOOL g_fAtomTableInited = FALSE;
  8. // BUGBUG: really bad out-of-memory handling
  9. CStringBuf::CStringBuf(int cGrowBy)
  10. {
  11. #ifdef USE_ATOM_TABLE
  12.     m_atomTable = NULL;
  13.     m_nAtoms = 0;
  14.     m_currentAtom = 0;
  15.     m_bufSize = 0;
  16.     if (!g_fAtomTableInited)
  17.     {
  18.         InitAtomTable(ATOM_TABLE_PRIME);
  19.         g_fAtomTableInited = TRUE;
  20.     }
  21. #else
  22.     m_growBy = cGrowBy;
  23.     m_buf = 0;
  24.     m_len = 0;
  25.     m_pos = 0;
  26. #endif
  27. }
  28. CStringBuf::~CStringBuf()
  29. {
  30. #ifdef USE_ATOM_TABLE
  31.     if(m_atomTable)
  32.         LocalFree((HANDLE)m_atomTable);
  33. #else
  34.     if(m_buf)
  35.         LocalFree((HANDLE)m_buf);
  36. #endif
  37. }
  38. char*   CStringBuf::DetachBuf()
  39. {
  40. #ifdef USE_ATOM_TABLE
  41.     LPSTR buf = NULL;
  42.     PWORD_ATOM pAtom = m_atomTable;
  43.     if(m_currentAtom)
  44.     {
  45.         buf = (LPSTR)LocalAlloc(LPTR, m_bufSize);
  46.         if(buf)
  47.         {
  48.             LPSTR pPos = buf;
  49.             UINT uiWordLen;
  50.             TCHAR szWord[MAX_WORD_LENGTH];
  51.             *pPos = '';
  52.             // go through the atom table.
  53.             // and delete the atoms as we get the names.
  54.             for(int i = 0; i < m_currentAtom; i++, pAtom++)
  55.             {
  56.                 if(uiWordLen = GetAtomName(pAtom->atom, szWord, ARRAYSIZE(szWord)))
  57.                 {
  58.                     // get the word.
  59.                     memcpy(pPos, szWord, uiWordLen);
  60.                     pPos += uiWordLen;
  61.                     *pPos++ = '+';
  62.                     // we don't put the number of hits in the query yet.
  63.                     // make sure the atom is deleted.
  64.                     // note that DeleteAtom returns zero when the ref count is not zero.
  65.                     // while (!DeleteAtom(pAtom->atom)) ;
  66.                     DeleteAtom(pAtom->atom);
  67.                 }
  68.             }
  69.             // free the atom table.
  70.             LocalFree((HANDLE)m_atomTable);
  71.             m_atomTable = NULL;
  72.             m_nAtoms = 0;
  73.             m_currentAtom = 0;
  74.             m_bufSize = 0;
  75.             // remove the last '+' character, and null terminate.
  76.             if(pPos > buf)
  77.                 *--pPos = '';
  78.             // note that caller of DeatchBuf is responsible for freeing the returned buffer.
  79.         }
  80.     }
  81. #else
  82.     char* buf = m_buf;
  83.     m_buf = 0;
  84.     m_len = 0;
  85.     m_pos = 0;
  86. #endif
  87.     return buf;
  88. }
  89. int     CStringBuf::Grow(int cAdd)
  90. {
  91. #ifdef USE_ATOM_TABLE
  92.     int newSize = m_nAtoms + cAdd;
  93.     PWORD_ATOM newBuf;
  94.     if(!m_atomTable)
  95.         newBuf = (PWORD_ATOM)LocalAlloc(LPTR, newSize * sizeof(WORD_ATOM));
  96.     else
  97.         newBuf = (PWORD_ATOM)LocalReAlloc((HANDLE)m_atomTable, newSize * sizeof(WORD_ATOM), LMEM_MOVEABLE);
  98.     if(!newBuf)
  99.         return 0;
  100.     m_atomTable = newBuf;
  101.     m_nAtoms = newSize;
  102.     return 1;
  103. #else
  104.     int newSize = m_pos + cAdd + m_growBy;
  105.     // char* newBuf = (char*) realloc(m_buf, newSize);
  106.     char* newBuf;
  107.     if(!m_buf)
  108.         newBuf = (char *)LocalAlloc(LPTR, newSize);
  109.     else
  110.         newBuf = (char *)LocalReAlloc((HANDLE)m_buf, newSize, LMEM_MOVEABLE);
  111.     if(!newBuf)
  112.         return 0;
  113.     m_buf = newBuf;
  114.     m_len = newSize;
  115.     return 1;
  116. #endif
  117. }
  118. void    CStringBuf::AddChar(char ch)
  119. {
  120. #ifndef USE_ATOM_TABLE
  121.     if(m_pos == m_len)
  122.     {
  123.         if(!Grow(1))
  124.             return;
  125.     }
  126.     m_buf[m_pos++] = ch;
  127. #endif
  128. }
  129. BOOL    CStringBuf::AddSZ(char* sz)
  130. {
  131. #ifdef USE_ATOM_TABLE
  132.     int iStrLen = lstrlen(sz);
  133.     // ignore single character words
  134.     if(iStrLen <= 1)
  135.         return FALSE;
  136.     // probably other kind of strings strings that we should also ignore.
  137.     // 1. two character words
  138.     // 2. numbers
  139.     if(m_currentAtom >= m_nAtoms)
  140.         if(!Grow(ATOM_TABLE_GROWBY))
  141.             return FALSE;
  142.     // haven't implement the hits yet.
  143.     // if we want the atom functions to handle the hits for us,
  144.     // we'll just keep calling AddAtom and count the hits when we DeleteAtom.
  145.     ATOM atom = FindAtom(sz);
  146.     if(!atom)
  147.     {
  148.         atom = AddAtom(sz);
  149.         if(atom)
  150.         {
  151.             m_atomTable[m_currentAtom].atom = atom;
  152.             m_currentAtom++;
  153.             m_bufSize += iStrLen + 1;
  154.             return TRUE;
  155.         }
  156.     }
  157.     return FALSE;
  158. #else
  159.     int szLen = strlen(sz);
  160.     if(m_pos + szLen >= m_len)
  161.     {
  162.         if(!Grow(szLen))
  163.             return FALSE;
  164.     }
  165.     memcpy(m_buf + m_pos, sz, szLen + 1);
  166.     m_pos += szLen;
  167.     return TRUE;
  168. #endif
  169. }
  170. void    CStringBuf::SetSize(int cNew)
  171. {
  172. #ifndef USE_ATOM_TABLE
  173.     ASSERT(cNew >= 0 && cNew <= m_pos);
  174.     if(m_buf && (cNew >= 0))
  175.     {
  176.         m_pos = cNew;
  177.         m_buf[m_pos] = '';
  178.     }
  179. #endif
  180. }
  181. #endif