/***************************************************************************
uvgetprogargs.h - description
-------------------
begin : Son Okt 13 2002
copyright : (C) 2002 by Werner Schulte
email : werner@schulte.-ac.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef UVGETPROGARGS_H
#define UVGETPROGARGS_H
/**
This Class provides program argument utilities.
@author Werner Schulte (email : werner@schulte-ac.de, homepage : www.uv-ac.de)
@version 0.2.0 - sc - 19.10.2002
This class provides programs with a well organized data structure which
contains the arguments given to the program in an array form.
The array size is 0x100, which means, that 256 argument pairs ars possible.
Each entry constist of an option (string) and an argument string, which are both
dynamically allocated ( size is strlen(argv[x]) + 1 ).
The variable argcnt contains the amount of argument pairs, which are stored in the array.
If either the option entry or the argument entry is empty, a 0 lenght string is used.
Entry beyond argcnt are Null-pointers.
The following paragraph shows the structure definitions and the variable definition
#define UVMAXOPT 0x100
typedef struct
{
char *option, *argument;
} opt;
int argcnt;
opt cont[UVMAXOPT];
int maxargs;
Here comes the example of the usage of uvProgArgs.
popts = new uvProgArgs( argc, argv );
if ( !popts )
{
qwdebug->doDebug( 0, "%s[%d]getArgums : could not allocate popts \n",
__FILE__, __LINE__ );
prArgums();
doQuit( 5 );
}
for ( i = 0; i < popts->argcnt; i++ )
{
// adjust timer
if ( strcmp( popts->cont[i].option, "-t") == 0 )
{
sleeptime = (unsigned long)atol( popts->cont[i].argument ) * 1000L;
if ( sleeptime < 10000 )
sleeptime = 10000;
}
// PID Mode ( check out running process )
else if ( strcmp( popts->cont[i].option, "-p") == 0 )
{
pidmode = 1;
progpid = atoi( popts->cont[i].argument );
}
// File Mode ( results into file instead of stdout )
else if ( strcmp( popts->cont[i].option, "-f") == 0 )
{
filemode = 1;
filename = (char *) malloc ( strlen( popts->cont[i].argument ) + 1);
memset(filename, 0, strlen( popts->cont[i].argument ) + 1);
sprintf( filename, "%s", popts->cont[i].argument );
}
// Output Mode ( OutputFormat )
else if ( strcmp( popts->cont[i].option, "-m") == 0 )
{
outform = (char *) malloc ( strlen( popts->cont[i].argument ) + 1);
memset( outform, 0, strlen( popts->cont[i].argument ) + 1);
sprintf( outform, "%s", popts->cont[i].argument );
}
// difference Mode
else if ( strcmp( popts->cont[i].option, "-d") == 0 )
{
diffmode = atoi( popts->cont[i].argument );
}
}
**/
extern "C"
{
#include
#include
#include
}
#define uvminus 45
#define UVMAXOPT 0x100
typedef struct
{
char *option, *argument;
} opt;
class uvProgArgs
{
public:
int argcnt;
opt cont[UVMAXOPT];
int maxargs;
/**
constructor
@p argc : the from main function transfered argc value
@p *argv[] : the from main function transfered argv value
**/
uvProgArgs( int argc, char *argv[] );
/**
destructor
the destructor frees all malloced strings
**/
~uvProgArgs();
protected:
/**
initializes the array. In fact all entries (option and argument) are set to 0.
**/
void initArgArray();
/**
frees all malloced data (options and arguments).
**/
void freeArgArray();
/**
fills the array.
The array is filled with the array data and ardcnt is incremented.
If an option (e.g. -f) is followed by an argument (e.g. /tmp/abc)
both are stroed in one array entry (option and argument). If either only
argument or option is given, the other entry is filled up with a single
0x0 (empty string).
@p *argv : the argument array.
This leads to the following example
The call
memtrace -f /tmp/abc -t 250 -d /opt/QtPalmtop/bin/qphoto -show
fills the array as follows ...
popts.cont[0].option == "-f", popts.cont[0].argument == "/tmp/abc"
popts.cont[1].option == "-t", popts.cont[0].argument == "250"
popts.cont[2].option == "-d", popts.cont[0].argument == ""
popts.cont[3].option == "", popts.cont[0].argument == "/opt/QtPalmtop/bin/qphoto"
popts.cont[0].option == "-show", popts.cont[0].argument == ""
**/
void fillArgArray( char *argv[] );
private:
int getMaxArgLen();
};
#endif
| Generated by: sc on schomep4 on Sun Nov 23 16:54:46 2003, using kdoc 2.0a54. |