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

  1. static char HELPTEXT[] =
  2.  
  3. " makehx.c --- This program makes the help file index \n"
  4. "              For use with the WTWG user interface system \n"
  5. " \n"
  6. "  PARAMETER:  FILENAME without extension.  \n"
  7. "        the input  file is named FILENAME.hlp \n"
  8. "        the output file is named FILENAME.hx \n"
  9. " \n"
  10. "  INPUT: ASCII file with help text.\n"
  11. "       New topics are identified by a '@' in column 1\n"
  12. "       ... followed immediately by the topic name, then <CR>\n"
  13. "    The text for that topic follows. Limit is 50 columns, 8 lines\n"
  14. "       The next topic begins with another @ in column 1\n"
  15. " \n"
  16. " OUTPUT: binary file containing help index file FILENAME.HX, which \n"
  17. "       can be read by the whelp() routine to give context-sensitive help\n"
  18. " \n"
  19. " by D Blum 1989 \n";
  20.  
  21. /* To compile this program under TurboC (NOTE large memory model)
  22.  *        tcc -ml makehx.c
  23.  *
  24.  *    To compile under microsoft C
  25.  *        cl /AL /D__LARGE__  makehx.c
  26.  */
  27.  
  28. #include <stdlib.h>
  29. #ifdef __TURBOC__
  30.     #include <alloc.h>
  31. #else
  32.     /* MICROSOFT version */
  33.     #include "msc.h"
  34.     #include <malloc.h>
  35. #endif        /* MICROSOFT */    
  36.  
  37. #include <string.h>
  38. #include <stdio.h>
  39.  
  40. #include "wtwg.h"        /* allows wsys.h to be included */
  41. #include "wsys.h"        /* for NORMALIZE macro */
  42. #include "whelp.h"
  43.  
  44.  
  45. #ifndef __LARGE__
  46. #error Program must be built in the large model. MICROSOFT: define __LARGE__
  47. #endif    /* LARGE */
  48.  
  49.  
  50. /* function to trim strings of terminal spaces, \r \n
  51.  */
  52. static void near strim ( char * );
  53.  
  54.  
  55. main ( int argc, char **argv )
  56.     {
  57.     FILE *infile, *outfile;
  58.     char inf_name[13],  outf_name[13];
  59.     char buff[256];
  60.  
  61.     HX   *hx;
  62.  
  63.     int  entry;
  64.     long lastpos;
  65.  
  66.     if ( argc == 1
  67.     || strlen (argv[1]) > 8
  68.     || strchr (argv[1], '.') )
  69.         {
  70.         puts (HELPTEXT);
  71.         exit (0);
  72.         }
  73.  
  74.     strcpy ( inf_name, argv[1] );
  75.     strcat ( inf_name, ".hlp" );
  76.     strcpy ( outf_name, argv[1] );
  77.     strcat ( outf_name, ".hx" );
  78.  
  79.     infile = fopen (inf_name, "rb");
  80.     if ( ! infile )
  81.         {
  82.         printf ( "error - file %s not found\n", inf_name );
  83.         puts (HELPTEXT);
  84.         exit (1);
  85.         }
  86.  
  87.     hx = malloc ( MAX_TOPICS * sizeof (HX) );
  88.     if ( ! hx )
  89.         {
  90.         perror ("Not enough memory");
  91.         exit (1);
  92.         }
  93.     _NORMALIZE (hx);
  94.     memset ( hx, 0, MAX_TOPICS * sizeof (HX) );
  95.  
  96.     lastpos = entry = 0;
  97.  
  98.     while ( fgets( buff, sizeof(buff), infile ) )    /* read thru text file */
  99.         {
  100.         if ( *buff == '@' )
  101.             {
  102.             /* new topic coming,
  103.              * first have to cleanup last topic entry, set size of last topic,
  104.              */
  105.             hx[entry].hxsize = lastpos - hx[entry].hxpos;
  106.  
  107.             /* start new entry
  108.              */
  109.             ++entry;
  110.             if ( entry == MAX_TOPICS )
  111.                 {
  112.                 puts ("Too many topics");
  113.                 exit ( 99 );
  114.                 }
  115.             strim (buff);   /* clean up new topic name */
  116.  
  117.             memcpy ( hx[entry].hxtopic, buff+1, TOPIC_SIZE );
  118.  
  119.             hx[entry].hxpos = ftell ( infile );
  120.  
  121.             printf ("Topic: %s\n", buff+1);
  122.             }
  123.         lastpos = ftell ( infile );
  124.  
  125.         }
  126.  
  127.     if ( entry == 0 )
  128.         {
  129.         puts ("No topics found");
  130.         exit (1);
  131.         }
  132.  
  133.     hx[entry].hxsize = lastpos - hx[entry].hxpos;    /* size of last entry */
  134.  
  135.  
  136.     /* finished constructing help index - now write it to disk */
  137.  
  138.  
  139.     outfile = fopen (outf_name, "wb");
  140.     if  ( ! outfile )
  141.         {
  142.         printf ("Could not open output file %s\n",outf_name);
  143.         exit (1);
  144.         }
  145.  
  146.  
  147.     /* The first thing found in the help index is the number of entries
  148.      */
  149.     if ( 1 != fwrite ( &entry, sizeof (entry), 1, outfile ) )
  150.         {
  151.         printf ("Error writing header to output file %s\n", outf_name);
  152.         }
  153.  
  154.     /* Now write the entire table of entries to the index file
  155.      * NOTE the first entry is a dummy (see the loop that constructed
  156.      *      the entries.
  157.      */
  158.     if ( entry != fwrite ( hx+1,   sizeof (HX),  entry, outfile ) )
  159.         {
  160. z        printf ("Error writing data to output file %s\n", outf_name);
  161.         }
  162.  
  163.     fclose (outfile);
  164.  
  165.     puts ("Sucessful Completion");
  166.  
  167.  
  168.      return (0);    /* main */
  169.      }
  170.  
  171.  
  172.  
  173. static void near strim (char *s)
  174.     {
  175.     int n;
  176.     char *p;
  177.  
  178.     _NORMALIZE (s);        /* large model */
  179.     n =  strlen (s);
  180.  
  181.     while ( n-- )
  182.         {
  183.         p = s+n;
  184.         if ( *p == '\r' || *p == '\n' || *p == ' ' )
  185.             {
  186.             *p = 0;
  187.             }
  188.         else
  189.             {
  190.             break;
  191.             }
  192.         }
  193.  
  194.     return; /* strim() */
  195.  
  196.  
  197.     }
  198. /*--------------------- end of MAKEHX.C --------------------------*/
  199.