GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



next up previous contents
Next: LogicalFile Management Up: Some Useful Programs Previous: A cp from the   Contents

gridhexdump, theme and variation

Earlier we gave a code example which created a sort of ``gridhexdump,'' implementing the basic functionality of the command line program hexdump. However the actual program hexdump is a bit more complicated than the one we gave. In particular, the program hexdump allows for various command line options which modify the basic functionality of hexdump. In this example will expand our gridhexdump to take one of these command line options.

The program hexdump has a command line option -s which allows for the user to stipulate where in the specified file hexdump should start reading from. For example the command

% hexdump -s 6 /usr/home/example

would case the program hexdump to start reading data from the 6th byte in the file /usr/home/example. While the command

% hexdump -s 4 /usr/home/example

would case the program hexdump to start reading data from the 4th byte in the file /usr/home/example.

If our example file is the same as before

Hello, cruel world!

then the command

% hexdump -s 4 /usr/home/example

would yield

0000004 6f2c 2063 7275 656c 2077 6f72 6c64 210a
0000014

We will add this optional command to our hexdump in the next code example. The full code for this little trick is below

#include <stdio.h>
#include <stdlib.h>
#include "GAT.h"

int main( int argc, char *argv[] )
{
  int counter;
  char buffer[16];
  GATuint32 offset;
  GATContext context;
  GATuint32 readBytes;  
  GATLocation location;
  GATuint32 newPosition;
  GATFileStream fileStream;
  GATObject fileStreamObject;
  
  /* Check command line syntax */
  if( (4 != argc) && (2 != argc) )
  {
     printf("usage: %s [-s N] file\n", argv[0]);
     
     return 1;
  }
  
  /* Set result to a memory failure */
  result = GAT_MEMORYFAILURE;
  
  /* Create a GATLocation location */
  if( 2 == argc )
  {
    location = GATLocation_Create( argv[1] );
  }
  else
  {
    location = GATLocation_Create( argv[3] );
  }
  
  /* Check GATLocation creation */
  if( NULL != location )
  {
    /* Create GATContext context */
    context = GATContext_Create();
    
    /* Check GATContext creation */
    if( NULL != context )
    {
      /* Create GATFileStream fileStream */
      fileStream  = GATFileStream_Create( context, 
                                          NULL, 
                                          location, 
                                          GATFileStreamMode_Read );
      
      /* Check GATFileStream creation */
      if( NULL != fileStream )
      {
        /* Set offset */
        if( 2 == argc )
        {
          offset = 0;
        }
        else
        {
          offset = (GATuint32) atol( argv[2] );
        }
        
        /* Cast GATFileStream to GATObject */
        fileStreamObject = GATFileStream_ToGATObject( fileStream );
        
        /* Seek to offset */
        result = GATStreamable_Seek( fileStreamObject, GATOrigin_Set, offset, &newPosition );
        
        /* Check previous seek */
        if( GAT_SUCCEEDED( result ) )
        {
        
          /* Read in 16 bytes */
          result = GATStreamable_Read( fileStreamObject, (void *) buffer, 16, &readBytes );
        
          /* Loop until a read failure */
          while( GAT_SUCCEEDED( result ) )
          {
            /* Print out offset */
            printf( "%07x ", offset );
          
            /* Print out data */
            for( count = 0; count < readBytes; count++ )
            {
              printf( "%02x", buffer[count] );
            
              if( 0 == ( (count + 1) % 2 ) )
              {
                printf(" ");
              }
            }
          
            /* Print newline character */
            printf( "\n" );
          
            /* Set offset */
            offset = offset + readBytes;
                    
            /* Read in 16 bytes */
            result =GATStreamable( fileStreamObject, (void *) buffer, 16, &readBytes );
          }
        
        }
        
        /* Destroy GATFileStream */
        GATFileStream_Destroy( &fileStream );
      }
      
      /* Destroy GATContext */
      GATContext_Destroy( &context );
    }
    
    /* Print the total number of bytes */
    if( 0 != offset )
    {
      printf( "%07x\n", offset );
    }
  
    /* Destroy GATLocation */
    GATLocation_Destroy( &location );
  }
  
  /* Check result for success and print error */
  if( GAT_FAILED( result) )
  {
    printf( "An error has occurred\n");
    
    return 1;
  }
    
  return 0;
}

Again as we have reviewed all the various functions in this program previously, we will not belabor the point by reviewing them again here.


next up previous contents
Next: LogicalFile Management Up: Some Useful Programs Previous: A cp from the   Contents
Andre Merzky 2004-05-13