]> Creatis software - gdcm.git/blob - src/gdcmArgMgr.cxx
Fix pb of GetListOfXXX when nothing was found.
[gdcm.git] / src / gdcmArgMgr.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmArgMgr.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/12/22 14:46:06 $
7   Version:   $Revision: 1.16 $
8   
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.
12   
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.
16   
17 =========================================================================*/
18
19 #include <stdio.h>
20 #include <iostream>
21 #include <ctype.h>
22 #include <string.h>  // For strlen
23
24 #include <string.h>  // For strtok and strlen
25 #include <stdlib.h>  // For strtol and strtod
26
27 #include "gdcmArgMgr.h"
28
29 namespace gdcm 
30 {
31 //-------------------------------------------------------------------------
32 // Constructor / Destructor
33
34 /**
35  * \brief   constructor
36  * @param argc arguments count, as passed to main()
37  * @param argv  pointers array on the arguments passed to main()  
38  */
39  ArgMgr::ArgMgr(int argc, char **argv)
40  {
41    int i;
42    int nblettre;
43    ArgUsed = NULL;
44    Appel   = NULL;
45   
46    /* Read the parameters of the command line *************************/
47    for ( ArgCount=0, nblettre=1 , i=0; i<argc; i++) 
48    {
49       if ( FiltreLong(argv[i]) ) 
50       { 
51           std::cout << "Argument too long ( > "
52                     << ARG_LONG_MAX << ")" << std::endl;
53           return;
54       }
55       if ( argv[i][0] == '@' )
56       {                       
57          nblettre  += ArgLoadFromFile ( &argv[i][1] );   
58       }
59       else
60       {                                         
61          ArgLab [ArgCount] = strcpy ( (char *)malloc(strlen(argv[i])+1), argv[i] ) ;
62          nblettre  += 1 + strlen(ArgLab[ArgCount]);     
63          ArgCount++;                               
64       }
65       if (ArgCount >= ARGMAXCOUNT )      
66       {
67           std::cout << "Too many Arguments ( more than "
68                     << ARGMAXCOUNT << ")" << std::endl; 
69           return;
70       }
71    }
72
73    /* Fills an array with the already used parameters ****/
74    ArgUsed = (char *)calloc (1, ArgCount );
75
76    /* Builds the full string with all the parameters  **************/
77    Appel = (char *) calloc (1, nblettre );
78
79    for ( *Appel = '\0', i=0; i<ArgCount; i++)
80    {
81       strcat ( Appel, ArgLab [i] ) ;
82       strcat ( Appel, " " ) ;
83    }
84
85    /* Splitting label from label value *************************************/
86    for ( i=0; i<ArgCount; i++) 
87    {
88       char * egaloufin = ArgLab[i] ;
89       while ( (*egaloufin != '\0') && (*egaloufin != '=') ) 
90          egaloufin ++ ;
91       if ( *egaloufin ) *(egaloufin++) = '\0';
92       ArgStr[i]= egaloufin;
93    }
94
95    /* Set labels to upper-case (labels are not case sensitive ) *********/
96    for ( i=0; i<ArgCount; i++)
97       ArgLab[i] = Majuscule ( ArgLab[i] ) ;
98
99   /* Standard arguments are managed by ArgStdArgs **********************/
100    ArgStdArgs(); 
101  }
102
103 /**
104  * \brief  canonical destructor
105  */
106 ArgMgr::~ArgMgr()
107 {
108    for(int i=0;i<ArgCount;i++)
109       if ( ArgLab[i] )
110          free(ArgLab[i]);
111    if ( ArgUsed )
112       free(ArgUsed);
113    if ( Appel )
114       free(Appel);
115 }
116  
117 /**
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.
122  */
123 int ArgMgr::ArgMgrDefined( const char *param )
124 {
125   int i;
126   bool trouve;
127   char *temp;
128   temp = Majuscule ( param ) ;
129   for ( i = ArgCount-1; i>0; i-- )
130   { 
131     trouve = ( strcmp( ArgLab[i], temp )==0 ) ;
132     if ( trouve )
133     {
134       ArgUsed[i] = true ;           
135       for ( int j=1; j<i; j++)
136       {                     
137          if ( (!ArgUsed[j])&&(!strcmp(ArgLab[i],ArgLab[j])) )
138             ArgUsed[j] = i ;
139       }
140       return i ;
141     }
142   }
143   return 0 ;
144 }
145
146 /**
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.
151  */
152 char *ArgMgr::ArgMgrValue ( const char *param )
153 {
154    int trouve ;
155    if ( (trouve = ArgMgrDefined ( param )) != false )
156       return ArgStr[trouve] ;
157    else
158       return NULL ;
159 }
160
161 /**
162  * \brief  Search for the first not yet used label
163  * @return Pointer to the char array holding the first non used label
164  */
165 char *ArgMgr::ArgMgrUnused ( )
166 {
167    int i ;
168    for ( i=ArgCount-1; i>0; i-- )
169    {
170       if ( ! ArgUsed[i] )
171       {
172          ArgMgrDefined(ArgLab[i]);
173          return ArgLab[i] ;
174       }
175   }
176   return NULL ;
177 }
178
179 /**
180  * \brief  Prints unused labels, if any
181  * @return number of unused labels
182  */
183 int ArgMgr::ArgMgrPrintUnusedLabels ()
184 {
185    char *label;
186    int i=0;
187    while ( (label=ArgMgrUnused())!=0 )
188    {
189       if (i==0)
190          std::cout << "\n Unused Labels:" << std::endl
191                    << "=============="    << std::endl;
192       std::cout << "Label : " << label << " = " 
193                 << ArgMgrValue(label) << std::endl;
194       i++;
195    }
196    return i;
197 }
198
199 /**
200  * \brief  Prints program usage
201  * @param usage  array of pointers to the documentation lines of the program.
202  * @return exception code
203  */
204 int ArgMgr::ArgMgrUsage(const char **usage )
205 {
206    while ( *usage ) 
207       std::cout << std::endl << *(usage++);
208    std::cout << std::endl; 
209    return (0);
210 }
211
212 /**
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
219  */
220 int ArgMgr::ArgMgrSave ( char *param )
221 {
222    static int   deja = 0;
223    FILE         *fd;
224    if ( *ArgParamOut == '\0' )
225       return 0;
226    if ( deja ) 
227    {
228       fd = fopen ( ArgParamOut, "a+" );
229    }
230    else
231    {
232       deja = 1;
233       fd = fopen ( ArgParamOut, "w" );
234    } 
235    if ( !fd ) 
236       return 0;
237    fprintf ( fd, "%s\n", param );
238    fclose  ( fd );
239    return 1;
240 }
241
242 /**
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
249  */
250 int ArgMgr::ArgMgrGetInt(const char *label, int defaultVal)
251 {
252    return ( (ArgMgrDefined(label))
253             ? (atoi(ArgMgrValue(label)))
254             : (defaultVal) );
255 }
256
257 /**
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
264  */
265 float ArgMgr::ArgMgrGetFloat(const char *param, float defaultVal)
266 {
267    return     ( (ArgMgrDefined(param))
268                ? ((float)atof(ArgMgrValue(param)))
269                : (defaultVal) );
270 }
271
272 /**
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
279  */
280 char *ArgMgr::ArgMgrGetString(const char *param, char *defaultVal)
281 {
282    return    ( (ArgMgrDefined(param)) 
283               ? (ArgMgrValue(param))
284               : (defaultVal) );
285 }
286
287 /**
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
297  */
298 int ArgMgr::ArgMgrGetLabel (const char *param, char *liste, int val )
299 {
300   char *lab;
301   char *vallab;
302   int i = 1;
303   char *tmp;
304   tmp = (char *) malloc(strlen(liste)+1);
305   strcpy(tmp,liste);
306
307   if ( (vallab = ArgMgrGetString(param,(char *)NULL)) != 0 ) 
308   { 
309      for ( lab = strtok (tmp,"\\"); 
310            lab != 0; 
311            lab = strtok(0L,"\\"), i++ )
312      { 
313         if ( strcmp(maj(lab),maj(vallab))==0)
314            return i;
315      } 
316      val=0;
317    }
318    free(tmp);
319    return val;
320 }
321
322 /**
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  '\\'.
328  *               No case sensitive.
329  * @param usage Usage program (displayed if label not found)
330  * @return   int : range of value amongst the values list
331  */
332 int ArgMgr::ArgMgrWantLabel (const char *param, char *liste, const char **usage )
333 {
334    char *lab;
335    char *vallab;
336    int i = 1;
337    if ( (vallab = ArgMgrGetString(param,0)) != 0 ) 
338    {
339       for ( lab = strtok (liste,"\\"); lab != 0; lab = strtok(0L,"\\"), i++ )
340         if ( strcmp(maj(lab),maj(vallab))==0) 
341            return i;
342       return 0;
343    }
344    ArgMgrUsage(usage);
345    return 0;
346 }
347
348 /**
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
355  */
356 int ArgMgr::ArgMgrWantInt (const char *label, const char **usage)
357 {
358    return        ( (ArgMgrDefined(label) ) 
359                  ? (atoi(ArgMgrValue(label) ) ) 
360                  : (ArgMgrUsage(usage),1) );
361 }
362
363 /**
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
370  */
371 float ArgMgr::ArgMgrWantFloat (const char *label, const char **usage)
372 {
373    return       ( (ArgMgrDefined(label) ) 
374                 ? ((float)atof(ArgMgrValue(label) ) ) 
375                 : (ArgMgrUsage(usage),(float)1.0) );
376 }
377
378 /**
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
385  */
386 char *ArgMgr::ArgMgrWantString(const char *label, const char **usage)
387 {
388    return      ( (ArgMgrDefined(label) ) 
389                ? (ArgMgrValue(label) ) 
390                : (ArgMgrUsage(usage),(char*)0) );
391 }
392
393 /**
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
399  */
400 char **ArgMgr::ArgMgrGetListOfString ( const char *label, int *number )
401 {
402   int taille;
403   char  *value = ArgMgrValue(label);
404   char **liste;
405   char **elem;
406   char  *chainecur; 
407   if (!value)
408   {
409      *number = 0;
410      return 0;
411   }
412   *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
413   taille = *number;
414   liste = (char **) malloc (sizeof(char*) * taille + strlen(value)+1);
415   if ( !liste )
416      return 0;
417   value = strcpy( ((char*)liste)+sizeof(char*) * taille, value );
418   for ( elem = liste, chainecur = strtok(value,", ");
419         taille>0;
420         taille--, chainecur = (chainecur) ? strtok ( 0, ", " ) : 0 )
421   {
422     *(elem++) = chainecur;
423   }
424   return liste;
425 }
426
427 /**
428  * \brief  decodes and returns an array of 'INT'
429  *  EXAMPLE:     int *points = ArgMgrGetListOfInt ( "POINTS", &nbOfPoints );  
430  * @param label   label name 
431  * @param number  nb of found INT
432  * @return   Pointer to the INT array; NULL if error
433  */
434 int *ArgMgr::ArgMgrGetListOfInt ( const char *label, int *number )
435 {
436   char *value = ArgMgrValue(label);
437   int *liste;
438   int *elem;
439   int taille;
440   if (!value)
441   {
442      *number = 0;
443      return 0;
444   }          
445   *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */ 
446   taille= *number;
447   liste = (int *) calloc (1,sizeof(int)*taille );
448   if ( !liste )
449      return 0;
450   elem = liste;
451   //*number = 1;
452
453   while ( taille>0 ) 
454   {
455     *(elem++) = (int) strtol ( value, &value, 10 );      
456     if ( *value == '\0' )
457        return liste;
458     if ( *(value++) != ',' ) 
459     {
460       free (liste);
461       return 0;
462     }
463     taille --;
464   }
465 return liste;
466 }
467
468 /**
469  * \brief  decodes and returns an array of 'FLOAT'
470  * @param label   label name 
471  * @param number  number of found FLOATs
472  * @return   Pointer to the FLOAT array; NULL if error
473  */
474 float *ArgMgr::ArgMgrGetListOfFloat ( const char *label, int *number )
475 {
476   char *value = ArgMgrValue(label);
477   float *liste;
478   float *elem;
479   int taille;
480   if (!value)
481     return 0;
482   *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
483   taille= *number;
484   liste = (float *) calloc (1,sizeof(float)*taille );
485   if ( !liste )
486   {
487      *number = 0;
488      return 0;
489   }
490   elem = liste;
491   //*number = 1;
492
493   while ( taille>0 ) 
494   {
495     *(elem++) = (float) strtod ( value, &value );      
496     if ( *value == '\0' )
497        return liste;
498     if ( *(value++) != ',' )
499     {
500       free (liste);
501       return 0;
502     }
503     taille --;
504   }
505 return liste;
506 }
507
508 /**
509  * \brief  decodes and returns an array of 'INT pairs', passed in decimal
510  * @param param   label name 
511  * @param number   nb of found pairs
512  * @return        pointer to the array of 'INT pairs'; NULL if fail
513  */
514 int *ArgMgr::ArgMgrGetIntEnum ( const char *param, int *number )
515 {
516    char *value = ArgMgrValue(param);
517    int *liste;
518    if (!value) 
519    {
520       *number = 0; 
521       return 0;
522    }
523    liste = IdStrIntEnum(value, number);
524    return liste;
525 }
526
527 /**
528  * \brief  decodes and returns an array of 'INT16 pairs', passed in hexadecimal
529  * @param param   label name 
530  * @param number   nb of found pairs
531  * @return        pointer to the array of 'INT16 pairs'; NULL if fail
532  */
533 uint16_t *ArgMgr::ArgMgrGetXInt16Enum ( const char *param, int *number )
534 {
535    char *value = ArgMgrValue(param);
536    uint16_t *liste;
537    if (!value) 
538    {
539       *number = 0; 
540       return 0;
541    }
542    liste = IdStrXInt16Enum(value, number);
543    return liste;
544 }
545 /**
546  * \brief  decodes and returns an array of 'FLOAT pairs'
547  * @param param   label name 
548  * @param number   nb of found pairs
549  * @return        pointer to the array of 'FLOAT pairs'; NULL if fail
550
551  */
552 float *ArgMgr::ArgMgrGetFloatEnum ( const char *param, int *number )
553 {
554    char  *value = ArgMgrValue(param);
555    float *liste;
556    if (!value) 
557    {
558       *number = 0; 
559       return 0;
560    }
561    liste = IdStrFloatEnum(value, number);
562    return liste;
563 }
564
565 // ------------------------ Those are 'service functions' ---------------------
566 // ------------------------       internal use only       ---------------------
567
568 /**
569  * \brief     Counts the nb of occurrences of a given charact within a 'string' 
570  * @param chaine     Pointer to the 'string'
571  * @param caract     charact to count
572  * @return       occurence number
573  */
574 int ArgMgr::IdStrCountChar (char *chaine, int caract)
575 {
576   int i=0;
577   char *ptr;
578   for ( ptr = chaine ; *ptr!='\0' ; ptr ++ ) 
579      if (*ptr==caract) 
580         i++;  
581   return i;
582 }
583
584 /**
585  * \brief     returns an array of 'INT pairs'
586  * @param value  char array decribing a set of 'INT pairs' (f1-l1, f2-l2, ...)
587  * @param number nb of found INT pairs
588  * @return       pointer to the array of 'INT pairs'
589  */
590 int *ArgMgr::IdStrIntEnum ( char* value, int *number)
591 {
592    int* liste;
593    int taille;
594    int i;
595
596    *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
597    taille= *number;
598    liste = (int *) calloc (1,sizeof(int)*2*taille );
599    if ( !liste )
600    {
601       return 0;
602    }
603    i=0;
604    while ( taille>0 ) 
605    {
606       liste[i] = (int) strtol ( value, &value, 10 );
607       if ( *value == '\0' ) 
608       {
609          liste[i+1]=liste[i];
610          return liste;
611       }
612       if ( *(value++) != '-' ) 
613       {
614          liste[i+1]=liste[i];
615          value--;
616        }
617        else
618        {
619           liste[i+1] = (int) strtol ( value, &value, 10 );
620        }
621        if ( *value == '\0' )
622           return liste;
623        if ( *(value++) != ',' )
624        {
625           free (liste);
626           return 0;
627        }
628        taille --; i+=2;
629    }
630    return liste;
631 }
632
633 /**
634  * \brief     returns an array of set of 'INT16 pairs', passed in Hexadecimal
635  * @param value  char array decribing a set of 'INT16 pairs' (f1-l1, f2-l2, ...)
636  *               coded in hexadecimal e.g. 0x0008,0x00ac
637  * @param number nb of found pairs
638  * @return        array of set of 'INT16 pairs'
639  */
640 uint16_t *ArgMgr::IdStrXInt16Enum ( char *value, int *number)
641 {
642    uint16_t *liste;
643    int taille;
644    int i;
645
646    *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
647    taille= *number;
648    liste = (uint16_t *) calloc (1,sizeof(uint16_t)*2*taille );
649    if ( !liste )
650    {
651       return 0;
652    }
653    i=0;
654    while ( taille>0 ) 
655    {
656       liste[i] = (uint16_t) strtol ( value, &value, 16 );
657       if ( *value == '\0' ) 
658       {
659          liste[i+1]=liste[i];
660          return liste;
661       }
662       if ( *(value++) != '-' ) 
663       {
664          liste[i+1]=liste[i];
665          value--;
666        }
667        else
668        {
669           liste[i+1] = (uint16_t) strtol ( value, &value, 16 );
670        }
671        if ( *value == '\0' )
672           return liste;
673        if ( *(value++) != ',' )
674        {
675           free (liste);
676           return 0;
677        }
678        taille --; i+=2;
679    }
680    return liste;
681
682 /**
683  * \brief     returns an array of 'FLOAT pairs'
684  * @param value  char array decribing a set of 'FLOAT pairs' (f1-l1, f2-l2, ...)
685  * @param number nb of found pairs
686  * @return       pointer to the array of 'FLOAT pairs'; NULL if fail
687  */
688 float *ArgMgr::IdStrFloatEnum (char *value, int *number)
689 {
690    float *liste;
691    int taille;
692    int i;
693    *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
694    taille= *number;
695    liste = (float *) calloc (1,sizeof(float)*2*taille );
696    if ( !liste )
697       return 0;
698    i=0;
699    while ( taille>0 ) 
700    {
701       liste[i] = (float) strtod ( value, &value );      
702       if ( *value == '\0' ) 
703       {
704          liste[i+1]=liste[i];
705          return liste;
706       }
707       if ( *(value++) != '-' ) 
708       {
709          liste[i+1]=liste[i];
710          value--;
711       }
712       else
713       {
714           liste[i+1] = (float) strtod ( value, &value );
715       }
716       if ( *value == '\0' ) 
717          return liste;
718       if ( *(value++) != ',' )
719       {
720          free (liste);
721          return 0;
722       }
723       taille --; i+=2;
724    }
725    return liste;
726 }
727
728 //-----------------------------------------------------------------------------
729 // Protected
730
731 //-----------------------------------------------------------------------------
732 // Private
733
734 /**************************************************************************
735 *                                                                         *
736 * Nom de la fonction : Majuscule                                          *
737 * Role ............. : Creates a new Upper case char array.               *
738 * parameters ....... : Pointer to the initial char array.                 *                           *
739 * Valeur retournee . : Pointer to the new Upper case char array.          *
740 *                                                                         *
741 **************************************************************************/
742 char *ArgMgr::Majuscule (const char *chaine )
743 {
744   char *ptr, *ptr2, *ptr3;
745   ptr2 = (char *)malloc(strlen(chaine)*sizeof(char)+1);
746   ptr3=ptr2;
747   for ( ptr = (char *)chaine ; *ptr!='\0' ; ptr ++ ) 
748    {  
749        *ptr3 = toupper ( * ptr ); ptr3++; 
750    }
751   *ptr3='\0'; 
752   return ptr2;
753 }
754
755 /**************************************************************************
756 *                                                                         *
757 * Nom de la fonction : FiltreLong                                         *
758 * Role ............. : Stops the program if argument is too long.         *
759 *                      ARG_LONG_MAX defines max length.                   *
760 * parameters ....... : Pointer to the argument.                           *
761 * Valeur retournee . : false if OK.                                       *
762 *                      true if KO.                                        *
763 **************************************************************************/
764 int ArgMgr::FiltreLong ( char *arg  )
765 {
766   int  n = 0 ;
767   while ( (n++<ARG_LONG_MAX) && (*(arg++) != '\0') ) ;
768   return (n>=ARG_LONG_MAX) ;
769 }
770
771 /*------------------------------------------------------------------------
772  | Role       : Reads a parameter from a file
773  | Return     : Type   : char *
774  |              Role   : pointer to the label
775  | parameters : param  : char *
776  |              Role   : one where the parameter will be stored
777  |              fd     : FILE *
778  |              Role   : File description (assumed to be open)
779  +------------------------------------------------------------------------*/
780 const char *ArgMgr::LoadedParam ( const char *param, FILE *fd )
781 {
782   int    carlu;
783   char  *car = (char *)param;
784   int    quote = false;
785   int    nbcar = 0;
786
787   /* remove spaces at the beginning****/
788   while ( isspace(carlu=fgetc (fd)) );
789   if (carlu==EOF)
790      return 0;
791   /* Search for a " */
792   if ( carlu=='\"' ) 
793   {
794     carlu=fgetc(fd);
795     quote=true;
796   /* Read all the characters */
797   }
798   while (  (carlu!=EOF)
799         && (  ( (!quote)&&(!isspace(carlu)) )
800          ||( (quote)&& !(carlu=='\"')   ) ) ) 
801   {
802      *(car++) = (char) carlu;
803      nbcar ++;
804   /* sans depasser la taille max*/
805      if ( nbcar >= ARG_LONG_MAX ) 
806      {
807         std::cout << "\nError: Argument too long ( > "
808                   << ARG_LONG_MAX << ")in parameter file."
809                   << std::endl;
810         break;
811      }
812      carlu = fgetc(fd);
813   }
814   *car = '\0';
815   return param;
816 }
817
818 /*------------------------------------------------------------------------
819  | Role       : Reading of arguments in a parameter file
820  |              (this function is recursive).
821  | Return     : Type   : int
822  |              Role   : length needed to store all the parameters
823  | parameters : filename : char *
824  |              Role     : parameter File name
825  |
826  +------------------------------------------------------------------------*/
827 int ArgMgr::ArgLoadFromFile ( char *filename )
828 {
829   int   nbl = 0;
830   char  param[ARG_LONG_MAX+1];
831   FILE  *fch;
832
833   fch = fopen ( filename, ID_RFILE_TEXT );
834   while ( LoadedParam (param, fch ) )
835   {
836     int n = strlen(param);
837     if ( param[0]=='@' )
838     {
839       nbl  += ArgLoadFromFile ( &param[1] );
840     }
841     else
842     {
843       ArgLab [ArgCount] = strcpy ((char *) malloc(n+1), param ) ;
844       nbl += n + 1 ;
845       ArgCount++;
846       if ( ArgCount >= ARGMAXCOUNT ) 
847          break;
848     }
849   }
850   fclose ( fch );
851   return nbl;
852 }
853
854 /*------------------------------------------------------------------------
855  | Role       : Standard parameters management (on command line)
856  | Return     : Type   : void
857  | parameters : none
858  +------------------------------------------------------------------------*/
859 void ArgMgr::ArgStdArgs()
860 {
861   char *logfile;
862   FILE *fd;
863
864   if ( (ArgParamOut=ArgMgrValue((char*)ARG_LABEL_PARAMOUT))==0 )
865     ArgParamOut = ARG_DEFAULT_PARAMOUT;
866   if ( (logfile = ArgMgrValue((char*)ARG_LABEL_LOGFILE))!=0) 
867   {
868     if ( *logfile == '\0' )
869        logfile = (char *)ARG_DEFAULT_LOGFILE;
870     fd = fopen ( logfile, "a+" );
871     if ( fd ) 
872     {
873       fprintf ( fd, "%s\n", Appel );
874       fclose  ( fd );
875     }
876   }
877 }
878
879 /*------------------------------------------------------------------------
880  | Role       : Sets in Upper Case.
881  | Return     : Type   : char *
882  | parameters : char *
883  +------------------------------------------------------------------------*/
884 char *ArgMgr::maj ( char *a )
885 {
886    char *b = a;
887    while ( *b !=0 ) 
888    {
889       if ( *b<='z' && *b>='a' ) *b = *b+'A'-'a';
890       b++;
891    }
892    return a;
893 }
894 //-----------------------------------------------------------------------------
895 // Print
896
897 //-----------------------------------------------------------------------------
898 } // end namespace gdcm