1 /*=========================================================================
4 Module: $RCSfile: gdcmArgMgr.cxx,v $
6 Date: $Date: 2005/06/08 08:06:55 $
7 Version: $Revision: 1.3 $
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"
29 #define ARG_DEFAULT_PARAMOUT "fileout.par"
30 #define ARG_DEFAULT_LOGFILE "gdcm.log"
35 //-------------------------------------------------------------------------
36 // Constructor / Destructor
40 * @param argc arguments count, as passed to main()
41 * @param argv pointers array on the arguments passed to main()
43 ArgMgr::ArgMgr(int argc, char *argv[])
50 /* Read the parameters of the command line *************************/
51 for ( ArgCount=0, nblettre=1 , i=0; i<argc; i++)
53 if ( FiltreLong(argv[i]) )
55 std::cout << "Argument too long ( > "
56 << ARG_LONG_MAX << ")" << std::endl;
59 if ( argv[i][0] == '@' )
61 nblettre += ArgLoadFromFile ( &argv[i][1] );
65 ArgLab [ArgCount] = strcpy ( (char *)malloc(strlen(argv[i])+1), argv[i] ) ;
66 nblettre += 1 + strlen(ArgLab[ArgCount]);
69 if (ArgCount >= ARGMAXCOUNT )
71 std::cout << "Too many Arguments ( more than "
72 << ARGMAXCOUNT << ")" << std::endl;
77 /* Fills an array with the alreadu used parameters ****/
78 ArgUsed = (char *)calloc (1, ArgCount );
80 /* Builds the full string with all the parameters **************/
81 Appel = (char *) calloc (1, nblettre );
83 for ( *Appel = '\0', i=0; i<ArgCount; i++)
85 strcat ( Appel, ArgLab [i] ) ;
86 strcat ( Appel, " " ) ;
89 /* Splitting label from label value *************************************/
90 for ( i=0; i<ArgCount; i++)
92 char * egaloufin = ArgLab[i] ;
93 while ( (*egaloufin != '\0') && (*egaloufin != '=') )
95 if ( *egaloufin ) *(egaloufin++) = '\0';
99 /* Set labels to upper-case (labels are not case sensitive ) *********/
100 for ( i=0; i<ArgCount; i++)
101 ArgLab[i] = Majuscule ( ArgLab[i] ) ;
103 /* Standard arguments are managed by ArgStdArgs **********************/
108 * \brief canonical destructor
112 for(int i=0;i<ArgCount;i++)
122 * \brief checks if a parameter exists in the command line
123 * @param searchParam label name
124 * @return true if parameter 'label' exists
125 * Actually, it returns 0 if label is not found
126 * else, returns the number of the spot it was found last time.
128 int ArgMgr::ArgMgrDefined( char *searchParam )
132 temp = Majuscule ( searchParam ) ;
133 for ( trouve = false, i = ArgCount-1; i>0; i-- )
135 trouve = ! strcmp( ArgLab[i], temp ) ;
139 for ( int j=1; j<i; j++)
141 if ( (!ArgUsed[j])&&(!strcmp(ArgLab[i],ArgLab[j])) )
151 * \brief Gets the parameter value, read on the command line
152 * @param param name of the searched parameter label
153 * @return Value, as a characters string, of the parameter
154 * whose label is given.
156 char *ArgMgr::ArgMgrValue ( char *param )
159 if ( (trouve = ArgMgrDefined ( param )) != false )
160 return ArgStr[trouve] ;
166 * \brief Search for the first not yet used label
167 * @return Pointer to a char array holding the first non used label
169 char *ArgMgr::ArgMgrUnused ( )
172 for ( i=ArgCount-1; i>0; i-- )
176 ArgMgrDefined(ArgLab[i]);
184 * \brief Prints unused labels, if any
185 * @return number of unused labels
187 int ArgMgr::ArgMgrPrintUnusedLabels ()
191 while ( (label=ArgMgrUnused())!=0 )
194 std::cout << "\n Unused Labels:" << std::endl
195 << "==============" << std::endl;
196 std::cout << "Label : " << label << " = "
197 << ArgMgrValue(label) << std::endl;
204 * \brief Prints program usage
205 * @param usage array of pointers to the documentation lines of the program.
206 * @return exception code
208 int ArgMgr::ArgMgrUsage(char **usage_text )
210 while ( *usage_text )
211 std::cout << std::endl << *(usage_text++);
212 std::cout << std::endl;
217 * \brief Forget it, right now ...
218 * Saves a char. array in a parameter file
219 * whose name is given on command line by : PARAMOUT=???
220 * or, as a default, by ARG_DEFAULT_PARAMOUT
221 * @param param char. array that defines the parameter:
222 * @return Entier correspondant au rang dans la liste de labels
224 int ArgMgr::ArgMgrSave ( char *param )
228 if ( *ArgParamOut == '\0' )
232 fd = fopen ( ArgParamOut, "a+" );
237 fd = fopen ( ArgParamOut, "w" );
241 fprintf ( fd, "%s\n", param );
247 * \brief Gets an int value passed as an argument to a program
248 * (use default value if not found)
249 * EXEMPLE: int dimx = ArgMgrGetInt ( "DIMX", 256 );
250 * @param label label name
251 * @param defaultVal default value
252 * @return parameter value
254 int ArgMgr::ArgMgrGetInt(char *label, int defaultVal)
256 return ( (ArgMgrDefined(label))
257 ? (atoi(ArgMgrValue(label)))
262 * \brief Gets a float value passed as an argument to a program
263 * (use default value if not found)
264 * EXEMPLE: float scale = ArgMgrGetFloat ( "SCALE", 0.33 );
265 * @param label label name
266 * @param defaultVal default value
267 * @return parameter value
269 float ArgMgr::ArgMgrGetFloat(char *label, float defaultVal)
271 return ( (ArgMgrDefined(label))
272 ? ((float)atof(ArgMgrValue(label)))
277 * \brief Gets a 'string' value passed as an argument to a program
278 * (use default value if not found)
279 * @param label label name
280 * @param defaultVal default value
281 * @return parameter value
283 char *ArgMgr::ArgMgrGetString(char *label, char *defaultVal)
285 return ( (ArgMgrDefined(label))
286 ? (ArgMgrValue(label))
291 * \brief Gets a value amongst a set of values
292 * (use default value if not found)
293 * EXEMPLE: int nlab = ArgMgrGetLabel("CONFIRM","NO\\YES", 0);
294 * @param label label name
295 * @param liste character Chain describing the various values.
296 * Value are separated by '\\'.
297 * Not case sensitive.
298 * @param val number of default value
299 * @return int : range of value amongst the values list
301 int ArgMgr::ArgMgrGetLabel (char *label, char *liste, int val )
307 tmp = (char *) malloc(strlen(liste)+1);
310 if ( (vallab = ArgMgrGetString(label,(char *)NULL)) != 0 )
312 for ( lab = strtok (tmp,"\\");
314 lab = strtok(0L,"\\"), i++ )
316 if ( strcmp(maj(lab),maj(vallab))==0)
326 * \brief Demands a value amongst a set of values (abort if not found)
327 * EXEMPLE: int nlab = ArgMgrWantLabel("CONFIRM","NO\\YES", usage);
328 * @param label label name
329 * @param liste character Chain describing the various values.
330 * Labels are separated by '\\'.
332 * @param usage Usage program (displayed if label not found)
333 * @return int : range of value amongst the values list
335 int ArgMgr::ArgMgrWantLabel (char *label, char *liste, char *usage[] )
340 if ( (vallab = ArgMgrGetString(label,0)) != 0 )
342 for ( lab = strtok (liste,"\\"); lab != 0; lab = strtok(0L,"\\"), i++ )
343 if ( strcmp(maj(lab),maj(vallab))==0)
352 * \brief Demands an int value passed as an argument to a program
353 * If not found usage is displayed and the prog aborted
354 * EXEMPLE: int dimx = ArgMgrWantInt ( "DIMX", usage );
355 * @param label label name
356 * @param usage Usage program (displayed if label not found)
357 * @return parameter value
359 int ArgMgr::ArgMgrWantInt (char *label, char **usage)
361 return ( (ArgMgrDefined(label) )
362 ? (atoi(ArgMgrValue(label) ) )
363 : (ArgMgrUsage(usage),1) );
367 * \brief Demands a float value passed as an argument to a program
368 * If not found usage is displayed and the prog aborted
369 * EXEMPLE: float scale = ArgMgrWantFloat ( "SCALE", usage );
370 * @param label label name
371 * @param usage Usage program (displayed if label not found)
372 * @return parameter value
374 float ArgMgr::ArgMgrWantFloat (char *label, char **usage)
376 return ( (ArgMgrDefined(label) )
377 ? ((float)atof(ArgMgrValue(label) ) )
378 : (ArgMgrUsage(usage),1.0) );
382 * \brief Demands a 'string' value passed as an argument to a program
383 * If not found usage is displayed and the prog aborted
384 * EXEMPLE: char *code = ArgMgrWantString ( "CODE", usage );
385 * @param label Parameter label
386 * @param usage Usage program (displayed if label not found)
387 * @return parameter value
389 char *ArgMgr::ArgMgrWantString(char *label, char **usage)
391 return ( (ArgMgrDefined(label) )
392 ? (ArgMgrValue(label) )
393 : (ArgMgrUsage(usage),(char*)0) );
397 * \brief decodage des elements d'un argument 'ensemble de STRING' de lgr qcq
398 * @param label label name
399 * @param number nb of found elements
400 * @return Pointer to the array
401 * Pointer NULL if error
403 char **ArgMgr::ArgMgrGetListOfString ( char *label, int *nbElem )
406 char *value = ArgMgrValue(label);
412 *nbElem = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
414 liste = (char **) malloc (sizeof(char*) * taille + strlen(value)+1);
417 value = strcpy( ((char*)liste)+sizeof(char*) * taille, value );
418 for ( elem = liste, chainecur = strtok(value,", ");
420 taille--, chainecur = (chainecur) ? strtok ( 0, ", " ) : 0 )
422 *(elem++) = chainecur;
428 * \brief decodage des elements d'un argument 'liste d'INTEGER' de lgr qcq
429 * @param label label name
430 * @param number nb of found elements
431 * @return Pointer to the array
432 * Pointer NULL if error
434 int *ArgMgr::ArgMgrGetListOfInt ( char *label, int *number )
436 char *value = ArgMgrValue(label);
442 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
444 liste = (int *) calloc (1,sizeof(int)*taille );
452 *(elem++) = (int) strtol ( value, &value, 10 );
453 if ( *value == '\0' )
455 if ( *(value++) != ',' )
466 * \brief decodage des elements d'un argument 'liste de FLOAT' de lgr qcq
467 * @param label label name
468 * @param number taille de la liste trouvee
469 * @return Pointer vers le tableau de lgr 'taille'
472 float *ArgMgr::ArgMgrGetListOfFloat ( char *label, int *number )
474 char *value = ArgMgrValue(label);
480 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
482 liste = (float *) calloc (1,sizeof(float)*taille );
490 *(elem++) = (float) strtod ( value, &value );
491 if ( *value == '\0' )
493 if ( *(value++) != ',' )
504 * \brief Counts the nb of occurrences of a given charact within a 'string'
505 * @param chaine Pointer to the 'string'
506 * @param caract charact to count
507 * @return occurence number
509 int ArgMgr::IdStrCountChar (char *chaine, int caract)
513 for ( ptr = chaine ; *ptr!='\0' ; ptr ++ )
520 * \brief renvoie 1 tableau contenant un ensemble de paires d'entiers
521 * @param value pointer vers la zone allouee contenant les
522 * intervalles (deb1,fin1, deb2,fin2, ...)
523 * @param number Pointer vers le nb de paires trouvees
524 * @return 1 tableau contenant un ensemble de paires d'entiers
526 int *ArgMgr::IdStrIntEnum ( char* value, int *number)
532 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Virgules +1 */
534 liste = (int *) calloc (1,sizeof(int)*2*taille );
542 liste[i] = (int) strtol ( value, &value, 10 );
543 if ( *value == '\0' )
548 if ( *(value++) != '-' )
555 liste[i+1] = (int) strtol ( value, &value, 10 );
557 if ( *value == '\0' )
559 if ( *(value++) != ',' )
570 * \brief renvoie 1 tableau contenant un ensemble de 'paires' de FLOAT
571 * @param value pointer vers la zone allouee contenant les
572 * paires (deb1,fin1, deb2,fin2, ...)
573 * @param number Pointer vers le nb de paires trouvees
574 * @return tableau contenant un ensemble de 'paires' de FLOAT
576 float *ArgMgr::IdStrFloatEnum (char *value, int *number)
581 *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Virgules +1 */
583 liste = (float *) calloc (1,sizeof(float)*2*taille );
589 liste[i] = (float) strtod ( value, &value );
590 if ( *value == '\0' )
595 if ( *(value++) != '-' )
602 liste[i+1] = (float) strtod ( value, &value );
604 if ( *value == '\0' )
606 if ( *(value++) != ',' )
617 * \brief decodage des elements d'un argument 'paires d'int' de lgr quelconque
618 * @param label label name
619 * @param number taille de l'ensemble de paires trouvee
620 * @return Pointer vers le tableau de taille '2*nbElem'
621 * Pointer NULL si erreur
623 int *ArgMgr::ArgMgrGetIntEnum ( char *label, int *number )
625 char *value = ArgMgrValue(label);
629 liste = IdStrIntEnum(value, number);
634 * \brief decodage des elements d'un argument 'paires de float' de lgr quelconque
635 * @param label label name
636 * @param number taille de l'ensemble de paires trouvee
637 * @return Pointer vers le tableau de taille '2*nbElem'
638 * Pointer NULL si erreur
640 float *ArgMgr::ArgMgrGetFloatEnum ( char *label, int *number )
642 char *value = ArgMgrValue(label);
646 liste = IdStrFloatEnum(value, number);
650 //-----------------------------------------------------------------------------
653 //-----------------------------------------------------------------------------
656 /**************************************************************************
658 * Nom de la fonction : Majuscule *
659 * Role ............. : Creates a new Upper case char array. *
660 * parameters ....... : Pointer to the initial char array. * *
661 * Valeur retournee . : Pointer to the new Upper case char array. *
663 **************************************************************************/
664 char *ArgMgr::Majuscule (char *chaine )
666 char *ptr, *ptr2, *ptr3;
667 ptr2 = (char *)malloc(strlen(chaine)*sizeof(char)+1);
669 for ( ptr = chaine ; *ptr!='\0' ; ptr ++ )
671 *ptr3 = toupper ( * ptr ); ptr3++;
677 /**************************************************************************
679 * Nom de la fonction : FiltreLong *
680 * Role ............. : Stops the program if argument is too long. *
681 * ARG_LONG_MAX defines max length. *
682 * parameters ....... : Pointer to the argument. *
683 * Valeur retournee . : false if OK. *
685 **************************************************************************/
686 int ArgMgr::FiltreLong ( char *arg )
689 while ( (n++<ARG_LONG_MAX) && (*(arg++) != '\0') ) ;
690 return (n>=ARG_LONG_MAX) ;
693 /*------------------------------------------------------------------------
694 | Role : Reads a parameter from a file
695 | Retour : Type : char *
696 | Role : pointer to the label
697 | parameters : param : char *
698 | Role : one where the parameter will be stored
700 | Role : File description (assumed to be open)
701 +------------------------------------------------------------------------*/
702 char *ArgMgr::LoadedParam ( char *param, FILE *fd )
709 /* remove spaces at the beginning****/
710 while ( isspace(carlu=fgetc (fd)) );
718 /* Read all the characters */
721 && ( ( (!quote)&&(!isspace(carlu)) )
722 ||( (quote)&& !(carlu=='\"') ) ) )
724 *(car++) = (char) carlu;
726 /* sans depasser la taille max*/
727 if ( nbcar >= ARG_LONG_MAX )
729 std::cout << "\nError: Argument too long ( > "
730 << ARG_LONG_MAX << ")in parameter file."
740 /*------------------------------------------------------------------------
741 | Role : Reading of arguments in a parameter file
742 | (this function is recursive).
743 | Retour : Type : int
744 | Role : length needed to store all the parameters
745 | parameters : filename : char *
746 | Role : parameter File name
748 +------------------------------------------------------------------------*/
749 int ArgMgr::ArgLoadFromFile ( char *filename )
752 char param[ARG_LONG_MAX+1];
755 fch = fopen ( filename, ID_RFILE_TEXT );
756 while ( LoadedParam (param, fch ) )
758 int n = strlen(param);
761 nbl += ArgLoadFromFile ( ¶m[1] );
765 ArgLab [ArgCount] = strcpy ((char *) malloc(n+1), param ) ;
768 if ( ArgCount >= ARGMAXCOUNT )
776 /*------------------------------------------------------------------------
777 | Role : Standard parameters management (on command line)
778 | Retour : Type : void
780 +------------------------------------------------------------------------*/
781 void ArgMgr::ArgStdArgs()
786 if ( (ArgParamOut=ArgMgrValue(ARG_LABEL_PARAMOUT))==0 )
787 ArgParamOut = ARG_DEFAULT_PARAMOUT;
788 if ( (logfile = ArgMgrValue(ARG_LABEL_LOGFILE))!=0)
790 if ( *logfile == '\0' )
791 logfile = ARG_DEFAULT_LOGFILE;
792 fd = fopen ( logfile, "a+" );
795 fprintf ( fd, "%s\n", Appel );
801 /*------------------------------------------------------------------------
802 | Role : Sets in Upper Case.
803 | Retour : Type : char *
804 | parameters : char *
805 +------------------------------------------------------------------------*/
806 char *ArgMgr::maj ( char *a )
811 if ( *b<='z' && *b>='a' ) *b = *b+'A'-'a';
816 //-----------------------------------------------------------------------------
819 //-----------------------------------------------------------------------------
820 } // end namespace gdcm