home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / shell / fileview / cstrtabl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  3.5 KB  |  129 lines

  1. //THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 
  2. //ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  3. //THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright  1994-1996  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //    PROGRAM:        CSTRTABL.CPP
  9. //
  10. //    PURPOSE:   Implementation of a string table handler.  The CStringTable
  11. // class hides details of storage from the user.  The strings might
  12. // be cached, or they might be loaded as necessary.  In either case,
  13. // we must know the number of strings so we know whether or not to
  14. // reload strings.
  15. //
  16. //    PLATFORMS:    Windows 95
  17. //
  18. //    FUNCTIONS:    
  19. //   
  20. //
  21. //    SPECIAL INSTRUCTIONS: N/A
  22. //
  23. #include "fileview.h"
  24.  
  25. //
  26. //   FUNCTION: CStringTable::CStringTable    
  27. //
  28. //   PURPOSE:     Constructor
  29. //
  30. //   PARAMETERS: 
  31. //         hInst           HANDLE to the module instance from which we  load strings.
  32. // 
  33. CStringTable::CStringTable(HINSTANCE hInst)
  34.     {
  35.     m_hInst=hInst;
  36.     m_pszStrings=NULL;
  37.     m_ppszTable=NULL;
  38.     return;
  39.     }
  40.  
  41. //
  42. //   FUNCTION:  CStringTable::~CStringTable    
  43. //
  44. //   PURPOSE:     Destructor
  45. //
  46. CStringTable::~CStringTable(void)
  47.     {
  48.     if (NULL!=m_pszStrings)
  49.         free(m_pszStrings);
  50.  
  51.     if (NULL!=m_ppszTable)
  52.         free(m_ppszTable);
  53.  
  54.     return;
  55.     }
  56. //
  57. //   FUNCTION: CStringTable::FInit    
  58. //
  59. //   PURPOSE:    Initialization function for a StringTable that is prone to
  60. //  failure.  If this fails then the caller is responsible for
  61. //  guaranteeing that the destructor is called quickly.
  62. //
  63. //   PARAMETERS: 
  64. //   idsMin          UINT first identifier in the stringtable
  65. //  idsMax          UINT last identifier in the stringtable.
  66. //  cchMax          UINT with the maximum string length allowed.
  67. //
  68. //   RETURN VALUE:
  69. //        BOOL            TRUE if the function is successful,  FALSE otherwise.
  70. //
  71. BOOL CStringTable::FInit(UINT idsMin, UINT idsMax, UINT cchMax)
  72.     {
  73.     UINT        i;
  74.     UINT        cch;
  75.     UINT        cchUsed=0;
  76.     LPSTR       psz;
  77.  
  78.     m_idsMin=idsMin;
  79.     m_idsMax=idsMax;
  80.     m_cStrings=(idsMax-idsMin+1);
  81.  
  82.     //Allocate space for the pointer table.
  83.     m_ppszTable=(LPSTR *)malloc(sizeof(LPSTR)*m_cStrings);
  84.  
  85.     if (NULL==m_ppszTable)
  86.         return FALSE;
  87.      // Allocate enough memory for cStrings*cchMax characters.  80
  88.      // characters is the maximum string length we allow.  This
  89.      // will result in some unused memory, but a few K is not
  90.      // worth quibbling over.
  91.     m_pszStrings=(LPSTR)malloc(m_cStrings * cchMax);
  92.  
  93.     if (NULL==m_pszStrings)
  94.         {
  95.         free(m_ppszTable);
  96.         m_ppszTable=NULL;
  97.         return FALSE;
  98.         }
  99.      // Load the strings:  we load each string in turn into psz,
  100.      // store the string pointer into the table and increment psz
  101.      // to the next positions.
  102.     psz=m_pszStrings;
  103.  
  104.     for (i=idsMin; i <= idsMax; i++)
  105.         {
  106.         m_ppszTable[i-idsMin]=psz;
  107.         cch=LoadString(m_hInst, i, psz, 255);
  108.  
  109.         //Account for a null terminator with +1
  110.         psz    +=cch+1;
  111.         cchUsed+=cch;
  112.         }
  113.  
  114.     return TRUE;
  115.     }
  116. //
  117. //   FUNCTION: CStringTable::operator[]    
  118. //
  119. //   PURPOSE:  Returns a pointer to the requested string in the stringtable
  120. //  or NULL if the specified string does not exist.
  121. // 
  122. const LPSTR CStringTable::operator[](const UINT uID) const
  123.     {
  124.     if (uID < m_idsMin || uID > m_idsMax)
  125.         return NULL;
  126.  
  127.     return (const LPSTR)m_ppszTable[uID-m_idsMin];
  128.     }
  129.