1 /*=========================================================================
4 Module: $RCSfile: gdcmArgMgr.cxx,v $
6 Date: $Date: 2005/07/30 18:18:44 $
7 Version: $Revision: 1.12 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
22 #include <string.h> // For strlen
24 #include <string.h> // For strtok and strlen
25 #include <stdlib.h> // For strtol and strtod
27 #include "gdcmArgMgr.h"
31 //-------------------------------------------------------------------------
32 // Constructor / Destructor
36 * @param argc arguments count, as passed to main()
37 * @param argv pointers array on the arguments passed to main()
39 ArgMgr::ArgMgr(int argc, char **argv)
46 /* Read the parameters of the command line *************************/
47 for ( ArgCount=0, nblettre=1 , i=0; i<argc; i++)
49 if ( FiltreLong(argv[i]) )
51 std::cout << "Argument too long ( > "
52 << ARG_LONG_MAX << ")" << std::endl;
55 if ( argv[i][0] == '@' )
57 nblettre += ArgLoadFromFile ( &argv[i][1] );
61 ArgLab [ArgCount] = strcpy ( (char *)malloc(strlen(argv[i])+1), argv[i] ) ;
62 nblettre += 1 + strlen(ArgLab[ArgCount]);
65 if (ArgCount >= ARGMAXCOUNT )
67 std::cout << "Too many Arguments ( more than "
68 << ARGMAXCOUNT << ")" << std::endl;
73 /* Fills an array with the already used parameters ****/
74 ArgUsed = (char *)calloc (1, ArgCount );
76 /* Builds the full string with all the parameters **************/
77 Appel = (char *) calloc (1, nblettre );
79 for ( *Appel = '\0', i=0; i<ArgCount; i++)
81 strcat ( Appel, ArgLab [i] ) ;
82 strcat ( Appel, " " ) ;
85 /* Splitting label from label value *************************************/
86 for ( i=0; i<ArgCount; i++)
88 char * egaloufin = ArgLab[i] ;
89 while ( (*egaloufin != '\0') && (*egaloufin != '=') )
91 if ( *egaloufin ) *(egaloufin++) = '\0';
95 /* Set labels to upper-case (labels are not case sensitive ) *********/
96 for ( i=0; i<ArgCount; i++)
97 ArgLab[i] = Majuscule ( ArgLab[i] ) ;
99 /* Standard arguments are managed by ArgStdArgs **********************/
104 * \brief canonical destructor
108 for(int i=0;i<ArgCount;i++)
118 * \brief checks if a parameter exists in the command line
119 * @param param label name
120 * @return false if label is not found
121 * true if label is not found
123 bool ArgMgr::ArgMgrDefined( const char *param )
127 temp = Majuscule ( param ) ;
128 for ( trouve = false, i = ArgCount-1; i>0; i-- )
130 trouve = ! strcmp( ArgLab[i], temp ) ;
134 for ( int j=1; j<i; j++)
136 if ( (!ArgUsed[j])&&(!strcmp(ArgLab[i],ArgLab[j])) )
146 * \brief Gets the parameter value, read on the command line
147 * @param param name of the searched parameter label
148 * @return Value, as a char array, of the parameter
149 * whose label is given.
151 char *ArgMgr::ArgMgrValue ( const char *param )
154 if ( (trouve = ArgMgrDefined ( param )) != false )
155 return ArgStr[trouve] ;
161 * \brief Search for the first not yet used label
162 * @return Pointer to the char array holding the first non used label
164 char *ArgMgr::ArgMgrUnused ( )
167 for ( i=ArgCount-1; i>0; i-- )
171 ArgMgrDefined(ArgLab[i]);
179 * \brief Prints unused labels, if any
180 * @return number of unused labels
182 int ArgMgr::ArgMgrPrintUnusedLabels ()
186 while ( (label=ArgMgrUnused())!=0 )
189 std::cout << "\n Unused Labels:" << std::endl
190 << "==============" << std::endl;
191 std::cout << "Label : " << label << " = "
192 << ArgMgrValue(label) << std::endl;
199 * \brief Prints program usage
200 * @param usage array of pointers to the documentation lines of the program.
201 * @return exception code
203 int ArgMgr::ArgMgrUsage(const char **usage )
206 std::cout << std::endl << *(usage++);
207 std::cout << std::endl;
212 * \brief Forget it, right now ...
213 * Saves a char. array in a parameter file
214 * whose name is given on command line by : PARAMOUT=???
215 * or, as a default, by ARG_DEFAULT_PARAMOUT
216 * @param param char. array that defines the parameter
217 * @return Entier correspondant au rang dans la liste de labels
219 int ArgMgr::ArgMgrSave ( char *param )
223 if ( *ArgParamOut == '\0' )
227 fd = fopen ( ArgParamOut, "a+" );
232 fd = fopen ( ArgParamOut, "w" );
236 fprintf ( fd, "%s\n", param );
242 * \brief Gets an int value passed as an argument to a program
243 * (use default value if not found)
244 * EXAMPLE: int dimx = ArgMgrGetInt ( "DIMX", 256 );
245 * @param label label name
246 * @param defaultVal default value
247 * @return parameter value
249 int ArgMgr::ArgMgrGetInt(const char *label, int defaultVal)
251 return ( (ArgMgrDefined(label))
252 ? (atoi(ArgMgrValue(label)))
257 * \brief Gets a float value passed as an argument to a program
258 * (use default value if not found)
259 * EXAMPLE: float scale = ArgMgrGetFloat ( "SCALE", 0.33 );
260 * @param param label name
261 * @param defaultVal default value
262 * @return parameter value
264 float ArgMgr::ArgMgrGetFloat(const char *param, float defaultVal)
266 return ( (ArgMgrDefined(param))
267 ? ((float)atof(ArgMgrValue(param)))
272 * \brief Gets a 'string' value passed as an argument to a program
273 * (use default value if not found)
274 * EXAMPLE : char *imageName = ArgMgrGetString( "NAME", "test.dcm" );
275 * @param param label name
276 * @param defaultVal default value
277 * @return parameter value
279 char *ArgMgr::ArgMgrGetString(const char *param, char *defaultVal)
281 return ( (ArgMgrDefined(param))
282 ? (ArgMgrValue(param))
287 * \brief Gets a value amongst a set of values
288 * (use default value if not found)
289 * EXAMPLE: int nlab = ArgMgrGetLabel("CONFIRM","NO\\YES", 0);
290 * @param param label name
291 * @param liste character Chain describing the various values.
292 * Value are separated by '\\'.
293 * Not case sensitive.
294 * @param val number of default value
295 * @return int : range of value amongst the values list
297 int ArgMgr::ArgMgrGetLabel (const char *param, char *liste, int val )
303 tmp = (char *) malloc(strlen(liste)+1);
306 if ( (vallab = ArgMgrGetString(param,(char *)NULL)) != 0 )
308 for ( lab = strtok (tmp,"\\");
310 lab = strtok(0L,"\\"), i++ )
312 if ( strcmp(maj(lab),maj(vallab))==0)
322 * \brief Demands a value amongst a set of values (abort if not found)
323 * EXaMPLE: int nlab = ArgMgrWantLabel("CONFIRM","NO\\YES", usage);
324 * @param param label name
325 * @param liste character Chain describing the various values.
326 * Labels are separated by '\\'.
328 * @param usage Usage program (displayed if label not found)
329 * @return int : range of value amongst the values list
331 int ArgMgr::ArgMgrWantLabel (const char *param, char *liste, const char **usage )
336 if ( (vallab = ArgMgrGetString(param,0)) != 0 )
338 for ( lab = strtok (liste,"\\"); lab != 0; lab = strtok(0L,"\\"), i++ )
339 if ( strcmp(maj(lab),maj(vallab))==0)
348 * \brief Demands an int value passed as an argument to a program
349 * If not found usage is displayed and the prog aborted
350 * EXAMPLE: int dimx = ArgMgrWantInt ( "DIMX", usage );
351 * @param label label name
352 * @param usage Usage program (displayed if label not found)
353 * @return parameter value
355 int ArgMgr::ArgMgrWantInt (const char *label, const char **usage)
357 return ( (ArgMgrDefined(label) )
358 ? (atoi(ArgMgrValue(label) ) )
359 : (ArgMgrUsage(usage),1) );
363 * \brief Demands a float value passed as an argument to a program
364 * If not found usage is displayed and the prog aborted
365 * EXAMPLE: float scale = ArgMgrWantFloat ( "SCALE", usage );
366 * @param label label name
367 * @param usage Usage program (displayed if label not found)
368 * @return parameter value
370 float ArgMgr::ArgMgrWantFloat (const char *label, const char **usage)
372 return ( (ArgMgrDefined(label) )
373 ? ((float)atof(ArgMgrValue(label) ) )
374 : (ArgMgrUsage(usage),(float)1.0) );
378 * \brief Demands a 'string' value passed as an argument to a program
379 * If not found usage is displayed and the prog aborted
380 * EXAMPLE: char *code = ArgMgrWantString ( "CODE", usage );
381 * @param label Parameter label
382 * @param usage Usage program (displayed if label not found)
383 * @return parameter value
385 char *ArgMgr::ArgMgrWantString(const char *label, const char **usage)
387 return ( (ArgMgrDefined(label) )
388 ? (ArgMgrValue(label) )
389 : (ArgMgrUsage(usage),(char*)0) );
393 * \brief decodes and returns an array of 'STRING'
394 * EXAMPLE: char **codes = ArgMgrGetListOfString ( "CODES", &nbOfCodes );
395 * @param label label name
396 * @param number nb of found 'STRINGs'
397 * @return Pointer to the 'STRING' array; NULL if error
399 char **ArgMgr::ArgMgrGetListOfString ( const char *label, int *number )
402 char *value = ArgMgrValue(label);
408 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
410 liste = (char **) malloc (sizeof(char*) * taille + strlen(value)+1);
413 value = strcpy( ((char*)liste)+sizeof(char*) * taille, value );
414 for ( elem = liste, chainecur = strtok(value,", ");
416 taille--, chainecur = (chainecur) ? strtok ( 0, ", " ) : 0 )
418 *(elem++) = chainecur;
424 * \brief decodes and returns an array of 'INT'
425 * EXAMPLE: int *points = ArgMgrGetListOfInt ( "POINTS", &nbOfPoints );
426 * @param label label name
427 * @param number nb of found INT
428 * @return Pointer to the INT array; NULL if error
430 int *ArgMgr::ArgMgrGetListOfInt ( const char *label, int *number )
432 char *value = ArgMgrValue(label);
438 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
440 liste = (int *) calloc (1,sizeof(int)*taille );
448 *(elem++) = (int) strtol ( value, &value, 10 );
449 if ( *value == '\0' )
451 if ( *(value++) != ',' )
462 * \brief decodes and returns an array of 'FLOAT'
463 * @param label label name
464 * @param number number of found FLOATs
465 * @return Pointer to the FLOAT array; NULL if error
467 float *ArgMgr::ArgMgrGetListOfFloat ( const char *label, int *number )
469 char *value = ArgMgrValue(label);
475 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
477 liste = (float *) calloc (1,sizeof(float)*taille );
485 *(elem++) = (float) strtod ( value, &value );
486 if ( *value == '\0' )
488 if ( *(value++) != ',' )
499 * \brief decodes and returns an array of 'INT pairs', passed in decimal
500 * @param param label name
501 * @param number nb of found pairs
502 * @return pointer to the array of 'INT pairs'; NULL if fail
504 int *ArgMgr::ArgMgrGetIntEnum ( const char *param, int *number )
506 char *value = ArgMgrValue(param);
513 liste = IdStrIntEnum(value, number);
518 * \brief decodes and returns an array of 'INT16 pairs', passed in hexadecimal
519 * @param param label name
520 * @param number nb of found pairs
521 * @return pointer to the array of 'INT16 pairs'; NULL if fail
523 uint16_t *ArgMgr::ArgMgrGetXInt16Enum ( const char *param, int *number )
525 char *value = ArgMgrValue(param);
532 liste = IdStrXInt16Enum(value, number);
536 * \brief decodes and returns an array of 'FLOAT pairs'
537 * @param param label name
538 * @param number nb of found pairs
539 * @return pointer to the array of 'FLOAT pairs'; NULL if fail
542 float *ArgMgr::ArgMgrGetFloatEnum ( const char *param, int *number )
544 char *value = ArgMgrValue(param);
551 liste = IdStrFloatEnum(value, number);
555 // ------------------------ Those are 'service functions' ---------------------
556 // ------------------------ internal use only ---------------------
559 * \brief Counts the nb of occurrences of a given charact within a 'string'
560 * @param chaine Pointer to the 'string'
561 * @param caract charact to count
562 * @return occurence number
564 int ArgMgr::IdStrCountChar (char *chaine, int caract)
568 for ( ptr = chaine ; *ptr!='\0' ; ptr ++ )
575 * \brief returns an array of 'INT pairs'
576 * @param value char array decribing a set of 'INT pairs' (f1-l1, f2-l2, ...)
577 * @param number nb of found INT pairs
578 * @return pointer to the array of 'INT pairs'
580 int *ArgMgr::IdStrIntEnum ( char* value, int *number)
586 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
588 liste = (int *) calloc (1,sizeof(int)*2*taille );
596 liste[i] = (int) strtol ( value, &value, 10 );
597 if ( *value == '\0' )
602 if ( *(value++) != '-' )
609 liste[i+1] = (int) strtol ( value, &value, 10 );
611 if ( *value == '\0' )
613 if ( *(value++) != ',' )
624 * \brief returns an array of set of 'INT16 pairs', passed in Hexadecimal
625 * @param value char array decribing a set of 'INT16 pairs' (f1-l1, f2-l2, ...)
626 * coded in hexadecimal e.g. 0x0008,0x00ac
627 * @param number nb of found pairs
628 * @return array of set of 'INT16 pairs'
630 uint16_t *ArgMgr::IdStrXInt16Enum ( char *value, int *number)
636 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
638 liste = (uint16_t *) calloc (1,sizeof(uint16_t)*2*taille );
646 liste[i] = (uint16_t) strtol ( value, &value, 16 );
647 if ( *value == '\0' )
652 if ( *(value++) != '-' )
659 liste[i+1] = (uint16_t) strtol ( value, &value, 16 );
661 if ( *value == '\0' )
663 if ( *(value++) != ',' )
673 * \brief returns an array of 'FLOAT pairs'
674 * @param value char array decribing a set of 'FLOAT pairs' (f1-l1, f2-l2, ...)
675 * @param number nb of found pairs
676 * @return pointer to the array of 'FLOAT pairs'; NULL if fail
678 float *ArgMgr::IdStrFloatEnum (char *value, int *number)
683 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
685 liste = (float *) calloc (1,sizeof(float)*2*taille );
691 liste[i] = (float) strtod ( value, &value );
692 if ( *value == '\0' )
697 if ( *(value++) != '-' )
704 liste[i+1] = (float) strtod ( value, &value );
706 if ( *value == '\0' )
708 if ( *(value++) != ',' )
718 //-----------------------------------------------------------------------------
721 //-----------------------------------------------------------------------------
724 /**************************************************************************
726 * Nom de la fonction : Majuscule *
727 * Role ............. : Creates a new Upper case char array. *
728 * parameters ....... : Pointer to the initial char array. * *
729 * Valeur retournee . : Pointer to the new Upper case char array. *
731 **************************************************************************/
732 char *ArgMgr::Majuscule (const char *chaine )
734 char *ptr, *ptr2, *ptr3;
735 ptr2 = (char *)malloc(strlen(chaine)*sizeof(char)+1);
737 for ( ptr = (char *)chaine ; *ptr!='\0' ; ptr ++ )
739 *ptr3 = toupper ( * ptr ); ptr3++;
745 /**************************************************************************
747 * Nom de la fonction : FiltreLong *
748 * Role ............. : Stops the program if argument is too long. *
749 * ARG_LONG_MAX defines max length. *
750 * parameters ....... : Pointer to the argument. *
751 * Valeur retournee . : false if OK. *
753 **************************************************************************/
754 int ArgMgr::FiltreLong ( char *arg )
757 while ( (n++<ARG_LONG_MAX) && (*(arg++) != '\0') ) ;
758 return (n>=ARG_LONG_MAX) ;
761 /*------------------------------------------------------------------------
762 | Role : Reads a parameter from a file
763 | Return : Type : char *
764 | Role : pointer to the label
765 | parameters : param : char *
766 | Role : one where the parameter will be stored
768 | Role : File description (assumed to be open)
769 +------------------------------------------------------------------------*/
770 const char *ArgMgr::LoadedParam ( const char *param, FILE *fd )
773 char *car = (char *)param;
777 /* remove spaces at the beginning****/
778 while ( isspace(carlu=fgetc (fd)) );
786 /* Read all the characters */
789 && ( ( (!quote)&&(!isspace(carlu)) )
790 ||( (quote)&& !(carlu=='\"') ) ) )
792 *(car++) = (char) carlu;
794 /* sans depasser la taille max*/
795 if ( nbcar >= ARG_LONG_MAX )
797 std::cout << "\nError: Argument too long ( > "
798 << ARG_LONG_MAX << ")in parameter file."
808 /*------------------------------------------------------------------------
809 | Role : Reading of arguments in a parameter file
810 | (this function is recursive).
811 | Return : Type : int
812 | Role : length needed to store all the parameters
813 | parameters : filename : char *
814 | Role : parameter File name
816 +------------------------------------------------------------------------*/
817 int ArgMgr::ArgLoadFromFile ( char *filename )
820 char param[ARG_LONG_MAX+1];
823 fch = fopen ( filename, ID_RFILE_TEXT );
824 while ( LoadedParam (param, fch ) )
826 int n = strlen(param);
829 nbl += ArgLoadFromFile ( ¶m[1] );
833 ArgLab [ArgCount] = strcpy ((char *) malloc(n+1), param ) ;
836 if ( ArgCount >= ARGMAXCOUNT )
844 /*------------------------------------------------------------------------
845 | Role : Standard parameters management (on command line)
846 | Return : Type : void
848 +------------------------------------------------------------------------*/
849 void ArgMgr::ArgStdArgs()
854 if ( (ArgParamOut=ArgMgrValue((char*)ARG_LABEL_PARAMOUT))==0 )
855 ArgParamOut = ARG_DEFAULT_PARAMOUT;
856 if ( (logfile = ArgMgrValue((char*)ARG_LABEL_LOGFILE))!=0)
858 if ( *logfile == '\0' )
859 logfile = (char *)ARG_DEFAULT_LOGFILE;
860 fd = fopen ( logfile, "a+" );
863 fprintf ( fd, "%s\n", Appel );
869 /*------------------------------------------------------------------------
870 | Role : Sets in Upper Case.
871 | Return : Type : char *
872 | parameters : char *
873 +------------------------------------------------------------------------*/
874 char *ArgMgr::maj ( char *a )
879 if ( *b<='z' && *b>='a' ) *b = *b+'A'-'a';
884 //-----------------------------------------------------------------------------
887 //-----------------------------------------------------------------------------
888 } // end namespace gdcm