home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / doc / mir / wp_gener.c < prev    next >
Text File  |  1992-07-02  |  7KB  |  241 lines

  1. /*
  2.  * Usage -  wp_gener  < ASCII_file > generic_file
  3.  *
  4.  * WP_GENER Prepares an ASCII file so that it may be used as input for
  5.  *          almost any word processing program.  This program replaces
  6.  *          a hard return with a blank between lines of continuous text.
  7.  *
  8.  * input:   An ASCII text file with lines of 80 bytes or less.
  9.  *
  10.  * output:  Same file with selected line ends replaced by blanks.
  11.  *
  12.  * writeup:  MIR Tutorial TWO, topic ..
  13.  *
  14.  *  Written:    Douglas Lowry   Apr 24 92
  15.  *              Copyright (C) 1992 Marpex Inc.
  16.  *
  17.  *    The MIR (Mass Indexing and Retrieval) Tutorials explain detailed
  18.  *    usage and co-ordination of the MIR family of programs to analyze,
  19.  *    prepare and index databases (small through gigabyte size), and
  20.  *    how to build integrated retrieval software around the MIR search
  21.  *    engine.  The fifth of the five MIR tutorial series explains how
  22.  *    to extend indexing capability into leading edge search-related
  23.  *    technologies.  For more information, GO IBMPRO on CompuServe;
  24.  *    MIR files are in the DBMS library.  The same files are on the
  25.  *    Canada Remote Systems BBS.  A diskette copy of the Introduction
  26.  *    is available by mail ($10 US... check, Visa or Mastercard);
  27.  *    diskettes with Introduction, Tutorial ONE software and the
  28.  *    shareware Tutorial ONE text cost $29.  Shareware registration
  29.  *    for a tutorial is also $29.
  30.  *
  31.  *    E-mail...
  32.  *                Compuserve  71431,1337
  33.  *                Internet    doug.lowry%canrem.com
  34.  *                UUCP        canrem!doug.lowry
  35.  *                Others:     doug.lowry@canrem.uucp
  36.  *
  37.  *    FAX...                  416 963-5677
  38.  *
  39.  *    "Snail mail"...         Douglas Lowry, Ph.D.
  40.  *                            Marpex Inc.
  41.  *                            5334 Yonge Street, #1102
  42.  *                            North York, Ontario
  43.  *                            Canada  M2N 6M2
  44.  *
  45.  *    Related database consultation and preparation services are
  46.  *    available through:
  47.  *              Innotech Inc., 2001 Sheppard Avenue E., Suite #118,
  48.  *              North York, Ontario  Canada   M2J 4Z7
  49.  *              Tel.  416 492-3838   FAX  416 492-3843
  50.  *
  51.  *  This program is free software; you may redistribute it and/or
  52.  *  modify it under the terms of the GNU General Public License as
  53.  *  published by the Free Software Foundation; either version 2 of
  54.  *  the License, or (at your option) any later version.
  55.  *
  56.  *  This program is distributed in the hope that it will be useful,
  57.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  58.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  59.  *  GNU General Public License for more details.
  60.  *
  61.  *  You should have received a copy of the GNU General Public License
  62.  *  (file 05LICENS) along with this program; if not, write to the
  63.  *  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  64.  *  USA.
  65.  */
  66.  
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69.  
  70. #define     repeat      for(;;)
  71.  
  72. /*
  73.  * declarations 
  74.  */
  75.  
  76. typedef     enum        _bool
  77.              { FALSE = 0, TRUE = 1 }  Bool;
  78.  
  79.     void        Usage_(), process();
  80.     int         classify();
  81.     char        *Cmdname_() {    return( "wp_gener" );  }
  82.  
  83. /*
  84.  * MAIN
  85.  */
  86.  
  87. main( argc, argv )
  88.     int  argc;
  89.     char **argv;
  90. {
  91.     if( argc > 1 )
  92.         Usage_() ;
  93.  
  94.     process( ) ;
  95.  
  96.     exit( 0 );
  97. }
  98. /*
  99.  *  Usage
  100.  */
  101.     void
  102. Usage_()
  103. {
  104.     fprintf( stderr,
  105. "\nUsage:  %s  < ASCII_file > generic_file\n\n\
  106.         Prepares an ASCII file so that it may be used as input for\n\
  107.         almost any word processing program.  This program replaces\n\
  108.         a hard return with a blank between lines of continuous text.\n\n\
  109. input:  An ASCII text file with lines of 80 bytes or less.\n\n",
  110.         Cmdname_() );
  111.     fprintf( stderr,
  112. "output: Same file with selected line ends replaced by blanks.\n\n\
  113. writeup: MIR Tutorial TWO, topic ..\n\n" ) ;
  114.     exit( 1 ) ;
  115. }
  116. #define     EMPTY       0
  117. #define     FIXED       1
  118. #define     LEADER      2
  119. #define     CONTINUOUS  3
  120. #define     BEGIN_FILE  4
  121.  
  122. /*
  123.  *  PROCESS -   Two adjacent lines are continuous if both contain
  124.  *              no tabs or sequences of 3 or more blanks.  One exception
  125.  *              is allowed:  A paragraph leader may have leading white
  126.  *              space.  EMPTY and FIXED lines are fully disqualified.  A
  127.  *              LEADER is part of continuous text only if followed by a
  128.  *              line that is CONTINUOUS.
  129.  */
  130.     void
  131. process( )
  132. {
  133.     unsigned char   buf[ 90 ],
  134.             c ;
  135.     Bool    ctl_z ;     /*  found an end of file marker            */
  136.     int     type,prev,  /*  current and previous lines classified  */
  137.                         /*  as FIXED, LEADER or CONTINUOUS text    */
  138.             len, i ;
  139.  
  140.     type = BEGIN_FILE ;
  141.     ctl_z = FALSE ;
  142.  
  143.     while( fgets( buf, 90, stdin ) != NULL )
  144.     {
  145.         if( ctl_z )
  146.         {
  147.             fprintf( stderr, "FATAL... Text continues after a CTL-Z\n" );
  148.             fprintf( stderr, "\tIs the input really a text file?\n\n" ) ;
  149.             exit( 1 ) ;
  150.         }
  151.         len = strlen( buf ) - 1 ;
  152.         repeat
  153.         {
  154.             c = buf[ len - 1 ] ;
  155.             if( c == '\032' )
  156.             {
  157.                 ctl_z = TRUE ;
  158.                 break ;
  159.             }
  160.             if( c == '\n' || c == '\015' )
  161.                 len-- ;
  162.             else
  163.                 break ;
  164.         }
  165.         if( ctl_z )
  166.             continue ;      /*  Try for more input      */
  167.  
  168.         if( len > 80 )
  169.         {
  170.             fprintf( stderr,"FATAL... line length exceeds 80 bytes.\n");
  171.             Usage_() ;
  172.         }
  173.         buf[ len ] = '\0' ;
  174.  
  175.         prev = type ;
  176.         type = classify( buf, len ) ;
  177.  
  178.         if( type == CONTINUOUS && ( prev == LEADER || prev == CONTINUOUS ))
  179.             putchar( ' ' ) ;
  180.         else if( prev != BEGIN_FILE )
  181.             putchar( '\n' ) ;
  182.  
  183.         for( i = 0 ; i < len ; i++ )
  184.         {
  185.             if( putchar( buf[i] ) != buf[i] )
  186.             {
  187.                 fprintf( stderr, "FATAL... Unable to write.\n\n" ) ;
  188.                 exit( 1 ) ;
  189.             }
  190.         }
  191.     }
  192.  
  193.     putchar( '\n' ) ;
  194.     putchar( '\032' );
  195.     return ;
  196. }
  197. /*
  198.  *  CLASSIFY    Classify line type as EMPTY, FIXED, LEADER or CONTINOUS
  199.  */
  200.     int
  201. classify( buf, len )
  202.     char    buf[90];
  203.     int     len ;
  204. {
  205.     int     txt_bgn,    /*  byte at which text starts   */
  206.             blank_ct,   /*  # of blanks in a row        */
  207.             pt ;
  208.  
  209.     if( !len )
  210.         return( EMPTY ) ;
  211.  
  212.     for( pt = 0 ; pt < len ; pt++ )
  213.     {
  214.         if( !isspace( buf[pt] ))
  215.             break ;
  216.     }
  217.  
  218.     txt_bgn = pt ;
  219.     blank_ct = 0 ;
  220.     for(    ; pt < len ; pt++ )
  221.     {
  222.         if( buf[ pt ] == '\t' )
  223.             return( FIXED ) ;
  224.         if( buf[ pt ] == ' ' )
  225.             blank_ct++ ;
  226.         else if( blank_ct )     /*  any other character     */
  227.         {
  228.             if( blank_ct > 2 )
  229.                 return( FIXED ) ;
  230.             blank_ct = 0 ;
  231.         }
  232.     }
  233.  
  234.     if( blank_ct > 1 )      /*  ...at end of line                  */
  235.         return( FIXED ) ;   /*  else converting an end to blank    */
  236.                             /*  will create a third blank          */
  237.     if( txt_bgn )
  238.         return( LEADER ) ;
  239.  
  240.     return( CONTINUOUS ) ;
  241. }