home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / APP_5 / XEL / XEL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  3.0 KB  |  157 lines

  1. /* 
  2. ** xel.c
  3. ** 
  4. ** Practical Algorithms for Image Analysis
  5. ** 
  6. ** Copyright (c) 1997 ????
  7. */
  8.  
  9. /*
  10. ** XEL
  11. **
  12. ** driver employing manipulations of doubly linked lists in llist.c
  13. **
  14. ** ref: 
  15. **    R. Sessions
  16. **    "Reusable Data Structures For C", sect. 7.1
  17. **    Prentice Hall, Englewood Cliffs, NJ, 1989;
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <malloc.h>
  23.  
  24. #include "ip.h"
  25.  
  26. struct emptype
  27. {
  28.     char ssnum[12];            /* empl social security number */
  29.     char name[40];            /* empl name */
  30. };
  31.  
  32.  
  33. #define    BUF_LENGTH    54
  34.  
  35. #define    START        's'        /* action code */
  36.  
  37.  
  38. /* function prototypes */
  39. extern  int rdemp(struct emptype *emp,char *code,FILE *file);
  40. extern  void prlist(struct linklist *list);
  41. extern  void add_to_list(struct linklist *newlist,struct emptype *emp);
  42. extern  void init_emplist(struct linklist *emplist);
  43. extern  void main(int argc,char **argv);
  44.  
  45.  
  46. /*
  47. ** read employee data from file emp.dat
  48. */
  49. int rdemp(struct emptype *emp, char *code, FILE *file)
  50. {
  51.     char buf[BUF_LENGTH];
  52.     int i;
  53.  
  54.     
  55.     for(i=0; i<BUF_LENGTH; i++)    *(buf+i) = '\0';
  56.     
  57.     if(fgets(buf, BUF_LENGTH, file) == NULL)
  58.         return(0);
  59.     sscanf(buf, "%12s%20s %c", emp->ssnum, emp->name, code);
  60.     return(1);
  61. }
  62.  
  63.  
  64. /*
  65. ** display list
  66. */
  67. void prlist(struct linklist *list)
  68. {
  69.     struct emptype *emp;
  70.     
  71.     llhead(list);
  72.     if( ll_length(list) != 0 )             /* list not empty */
  73.     {
  74.         do
  75.         {
  76.             emp = (struct emptype *)list->clp->item;
  77.             printf("  %s  %s\n", emp->ssnum, emp->name);
  78.  
  79.         }while( llnext(list) == True );
  80.     }
  81. }
  82.  
  83.  
  84.  
  85. /*
  86. ** add item to list
  87. */
  88. void add_to_list(struct linklist *list, struct emptype *emp)
  89. {
  90.     
  91.     lltail(list);        /* add to new list; if empty, init */
  92.     lladd((char *)emp, list);
  93.  
  94. }
  95.  
  96.  
  97. /*
  98. ** initialize list paramters
  99. */
  100. void init_emplist(struct linklist *emplist)
  101. {
  102.     
  103.     llzero(emplist);                /* init empty list */
  104.     llsetsize(sizeof(struct emptype), emplist);    
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. void main(int argc, char *argv[])
  112. {
  113.     
  114.     struct linklist curemp;
  115.     struct emptype employee, *emp=&employee;
  116.     char code;
  117.     int retval;
  118.     FILE *file;
  119.  
  120.     printf("xel: test driver for linked list functions\n\n");
  121.  
  122.     printf("...memory stats:\n");
  123.     printf("...size of struct emptype: %d\n", sizeof(struct emptype));
  124.     printf("...size of ptr to emptype: %d\n", sizeof(struct emptype *));
  125.     printf("...size of ptr to char:    %d\n", sizeof(char *));
  126.     
  127.     printf("\n...initialize lists\n");
  128.     init_emplist(&curemp);            /* init list of employees */
  129.     
  130.     printf("...open data file\n");
  131.     if( (file = fopen("emp.dat", "r")) == NULL )
  132.     {
  133.         puts("\nCould not open input file\n");
  134.         exit(1);
  135.     }
  136.  
  137. /* 
  138. ** scan file and update lists 
  139. */    
  140.     printf("...read test data from file\n");
  141.     while( (retval = rdemp(emp, &code, file)) != 0 )
  142.     {
  143.         printf("...name = %s, ssnum = %s, action code = %c\n",
  144.                 emp->name, emp->ssnum, code);
  145.         
  146.         if( code == START )
  147.             add_to_list(&curemp, emp);
  148.  
  149.     }
  150.     fclose(file);
  151.     
  152.     printf("\n");                /* display lists */
  153.     printf("list of active employees:\n");
  154.     prlist(&curemp);
  155.  
  156. }
  157.