home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / rolodex / part03 / io.c next >
Encoding:
C/C++ Source or Header  |  1988-01-30  |  10.5 KB  |  466 lines

  1. /* io.c */
  2. #include <stdio.h>
  3. #ifdef UNIX
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #ifdef BSD
  7. #include <sys/file.h>
  8. #else
  9. #include <fcntl.h>
  10. #endif
  11. #endif
  12.  
  13. #ifdef VMS
  14. #include <types.h>
  15. #include <stat.h>
  16. #include <file.h>
  17. #endif
  18.  
  19. #ifdef MSDOS
  20. #    ifdef MSC
  21. #    include <sys/types.h>
  22. #    endif    /* MSC */
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #endif
  26.  
  27. #include <ctype.h>
  28.  
  29. #ifdef TMC
  30. #include <ctools.h>
  31. #else
  32. #include "ctools.h"
  33. #endif
  34.  
  35. #include "rolofilz.h"
  36. #include "datadef.h"
  37.  
  38.  
  39. char *Field_Names[N_BASIC_FIELDS] = {
  40.         
  41.         "Name: ", "Work Phone: ", "Home Phone: ", "Company: ",
  42.         "Work Address: ", "Home Address: ", "Remarks: ", "Date Updated: "
  43.         
  44.      };
  45.  
  46. Ptr_Rolo_List Begin_Rlist = 0;
  47. Ptr_Rolo_List End_Rlist = 0;
  48. Ptr_Rolo_List Current_Entry = 0;
  49.  
  50. static char *rolofiledata;
  51.  
  52. /*
  53.  * The following has been added to accomplish two goals:
  54.  *
  55.  * 1)  Document all help files expected in the system, to avoid/identify
  56.  *     lost elements in distribution.
  57.  *
  58.  * 2)  Allow a flexible, easily-modified way of changing filenames as
  59.  *     necessary/desired to comply with different operating systems.
  60.  */
  61. char *hlpfiles[] = {
  62.     "addhelp.hlp",    /* ADDHELP        */
  63.     "addinfo.hlp",    /* ADDINFO        */
  64.     "confirm.hlp",    /* CONFIRMHELP        */
  65.     "entrymnu.hlp",    /* ENTRYMENU        */
  66.     "escan.hlp",    /* ESCANHELP        */
  67.     "esearch.hlp",    /* ESEARCHHELP          */
  68.     "fldsrch.hlp",    /* FIELDSEARCHHELP      */
  69. #ifdef UNIX
  70.     "lockinfo.unx",    /* LOCKINFO            */
  71. #endif
  72. #ifdef VMS
  73.     "lockinfo.vms",    /* LOCKINFO            */
  74. #endif
  75. #ifdef MSDOS
  76.     "lockinfo.dos",    /* LOCKINFO            */
  77. #endif
  78.     "mainmenu.hlp",    /* MAINMENU        */
  79.     "mnymtch.hlp",    /* MANYMATCHHELP         */
  80.     "moption.hlp",    /* MOPTIONHELP            */
  81.     "moptions.hlp",    /* MOPTIONSHELP            */
  82.     "moreflds.hlp",    /* MOREFIELDSHELP        */
  83.     "newadd.hlp",    /* NEWADDHELP             */
  84.     "otherfmt.hlp",    /* OTHERFORMATHELP     */
  85.     "pkentry.hlp",    /* PICKENTRYHELP         */
  86.     "pkntmenu.hlp",    /* PICKENTRYMENU    */
  87.     "poptmenu.hlp",    /* POPTIONMENU             */
  88.     "poptions.hlp",    /* POPTIONSHELP             */
  89.     "srchstr.hlp",    /* SEARCHSTRINGHELP      */
  90.     "update.hlp",    /* UPDATEHELP            */
  91.     "updatmnu.hlp",    /* UPDATEMENU           */
  92.     "usrfld.hlp",    /* USERFIELDHELP         */
  93. };
  94.  
  95.  
  96. read_rolodex (fd) int fd;
  97.                                              
  98. {
  99.   struct stat statdata;
  100.   long filesize;
  101.   int i,j,k,start_of_others,warning_given;
  102.   Ptr_Rolo_Entry newentry;
  103.   Ptr_Rolo_List newlink,rptr;
  104.   char *next_field,*next_other;
  105.   char **other_pointers;
  106.   int n_entries = 0;
  107.  
  108.   /* find out how many bytes are in the file */
  109.  
  110.   fstat(fd,&statdata);
  111.   if ((filesize = statdata.st_size) == 0) {
  112.      return(0);
  113.   }
  114.  
  115.   /* create an array of characters that big */
  116.   
  117.   rolofiledata = rolo_emalloc(filesize);
  118.  
  119.   /* read them all in at once for efficiency */
  120.   
  121. #ifdef MSDOS
  122.   /*
  123.    * Unlike Unix, MS-DOS compilers make a distinction between text and
  124.    * binary files.  Unfortunately, this means that in text mode, the file
  125.    * size is reported by the stat call won't necessarily match the value
  126.    * reported by the read, since there is CR/LF character translation.
  127.    * So, the best we can hope for here is that a failed read will give a
  128.    * zero or negative return value...
  129.    */
  130.   if ((filesize = read(fd,rolofiledata,filesize)) <= 0) {
  131. #else
  132.   if (filesize != read(fd,rolofiledata,filesize)) {
  133. #endif
  134.      fprintf(stderr,"rolodex read failed\n");
  135.      exit(-1);
  136.   }
  137.  
  138.   j = 0;
  139.   
  140.   /* for each entry in the rolodex file */
  141.   
  142.   while (j < filesize) {
  143.  
  144.       n_entries++;
  145.         
  146.       /* create the link and space for the data entry */
  147.         
  148.       newlink = new_link_with_entry();
  149.       newentry = get_entry(newlink);
  150.       if (j == 0) {
  151.          Begin_Rlist = newlink;
  152.          set_prev_link(newlink,0);
  153.          set_next_link(newlink,0);
  154.       }
  155.       else {
  156.           set_next_link(End_Rlist,newlink);
  157.           set_prev_link(newlink,End_Rlist);
  158.           set_next_link(newlink,0);
  159.       }
  160.       End_Rlist = newlink;
  161.  
  162.       /* locate each required field in the character array and change */
  163.       /* the ending line feed to a null.  Insert a pointer to the */
  164.       /* beginning of the field into the data entry */
  165.  
  166.       for (i = 0; i < N_BASIC_FIELDS; i++) {
  167.           next_field = rolofiledata + j;
  168.           while (rolofiledata[j] != '\n') {
  169.             j++;
  170.           }
  171.           rolofiledata[j] = '\0';
  172.           j++;
  173.           set_basic_rolo_field(i,newentry,next_field);
  174.       }
  175.  
  176.       /* the end of an entry is indicated by two adjacent newlines */
  177.  
  178.       if (rolofiledata[j] == '\n') {
  179.          j++;
  180.          newentry -> other_fields = 0;
  181.          continue;
  182.       }
  183.  
  184.       /* there must be additional, user-inserted fields. Find out how many. */
  185.  
  186.       start_of_others = j;
  187.       while (1) {
  188.         while (rolofiledata[j] != '\n') {
  189.           j++;
  190.         }
  191.         incr_n_others(newentry);
  192.         j++;
  193.         if (rolofiledata[j] == '\n') {
  194.            j++;
  195.            break;
  196.         }
  197.      }
  198.  
  199.      /* allocate an array of character pointers to hold these fields */
  200.  
  201.      other_pointers = (char **)rolo_emalloc(get_n_others(newentry)*sizeof(char *));
  202.  
  203.      /* separate each field and insert a pointer to it in the char array */
  204.  
  205.      k = start_of_others;
  206.      for (i = 0; i < get_n_others(newentry); i++) {
  207.          next_other = rolofiledata + k;
  208.          while (rolofiledata[k] != '\n') {
  209.            k++;
  210.          }
  211.          rolofiledata[k] = '\0';
  212.          other_pointers[i] = next_other;
  213.          k++;
  214.      }
  215.  
  216.      /* insert the pointer to this character array into the data entry */
  217.  
  218.      newentry -> other_fields = other_pointers;
  219.  
  220.   }
  221.  
  222.   /* check that all the entries are in alphabetical order by name */
  223.   
  224.   warning_given = 0;
  225.   rptr = get_next_link(Begin_Rlist);
  226.   while (rptr != 0) {
  227.     if (1 == compare_links(get_prev_link(rptr),rptr)) {
  228.        if (!warning_given) fprintf(stderr,"Warning, rolodex out of order\n");
  229.        warning_given = 1;
  230.        reorder_file = 1;
  231.     }
  232.     rptr = get_next_link(rptr);
  233.   }    
  234.     
  235.   return(n_entries);
  236.   
  237. }
  238.  
  239.  
  240. write_rolo_list (fp) FILE *fp; 
  241.  
  242. /* write the entire in-core rolodex to a file */
  243.  
  244. {
  245.  
  246.   Ptr_Rolo_List rptr;
  247.   Ptr_Rolo_Entry lentry;
  248.   int j;
  249.  
  250.   rptr = Begin_Rlist;
  251.  
  252.   while (rptr != 0) {
  253.     lentry = get_entry(rptr);
  254.     for (j = 0; j < N_BASIC_FIELDS; j++) {
  255.         fprintf(fp,"%s\n",get_basic_rolo_field(j,lentry));
  256.     }
  257.     for (j = 0; j < get_n_others(lentry); j++) {
  258.         fprintf(fp,"%s\n",get_other_field(j,lentry));
  259.     }
  260.     fprintf(fp,"\n");
  261.     rptr = get_next_link(rptr);
  262.   }
  263.  
  264. }
  265.  
  266.  
  267. write_rolo (fp1,fp2) FILE *fp1; FILE *fp2;
  268.  
  269. {
  270.   write_rolo_list(fp1);
  271.   write_rolo_list(fp2);
  272. }
  273.  
  274.  
  275. display_basic_field (name,value,show,up) char *name; char *value; int show,up;
  276. {
  277.   int i;
  278.   if ((value == (char *)NULL) || (all_whitespace(value) && !show)) return;
  279.   printf("%-25s",name);
  280.   while (*value != '\0') {
  281.     if (*value == ';') {
  282.        while (*++value == ' '); 
  283.        putchar('\n');
  284.        for (i = 0; i < (up ? 28 : 25); i++) putchar(' ');
  285.     }
  286.     else {
  287.        putchar(*value++);
  288.     }
  289.   }
  290.   putchar('\n');
  291. }
  292.  
  293.  
  294. display_other_field (fieldstring) char *fieldstring;
  295. {
  296.   int already_put_sep = 0;        
  297.   int count = 0;
  298.   int i;
  299.   while (*fieldstring != '\0') {
  300.     if (*fieldstring == ';' && already_put_sep) {
  301.        while (*++fieldstring == ' ');
  302.        putchar('\n');
  303.        for (i = 0; i < 25; i++) putchar(' ');
  304.        continue;
  305.     }
  306.     putchar(*fieldstring);
  307.     count++;
  308.     if (*fieldstring == ':' && !already_put_sep) {
  309.        for (i = count; i < 24; i++) putchar(' ');
  310.        already_put_sep = 1;
  311.     }
  312.     fieldstring++;
  313.   }
  314.   putchar('\n');
  315. }
  316.  
  317.  
  318. summarize_entry_list (rlist,ss) Ptr_Rolo_List rlist; char *ss;
  319.  
  320. /* print out the Name field for each entry that is tagged as matched */
  321. /* and number each entry. */
  322.  
  323. {
  324.   int count = 1;
  325.   clear_the_screen();
  326.   printf("Entries that match '%s' :\n\n",ss);
  327.   while (rlist != 0) {
  328.     if (get_matched(rlist)) {
  329.        printf (
  330.           "%d. \t%s\n",
  331.           count++,
  332.           get_basic_rolo_field((int) R_NAME,get_entry(rlist))
  333.        );
  334.     }
  335.     rlist = get_next_link(rlist);    
  336.   }
  337.   putchar('\n');
  338. }
  339.  
  340.  
  341. display_field_names ()
  342.  
  343. /* display and number each standard field name. */
  344.  
  345. {
  346.   int j;
  347.   char *name;
  348.   clear_the_screen();
  349.   for (j = 0; j < N_BASIC_FIELDS - 1; j++) {        
  350.       name = Field_Names[j];        
  351.       printf("%d. ",j+1);
  352.       while (*name != ':') putchar(*name++);
  353.       putchar('\n');
  354.   }
  355.   printf("%d. ",N_BASIC_FIELDS);
  356.   printf("A user created item name\n\n");
  357. }  
  358.   
  359. display_entry (lentry) Ptr_Rolo_Entry lentry;
  360. {
  361.   int j,n_others;
  362.   char *string;
  363.   
  364.   clear_the_screen();
  365.   
  366.   /* display the standard fields other than Date Updated */
  367.   
  368.   for (j = 0; j < N_BASIC_FIELDS - 1; j++) {
  369.       string = get_basic_rolo_field(j,lentry);
  370.       display_basic_field(Field_Names[j],string,0,0);
  371.   }        
  372.       
  373.   /* display any additional fields the user has defined for this entry */
  374.   
  375.   n_others = get_n_others(lentry);
  376.   for (j = 0; j < n_others; j++) {
  377.       string = get_other_field(j,lentry);
  378.       display_other_field(string);
  379.    }
  380.  
  381.    /* display the Date Updated field */
  382.    
  383.    j = N_BASIC_FIELDS - 1;
  384.    display_basic_field(Field_Names[j],get_basic_rolo_field(j,lentry),0,0);
  385.    fprintf(stdout,"\n");
  386.  
  387. }
  388.  
  389.  
  390. display_entry_for_update (lentry) Ptr_Rolo_Entry lentry;
  391.  
  392. /* same as display_entry, except each item is numbered and the Date Updated */
  393. /* item is not displayed */
  394.  
  395. {
  396.   int j,n_others;
  397.   char *string;
  398.   int count = 1;
  399.   
  400.   clear_the_screen();
  401.   
  402.   for (j = 0; j < N_BASIC_FIELDS - 1; j++) {
  403.       string = get_basic_rolo_field(j,lentry);
  404.       printf("%d. ",count++);
  405.       display_basic_field(Field_Names[j],string,1,1);
  406.   }        
  407.       
  408.   n_others = get_n_others(lentry);
  409.   for (j = 0; j < n_others; j++) {
  410.       string = get_other_field(j,lentry);
  411.       printf("%d. ",count++);
  412.       display_other_field(string);
  413.   }
  414.   
  415.   printf("%d. Add a new user defined field\n",count);
  416.  
  417.   fprintf(stdout,"\n");
  418.  
  419. }
  420.  
  421.  
  422. int cathelpfile (fileidx,helptopic,clear)
  423.  
  424.   int fileidx;
  425.   char *helptopic; 
  426.   int clear;
  427.  
  428. {
  429.   register char *filepath;
  430.   FILE *fp;
  431.   char buffer[MAXLINELEN];
  432.  
  433.   if(fileidx > LAST_HELP) {
  434.     fprintf(stderr,
  435.         "INTERNAL ERROR:  Error file index, max: %d, requested: %d\n",
  436.         LAST_HELP,fileidx);
  437.     return;
  438.   }else
  439.       filepath = libdir(hlpfiles[fileidx]);
  440.     
  441.   if (clear) clear_the_screen();
  442.   if (NULL == (fp = fopen(filepath,"r"))) {
  443.      if (helptopic) {
  444.         printf("No help available on %s, sorry.\n\n",helptopic); 
  445.      }
  446.      else {
  447.         fprintf(stderr,"Fatal error, can't open %s\n",filepath);
  448.         exit(-1);
  449.      }
  450.      return;
  451.   }
  452.   while (NULL != fgets(buffer,MAXLINELEN,fp)) printf("%s",buffer);  
  453.   printf("\n");
  454.   fclose(fp);
  455.   return;
  456. }
  457.  
  458.  
  459. any_char_to_continue ()
  460. {
  461.   char buffer[80];
  462.   printf("RETURN to continue: ");
  463.   fgets(buffer,80,stdin);
  464.   return;
  465. }
  466.