home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 344_01 / cirque.c < prev    next >
C/C++ Source or Header  |  1991-03-05  |  3KB  |  125 lines

  1. /*-------------------------------------------------
  2. 03/02/91
  3. You are free to use the code in this file for ANY
  4. purpose whatsoever.
  5. If you can sell it.....go for it.
  6. ---------------------
  7. Conrad Thornton
  8. RR1 Box 87C
  9. Downsville,La. 71234
  10. --------------------------------------------------*/
  11. #include <stdio.h>
  12. #include <malloc.h>
  13. #include <string.h>
  14.  
  15. #define ITEMSIZE (sizeof(char) + sizeof(char *))
  16. #define ITSOK      0
  17. #define ERROR     -1
  18. #define QUEFULL  -2
  19. #define QUEEMPTY -3
  20.  
  21. static char *cir_que,*writehead,*readtail,*queend;
  22.  
  23. int setupque(int);
  24. int quewrite(char *,int);
  25. int queread(char **,int *);
  26. int quekill(void);
  27.  
  28. static int QUESTART;
  29. static int ITEMSINQUE;
  30. /*-------------------------------------------------*/
  31. int setupque(qsize)
  32. int qsize;
  33. {
  34.     int quesize;
  35.  
  36.     quesize = qsize * ITEMSIZE;
  37.     cir_que = (char *)malloc(quesize+1);
  38.     if(cir_que == (char *)NULL)
  39.        return(ERROR);
  40.     memset(cir_que,'\0',quesize);
  41.     readtail  = cir_que;
  42.     writehead = readtail;
  43.     queend      = readtail+quesize;
  44.     QUESTART  = 1;
  45.     ITEMSINQUE= 0;
  46.     return(ITSOK);
  47. }
  48. /*-------------------------------------------------*/
  49. int quewrite(data,type)
  50. char *data;
  51. int  type;
  52. {
  53.     if(writehead == readtail && ! QUESTART)
  54.        return(QUEFULL);
  55.     if(writehead == queend) {
  56.        if(readtail == cir_que)
  57.           return(QUEFULL);
  58.        writehead =    cir_que;
  59.     }
  60.     *writehead++ = (char) type;/* ins type and data */
  61.     memcpy(writehead,&data,sizeof(char *));
  62.     writehead += sizeof(char *);
  63.     QUESTART   = 0;
  64.     ITEMSINQUE++;
  65.     return(ITSOK);
  66. }
  67. /*------------------------------------------------------*/
  68. int queovwrite(data,type)
  69. char *data;
  70. int  type;
  71. {
  72.     char *p;
  73.     int  ecode,j;
  74.  
  75.     if(writehead == queend)
  76.        writehead = cir_que;
  77.     if(readtail  == queend)
  78.        readtail  = cir_que;
  79.     if(writehead == readtail && ! QUESTART) {
  80.        ecode = queread(&p,&j); /* free previous malloc */
  81.        if(ecode < 0)
  82.           return(ERROR);
  83.        free(p);
  84.        readtail += ITEMSIZE;
  85.     }          /* now we can write in the old space */
  86.     *writehead++ = (char) type;
  87.     memcpy(writehead,&data,sizeof(char *));
  88.     writehead += sizeof(char *);
  89.     QUESTART   = 0;
  90.     ITEMSINQUE++;
  91.     return(ITSOK);
  92. }
  93. /*--------------------------------------------------------*/
  94. int queread(p,type)
  95. char **p;
  96. int  *type;
  97. {
  98.     if(readtail == writehead)
  99.        return(QUEEMPTY);
  100.     if(readtail == queend) {
  101.        if(writehead == cir_que)
  102.           return(QUEEMPTY);
  103.        readtail = cir_que;
  104.     }
  105.     *type = (int) *readtail++;
  106.     memcpy(p,readtail,sizeof(char *));
  107.     readtail += sizeof(char *);
  108.     ITEMSINQUE--;
  109.     if(ITEMSINQUE == 0) {
  110.        readtail  = cir_que;
  111.        writehead = cir_que;
  112.        QUESTART  = 1;
  113.     }
  114.     return(ITSOK);
  115. }
  116. /*------------------------------------------------------*/
  117. int quekill()
  118. {
  119.     if(ITEMSINQUE)
  120.        return(ERROR);
  121.     free(cir_que);
  122.     return(ITSOK);
  123. }
  124. /*-----------------------------------------------------*/
  125.