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