home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk11 / tk3 / comtalk / circleq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-20  |  3.2 KB  |  141 lines

  1. /*
  2.     Circular Queue buffer implementation (which gets read by AVIO module)
  3.     Created by Microsoft Corporation, 1989
  4. */
  5. #define INCL_DOSSEMAPHORES
  6. #include <os2.h>        /* Need USHORT for global.h */
  7. #include <string.h>        /* One strcpy call */
  8. #include "global.h"
  9. #include "circleq.h"
  10.  
  11. #define    TIMEOUT    1000L        /* A second */
  12.  
  13. LineInfo aliRing[QUEUESIZE];    /* The Circular Queue...*/
  14. int  iHead, iTail;
  15. BOOL fFirst;            /* Are we just starting? */
  16. LONG lSemMyQueue;        /* Queue lock */
  17.  
  18. void LineCopy(Line, Line);
  19. void QueFill(void);
  20.  
  21. #define QueLock()   DosSemRequest(&lSemMyQueue, -1L)
  22. #define QueUnlock() DosSemClear(&lSemMyQueue)
  23.  
  24. #define Fix(n) (((n) >= 0) ? (n) : ((n) + QUEUESIZE))
  25. #define Circle(x)    ((x) % QUEUESIZE)
  26. #define Incr(x)     (x = Circle(x + 1))
  27. #define Decr(x)        (x = (x > 0) ? (x - 1) : (QUEUESIZE - 1))
  28.  
  29. void QueFill(void) {
  30.     int i, j;
  31.  
  32.     for (i = 0; i < 25; i++) {
  33.     aliRing[i].cch = MAXLINELEN;
  34.     for (j = 0; j < MAXLINELEN; j++)
  35.         aliRing[i].szText[j] = (char) ((i * j) % 10) + '0';
  36.     }
  37.     iHead = 0; iTail = 24;
  38. }
  39.  
  40. void QueInit(void) {
  41.     int i;
  42.  
  43.     fFirst = TRUE;
  44.     QueLock();
  45.     iHead = 0; iTail = 0;
  46.     for (i = 0; i < QUEUESIZE; i++) aliRing[i].cch = 0;
  47.     QueUnlock();
  48. }
  49.  
  50. void QueAdvance(int n) {
  51.     QueLock();
  52.     iHead = Circle(iHead + n);
  53.     QueUnlock();
  54. }
  55.  
  56. Line QueQuery(int LineNum) { return &aliRing[Circle(iHead + LineNum)]; }
  57.  
  58. BOOL QueInsertLine(Line pli) {
  59. /*
  60.     Return FALSE if we try to overwrite the head
  61. */
  62.     QueLock();
  63.     /*
  64.     Initialize the queue
  65.     */
  66.     if (fFirst) fFirst = FALSE;
  67.     /*
  68.     Increment TAIL, act if queue full
  69.     Overwrite if last entry was incomplete
  70.     */
  71.     else if (aliRing[iTail].fComplete && (Incr(iTail) == iHead)) {
  72.     /*
  73.         We are overflowing...
  74.     */
  75.     Decr(iTail);
  76.     QueUnlock();
  77.     return FALSE;
  78.     }
  79.     /*
  80.     Insert the element
  81.     */
  82.     LineCopy(pli, &aliRing[iTail]);
  83.     QueUnlock();
  84.     return TRUE;
  85. }
  86.  
  87. BOOL QueCompleteLine(void) { return aliRing[iTail].fComplete; }
  88.  
  89. void LineCopy(Line pliSrc, Line pliDst) {
  90.     int i;
  91.  
  92.     pliDst->fDrawn        = pliSrc->fDrawn;
  93.     pliDst->fComplete        = pliSrc->fComplete;
  94.     pliDst->cch            = pliSrc->cch;
  95.     for (i = 0; i < pliSrc->cch; i++) pliDst->szText[i] = pliSrc->szText[i];
  96. }
  97.  
  98. int QueUpdateHead(int nRows, BOOL bPage, BOOL bPaging) {
  99.     int i, nLines;
  100.  
  101.     nLines = Fix(Circle(iTail - iHead));
  102.     nLines = (nLines >= nRows) ? (nLines - nRows + 1) : 0;
  103.     if ((nLines = Min(nLines, nRows)) > 0) {
  104.     if (bPage) {
  105.         if (nLines < nRows) {
  106.         QueLock();
  107.         for (i = nLines; i < nRows; i++)
  108.             aliRing[Circle(iHead + nRows + i)].cch = 0;
  109.         QueUnlock();
  110.         }
  111.         nLines = nRows;
  112.     }
  113.     else if (bPaging) nLines = 0;
  114.     QueLock();
  115.     iHead = Circle(iHead + nLines);
  116.     QueUnlock();
  117.     }
  118.     return nLines;
  119. }
  120.  
  121. Line QueLastLine(void) {
  122.     QueLock();
  123.     aliRing[iTail].szText[aliRing[iTail].cch] = '\0';
  124.     QueUnlock();
  125.     return &aliRing[iTail];
  126. }
  127.  
  128. int QuePageUp(int nRows) {
  129.     int i, nLines;
  130.  
  131.     QueLock();
  132.     nLines = Min((QUEUESIZE - 1) - Fix(Circle(iTail - iHead)), nRows);
  133.     if (nLines) {
  134.     iHead = Fix(Circle(iHead - nLines));
  135.     for (i = 0; i < nLines; i++)
  136.         aliRing[Circle(iHead + nRows + i)].fDrawn = FALSE;
  137.     }
  138.     QueUnlock();
  139.     return nLines;
  140. }
  141.