home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / quot210s.zip / src / qthreads.c < prev    next >
C/C++ Source or Header  |  1998-06-07  |  7KB  |  274 lines

  1. /*
  2.  * qthreads.c
  3.  *
  4.  * Threads for use with quote databases.
  5.  *
  6.  *      Created: 27th January, 1997
  7.  * Version 1.00: 10th April, 1997
  8.  * Version 2.00: 18th December, 1997
  9.  *
  10.  * (C) 1997 Nicholas Paul Sheppard
  11.  *
  12.  * This file is distributed under the GNU General Public License. See the
  13.  * file copying.txt for details.
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <math.h>
  20. #include "quotes.h"
  21. #include "threads.h"
  22. #include "types.h"
  23.  
  24.  
  25. void ThreadFindAllQuotes(THREADINFO *pti)
  26. /*
  27.  * Find all quotes in a database. The code of each quote is returned to the
  28.  * calling window by sending a QM_FOUND message to it with the first parameter
  29.  * set to be a pointer to the code. The thread sends a QM_DONE message to the
  30.  * caller when it is finished.
  31.  */
  32. {
  33.     HAB        hab;
  34.     HMQ        hmq;
  35.     QUOTESEARCH    qs;
  36.  
  37.     /* set up a message queue */
  38.     if ((hab = WinInitialize(0)) == NULLHANDLE)
  39.         return;
  40.     hmq = WinCreateMsgQueue(hab, 0);
  41.  
  42.     /* perform the search */
  43.     if (QuoteFullSearchInit(&(pti->pps->qdb), &qs)) {
  44.         WinSendMsg(pti->hwndCaller, QM_FOUND, MPFROMP(qs.szLastCode), 0);
  45.         while (QuoteFullSearchNext(&qs))
  46.             WinSendMsg(pti->hwndCaller, QM_FOUND, MPFROMP(qs.szLastCode), 0);
  47.     }
  48.     WinSendMsg(pti->hwndCaller, QM_DONE, 0, 0);
  49.  
  50.     /* clean up */
  51.     QuoteFullSearchEnd(&qs);
  52.     WinDestroyMsgQueue(hmq);
  53.     WinTerminate(hab);
  54.     _endthread();
  55. }
  56.  
  57.  
  58. void ThreadFindKeywords(THREADINFO *pti)
  59. /*
  60.  * Find all quotes in a database with the keyword given in the pData field of
  61.  * the THREADINFO structure. The code of each quote is returned to the calling
  62.  * window by sending a QM_FOUND message to it with the first parameter set to
  63.  * be a pointer to the code. The thread sends a QM_DONE message to the caller
  64.  * when it is finished.
  65.  */
  66. {
  67.     HAB        hab;
  68.     HMQ        hmq;
  69.     QUOTESEARCH    qs;
  70.  
  71.     /* set up a message queue */
  72.     if ((hab = WinInitialize(0)) == NULLHANDLE)
  73.         return;
  74.     hmq = WinCreateMsgQueue(hab, 0);
  75.  
  76.     /* perform the search */
  77.     switch (QuoteKeySearchInit(&(pti->pps->qdb), (char *)(pti->pData), &qs)) {
  78.         case -1:
  79.             WinSendMsg(pti->hwndCaller, QM_ERROR, 0, 0);
  80.             break;
  81.  
  82.         case 1:
  83.             WinSendMsg(pti->hwndCaller, QM_FOUND, MPFROMP(qs.szLastCode), 0);
  84.             while (QuoteKeySearchNext(&qs))
  85.                 WinSendMsg(pti->hwndCaller, QM_FOUND, MPFROMP(qs.szLastCode), 0);
  86.  
  87.         case 0:
  88.             WinSendMsg(pti->hwndCaller, QM_DONE, 0, 0);
  89.             break;
  90.     }
  91.  
  92.     /* clean up */
  93.     QuoteKeySearchEnd(&qs);
  94.     WinDestroyMsgQueue(hmq);
  95.     WinTerminate(hab);
  96.     _endthread();
  97. }
  98.  
  99.  
  100. void ThreadFindQuoteText(THREADINFO *pti)
  101. /*
  102.  * Find all quotes in a database containing the text given in the pData field
  103.  * of the THREADINFO structure. The code of each quote is returned to the
  104.  * calling window by sending a QM_FOUND message to it with the first parameter
  105.  * set to be a pointer to the code. The thread sends a QM_DONE message to the
  106.  * caller when it is finished.
  107.  */
  108. {
  109.     HAB        hab;
  110.     HMQ        hmq;
  111.     QUOTESEARCH    qs;
  112.  
  113.     /* set up a message queue */
  114.     if ((hab = WinInitialize(0)) == NULLHANDLE)
  115.         return;
  116.     hmq = WinCreateMsgQueue(hab, 0);
  117.  
  118.     /* perform the search */
  119.     switch (QuoteTextSearchInit(&(pti->pps->qdb), (char *)(pti->pData), &qs)) {
  120.         case -1:
  121.             WinSendMsg(pti->hwndCaller, QM_ERROR, 0, 0);
  122.             break;
  123.  
  124.         case 1:
  125.             WinSendMsg(pti->hwndCaller, QM_FOUND, MPFROMP(qs.szLastCode), 0);
  126.             while (QuoteTextSearchNext(&qs))
  127.                 WinSendMsg(pti->hwndCaller, QM_FOUND, MPFROMP(qs.szLastCode), 0);
  128.  
  129.         case 0:
  130.             WinSendMsg(pti->hwndCaller, QM_DONE, 0, 0);
  131.             break;
  132.     }
  133.  
  134.     /* clean up */
  135.     QuoteTextSearchEnd(&qs);
  136.     WinDestroyMsgQueue(hmq);
  137.     WinTerminate(hab);
  138.     _endthread();
  139. }
  140.  
  141.  
  142. void ThreadFindAuthorsQuotes(THREADINFO *pti)
  143. /*
  144.  * Find all quotes in a database from the author whose code is given in the
  145.  * pData field of the the THREADINFO structure. The code of each quote is
  146.  * returned to the calling window by sending a QM_FOUND message to it with the
  147.  * first parameter set to be a pointer to the code. The thread sends a QM_DONE
  148.  * message to the caller when it is finished.
  149.  */
  150. {
  151.     HAB        hab;
  152.     HMQ        hmq;
  153.     QUOTESEARCH    qs;
  154.  
  155.     /* set up a message queue */
  156.     if ((hab = WinInitialize(0)) == NULLHANDLE)
  157.         return;
  158.     hmq = WinCreateMsgQueue(hab, 0);
  159.  
  160.     /* perform the search */
  161.     if (QuoteAuthorSearchInit(&(pti->pps->qdb), (char *)(pti->pData), &qs)) {
  162.         WinSendMsg(pti->hwndCaller, QM_FOUND, MPFROMP(qs.szLastCode), 0);
  163.         while (QuoteAuthorSearchNext(&qs))
  164.             WinSendMsg(pti->hwndCaller, QM_FOUND, MPFROMP(qs.szLastCode), 0);
  165.     }
  166.     WinSendMsg(pti->hwndCaller, QM_DONE, 0, 0);
  167.  
  168.     /* clean up */
  169.     QuoteAuthorSearchEnd(&qs);
  170.     WinDestroyMsgQueue(hmq);
  171.     WinTerminate(hab);
  172.     _endthread();
  173. }
  174.  
  175.  
  176. void ThreadFindRandomQuote(THREADINFO *pti)
  177. /*
  178.  * Choose a random quote from a database. When one has been chosen, a QM_FOUND
  179.  * message is sent to the calling window with the first parameter set to be a
  180.  * pointer to the code chosen. If the database is empty, place NULL in this
  181.  * first parameter instead.
  182.  */
  183. {
  184.     HAB        hab;
  185.     HMQ        hmq;
  186.     QUOTESEARCH    qs;
  187.     int        i, iQuote;
  188.  
  189.     /* set up a message queue */
  190.     if ((hab = WinInitialize(0)) == NULLHANDLE)
  191.         return;
  192.     hmq = WinCreateMsgQueue(hab, 0);
  193.  
  194.     /* count the number of quotes in the database */
  195.     i = 0;
  196.     if (QuoteFullSearchInit(&(pti->pps->qdb), &qs)) {
  197.         i++;
  198.         while (QuoteFullSearchNext(&qs))
  199.             i++;
  200.     }
  201.     QuoteFullSearchEnd(&qs);
  202.  
  203.     if (i == 0)
  204.         /* empty database */
  205.         WinSendMsg(pti->hwndCaller, QM_FOUND, MPFROMP(NULL), 0);
  206.     else {
  207.         /* choose a random quote */
  208.         if (i > 1) {
  209.             srand(time(NULL));
  210.             iQuote = rand() % i;
  211.         } else
  212.             iQuote = 0;
  213.         for (QuoteFullSearchInit(&(pti->pps->qdb), &qs), i = 0; i < iQuote; QuoteFullSearchNext(&qs), i++);
  214.         WinSendMsg(pti->hwndCaller, QM_FOUND, MPFROMP(qs.szLastCode), 0);
  215.         QuoteFullSearchEnd(&qs);
  216.     }
  217.  
  218.     /* clean up */
  219.     WinDestroyMsgQueue(hmq);
  220.     WinTerminate(hab);
  221.     _endthread();
  222. }
  223.  
  224.  
  225. void ThreadImportQuotes(THREADINFO *pti)
  226. /*
  227.  * Import a quote database in pti->pps->pqdb. The pData entry in pti should
  228.  * point to an IMPORTINFO structure describing the import parameters.
  229.  */
  230. {
  231.     HAB        hab;
  232.     HMQ        hmq;
  233.     QUOTESEARCH    qs;
  234.     IMPORTINFO    *pii;
  235.     BOOL        bOkay;
  236.     char        szString[100];
  237.     QUOTEINFO    *pqi;
  238.     char        *pszText;
  239.  
  240.     /* set up a message queue */
  241.     if ((hab = WinInitialize(0)) == NULLHANDLE)
  242.         return;
  243.     hmq = WinCreateMsgQueue(hab, 0);
  244.  
  245.     /* import, quote by quote */
  246.     pii = (IMPORTINFO *)pti->pData;
  247.     if (QuoteFullSearchInit((QUOTEDB *)pii->pdb, &qs)) {
  248.         do {
  249.             bOkay = !QuoteExists(&(pti->pps->qdb), qs.szLastCode);
  250.             if (!bOkay) {
  251.                 if (pii->bAsk) {
  252.                     sprintf(szString, "%s exists. Replace it?", qs.szLastCode);
  253.                     if (WinMessageBox(HWND_DESKTOP, pti->hwndCaller, szString, "Confirmation", 0, MB_QUERY | MB_YESNO) == MBID_YES)
  254.                         bOkay = TRUE;
  255.                 } else if (pii->bReplace)
  256.                         bOkay = TRUE;
  257.             }
  258.             if (bOkay) {
  259.                 QuoteGetQuote((QUOTEDB *)pii->pdb, qs.szLastCode, &pqi, &pszText);
  260.                 QuoteAddQuote(&(pti->pps->qdb), qs.szLastCode, pqi, pszText);
  261.                 QuoteFreeQuote(&pqi, &pszText);
  262.             }
  263.         } while (QuoteFullSearchNext(&qs));
  264.     }
  265.  
  266.     /* tell the calling window that we're finished */
  267.     WinSendMsg(pti->hwndCaller, QM_DONE, NULL, NULL);
  268.  
  269.     /* clean up */
  270.     WinDestroyMsgQueue(hmq);
  271.     WinTerminate(hab);
  272.     _endthread();
  273. }
  274.