home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_02 / whelp.c < prev    next >
C/C++ Source or Header  |  1991-03-24  |  4KB  |  228 lines

  1. /* whelp
  2.  *        Context-sensitive help system.
  3.  *        The help system is installed with a file reference.
  4.  *        The program looks for help files with the extension .HLP and .HX
  5.  *            in the 'home directory' specified by whome_dir
  6.  *        
  7.  *        HELP is displayed until ESCAPE is pressed.
  8.  *        F1 again displays a list of HELP topics.
  9.  *
  10.  */
  11. #include "wsys.h"
  12.  
  13. #ifndef __TURBOC__
  14.     /* this is MSC */
  15.     #include "direct.h"
  16.     #define MAXPATH  80
  17. #else  
  18.     /* __TURBOC__ */
  19.     #include <dir.h>
  20. #endif
  21.  
  22.  
  23. #include "whelp.h"
  24.  
  25.  
  26.  
  27. static void help(void);
  28.  
  29.  
  30. static char hname[MAXPATH] = {0};
  31. static char xname[MAXPATH] = {0};
  32.  
  33.  
  34. static char NOHELP[] = "Not available";
  35.  
  36.  
  37.  
  38.  
  39.  
  40. void whelp_install ( char *fn )
  41.     {
  42.     if ( strlen ( fn ) > 8 )
  43.         {
  44.         werror ('H', "Helpfile name > 8");
  45.         }
  46.  
  47.  
  48.     if ( ! *hname )
  49.         {
  50.         /* not yet installed
  51.          */
  52.         whotkey_install ( FKEY(1), help );
  53.         }
  54.             
  55.     strcpy ( hname, whome_dir );
  56.     strcat ( hname, fn );
  57.     strcat ( hname, ".HLP" );
  58.  
  59.     strcpy ( xname, whome_dir );
  60.     strcat ( xname, fn );
  61.     strcat ( xname, ".HX" );
  62.  
  63.  
  64.  
  65.     return;        /* whelp_install */
  66.     }
  67.  
  68.  
  69. static void help(void)
  70.     {
  71.  
  72.     /* a list of ptrs into the file topics
  73.      */
  74.     char **hxlist;
  75.     int  key, n, ntopics, nbytes, complen, l_marg, t_marg;
  76.  
  77.     char *topic, *text;
  78.  
  79.     HX  *hx;
  80.  
  81.     FILE *hfile;
  82.  
  83.     topic = whelp_ptr;
  84.  
  85.     /* read the index file
  86.      */
  87.     if ( NULL == (hfile =fopen (xname, "rb") ))
  88.         {
  89.         werror ('H', "HELP- open index");
  90.         }
  91.     if ( 1 != fread ( &ntopics, sizeof(ntopics), 1, hfile ) )
  92.         {
  93.         werror ('H', "HELP- read index");
  94.         }
  95.     hx = wmalloc ( sizeof (HX) * (1+ntopics), " HELP ");
  96.  
  97.     if ( ntopics != fread (hx, sizeof(HX), ntopics, hfile) )
  98.         {
  99.         werror ('H', "HELP- read index");
  100.         }
  101.     fclose (hfile);
  102.  
  103.  
  104.  
  105.     /* run through the topics and create list of ptrs to the strings
  106.      */
  107.     hxlist = wmalloc ( sizeof (char *) * (ntopics+1), " HELP " );
  108.     for ( n=0; n<ntopics; ++n )
  109.         {
  110.         hxlist[n] = hx[n].hxtopic;
  111.         }
  112.     hxlist[n] = NULL;
  113.  
  114.  
  115.  
  116.  
  117.     wopen (10, 5, 60, 15, whelpattr, DOUBLE_BORDER, whelpattr,
  118.             WSAVE2RAM);
  119.     wtitle ( " HELP " );
  120.  
  121.     wbutton_add ("ESCAPE",     6, 13, 7, ESCAPE, WBTN_BOX );
  122.     wbutton_add ("F1 TOPICS", 46, 13,10, FKEY(1),WBTN_BOX );
  123.  
  124.     do
  125.         {
  126.         /* find the topic in the index
  127.          */
  128.         for ( n=0; n<ntopics; ++n )
  129.             {
  130.             /* shorten comparison lenght to exclude terminal 
  131.              * spaces, punctuation chars, newline chars etc...
  132.              * required because of method of generating help 
  133.              * for WFORM and WMENU
  134.              */
  135.             complen = min ( strlen(topic), TOPIC_SIZE );
  136.             while ( (complen > 0) 
  137.                   && (strchr ( " :-.=?+\n\r", topic[complen-1] ) != NULL ) )
  138.                 {
  139.                 --complen;
  140.                 }
  141.  
  142.             if ( memicmp (hxlist[n], topic, complen )==0 )
  143.                 {
  144.                 /* found the topic - read in from file.
  145.                  */
  146.                 nbytes = hx[n].hxsize;
  147.                 text = wmalloc ( nbytes+1, " HELP " );
  148.  
  149.                 if ( NULL == (hfile = fopen ( hname, "rb" )))
  150.                     {
  151.                     werror ('H', "HELP -helpfileO");
  152.                     }
  153.                 if (0!=fseek (hfile, hx[n].hxpos, SEEK_SET) )
  154.                     {
  155.                     werror ('H', "HELP -helpfileS");
  156.                     }
  157.  
  158.                 if (1!= fread (text, nbytes, 1, hfile))
  159.                     {
  160.                     werror ('H', "HELP -helpfileR");
  161.                     }
  162.                 fclose (hfile);
  163.  
  164.                 text[nbytes] =0;
  165.  
  166.                 break;
  167.                 }
  168.  
  169.             }
  170.         if ( n == ntopics )
  171.             {
  172.             /* topic not found
  173.              * NOTE that 'text' is freed later, so must alloc
  174.              *     even though we know exactly what to put.
  175.              */
  176.             text = wmalloc (sizeof (NOHELP), " HELP ");
  177.             strcpy (text,NOHELP);
  178.             }
  179.  
  180.  
  181.  
  182.         /* print the help text
  183.          *        NOTE recalc position each time in case window is wmsdrag()'d
  184.          */
  185.         l_marg = w0-> winleft + 5;
  186.         t_marg = w0-> wintop  + 3; 
  187.          
  188.         /* define a small window to create left and top margins for text.
  189.          * also, this clears previous text.
  190.          */
  191.         wopen ( l_marg, t_marg, 50, 8, whelpattr, NO_BORDER, 0, WSAVE2NULL );
  192.         wputs (text);
  193.         free  (text);
  194.         wabandon();
  195.  
  196.         wgoto ( 5, 1 );
  197.         wclearline();
  198.         wputs ("TOPIC: "); 
  199.         wputs (topic);
  200.  
  201.         key = wgetc ();
  202.  
  203.         if ( key == FKEY(1) )
  204.             {
  205.             /* set location for list of topics above the "F1" button.
  206.              */
  207.             wsetlocation ( WLOC_ATWIN, 46, 1 );
  208.  
  209.             topic = hxlist [ wpicklist ("help topics",
  210.                             hxlist ) ];
  211.  
  212.             if ( topic == NULL )  /* ESCAPE pressed */
  213.                 {
  214.                 topic = whelp_ptr;
  215.                 }
  216.             }
  217.  
  218.         }
  219.     while ( key != ESCAPE );
  220.  
  221.     wclose ();
  222.  
  223.     free (hx);
  224.  
  225.     return;        /* help */
  226.     }
  227. /*------------------ end of whelp.c ----------------------*/
  228.