📄 gdcmargmgr.cxx
字号:
liste = (int *) calloc (1,sizeof(int)*taille );
if ( !liste )
return 0;
elem = liste;
//*number = 1;
while ( taille>0 )
{
*(elem++) = (int) strtol ( value, &value, 10 );
if ( *value == '\0' )
return liste;
if ( *(value++) != ',' )
{
free (liste);
return 0;
}
taille --;
}
return liste;
}
/**
* \brief decodes and returns an array of 'FLOAT'
* @param label label name
* @param number number of found FLOATs
* @return Pointer to the FLOAT array; NULL if error
*/
float *ArgMgr::ArgMgrGetListOfFloat ( const char *label, int *number )
{
char *value = ArgMgrValue(label);
float *liste;
float *elem;
int taille;
if (!value)
return 0;
*number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
taille= *number;
liste = (float *) calloc (1,sizeof(float)*taille );
if ( !liste )
{
*number = 0;
return 0;
}
elem = liste;
//*number = 1;
while ( taille>0 )
{
*(elem++) = (float) strtod ( value, &value );
if ( *value == '\0' )
return liste;
if ( *(value++) != ',' )
{
free (liste);
return 0;
}
taille --;
}
return liste;
}
/**
* \brief decodes and returns an array of 'INT pairs', passed in decimal
* @param param label name
* @param number nb of found pairs
* @return pointer to the array of 'INT pairs'; NULL if fail
*/
int *ArgMgr::ArgMgrGetIntEnum ( const char *param, int *number )
{
char *value = ArgMgrValue(param);
int *liste;
if (!value)
{
*number = 0;
return 0;
}
liste = IdStrIntEnum(value, number);
return liste;
}
/**
* \brief decodes and returns an array of 'INT16 pairs', passed in hexadecimal
* @param param label name
* @param number nb of found pairs
* @return pointer to the array of 'INT16 pairs'; NULL if fail
*/
uint16_t *ArgMgr::ArgMgrGetXInt16Enum ( const char *param, int *number )
{
char *value = ArgMgrValue(param);
uint16_t *liste;
if (!value)
{
*number = 0;
return 0;
}
liste = IdStrXInt16Enum(value, number);
return liste;
}
/**
* \brief decodes and returns an array of 'FLOAT pairs'
* @param param label name
* @param number nb of found pairs
* @return pointer to the array of 'FLOAT pairs'; NULL if fail
*/
float *ArgMgr::ArgMgrGetFloatEnum ( const char *param, int *number )
{
char *value = ArgMgrValue(param);
float *liste;
if (!value)
{
*number = 0;
return 0;
}
liste = IdStrFloatEnum(value, number);
return liste;
}
// ------------------------ Those are 'service functions' ---------------------
// ------------------------ internal use only ---------------------
/**
* \brief Counts the nb of occurrences of a given charact within a 'string'
* @param chaine Pointer to the 'string'
* @param caract charact to count
* @return occurence number
*/
int ArgMgr::IdStrCountChar (char *chaine, int caract)
{
int i=0;
char *ptr;
for ( ptr = chaine ; *ptr!='\0' ; ptr ++ )
if (*ptr==caract)
i++;
return i;
}
/**
* \brief returns an array of 'INT pairs'
* @param value char array decribing a set of 'INT pairs' (f1-l1, f2-l2, ...)
* @param number nb of found INT pairs
* @return pointer to the array of 'INT pairs'
*/
int *ArgMgr::IdStrIntEnum ( char* value, int *number)
{
int* liste;
int taille;
int i;
*number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
taille= *number;
liste = (int *) calloc (1,sizeof(int)*2*taille );
if ( !liste )
{
return 0;
}
i=0;
while ( taille>0 )
{
liste[i] = (int) strtol ( value, &value, 10 );
if ( *value == '\0' )
{
liste[i+1]=liste[i];
return liste;
}
if ( *(value++) != '-' )
{
liste[i+1]=liste[i];
value--;
}
else
{
liste[i+1] = (int) strtol ( value, &value, 10 );
}
if ( *value == '\0' )
return liste;
if ( *(value++) != ',' )
{
free (liste);
return 0;
}
taille --; i+=2;
}
return liste;
}
/**
* \brief returns an array of set of 'INT16 pairs', passed in Hexadecimal
* @param value char array decribing a set of 'INT16 pairs' (f1-l1, f2-l2, ...)
* coded in hexadecimal e.g. 0x0008,0x00ac
* @param number nb of found pairs
* @return array of set of 'INT16 pairs'
*/
uint16_t *ArgMgr::IdStrXInt16Enum ( char *value, int *number)
{
uint16_t *liste;
int taille;
int i;
*number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
taille= *number;
liste = (uint16_t *) calloc (1,sizeof(uint16_t)*2*taille );
if ( !liste )
{
return 0;
}
i=0;
while ( taille>0 )
{
liste[i] = (uint16_t) strtol ( value, &value, 16 );
if ( *value == '\0' )
{
liste[i+1]=liste[i];
return liste;
}
if ( *(value++) != '-' )
{
liste[i+1]=liste[i];
value--;
}
else
{
liste[i+1] = (uint16_t) strtol ( value, &value, 16 );
}
if ( *value == '\0' )
return liste;
if ( *(value++) != ',' )
{
free (liste);
return 0;
}
taille --; i+=2;
}
return liste;
}
/**
* \brief returns an array of 'FLOAT pairs'
* @param value char array decribing a set of 'FLOAT pairs' (f1-l1, f2-l2, ...)
* @param number nb of found pairs
* @return pointer to the array of 'FLOAT pairs'; NULL if fail
*/
float *ArgMgr::IdStrFloatEnum (char *value, int *number)
{
float *liste;
int taille;
int i;
*number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
taille= *number;
liste = (float *) calloc (1,sizeof(float)*2*taille );
if ( !liste )
return 0;
i=0;
while ( taille>0 )
{
liste[i] = (float) strtod ( value, &value );
if ( *value == '\0' )
{
liste[i+1]=liste[i];
return liste;
}
if ( *(value++) != '-' )
{
liste[i+1]=liste[i];
value--;
}
else
{
liste[i+1] = (float) strtod ( value, &value );
}
if ( *value == '\0' )
return liste;
if ( *(value++) != ',' )
{
free (liste);
return 0;
}
taille --; i+=2;
}
return liste;
}
//-----------------------------------------------------------------------------
// Protected
//-----------------------------------------------------------------------------
// Private
/**************************************************************************
* *
* Nom de la fonction : Majuscule *
* Role ............. : Creates a new Upper case char array. *
* parameters ....... : Pointer to the initial char array. * *
* Valeur retournee . : Pointer to the new Upper case char array. *
* *
**************************************************************************/
char *ArgMgr::Majuscule (const char *chaine )
{
char *ptr, *ptr2, *ptr3;
ptr2 = (char *)malloc(strlen(chaine)*sizeof(char)+1);
ptr3=ptr2;
for ( ptr = const_cast<char *>(chaine) ; *ptr!='\0' ; ptr ++ )
{
*ptr3 = toupper ( * ptr ); ptr3++;
}
*ptr3='\0';
return ptr2;
}
/**************************************************************************
* *
* Nom de la fonction : FiltreLong *
* Role ............. : Stops the program if argument is too long. *
* ARG_LONG_MAX defines max length. *
* parameters ....... : Pointer to the argument. *
* Valeur retournee . : false if OK. *
* true if KO. *
**************************************************************************/
int ArgMgr::FiltreLong ( const char *arg )
{
int n = 0 ;
while ( (n++<ARG_LONG_MAX) && (*(arg++) != '\0') ) ;
return (n>=ARG_LONG_MAX) ;
}
/*------------------------------------------------------------------------
| Role : Reads a parameter from a file
| Return : Type : char *
| Role : pointer to the label
| parameters : param : char *
| Role : one where the parameter will be stored
| fd : FILE *
| Role : File description (assumed to be open)
+------------------------------------------------------------------------*/
const char *ArgMgr::LoadedParam ( const char *param, FILE *fd )
{
int carlu;
char *car = const_cast<char *>(param);
int quote = false;
int nbcar = 0;
/* remove spaces at the beginning****/
while ( isspace(carlu=fgetc (fd)) ) {}
if (carlu==EOF)
return 0;
/* Search for a " */
if ( carlu=='\"' )
{
carlu=fgetc(fd);
quote=true;
/* Read all the characters */
}
while ( (carlu!=EOF)
&& ( ( (!quote)&&(!isspace(carlu)) )
||( (quote)&& !(carlu=='\"') ) ) )
{
*(car++) = (char) carlu;
nbcar ++;
/* sans depasser la taille max*/
if ( nbcar >= ARG_LONG_MAX )
{
std::cout << "\nError: Argument too long ( > "
<< ARG_LONG_MAX << ")in parameter file."
<< std::endl;
break;
}
carlu = fgetc(fd);
}
*car = '\0';
return param;
}
/*------------------------------------------------------------------------
| Role : Reading of arguments in a parameter file
| (this function is recursive).
| Return : Type : int
| Role : length needed to store all the parameters
| parameters : filename : char *
| Role : parameter File name
|
+------------------------------------------------------------------------*/
int ArgMgr::ArgLoadFromFile ( const char *filename )
{
size_t nbl = 0;
char param[ARG_LONG_MAX+1];
FILE *fch;
fch = fopen ( filename, ID_RFILE_TEXT );
while ( LoadedParam (param, fch ) )
{
size_t n = strlen(param);
if ( param[0]=='@' )
{
nbl += ArgLoadFromFile ( ¶m[1] );
}
else
{
ArgLab [ArgCount] = strcpy ((char *) malloc(n+1), param ) ;
nbl += n + 1 ;
ArgCount++;
if ( ArgCount >= ARGMAXCOUNT )
break;
}
}
fclose ( fch );
return static_cast< int >( nbl );
}
/*------------------------------------------------------------------------
| Role : Standard parameters management (on command line)
| Return : Type : void
| parameters : none
+------------------------------------------------------------------------*/
void ArgMgr::ArgStdArgs()
{
char *logfile;
FILE *fd;
if ( (ArgParamOut=ArgMgrValue(const_cast<char*>(ARG_LABEL_PARAMOUT)))==0 )
ArgParamOut = ARG_DEFAULT_PARAMOUT;
if ( (logfile = ArgMgrValue(const_cast<char*>(ARG_LABEL_LOGFILE)))!=0)
{
if ( *logfile == '\0' )
logfile = const_cast<char *>(ARG_DEFAULT_LOGFILE);
fd = fopen ( logfile, "a+" );
if ( fd )
{
fprintf ( fd, "%s\n", Appel );
fclose ( fd );
}
}
}
/*------------------------------------------------------------------------
| Role : Sets in Upper Case.
| Return : Type : char *
| parameters : char *
+------------------------------------------------------------------------*/
char *ArgMgr::maj ( char *a )
{
char *b = a;
while ( *b !=0 )
{
if ( *b<='z' && *b>='a' ) *b = *b+'A'-'a';
b++;
}
return a;
}
//-----------------------------------------------------------------------------
// Print
//-----------------------------------------------------------------------------
} // end namespace gdcm
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -