1 /*=========================================================================
4 Module: $RCSfile: gdcmArgMgr.cxx,v $
6 Date: $Date: 2006/01/26 18:34:13 $
7 Version: $Revision: 1.20 $
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 // No strcasecmp in WIN32 world, but stricmp
25 // http://www.opengroup.org/onlinepubs/007908799/xsh/strcasecmp.html
27 #define strcasecmp stricmp
30 #include <string.h> // For strtok and strlen
31 #include <stdlib.h> // For strtol and strtod
33 #include "gdcmArgMgr.h"
37 //-------------------------------------------------------------------------
38 // Constructor / Destructor
42 * @param argc arguments count, as passed to main()
43 * @param argv pointers array on the arguments passed to main()
45 ArgMgr::ArgMgr(int argc, char **argv)
52 /* Read the parameters of the command line *************************/
53 for ( ArgCount=0, nblettre=1 , i=0; i<argc; i++)
55 if ( FiltreLong(argv[i]) )
57 std::cout << "Argument too long ( > "
58 << ARG_LONG_MAX << ")" << std::endl;
61 if ( argv[i][0] == '@' )
63 nblettre += ArgLoadFromFile ( &argv[i][1] );
67 ArgLab [ArgCount] = strcpy ( (char *)malloc(strlen(argv[i])+1), argv[i] ) ;
68 nblettre += 1 + strlen(ArgLab[ArgCount]);
71 if (ArgCount >= ARGMAXCOUNT )
73 std::cout << "Too many Arguments ( more than "
74 << ARGMAXCOUNT << ")" << std::endl;
79 /* Fills an array with the already used parameters ****/
80 ArgUsed = (char *)calloc (1, ArgCount );
82 /* Builds the full string with all the parameters **************/
83 Appel = (char *) calloc (1, nblettre );
85 for ( *Appel = '\0', i=0; i<ArgCount; i++)
87 strcat ( Appel, ArgLab [i] ) ;
88 strcat ( Appel, " " ) ;
91 /* Splitting label from label value *************************************/
92 for ( i=0; i<ArgCount; i++)
94 char * egaloufin = ArgLab[i] ;
95 while ( (*egaloufin != '\0') && (*egaloufin != '=') )
97 if ( *egaloufin ) *(egaloufin++) = '\0';
101 /* Set labels to upper-case (labels are not case sensitive ) *********/
102 for ( i=0; i<ArgCount; i++)
103 ArgLab[i] = Majuscule ( ArgLab[i] ) ;
105 /* Standard arguments are managed by ArgStdArgs **********************/
110 * \brief canonical destructor
114 for(int i=0;i<ArgCount;i++)
124 * \brief checks if a parameter exists in the command line
125 * @param param label name
126 * @return 0 if label is not found
127 * else, returns the number of the spot it was found last time.
129 int ArgMgr::ArgMgrDefined( const char *param )
134 temp = Majuscule ( param ) ;
135 for ( i = ArgCount-1; i>0; i-- )
137 trouve = ( strcmp( ArgLab[i], temp )==0 ) ;
141 for ( int j=1; j<i; j++)
143 if ( (!ArgUsed[j])&&(!strcmp(ArgLab[i],ArgLab[j])) )
153 * \brief Gets the parameter value, read on the command line
154 * @param param name of the searched parameter label
155 * @return Value, as a char array, of the parameter
156 * whose label is given.
158 char *ArgMgr::ArgMgrValue ( const char *param )
161 if ( (trouve = ArgMgrDefined ( param )) != false )
162 return ArgStr[trouve] ;
168 * \brief Search for the first not yet used label
169 * @return Pointer to the char array holding the first non used label
171 const char *ArgMgr::ArgMgrUnused ( )
174 for ( i=ArgCount-1; i>0; i-- )
178 ArgMgrDefined(ArgLab[i]);
186 * \brief Prints unused labels, if any
187 * @return number of unused labels
189 int ArgMgr::ArgMgrPrintUnusedLabels ()
193 while ( (label=ArgMgrUnused())!=0 )
196 std::cout << "\n Unused Labels:" << std::endl
197 << "==============" << std::endl;
198 std::cout << "Label : " << label << " = "
199 << ArgMgrValue(label) << std::endl;
206 * \brief Prints program usage
207 * @param usage array of pointers to the documentation lines of the program.
208 * @return exception code
210 int ArgMgr::ArgMgrUsage(const char **usage )
213 std::cout << std::endl << *(usage++);
214 std::cout << std::endl;
219 * \brief Forget it, right now ...
220 * Saves a char. array in a parameter file
221 * whose name is given on command line by : PARAMOUT=???
222 * or, as a default, by ARG_DEFAULT_PARAMOUT
223 * @param param char. array that defines the parameter
224 * @return Entier correspondant au rang dans la liste de labels
226 int ArgMgr::ArgMgrSave ( const char *param )
230 if ( *ArgParamOut == '\0' )
234 fd = fopen ( ArgParamOut, "a+" );
239 fd = fopen ( ArgParamOut, "w" );
243 fprintf ( fd, "%s\n", param );
249 * \brief Gets an int value passed as an argument to a program
250 * (use default value if not found)
251 * EXAMPLE: int dimx = ArgMgrGetInt ( "DIMX", 256 );
252 * @param label label name
253 * @param defaultVal default value
254 * @return parameter value
256 int ArgMgr::ArgMgrGetInt(const char *label, int defaultVal)
258 return ( (ArgMgrDefined(label))
259 ? (atoi(ArgMgrValue(label)))
264 * \brief Gets a float value passed as an argument to a program
265 * (use default value if not found)
266 * EXAMPLE: float scale = ArgMgrGetFloat ( "SCALE", 0.33 );
267 * @param param label name
268 * @param defaultVal default value
269 * @return parameter value
271 float ArgMgr::ArgMgrGetFloat(const char *param, float defaultVal)
273 return ( (ArgMgrDefined(param))
274 ? ((float)atof(ArgMgrValue(param)))
279 * \brief Gets a 'string' value passed as an argument to a program
280 * (use default value if not found)
281 * EXAMPLE : char *imageName = ArgMgrGetString( "NAME", "test.dcm" );
282 * @param param label name
283 * @param defaultVal default value
284 * @return parameter value
286 const char *ArgMgr::ArgMgrGetString(const char *param, const char *defaultVal)
288 return ( (ArgMgrDefined(param))
289 ? (ArgMgrValue(param))
294 * \brief Gets a value amongst a set of values
295 * (use default value if not found)
296 * EXAMPLE: int nlab = ArgMgrGetLabel("CONFIRM","NO\\YES", 0);
297 * @param param label name
298 * @param liste character Chain describing the various values.
299 * Value are separated by '\\'.
300 * Not case sensitive.
301 * @param val number of default value
302 * @return int : range of value amongst the values list
304 int ArgMgr::ArgMgrGetLabel (const char *param, const char *liste, int val )
310 tmp = (char *) malloc(strlen(liste)+1);
313 if ( (vallab = ArgMgrGetString(param,(const char *)NULL)) != 0 )
315 for ( lab = strtok (tmp,"\\");
317 lab = strtok(0L,"\\"), i++ )
319 // strcmp ignoring case
320 if( strcasecmp(lab, vallab) == 0)
330 * \brief Demands a value amongst a set of values (abort if not found)
331 * EXaMPLE: int nlab = ArgMgrWantLabel("CONFIRM","NO\\YES", usage);
332 * @param param label name
333 * @param liste character Chain describing the various values.
334 * Labels are separated by '\\'.
336 * WARNING this will be changed (not const)
337 * @param usage Usage program (displayed if label not found)
338 * @return int : range of value amongst the values list
340 int ArgMgr::ArgMgrWantLabel (const char *param, char *liste, const char **usage )
345 if ( (vallab = ArgMgrGetString(param,0)) != 0 )
347 for ( lab = strtok (liste,"\\"); lab != 0; lab = strtok(0L,"\\"), i++ )
348 if ( strcasecmp(lab,vallab)==0)
357 * \brief Demands an int value passed as an argument to a program
358 * If not found usage is displayed and the prog aborted
359 * EXAMPLE: int dimx = ArgMgrWantInt ( "DIMX", usage );
360 * @param label label name
361 * @param usage Usage program (displayed if label not found)
362 * @return parameter value
364 int ArgMgr::ArgMgrWantInt (const char *label, const char **usage)
366 return ( (ArgMgrDefined(label) )
367 ? (atoi(ArgMgrValue(label) ) )
368 : (ArgMgrUsage(usage),1) );
372 * \brief Demands a float value passed as an argument to a program
373 * If not found usage is displayed and the prog aborted
374 * EXAMPLE: float scale = ArgMgrWantFloat ( "SCALE", usage );
375 * @param label label name
376 * @param usage Usage program (displayed if label not found)
377 * @return parameter value
379 float ArgMgr::ArgMgrWantFloat (const char *label, const char **usage)
381 return ( (ArgMgrDefined(label) )
382 ? ((float)atof(ArgMgrValue(label) ) )
383 : (ArgMgrUsage(usage),(float)1.0) );
387 * \brief Demands a 'string' value passed as an argument to a program
388 * If not found usage is displayed and the prog aborted
389 * EXAMPLE: char *code = ArgMgrWantString ( "CODE", usage );
390 * @param label Parameter label
391 * @param usage Usage program (displayed if label not found)
392 * @return parameter value
394 char *ArgMgr::ArgMgrWantString(const char *label, const char **usage)
396 return ( (ArgMgrDefined(label) )
397 ? (ArgMgrValue(label) )
398 : (ArgMgrUsage(usage),(char*)0) );
402 * \brief decodes and returns an array of 'STRING'
403 * EXAMPLE: char **codes = ArgMgrGetListOfString ( "CODES", &nbOfCodes );
404 * @param label label name
405 * @param number nb of found 'STRINGs'
406 * @return Pointer to the 'STRING' array; NULL if error
408 char **ArgMgr::ArgMgrGetListOfString ( const char *label, int *number )
411 char *value = ArgMgrValue(label);
420 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
422 liste = (char **) malloc (sizeof(char*) * taille + strlen(value)+1);
425 value = strcpy( ((char*)liste)+sizeof(char*) * taille, value );
426 for ( elem = liste, chainecur = strtok(value,", ");
428 taille--, chainecur = (chainecur) ? strtok ( 0, ", " ) : 0 )
430 *(elem++) = chainecur;
436 * \brief decodes and returns an array of 'INT'
437 * EXAMPLE: int *points = ArgMgrGetListOfInt ( "POINTS", &nbOfPoints );
438 * @param label label name
439 * @param number nb of found INT
440 * @return Pointer to the INT array; NULL if error
442 int *ArgMgr::ArgMgrGetListOfInt ( const char *label, int *number )
444 char *value = ArgMgrValue(label);
453 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
455 liste = (int *) calloc (1,sizeof(int)*taille );
463 *(elem++) = (int) strtol ( value, &value, 10 );
464 if ( *value == '\0' )
466 if ( *(value++) != ',' )
477 * \brief decodes and returns an array of 'FLOAT'
478 * @param label label name
479 * @param number number of found FLOATs
480 * @return Pointer to the FLOAT array; NULL if error
482 float *ArgMgr::ArgMgrGetListOfFloat ( const char *label, int *number )
484 char *value = ArgMgrValue(label);
490 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
492 liste = (float *) calloc (1,sizeof(float)*taille );
503 *(elem++) = (float) strtod ( value, &value );
504 if ( *value == '\0' )
506 if ( *(value++) != ',' )
517 * \brief decodes and returns an array of 'INT pairs', passed in decimal
518 * @param param label name
519 * @param number nb of found pairs
520 * @return pointer to the array of 'INT pairs'; NULL if fail
522 int *ArgMgr::ArgMgrGetIntEnum ( const char *param, int *number )
524 char *value = ArgMgrValue(param);
531 liste = IdStrIntEnum(value, number);
536 * \brief decodes and returns an array of 'INT16 pairs', passed in hexadecimal
537 * @param param label name
538 * @param number nb of found pairs
539 * @return pointer to the array of 'INT16 pairs'; NULL if fail
541 uint16_t *ArgMgr::ArgMgrGetXInt16Enum ( const char *param, int *number )
543 char *value = ArgMgrValue(param);
550 liste = IdStrXInt16Enum(value, number);
554 * \brief decodes and returns an array of 'FLOAT pairs'
555 * @param param label name
556 * @param number nb of found pairs
557 * @return pointer to the array of 'FLOAT pairs'; NULL if fail
560 float *ArgMgr::ArgMgrGetFloatEnum ( const char *param, int *number )
562 char *value = ArgMgrValue(param);
569 liste = IdStrFloatEnum(value, number);
573 // ------------------------ Those are 'service functions' ---------------------
574 // ------------------------ internal use only ---------------------
577 * \brief Counts the nb of occurrences of a given charact within a 'string'
578 * @param chaine Pointer to the 'string'
579 * @param caract charact to count
580 * @return occurence number
582 int ArgMgr::IdStrCountChar (char *chaine, int caract)
586 for ( ptr = chaine ; *ptr!='\0' ; ptr ++ )
593 * \brief returns an array of 'INT pairs'
594 * @param value char array decribing a set of 'INT pairs' (f1-l1, f2-l2, ...)
595 * @param number nb of found INT pairs
596 * @return pointer to the array of 'INT pairs'
598 int *ArgMgr::IdStrIntEnum ( char* value, int *number)
604 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
606 liste = (int *) calloc (1,sizeof(int)*2*taille );
614 liste[i] = (int) strtol ( value, &value, 10 );
615 if ( *value == '\0' )
620 if ( *(value++) != '-' )
627 liste[i+1] = (int) strtol ( value, &value, 10 );
629 if ( *value == '\0' )
631 if ( *(value++) != ',' )
642 * \brief returns an array of set of 'INT16 pairs', passed in Hexadecimal
643 * @param value char array decribing a set of 'INT16 pairs' (f1-l1, f2-l2, ...)
644 * coded in hexadecimal e.g. 0x0008,0x00ac
645 * @param number nb of found pairs
646 * @return array of set of 'INT16 pairs'
648 uint16_t *ArgMgr::IdStrXInt16Enum ( char *value, int *number)
654 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
656 liste = (uint16_t *) calloc (1,sizeof(uint16_t)*2*taille );
664 liste[i] = (uint16_t) strtol ( value, &value, 16 );
665 if ( *value == '\0' )
670 if ( *(value++) != '-' )
677 liste[i+1] = (uint16_t) strtol ( value, &value, 16 );
679 if ( *value == '\0' )
681 if ( *(value++) != ',' )
691 * \brief returns an array of 'FLOAT pairs'
692 * @param value char array decribing a set of 'FLOAT pairs' (f1-l1, f2-l2, ...)
693 * @param number nb of found pairs
694 * @return pointer to the array of 'FLOAT pairs'; NULL if fail
696 float *ArgMgr::IdStrFloatEnum (char *value, int *number)
701 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
703 liste = (float *) calloc (1,sizeof(float)*2*taille );
709 liste[i] = (float) strtod ( value, &value );
710 if ( *value == '\0' )
715 if ( *(value++) != '-' )
722 liste[i+1] = (float) strtod ( value, &value );
724 if ( *value == '\0' )
726 if ( *(value++) != ',' )
736 //-----------------------------------------------------------------------------
739 //-----------------------------------------------------------------------------
742 /**************************************************************************
744 * Nom de la fonction : Majuscule *
745 * Role ............. : Creates a new Upper case char array. *
746 * parameters ....... : Pointer to the initial char array. * *
747 * Valeur retournee . : Pointer to the new Upper case char array. *
749 **************************************************************************/
750 char *ArgMgr::Majuscule (const char *chaine )
752 char *ptr, *ptr2, *ptr3;
753 ptr2 = (char *)malloc(strlen(chaine)*sizeof(char)+1);
755 for ( ptr = (char *)chaine ; *ptr!='\0' ; ptr ++ )
757 *ptr3 = toupper ( * ptr ); ptr3++;
763 /**************************************************************************
765 * Nom de la fonction : FiltreLong *
766 * Role ............. : Stops the program if argument is too long. *
767 * ARG_LONG_MAX defines max length. *
768 * parameters ....... : Pointer to the argument. *
769 * Valeur retournee . : false if OK. *
771 **************************************************************************/
772 int ArgMgr::FiltreLong ( const char *arg )
775 while ( (n++<ARG_LONG_MAX) && (*(arg++) != '\0') ) ;
776 return (n>=ARG_LONG_MAX) ;
779 /*------------------------------------------------------------------------
780 | Role : Reads a parameter from a file
781 | Return : Type : char *
782 | Role : pointer to the label
783 | parameters : param : char *
784 | Role : one where the parameter will be stored
786 | Role : File description (assumed to be open)
787 +------------------------------------------------------------------------*/
788 const char *ArgMgr::LoadedParam ( const char *param, FILE *fd )
791 char *car = (char *)param;
795 /* remove spaces at the beginning****/
796 while ( isspace(carlu=fgetc (fd)) );
804 /* Read all the characters */
807 && ( ( (!quote)&&(!isspace(carlu)) )
808 ||( (quote)&& !(carlu=='\"') ) ) )
810 *(car++) = (char) carlu;
812 /* sans depasser la taille max*/
813 if ( nbcar >= ARG_LONG_MAX )
815 std::cout << "\nError: Argument too long ( > "
816 << ARG_LONG_MAX << ")in parameter file."
826 /*------------------------------------------------------------------------
827 | Role : Reading of arguments in a parameter file
828 | (this function is recursive).
829 | Return : Type : int
830 | Role : length needed to store all the parameters
831 | parameters : filename : char *
832 | Role : parameter File name
834 +------------------------------------------------------------------------*/
835 int ArgMgr::ArgLoadFromFile ( const char *filename )
838 char param[ARG_LONG_MAX+1];
841 fch = fopen ( filename, ID_RFILE_TEXT );
842 while ( LoadedParam (param, fch ) )
844 int n = strlen(param);
847 nbl += ArgLoadFromFile ( ¶m[1] );
851 ArgLab [ArgCount] = strcpy ((char *) malloc(n+1), param ) ;
854 if ( ArgCount >= ARGMAXCOUNT )
862 /*------------------------------------------------------------------------
863 | Role : Standard parameters management (on command line)
864 | Return : Type : void
866 +------------------------------------------------------------------------*/
867 void ArgMgr::ArgStdArgs()
872 if ( (ArgParamOut=ArgMgrValue((char*)ARG_LABEL_PARAMOUT))==0 )
873 ArgParamOut = ARG_DEFAULT_PARAMOUT;
874 if ( (logfile = ArgMgrValue((char*)ARG_LABEL_LOGFILE))!=0)
876 if ( *logfile == '\0' )
877 logfile = (char *)ARG_DEFAULT_LOGFILE;
878 fd = fopen ( logfile, "a+" );
881 fprintf ( fd, "%s\n", Appel );
887 /*------------------------------------------------------------------------
888 | Role : Sets in Upper Case.
889 | Return : Type : char *
890 | parameters : char *
891 +------------------------------------------------------------------------*/
892 char *ArgMgr::maj ( char *a )
897 if ( *b<='z' && *b>='a' ) *b = *b+'A'-'a';
902 //-----------------------------------------------------------------------------
905 //-----------------------------------------------------------------------------
906 } // end namespace gdcm