home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap19 / strlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  2.6 KB  |  107 lines

  1. /*------------------------------------------------
  2.    STRLIB.C -- Library module for STRPROG program
  3.                (c) Charles Petzold,  1990
  4.   ------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. typedef BOOL FAR PASCAL GETSTR (LPSTR, LPSTR) ;
  9. HANDLE hStrings [256] ;
  10. short  nTotal = 0 ;
  11.  
  12. int FAR PASCAL LibMain (HANDLE hInstance, WORD wDataSeg, WORD wHeapSize,
  13.                         LPSTR lpszCmdLine)
  14.      {
  15.      if (wHeapSize > 0)
  16.           UnlockData (0) ;
  17.  
  18.      return 1 ;
  19.      }
  20.  
  21. BOOL FAR PASCAL AddString (LPSTR lpStringIn)
  22.      {
  23.      HANDLE hString ;
  24.      NPSTR  npString ;
  25.      short  i, nLength, nCompare ;
  26.  
  27.      if (nTotal == 255)
  28.           return FALSE ;
  29.  
  30.      if (0 == (nLength = lstrlen (lpStringIn)))
  31.           return FALSE ;
  32.  
  33.      if (NULL == (hString = LocalAlloc (LHND, 1 + nLength)))
  34.           return FALSE ;
  35.  
  36.      npString = LocalLock (hString) ;
  37.      lstrcpy (npString, lpStringIn) ;
  38.      AnsiUpper (npString) ;
  39.      LocalUnlock (hString) ;
  40.  
  41.      for (i = nTotal ; i > 0 ; i--)
  42.           {
  43.           npString = LocalLock (hStrings [i - 1]) ;
  44.           nCompare = lstrcmpi (lpStringIn, npString) ;
  45.           LocalUnlock (hStrings [i - 1]) ;
  46.  
  47.           if (nCompare > 0)
  48.                {
  49.                hStrings [i] = hString ;
  50.                break ;
  51.                }
  52.           hStrings [i] = hStrings [i - 1] ;
  53.           }
  54.  
  55.      if (i == 0)
  56.           hStrings [0] = hString ;
  57.  
  58.      nTotal++ ;
  59.      return TRUE ;
  60.      }
  61.  
  62. BOOL FAR PASCAL DeleteString (LPSTR lpStringIn)
  63.      {
  64.      NPSTR npString ;
  65.      short i, j, nCompare ;
  66.  
  67.      if (0 == lstrlen (lpStringIn))
  68.           return FALSE ;
  69.  
  70.      for (i = 0 ; i < nTotal ; i++)
  71.           {
  72.           npString = LocalLock (hStrings [i]) ;
  73.           nCompare = lstrcmpi (npString, lpStringIn) ;
  74.           LocalUnlock (hStrings [i]) ;
  75.  
  76.           if (nCompare == 0)
  77.                break ;
  78.           }
  79.  
  80.      if (i == nTotal)
  81.           return FALSE ;
  82.  
  83.      for (j = i ; j < nTotal ; j++)
  84.           hStrings [j] = hStrings [j + 1] ;
  85.  
  86.      nTotal-- ;
  87.      return TRUE ;
  88.      }
  89.  
  90. short FAR PASCAL GetStrings (GETSTR lpfnGetStrCallBack, LPSTR lpParam)
  91.      {
  92.      BOOL  bReturn ;
  93.      NPSTR npString ;
  94.      short i ;
  95.  
  96.      for (i = 0 ; i < nTotal ; i++)
  97.           {
  98.           npString = LocalLock (hStrings [i]) ;
  99.           bReturn = (*lpfnGetStrCallBack) ((LPSTR) npString, lpParam) ;
  100.           LocalUnlock (hStrings [i]) ;
  101.  
  102.           if (bReturn == FALSE)
  103.                return i + 1 ;
  104.           }
  105.      return nTotal ;
  106.      }
  107.