home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / doc / mir / dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-02  |  9.9 KB  |  295 lines

  1. /*
  2.  * Usage -  dump file_name [/a] [ from_byte [ to_byte ] ] > report
  3.  *
  4.  *  DUMP -  Lists the contents of a specified portion of any file,
  5.  *          reporting 16 bytes per line.  "/a" causes accented high
  6.  *          bit characters to be printed.
  7.  *
  8.  *  input:  Any file whatsoever.
  9.  *
  10.  *  output: Printable ASCII report, listing offset, then 16 bytes
  11.  *          in hexadecimal format, with printable ASCII on the
  12.  *          right;  periods substitute for non-printable bytes.
  13.  *
  14.  *  writeup: MIR TUTORIAL ONE, topic 5
  15.  *
  16.  *  Written:    Douglas Lowry   Jan 09 92
  17.  *  Modified:   Douglas Lowry   Feb 28 92  Add /a argument
  18.  *              Copyright (C) 1992 Marpex Inc.
  19.  *
  20.  *    The MIR (Mass Indexing and Retrieval) Tutorials explain detailed
  21.  *    usage and co-ordination of the MIR family of programs to analyze,
  22.  *    prepare and index databases (small through gigabyte size), and
  23.  *    how to build integrated retrieval software around the MIR search
  24.  *    engine.  The fifth of the five MIR tutorial series explains how
  25.  *    to extend indexing capability into leading edge search-related
  26.  *    technologies.  For more information, GO IBMPRO on CompuServe;
  27.  *    MIR files are in the DBMS library.  The same files are on the
  28.  *    Canada Remote Systems BBS.  A diskette copy of the Introduction
  29.  *    is available by mail ($10 US... check, Visa or Mastercard);
  30.  *    diskettes with Introduction, Tutorial ONE software and the
  31.  *    shareware Tutorial ONE text cost $29.  Shareware registration
  32.  *    for a tutorial is also $29.
  33.  *
  34.  *    E-mail...
  35.  *                Compuserve  71431,1337
  36.  *                Internet    doug.lowry%canrem.com
  37.  *                UUCP        canrem!doug.lowry
  38.  *                Others:     doug.lowry@canrem.uucp
  39.  *
  40.  *    FAX...                  416 963-5677
  41.  *
  42.  *    "Snail mail"...         Douglas Lowry, Ph.D.
  43.  *                            Marpex Inc.
  44.  *                            5334 Yonge Street, #1102
  45.  *                            North York, Ontario
  46.  *                            Canada  M2N 6M2
  47.  *
  48.  *    Related database consultation and preparation services are
  49.  *    available through:
  50.  *              Innotech Inc., 2001 Sheppard Avenue E., Suite #118,
  51.  *              North York, Ontario  Canada   M2J 4Z7
  52.  *              Tel.  416 492-3838   FAX  416 492-3843
  53.  *
  54.  *  This program is free software; you may redistribute it and/or
  55.  *  modify it under the terms of the GNU General Public License as
  56.  *  published by the Free Software Foundation; either version 2 of
  57.  *  the License, or (at your option) any later version.
  58.  *
  59.  *  This program is distributed in the hope that it will be useful,
  60.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  61.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  62.  *  GNU General Public License for more details.
  63.  *
  64.  *  You should have received a copy of the GNU General Public License
  65.  *  (file 05LICENS) along with this program; if not, write to the
  66.  *  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  67.  *  USA.
  68.  */
  69.  
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <ctype.h>
  73.  
  74. #define     BIGBUF      2048        /*  must be a multiple of 16 */
  75. #define     repeat      for(;;)
  76.  
  77. /*
  78.  * declarations 
  79.  */
  80.  
  81.     typedef     enum        _bool
  82.              { FALSE = 0, TRUE = 1 }  Bool;
  83.  
  84.     void        Usage_(), process(), dump_line() ;
  85.     char        *Cmdname_() {    return( "dump" );  }
  86.  
  87. /*
  88.  * MAIN
  89.  */
  90.  
  91. main( argc, argv )
  92.     int  argc;
  93.     char **argv;
  94. {
  95.     FILE    *fp ;
  96.     Bool    accent,     /*  user wants accented letters to print */
  97.             got_low ;   /*  already read in a from byte arg      */
  98.     char    c, c10 ;
  99.     long    fr_byte, to_byte;       /*  byte range      */
  100.     int     i ;
  101.  
  102.     /* Usage -  dump file_name [/a] [ from_byte [ to_byte ] ] > report */
  103.  
  104.     c10 = argv[1][0] ;
  105.     if( argc < 2 || argc > 5 || c10 == '-' || c10 == '/' || c10 == '?' )
  106.         Usage_() ;
  107.  
  108.     if(( fp = fopen( argv[1], "rb" )) == NULL )
  109.     {
  110.         fprintf( stderr, "\nUnable to open file %s.\n", argv[1] );
  111.         Usage_();
  112.     }
  113.  
  114.     fr_byte = 0 ;
  115.     to_byte = 0x0fffffff;
  116.     accent = got_low = FALSE ;
  117.     for( i = 2 ; i < argc ; i++ )
  118.     {
  119.         if( argv[i][0] == '-' && ( argv[i][1] == 'a' || argv[i][1] == 'A' ))
  120.             accent = TRUE ;
  121.         else if( got_low )
  122.             to_byte = atol( argv[i] );
  123.         else
  124.         {
  125.             fr_byte = atol( argv[i] );
  126.             got_low = TRUE ;
  127.         }
  128.     }
  129.  
  130.     if( fr_byte )
  131.     {
  132.         if( fseek( fp, fr_byte, SEEK_SET ))
  133.         {
  134.             fprintf( stderr, "Unable to position %s to %ld\n",
  135.                 argv[1], fr_byte );
  136.             Usage_() ;
  137.         }
  138.     }
  139.  
  140.     process( fp, fr_byte, to_byte, accent ) ;
  141.  
  142.     fclose( fp );
  143.     exit( 0 );
  144. }
  145. /*
  146.  *  Usage
  147.  */
  148.     void
  149. Usage_()
  150. {
  151.     fprintf( stderr,
  152. "\nUsage:  %s  file_name [/a] [ from_byte [ to_byte ] ] > report\n\n\
  153.         Lists the contents of a specified portion of any file,\n\
  154.         reporting 16 bytes per line.  \"/a\" causes accented high\n\
  155.         bit characters to be printed.\n\n\
  156. input:  Any file whatsoever.\n\n", Cmdname_() );
  157.     fprintf( stderr,
  158. "Output: Printable ASCII report, listing offset, then 16 bytes\n\
  159.         in hexadecimal format, with printable ASCII on the\n\
  160.         right;  periods substitute for non-printable bytes.\n\n\
  161. writeup: MIR TUTORIAL ONE, topic 5\n\n" ) ;
  162.     exit( 1 ) ;
  163. }
  164. /*
  165.  *  PROCESS -   Passes through file from starting position,
  166.  *              displaying 16 bytes per line.
  167.  */
  168.     void
  169. process( fp, fr_byte, to_byte, accent )
  170.     FILE        *fp ;
  171.     long int    fr_byte,    /*  beginning offset        */
  172.                 to_byte ;   /*  ending offset           */
  173.     Bool        accent;     /*  user wants accented letters to print */
  174. {
  175.     unsigned char   buffer[ BIGBUF ];
  176.     long int        offset;     /*  cumulative bytes into file   */
  177.     int             length,     /*  of buffer contents           */
  178.                     i, j, pt ;
  179.  
  180.     offset = fr_byte ;
  181.  
  182.     repeat
  183.     {
  184.         if( offset > to_byte )
  185.             break ;
  186.         length = fread( buffer, sizeof( char ), BIGBUF, fp );
  187.         if( !length )
  188.             break ;
  189.  
  190.             /*  After the end of last buffer in the file, */
  191.             /*  reduce any trailing bytes to NULLs.       */
  192.  
  193.         if( length < BIGBUF )
  194.         {
  195.             for( i= length, j= 0 ; ( i < BIGBUF && j < 16 ) ; i++, j++ )
  196.                 buffer[i] = '\0' ;
  197.         }
  198.  
  199.         for( pt = 0 ; pt < length ; pt += 16 )
  200.         {
  201.             dump_line( stdout, offset, &buffer[ pt ], accent ) ;
  202.             offset += 16 ;
  203.             if( offset > to_byte )
  204.                 break ;
  205.         }
  206.     }
  207.     return;
  208. }
  209. /*
  210.  *  DUMP_LINE   Output an offset followed by 16 bytes, first in
  211.  *              hexadecimal, then in printable form, with periods
  212.  *              substituting for non-printable characters
  213.  */
  214.     void
  215. dump_line( fp_out, offset, buf, accent )
  216.     FILE        *fp_out ;
  217.     long int    offset ;
  218.     unsigned char   *buf ;
  219.     Bool        accent;     /*  user wants accented letters to print */
  220. {
  221.  
  222. #define     NON_PRINT       0
  223. #define     WHITE_SPACE     1
  224. #define     PUNCTUATION     2
  225. #define     DIGIT           3
  226. #define     CONSONANT       4
  227. #define     VOWEL           5
  228. #define     HI_CONSONANT    6
  229. #define     HI_VOWEL        7
  230. #define     TYPE_CT         8       /*  count of above types    */
  231.  
  232.     static unsigned char   table[256] = {
  233.          0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, /* ctls */
  234.          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ctls */
  235. /*      bl  !  "  #  $  %  &  '  (  )  *  +  ,  -  .  /     */
  236.          1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 2,
  237. /*       0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?     */
  238.          3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2,
  239. /*       @  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O     */
  240.          4, 5, 4, 4, 4, 5, 4, 4, 4, 5, 4, 4, 4, 4, 4, 5,
  241. /*       P  Q  R  S  T  U  V  W  X  Y  Z  [  \  ]  ^  _     */
  242.          4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 2, 2, 2, 2, 2,
  243. /*       `  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o     */
  244.          2, 5, 4, 4, 4, 5, 4, 4, 4, 5, 4, 4, 4, 4, 4, 5,
  245. /*       p  q  r  s  t  u  v  w  x  y  z  {  |  }  ~  NULL  */
  246.          4, 4, 4, 4, 4, 5, 4, 4, 4, 5, 4, 2, 2, 2, 2, 0,
  247. /*       Ç  ü  é  â  ä  à  å  ç  ê  ë  è  ï  î  ì  Ä  Å     */
  248.          6, 7, 7, 7, 7, 7, 7, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  249. /*       É  æ  Æ  ô  ö  ò  û  ù  ÿ  Ö  Ü  ¢  £  ¥  ₧  ƒ     */
  250.          7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0,
  251. /*       á  í  ó  ú  ñ  Ñ  ª  º  ¿  ⌐  ¬  ½  ¼  ¡  «  »     */
  252.          7, 7, 7, 7, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  253.          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  254.          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  255.          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  256.          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  257.          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  258.             };
  259.  
  260.     int         type,   /*  of character per above table    */
  261.                 i ;
  262.     unsigned char       c ;
  263.  
  264.     fprintf( fp_out, "%08ld: ", offset );
  265.     for( i = 0 ; i < 16 ; i++ )
  266.         fprintf( fp_out, "%02x ", buf[ i ] );
  267.     fprintf( fp_out, "   " );
  268.     for( i = 0 ; i < 16 ; i++ )
  269.     {
  270.         c = buf[ i ] ;
  271.         type = table[ c ] ;
  272.         if( type == WHITE_SPACE )
  273.             c = ' ' ;
  274.         if( type == NON_PRINT )
  275.             c = '.' ;
  276.         if( !accent )
  277.         {
  278.             if( type == HI_CONSONANT || type == HI_VOWEL )
  279.                 c = '.' ;
  280.         }
  281.         fputc( c, fp_out );
  282.     }
  283.  
  284.     /*  If we test the output once per line, */
  285.     /*  that will catch any write errors.    */
  286.  
  287.     if( fputc( '\n', fp_out ) != '\n' )
  288.     {
  289.         fprintf( stderr, "FATAL... Unable to write output.\n\n" );
  290.         exit( 1 );
  291.     }
  292.  
  293.     return ;
  294. }
  295.