home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / window / ultrawin / uwdemo / prt_help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-02  |  5.2 KB  |  177 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* PRT_HELP.C                                                                                                                           */
  4. /*                                                                                                                                    */
  5. /* This program will take a hypertext help file, strip out any commands,    */
  6. /* print it to the screen as straight ASCII text.                           */
  7. /* If you wish to convert the help file to a simple text file, just re-     */
  8. /* direct the output to a file!  For example, to convert UW_HELP0.HLP to    */
  9. /* UW_HELP0.TXT, just type:                                                 */
  10. /*                                                                          */
  11. /* PRT_HELP UW_HELP0.HLP > UW_HELP0.TXT                                     */
  12. /*                                                                          */
  13. /****************************************************************************/
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18.  
  19. typedef unsigned char uchar;
  20. #define CMD_CHAR '`'
  21.  
  22. /*------------------------ prototypes for the program ----------------------*/
  23. void remove_commands( char *s, char *d );
  24. int cvt_tabs_to_spaces( char *s, int *text_len, int tab_size );
  25. char *get_token( char *s, char *d, char delim, int mode );
  26.  
  27.  
  28. /*********/
  29. /* ~main */
  30. /*       ********************************************************************/
  31. /****************************************************************************/
  32. int main( int argc, char *argv[] )
  33. {
  34.     int len;
  35.     char buff[257], dest[257];
  36.     FILE *fp;
  37.  
  38.     if( argc > 1 )
  39.     {
  40.         if( (fp = fopen(argv[1],"r")) != NULL )
  41.         {
  42.             setmem(buff,256,0);
  43.             setmem(dest,256,0);
  44.             while( fgets(buff, 256, fp) != NULL )
  45.             {
  46.                 if( buff[0] == 12 || buff[0] == '\r' || buff[0] == '\n' )
  47.                     buff[0] = '\0';
  48.                 if( strlen(buff) > 2 )
  49.                 {
  50.                     remove_commands( buff, dest );
  51.                     cvt_tabs_to_spaces( dest, &len, 2 );
  52.                     strcpy( buff, dest );
  53.                 }
  54.                 printf("%s\n",buff);
  55.             }
  56.             fclose(fp);
  57.         }
  58.         return(1);
  59.     }else
  60.         printf("usage: prt_help filename\r\n");
  61.     return(0);
  62. }
  63. /*** end of main ***/
  64.  
  65. /********************/
  66. /* ~remove_commands */
  67. /*                  *********************************************************/
  68. /****************************************************************************/
  69. void remove_commands( char *s, char *d )
  70. {
  71.     while( *s )
  72.     {
  73.         if( *s == CMD_CHAR )
  74.         {
  75.             if( toupper(*(s+1)) == 'K' )
  76.             {
  77.                 while( *s && (*s != '(') ) s++;
  78.                 if( *s == '(' )
  79.                     s++;
  80.                 while( *s && (*s != ',') )
  81.                     *d++ = *s++;
  82.                 if( *s == ',' ) s++;
  83.                 while( *s && (*s != ';') ) s++;
  84.                 if( *s == ';' ) s++;
  85.             }else{
  86.                 while( *s && (*s != ';')) s++;
  87.                 if( *s == ';' ) s++;
  88.             }
  89.         }else{
  90.             if( *s && ((*s == 9) || (isascii(*s) && isprint(*s))) )
  91.                 *d++ = *s++;
  92.             else
  93.                 *d++ = ' ', s++;
  94.         }
  95.     }
  96.     *d = '\0';
  97. }
  98. /*** end of remove_commands ***/
  99.  
  100. /***********************/
  101. /* ~cvt_tabs_to_spaces */
  102. /*                     ******************************************************/
  103. /****************************************************************************/
  104. int cvt_tabs_to_spaces( char *s, int *text_len, int tab_size )
  105. {
  106.     char buffer[257], *save_s = s;
  107.     char parm[81], parm2[81], function[81], *save_p;
  108.     int i, cnt, len = 0, len1 = 0, len2;
  109.  
  110.     setmem(buffer,256,0);
  111.     while( *s )
  112.     {
  113.         if( *s == 9 )
  114.         {
  115.             cnt = tab_size - (len % tab_size);
  116.             for( i = 0; i < cnt; i++ )
  117.                 buffer[len++] = ' ';
  118.             s++;
  119.         }
  120.         else if( *s == CMD_CHAR )
  121.         {
  122.             len2 = 0;
  123.             save_p = s;
  124.             s++;
  125.             s = get_token(s, function, '(', 1);                            /* get function name  */
  126.             if( !strcmp(function, "KEYWORD") )
  127.             {
  128.                 s = get_token(s, parm,  ',', 1);                            /* get keyword            */
  129.                 s = get_token(s, parm2, ')', 0);                            /* get search string    */
  130.                 len2 = strlen(parm);                                                    /* get keyword length    */
  131.             }
  132.             s = save_p;
  133.             do{
  134.                 buffer[len++] = *s, len1++;
  135.             }while( *s++ != ';' );
  136.             len1 -= len2;                                                                        /* don't count keyword*/
  137.         }else
  138.             buffer[len++] = *s++;
  139.     }
  140.     if( len )
  141.     {
  142.         while( isascii(buffer[len-1]) && isspace(buffer[len-1]) )
  143.             len--;
  144.     }
  145.     buffer[len] = '\0';
  146.     strcpy(save_s, buffer);
  147.     *text_len = len - len1;
  148.     return(len);
  149. }
  150. /*** end of cvt_tabs_to_spaces ***/
  151.  
  152. /*************/
  153. /*~get_token */
  154. /*           ****************************************************************/
  155. /* This routine extracts a "token" from the source string "s", stopping     */
  156. /* when the character "delim" is reached".  A pointer to the character      */
  157. /* following the delim is returned.                                         */
  158. /* NOTE: The token is converted to uppercase as it is extracted and is      */
  159. /*       NULL terminated...                                                 */
  160. /****************************************************************************/
  161. char *get_token( char *s, char *d, char delim, int mode )
  162. {
  163.     if( mode )                                                                            /* convert to uppercase        */
  164.     {
  165.         while( *s && (*s != delim) )
  166.             *d++ = toupper(*s++);
  167.     }else{
  168.         while( *s && (*s != delim) )
  169.             *d++ = *s++;
  170.     }
  171.     *d = '\0';
  172.     return(s+1);
  173. }
  174. /*** end of get_token ***/
  175.  
  176. /*** END OF FILE ***/
  177.