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

  1. /* ==( help/rwt.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 help text files */
  23.  
  24. /* 
  25.  * Routines in this file :
  26.  *
  27.  * long fsize(int);
  28.  *    returns the size of the file
  29.  *
  30.  * static void xtoa(int, char *, int);
  31.  *    convert hex to ascii
  32.  *
  33.  * static int atox(char *, int);
  34.  *    convert ascii to hex
  35.  *
  36.  * int read_help(char *, int, struct help_ndx);
  37.  *     reads a line from the help text file into the buffer
  38.  *
  39.  * int write_help(struct help_ndx, char *);
  40.  *     writes a line to end of the help text file
  41.  *
  42.  * int read_hdr(struct help_ndx);
  43.  *    reads the header from the help text file 
  44.  *
  45.  * int write_hdr(struct help_ndx);
  46.  *    writes a header to the help text file
  47.  *
  48.  * int openhelp(char *, int);
  49.  *    opens the help text, and index files
  50.  *
  51.  * int create_node(int);
  52.  *     initializes a newly created help file
  53.  *
  54.  * int helptofile(char *);
  55.  *    transfers help text from help text file to edit file for editting
  56.  *
  57.  * int filetohelp(char *);
  58.  *    transfers help text from edit file to actual help text file
  59.  *
  60.  */
  61.  
  62. # include <stdio.h> 
  63. # include <bench.h>
  64. # include <fileio.h>
  65. # include "help.h"
  66.  
  67. # include <errno.h>
  68.  
  69.  
  70. # ifdef ANSI
  71. static void xtoa(int, char *, int);
  72. static int atox(char *);
  73. long fsize(int);
  74. # else
  75. static void xtoa();
  76. static int atox();
  77. long fsize();
  78. # endif
  79.  
  80.  
  81. int read_help(line, lmax, hptr)
  82.     char * line;
  83.     int lmax;
  84.     struct help_ndx *hptr;
  85. {
  86.     /* Seek to position and fill yer boots */
  87.     if (fseek(hfptr, hptr->seekpos + (long)HSTRSIZE, SEEK_SET))
  88.     {
  89. # ifdef HDEBUGI
  90.         errmsg("read_help() : Seek failed (%d)", errno);
  91. # endif
  92.         return(FALSE);
  93.     }
  94.     if (!(fgets(line, lmax, hfptr)))
  95.     {
  96. # ifdef HDEBUGI
  97.         errmsg("read_help() : Read failed (%d)", errno);
  98. # endif
  99.         return(FALSE);
  100.     }
  101.     return(TRUE);
  102. }
  103.  
  104. int write_help(hptr, text_buf)
  105.     struct help_ndx *hptr;
  106.     char * text_buf;
  107. {
  108.     /* Seek to position and dump yer boots */
  109.     if (fseek(hfptr, hptr->seekpos + (long)HSTRSIZE, SEEK_SET))
  110.     {
  111.         errmsg("write_help() : Seek failed (%d)", errno);
  112.         return(FALSE);
  113.     }
  114.     /* Text part */
  115.     if (fputs(text_buf,hfptr) == EOF)
  116.     {
  117.         errmsg("write_help() : Write failed (%d)", errno);
  118.         return(FALSE);
  119.     }
  120.     (void) fflush(hfptr);
  121.     return(TRUE);
  122. }
  123.  
  124. int read_hdr(hptr)
  125.     struct help_ndx *hptr;
  126. {
  127.     char text_buf[HSTRSIZE+1];
  128.     long csp;
  129.     int ch;
  130.  
  131.     /* Header will be zeroed if an error occurs */
  132.     zerorec(&h_header, sizeof(struct help_hdr));
  133.  
  134.     /* Check it exists first */
  135.     if (hptr->seekpos >= fsize(fileno(hfptr)))
  136.     {
  137.         errmsg("read_hdr() : help does not exist");
  138.         return(FALSE);
  139.     }
  140.     /* Seek to position and dump yer boots */
  141.     if (fseek(hfptr, hptr->seekpos, SEEK_SET))
  142.     {
  143.         errmsg("read_hdr() : Seek failed (%d)", errno);
  144.         return(FALSE);
  145.     }
  146.     csp = hptr->seekpos;
  147.  
  148.     while(!isdigit(ch = getc(hfptr)))
  149.     {
  150.         if (ch == EOF)
  151.         {
  152.             csp = hptr->seekpos;
  153.             break;
  154.         }
  155.         csp++;
  156.     }
  157.     hptr->seekpos = csp;
  158.  
  159.     if (fseek(hfptr, hptr->seekpos, SEEK_SET))
  160.     {
  161.         errmsg("read_hdr() : Seek correction failed (%d)", errno);
  162.         return(FALSE);
  163.     }
  164.  
  165.     /* Text part */
  166.     if (!(fgets(text_buf, HSTRSIZE+1, hfptr)))
  167.     {
  168.         errmsg("read_hdr() : Read failed (%d)", errno);
  169.         return(FALSE);
  170.     }
  171.     /* set all fields of the header using the ascii chars read in */
  172.     h_header.row         = (atox(text_buf) << 4) + atox(text_buf+1);
  173.     h_header.col         = (atox(text_buf+2) << 4) + atox(text_buf+3);
  174.     h_header.width       = (atox(text_buf+4) << 4) + atox(text_buf+5);
  175.     h_header.height      = (atox(text_buf+6) << 4) + atox(text_buf+7);
  176.  
  177.     h_header.box_style   = atox(text_buf+8);
  178.     h_header.box_attr    = atox(text_buf+9);
  179.     h_header.disp_attr   = atox(text_buf+10);
  180.     h_header.wrap        = atox(text_buf+11);
  181.     h_header.tabs        = atox(text_buf+12);
  182.  
  183.     h_header.helpno      = (atox(text_buf+36) << 12) +
  184.                 (atox(text_buf+37) << 8) +
  185.                 (atox(text_buf+38) << 4) +
  186.                 atox(text_buf+39);
  187.     return(TRUE);
  188. }
  189.  
  190. int mark_hdr(hptr)
  191.     struct help_ndx *hptr;
  192. {
  193.     char header_buf[HSTRSIZE+1];
  194.  
  195.     byteset(header_buf, '0', HSTRSIZE);
  196.     header_buf[HSTRSIZE] = '\0';
  197.  
  198.     /* convert the entire header into ascii format before writing out */
  199.     xtoa(h_header.row        , header_buf   , 2);
  200.     xtoa(h_header.col        , header_buf+2 , 2);
  201.     xtoa(h_header.width      , header_buf+4 , 2);
  202.     xtoa(h_header.height     , header_buf+6 , 2);
  203.     xtoa(h_header.box_style  , header_buf+8 , 1);
  204.     xtoa(h_header.box_attr   , header_buf+9 , 1);
  205.     xtoa(h_header.disp_attr  , header_buf+10, 1);
  206.     xtoa(h_header.wrap       , header_buf+11, 1);
  207.     xtoa(h_header.tabs       , header_buf+12, 1);
  208.     xtoa(0xffff              , header_buf+36, 4);
  209.  
  210.     /* Seek to position and dump yer boots */
  211.     if (fseek(hfptr, hptr->seekpos, SEEK_SET))
  212.     {
  213.         errmsg("mark_hdr() : Seek failed (%d)", errno);
  214.         return(FALSE);
  215.     }
  216.     /* Text hdr */
  217.     if (fputs(header_buf, hfptr) == EOF)
  218.     {
  219.         errmsg("mark_hdr() : Write failed (%d)", errno);
  220.         return(FALSE);
  221.     }
  222.  
  223.     (void) fflush(hfptr);
  224.     return(TRUE);
  225. }
  226.  
  227. int write_hdr(hptr)
  228.     struct help_ndx *hptr;
  229. {
  230.     char header_buf[HSTRSIZE+1];
  231.  
  232.     byteset(header_buf, '0', HSTRSIZE);
  233.     header_buf[HSTRSIZE] = '\0';
  234.  
  235.     /* convert the entire header into ascii format before writing out */
  236.     xtoa(h_header.row        , header_buf   , 2);
  237.     xtoa(h_header.col        , header_buf+2 , 2);
  238.     xtoa(h_header.width      , header_buf+4 , 2);
  239.     xtoa(h_header.height     , header_buf+6 , 2);
  240.     xtoa(h_header.box_style  , header_buf+8 , 1);
  241.     xtoa(h_header.box_attr   , header_buf+9 , 1);
  242.     xtoa(h_header.disp_attr  , header_buf+10, 1);
  243.     xtoa(h_header.wrap       , header_buf+11, 1);
  244.     xtoa(h_header.tabs       , header_buf+12, 1);
  245.     xtoa(h_header.helpno     , header_buf+36, 4);
  246.  
  247.     /* Seek to position and dump yer boots */
  248.     if (fseek(hfptr, hptr->seekpos, SEEK_SET))
  249.     {
  250.         errmsg("write_hdr() : Seek failed (%d)", errno);
  251.         return(FALSE);
  252.     }
  253.     /* Text hdr */
  254.     if (fputs(header_buf, hfptr) == EOF)
  255.     {
  256.         errmsg("write_hdr() : Write failed (%d)", errno);
  257.         return(FALSE);
  258.     }
  259.  
  260.     (void) fflush(hfptr);
  261.     return(TRUE);
  262. }
  263.  
  264. int openhelp(file, num)
  265.     char    * file;
  266.     int    num;
  267. {
  268.     char    helpfile[128];
  269.     char    ndxfile[128];
  270.     struct help_ndx garbage_ndx;
  271.     int crtflag = 0;
  272.  
  273.     /* open or create the help and help index files */
  274.     (void) strcpy(helpfile, file);
  275.     (void) strcat(helpfile, ".hlp");
  276.  
  277.     (void) strcpy(ndxfile,  file);
  278.     (void) strcat(ndxfile,  ".ndx");
  279.  
  280.     if ((hfptr = fopen(helpfile, "r+")) == NULL)
  281.     {
  282.         if (!warning(-1, "Help message not found, Create it ?"))
  283.             return(FALSE);
  284.  
  285.         /* index file doesn't exist so create it and the help file */
  286.         if ((hfptr = fopen(helpfile, "w+")) == NULL)
  287.         {
  288.             /* couldn't create the help file so abort help */
  289.             errmsg("Unable to create help file");
  290.             return(FALSE);
  291.         }
  292.         unlink(ndxfile);
  293.         openf(ndxfile, SH_OPENRWC, sizeof(struct help_ndx), &nfd);
  294.         if (nfd < 0)
  295.         {
  296.             (void) fclose(hfptr);
  297.             if (nfd == -1)
  298.                 errmsg("Unable to create help index file");
  299.             return(FALSE);
  300.         }
  301.         garbage_ndx.seekpos = 0L;
  302.         garbage_ndx.size = 0;
  303.         put_garbage_rec(&garbage_ndx); /* force a null record */
  304.  
  305.         /* files are created so initialize them */
  306.         (void) create_node(num);
  307.         closef(nfd);
  308.         openf(ndxfile, SH_OPENRWNC, sizeof(struct help_ndx), &nfd);
  309.     }
  310.     else
  311.     {
  312.         openf(ndxfile, SH_OPENRWNC, sizeof(struct help_ndx), &nfd);
  313.         if (nfd < 0 )
  314.         {
  315.             openf(ndxfile, SH_OPENRWC, sizeof(struct help_ndx), &nfd);
  316.             if (nfd < 0 )
  317.             {
  318.                 (void) fclose(hfptr);
  319.                 if (nfd == -1)
  320.                     errmsg("Unable to create help index file");
  321.                 return(FALSE);
  322.             }
  323.  
  324.             build_ndxfile();
  325.             closef(nfd);
  326.             openf(ndxfile, SH_OPENRWNC, sizeof(struct help_ndx), &nfd);
  327.             if (nfd < 0)
  328.             {
  329.                 fclose(hfptr);
  330.                 return(FALSE);
  331.             }
  332.         }
  333.         /* if index is older than help text file - recreate it*/
  334.         else
  335.         {
  336.             if (file_time(ndxfile) < file_time(helpfile))
  337.             {
  338.                 closef(nfd);
  339.                 unlink(ndxfile);
  340.                 openf(ndxfile, SH_OPENRWC, sizeof(struct help_ndx), &nfd);
  341.                 if (nfd < 0)
  342.                 {
  343.                     fclose(hfptr);
  344.                     return(FALSE);
  345.                 }
  346.                 build_ndxfile();
  347.                 closef(nfd);
  348.                 openf(ndxfile, SH_OPENRWNC, sizeof(struct help_ndx), &nfd);
  349.                 if (nfd < 0)
  350.                 {
  351.                     fclose(hfptr);
  352.                     return(FALSE);
  353.                 }
  354.             }
  355.         }
  356.     }
  357.  
  358. # ifdef UNIX
  359.     chmod(ndxfile, 00666);
  360. # endif
  361.  
  362.     return(TRUE);
  363. }
  364.  
  365. /* default help header, used to initialize newly created nodes */
  366. static struct help_hdr default_header = {
  367.     12,
  368.     1,
  369.     80,
  370.     12,
  371.     0,
  372.     NORMAL,
  373.     NORMAL,
  374.     0,
  375.     4,
  376.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  377.     0
  378. };
  379.  
  380. int create_node(num)
  381.     int  num;
  382. {
  383.     int total;
  384.     static char first_line[] = "\n\f";
  385.  
  386.     if ((total = read_total()) == -1)
  387.     {
  388. # ifdef HDEBUG
  389.         errmsg("create_node(): read_total failed");
  390. # endif
  391.         return(FALSE);
  392.     }
  393.  
  394.     h_header = default_header;
  395.  
  396.     h_header.row = w_nrows / 2;
  397.     h_header.width = w_ncols;
  398.     h_header.height = w_nrows / 2;
  399.  
  400.     while (total < num)
  401.     {
  402.         /* Add to the end */
  403.         h_ndx.seekpos = fsize(fileno(hfptr));
  404.         h_ndx.size = strlen(first_line);
  405.  
  406.         /* save this index to file */
  407.         write_index(++total);
  408.  
  409.         h_header.helpno = total;
  410.  
  411.         /* write the header out to file */
  412.         if (write_hdr(&h_ndx) == FALSE)
  413.             return(FALSE);
  414.  
  415.         if (write_help(&h_ndx, first_line) == FALSE)
  416.             return(FALSE);
  417.     }
  418.     return(TRUE);
  419. }
  420.  
  421.  
  422. int helptofile(editfile)
  423.     char * editfile;
  424. {
  425.     FILE * fptr;
  426.     int  ch;
  427.  
  428.     if ((fptr = fopen(editfile, "w")) == NULL)
  429.     {
  430.         errmsg("helptofile(): can not open text file");
  431.         return(FALSE);
  432.     }
  433.  
  434.     if (read_index(h_num) == FALSE || h_ndx.size == 0)
  435.     {
  436. # ifdef HDEBUG
  437.         errmsg("helptofile(): read_index failed");
  438. # endif
  439.         (void) fclose(fptr);
  440.         return(FALSE);
  441.     }
  442.     if (fseek(hfptr, h_ndx.seekpos + (long)HSTRSIZE, SEEK_SET))
  443.     {
  444. # ifdef HDEBUG
  445.         errmsg("helptofile(): lseek failed (%d)", errno);
  446. # endif
  447.         (void) fclose(fptr);
  448.         return(FALSE);
  449.     }
  450.  
  451.     /* copy the help text into the edit file */
  452.     while (((ch = getc(hfptr)) != EOF) && (ch != '\f'))
  453.         putc(ch, fptr);
  454.  
  455.     (void)fclose(fptr);
  456.     return(TRUE);
  457. }
  458.  
  459. int filetohelp(editfile)
  460.     char * editfile;
  461. {
  462.     FILE * fptr;
  463.     int     ch;
  464.     extern char h_file[]; /* callhelp.c */
  465.     struct help_ndx garbage_ndx;
  466.  
  467.     if ((fptr = fopen(editfile, "r")) == NULL)
  468.     {
  469.         errmsg("filetohelp(): can not open text file");
  470.         return(FALSE);
  471.     }
  472.  
  473.     get_garbage_rec(&garbage_ndx);
  474.     garbage_ndx.size += h_ndx.size;
  475.     put_garbage_rec(&garbage_ndx);
  476.     
  477.     mark_hdr(&h_ndx);
  478.  
  479.     h_ndx.seekpos = fsize(fileno(hfptr));
  480.     h_ndx.size = (int)fsize(fileno(fptr));
  481.     h_ndx.size++; /* include the \f that we will write to the file */
  482.  
  483.     write_index(h_num);
  484.  
  485.     h_header.helpno = h_num;
  486.  
  487.     if (write_hdr(&h_ndx) == FALSE) 
  488.     {
  489.         errmsg("filetohelp(): can't write to index file");
  490.         return(FALSE);
  491.     }
  492.  
  493.     /* copy the help text from the edit file to the help file */
  494.     if (h_ndx.size - 1)
  495.         while((ch = getc(fptr)) != EOF)
  496.             putc(ch, hfptr);
  497.  
  498.     /* make sure line feed separator is in help file */
  499.     putc('\f', hfptr);  
  500.  
  501.     fflush(hfptr);
  502.     (void) fclose(fptr);
  503.  
  504.     lock_index(h_num, ULOK);
  505.     if (lock_index_file(WLOK))
  506.     {
  507.         /* clean fragmentation out of file */
  508.         if (eat_garbage(h_file) == FALSE)
  509.         {
  510.             errmsg("filetohelp(): got FALSE from eat_garbage()");
  511.             return(FALSE);
  512.         }
  513.         lock_index_file(ULOK);
  514.     }
  515.     lock_index(h_num, WLOK);
  516.  
  517.     close_help_ndxfile();
  518.     open_help_ndxfile();
  519.     if (build_help_ndxfile(h_num) == FALSE) 
  520.     {
  521.         errmsg("filetohelp(): build_help_ndxfile failed");
  522.         return(FALSE);
  523.     }
  524.  
  525.     return(TRUE);
  526. }
  527.  
  528. static void xtoa(val, buf, num)
  529.     int    val;
  530.     char    * buf;
  531.     int    num;
  532. {
  533.     char    temp[18];
  534.     int    i;
  535.     int     j;
  536.  
  537.     (void) sprintf(temp, "%x", val);
  538.     byteset(buf, 0x30, num);
  539.     for (i=strlen(temp)-1, j=num-1; i >= 0; )
  540.         buf[j--] = temp[i--];
  541. }
  542.  
  543.  
  544. static int atox(buf)
  545.     char * buf;
  546. {
  547.     int temp = 0;
  548.  
  549.     if (buf[0] >= 'a' && buf[0] <= 'f')
  550.         temp = (int)(buf[0] - 'a' + 0x0a);
  551.     else
  552.         temp = (int)(buf[0] - '0');
  553.  
  554.     return(temp);
  555. }
  556.