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

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