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

  1. /*----------------------------------------------
  2. Test code for cirque.c
  3. You are free to use any or all for any purpose
  4. whatsoever.
  5. --------------------
  6. Conrad Thornton
  7. -----------------------------------------------*/
  8. #include <stdio.h>
  9. #include <malloc.h>
  10. #include <string.h>
  11.  
  12. extern int setupque(int);
  13. extern int quewrite(char *,int);
  14. extern int queread(char **,int *);
  15. extern int quekill(void);
  16.  
  17. typedef struct {
  18.  char name[50];
  19.  int  agelie;
  20.  }xrs;
  21. /*----------------------------------------------*/
  22. main(argc,argv)
  23. int  argc;
  24. char *argv[];
  25. {
  26.        int  j,k,ecode,snum;
  27.        char buff[50],*gets();
  28.  
  29.        if(argc < 2) {
  30.       printf("Usage: program-name Queue-Size\n");
  31.       exit(1);
  32.        }
  33.        snum = atoi(argv[1]);
  34.        if((ecode = setupque(snum)) != 0) {
  35.        printf("Cannot malloc for Que\n");
  36.        exit(1);
  37.        }
  38.        printf("Queue Size = %d\n",snum);
  39.        while(1) {
  40.       ecode = 0;
  41.       printf("1=Ins-1-Record 2=Read-All-Data 3=Quit ");
  42.       j = getch();
  43.       printf("\n");
  44.       if(j == '3') {
  45.          if((ecode = quekill()) != 0) {
  46.         printf("\nTHE QUEUE IS NOT EMPTY. CANNOT KILL IT\n");
  47.         printf("SELECT 2=Read-All-Data  then 3=Quit\n\n");
  48.          }
  49.          else
  50.         break;
  51.       }
  52.       else if(j == '2')
  53.          ecode = read_em();
  54.       else if(j == '1') {
  55.          printf("\n1=char string  2=ints(6)  3=struct ");
  56.          k = getch();
  57.          k -= 48;
  58.          printf("\n");
  59.          switch(k)
  60.          {
  61.          case 1: printf("Enter String ..Limit 48 chars\n");
  62.              gets(buff);
  63.              buff[48]=NULL;
  64.              ecode = insert(buff,snum,1);
  65.              break;
  66.          case 2: printf("Enter seed number\n");
  67.              gets(buff);
  68.              snum = atoi(buff);
  69.              ecode = insert(buff,snum,2);
  70.              break;
  71.          case 3: printf("Enter int value for struct\n");
  72.              gets(buff);
  73.              snum = atoi(buff);
  74.              printf("Enter string for name field..Limit 48 chars\n");
  75.              gets(buff);
  76.              buff[48]=NULL;
  77.              ecode = insert(buff,snum,3);
  78.          }
  79.       }
  80.       if(ecode != 0)
  81.          printf("Error: Errcode = < %d >\n",ecode);
  82.        }
  83. }
  84. /*-------------------------------------------------------------*/
  85. int insert(str,val,key)
  86. char *str;
  87. int  val,key;
  88. {
  89.        int  x,z,ecode;
  90.        char *pc,*getptc();
  91.        int  *pi,*px,*getpti();
  92.        xrs  *pxrs;
  93.  
  94.        ecode = 0;
  95.        switch(key)
  96.        {
  97.        case 1: if((pc = getptc(str)) == (char *)NULL) {
  98.           printf("Could not malloc for string\n");
  99.           ecode = -4;
  100.            }
  101.            else if((x = quewrite(pc,1)) != 0) {
  102.           printf("Quewrite failed on string load\n");
  103.           ecode = -5;
  104.            }
  105.            break;
  106.        case 2: if((pi = getpti(6)) == (int *)NULL) {
  107.           printf("Could not malloc for 6 int's\n");
  108.           ecode = -6;
  109.            }
  110.            else {
  111.           z  = val;
  112.           px = pi;
  113.           x = 0;
  114.           while(x < 6) {   /* just assigns nums for test */
  115.              *px = z;
  116.              ++px;
  117.              ++x;
  118.              ++z;
  119.           }
  120.           if((x = quewrite((char *)pi,2)) != 0) {
  121.              printf("Quewrite failed on int load\n");
  122.              ecode = -7;
  123.           }
  124.            }
  125.            break;
  126.        case 3: pxrs = (xrs *) malloc(sizeof(xrs));
  127.            if(! pxrs) {
  128.           printf("Could not malloc for struct\n");
  129.           ecode = -8;
  130.            }
  131.            else {
  132.           strcpy(pxrs->name,str);
  133.           pxrs->agelie = val;
  134.           if((x = quewrite((char *)pxrs,3)) != 0) {    /* xrs type 3 */
  135.              printf("Quewrite failed on struct load\n");
  136.              ecode = -9;
  137.           }
  138.            }
  139.        }
  140.        return(ecode);
  141. }
  142. /*-----------------------------------------------------------------*/
  143. int read_em() /* This temp fun ALWAYS returns QUEEMPTY (-3) at end */
  144. {
  145.        int  x,z,ecode;
  146.        char *pc,*getptc();
  147.        int  *pi,*px,*getpti();
  148.        xrs  *pxrs;
  149.  
  150.        do {
  151.       ecode = queread(&pc,&x);
  152.       if(ecode >= 0) {              /* queue is not empty */
  153.          if(x == 1) {            /* it's our char string */
  154.         printf("String = %s\n",pc);
  155.         free(pc);
  156.          }
  157.          else if(x == 2) {               /* its our int's */
  158.         pi = px = (int *)pc;
  159.         x = 0;
  160.         while(x < 6) {
  161.            printf("%d ",*px);
  162.            ++x;
  163.            ++px;
  164.         }
  165.         printf("\n");
  166.         free(pi);
  167.          }
  168.          else if(x == 3) {              /* it's our structure */
  169.         pxrs = (xrs *) pc;
  170.         printf("Name = %s Age = %d\n",pxrs->name,pxrs->agelie);
  171.         free(pxrs);
  172.          }
  173.       }
  174.       else {
  175.          printf("Ecode returned val = %d\n",ecode);
  176.          break;
  177.       }
  178.        }while(1);
  179.        return(ecode);
  180. }
  181. /*------------------------------------------------------------------*/
  182. char *getptc(str)
  183. char *str;
  184. {
  185.     int  ln;
  186.     char *p;
  187.  
  188.     ln = strlen(str);
  189.     if(ln == 0)
  190.        return((char *)NULL);
  191.     if((p = (char *)malloc(ln+1)) != (char *)NULL)
  192.        memcpy(p,str,ln+1);
  193.     return(p);
  194. }
  195. /*-----------------------------------------------------------------*/
  196. int *getpti(many)
  197. int many;
  198. {
  199.     int *pix;
  200.  
  201.     pix = (int *)malloc(many*sizeof(int));
  202.     return(pix);
  203. }
  204. /*-----------------------------------------------------------------*/
  205.