1 /*=========================================================================
4 Module: $RCSfile: gdcmArgMgr.cxx,v $
6 Date: $Date: 2005/09/20 09:21:35 $
7 Version: $Revision: 1.15 $
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 char *ArgMgr::ArgMgrGetString(const char *param, 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,(char *)NULL)) != 0 )
309 for ( lab = strtok (tmp,"\\");
311 lab = strtok(0L,"\\"), i++ )
313 if ( strcmp(maj(lab),maj(vallab))==0)
323 * \brief Demands a value amongst a set of values (abort if not found)
324 * EXaMPLE: int nlab = ArgMgrWantLabel("CONFIRM","NO\\YES", usage);
325 * @param param label name
326 * @param liste character Chain describing the various values.
327 * Labels are separated by '\\'.
329 * @param usage Usage program (displayed if label not found)
330 * @return int : range of value amongst the values list
332 int ArgMgr::ArgMgrWantLabel (const char *param, char *liste, const char **usage )
337 if ( (vallab = ArgMgrGetString(param,0)) != 0 )
339 for ( lab = strtok (liste,"\\"); lab != 0; lab = strtok(0L,"\\"), i++ )
340 if ( strcmp(maj(lab),maj(vallab))==0)
349 * \brief Demands an int value passed as an argument to a program
350 * If not found usage is displayed and the prog aborted
351 * EXAMPLE: int dimx = ArgMgrWantInt ( "DIMX", usage );
352 * @param label label name
353 * @param usage Usage program (displayed if label not found)
354 * @return parameter value
356 int ArgMgr::ArgMgrWantInt (const char *label, const char **usage)
358 return ( (ArgMgrDefined(label) )
359 ? (atoi(ArgMgrValue(label) ) )
360 : (ArgMgrUsage(usage),1) );
364 * \brief Demands a float value passed as an argument to a program
365 * If not found usage is displayed and the prog aborted
366 * EXAMPLE: float scale = ArgMgrWantFloat ( "SCALE", usage );
367 * @param label label name
368 * @param usage Usage program (displayed if label not found)
369 * @return parameter value
371 float ArgMgr::ArgMgrWantFloat (const char *label, const char **usage)
373 return ( (ArgMgrDefined(label) )
374 ? ((float)atof(ArgMgrValue(label) ) )
375 : (ArgMgrUsage(usage),(float)1.0) );
379 * \brief Demands a 'string' value passed as an argument to a program
380 * If not found usage is displayed and the prog aborted
381 * EXAMPLE: char *code = ArgMgrWantString ( "CODE", usage );
382 * @param label Parameter label
383 * @param usage Usage program (displayed if label not found)
384 * @return parameter value
386 char *ArgMgr::ArgMgrWantString(const char *label, const char **usage)
388 return ( (ArgMgrDefined(label) )
389 ? (ArgMgrValue(label) )
390 : (ArgMgrUsage(usage),(char*)0) );
394 * \brief decodes and returns an array of 'STRING'
395 * EXAMPLE: char **codes = ArgMgrGetListOfString ( "CODES", &nbOfCodes );
396 * @param label label name
397 * @param number nb of found 'STRINGs'
398 * @return Pointer to the 'STRING' array; NULL if error
400 char **ArgMgr::ArgMgrGetListOfString ( const char *label, int *number )
403 char *value = ArgMgrValue(label);
409 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
411 liste = (char **) malloc (sizeof(char*) * taille + strlen(value)+1);
414 value = strcpy( ((char*)liste)+sizeof(char*) * taille, value );
415 for ( elem = liste, chainecur = strtok(value,", ");
417 taille--, chainecur = (chainecur) ? strtok ( 0, ", " ) : 0 )
419 *(elem++) = chainecur;
425 * \brief decodes and returns an array of 'INT'
426 * EXAMPLE: int *points = ArgMgrGetListOfInt ( "POINTS", &nbOfPoints );
427 * @param label label name
428 * @param number nb of found INT
429 * @return Pointer to the INT array; NULL if error
431 int *ArgMgr::ArgMgrGetListOfInt ( const char *label, int *number )
433 char *value = ArgMgrValue(label);
439 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
441 liste = (int *) calloc (1,sizeof(int)*taille );
449 *(elem++) = (int) strtol ( value, &value, 10 );
450 if ( *value == '\0' )
452 if ( *(value++) != ',' )
463 * \brief decodes and returns an array of 'FLOAT'
464 * @param label label name
465 * @param number number of found FLOATs
466 * @return Pointer to the FLOAT array; NULL if error
468 float *ArgMgr::ArgMgrGetListOfFloat ( const char *label, int *number )
470 char *value = ArgMgrValue(label);
476 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
478 liste = (float *) calloc (1,sizeof(float)*taille );
486 *(elem++) = (float) strtod ( value, &value );
487 if ( *value == '\0' )
489 if ( *(value++) != ',' )
500 * \brief decodes and returns an array of 'INT pairs', passed in decimal
501 * @param param label name
502 * @param number nb of found pairs
503 * @return pointer to the array of 'INT pairs'; NULL if fail
505 int *ArgMgr::ArgMgrGetIntEnum ( const char *param, int *number )
507 char *value = ArgMgrValue(param);
514 liste = IdStrIntEnum(value, number);
519 * \brief decodes and returns an array of 'INT16 pairs', passed in hexadecimal
520 * @param param label name
521 * @param number nb of found pairs
522 * @return pointer to the array of 'INT16 pairs'; NULL if fail
524 uint16_t *ArgMgr::ArgMgrGetXInt16Enum ( const char *param, int *number )
526 char *value = ArgMgrValue(param);
533 liste = IdStrXInt16Enum(value, number);
537 * \brief decodes and returns an array of 'FLOAT pairs'
538 * @param param label name
539 * @param number nb of found pairs
540 * @return pointer to the array of 'FLOAT pairs'; NULL if fail
543 float *ArgMgr::ArgMgrGetFloatEnum ( const char *param, int *number )
545 char *value = ArgMgrValue(param);
552 liste = IdStrFloatEnum(value, number);
556 // ------------------------ Those are 'service functions' ---------------------
557 // ------------------------ internal use only ---------------------
560 * \brief Counts the nb of occurrences of a given charact within a 'string'
561 * @param chaine Pointer to the 'string'
562 * @param caract charact to count
563 * @return occurence number
565 int ArgMgr::IdStrCountChar (char *chaine, int caract)
569 for ( ptr = chaine ; *ptr!='\0' ; ptr ++ )
576 * \brief returns an array of 'INT pairs'
577 * @param value char array decribing a set of 'INT pairs' (f1-l1, f2-l2, ...)
578 * @param number nb of found INT pairs
579 * @return pointer to the array of 'INT pairs'
581 int *ArgMgr::IdStrIntEnum ( char* value, int *number)
587 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
589 liste = (int *) calloc (1,sizeof(int)*2*taille );
597 liste[i] = (int) strtol ( value, &value, 10 );
598 if ( *value == '\0' )
603 if ( *(value++) != '-' )
610 liste[i+1] = (int) strtol ( value, &value, 10 );
612 if ( *value == '\0' )
614 if ( *(value++) != ',' )
625 * \brief returns an array of set of 'INT16 pairs', passed in Hexadecimal
626 * @param value char array decribing a set of 'INT16 pairs' (f1-l1, f2-l2, ...)
627 * coded in hexadecimal e.g. 0x0008,0x00ac
628 * @param number nb of found pairs
629 * @return array of set of 'INT16 pairs'
631 uint16_t *ArgMgr::IdStrXInt16Enum ( char *value, int *number)
637 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
639 liste = (uint16_t *) calloc (1,sizeof(uint16_t)*2*taille );
647 liste[i] = (uint16_t) strtol ( value, &value, 16 );
648 if ( *value == '\0' )
653 if ( *(value++) != '-' )
660 liste[i+1] = (uint16_t) strtol ( value, &value, 16 );
662 if ( *value == '\0' )
664 if ( *(value++) != ',' )
674 * \brief returns an array of 'FLOAT pairs'
675 * @param value char array decribing a set of 'FLOAT pairs' (f1-l1, f2-l2, ...)
676 * @param number nb of found pairs
677 * @return pointer to the array of 'FLOAT pairs'; NULL if fail
679 float *ArgMgr::IdStrFloatEnum (char *value, int *number)
684 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
686 liste = (float *) calloc (1,sizeof(float)*2*taille );
692 liste[i] = (float) strtod ( value, &value );
693 if ( *value == '\0' )
698 if ( *(value++) != '-' )
705 liste[i+1] = (float) strtod ( value, &value );
707 if ( *value == '\0' )
709 if ( *(value++) != ',' )
719 //-----------------------------------------------------------------------------
722 //-----------------------------------------------------------------------------
725 /**************************************************************************
727 * Nom de la fonction : Majuscule *
728 * Role ............. : Creates a new Upper case char array. *
729 * parameters ....... : Pointer to the initial char array. * *
730 * Valeur retournee . : Pointer to the new Upper case char array. *
732 **************************************************************************/
733 char *ArgMgr::Majuscule (const char *chaine )
735 char *ptr, *ptr2, *ptr3;
736 ptr2 = (char *)malloc(strlen(chaine)*sizeof(char)+1);
738 for ( ptr = (char *)chaine ; *ptr!='\0' ; ptr ++ )
740 *ptr3 = toupper ( * ptr ); ptr3++;
746 /**************************************************************************
748 * Nom de la fonction : FiltreLong *
749 * Role ............. : Stops the program if argument is too long. *
750 * ARG_LONG_MAX defines max length. *
751 * parameters ....... : Pointer to the argument. *
752 * Valeur retournee . : false if OK. *
754 **************************************************************************/
755 int ArgMgr::FiltreLong ( char *arg )
758 while ( (n++<ARG_LONG_MAX) && (*(arg++) != '\0') ) ;
759 return (n>=ARG_LONG_MAX) ;
762 /*------------------------------------------------------------------------
763 | Role : Reads a parameter from a file
764 | Return : Type : char *
765 | Role : pointer to the label
766 | parameters : param : char *
767 | Role : one where the parameter will be stored
769 | Role : File description (assumed to be open)
770 +------------------------------------------------------------------------*/
771 const char *ArgMgr::LoadedParam ( const char *param, FILE *fd )
774 char *car = (char *)param;
778 /* remove spaces at the beginning****/
779 while ( isspace(carlu=fgetc (fd)) );
787 /* Read all the characters */
790 && ( ( (!quote)&&(!isspace(carlu)) )
791 ||( (quote)&& !(carlu=='\"') ) ) )
793 *(car++) = (char) carlu;
795 /* sans depasser la taille max*/
796 if ( nbcar >= ARG_LONG_MAX )
798 std::cout << "\nError: Argument too long ( > "
799 << ARG_LONG_MAX << ")in parameter file."
809 /*------------------------------------------------------------------------
810 | Role : Reading of arguments in a parameter file
811 | (this function is recursive).
812 | Return : Type : int
813 | Role : length needed to store all the parameters
814 | parameters : filename : char *
815 | Role : parameter File name
817 +------------------------------------------------------------------------*/
818 int ArgMgr::ArgLoadFromFile ( char *filename )
821 char param[ARG_LONG_MAX+1];
824 fch = fopen ( filename, ID_RFILE_TEXT );
825 while ( LoadedParam (param, fch ) )
827 int n = strlen(param);
830 nbl += ArgLoadFromFile ( ¶m[1] );
834 ArgLab [ArgCount] = strcpy ((char *) malloc(n+1), param ) ;
837 if ( ArgCount >= ARGMAXCOUNT )
845 /*------------------------------------------------------------------------
846 | Role : Standard parameters management (on command line)
847 | Return : Type : void
849 +------------------------------------------------------------------------*/
850 void ArgMgr::ArgStdArgs()
855 if ( (ArgParamOut=ArgMgrValue((char*)ARG_LABEL_PARAMOUT))==0 )
856 ArgParamOut = ARG_DEFAULT_PARAMOUT;
857 if ( (logfile = ArgMgrValue((char*)ARG_LABEL_LOGFILE))!=0)
859 if ( *logfile == '\0' )
860 logfile = (char *)ARG_DEFAULT_LOGFILE;
861 fd = fopen ( logfile, "a+" );
864 fprintf ( fd, "%s\n", Appel );
870 /*------------------------------------------------------------------------
871 | Role : Sets in Upper Case.
872 | Return : Type : char *
873 | parameters : char *
874 +------------------------------------------------------------------------*/
875 char *ArgMgr::maj ( char *a )
880 if ( *b<='z' && *b>='a' ) *b = *b+'A'-'a';
885 //-----------------------------------------------------------------------------
888 //-----------------------------------------------------------------------------
889 } // end namespace gdcm