Next: Who lives at 1600
Up: Some Useful Programs
Previous: Some Useful Programs
  Contents
This question has often vexed me since my childhood, ``What's big, red, and eats rocks?''
As anyone who grew up with the excellent children's book Big Red Rock Eater
knows, a big red rock eater is big red and eats rocks. ( Sometimes the most complicated
questions have the simplest answers. ) So what does this have to do with GAT much less
the GATAdvertService?
We will next create a command line program, which we will christen brre, pronounced
like ``brie'' and whose letters are an abbreviation for the phrase ``Big Red Rock Eater.''
This program given an set of key/value pairs will print out the POSIX paths of all the
various advertisables which fit the description created by the passed key/value paris.
For example, one could call brre as follows
% brre size=big color=red diet=rocks
The program would then respond with a list of POSIX paths which might look like this
/Users/MrBigRedRockEater
/Users/MrsBigRedRockEater
In addition these command line arguments can also contain POSIX regular expressions
as values, see Appendix for the details of what a
POSIX regular expression is. For example, one might also use brre as follows
% brre size=big color=r* diet=rocks
which might yield the results
/Users/MrsBigRotRockEater
/Users/MrBigRedRockEater
/Users/MrsBigRedRockEater
The full code for the command line tool brre is as follows
#include <stdio.h>
#include <string.h>
#include "GAT.h"
int main( int argc, char *argv[] )
{
int count;
char *key;
char *value;
int returnValue;
GATTable table;
GATResult result;
GATContext context;
GATList_String strings;
GATAdvertService advertService;
GATList_String_Iterator currentIterator;
/* Set result to a memory failure */
result = GAT_MEMORYFAILURE;
/* Create GATTable */
table = GATTable_Create();
/* Check GATTable creation */
if( NULL != table )
{
/* Loop over command line arguments */
for( count = 1; count < argc; count++ )
{
/* Obtain key */
key = strtok( argv[count], "=" );
/* Obtain value */
if( NULL != key )
value = strtok( NULL, "=" );
/* Place key and value in GATTable */
if( (NULL != key) && (NULL != value) )
result = GATTable_Add_String( table, key, value );
/* Print out error */
if( (NULL == key) || (NULL == value) || GAT_FAILED( result ) )
printf( "Error parsing command line argument: %s\n", argv[count] );
}
/* Set result to a memory failure */
result = GAT_MEMORYFAILURE;
/* Create GATContext */
context = GATContext_Create();
/* Check GATContext creation */
if( NULL != context )
{
/* Create GATAdvertService */
advertService = GATAdvertService_Create( context, NULL );
/* Check GATAdvertService creation */
if( NULL != advertService )
{
/* Find Advertisables */
result = GATAdvertService_Find( advertService, table, strings );
/* Check Success of Last Call */
if( GAT_SUCCEEDED( result ) )
{
/* Obtain Beginning GATList_String_Iterator */
currentIterator = GATList_String_Begin( strings );
/* Loop over strings */
while( currentIterator != GATList_String_End( strings ) )
{
/* Print out POSIX path */
printf( "%s\n", GATList_String_Get( currentIterator ) );
/* Increment currentIterator */
currentIterator = GATList_String_Next( currentIterator );
}
/* Destroy GATList_String */
GATList_String_Destroy( &strings );
}
/* Destroy GATAdvertService */
GATAdvertService_Destroy( &advertService );
}
/* Destroy GATContext */
GATContext_Destroy( &context );
}
/* Destroy GATTable */
GATTable_Destroy( &table );
}
/* Set returnValue and print error message */
returnValue = 0;
if( GAT_FAILED( result ) )
{
returnValue = 1;
printf( "There was an error in execution of brre\n" );
}
/* Return to OS */
return returnValue;
}
Next: Who lives at 1600
Up: Some Useful Programs
Previous: Some Useful Programs
  Contents
Andre Merzky
2004-05-13
|