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

  1. /*
  2.  * Usage -  debloc_b  binary_blocked_file [/s][/e] > unblocked_output
  3.  *
  4.  * DEBLOC_B Remove blocking, and (if not suppressed by /s argument)
  5.  *          insert line feeds.  Argument /e must be used if file was
  6.  *          originally EBCDIC, in which case the block lengths must
  7.  *          be converted back to EBCDIC before they are interpreted.
  8.  *
  9.  * input:   File with four byte binary inclusive block lengths and
  10.  *          sub-lengths, two bytes in high to low order, then two
  11.  *          NULLs.
  12.  *
  13.  * output:  Same data with counts out, line feeds/carriage returns
  14.  *          in (unless suppressed).
  15.  *
  16.  * writeup: MIR TUTORIAL ONE, topic 9
  17.  *
  18.  *  Written:    Douglas Lowry   May 12 92
  19.  *              Copyright (C) 1992 Marpex Inc.
  20.  *
  21.  *    The MIR (Mass Indexing and Retrieval) Tutorials explain detailed
  22.  *    usage and co-ordination of the MIR family of programs to analyze,
  23.  *    prepare and index databases (small through gigabyte size), and
  24.  *    how to build integrated retrieval software around the MIR search
  25.  *    engine.  The fifth of the five MIR tutorial series explains how
  26.  *    to extend indexing capability into leading edge search-related
  27.  *    technologies.  For more information, GO IBMPRO on CompuServe;
  28.  *    MIR files are in the DBMS library.  The same files are on the
  29.  *    Canada Remote Systems BBS.  A diskette copy of the Introduction
  30.  *    is available by mail ($10 US... check, Visa or Mastercard);
  31.  *    diskettes with Introduction, Tutorial ONE software and the
  32.  *    shareware Tutorial ONE text cost $29.  Shareware registration
  33.  *    for a tutorial is also $29.
  34.  *
  35.  *    E-mail...
  36.  *                Compuserve  71431,1337
  37.  *                Internet    doug.lowry%canrem.com
  38.  *                UUCP        canrem!doug.lowry
  39.  *                Others:     doug.lowry@canrem.uucp
  40.  *
  41.  *    FAX...                  416 963-5677
  42.  *
  43.  *    "Snail mail"...         Douglas Lowry, Ph.D.
  44.  *                            Marpex Inc.
  45.  *                            5334 Yonge Street, #1102
  46.  *                            North York, Ontario
  47.  *                            Canada  M2N 6M2
  48.  *
  49.  *    Related database consultation and preparation services are
  50.  *    available through:
  51.  *              Innotech Inc., 2001 Sheppard Avenue E., Suite #118,
  52.  *              North York, Ontario  Canada   M2J 4Z7
  53.  *              Tel.  416 492-3838   FAX  416 492-3843
  54.  *
  55.  *  This program is free software; you may redistribute it and/or
  56.  *  modify it under the terms of the GNU General Public License as
  57.  *  published by the Free Software Foundation; either version 2 of
  58.  *  the License, or (at your option) any later version.
  59.  *
  60.  *  This program is distributed in the hope that it will be useful,
  61.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  62.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  63.  *  GNU General Public License for more details.
  64.  *
  65.  *  You should have received a copy of the GNU General Public License
  66.  *  (file 05LICENS) along with this program; if not, write to the
  67.  *  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  68.  *  USA.
  69.  */
  70.  
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <ctype.h>
  74.  
  75. #define     MAX_BYTES   2048
  76. #define     repeat      for(;;)
  77.  
  78. /*
  79.  * declarations 
  80.  */
  81.  
  82. typedef     enum        _bool
  83.              { FALSE = 0, TRUE = 1 }  Bool;
  84.  
  85.     void        Usage_(), process(), puterr() ;
  86.     int         get_data();
  87.     char        *Cmdname_() {    return( "debloc_b" );  }
  88.  
  89.     static long int offset ;
  90.  
  91. /*
  92. /*
  93.  *  MAIN -
  94.  */
  95.  
  96. main( argc, argv )
  97.     int argc;
  98.     char    **argv;
  99. {
  100.     char    c10 ;
  101.     Bool    linefeeds,      /*  insert line feeds   */
  102.             ebcdic ;        /*  convert to binary thru EBCDIC   */
  103.     FILE    *fp ;
  104.     int     i ;
  105.  
  106.     if( argc < 2 || argc > 4 )
  107.         Usage_() ;
  108.     c10 = argv[1][0] ;
  109.     if( c10 == '/' || c10 == '-' || c10 == '?' )
  110.         Usage_() ;
  111.     if (( fp = fopen( argv[ 1 ], "rb" )) == NULL )
  112.     {
  113.         fprintf( stderr, "Unable to open file %s\n", argv[ 1 ] );
  114.         Usage_() ;
  115.     }
  116.  
  117.     linefeeds = TRUE ;
  118.     ebcdic = FALSE ;
  119.     for( i = 2 ; i < argc ; i++ )
  120.     {
  121.         c10 = argv[i][0] ;
  122.         if( !( c10 == '/' || c10 == '-' ))
  123.             Usage_() ;
  124.         if( islower( argv[i][1] ))
  125.             argv[i][1] = toupper( argv[i][1] ) ;
  126.         if( argv[i][1] == 'S' )
  127.             linefeeds = FALSE ;
  128.         else if( argv[i][1] == 'E' )
  129.             ebcdic = TRUE ;
  130.         else
  131.             Usage_() ;
  132.     }
  133.  
  134.     process( fp, linefeeds, ebcdic );
  135.  
  136.     fclose( fp );
  137.     exit( 0 );
  138. }
  139. /*
  140.  *  Usage
  141.  */
  142.     void
  143. Usage_()
  144. {
  145.     fprintf( stderr,
  146. "\nUsage:  %s  binary_blocked_file [/s][/e] > unblocked_version\n\n\
  147.         Remove blocking, and (if not suppressed by /s argument)\n\
  148.         insert line feeds.  Argument /e must be used if file was\n\
  149.         originally EBCDIC, in which case the block lengths must\n",
  150.             Cmdname_() ) ;
  151.     fprintf( stderr,
  152. "        be converted back to EBCDIC before they are interpreted.\n\n\
  153. input:  File with four byte binary inclusive block lengths and\n\
  154.         sub-lengths, two bytes in high to low order, then two NULLs.\n\n" );
  155.     fprintf( stderr,
  156. "output: Same data with counts out, line feeds/carriage returns\n\
  157.         in (unless suppressed).\n\n\
  158. writeup: MIR TUTORIAL ONE, topic 9\n\n" ) ;
  159.     exit( 1 ) ;
  160. }
  161. /*
  162.  *  PROCESS
  163.  */
  164.  
  165.     void
  166. process( fp, linefeeds, ebcdic )
  167.     FILE    *fp ;
  168.     Bool    linefeeds,      /*  insert line feeds   */
  169.             ebcdic ;        /*  convert to binary thru EBCDIC   */
  170. {
  171.     char    buf[ MAX_BYTES ] ;
  172.     int length;     /* length of current line   */
  173.     int j;
  174.  
  175.     offset = 0 ;
  176.  
  177.     while( ( length= get_data( fp, buf, ebcdic )) > -1 )
  178.     {
  179.         for( j= 0; j < length; j++ )
  180.             putchar( buf[ j ] );
  181.         if( linefeeds )
  182.         {
  183.             if( putchar( '\n' ) != '\n' )
  184.             {
  185.                 fprintf( stderr, "FATAL... Unable to write.\n\n" ) ;
  186.                 exit( 1 ) ;
  187.             }
  188.         }
  189.     }
  190.     return;
  191. }
  192. #define     SYNC        1       /*  error codes     */
  193. #define     SIZE        2
  194. #define     LINESYNC    3
  195. #define     OVERSIZE    4
  196. #define     LINEREAD    5
  197.  
  198. /*
  199.  *  GET_DATA
  200.  */
  201.     int
  202. get_data( fp, buf, ebcdic )
  203.     FILE    *fp;
  204.     char    buf[ MAX_BYTES ] ;
  205.     Bool    ebcdic ;        /*  convert to binary thru EBCDIC   */
  206. {
  207.             /*  ASCII to EBCDIC     */
  208.     short   table[ 256 ] = {
  209.   0,  1,  2,  3, 55, 45, 46, 47, 22,  5, 37, 11, 12, 13, 14, 15,
  210.  16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
  211.  64, 79,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
  212. 240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
  213. 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
  214. 215,216,217,226,227,228,229,230,231,232,233, 74,224, 90, 95,109,
  215. 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
  216. 151,152,153,162,163,164,165,166,167,168,169,192,106,208,161,  7,
  217.  32, 33, 34, 35, 36, 21,  6, 23, 40, 41, 42, 43, 44,  9, 10, 27,
  218.  48, 49, 26, 51, 52, 53, 54,  8, 56, 57, 58, 59,  4, 20, 62,225,
  219.  65, 66, 67, 68, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
  220.  88, 89, 98, 99,100,101,102,103,104,105,112,113,114,115,116,117,
  221. 118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
  222. 159,160,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
  223. 184,185,186,187,188,189,190,191,202,203,204,205,206,207,218,219,
  224. 220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255 } ;
  225.  
  226.     static short    latest_block;   /* most recent block size read*/
  227.     static short    block_bytes;    /* cumulative bytes this block*/
  228.     static short    line_bytes; /* cumulative bytes this line   */
  229.     int     length;     /* bytes in current line    */
  230.     int     size1,size2;    /* size bytes from input    */
  231.     short       zilch;      /* used for zero test of two
  232.                                 high bytes      */
  233.  
  234.     if( !offset )       /*  First time of entry only    */
  235.         latest_block= block_bytes = line_bytes=   0;
  236.  
  237.     /*  If block header due to be read, attempt to evaluate it  */
  238.  
  239.     if( block_bytes == latest_block )
  240.     {
  241.         if( ( size1= fgetc( fp )) == EOF ||
  242.             ( size2= fgetc( fp )) == EOF ||
  243.             ( fread( (char *)&zilch, sizeof(short), 1, fp ) < 1 ))
  244.             return( 0 );
  245.         if ( zilch )
  246.             puterr( SYNC ) ;
  247.         if( ebcdic )
  248.             latest_block= ( table[ size1 ]<<8 ) + table[ size2 ];
  249.         else
  250.             latest_block= ( size1 <<8 ) + size2 ;
  251.         block_bytes= 4;
  252.         offset += 4;
  253.     }
  254.  
  255. /*  ...Read in the line size    */
  256.  
  257.     if( ( size1= fgetc( fp )) == EOF ||
  258.         ( size2= fgetc( fp )) == EOF ||
  259.         ( fread( (char *)&zilch, sizeof(short), 1, fp ) < 1 ))
  260.             puterr( SIZE ) ;
  261.     if ( zilch || size1 )
  262.             puterr( LINESYNC );
  263.     if( ebcdic )
  264.         line_bytes= table[ size2 ];
  265.     else
  266.         line_bytes= size2 ;
  267.     offset += 4;
  268.  
  269. /*  Now load the data stream    */
  270.  
  271.     length= line_bytes - 4;
  272.     if ( length > MAX_BYTES )
  273.         puterr( OVERSIZE ) ;
  274.     if ( fread( buf, sizeof(char), length, fp ) < length )
  275.         puterr( LINEREAD );
  276.     offset += length;
  277.  
  278.     return( length );
  279. }
  280. /*
  281.  *  PUTERR
  282.  */
  283.     void
  284. puterr( type )
  285.     int     type ;
  286. {
  287.     switch( type )
  288.     {
  289.     case SYNC:
  290.         fprintf( stderr, "Block size out of sync" ) ;
  291.         break ;
  292.     case SIZE: 
  293.         fprintf( stderr, "Failure reading line size starting" ) ;
  294.         break ;
  295.     case LINESYNC:
  296.         fprintf( stderr, "Line size out of sync" ) ;
  297.         break ;
  298.     case OVERSIZE:
  299.         fprintf( stderr, "Oversize line" ) ;
  300.         break ;
  301.     case LINEREAD: 
  302.         fprintf( stderr, "Failure reading line data starting" ) ;
  303.         break ;
  304.     }
  305.  
  306.     fprintf( stderr, " at byte %ld\n\n", offset ) ;
  307.     exit( 1 ) ;
  308. }