home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 125.img / PRO-C4.ZIP / BENCH1.ZIP / HELP / RWI.C < prev    next >
C/C++ Source or Header  |  1990-05-28  |  6KB  |  328 lines

  1. /* ==( help/rwi.c )== */
  2. /* ----------------------------------------------- */
  3. /* Pro-C  Copyright (C) 1988 - 1990 Vestronix Inc. */
  4. /* Modification to this source is not supported    */
  5. /* by Vestronix Inc.                               */
  6. /*            All Rights Reserved                  */
  7. /* ----------------------------------------------- */
  8. /* Written   JPK  26-Sep-88                        */
  9. /* Modified  Geo  12-Dec-89  See comments below    */
  10. /* ----------------------------------------------- */
  11. /* %W%  (%H% %T%) */
  12.  
  13. /*
  14.  *  Modifications
  15.  *
  16.  *  12-Dec-89  Geo - V2 version with variable lines
  17.  *  25-Oct-89  Geo - 1.32 Merge
  18.  *
  19.  *
  20. */
  21.  
  22. /* contains the routines that handle the index files */
  23.  
  24. /* 
  25.  * Routines in this file :
  26.  *
  27.  * int read_total(void);
  28.  *    calculates the number of records in the index file
  29.  *
  30.  * int read_index(int);
  31.  *    reads the index file and fills the index structure with the 
  32.  *    appropriate record
  33.  *
  34.  * void write_index(int);
  35.  *    writes the index structure to the index file  
  36.  *
  37.  * void build_ndxfile(void);
  38.  *    builds the index file from the help text file.  writes a record
  39.  *     to the file on every occurence of \f
  40.  *
  41.  * int read_help_index(int);
  42.  *    reads the temporary index file and fills the structure with the
  43.  *     position of the requested text block
  44.  *
  45.  * int build_help_ndxfile(void);
  46.  *    builds the temporary index file of the individual help records
  47.  *
  48.  */
  49.  
  50. # include <stdio.h> 
  51. # include <bench.h>
  52. # include <fileio.h>
  53. # include "help.h"
  54.  
  55. # ifdef UNIX
  56. #  include <setjmp.h>
  57. #  include <signal.h>
  58. # endif
  59.  
  60. # ifdef ANSI
  61. long fsize(int);
  62. # else
  63. long fsize();
  64. # endif
  65.  
  66. static int tfd;   /* file descriptor for help text index file */
  67.  
  68. int read_total()
  69. {
  70.     long size;
  71.  
  72.     if ((size = fsize(nfd)) == -1)
  73.         return(-1);
  74.  
  75.     /* 0 .. n -1 prob */
  76.     return( (int)((size / sizeof(struct help_ndx)) - 1) );
  77. }
  78.  
  79. int check_index(num)
  80.     int num;
  81. {
  82.     if ((long)(num * sizeof(struct help_ndx)) >= fsize(nfd))
  83.         return(FALSE);
  84.  
  85.     return(TRUE);
  86. }
  87.  
  88. #ifdef UNIX
  89. static jmp_buf alarmjmp;
  90. void (*oldalarmfunc)();
  91.  
  92. static void alarmsigstub()
  93. {
  94.     longjmp(alarmjmp, -1);
  95. }
  96. #endif
  97.  
  98. int lock_index_file(locktyp)
  99. {
  100. #ifdef UNIX
  101.     struct flock ldesc;
  102.     int i;
  103.  
  104.     switch(locktyp)
  105.     {
  106.         case ULOK:
  107.             ldesc.l_type = F_UNLCK;
  108.             break;
  109.         case RLOK:
  110.             ldesc.l_type = F_RDLCK;
  111.             break;
  112.         case WLOK:
  113.             ldesc.l_type = F_WRLCK;
  114.             break;
  115.         default:
  116.             errmsg("lock_index_file(): Bad lock type.");
  117.             break;
  118.     }
  119.     ldesc.l_whence = 0L;
  120.     ldesc.l_start = 0L;
  121.     ldesc.l_len = 0L;
  122.  
  123.     if ((i = setjmp(alarmjmp)) == 0)
  124.     {
  125.         oldalarmfunc = signal(SIGALRM, alarmsigstub);
  126.         alarm(5);
  127.         i = fcntl(nfd, F_SETLKW, &ldesc);
  128.     }
  129.     alarm(0);
  130.     signal(SIGALRM, oldalarmfunc);
  131.     if (i != 0)
  132.         return(FALSE);
  133.     else
  134. #endif
  135.         return(TRUE);
  136. }
  137.  
  138. int lock_index(num, locktyp)
  139.     int num;
  140. {
  141.     /* Check it exists first */
  142.     if (!check_index(num))
  143.     {
  144. # ifdef HDEBUG
  145.         errmsg("lock_index() : index does not exist");
  146. # endif
  147.         return(FALSE);
  148.     }
  149.     /* read in the index records */
  150.     if (lok_rec(nfd, num, locktyp) == -1)
  151.         return(FALSE);
  152.  
  153.     return(TRUE);
  154. }
  155.  
  156. int read_index(num)
  157.     int num;
  158. {
  159.     /* Check it exists first */
  160.     if (!check_index(num))
  161.     {
  162. # ifdef HDEBUG
  163.         errmsg("read_index() : index does not exist");
  164. # endif
  165.         return(FALSE);
  166.     }
  167.     /* read in the index records */
  168.     get_rec(nfd, num, (char *)&h_ndx);
  169.     return(TRUE);
  170. }
  171.  
  172. void write_index(num)
  173.     int num;
  174. {
  175.     /* write out index record */
  176.     put_rec(nfd, num, (char *)&h_ndx); /* Errors done by relio.c */
  177. }
  178.  
  179.  
  180.  
  181. void build_ndxfile()
  182. {
  183.     int    ch;
  184.     struct    help_ndx ndx;
  185.  
  186.     statmsg("Building help index...");
  187.  
  188.     ndx.seekpos = 0L;
  189.     ndx.size = 0;
  190.     put_garbage_rec(&ndx);
  191.  
  192.     fseek(hfptr, 0L, SEEK_SET);
  193.     while(ndx.seekpos != fsize(fileno(hfptr)))
  194.     {
  195.         if (read_hdr(&ndx) == FALSE)
  196.             return;
  197.         if (h_header.helpno == 0xffff)
  198.             while((ch = getc(hfptr)) != EOF && ch != '\f')
  199.                 ;
  200.         else
  201.         {
  202.             while((ch = getc(hfptr)) != EOF)
  203.             {
  204.                 ndx.size++;
  205.                 if (ch == '\f')
  206.                 {
  207.                     put_rec(nfd, h_header.helpno, (char *)&ndx);
  208.                     ndx.size = 0;
  209.                     break;
  210.                 }
  211.             }
  212.         }
  213.         ndx.seekpos = ftell(hfptr); 
  214.     }
  215. }
  216.  
  217.  
  218. int read_help_index(num)
  219.     int num;
  220. {
  221.     /* Check it exists first */
  222.     if ((long)(num * sizeof(struct help_ndx)) >= fsize(tfd))
  223.     {
  224. # ifdef HDEBUG
  225.         errmsg("read_help_index(): invalid index number (%d)", num);
  226. # endif
  227.         return(FALSE);
  228.     }
  229.  
  230.     /* read in the index records */
  231.     get_rec(tfd, num, (char *)&h_tdx);
  232.     return(TRUE);
  233. }
  234.  
  235.  
  236. int build_help_ndxfile(help_num)
  237. int help_num;
  238. {
  239.     int    buf;
  240.     struct    help_ndx ndx;
  241.  
  242.     h_part.len = 0;
  243.     if (read_index(help_num) == FALSE)
  244.     {
  245.         errmsg("build_help_ndxfile(): read_index failed");
  246.         return(FALSE);
  247.     }
  248.     if (fseek(hfptr, h_ndx.seekpos + (long)HSTRSIZE, SEEK_SET))
  249.     {
  250.         errmsg("build_help_ndxfile(): fseek failed");
  251.         return(FALSE);
  252.     }
  253.  
  254.     lseek(tfd, 0L, SEEK_SET);  /* go to beginning of file */
  255.     ndx.seekpos = h_ndx.seekpos;
  256.     ndx.size = 0;
  257.     put_rec(tfd, 0, (char *)&ndx);  /* put zeroth record */
  258.  
  259.     /* build the temporary index */
  260.     while (((buf = getc(hfptr)) != EOF) && (ndx.size < h_ndx.size))
  261.     {
  262.         ndx.size++;
  263.         if (buf == '\n')
  264.         {
  265.             write(tfd, (char *)&ndx, sizeof(struct help_ndx));
  266.             ndx.seekpos = ftell(hfptr) - HSTRSIZE;
  267.             ndx.size = 0;
  268.             h_part.len++; /* count number of lines */
  269.         }
  270.         if (buf == '\f')
  271.             break;
  272.     }
  273.     /* write out the last index for last line */
  274.     if (buf != '\f')
  275.         write(tfd, (char *)&ndx, sizeof(struct help_ndx));
  276.  
  277.     h_part.len++;
  278.     return(TRUE);
  279. }
  280.  
  281.  
  282. open_help_ndxfile()
  283. {
  284.     char    tdxfile[128];
  285.     char *envptr;
  286.     char pidstr[10];
  287.  
  288.     envptr = getenv("TMP");
  289.     if (envptr == NULL)
  290.         getcwd(tdxfile, 127);
  291.     else
  292.         strcpy(tdxfile, envptr);
  293.     addslash(tdxfile);
  294.     sprintf(pidstr, "%04X.tdx", getpid());
  295.     strcat(tdxfile, pidstr);
  296.  
  297.     tfd = -1;
  298.  
  299.     openf(tdxfile, SH_OPENRWC, sizeof(struct help_ndx), &tfd);
  300.     if (tfd == -1)
  301.     {
  302.         errmsg("Unable to create help index file");
  303.         return(FALSE);
  304.     }
  305.     return(TRUE);
  306. }
  307.  
  308. void close_help_ndxfile()
  309. {
  310.     char tmp_ndxfile[128];
  311.     char *envptr;
  312.     char pidstr[10];
  313.  
  314.     closef(tfd);
  315.     tfd = -1;
  316.  
  317.     envptr = getenv("TMP");
  318.     if (envptr == NULL)
  319.         getcwd(tmp_ndxfile, 127);
  320.     else
  321.         strcpy(tmp_ndxfile, envptr);
  322.     addslash(tmp_ndxfile);
  323.     sprintf(pidstr, "%04X.tdx", getpid());
  324.     strcat(tmp_ndxfile, pidstr);
  325.     unlink(tmp_ndxfile);
  326. }
  327.  
  328.