]> Creatis software - gdcm.git/blob - src/gdcmArgMgr.cxx
REmove useless variable assignation
[gdcm.git] / src / gdcmArgMgr.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmArgMgr.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/09/20 09:21:35 $
7   Version:   $Revision: 1.15 $
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      return 0;
409   *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
410   taille = *number;
411   liste = (char **) malloc (sizeof(char*) * taille + strlen(value)+1);
412   if ( !liste )
413      return 0;
414   value = strcpy( ((char*)liste)+sizeof(char*) * taille, value );
415   for ( elem = liste, chainecur = strtok(value,", ");
416         taille>0;
417         taille--, chainecur = (chainecur) ? strtok ( 0, ", " ) : 0 )
418   {
419     *(elem++) = chainecur;
420   }
421   return liste;
422 }
423
424 /**
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
430  */
431 int *ArgMgr::ArgMgrGetListOfInt ( const char *label, int *number )
432 {
433   char *value = ArgMgrValue(label);
434   int *liste;
435   int *elem;
436   int taille;
437   if (!value)
438      return 0;
439   *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
440   taille= *number;
441   liste = (int *) calloc (1,sizeof(int)*taille );
442   if ( !liste )
443      return 0;
444   elem = liste;
445   *number = 1;
446
447   while ( taille>0 ) 
448   {
449     *(elem++) = (int) strtol ( value, &value, 10 );      
450     if ( *value == '\0' )
451        return liste;
452     if ( *(value++) != ',' ) 
453     {
454       free (liste);
455       return 0;
456     }
457     taille --;
458   }
459 return liste;
460 }
461
462 /**
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
467  */
468 float *ArgMgr::ArgMgrGetListOfFloat ( const char *label, int *number )
469 {
470   char *value = ArgMgrValue(label);
471   float *liste;
472   float *elem;
473   int taille;
474   if (!value)
475     return 0;
476   *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
477   taille= *number;
478   liste = (float *) calloc (1,sizeof(float)*taille );
479   if ( !liste )
480      return 0;
481   elem = liste;
482   *number = 1;
483
484   while ( taille>0 ) 
485   {
486     *(elem++) = (float) strtod ( value, &value );      
487     if ( *value == '\0' )
488        return liste;
489     if ( *(value++) != ',' )
490     {
491       free (liste);
492       return 0;
493     }
494     taille --;
495   }
496 return liste;
497 }
498
499 /**
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
504  */
505 int *ArgMgr::ArgMgrGetIntEnum ( const char *param, int *number )
506 {
507    char *value = ArgMgrValue(param);
508    int *liste;
509    if (!value) 
510    {
511       *number = 0; 
512       return 0;
513    }
514    liste = IdStrIntEnum(value, number);
515    return liste;
516 }
517
518 /**
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
523  */
524 uint16_t *ArgMgr::ArgMgrGetXInt16Enum ( const char *param, int *number )
525 {
526    char *value = ArgMgrValue(param);
527    uint16_t *liste;
528    if (!value) 
529    {
530       *number = 0; 
531       return 0;
532    }
533    liste = IdStrXInt16Enum(value, number);
534    return liste;
535 }
536 /**
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
541
542  */
543 float *ArgMgr::ArgMgrGetFloatEnum ( const char *param, int *number )
544 {
545    char  *value = ArgMgrValue(param);
546    float *liste;
547    if (!value) 
548    {
549       *number = 0; 
550       return 0;
551    }
552    liste = IdStrFloatEnum(value, number);
553    return liste;
554 }
555
556 // ------------------------ Those are 'service functions' ---------------------
557 // ------------------------       internal use only       ---------------------
558
559 /**
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
564  */
565 int ArgMgr::IdStrCountChar (char *chaine, int caract)
566 {
567   int i=0;
568   char *ptr;
569   for ( ptr = chaine ; *ptr!='\0' ; ptr ++ ) 
570      if (*ptr==caract) 
571         i++;  
572   return i;
573 }
574
575 /**
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'
580  */
581 int *ArgMgr::IdStrIntEnum ( char* value, int *number)
582 {
583    int* liste;
584    int taille;
585    int i;
586
587    *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
588    taille= *number;
589    liste = (int *) calloc (1,sizeof(int)*2*taille );
590    if ( !liste )
591    {
592       return 0;
593    }
594    i=0;
595    while ( taille>0 ) 
596    {
597       liste[i] = (int) strtol ( value, &value, 10 );
598       if ( *value == '\0' ) 
599       {
600          liste[i+1]=liste[i];
601          return liste;
602       }
603       if ( *(value++) != '-' ) 
604       {
605          liste[i+1]=liste[i];
606          value--;
607        }
608        else
609        {
610           liste[i+1] = (int) strtol ( value, &value, 10 );
611        }
612        if ( *value == '\0' )
613           return liste;
614        if ( *(value++) != ',' )
615        {
616           free (liste);
617           return 0;
618        }
619        taille --; i+=2;
620    }
621    return liste;
622 }
623
624 /**
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'
630  */
631 uint16_t *ArgMgr::IdStrXInt16Enum ( char *value, int *number)
632 {
633    uint16_t *liste;
634    int taille;
635    int i;
636
637    *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
638    taille= *number;
639    liste = (uint16_t *) calloc (1,sizeof(uint16_t)*2*taille );
640    if ( !liste )
641    {
642       return 0;
643    }
644    i=0;
645    while ( taille>0 ) 
646    {
647       liste[i] = (uint16_t) strtol ( value, &value, 16 );
648       if ( *value == '\0' ) 
649       {
650          liste[i+1]=liste[i];
651          return liste;
652       }
653       if ( *(value++) != '-' ) 
654       {
655          liste[i+1]=liste[i];
656          value--;
657        }
658        else
659        {
660           liste[i+1] = (uint16_t) strtol ( value, &value, 16 );
661        }
662        if ( *value == '\0' )
663           return liste;
664        if ( *(value++) != ',' )
665        {
666           free (liste);
667           return 0;
668        }
669        taille --; i+=2;
670    }
671    return liste;
672
673 /**
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
678  */
679 float *ArgMgr::IdStrFloatEnum (char *value, int *number)
680 {
681    float *liste;
682    int taille;
683    int i;
684    *number = IdStrCountChar(value,',')+1; /* nb Elements = nb Commas +1 */
685    taille= *number;
686    liste = (float *) calloc (1,sizeof(float)*2*taille );
687    if ( !liste )
688       return 0;
689    i=0;
690    while ( taille>0 ) 
691    {
692       liste[i] = (float) strtod ( value, &value );      
693       if ( *value == '\0' ) 
694       {
695          liste[i+1]=liste[i];
696          return liste;
697       }
698       if ( *(value++) != '-' ) 
699       {
700          liste[i+1]=liste[i];
701          value--;
702       }
703       else
704       {
705           liste[i+1] = (float) strtod ( value, &value );
706       }
707       if ( *value == '\0' ) 
708          return liste;
709       if ( *(value++) != ',' )
710       {
711          free (liste);
712          return 0;
713       }
714       taille --; i+=2;
715    }
716    return liste;
717 }
718
719 //-----------------------------------------------------------------------------
720 // Protected
721
722 //-----------------------------------------------------------------------------
723 // Private
724
725 /**************************************************************************
726 *                                                                         *
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.          *
731 *                                                                         *
732 **************************************************************************/
733 char *ArgMgr::Majuscule (const char *chaine )
734 {
735   char *ptr, *ptr2, *ptr3;
736   ptr2 = (char *)malloc(strlen(chaine)*sizeof(char)+1);
737   ptr3=ptr2;
738   for ( ptr = (char *)chaine ; *ptr!='\0' ; ptr ++ ) 
739    {  
740        *ptr3 = toupper ( * ptr ); ptr3++; 
741    }
742   *ptr3='\0'; 
743   return ptr2;
744 }
745
746 /**************************************************************************
747 *                                                                         *
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.                                       *
753 *                      true if KO.                                        *
754 **************************************************************************/
755 int ArgMgr::FiltreLong ( char *arg  )
756 {
757   int  n = 0 ;
758   while ( (n++<ARG_LONG_MAX) && (*(arg++) != '\0') ) ;
759   return (n>=ARG_LONG_MAX) ;
760 }
761
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
768  |              fd     : FILE *
769  |              Role   : File description (assumed to be open)
770  +------------------------------------------------------------------------*/
771 const char *ArgMgr::LoadedParam ( const char *param, FILE *fd )
772 {
773   int    carlu;
774   char  *car = (char *)param;
775   int    quote = false;
776   int    nbcar = 0;
777
778   /* remove spaces at the beginning****/
779   while ( isspace(carlu=fgetc (fd)) );
780   if (carlu==EOF)
781      return 0;
782   /* Search for a " */
783   if ( carlu=='\"' ) 
784   {
785     carlu=fgetc(fd);
786     quote=true;
787   /* Read all the characters */
788   }
789   while (  (carlu!=EOF)
790         && (  ( (!quote)&&(!isspace(carlu)) )
791          ||( (quote)&& !(carlu=='\"')   ) ) ) 
792   {
793      *(car++) = (char) carlu;
794      nbcar ++;
795   /* sans depasser la taille max*/
796      if ( nbcar >= ARG_LONG_MAX ) 
797      {
798         std::cout << "\nError: Argument too long ( > "
799                   << ARG_LONG_MAX << ")in parameter file."
800                   << std::endl;
801         break;
802      }
803      carlu = fgetc(fd);
804   }
805   *car = '\0';
806   return param;
807 }
808
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
816  |
817  +------------------------------------------------------------------------*/
818 int ArgMgr::ArgLoadFromFile ( char *filename )
819 {
820   int   nbl = 0;
821   char  param[ARG_LONG_MAX+1];
822   FILE  *fch;
823
824   fch = fopen ( filename, ID_RFILE_TEXT );
825   while ( LoadedParam (param, fch ) )
826   {
827     int n = strlen(param);
828     if ( param[0]=='@' )
829     {
830       nbl  += ArgLoadFromFile ( &param[1] );
831     }
832     else
833     {
834       ArgLab [ArgCount] = strcpy ((char *) malloc(n+1), param ) ;
835       nbl += n + 1 ;
836       ArgCount++;
837       if ( ArgCount >= ARGMAXCOUNT ) 
838          break;
839     }
840   }
841   fclose ( fch );
842   return nbl;
843 }
844
845 /*------------------------------------------------------------------------
846  | Role       : Standard parameters management (on command line)
847  | Return     : Type   : void
848  | parameters : none
849  +------------------------------------------------------------------------*/
850 void ArgMgr::ArgStdArgs()
851 {
852   char *logfile;
853   FILE *fd;
854
855   if ( (ArgParamOut=ArgMgrValue((char*)ARG_LABEL_PARAMOUT))==0 )
856     ArgParamOut = ARG_DEFAULT_PARAMOUT;
857   if ( (logfile = ArgMgrValue((char*)ARG_LABEL_LOGFILE))!=0) 
858   {
859     if ( *logfile == '\0' )
860        logfile = (char *)ARG_DEFAULT_LOGFILE;
861     fd = fopen ( logfile, "a+" );
862     if ( fd ) 
863     {
864       fprintf ( fd, "%s\n", Appel );
865       fclose  ( fd );
866     }
867   }
868 }
869
870 /*------------------------------------------------------------------------
871  | Role       : Sets in Upper Case.
872  | Return     : Type   : char *
873  | parameters : char *
874  +------------------------------------------------------------------------*/
875 char *ArgMgr::maj ( char *a )
876 {
877    char *b = a;
878    while ( *b !=0 ) 
879    {
880       if ( *b<='z' && *b>='a' ) *b = *b+'A'-'a';
881       b++;
882    }
883    return a;
884 }
885 //-----------------------------------------------------------------------------
886 // Print
887
888 //-----------------------------------------------------------------------------
889 } // end namespace gdcm