]> Creatis software - gdcm.git/blob - src/gdcmArgMgr.cxx
ENH: Finally I got ArgMgr to be const string instead of char*... strcasecmp is POSIX...
[gdcm.git] / src / gdcmArgMgr.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmArgMgr.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/01/26 15:52:56 $
7   Version:   $Revision: 1.17 $
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 const char *ArgMgr::ArgMgrGetString(const char *param, const 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   const 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,(const char *)NULL)) != 0 ) 
308   { 
309      for ( lab = strtok (tmp,"\\"); 
310            lab != 0; 
311            lab = strtok(0L,"\\"), i++ )
312      { 
313         // strcmp ignoring case
314         if( strcasecmp(lab, vallab) == 0)
315            return i;
316      } 
317      val=0;
318    }
319    free(tmp);
320    return val;
321 }
322
323 /**
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  '\\'.
329  *               No case sensitive.
330  * @param usage Usage program (displayed if label not found)
331  * @return   int : range of value amongst the values list
332  */
333 int ArgMgr::ArgMgrWantLabel (const char *param, char *liste, const char **usage )
334 {
335    char *lab;
336    const char *vallab;
337    int i = 1;
338    if ( (vallab = ArgMgrGetString(param,0)) != 0 ) 
339    {
340       for ( lab = strtok (liste,"\\"); lab != 0; lab = strtok(0L,"\\"), i++ )
341         if ( strcasecmp(lab,vallab)==0) 
342            return i;
343       return 0;
344    }
345    ArgMgrUsage(usage);
346    return 0;
347 }
348
349 /**
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
356  */
357 int ArgMgr::ArgMgrWantInt (const char *label, const char **usage)
358 {
359    return        ( (ArgMgrDefined(label) ) 
360                  ? (atoi(ArgMgrValue(label) ) ) 
361                  : (ArgMgrUsage(usage),1) );
362 }
363
364 /**
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
371  */
372 float ArgMgr::ArgMgrWantFloat (const char *label, const char **usage)
373 {
374    return       ( (ArgMgrDefined(label) ) 
375                 ? ((float)atof(ArgMgrValue(label) ) ) 
376                 : (ArgMgrUsage(usage),(float)1.0) );
377 }
378
379 /**
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
386  */
387 char *ArgMgr::ArgMgrWantString(const char *label, const char **usage)
388 {
389    return      ( (ArgMgrDefined(label) ) 
390                ? (ArgMgrValue(label) ) 
391                : (ArgMgrUsage(usage),(char*)0) );
392 }
393
394 /**
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
400  */
401 char **ArgMgr::ArgMgrGetListOfString ( const char *label, int *number )
402 {
403   int taille;
404   char  *value = ArgMgrValue(label);
405   char **liste;
406   char **elem;
407   char  *chainecur; 
408   if (!value)
409   {
410      *number = 0;
411      return 0;
412   }
413   *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
414   taille = *number;
415   liste = (char **) malloc (sizeof(char*) * taille + strlen(value)+1);
416   if ( !liste )
417      return 0;
418   value = strcpy( ((char*)liste)+sizeof(char*) * taille, value );
419   for ( elem = liste, chainecur = strtok(value,", ");
420         taille>0;
421         taille--, chainecur = (chainecur) ? strtok ( 0, ", " ) : 0 )
422   {
423     *(elem++) = chainecur;
424   }
425   return liste;
426 }
427
428 /**
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
434  */
435 int *ArgMgr::ArgMgrGetListOfInt ( const char *label, int *number )
436 {
437   char *value = ArgMgrValue(label);
438   int *liste;
439   int *elem;
440   int taille;
441   if (!value)
442   {
443      *number = 0;
444      return 0;
445   }          
446   *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */ 
447   taille= *number;
448   liste = (int *) calloc (1,sizeof(int)*taille );
449   if ( !liste )
450      return 0;
451   elem = liste;
452   //*number = 1;
453
454   while ( taille>0 ) 
455   {
456     *(elem++) = (int) strtol ( value, &value, 10 );      
457     if ( *value == '\0' )
458        return liste;
459     if ( *(value++) != ',' ) 
460     {
461       free (liste);
462       return 0;
463     }
464     taille --;
465   }
466 return liste;
467 }
468
469 /**
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
474  */
475 float *ArgMgr::ArgMgrGetListOfFloat ( const char *label, int *number )
476 {
477   char *value = ArgMgrValue(label);
478   float *liste;
479   float *elem;
480   int taille;
481   if (!value)
482     return 0;
483   *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
484   taille= *number;
485   liste = (float *) calloc (1,sizeof(float)*taille );
486   if ( !liste )
487   {
488      *number = 0;
489      return 0;
490   }
491   elem = liste;
492   //*number = 1;
493
494   while ( taille>0 ) 
495   {
496     *(elem++) = (float) strtod ( value, &value );      
497     if ( *value == '\0' )
498        return liste;
499     if ( *(value++) != ',' )
500     {
501       free (liste);
502       return 0;
503     }
504     taille --;
505   }
506 return liste;
507 }
508
509 /**
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
514  */
515 int *ArgMgr::ArgMgrGetIntEnum ( const char *param, int *number )
516 {
517    char *value = ArgMgrValue(param);
518    int *liste;
519    if (!value) 
520    {
521       *number = 0; 
522       return 0;
523    }
524    liste = IdStrIntEnum(value, number);
525    return liste;
526 }
527
528 /**
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
533  */
534 uint16_t *ArgMgr::ArgMgrGetXInt16Enum ( const char *param, int *number )
535 {
536    char *value = ArgMgrValue(param);
537    uint16_t *liste;
538    if (!value) 
539    {
540       *number = 0; 
541       return 0;
542    }
543    liste = IdStrXInt16Enum(value, number);
544    return liste;
545 }
546 /**
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
551
552  */
553 float *ArgMgr::ArgMgrGetFloatEnum ( const char *param, int *number )
554 {
555    char  *value = ArgMgrValue(param);
556    float *liste;
557    if (!value) 
558    {
559       *number = 0; 
560       return 0;
561    }
562    liste = IdStrFloatEnum(value, number);
563    return liste;
564 }
565
566 // ------------------------ Those are 'service functions' ---------------------
567 // ------------------------       internal use only       ---------------------
568
569 /**
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
574  */
575 int ArgMgr::IdStrCountChar (char *chaine, int caract)
576 {
577   int i=0;
578   char *ptr;
579   for ( ptr = chaine ; *ptr!='\0' ; ptr ++ ) 
580      if (*ptr==caract) 
581         i++;  
582   return i;
583 }
584
585 /**
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'
590  */
591 int *ArgMgr::IdStrIntEnum ( char* value, int *number)
592 {
593    int* liste;
594    int taille;
595    int i;
596
597    *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
598    taille= *number;
599    liste = (int *) calloc (1,sizeof(int)*2*taille );
600    if ( !liste )
601    {
602       return 0;
603    }
604    i=0;
605    while ( taille>0 ) 
606    {
607       liste[i] = (int) strtol ( value, &value, 10 );
608       if ( *value == '\0' ) 
609       {
610          liste[i+1]=liste[i];
611          return liste;
612       }
613       if ( *(value++) != '-' ) 
614       {
615          liste[i+1]=liste[i];
616          value--;
617        }
618        else
619        {
620           liste[i+1] = (int) strtol ( value, &value, 10 );
621        }
622        if ( *value == '\0' )
623           return liste;
624        if ( *(value++) != ',' )
625        {
626           free (liste);
627           return 0;
628        }
629        taille --; i+=2;
630    }
631    return liste;
632 }
633
634 /**
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'
640  */
641 uint16_t *ArgMgr::IdStrXInt16Enum ( char *value, int *number)
642 {
643    uint16_t *liste;
644    int taille;
645    int i;
646
647    *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
648    taille= *number;
649    liste = (uint16_t *) calloc (1,sizeof(uint16_t)*2*taille );
650    if ( !liste )
651    {
652       return 0;
653    }
654    i=0;
655    while ( taille>0 ) 
656    {
657       liste[i] = (uint16_t) strtol ( value, &value, 16 );
658       if ( *value == '\0' ) 
659       {
660          liste[i+1]=liste[i];
661          return liste;
662       }
663       if ( *(value++) != '-' ) 
664       {
665          liste[i+1]=liste[i];
666          value--;
667        }
668        else
669        {
670           liste[i+1] = (uint16_t) strtol ( value, &value, 16 );
671        }
672        if ( *value == '\0' )
673           return liste;
674        if ( *(value++) != ',' )
675        {
676           free (liste);
677           return 0;
678        }
679        taille --; i+=2;
680    }
681    return liste;
682
683 /**
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
688  */
689 float *ArgMgr::IdStrFloatEnum (char *value, int *number)
690 {
691    float *liste;
692    int taille;
693    int i;
694    *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
695    taille= *number;
696    liste = (float *) calloc (1,sizeof(float)*2*taille );
697    if ( !liste )
698       return 0;
699    i=0;
700    while ( taille>0 ) 
701    {
702       liste[i] = (float) strtod ( value, &value );      
703       if ( *value == '\0' ) 
704       {
705          liste[i+1]=liste[i];
706          return liste;
707       }
708       if ( *(value++) != '-' ) 
709       {
710          liste[i+1]=liste[i];
711          value--;
712       }
713       else
714       {
715           liste[i+1] = (float) strtod ( value, &value );
716       }
717       if ( *value == '\0' ) 
718          return liste;
719       if ( *(value++) != ',' )
720       {
721          free (liste);
722          return 0;
723       }
724       taille --; i+=2;
725    }
726    return liste;
727 }
728
729 //-----------------------------------------------------------------------------
730 // Protected
731
732 //-----------------------------------------------------------------------------
733 // Private
734
735 /**************************************************************************
736 *                                                                         *
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.          *
741 *                                                                         *
742 **************************************************************************/
743 char *ArgMgr::Majuscule (const char *chaine )
744 {
745   char *ptr, *ptr2, *ptr3;
746   ptr2 = (char *)malloc(strlen(chaine)*sizeof(char)+1);
747   ptr3=ptr2;
748   for ( ptr = (char *)chaine ; *ptr!='\0' ; ptr ++ ) 
749    {  
750        *ptr3 = toupper ( * ptr ); ptr3++; 
751    }
752   *ptr3='\0'; 
753   return ptr2;
754 }
755
756 /**************************************************************************
757 *                                                                         *
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.                                       *
763 *                      true if KO.                                        *
764 **************************************************************************/
765 int ArgMgr::FiltreLong ( char *arg  )
766 {
767   int  n = 0 ;
768   while ( (n++<ARG_LONG_MAX) && (*(arg++) != '\0') ) ;
769   return (n>=ARG_LONG_MAX) ;
770 }
771
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
778  |              fd     : FILE *
779  |              Role   : File description (assumed to be open)
780  +------------------------------------------------------------------------*/
781 const char *ArgMgr::LoadedParam ( const char *param, FILE *fd )
782 {
783   int    carlu;
784   char  *car = (char *)param;
785   int    quote = false;
786   int    nbcar = 0;
787
788   /* remove spaces at the beginning****/
789   while ( isspace(carlu=fgetc (fd)) );
790   if (carlu==EOF)
791      return 0;
792   /* Search for a " */
793   if ( carlu=='\"' ) 
794   {
795     carlu=fgetc(fd);
796     quote=true;
797   /* Read all the characters */
798   }
799   while (  (carlu!=EOF)
800         && (  ( (!quote)&&(!isspace(carlu)) )
801          ||( (quote)&& !(carlu=='\"')   ) ) ) 
802   {
803      *(car++) = (char) carlu;
804      nbcar ++;
805   /* sans depasser la taille max*/
806      if ( nbcar >= ARG_LONG_MAX ) 
807      {
808         std::cout << "\nError: Argument too long ( > "
809                   << ARG_LONG_MAX << ")in parameter file."
810                   << std::endl;
811         break;
812      }
813      carlu = fgetc(fd);
814   }
815   *car = '\0';
816   return param;
817 }
818
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
826  |
827  +------------------------------------------------------------------------*/
828 int ArgMgr::ArgLoadFromFile ( char *filename )
829 {
830   int   nbl = 0;
831   char  param[ARG_LONG_MAX+1];
832   FILE  *fch;
833
834   fch = fopen ( filename, ID_RFILE_TEXT );
835   while ( LoadedParam (param, fch ) )
836   {
837     int n = strlen(param);
838     if ( param[0]=='@' )
839     {
840       nbl  += ArgLoadFromFile ( &param[1] );
841     }
842     else
843     {
844       ArgLab [ArgCount] = strcpy ((char *) malloc(n+1), param ) ;
845       nbl += n + 1 ;
846       ArgCount++;
847       if ( ArgCount >= ARGMAXCOUNT ) 
848          break;
849     }
850   }
851   fclose ( fch );
852   return nbl;
853 }
854
855 /*------------------------------------------------------------------------
856  | Role       : Standard parameters management (on command line)
857  | Return     : Type   : void
858  | parameters : none
859  +------------------------------------------------------------------------*/
860 void ArgMgr::ArgStdArgs()
861 {
862   char *logfile;
863   FILE *fd;
864
865   if ( (ArgParamOut=ArgMgrValue((char*)ARG_LABEL_PARAMOUT))==0 )
866     ArgParamOut = ARG_DEFAULT_PARAMOUT;
867   if ( (logfile = ArgMgrValue((char*)ARG_LABEL_LOGFILE))!=0) 
868   {
869     if ( *logfile == '\0' )
870        logfile = (char *)ARG_DEFAULT_LOGFILE;
871     fd = fopen ( logfile, "a+" );
872     if ( fd ) 
873     {
874       fprintf ( fd, "%s\n", Appel );
875       fclose  ( fd );
876     }
877   }
878 }
879
880 /*------------------------------------------------------------------------
881  | Role       : Sets in Upper Case.
882  | Return     : Type   : char *
883  | parameters : char *
884  +------------------------------------------------------------------------*/
885 char *ArgMgr::maj ( char *a )
886 {
887    char *b = a;
888    while ( *b !=0 ) 
889    {
890       if ( *b<='z' && *b>='a' ) *b = *b+'A'-'a';
891       b++;
892    }
893    return a;
894 }
895 //-----------------------------------------------------------------------------
896 // Print
897
898 //-----------------------------------------------------------------------------
899 } // end namespace gdcm