home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / rolodex / part2 / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  8.3 KB  |  383 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/file.h>
  5. #include <ctype.h>
  6.  
  7. #include "sys5.h"
  8.  
  9. #ifdef TMC
  10. #include <ctools.h>
  11. #else
  12. #include "ctools.h"
  13. #endif
  14.  
  15. #include "datadef.h"
  16. #include "rolofiles.h"
  17.  
  18.  
  19. char *Field_Names[N_BASIC_FIELDS] = {
  20.         
  21.         "Name: ", "Work Phone: ", "Home Phone: ", "Company: ",
  22.         "Work Address: ", "Home Address: ", "Remarks: ", "Date Updated: "
  23.         
  24.      };
  25.  
  26. Ptr_Rolo_List Begin_Rlist = 0;
  27. Ptr_Rolo_List End_Rlist = 0;
  28. Ptr_Rolo_List Current_Entry = 0;
  29.  
  30. static char *rolofiledata;
  31.  
  32. read_rolodex (fd) int fd;
  33.  
  34. {
  35.   struct stat statdata;
  36.   int filesize,i,j,k,start_of_others,warning_given;
  37.   Ptr_Rolo_Entry newentry,oldentry,currententry;
  38.   Ptr_Rolo_List newlink,rptr;
  39.   char *next_field,*next_other,*oldname,*currentname;
  40.   char **other_pointers;
  41.   int n_entries = 0;
  42.   
  43.   /* find out how many bytes are in the file */
  44.   
  45.   fstat(fd,&statdata);
  46.   if ((filesize = statdata.st_size) == 0) {
  47.      return(0);
  48.   }
  49.  
  50.   /* create an array of characters that big */
  51.   
  52.   rolofiledata = rolo_emalloc(filesize);
  53.  
  54.   /* read them all in at once for efficiency */
  55.   
  56.   if (filesize != read(fd,rolofiledata,filesize)) {
  57.      fprintf(stderr,"rolodex read failed\n");
  58.      exit(-1);
  59.   }
  60.  
  61.   j = 0;
  62.   
  63.   /* for each entry in the rolodex file */
  64.   
  65.   while (j < filesize) {
  66.  
  67.       n_entries++;
  68.         
  69.       /* create the link and space for the data entry */
  70.         
  71.       newlink = new_link_with_entry();
  72.       newentry = get_entry(newlink);
  73.       if (j == 0) {
  74.          Begin_Rlist = newlink;
  75.          set_prev_link(newlink,0);
  76.          set_next_link(newlink,0);
  77.       }
  78.       else {
  79.           set_next_link(End_Rlist,newlink);
  80.           set_prev_link(newlink,End_Rlist);
  81.           set_next_link(newlink,0);
  82.       }
  83.       End_Rlist = newlink;
  84.  
  85.       /* locate each required field in the character array and change */
  86.       /* the ending line feed to a null.  Insert a pointer to the */
  87.       /* beginning of the field into the data entry */
  88.  
  89.       for (i = 0; i < N_BASIC_FIELDS; i++) {
  90.           next_field = rolofiledata + j;
  91.           while (rolofiledata[j] != '\n') {
  92.             j++;
  93.           }
  94.           rolofiledata[j] = '\0';
  95.           j++;
  96.           set_basic_rolo_field(i,newentry,next_field);
  97.       }
  98.  
  99.       /* the end of an entry is indicated by two adjacent newlines */
  100.  
  101.       if (rolofiledata[j] == '\n') {
  102.          j++;
  103.          newentry -> other_fields = 0;
  104.          continue;
  105.       }
  106.  
  107.       /* there must be additional, user-inserted fields. Find out how many. */
  108.  
  109.       start_of_others = j;
  110.       while (1) {
  111.         while (rolofiledata[j] != '\n') {
  112.           j++;
  113.         }
  114.         incr_n_others(newentry);
  115.         j++;
  116.         if (rolofiledata[j] == '\n') {
  117.            j++;
  118.            break;
  119.         }
  120.      }
  121.  
  122.      /* allocate an array of character pointers to hold these fields */
  123.  
  124.      other_pointers = (char **)rolo_emalloc(get_n_others(newentry)*sizeof(char *));
  125.  
  126.      /* separate each field and insert a pointer to it in the char array */
  127.  
  128.      k = start_of_others;
  129.      for (i = 0; i < get_n_others(newentry); i++) {
  130.          next_other = rolofiledata + k;
  131.          while (rolofiledata[k] != '\n') {
  132.            k++;
  133.          }
  134.          rolofiledata[k] = '\0';
  135.          other_pointers[i] = next_other;
  136.          k++;
  137.      }
  138.  
  139.      /* insert the pointer to this character array into the data entry */
  140.  
  141.      newentry -> other_fields = other_pointers;
  142.  
  143.   }
  144.  
  145.   /* check that all the entries are in alphabetical order by name */
  146.   
  147.   warning_given = 0;
  148.   rptr = get_next_link(Begin_Rlist);
  149.   while (rptr != 0) {
  150.     if (1 == compare_links(get_prev_link(rptr),rptr)) {
  151.        if (!warning_given) fprintf(stderr,"Warning, rolodex out of order\n");
  152.        warning_given = 1;
  153.        reorder_file = 1;
  154.     }
  155.     rptr = get_next_link(rptr);
  156.   }    
  157.     
  158.   return(n_entries);
  159.   
  160. }
  161.  
  162.  
  163. write_rolo_list (fp) FILE *fp; 
  164.  
  165. /* write the entire in-core rolodex to a file */
  166.  
  167. {
  168.  
  169.   Ptr_Rolo_List rptr;
  170.   Ptr_Rolo_Entry entry;
  171.   int j;
  172.  
  173.   rptr = Begin_Rlist;
  174.  
  175.   while (rptr != 0) {
  176.     entry = get_entry(rptr);
  177.     for (j = 0; j < N_BASIC_FIELDS; j++) {
  178.         fprintf(fp,"%s\n",get_basic_rolo_field(j,entry));
  179.     }
  180.     for (j = 0; j < get_n_others(entry); j++) {
  181.         fprintf(fp,"%s\n",get_other_field(j,entry));
  182.     }
  183.     fprintf(fp,"\n");
  184.     rptr = get_next_link(rptr);
  185.   }
  186.  
  187. }
  188.  
  189.  
  190. write_rolo (fp1,fp2) FILE *fp1; FILE *fp2;
  191.  
  192. {
  193.   write_rolo_list(fp1);
  194.   write_rolo_list(fp2);
  195. }
  196.  
  197.  
  198. display_basic_field (name,value,show,up) char *name; char *value; int show,up;
  199. {
  200.   int semi = 0;        
  201.   int i;
  202.   if (all_whitespace(value) && !show) return;
  203.   printf("%-25s",name);
  204.   while (*value != '\0') {
  205.     if (*value == ';') {
  206.        while (*++value == ' '); 
  207.        putchar('\n');
  208.        for (i = 0; i < (up ? 28 : 25); i++) putchar(' ');
  209.        semi = 1;
  210.     }
  211.     else {
  212.        semi = 0;
  213.        putchar(*value++);
  214.     }
  215.   }
  216.   putchar('\n');
  217. }
  218.  
  219.  
  220. display_other_field (fieldstring) char *fieldstring;
  221. {
  222.   int already_put_sep = 0;        
  223.   int count = 0;
  224.   int i;
  225.   while (*fieldstring != '\0') {
  226.     if (*fieldstring == ';' && already_put_sep) {
  227.        while (*++fieldstring == ' ');
  228.        putchar('\n');
  229.        for (i = 0; i < 25; i++) putchar(' ');
  230.        continue;
  231.     }
  232.     putchar(*fieldstring);
  233.     count++;
  234.     if (*fieldstring == ':' && !already_put_sep) {
  235.        for (i = count; i < 24; i++) putchar(' ');
  236.        already_put_sep = 1;
  237.     }
  238.     fieldstring++;
  239.   }
  240.   putchar('\n');
  241. }
  242.  
  243.  
  244. summarize_entry_list (rlist,ss) Ptr_Rolo_List rlist; char *ss;
  245.  
  246. /* print out the Name field for each entry that is tagged as matched */
  247. /* and number each entry. */
  248.  
  249. {
  250.   int count = 1;
  251.   clear_the_screen();
  252.   printf("Entries that match '%s' :\n\n",ss);
  253.   while (rlist != 0) {
  254.     if (get_matched(rlist)) {
  255.        printf (
  256.           "%d. \t%s\n",
  257.           count++,
  258.           get_basic_rolo_field((int) R_NAME,get_entry(rlist))
  259.        );
  260.     }
  261.     rlist = get_next_link(rlist);    
  262.   }
  263.   putchar('\n');
  264. }
  265.  
  266.  
  267. display_field_names ()
  268.  
  269. /* display and number each standard field name. */
  270.  
  271. {
  272.   int j;
  273.   char *name;
  274.   clear_the_screen();
  275.   for (j = 0; j < N_BASIC_FIELDS - 1; j++) {        
  276.       name = Field_Names[j];        
  277.       printf("%d. ",j+1);
  278.       while (*name != ':') putchar(*name++);
  279.       putchar('\n');
  280.   }
  281.   printf("%d. ",N_BASIC_FIELDS);
  282.   printf("A user created item name\n\n");
  283. }  
  284.   
  285.  
  286. display_entry (entry) Ptr_Rolo_Entry entry;
  287.  
  288. {
  289.   int j,n_others;
  290.   char *string;
  291.   
  292.   clear_the_screen();
  293.   
  294.   /* display the standard fields other than Date Updated */
  295.   
  296.   for (j = 0; j < N_BASIC_FIELDS - 1; j++) {
  297.       string = get_basic_rolo_field(j,entry);
  298.       display_basic_field(Field_Names[j],string,0,0);
  299.   }        
  300.       
  301.   /* display any additional fields the user has defined for this entry */
  302.   
  303.   n_others = get_n_others(entry);
  304.   for (j = 0; j < n_others; j++) {
  305.       string = get_other_field(j,entry);
  306.       display_other_field(string);
  307.    }
  308.  
  309.    /* display the Date Updated field */
  310.    
  311.    j = N_BASIC_FIELDS - 1;
  312.    display_basic_field(Field_Names[j],get_basic_rolo_field(j,entry),0,0);
  313.    fprintf(stdout,"\n");
  314.  
  315. }
  316.  
  317.  
  318. display_entry_for_update (entry) Ptr_Rolo_Entry entry;
  319.  
  320. /* same as display_entry, except each item is numbered and the Date Updated */
  321. /* item is not displayed */
  322.  
  323. {
  324.   int j,n_others;
  325.   char *string;
  326.   int count = 1;
  327.   
  328.   clear_the_screen();
  329.   
  330.   for (j = 0; j < N_BASIC_FIELDS - 1; j++) {
  331.       string = get_basic_rolo_field(j,entry);
  332.       printf("%d. ",count++);
  333.       display_basic_field(Field_Names[j],string,1,1);
  334.   }        
  335.       
  336.   n_others = get_n_others(entry);
  337.   for (j = 0; j < n_others; j++) {
  338.       string = get_other_field(j,entry);
  339.       printf("%d. ",count++);
  340.       display_other_field(string);
  341.   }
  342.   
  343.   printf("%d. Add a new user defined field\n",count);
  344.  
  345.   fprintf(stdout,"\n");
  346.  
  347. }
  348.  
  349.  
  350. int cathelpfile (filepath,helptopic,clear) 
  351.  
  352.   char *filepath, *helptopic; 
  353.   int clear;
  354.  
  355. {
  356.   FILE *fp;          
  357.   char buffer[MAXLINELEN];
  358.   if (clear) clear_the_screen();
  359.   if (NULL == (fp = fopen(filepath,"r"))) {
  360.      if (helptopic) {
  361.         printf("No help available on %s, sorry.\n\n",helptopic); 
  362.      }
  363.      else {
  364.         fprintf(stderr,"Fatal error, can't open %s\n",filepath);
  365.         exit(-1);
  366.      }
  367.      return;
  368.   }
  369.   while (NULL != fgets(buffer,MAXLINELEN,fp)) printf("%s",buffer);  
  370.   printf("\n");
  371.   fclose(fp);
  372.   return;
  373. }
  374.  
  375.  
  376. any_char_to_continue ()
  377. {
  378.   char buffer[80];
  379.   printf("RETURN to continue: ");
  380.   fgets(buffer,80,stdin);
  381.   return;
  382. }
  383.