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