1 /*=========================================================================
4 Module: $RCSfile: gdcmArgMgr.cxx,v $
6 Date: $Date: 2006/01/26 15:52:56 $
7 Version: $Revision: 1.17 $
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 0 if label is not found
121 * else, returns the number of the spot it was found last time.
123 int ArgMgr::ArgMgrDefined( const char *param )
128 temp = Majuscule ( param ) ;
129 for ( i = ArgCount-1; i>0; i-- )
131 trouve = ( strcmp( ArgLab[i], temp )==0 ) ;
135 for ( int j=1; j<i; j++)
137 if ( (!ArgUsed[j])&&(!strcmp(ArgLab[i],ArgLab[j])) )
147 * \brief Gets the parameter value, read on the command line
148 * @param param name of the searched parameter label
149 * @return Value, as a char array, of the parameter
150 * whose label is given.
152 char *ArgMgr::ArgMgrValue ( const char *param )
155 if ( (trouve = ArgMgrDefined ( param )) != false )
156 return ArgStr[trouve] ;
162 * \brief Search for the first not yet used label
163 * @return Pointer to the char array holding the first non used label
165 char *ArgMgr::ArgMgrUnused ( )
168 for ( i=ArgCount-1; i>0; i-- )
172 ArgMgrDefined(ArgLab[i]);
180 * \brief Prints unused labels, if any
181 * @return number of unused labels
183 int ArgMgr::ArgMgrPrintUnusedLabels ()
187 while ( (label=ArgMgrUnused())!=0 )
190 std::cout << "\n Unused Labels:" << std::endl
191 << "==============" << std::endl;
192 std::cout << "Label : " << label << " = "
193 << ArgMgrValue(label) << std::endl;
200 * \brief Prints program usage
201 * @param usage array of pointers to the documentation lines of the program.
202 * @return exception code
204 int ArgMgr::ArgMgrUsage(const char **usage )
207 std::cout << std::endl << *(usage++);
208 std::cout << std::endl;
213 * \brief Forget it, right now ...
214 * Saves a char. array in a parameter file
215 * whose name is given on command line by : PARAMOUT=???
216 * or, as a default, by ARG_DEFAULT_PARAMOUT
217 * @param param char. array that defines the parameter
218 * @return Entier correspondant au rang dans la liste de labels
220 int ArgMgr::ArgMgrSave ( char *param )
224 if ( *ArgParamOut == '\0' )
228 fd = fopen ( ArgParamOut, "a+" );
233 fd = fopen ( ArgParamOut, "w" );
237 fprintf ( fd, "%s\n", param );
243 * \brief Gets an int value passed as an argument to a program
244 * (use default value if not found)
245 * EXAMPLE: int dimx = ArgMgrGetInt ( "DIMX", 256 );
246 * @param label label name
247 * @param defaultVal default value
248 * @return parameter value
250 int ArgMgr::ArgMgrGetInt(const char *label, int defaultVal)
252 return ( (ArgMgrDefined(label))
253 ? (atoi(ArgMgrValue(label)))
258 * \brief Gets a float value passed as an argument to a program
259 * (use default value if not found)
260 * EXAMPLE: float scale = ArgMgrGetFloat ( "SCALE", 0.33 );
261 * @param param label name
262 * @param defaultVal default value
263 * @return parameter value
265 float ArgMgr::ArgMgrGetFloat(const char *param, float defaultVal)
267 return ( (ArgMgrDefined(param))
268 ? ((float)atof(ArgMgrValue(param)))
273 * \brief Gets a 'string' value passed as an argument to a program
274 * (use default value if not found)
275 * EXAMPLE : char *imageName = ArgMgrGetString( "NAME", "test.dcm" );
276 * @param param label name
277 * @param defaultVal default value
278 * @return parameter value
280 const char *ArgMgr::ArgMgrGetString(const char *param, const char *defaultVal)
282 return ( (ArgMgrDefined(param))
283 ? (ArgMgrValue(param))
288 * \brief Gets a value amongst a set of values
289 * (use default value if not found)
290 * EXAMPLE: int nlab = ArgMgrGetLabel("CONFIRM","NO\\YES", 0);
291 * @param param label name
292 * @param liste character Chain describing the various values.
293 * Value are separated by '\\'.
294 * Not case sensitive.
295 * @param val number of default value
296 * @return int : range of value amongst the values list
298 int ArgMgr::ArgMgrGetLabel (const char *param, char *liste, int val )
304 tmp = (char *) malloc(strlen(liste)+1);
307 if ( (vallab = ArgMgrGetString(param,(const char *)NULL)) != 0 )
309 for ( lab = strtok (tmp,"\\");
311 lab = strtok(0L,"\\"), i++ )
313 // strcmp ignoring case
314 if( strcasecmp(lab, vallab) == 0)
324 * \brief Demands a value amongst a set of values (abort if not found)
325 * EXaMPLE: int nlab = ArgMgrWantLabel("CONFIRM","NO\\YES", usage);
326 * @param param label name
327 * @param liste character Chain describing the various values.
328 * Labels are separated by '\\'.
330 * @param usage Usage program (displayed if label not found)
331 * @return int : range of value amongst the values list
333 int ArgMgr::ArgMgrWantLabel (const char *param, char *liste, const char **usage )
338 if ( (vallab = ArgMgrGetString(param,0)) != 0 )
340 for ( lab = strtok (liste,"\\"); lab != 0; lab = strtok(0L,"\\"), i++ )
341 if ( strcasecmp(lab,vallab)==0)
350 * \brief Demands an int value passed as an argument to a program
351 * If not found usage is displayed and the prog aborted
352 * EXAMPLE: int dimx = ArgMgrWantInt ( "DIMX", usage );
353 * @param label label name
354 * @param usage Usage program (displayed if label not found)
355 * @return parameter value
357 int ArgMgr::ArgMgrWantInt (const char *label, const char **usage)
359 return ( (ArgMgrDefined(label) )
360 ? (atoi(ArgMgrValue(label) ) )
361 : (ArgMgrUsage(usage),1) );
365 * \brief Demands a float value passed as an argument to a program
366 * If not found usage is displayed and the prog aborted
367 * EXAMPLE: float scale = ArgMgrWantFloat ( "SCALE", usage );
368 * @param label label name
369 * @param usage Usage program (displayed if label not found)
370 * @return parameter value
372 float ArgMgr::ArgMgrWantFloat (const char *label, const char **usage)
374 return ( (ArgMgrDefined(label) )
375 ? ((float)atof(ArgMgrValue(label) ) )
376 : (ArgMgrUsage(usage),(float)1.0) );
380 * \brief Demands a 'string' value passed as an argument to a program
381 * If not found usage is displayed and the prog aborted
382 * EXAMPLE: char *code = ArgMgrWantString ( "CODE", usage );
383 * @param label Parameter label
384 * @param usage Usage program (displayed if label not found)
385 * @return parameter value
387 char *ArgMgr::ArgMgrWantString(const char *label, const char **usage)
389 return ( (ArgMgrDefined(label) )
390 ? (ArgMgrValue(label) )
391 : (ArgMgrUsage(usage),(char*)0) );
395 * \brief decodes and returns an array of 'STRING'
396 * EXAMPLE: char **codes = ArgMgrGetListOfString ( "CODES", &nbOfCodes );
397 * @param label label name
398 * @param number nb of found 'STRINGs'
399 * @return Pointer to the 'STRING' array; NULL if error
401 char **ArgMgr::ArgMgrGetListOfString ( const char *label, int *number )
404 char *value = ArgMgrValue(label);
413 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
415 liste = (char **) malloc (sizeof(char*) * taille + strlen(value)+1);
418 value = strcpy( ((char*)liste)+sizeof(char*) * taille, value );
419 for ( elem = liste, chainecur = strtok(value,", ");
421 taille--, chainecur = (chainecur) ? strtok ( 0, ", " ) : 0 )
423 *(elem++) = chainecur;
429 * \brief decodes and returns an array of 'INT'
430 * EXAMPLE: int *points = ArgMgrGetListOfInt ( "POINTS", &nbOfPoints );
431 * @param label label name
432 * @param number nb of found INT
433 * @return Pointer to the INT array; NULL if error
435 int *ArgMgr::ArgMgrGetListOfInt ( const char *label, int *number )
437 char *value = ArgMgrValue(label);
446 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
448 liste = (int *) calloc (1,sizeof(int)*taille );
456 *(elem++) = (int) strtol ( value, &value, 10 );
457 if ( *value == '\0' )
459 if ( *(value++) != ',' )
470 * \brief decodes and returns an array of 'FLOAT'
471 * @param label label name
472 * @param number number of found FLOATs
473 * @return Pointer to the FLOAT array; NULL if error
475 float *ArgMgr::ArgMgrGetListOfFloat ( const char *label, int *number )
477 char *value = ArgMgrValue(label);
483 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
485 liste = (float *) calloc (1,sizeof(float)*taille );
496 *(elem++) = (float) strtod ( value, &value );
497 if ( *value == '\0' )
499 if ( *(value++) != ',' )
510 * \brief decodes and returns an array of 'INT pairs', passed in decimal
511 * @param param label name
512 * @param number nb of found pairs
513 * @return pointer to the array of 'INT pairs'; NULL if fail
515 int *ArgMgr::ArgMgrGetIntEnum ( const char *param, int *number )
517 char *value = ArgMgrValue(param);
524 liste = IdStrIntEnum(value, number);
529 * \brief decodes and returns an array of 'INT16 pairs', passed in hexadecimal
530 * @param param label name
531 * @param number nb of found pairs
532 * @return pointer to the array of 'INT16 pairs'; NULL if fail
534 uint16_t *ArgMgr::ArgMgrGetXInt16Enum ( const char *param, int *number )
536 char *value = ArgMgrValue(param);
543 liste = IdStrXInt16Enum(value, number);
547 * \brief decodes and returns an array of 'FLOAT pairs'
548 * @param param label name
549 * @param number nb of found pairs
550 * @return pointer to the array of 'FLOAT pairs'; NULL if fail
553 float *ArgMgr::ArgMgrGetFloatEnum ( const char *param, int *number )
555 char *value = ArgMgrValue(param);
562 liste = IdStrFloatEnum(value, number);
566 // ------------------------ Those are 'service functions' ---------------------
567 // ------------------------ internal use only ---------------------
570 * \brief Counts the nb of occurrences of a given charact within a 'string'
571 * @param chaine Pointer to the 'string'
572 * @param caract charact to count
573 * @return occurence number
575 int ArgMgr::IdStrCountChar (char *chaine, int caract)
579 for ( ptr = chaine ; *ptr!='\0' ; ptr ++ )
586 * \brief returns an array of 'INT pairs'
587 * @param value char array decribing a set of 'INT pairs' (f1-l1, f2-l2, ...)
588 * @param number nb of found INT pairs
589 * @return pointer to the array of 'INT pairs'
591 int *ArgMgr::IdStrIntEnum ( char* value, int *number)
597 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
599 liste = (int *) calloc (1,sizeof(int)*2*taille );
607 liste[i] = (int) strtol ( value, &value, 10 );
608 if ( *value == '\0' )
613 if ( *(value++) != '-' )
620 liste[i+1] = (int) strtol ( value, &value, 10 );
622 if ( *value == '\0' )
624 if ( *(value++) != ',' )
635 * \brief returns an array of set of 'INT16 pairs', passed in Hexadecimal
636 * @param value char array decribing a set of 'INT16 pairs' (f1-l1, f2-l2, ...)
637 * coded in hexadecimal e.g. 0x0008,0x00ac
638 * @param number nb of found pairs
639 * @return array of set of 'INT16 pairs'
641 uint16_t *ArgMgr::IdStrXInt16Enum ( char *value, int *number)
647 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
649 liste = (uint16_t *) calloc (1,sizeof(uint16_t)*2*taille );
657 liste[i] = (uint16_t) strtol ( value, &value, 16 );
658 if ( *value == '\0' )
663 if ( *(value++) != '-' )
670 liste[i+1] = (uint16_t) strtol ( value, &value, 16 );
672 if ( *value == '\0' )
674 if ( *(value++) != ',' )
684 * \brief returns an array of 'FLOAT pairs'
685 * @param value char array decribing a set of 'FLOAT pairs' (f1-l1, f2-l2, ...)
686 * @param number nb of found pairs
687 * @return pointer to the array of 'FLOAT pairs'; NULL if fail
689 float *ArgMgr::IdStrFloatEnum (char *value, int *number)
694 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
696 liste = (float *) calloc (1,sizeof(float)*2*taille );
702 liste[i] = (float) strtod ( value, &value );
703 if ( *value == '\0' )
708 if ( *(value++) != '-' )
715 liste[i+1] = (float) strtod ( value, &value );
717 if ( *value == '\0' )
719 if ( *(value++) != ',' )
729 //-----------------------------------------------------------------------------
732 //-----------------------------------------------------------------------------
735 /**************************************************************************
737 * Nom de la fonction : Majuscule *
738 * Role ............. : Creates a new Upper case char array. *
739 * parameters ....... : Pointer to the initial char array. * *
740 * Valeur retournee . : Pointer to the new Upper case char array. *
742 **************************************************************************/
743 char *ArgMgr::Majuscule (const char *chaine )
745 char *ptr, *ptr2, *ptr3;
746 ptr2 = (char *)malloc(strlen(chaine)*sizeof(char)+1);
748 for ( ptr = (char *)chaine ; *ptr!='\0' ; ptr ++ )
750 *ptr3 = toupper ( * ptr ); ptr3++;
756 /**************************************************************************
758 * Nom de la fonction : FiltreLong *
759 * Role ............. : Stops the program if argument is too long. *
760 * ARG_LONG_MAX defines max length. *
761 * parameters ....... : Pointer to the argument. *
762 * Valeur retournee . : false if OK. *
764 **************************************************************************/
765 int ArgMgr::FiltreLong ( char *arg )
768 while ( (n++<ARG_LONG_MAX) && (*(arg++) != '\0') ) ;
769 return (n>=ARG_LONG_MAX) ;
772 /*------------------------------------------------------------------------
773 | Role : Reads a parameter from a file
774 | Return : Type : char *
775 | Role : pointer to the label
776 | parameters : param : char *
777 | Role : one where the parameter will be stored
779 | Role : File description (assumed to be open)
780 +------------------------------------------------------------------------*/
781 const char *ArgMgr::LoadedParam ( const char *param, FILE *fd )
784 char *car = (char *)param;
788 /* remove spaces at the beginning****/
789 while ( isspace(carlu=fgetc (fd)) );
797 /* Read all the characters */
800 && ( ( (!quote)&&(!isspace(carlu)) )
801 ||( (quote)&& !(carlu=='\"') ) ) )
803 *(car++) = (char) carlu;
805 /* sans depasser la taille max*/
806 if ( nbcar >= ARG_LONG_MAX )
808 std::cout << "\nError: Argument too long ( > "
809 << ARG_LONG_MAX << ")in parameter file."
819 /*------------------------------------------------------------------------
820 | Role : Reading of arguments in a parameter file
821 | (this function is recursive).
822 | Return : Type : int
823 | Role : length needed to store all the parameters
824 | parameters : filename : char *
825 | Role : parameter File name
827 +------------------------------------------------------------------------*/
828 int ArgMgr::ArgLoadFromFile ( char *filename )
831 char param[ARG_LONG_MAX+1];
834 fch = fopen ( filename, ID_RFILE_TEXT );
835 while ( LoadedParam (param, fch ) )
837 int n = strlen(param);
840 nbl += ArgLoadFromFile ( ¶m[1] );
844 ArgLab [ArgCount] = strcpy ((char *) malloc(n+1), param ) ;
847 if ( ArgCount >= ARGMAXCOUNT )
855 /*------------------------------------------------------------------------
856 | Role : Standard parameters management (on command line)
857 | Return : Type : void
859 +------------------------------------------------------------------------*/
860 void ArgMgr::ArgStdArgs()
865 if ( (ArgParamOut=ArgMgrValue((char*)ARG_LABEL_PARAMOUT))==0 )
866 ArgParamOut = ARG_DEFAULT_PARAMOUT;
867 if ( (logfile = ArgMgrValue((char*)ARG_LABEL_LOGFILE))!=0)
869 if ( *logfile == '\0' )
870 logfile = (char *)ARG_DEFAULT_LOGFILE;
871 fd = fopen ( logfile, "a+" );
874 fprintf ( fd, "%s\n", Appel );
880 /*------------------------------------------------------------------------
881 | Role : Sets in Upper Case.
882 | Return : Type : char *
883 | parameters : char *
884 +------------------------------------------------------------------------*/
885 char *ArgMgr::maj ( char *a )
890 if ( *b<='z' && *b>='a' ) *b = *b+'A'-'a';
895 //-----------------------------------------------------------------------------
898 //-----------------------------------------------------------------------------
899 } // end namespace gdcm