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

  1. /*
  2.  * Usage -  debloc_a  ASCII_blocked_file > unblocked_version
  3.  *
  4.  * DEBLOC_A Remove blocking, insert line feeds in ASCII blocked file.
  5.  *
  6.  * input:   ASCII file with four byte inclusive line lengths at the
  7.  *          beginning of every line, no line feeds at end.
  8.  *
  9.  * output:  Same data with counts out, line feeds/carriage returns in.
  10.  *
  11.  * writeup: MIR TUTORIAL ONE, topic 9
  12.  *
  13.  *  Written:    Douglas Lowry   May 11 92
  14.  *              Copyright (C) 1992 Marpex Inc.
  15.  *
  16.  *    The MIR (Mass Indexing and Retrieval) Tutorials explain detailed
  17.  *    usage and co-ordination of the MIR family of programs to analyze,
  18.  *    prepare and index databases (small through gigabyte size), and
  19.  *    how to build integrated retrieval software around the MIR search
  20.  *    engine.  The fifth of the five MIR tutorial series explains how
  21.  *    to extend indexing capability into leading edge search-related
  22.  *    technologies.  For more information, GO IBMPRO on CompuServe;
  23.  *    MIR files are in the DBMS library.  The same files are on the
  24.  *    Canada Remote Systems BBS.  A diskette copy of the Introduction
  25.  *    is available by mail ($10 US... check, Visa or Mastercard);
  26.  *    diskettes with Introduction, Tutorial ONE software and the
  27.  *    shareware Tutorial ONE text cost $29.  Shareware registration
  28.  *    for a tutorial is also $29.
  29.  *
  30.  *    E-mail...
  31.  *                Compuserve  71431,1337
  32.  *                Internet    doug.lowry%canrem.com
  33.  *                UUCP        canrem!doug.lowry
  34.  *                Others:     doug.lowry@canrem.uucp
  35.  *
  36.  *    FAX...                  416 963-5677
  37.  *
  38.  *    "Snail mail"...         Douglas Lowry, Ph.D.
  39.  *                            Marpex Inc.
  40.  *                            5334 Yonge Street, #1102
  41.  *                            North York, Ontario
  42.  *                            Canada  M2N 6M2
  43.  *
  44.  *    Related database consultation and preparation services are
  45.  *    available through:
  46.  *              Innotech Inc., 2001 Sheppard Avenue E., Suite #118,
  47.  *              North York, Ontario  Canada   M2J 4Z7
  48.  *              Tel.  416 492-3838   FAX  416 492-3843
  49.  *
  50.  *  This program is free software; you may redistribute it and/or
  51.  *  modify it under the terms of the GNU General Public License as
  52.  *  published by the Free Software Foundation; either version 2 of
  53.  *  the License, or (at your option) any later version.
  54.  *
  55.  *  This program is distributed in the hope that it will be useful,
  56.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  57.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  58.  *  GNU General Public License for more details.
  59.  *
  60.  *  You should have received a copy of the GNU General Public License
  61.  *  (file 05LICENS) along with this program; if not, write to the
  62.  *  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  63.  *  USA.
  64.  */
  65.  
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <ctype.h>
  69.  
  70. #define     MAX_BYTES   2048
  71. #define     repeat      for(;;)
  72.  
  73. /*
  74.  * declarations 
  75.  */
  76.  
  77. typedef     enum        _bool
  78.              { FALSE = 0, TRUE = 1 }  Bool;
  79.  
  80.     void        Usage_(), process();
  81.     int         get_data();
  82.     char        *Cmdname_() {    return( "debloc_a" );  }
  83.  
  84.     static long int  offset ;
  85.  
  86. /*
  87.  *  MAIN -
  88.  */
  89.  
  90. main( argc, argv )
  91.     int argc;
  92.     char    **argv;
  93. {
  94.     char    c10 ;
  95.     FILE    *fp ;
  96.  
  97.     if( argc != 2 )
  98.         Usage_() ;
  99.     c10 = argv[1][0] ;
  100.     if( c10 == '/' || c10 == '-' || c10 == '?' )
  101.         Usage_() ;
  102.     if (( fp = fopen( argv[ 1 ], "r" )) == NULL )
  103.     {
  104.         fprintf( stderr, "Unable to open file %s\n", argv[ 1 ] );
  105.         Usage_() ;
  106.     }
  107.  
  108.     process( fp );
  109.  
  110.     fclose( fp );
  111.     exit( 0 );
  112. }
  113. /*
  114.  *  Usage
  115.  */
  116.     void
  117. Usage_()
  118. {
  119.     fprintf( stderr,
  120. "\nUsage:  %s  ASCII_blocked_file > unblocked_version\n\n\
  121.         Remove blocking, insert line feeds in ASCII blocked file.\n\n\
  122. input:  ASCII file with four byte inclusive line lengths at the\n\
  123.         beginning of every line, no line feeds at end.\n\n",
  124.             Cmdname_() );
  125.     fprintf( stderr,
  126. "output: Same data with counts out, line feeds/carriage returns in.\n\n\
  127. writeup: MIR TUTORIAL ONE, topic 9\n\n" ) ;
  128.     exit( 1 ) ;
  129. }
  130. /*
  131.  *  PROCESS -
  132.  */
  133.     void
  134. process( fp )
  135.     FILE    *fp ;
  136. {
  137.     char    buf[ MAX_BYTES ] ;
  138.     int length;     /* length of current line   */
  139.     int j;
  140.  
  141.     offset = 0 ;
  142.  
  143.     while( ( length= get_data( fp, buf )) > -1 )
  144.     {
  145.         for( j= 0; j < length; j++ )
  146.             putchar( buf[ j ] );
  147.         if( putchar( '\n' ) != '\n' )
  148.         {
  149.             fprintf( stderr, "FATAL... Unable to write.\n\n" ) ;
  150.             exit( 1 ) ;
  151.         }
  152.     }
  153.     return;
  154. }
  155.  
  156. /*
  157.  *  GET_DATA
  158.  */
  159.  
  160.     int
  161. get_data( fp, buf )
  162.     FILE    *fp;
  163.     char    buf[ MAX_BYTES ] ;
  164. {
  165.     char    lil[5];
  166.     int     length,     /* bytes in current line    */
  167.             i;
  168.  
  169.     lil[ 4 ] = '\0' ;
  170.  
  171. /*  ...Read in the line size    */
  172.  
  173.     for( i = 0 ; i < 4 ; i++ )
  174.     {
  175.         if(( lil[i] = fgetc( fp )) == EOF )
  176.             return( -1 );
  177.         if( !isdigit( lil[i] ))
  178.         {
  179.             fprintf( stderr, "Line size out of sync at byte %ld\n",
  180.                 offset ) ;
  181.             exit( 1 ) ;
  182.         }
  183.     }
  184.  
  185.     length = atoi( lil ) - 4;
  186.     offset += 4;
  187.  
  188. /*  Now load the data stream    */
  189.  
  190.     if ( length > MAX_BYTES )
  191.     {
  192.         fprintf( stderr, "Oversize line at byte %ld\n", offset );
  193.         exit( 1 ) ;
  194.     }
  195.  
  196.     if ( fread( buf, sizeof(char), length, fp ) < length )
  197.     {
  198.         fprintf( stderr,"Failure reading line data starting at byte %ld\n",
  199.             offset );
  200.         exit( 1 ) ;
  201.     }
  202.     offset += length;
  203.  
  204.     return( length );
  205. }
  206.