home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_07 / 8n07119a < prev    next >
Text File  |  1990-06-19  |  3KB  |  132 lines

  1. /*  Listing 5 */
  2. /*-----------------  PHLIST1.H ---------------*/
  3. #include "arrylist.h"
  4.  
  5. typedef struct phone_entry {
  6.     char last_name[21], first_name[11], phone_no[14];
  7. } PHONE_ENTRY;
  8.  
  9. #define PHONE_LIST_CLASS ARRAY_LIST_CLASS \
  10.              PHONE_ENTRY *data; \
  11.              void (*sort)(struct phone_list *);
  12.  
  13. typedef struct phone_list {
  14.     PHONE_LIST_CLASS
  15. } PHONE_LIST;
  16.  
  17. PHONE_LIST *new_phone_list();
  18. void destroy_phone_list(PHONE_LIST *);
  19.  
  20. /* ------------------ PHLIST1.C ----------------*/
  21. #include "phlist1.h"
  22. #include <string.h>
  23. #include <conio.h>
  24. #include <stdlib.h>
  25.  
  26. static void phone_list_memory_error(char *fun) {
  27.     fprintf(stderr,
  28.     "\nMemory Error in Function %s <Press a Key>\n", fun);
  29.     getch(); exit(1);
  30. }
  31.  
  32. static unsigned find(PHONE_LIST *this,char *srch_last_name){
  33.     PHONE_ENTRY *pe;
  34.     int orig;
  35.  
  36.     orig = this->curr;
  37.     while(! this->at_end(this)) {
  38.        pe = this->current(this);
  39.        if ( stricmp(pe->last_name,srch_last_name) == 0)
  40.           return(TRUE);
  41.        else if (stricmp(pe->last_name,srch_last_name) > 0) {
  42.           this->curr = orig;
  43.           return(FALSE);
  44.        }
  45.        else
  46.            this->next(this);
  47.     }
  48.     pe = this->current(this);
  49.     if ( stricmp(pe->last_name,srch_last_name) == 0)
  50.         return(TRUE);
  51.     this->curr = orig;
  52.     return(FALSE);
  53. }
  54.  
  55. static display(PHONE_LIST *this) {
  56.     PHONE_ENTRY *pe;
  57.  
  58.     pe = this->current(this);
  59.     if (pe != NULL) {
  60.         printf("%-20s, %-10s  -  %-13s\n", pe->last_name,
  61.         pe->first_name, pe->phone_no);
  62.     }
  63. }
  64.  
  65. static void add_member(PHONE_LIST *this, PHONE_ENTRY *pe) {
  66.     this->data = realloc(this->data,
  67.     sizeof(PHONE_ENTRY) * (this->tot_members + 1));
  68.  
  69.     if (this->data == NULL)
  70.        phone_list_memory_error("phone_list: add_member");
  71.  
  72.     memmove(this->data + this->tot_members, pe,
  73.     sizeof(PHONE_ENTRY));
  74.     ++(this->tot_members);
  75.     this->sort(this);
  76. }
  77.  
  78. static void replace_member(PHONE_LIST *this,
  79. PHONE_ENTRY *pe){
  80.  if (this->data != NULL)
  81.    memmove(this->data + this->curr, pe,sizeof(PHONE_ENTRY));
  82. }
  83.  
  84. static PHONE_LIST *current(PHONE_LIST *this) {
  85.     if (! this->at_end(this) && this->data != NULL)
  86.         return(this->data + this->curr);
  87.      else
  88.         return(NULL);
  89. }
  90.  
  91. static int pe_comp(PHONE_ENTRY *pe1, PHONE_ENTRY *pe2) {
  92.     int ret;
  93.     ret = stricmp(pe1->last_name, pe2->last_name);
  94.     if (ret == 0)
  95.        return(stricmp(pe1->first_name, pe2->first_name));
  96.     return(ret);
  97. }
  98.  
  99. static sort(PHONE_LIST *this) {
  100.     qsort(this->data, (size_t) this->tot_members,
  101.     sizeof(PHONE_ENTRY),  pe_comp);
  102. }
  103.  
  104. PHONE_LIST *new_phone_list() {
  105. ARRAY_LIST *al;
  106. PHONE_LIST *this;
  107.  
  108. al = new_array_list();
  109. if (al == NULL)
  110.        return(NULL);
  111.  
  112. this = calloc(1,sizeof(PHONE_LIST));
  113. if (this == NULL) {
  114. destroy_array_list(al);
  115.     return(NULL);
  116. }
  117. memmove(this,al,sizeof(ARRAY_LIST));
  118. free(al);
  119.  
  120. this->find = find; this->display = display;
  121. this->add_member = add_member;
  122. this->replace_member = replace_member;
  123. this->current = current; this->sort = sort;
  124. return(this);
  125. }
  126.  
  127. void destroy_phone_list(PHONE_LIST *this) {
  128. if (this->data)
  129.    free(this->data);
  130. destroy_array_list(this);
  131. }
  132.