[Phission-cvs] phission/base/src phArgTable.cpp,NONE,1.1
Brought to you by:
pthoren
From: Philip T. <pt...@us...> - 2004-10-18 21:52:46
|
Update of /cvsroot/phission/phission/base/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17073/src Added Files: phArgTable.cpp Log Message: Going to be used in the tests/examples. Just to make the argument parsing a bit easier. Not meant to be very comprehensive --- NEW FILE: phArgTable.cpp --- /* ----------------------------------------------------------------------- */ /* argtable.c - this code is used for easily constructing a command line * * interface for command line driven programs. * * */ /* ----------------------------------------------------------------------- */ /* Coded by Philip D.S. Thoren (pt...@cs...) */ /* ----------------------------------------------------------------------- */ #include <stdio.h> #include <stdlib.h> #include <phArgTable.h> #include <string.h> #include <util.h> /*--------------------------------------------------------------------------*/ /* This sdtruct holds all the details of the arguments from the command line*/ /*--------------------------------------------------------------------------*/ typedef struct arg_node_t { char *flag; void *pointer; int type; struct arg_node_t *next; } arg_node_struct; /*--------------------------------------------------------------------------*/ /* This structure holds details about the arguments being passed into * the program and how many parameters there are */ /*--------------------------------------------------------------------------*/ struct argument_table_t{ arg_node_struct *list; int total_arguments; }; /*---------------------------------------------------------------------------*/ static int ph_copy_string( char *orig, char **copy); static int ph_parse_arguments( int argc, char **argv, struct argument_table_t *table); static int ph_add_argument( const char *flag, void *pointer, int type, struct argument_table_t *table ); static int ph_free_argument_table( struct argument_table_t **table ); static int ph_new_argument_table( struct argument_table_t **table ); /*---------------------------------------------------------------------------*/ /* This function copies orig by allocating new memory and setting copy to * point to that lcoation */ /*---------------------------------------------------------------------------*/ int ph_copy_string( char *orig, char **copy) { const char *function = "ph_copy_string"; int rc = 0; /* Make sure the parameters are valid */ CHECK_NULLPTR(orig,NULL,"Invalid function parameter: char *orig"); CHECK_NULLPTR(copy,NULL,"Invalid function parameter: char **copy"); /* Allocate memory for the copied string */ (*copy) = (char *)phCalloc(strlen(orig) + 5, sizeof(char)); /* Check the allocation */ CHECK_NULLPTR((*copy),"phCalloc","allocation of address string failed"); /* Copy the string */ strncpy((*copy),orig,strlen(orig)); return phSUCCESS; error: return phFAIL; } /*---------------------------------------------------------------------------*/ /* This function takes the arguments from the command line and an Argument * Table that has been set up in order to get switches on the command line * and put the information into the appropriate variables */ /*---------------------------------------------------------------------------*/ int ph_parse_arguments( int argc, char **argv, struct argument_table_t *table) { const char *function = "ph_parse_arguments"; int rc = 0; int i = 1; arg_node_struct *node = NULL; CHECK_NULLPTR(table,NULL,"Invalid function parameter: table"); /* Go through all the arguments passed to the program*/ while ( i < argc ) { /* Initialize the node pointer to the start of the list */ node = table->list; /* Go through all the arguments available */ while (node != NULL) { /* Check for a match */ //if (strncmp(argv[i],node->flag,strlen(node->flag)) == 0) { DEBUG_PRINT("Checking - %s against %s\n",argv[i],node->flag); if (strncmp(argv[i],node->flag,strlen(node->flag)) == 0) { /* Since the existance of the flag/switch... */ if (node->type == phARG_BOOL) { DEBUG_PRINT( "Parsed phARG_BOOL - %s\n",argv[i]); /* ... tells that the user wants something on */ /* then turn it on, and leave the next arg alone */ /* The fact the switch is around means it's on */ /* it's a BOOL: no flag == 0. flag == 1 */ (*((int*)(node->pointer))) = 1; node = NULL; continue; } else if (node->type == phARG_FUNC) { /* convert the argument to a long number and set it */ ph_argtable_funcptr func = (ph_argtable_funcptr)(node->pointer); (func)(); } /* Check to make sure there are enough arguments */ if (argc <= (i+1)) { printf("\n%s - needs more arguments.\n",node->flag); printf("Not enough arguments passed to program.\n"); return phFAIL; } /* Check the type of the argument for what to do with it */ switch (node->type) { /* ----------------------------------------------------- */ case phARG_CHAR: { DEBUG_PRINT( "Parsed phARG_CHAR - %s\n",argv[i]); /* Copy the string */ if (ph_copy_string(argv[++i],(char **)node->pointer) == phFAIL) return phFAIL; } break; /* ----------------------------------------------------- */ case phARG_INT: { DEBUG_PRINT( "Parsed phARG_INT - %s\n",argv[i]); /* convert the argument to a number and set it */ (*((int*)(node->pointer))) = atoi(argv[++i]); } break; /* ----------------------------------------------------- */ case phARG_UINT: { DEBUG_PRINT( "Parsed phARG_UINT - %s\n",argv[i]); long tempnum = atol(argv[++i]); CHECK_RC(tempnum,NULL, "argument is unsigned. Must be > 0.\n"); /* convert the argument to an unsigned integer and set it */ (*((unsigned int*)(node->pointer))) = (unsigned int)tempnum; } break; /* ----------------------------------------------------- */ case phARG_LONG: { DEBUG_PRINT( "Parsed phARG_LONG - %s\n",argv[i]); /* convert the argument to a long number and set it */ (*((long*)(node->pointer))) = atol(argv[++i]); } break; /* ----------------------------------------------------- */ case phARG_ULONG: { DEBUG_PRINT( "Parsed phARG_ULONG - %s\n",argv[i] ); /* convert the argument to a long number and set it */ (*((unsigned long*)(node->pointer))) = (unsigned long)atol(argv[++i]); } break; /* ----------------------------------------------------- */ default: /* Don't fail here CHECK_RC(-1,NULL, "default in switch reached. Invalid argument"); */ break; } node = NULL; } else { /* Get the next node */ node = node->next; } } i++; } return phSUCCESS; error: return phFAIL; } /*---------------------------------------------------------------------------*/ /* Add an argument to the Argument Table for the parsing function to match * against when parsing the command line arguments. */ /*---------------------------------------------------------------------------*/ int ph_add_argument( const char *flag, void *pointer, int type, struct argument_table_t *table ) { const char *function = "ph_add_argument"; int rc = 0; arg_node_struct *newNode = NULL; /* Check the arguments for validity */ CHECK_NULLPTR(flag,NULL,"Invalid function parameter: flag"); CHECK_NULLPTR(pointer,NULL,"Invalid function parameter: pointer"); if ((type != phARG_BOOL) && (type != phARG_CHAR) && (type != phARG_INT) && (type != phARG_UINT) && (type != phARG_FUNC) && (type != phARG_LONG) && (type != phARG_ULONG)) { CHECK_RC(-1,NULL, "Invalid type( != phARG_CHAR || phARG_INT || phARG_UINT || phARG_BOOL || phARG_LONG || phARG_ULONG || phARG_FUNC)" ); } CHECK_NULLPTR(table,NULL,"table == NULL" ); /* Allocate space and copy over the argument switch string */ newNode = (arg_node_struct *)phCalloc(1,sizeof(arg_node_struct)); /* allocate space for the flag string */ newNode->flag = (char *)phCalloc(strlen(flag)+2, sizeof(char)); /* Check the allocation */ CHECK_NULLPTR(newNode->flag,"phCalloc", "Couldn't allocate memory for arg node flag." ); /* copy the flag into the struct field */ sprintf(newNode->flag,"%s",flag); /* set pointer to argument that string/int will be copied to */ newNode->pointer = pointer; newNode->type = type; /* keep track of the rest of the list...*/ newNode->next = table->list; /* ...Update the table */ table->list = newNode; table->total_arguments++; return phSUCCESS; error: phFree(newNode); return phFAIL; } /*---------------------------------------------------------------------------*/ /* Free all the structures of the Argument Table. */ /*---------------------------------------------------------------------------*/ int ph_free_argument_table( struct argument_table_t **table ) { const char *function = "ph_free_argument_table"; int rc = 0; arg_node_struct *node = NULL; arg_node_struct *temp = NULL; /* Check the table parameter passed to the function */ CHECK_NULLPTR(table,NULL,"Invalid function parameter: table"); CHECK_NULLPTR((*table),NULL,"Invalid function parameter: *table"); /* Initialize the node pointer */ node = (*table)->list; /* While there are nodes in the list, free them */ while( node != NULL ) { /* Hold a place for the current node */ temp = node; /* get the next node for the next loop around */ node = node->next; /* free the flag, which was allocated in * the ph_add_argument function */ phFree(temp->flag); phFree(temp); } /* Re-initialize the table */ (*table)->list = NULL; (*table)->total_arguments = 0; phFree(*table); return phSUCCESS; error: return phFAIL; } /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ int ph_new_argument_table( struct argument_table_t **table ) { const char *function = "ph_new_argument_table"; int rc = 0; CHECK_NULLPTR(table,NULL,"Invalid function parameter: table"); (*table) = (struct argument_table_t *) phCalloc(1,sizeof(struct argument_table_t)); CHECK_NULLPTR((*table),"phCalloc","Couldn't allocate arg_table."); (*table)->list = NULL; (*table)->total_arguments = 0; return phSUCCESS; error: return phFAIL; } /*---------------------------------------------------------------------------*/ phArgTable::phArgTable() { const char *function = "phArgTable::phArgTable"; int rc = 0; this->m_table = NULL; /*static int ph_new_argument_table( struct argument_table_t **table );*/ rc = ph_new_argument_table(&this->m_table); PRINT_RC(rc,NULL,"ph_new_argument_table"); } /*---------------------------------------------------------------------------*/ phArgTable::~phArgTable() { const char *function = "phArgTable::~phArgTable"; int rc = 0; /* static int ph_free_argument_table( struct argument_table_t **table );*/ rc = ph_free_argument_table(&this->m_table); PRINT_RC(rc,NULL,"ph_free_argument_table"); } /*---------------------------------------------------------------------------*/ int phArgTable::parse( int argc, char *argv[] ) { const char *function = "phArgTable::parse"; int rc = 0; /* static int ph_parse_arguments( int argc, * char **argv, * struct argument_table_t *table); */ rc = ph_parse_arguments( argc, argv, this->m_table); CHECK_RC(rc,NULL,"ph_parse_arguments"); return phSUCCESS; error: return phFAIL; } /*---------------------------------------------------------------------------*/ int phArgTable::add( const char *flag, void *pointer, int type ) { const char *function = "phArgTable::add"; int rc = 0; /* static int ph_add_argument( const char *flag, * void *pointer, * int type, * struct argument_table_t *table ); */ rc = ph_add_argument( flag, pointer, type, this->m_table); CHECK_RC(rc,NULL,"ph_add_argument"); return phSUCCESS; error: return phFAIL; } |