home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / classlib / cstrtabl.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  3KB  |  149 lines

  1. /*
  2.  * CSTRTABL.CPP
  3.  * Sample Code Class Libraries
  4.  *
  5.  * Implementation of a string table handler.  The CStringTable
  6.  * class hides details of storage from the user.  The strings might
  7.  * be cached, or they might be loaded as necessary.  In either case,
  8.  * we must know the number of strings so we know whether or not to
  9.  * reload strings.
  10.  *
  11.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  12.  *
  13.  * Kraig Brockschmidt, Microsoft
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19. #include <windows.h>
  20. #include <malloc.h>
  21. #include "classlib.h"
  22.  
  23.  
  24.  
  25. /*
  26.  * CStringTable::CStringTable
  27.  * CStringTable::~CStringTable
  28.  *
  29.  * Constructor Parameters:
  30.  *  hInst           HANDLE to the application instance from which we
  31.  *                  load strings.
  32.  */
  33.  
  34. CStringTable::CStringTable(HINSTANCE hInst)
  35.     {
  36.     m_hInst=hInst;
  37.     m_pszStrings=NULL;
  38.     m_ppszTable=NULL;
  39.     return;
  40.     }
  41.  
  42.  
  43. CStringTable::~CStringTable(void)
  44.     {
  45.     if (NULL!=m_pszStrings)
  46.         free(m_pszStrings);
  47.  
  48.     if (NULL!=m_ppszTable)
  49.         free(m_ppszTable);
  50.  
  51.     return;
  52.     }
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. /*
  61.  * CStringTable::Init
  62.  *
  63.  * Purpose:
  64.  *  Initialization function for a StringTable that is prone to
  65.  *  failure.  If this fails then the caller is responsible for
  66.  *  guaranteeing that the destructor is called quickly.
  67.  *
  68.  * Parameters:
  69.  *  idsMin          UINT first identifier in the stringtable
  70.  *  idsMax          UINT last identifier in the stringtable.
  71.  *
  72.  * Return Value:
  73.  *  BOOL            TRUE if the function is successful, FALSE
  74.  *                  otherwise.
  75.  */
  76.  
  77.  
  78. BOOL CStringTable::Init(UINT idsMin, UINT idsMax)
  79.     {
  80.     UINT        i;
  81.     UINT        cch;
  82.     UINT        cchUsed=0;
  83.     LPTSTR      psz;
  84.  
  85.     m_idsMin=idsMin;
  86.     m_idsMax=idsMax;
  87.     m_cStrings=(idsMax-idsMin+1);
  88.  
  89.  
  90.     //Allocate space for the pointer table.
  91.     m_ppszTable=(LPTSTR *)malloc(sizeof(LPTSTR)*m_cStrings);
  92.  
  93.     if (NULL==m_ppszTable)
  94.         return FALSE;
  95.  
  96.  
  97.     //Allocate enough memory for cStrings of CCHSTRINGMAX.
  98.     m_pszStrings=(LPTSTR)malloc(m_cStrings * CCHSTRINGMAX * sizeof(TCHAR));
  99.  
  100.     if (NULL==m_pszStrings)
  101.         {
  102.         free(m_ppszTable);
  103.         m_ppszTable=NULL;
  104.         return FALSE;
  105.         }
  106.  
  107.  
  108.     /*
  109.      * Load the strings:  we load each string in turn into psz,
  110.      * store the string pointer into the table and increment psz
  111.      * to the next positions.
  112.      */
  113.  
  114.     psz=m_pszStrings;
  115.  
  116.     for (i=idsMin; i <= idsMax; i++)
  117.         {
  118.         m_ppszTable[i-idsMin]=psz;
  119.         cch=LoadString(m_hInst, i, psz, CCHSTRINGMAX-1);
  120.  
  121.         //Account for a null terminator
  122.         psz+=cch+1;
  123.         cchUsed+=cch;
  124.         }
  125.  
  126.     return TRUE;
  127.     }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. /*
  135.  * CStringTable::operator[]
  136.  *
  137.  * Purpose:
  138.  *  Returns a pointer to the requested string in the stringtable or
  139.  *  NULL if the specified string does not exist.
  140.  */
  141.  
  142. const LPTSTR CStringTable::operator[](const UINT uID) const
  143.     {
  144.     if (uID < m_idsMin || uID > m_idsMax)
  145.         return NULL;
  146.  
  147.     return (const LPTSTR)m_ppszTable[uID-m_idsMin];
  148.     }
  149.