home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / crypto / cfiler / table.c < prev    next >
C/C++ Source or Header  |  1997-10-08  |  13KB  |  546 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright 1996-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. // TABLE.C
  13.  
  14. #include "cfiler.h"
  15. #include <malloc.h>
  16.  
  17. /******************************************************************\
  18. * CheckHeap()
  19. *
  20. * input: 
  21. *
  22. * message - buffer containing information to display on the
  23. * debug window
  24. *
  25. * purpose:
  26. *
  27. * Calls _heapchk and outputs a formatted message onto the
  28. * debug window.
  29. \******************************************************************/
  30.  
  31. VOID CheckHeap(LPTSTR message) {
  32.     INT rv = _heapchk();
  33.  
  34.     if (rv == _HEAPBADBEGIN) {
  35.         ErrorMsg(message);
  36.         ErrorMsg(TEXT("Initial header information is bad or cannot be found."));
  37.         return;
  38.     }
  39.     else if (rv == _HEAPBADNODE) {
  40.         ErrorMsg(message);
  41.         ErrorMsg(TEXT("Bad node has been found or heap is damaged."));
  42.         return;
  43.     }
  44.     else if (rv == _HEAPBADPTR) {
  45.         ErrorMsg(message);
  46.         ErrorMsg(TEXT("Pointer into heap is not valid."));
  47.         return;
  48.     }
  49.     else if (rv == _HEAPEMPTY) {
  50.         ErrorMsg(message);
  51.         ErrorMsg(TEXT("Heap has not been initialized."));
  52.         return;
  53.     }
  54.     else if (rv == _HEAPOK) {
  55.         ErrorMsg(message);
  56.         ErrorMsg(TEXT("Heap appears to be consistent."));
  57.         return;
  58.     }
  59.     else {
  60.         ErrorMsg(message);
  61.         ErrorMsg(TEXT("_heapcheck did not return a valid value."));
  62.     }
  63.     return;
  64. }
  65.  
  66. /******************************************************************\
  67. * GetSize()
  68. *
  69. * in parameters:
  70. * pTable - pointer to string table
  71. *
  72. * returns
  73. * number of elements of pTable
  74. * -1 if error
  75. \******************************************************************/
  76.  
  77. INT GetSize(TABLE pTable) {
  78.     if (!pTable) {
  79.         ErrorMsg(TEXT("GetSize: pTable is NULL."));
  80.         return -1;
  81.     }
  82.     
  83.     return pTable->iNumElems;
  84. }
  85.  
  86. /******************************************************************\
  87. * TableNew()
  88. *
  89. * Returns a new string table with no elements or NULL
  90. * if unsuccessful
  91. \******************************************************************/
  92.  
  93. TABLE TableNew(VOID) {
  94.     TABLE pTable;
  95.  
  96.     pTable = malloc(sizeof(*pTable) + 100);
  97.     
  98.     if (!pTable) {
  99.         ErrorMsg(TEXT("TableNew: malloc failed."));
  100.         return FALSE;
  101.     }
  102.     
  103.     pTable->iNumElems = 0;
  104.     pTable->pCurrentFiles = NULL;
  105.     
  106.     return pTable;
  107. }
  108.  
  109. /******************************************************************\
  110. * TableFree()
  111. *
  112. * input: pTable - pointer to a table
  113. *
  114. * purpose:
  115. * Frees the string table pointed to by pTable.
  116. *
  117. * return value:
  118. *
  119. * TRUE if successful
  120. * FALSE if unsuccessful
  121. \******************************************************************/
  122.  
  123. BOOL TableFree(TABLE pTable) {
  124.     ELEM *p, *q;
  125.  
  126.     if (!pTable) {
  127.         ErrorMsg(TEXT("TableFree: pTable is NULL."));
  128.         return FALSE;
  129.     }
  130.     
  131.     for (p = pTable->pCurrentFiles; p; p = q) {
  132.         q = p->next;
  133.         free(p->hidden);
  134.         free(p->displayed);
  135.         free(p);
  136.         p = NULL;
  137.     }
  138.  
  139.     free(pTable);
  140.  
  141.     pTable = NULL;
  142.  
  143.     return TRUE;
  144. }
  145.  
  146. /******************************************************************\
  147. * TableFind()
  148. *
  149. * input: pTable - pointer to a table
  150. * szFindString - string in question.
  151. *
  152. * purpose:
  153. * Searches for szFindString in table pTable.
  154. *
  155. * return value:
  156. *
  157. * TRUE if szFindString is in table
  158. * FALSE if otherwise
  159. \******************************************************************/
  160.  
  161. BOOL TableFind(TABLE pTable, LPTSTR szFindString) {
  162.     ELEM *p, *q;
  163.  
  164.     if (!pTable) {
  165.         ErrorMsg(TEXT("TableFree: pTable is NULL."));
  166.         return FALSE;
  167.     }
  168.     
  169.     for (p = pTable->pCurrentFiles; p; p = q) {
  170.         q = p->next;
  171.         if (!CFilerlstrcmp(p->displayed, szFindString)) {
  172.             return TRUE;
  173.         }
  174.     }
  175.  
  176.     return FALSE;
  177. }
  178.  
  179. /******************************************************************\
  180. * TableAdd()
  181. *
  182. * input:
  183. *
  184. * pTable - pointer to a string table
  185. *
  186. * szHidden, szDisplayed: pair of strings to insert into table
  187. *
  188. *
  189. * purpose:
  190. *
  191. * Inserts a string pair at the end of the string table pointed
  192. * to by pTable.
  193. \******************************************************************/
  194.  
  195. BOOL TableAdd(TABLE pTable, TCHAR *szHidden, LPTSTR szDisplayed) {
  196.     return TableInsert(pTable, szHidden, szDisplayed, pTable->iNumElems);
  197. }
  198.  
  199. /******************************************************************\
  200. * TableInsert()
  201. *
  202. * input:
  203. *
  204. * pTable - pointer to a string table
  205. * szHidden, szDisplayed : string pair to be inserted
  206. * i - position to insert the string pair.
  207. *
  208. * purpose:
  209. * inserts a string pair into the table pointed to by pTable
  210. * at position indicated by i.
  211. \******************************************************************/
  212.  
  213. BOOL TableInsert(TABLE pTable, LPTSTR szHidden, LPTSTR szDisplayed, INT i) {
  214.     ELEM *new, *p = NULL, *q = NULL;
  215.     INT j;
  216.  
  217.     if (!pTable) {
  218.         ErrorMsg(TEXT("TableInsert: pTable is NULL."));
  219.         return FALSE;
  220.     }
  221.     
  222.     new = malloc(sizeof(*new) + 2);
  223.  
  224.     new->hidden = malloc(3 + sizeof(*szHidden) * lstrlen(szHidden));
  225.     new->displayed = malloc(3 + sizeof(*szDisplayed) * lstrlen(szDisplayed));
  226.     new->next = NULL;
  227.  
  228.     lstrcpy(new->hidden, szHidden);
  229.     lstrcpy(new->displayed, szDisplayed);
  230.         
  231.     if (!pTable->pCurrentFiles) {
  232.         pTable->pCurrentFiles = new;
  233.         pTable->iNumElems++;
  234.         return TRUE;
  235.     }
  236.     
  237.     if (!i) {
  238.         ELEM *t;
  239.         t = pTable->pCurrentFiles;
  240.         pTable->pCurrentFiles = new;
  241.         new->next = t;
  242.         pTable->iNumElems++;
  243.         return TRUE;
  244.     }    
  245.  
  246.     for (j = 0, p = pTable->pCurrentFiles; j < i; j++, p = p->next)
  247.         q = p;
  248.         
  249.     q->next = new;
  250.     new->next = p;
  251.  
  252.     pTable->iNumElems++;
  253.     
  254.     return TRUE;
  255. }
  256.  
  257. /******************************************************************\
  258. * TableRemove()
  259. *
  260. * input: 
  261. * pTable - pointer to a string table
  262. * i - index of element to be removed
  263. *
  264. * purpose: 
  265. * Removes the element at position i in the string table 
  266. * pointed to by pTable.
  267. *
  268. * Return values:
  269. * TRUE if successful
  270. * FALSE if unsuccessful
  271. \******************************************************************/
  272.  
  273. BOOL TableRemove(TABLE pTable, INT i) {
  274.     INT j;
  275.     ELEM *p, *q;
  276.         
  277.     if (!pTable) {
  278.         ErrorMsg(TEXT("TableRemove: pTable is NULL."));
  279.         return FALSE;
  280.     }
  281.     
  282.     if (!pTable->pCurrentFiles) {
  283.         pTable->iNumElems = 0;
  284.         return TRUE;
  285.     }
  286.  
  287.     if (!i) {
  288.         ELEM *t = pTable->pCurrentFiles->next;
  289.         p = pTable->pCurrentFiles;
  290.         free(p->hidden);
  291.         free(p->displayed);
  292.         free(p);
  293.         pTable->pCurrentFiles = t;
  294.         pTable->iNumElems--;
  295.         return TRUE;
  296.     }
  297.  
  298.     for (j = 0, p = pTable->pCurrentFiles; j < i; j++, p = p->next) {
  299.         q = p;
  300.     }
  301.     
  302.     q->next = p->next;
  303.     free(p->hidden);
  304.     free(p->displayed);
  305.     free(p);
  306.  
  307.     pTable->iNumElems--;
  308.  
  309.     return TRUE;
  310. }
  311.  
  312. /******************************************************************\
  313. * TableSort()
  314. *
  315. * input:
  316. * pTable - pointer to a string table
  317. * compare - pointer to a compare function to be used in the
  318. * sort process
  319. *
  320. * Purpose: 
  321. * sorts the elements of the string table pointed to by pTable
  322. * on the displayed field.
  323. *
  324. * Return values:
  325. * TRUE if successful
  326. * FALSE if unsuccessful
  327. \******************************************************************/
  328.  
  329. BOOL TableSort(TABLE pTable, INT compare(LPCTSTR , LPCTSTR)) {
  330.     ELEM *p, *q, *min, *t;
  331.     INT i, j;
  332.  
  333.     if (!pTable) {
  334.         ErrorMsg(TEXT("TableSort: pTable is NULL."));
  335.         return FALSE;
  336.     }
  337.     
  338.     t = malloc(sizeof(*t));
  339.     
  340.     for (i = 0, p = pTable->pCurrentFiles; p && i < pTable->iNumElems - 1; i++, p = p->next) {
  341.         min = p;
  342.         for (q = p->next, j = 0; q && j < pTable->iNumElems; j++, q = q->next) {
  343.             if (!q->displayed || !p->displayed) {
  344.                 ErrorMsg(TEXT("TableSort: parameters to compare are null."));
  345.                 return FALSE;
  346.             }
  347.             if (compare(q->displayed, min->displayed) < 0) min = q;
  348.         }
  349.         t->displayed = min->displayed;
  350.         t->hidden = min->hidden; 
  351.         min->displayed = p->displayed; 
  352.         min->hidden = p->hidden;
  353.         p->displayed = t->displayed;
  354.         p->hidden = t->hidden;
  355.     }
  356.  
  357.     free(t);
  358.  
  359.     return TRUE;
  360. }
  361.  
  362. /******************************************************************\
  363. * TableSend()
  364. *
  365. * input:
  366. * pTable - pointer to a string table
  367. * mybox - handle to the listbox to which LB_ADDSTRING
  368. * messages are to be sent
  369. *
  370. * Purpose:
  371. * Sends the displayed members of the string table pointed to by
  372. * pTable to the listbox whose handle is mybox
  373. * by sending LB_ADDSTRING messages to the listbox for each
  374. * element of the table.
  375. *
  376. * Return values:
  377. * TRUE if successful
  378. * FALSE if unsuccessful
  379. \******************************************************************/
  380.  
  381. BOOL TableSend(TABLE pTable, HWND mybox) {
  382.     int j;
  383.     ELEM *p;
  384.     LRESULT result;
  385.  
  386.     if (!pTable) {
  387.         ErrorMsg(TEXT("TableSend: pTable is NULL."));
  388.         return FALSE;
  389.     }
  390.     
  391.     for (j = 0, p = pTable->pCurrentFiles; j < pTable->iNumElems; j++, p = p->next) {
  392.         result = SendMessage(mybox, LB_ADDSTRING, (WPARAM)0, (LPARAM)p->displayed);
  393.         
  394.         if (result == LB_ERR) {
  395.             ErrorMsg(TEXT("TableSend: LB_ADDSTRING failed."));
  396.             return FALSE;
  397.         }
  398.     }
  399.     
  400.     return TRUE;
  401. }
  402.  
  403. /******************************************************************\
  404. * TableGetHidden()
  405. *
  406. * input:
  407. * pTable - pointer to a string table
  408. * i - index of hidden element to retrieve
  409. * rv - (out parameter): buffer to store the hidden string
  410. *
  411. * Purpose:
  412. * Retrieves the hidden memeber of the table stored at position i
  413. * in the string table pointed to by pTable.
  414. *
  415. * Returns:
  416. * TRUE if successful
  417. * FALSE if unsuccessful
  418. \******************************************************************/
  419.  
  420. BOOL TableGetHidden(TABLE pTable, INT i, LPTSTR rv) {
  421.     INT j;
  422.     ELEM *p, *q;
  423.  
  424.     if (!pTable) {
  425.         ErrorMsg(TEXT("TableGetHidden: pTable is NULL."));
  426.         return FALSE;
  427.     }
  428.     
  429.     q = pTable->pCurrentFiles;
  430.     
  431.     if (q) {
  432.         for (p = pTable->pCurrentFiles, j = 0; j <= i; j++, p = p->next)
  433.             q = p;
  434.     }
  435.     else {
  436.         ErrorMsg(TEXT("TableGetHidden: pTable->pCurrentFiles is NULL."));
  437.         lstrcpy(rv, TEXT("error"));
  438.         return FALSE;
  439.     }
  440.  
  441.     if (q && q->hidden)
  442.         lstrcpy(rv, q->hidden);
  443.  
  444.     return TRUE;
  445. }
  446.  
  447. /******************************************************************\
  448. * SimplifyFileName()
  449. *
  450. * input:
  451. * szFileName - buffer of string to be "simplified"
  452. * rv - (out parameter) - buffer where simplified string
  453. * is to be stored
  454. *
  455. * purpose:
  456. * strips the characters [, ], >, |, and ; from szFileName and
  457. * stores the result in rv.
  458. *
  459. * returns:
  460. * TRUE if successful
  461. * FALSE if unsuccessful
  462. \******************************************************************/
  463.  
  464. BOOL SimplifyFileName(LPTSTR szFileName, LPTSTR rv) {
  465.     INT i, j;
  466.  
  467.     for (i = 0, j = 0; szFileName[i]; ) {
  468.         if (szFileName[i] == TEXT('[') || 
  469.             szFileName[i] == TEXT(']') || 
  470.             szFileName[i] == TEXT('|') || 
  471.             szFileName[i] == TEXT('>') ||
  472.             szFileName[i] == TEXT(';'))
  473.             i++;
  474.         else {
  475.             rv[j] = szFileName[i];
  476.             i++;
  477.             j++;
  478.         }
  479.     }
  480.  
  481.     rv[j] = TEXT('\0');
  482.     return TRUE;
  483. }
  484.  
  485. /******************************************************************\
  486. * ReplaceEscapeCharacters()
  487. *
  488. * input:
  489. * szFileName - input buffer
  490. * rv - (out parameter) - output buffer
  491. *
  492. * purpose:
  493. * replaces '&' characters in szFileName with '\&' and stores
  494. * the result in rv.
  495. *
  496. * returns:
  497. * TRUE if successful
  498. * FALSE if unsuccessful
  499. \******************************************************************/
  500.  
  501. BOOL ReplaceEscapeCharacters(LPTSTR szFileName, LPTSTR rv) {
  502.     INT i, j;
  503.  
  504.     for (i = 0, j = 0; szFileName[i]; ) {
  505.         if (szFileName[i] == TEXT('&')) {
  506.             rv[j] = TEXT('&');
  507.             rv[j + 1] = TEXT('&');
  508.             i++;
  509.             j += 2;
  510.         }
  511.         else {
  512.             rv[j] = szFileName[i];
  513.             i++;
  514.             j++;
  515.         }
  516.     }
  517.  
  518.     rv[j] = TEXT('\0');
  519.     return TRUE;
  520. }
  521.  
  522. /******************************************************************\
  523. * CFilerlstrcmp: Case-insensitive comparison of the strings s and t.
  524. * foo and |foo are the same.
  525. * [foo] and |[foo] are the same.
  526. * [foo] and foo are not the same.
  527. \******************************************************************/
  528.  
  529. INT CFilerlstrcmp(LPCTSTR s, LPCTSTR t)
  530. {
  531.     int i = 0, j = 0;
  532.  
  533.     if (s[0] == TEXT('|') || s[0] == TEXT('>') || s[0] == TEXT(';'))
  534.         i++;
  535.     
  536.     if (t[0] == TEXT('|') || t[0] == TEXT('>') || t[0] == TEXT(';'))
  537.         j++;
  538.         
  539.     for ( ; toupper(s[i]) == toupper(t[j]); i++, j++)
  540.         if (toupper(s[i]) == TEXT('\0'))
  541.             return 0;
  542.  
  543.     return toupper(s[i]) - toupper(t[j]);
  544. }
  545.