~ubuntu-branches/ubuntu/vivid/esorex/vivid-proposed

« back to all changes in this revision

Viewing changes to src/er_paramutils.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2011-11-25 12:08:00 UTC
  • Revision ID: package-import@ubuntu.com-20111125120800-hb8qatpoxkhv0fne
Tags: upstream-3.9.0
ImportĀ upstreamĀ versionĀ 3.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: er_paramutils.c,v 1.39 2010/04/27 17:11:48 cgarcia Exp $
 
2
 *
 
3
 *   This file is part of the ESO Common Pipeline Library
 
4
 *   Copyright (C) 2001-2004 European Southern Observatory
 
5
 *
 
6
 *   This program is free software; you can redistribute it and/or modify
 
7
 *   it under the terms of the GNU General Public License as published by
 
8
 *   the Free Software Foundation; either version 2 of the License, or
 
9
 *   (at your option) any later version.
 
10
 *
 
11
 *   This program is distributed in the hope that it will be useful,
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *   GNU General Public License for more details.
 
15
 *
 
16
 *   You should have received a copy of the GNU General Public License
 
17
 *   along with this program; if not, write to the Free Software
 
18
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 */
 
20
 
 
21
/*
 
22
 * $Author: cgarcia $
 
23
 * $Date: 2010/04/27 17:11:48 $
 
24
 * $Revision: 1.39 $
 
25
 * $Name: esorex-3_9_0 $
 
26
 */
 
27
 
 
28
#ifdef HAVE_CONFIG_H
 
29
#   include "config.h"
 
30
#endif
 
31
 
 
32
#include <stdio.h>
 
33
#include <string.h>
 
34
#include <stdlib.h>
 
35
#include <wordexp.h>
 
36
 
 
37
 
 
38
#include <cpl.h>
 
39
 
 
40
#include "er_main.h"
 
41
#include "er_plugin.h"
 
42
#include "er_paramutils.h"
 
43
#include "er_stringutils.h"
 
44
 
 
45
#define  SIZE_X  200
 
46
#define  MAXSTR  MAXSTRLENCONF - 4
 
47
 
 
48
/**
 
49
 * @defgroup er_params_utils EsoRex Parameter Utility Functions
 
50
 *
 
51
 * EsoRex Parameter Utility Functions
 
52
 *
 
53
 */
 
54
 
 
55
/**@{*/
 
56
 
 
57
/**********************************************************************/
 
58
/**
 
59
 * @brief  Replaces any tilde in parameters within a parameter list.
 
60
 *
 
61
 * @param param_list  List of Parameters
 
62
 *
 
63
 * @return None
 
64
 *
 
65
 * Function searches @em param_list for parameters whose values begin 
 
66
 * with a tilde (~) and if found, replace the tilde with the user's
 
67
 * home directory (using the $HOME environment variable).
 
68
 * 
 
69
 */
 
70
/**********************************************************************/
 
71
 
 
72
void er_paramutils_tilde_convert (cpl_parameterlist * param_list)
 
73
 
 
74
{
 
75
cpl_parameter *p = NULL;
 
76
cpl_type ptype;
 
77
 
 
78
int f_is_present = 0;           /* FALSE */
 
79
 
 
80
const char *old_str = NULL;
 
81
 
 
82
 
 
83
 
 
84
ER_TRACE(__func__)
 
85
 
 
86
 
 
87
p = cpl_parameterlist_get_first (param_list);
 
88
 
 
89
while (p != NULL)
 
90
   {
 
91
   f_is_present = cpl_parameter_get_default_flag (p);
 
92
 
 
93
   if (f_is_present)
 
94
      {
 
95
      ptype = cpl_parameter_get_type (p);
 
96
      if (ptype == CPL_TYPE_STRING)
 
97
         {
 
98
         old_str = cpl_parameter_get_string (p);
 
99
         if (*old_str == '~')
 
100
            {
 
101
            char  *cpp;
 
102
            wordexp_t  result;
 
103
 
 
104
            /* use C library (glibc) routine wordexp() to expand ~/ or ~name/ */
 
105
 
 
106
            if (wordexp(old_str,&result,0) != 0)
 
107
               {
 
108
               cpl_msg_error (er_func, "Expansion of ~ (tilde) failed...");
 
109
               return;
 
110
               }
 
111
 
 
112
            cpp = *result.we_wordv;             /* use 1st and only result word */
 
113
            if (cpp == NULL)
 
114
               {
 
115
               cpl_msg_error (er_func, "Env. variable HOME not set");
 
116
               return;
 
117
               }
 
118
 
 
119
            cpl_parameter_set_string (p, cpp);
 
120
            wordfree(&result);
 
121
            }
 
122
         }
 
123
      }
 
124
 
 
125
   /* Get the next parameter in the list */
 
126
   p = cpl_parameterlist_get_next (param_list);
 
127
   }                    /* End of loop through all parameters in the list */
 
128
 
 
129
}
 
130
 
 
131
 
 
132
 
 
133
 
 
134
/**********************************************************************/
 
135
/**
 
136
 * @brief Neatly print a parameter and its description.
 
137
 *
 
138
 * @param flag_prefix This is the prefix used to flag certain types of
 
139
 *                    switches/parameters. For example a double hypen
 
140
 *                    "--" may be used before the long form of command
 
141
 *                    line options.
 
142
 * @param keyword     This is the keyword itself. It is appended to the
 
143
 *                    flag-prefix, if any.
 
144
 * @param description This is the descriptive text that follows the 
 
145
 *                    keyword. It is put at a tab-stop, so that all the
 
146
 *                    descriptions are neatly aligned.
 
147
 *
 
148
 * This function neatly prints a keyword and its associated description
 
149
 * to the normal output channel. The formatting is handled automatically
 
150
 * to get neat alignment of the output.
 
151
 */
 
152
/**********************************************************************/
 
153
 
 
154
void er_paramutils_print_key_desc
 
155
(const char *flag_prefix, const char *keyword, const char *description)
 
156
 
 
157
{
 
158
int i, ii, tab = COMMENT_TAB_POSITION;
 
159
 
 
160
char *mystr, *ptr, *mykeyword;
 
161
 
 
162
 
 
163
ER_TRACE(__func__)
 
164
 
 
165
 
 
166
mystr = (char *) cpl_malloc((size_t) SIZE_X);
 
167
if (mystr == NULL)
 
168
   {
 
169
   cpl_msg_warning (er_func,
 
170
            "Could not allocate initial %d bytes for help description",
 
171
            SIZE_X);
 
172
   return;
 
173
   }
 
174
 
 
175
/* store the indent, optional flag ("--") and keyword */
 
176
 
 
177
i = (int) strlen(flag_prefix);
 
178
ii = (int) strlen(keyword);
 
179
if ((i+ii) > (SIZE_X-4))
 
180
   {
 
181
   cpl_msg_warning (er_func, 
 
182
                    "Size of keyword (= %d) too large - will be truncated...",ii);
 
183
   mykeyword = (char *) cpl_malloc((size_t) SIZE_X);
 
184
   (void) strncpy(mykeyword,keyword,(SIZE_X-6));
 
185
   mykeyword[SIZE_X-6] = '\0';
 
186
   }
 
187
else
 
188
   mykeyword = (char *) keyword;
 
189
 
 
190
i = sprintf(mystr,"  %s%s",flag_prefix,mykeyword);
 
191
if (tab < i) tab = i + 2;                /* if keyword quite long, move the tab */
 
192
 
 
193
 
 
194
/* pad out to the tab-point, then append ": " */
 
195
 
 
196
ptr = mystr + i;
 
197
for (ii = i; ii < tab; ii++) *ptr++ = ' ';
 
198
*ptr++ = ':';
 
199
*ptr++ = ' ';
 
200
*ptr = '\0';
 
201
tab += 2;
 
202
 
 
203
/* append description  */
 
204
 
 
205
i = tab + (int) strlen(description) + 2;
 
206
if (i > SIZE_X)
 
207
   {
 
208
   er_enlarge("paramutils_print_key_desc",&mystr,i);
 
209
   }
 
210
(void) strcpy(mystr+tab, description);          /* append full description */
 
211
 
 
212
 
 
213
/* finally, print the nicely wrapped string */
 
214
 
 
215
(void) printf ("%s\n",er_strutils_split(mystr,tab,er_strutils_termwidth()));
 
216
 
 
217
cpl_free(mystr);
 
218
 
219
/*
 
220
 
 
221
*/
 
222
 
 
223
/**********************************************************************/
 
224
/**
 
225
 * @brief Manage buffer of sources of all used parameters (Esorex + recipes)
 
226
 *
 
227
 * @param flag       Control flag:
 
228
 *                   = 1, for given param. save source in internal buffer
 
229
 *                   = 2, for given param. pull out source from internal buffer
 
230
 *                   = 3, free allocated memory
 
231
 * @param name       Name of parameter
 
232
 * @param source     desription of source of a param.
 
233
 *                   maybe "default", config-file name, "command line", etc
 
234
 *
 
235
 * This function manages a buffer of sources of all used parameters 
 
236
 * in Esorex + all called recipes.
 
237
 * Max MAX_SRC can be handled, i.e. for at most MAX_SRC different params.
 
238
 * Esorex has already 23 parameters...
 
239
 * Max length of parameter name is PAR_LEN chars.
 
240
 */
 
241
/**********************************************************************/
 
242
 
 
243
int er_manage_sources(const int flag, const char *name, char **source)
 
244
 
 
245
#define MAX_SRC 60
 
246
#define PAR_LEN 40
 
247
 
 
248
{
 
249
int offset, n, m, indx, lp;
 
250
static int no_sources = -1;
 
251
 
 
252
static char  *ptr, *p_sources[MAX_SRC];
 
253
static char  p_names[MAX_SRC*PAR_LEN];
 
254
 
 
255
size_t  slen;
 
256
 
 
257
 
 
258
 
 
259
 
 
260
/* write parameter + source into save buffer */
 
261
 
 
262
if (flag == 1)
 
263
   {
 
264
   if (no_sources < 0)                          /* first time */
 
265
      {
 
266
      no_sources = 1;
 
267
      indx = 0;
 
268
      offset = 0;
 
269
      }
 
270
   else
 
271
      {                                         /* loop through existing list */
 
272
      indx = -1;
 
273
      lp = PAR_LEN-1;
 
274
      for (n=0; n<no_sources; n++)
 
275
         {
 
276
         offset = n * PAR_LEN;
 
277
         m = strncmp(name,p_names+offset,lp);
 
278
         if (m == 0) 
 
279
            {
 
280
            indx = n;
 
281
            cpl_free(p_sources[indx]);          /* free space for new source */
 
282
            break;
 
283
            }
 
284
         } 
 
285
      if (indx == -1)                           /* we need to add new entry */
 
286
         {
 
287
         if (no_sources < MAX_SRC) 
 
288
            {
 
289
            no_sources ++;
 
290
            indx = no_sources - 1;
 
291
            offset = indx * PAR_LEN;
 
292
            }
 
293
         else
 
294
            return 1;                           /* buffer full... */
 
295
         }
 
296
      }
 
297
 
 
298
   (void) strncpy(p_names+offset,name,PAR_LEN-1);  /* save parameter name */
 
299
   *(p_names+offset+PAR_LEN-1) = '\0';
 
300
 
 
301
   slen = strlen(*source) + 1;                  /* get memory for source */
 
302
   ptr = (char *) cpl_malloc(slen);
 
303
   if (ptr == NULL) return (-2);                /* no space? just leave... */
 
304
 
 
305
   (void) strcpy(ptr,*source);
 
306
   p_sources[indx] = ptr;
 
307
   return 0;
 
308
   }
 
309
 
 
310
 
 
311
/* for given parameter read source out of save buffer */
 
312
 
 
313
else if (flag == 2)
 
314
   {
 
315
   lp = PAR_LEN-1;
 
316
   for (n=0; n<no_sources; n++)
 
317
      {
 
318
      offset = n*PAR_LEN;
 
319
      m = strncmp(name,p_names+offset,lp);
 
320
      if (m == 0)
 
321
         {
 
322
         *source = p_sources[n];
 
323
         return 0;
 
324
         }
 
325
      }
 
326
   return (-1);                                 /* par. name not found */
 
327
   }
 
328
 
 
329
 
 
330
/* free all the allocated memory */
 
331
 
 
332
else
 
333
   {
 
334
   for (n=0; n<no_sources; n++)
 
335
      {
 
336
      cpl_free(p_sources[n]);
 
337
      }
 
338
   no_sources = -1;
 
339
   return 0;
 
340
   }
 
341
}
 
342
/*
 
343
 
 
344
*/
 
345
 
 
346
/**********************************************************************/
 
347
/**
 
348
 * @brief Neatly print auxiliary information for a parameter
 
349
 *
 
350
 * @param flag_prefix This is the prefix used to flag certain types of
 
351
 *                    switches/parameters. For example a double hypen
 
352
 *                    "--" may be used before the long form of command
 
353
 *                    line options. It is only used for spacing.
 
354
 * @param keyword     This is the keyword itself. It is only used for 
 
355
 *                    spacing.
 
356
 * @param description This is the descriptive text that is to be 
 
357
 *                    printed. It is aligned to a tab stop to ensure 
 
358
 *                    that the output is neat.
 
359
 *
 
360
 * This function neatly prints an auxiliary line of information 
 
361
 * regarding a parameter. The infomation is printed to the normal output 
 
362
 * stream. The formatting is handled automatically to get neat alignment 
 
363
 * of the output.
 
364
 */
 
365
/**********************************************************************/
 
366
 
 
367
void er_paramutils_print_aux_info
 
368
(const char *flag_prefix, const char *keyword, const char *description)
 
369
 
 
370
{
 
371
int i, ii, tab = COMMENT_TAB_POSITION;
 
372
 
 
373
char  spc[88];                          /* to determine string padding */
 
374
char  str[MAXSTRLENCONF];
 
375
 
 
376
 
 
377
 
 
378
 
 
379
ER_TRACE(__func__)
 
380
 
 
381
i = (int) strlen(flag_prefix);
 
382
ii = (int) strlen(keyword);
 
383
if ((i+ii) > 80)
 
384
   {
 
385
   cpl_msg_error (er_func, "Size of prefix (= %d), keyword (= %d) too large",i,ii);
 
386
   cpl_error_set (er_func, CPL_ERROR_ILLEGAL_INPUT);
 
387
   return;
 
388
   }
 
389
 
 
390
i = sprintf (spc, "  %s%s", flag_prefix, keyword);
 
391
if (tab < i) tab = i + 2;       /* if the keyword is quite long, move the tab */
 
392
 
 
393
 
 
394
/* for the real string, pad out to the tab-point */
 
395
 
 
396
memset ((void *) str, 32,(size_t) tab);         /* int 32 = ' ' */
 
397
str[tab] = '\0';
 
398
 
 
399
(void) strcat (str, "  (source = ");    /* build up string with the description */
 
400
i = (int) strlen(description);
 
401
if (i > 900)
 
402
   {
 
403
   ii = (int) strlen(str);
 
404
   (void) sprintf (&str[ii], "%900.900s",description); /* truncate description */
 
405
   }
 
406
else
 
407
   (void) strcat (str, description);
 
408
(void) strcat (str, ")");
 
409
 
 
410
/* finally, print the nicely wrapped string */
 
411
 
 
412
(void) printf("%s\n", er_strutils_split (str, (tab + 2), er_strutils_termwidth ()));
 
413
 
 
414
}                      
 
415
 
 
416
 
 
417
 
 
418
/**********************************************************************/
 
419
/**
 
420
 * @brief  Pretty-print a parameter list with a given header text.
 
421
 *
 
422
 * @param  param_list   List of Parameters
 
423
 * @param  header       Header Text
 
424
 *
 
425
 * @returns Zero on sucess, or non-zero in the case of error.
 
426
 *
 
427
 * This function takes a parameter list, and a title and prints them.
 
428
 * The function makes use of the @c COMMENT_TAB_POSITION constant that
 
429
 * is defined in @c er_main.h .
 
430
 */
 
431
/**********************************************************************/
 
432
 
 
433
int er_paramutils_print_list (cpl_parameterlist * param_list, const char * header)
 
434
 
 
435
{
 
436
cpl_parameter *p;
 
437
cpl_msg_severity msg_level = CPL_MSG_ERROR;
 
438
cpl_type ptype;
 
439
 
 
440
const char *tmp_cmdl_keywd;
 
441
const char *tmp_string;
 
442
char tmp_cmdl_descr[MAXSTRLENCONF];
 
443
 
 
444
int i, tmp_int;
 
445
 
 
446
double tmp_double;
 
447
 
 
448
 
 
449
 
 
450
 
 
451
ER_TRACE(__func__)
 
452
 
 
453
tmp_cmdl_descr[0] = '\0';
 
454
 
 
455
/* Check that the inputs are valid */
 
456
 
 
457
if (param_list == NULL) return -1;
 
458
if (header == NULL) return -1;
 
459
 
 
460
/* Determine the message level (for printing auxiliary information) */
 
461
msg_level = cpl_msg_get_level ();
 
462
 
 
463
/* Print some header text */
 
464
(void) printf ("%s :\n\n", header);
 
465
 
 
466
/* Get the first parameter from the list */
 
467
p = cpl_parameterlist_get_first (param_list);
 
468
 
 
469
/* Loop through all the parameters in the list */
 
470
while (p != NULL)
 
471
   {                                    /* Get and print the keyword */
 
472
   tmp_cmdl_keywd = cpl_parameter_get_alias (p, CPL_PARAMETER_MODE_CLI);
 
473
 
 
474
   /* Determine the type of the parameter */
 
475
   ptype = cpl_parameter_get_type (p);
 
476
 
 
477
   /* Depending on the parameter type, print the parameter appropriately */
 
478
   switch (ptype)
 
479
      {
 
480
     case CPL_TYPE_BOOL:
 
481
      (void) sprintf (tmp_cmdl_descr, "%s",
 
482
                      cpl_parameter_get_bool (p) ? "TRUE" : "FALSE");
 
483
      break;
 
484
 
 
485
     case CPL_TYPE_INT:
 
486
      tmp_int = cpl_parameter_get_int (p);
 
487
      (void) sprintf (tmp_cmdl_descr, "%d", tmp_int);
 
488
      break;
 
489
 
 
490
     case CPL_TYPE_DOUBLE:
 
491
      tmp_double = cpl_parameter_get_double (p);
 
492
      (void) sprintf (tmp_cmdl_descr, "%s", er_strutils_dblstr (tmp_double));
 
493
      break;
 
494
 
 
495
     case CPL_TYPE_STRING:
 
496
      tmp_string = cpl_parameter_get_string (p);
 
497
      i = (int) strlen(tmp_string);
 
498
      if (i > MAXSTR)
 
499
         (void) sprintf (tmp_cmdl_descr, "%MAXSTR.MAXSTR", tmp_string);
 
500
      else
 
501
         (void) sprintf (tmp_cmdl_descr, "%s", tmp_string ? tmp_string : "-");
 
502
      break;
 
503
 
 
504
     default:
 
505
      break;
 
506
      }
 
507
 
 
508
   /* Actually print the parameter pair */
 
509
   er_paramutils_print_key_desc ("", tmp_cmdl_keywd, tmp_cmdl_descr);
 
510
 
 
511
   /* If we are set to DEBUG level, then print the source of the value too */
 
512
   if (msg_level == CPL_MSG_DEBUG)
 
513
      {
 
514
      char  *myptr;
 
515
 
 
516
      i = er_manage_sources(2,cpl_parameter_get_name (p),&myptr);
 
517
      if (i == 0)
 
518
         er_paramutils_print_aux_info ("", tmp_cmdl_keywd, myptr);
 
519
      else
 
520
         er_paramutils_print_aux_info ("", tmp_cmdl_keywd, "ambiguous...");
 
521
      }
 
522
 
 
523
   /* Determine the next parameter in the list */
 
524
   p = cpl_parameterlist_get_next (param_list);
 
525
   }                       
 
526
 
 
527
printf ("\n");
 
528
 
 
529
return 0;
 
530
 
 
531
}                               /* End of er_paramutils_print_list() */
 
532
 
 
533
 
 
534
 
 
535
/**********************************************************************/
 
536
/**
 
537
 * @brief  Set the value of a parameter from that of a given string.
 
538
 *
 
539
 * @param p          Parameter that will have its value set.
 
540
 * @param value      A string containing the value to be assigned to the 
 
541
 *                   parameter.
 
542
 * @param source     The source of @c value
 
543
 *
 
544
 * @returns 0 if successfull, !=0 otherwise
 
545
 *
 
546
 * Function sets the current value of the given parameter. Converts
 
547
 * the string value to the necessary type if needed.
 
548
 */
 
549
/**********************************************************************/
 
550
 
 
551
int paramutils_set_from_string
 
552
    (cpl_parameter * p, const char * value, const char *source)
 
553
 
 
554
{
 
555
cpl_parameter_class pclass;
 
556
cpl_type ptype;
 
557
 
 
558
int f_found = 0;        /* FALSE */
 
559
int n, scanlen = 0, tmp_int = 0, i = 0;
 
560
 
 
561
double tmp_double = 0.0;
 
562
 
 
563
char **pptr;
 
564
 
 
565
 
 
566
 
 
567
ER_TRACE(__func__)
 
568
 
 
569
if (p == NULL)
 
570
   {
 
571
   cpl_error_set (er_func, CPL_ERROR_NULL_INPUT);
 
572
   return 1;
 
573
   }
 
574
 
 
575
 
 
576
/* save for later where the value came from */
 
577
 
 
578
   pptr = (char **) &source;
 
579
   er_manage_sources(1,cpl_parameter_get_name(p),pptr);
 
580
 
 
581
cpl_parameter_set_default_flag (p, TRUE);
 
582
 
 
583
pclass = cpl_parameter_get_class (p);
 
584
ptype = cpl_parameter_get_type (p);
 
585
 
 
586
/*
 
587
 * ????? - This is a mess. The following switch blocks should be a
 
588
 * single switch block, calling a function fn_class(p, value); depending
 
589
 * on the class. Each of those functions would determine the type and then
 
590
 * do the actual processing.
 
591
 */
 
592
 
 
593
switch (pclass)
 
594
   {
 
595
   case CPL_PARAMETER_CLASS_VALUE:
 
596
   switch (ptype)
 
597
      {
 
598
     case CPL_TYPE_BOOL:
 
599
      if ((strcmp (value, "TRUE") == 0) || (strcmp (value, "true") == 0))
 
600
         {
 
601
         cpl_parameter_set_bool (p, TRUE);
 
602
         }
 
603
      else if ((strcmp (value, "FALSE") == 0) || (strcmp (value, "false") == 0))
 
604
         {
 
605
         cpl_parameter_set_bool (p, FALSE);
 
606
         }
 
607
      else
 
608
         {
 
609
         cpl_error_set (er_func, CPL_ERROR_TYPE_MISMATCH);
 
610
         return -1;
 
611
         }
 
612
      break;
 
613
 
 
614
     case CPL_TYPE_INT:
 
615
      scanlen = sscanf (value, " %d ", &tmp_int);
 
616
      if (scanlen == 1)
 
617
         {
 
618
         cpl_parameter_set_int (p, tmp_int);
 
619
         }
 
620
      else
 
621
         {
 
622
         cpl_error_set (er_func, CPL_ERROR_TYPE_MISMATCH);
 
623
         return -1;
 
624
         }
 
625
      break;
 
626
 
 
627
     case CPL_TYPE_DOUBLE:
 
628
      scanlen = sscanf (value, " %le ", &tmp_double);
 
629
      if (scanlen == 1)
 
630
         {
 
631
         cpl_parameter_set_double (p, tmp_double);
 
632
         }
 
633
      else
 
634
         {
 
635
         cpl_error_set (er_func, CPL_ERROR_TYPE_MISMATCH);
 
636
         return -1;
 
637
         }
 
638
      break;
 
639
 
 
640
     case CPL_TYPE_STRING:
 
641
      cpl_parameter_set_string (p, value);
 
642
      break;
 
643
 
 
644
     default:
 
645
      break;
 
646
      }
 
647
 
 
648
   break;
 
649
 
 
650
 
 
651
  case CPL_PARAMETER_CLASS_RANGE:
 
652
 
 
653
   switch (ptype)
 
654
      {
 
655
     case CPL_TYPE_BOOL:
 
656
      break;
 
657
 
 
658
     case CPL_TYPE_INT:
 
659
      scanlen = sscanf (value, " %d ", &tmp_int);
 
660
      if (scanlen == 1)
 
661
         {
 
662
         if ((tmp_int <= cpl_parameter_get_range_max_int (p)) &&
 
663
             (tmp_int >= cpl_parameter_get_range_min_int (p)))
 
664
            {
 
665
            cpl_parameter_set_int (p, tmp_int);
 
666
            }
 
667
         else
 
668
            {
 
669
            cpl_error_set (er_func, CPL_ERROR_ILLEGAL_INPUT);
 
670
            cpl_msg_error (er_func,
 
671
                          "Specified 'int' value (%d) for the parameter '%s' was outside the "
 
672
                           "permitted range %d to %d\n", tmp_int, 
 
673
                           cpl_parameter_get_alias (p, CPL_PARAMETER_MODE_CLI),
 
674
                           cpl_parameter_get_range_min_int (p),
 
675
                           cpl_parameter_get_range_max_int (p));
 
676
            return -1;
 
677
            }
 
678
         }
 
679
      else
 
680
         {
 
681
         cpl_error_set (er_func, CPL_ERROR_TYPE_MISMATCH);
 
682
         return -1;
 
683
         }
 
684
      break;
 
685
 
 
686
     case CPL_TYPE_DOUBLE:
 
687
      scanlen = sscanf (value, " %le ", &tmp_double);
 
688
      if (scanlen == 1)
 
689
         {
 
690
         if ((tmp_double <= cpl_parameter_get_range_max_double (p)) &&
 
691
             (tmp_double >= cpl_parameter_get_range_min_double (p)))
 
692
            {
 
693
            cpl_parameter_set_double (p, tmp_double);
 
694
            }
 
695
         else
 
696
            {
 
697
            char  *tmp;
 
698
 
 
699
 
 
700
            cpl_error_set (er_func, CPL_ERROR_ILLEGAL_INPUT);
 
701
 
 
702
 
 
703
            /*          simple version
 
704
                        cpl_msg_error (er_func,
 
705
                           "Specified 'double' value (%lg) for the parameter '%s' was outside the "
 
706
                           "permitted range %lg to %lg\n", tmp_double, 
 
707
                           cpl_parameter_get_alias (p, CPL_PARAMETER_MODE_CLI),
 
708
                           cpl_parameter_get_range_min_double (p),
 
709
                           cpl_parameter_get_range_max_double (p));
 
710
            */
 
711
 
 
712
 
 
713
            tmp = (char *) cpl_malloc((size_t) 1024);
 
714
            if ( tmp == NULL)
 
715
               {
 
716
               cpl_msg_error (er_func, "Could not allocate 1024 bytes for tmp");
 
717
               return -1;
 
718
               }
 
719
 
 
720
            /*
 
721
             * formulate the error message in steps... 
 
722
             * er_strutils_dblstr() uses an internal static buffer!
 
723
             */
 
724
 
 
725
            (void) strcpy (tmp, "Specified 'double' value (");
 
726
            (void) strcat (tmp, er_strutils_dblstr (tmp_double));
 
727
            (void) strcat (tmp, ") for the parameter '");
 
728
            (void) strcat (tmp, cpl_parameter_get_alias (p, CPL_PARAMETER_MODE_CLI));
 
729
            (void) strcat (tmp, "' was outside the permitted range ");
 
730
            (void) strcat (tmp, er_strutils_dblstr (cpl_parameter_get_range_min_double (p)));
 
731
            (void) strcat (tmp, " to ");
 
732
            (void) strcat (tmp, er_strutils_dblstr (cpl_parameter_get_range_max_double (p)));
 
733
            (void) strcat (tmp, ".\n");
 
734
            cpl_msg_error (er_func, tmp);
 
735
 
 
736
            cpl_free(tmp);
 
737
            return -1;
 
738
            }
 
739
         }
 
740
      else
 
741
         {
 
742
         cpl_error_set (er_func, CPL_ERROR_TYPE_MISMATCH);
 
743
         return -1;
 
744
         }
 
745
      break;
 
746
 
 
747
     case CPL_TYPE_STRING:
 
748
      break;
 
749
 
 
750
     default:
 
751
      break;
 
752
      }
 
753
 
 
754
   break;
 
755
 
 
756
 
 
757
  case CPL_PARAMETER_CLASS_ENUM:
 
758
 
 
759
   switch (ptype)
 
760
      {
 
761
 
 
762
     case CPL_TYPE_BOOL:
 
763
      break;
 
764
 
 
765
     case CPL_TYPE_INT:
 
766
      scanlen = sscanf (value, " %d ", &tmp_int);
 
767
      if (scanlen == 1)
 
768
         {
 
769
         for (i=0; i<cpl_parameter_get_enum_size (p); i++)
 
770
            {
 
771
            if (tmp_int == cpl_parameter_get_enum_int (p, i))
 
772
               {
 
773
               cpl_parameter_set_int (p, tmp_int);
 
774
               f_found = 1;
 
775
               }
 
776
            }
 
777
         if (!f_found)
 
778
            {
 
779
            cpl_error_set (er_func, CPL_ERROR_INCOMPATIBLE_INPUT);
 
780
            return -1;
 
781
            }
 
782
         }
 
783
      else
 
784
         {
 
785
         cpl_error_set (er_func, CPL_ERROR_TYPE_MISMATCH);
 
786
         return -1;
 
787
         }
 
788
      break;
 
789
 
 
790
     case CPL_TYPE_DOUBLE:
 
791
      scanlen = sscanf (value, " %le ", &tmp_double);
 
792
      if (scanlen == 1)
 
793
         {
 
794
 
 
795
         /* WARNING --- The following code may need fabs() */
 
796
 
 
797
         for (i=0; i<cpl_parameter_get_enum_size (p); i++)
 
798
            {
 
799
            if ((cpl_parameter_get_enum_double (p, i) - tmp_double) < 0.000001)
 
800
               {
 
801
               cpl_parameter_set_double (p, tmp_double);
 
802
               f_found = 1;
 
803
               }
 
804
            }
 
805
         if (!f_found )
 
806
            {
 
807
            cpl_error_set (er_func, CPL_ERROR_INCOMPATIBLE_INPUT);
 
808
            return -1;
 
809
            }
 
810
 
 
811
         }
 
812
      else
 
813
         {
 
814
         cpl_error_set (er_func, CPL_ERROR_TYPE_MISMATCH);
 
815
         return -1;
 
816
         }
 
817
      break;
 
818
 
 
819
     case CPL_TYPE_STRING:
 
820
      for (i=0; i<cpl_parameter_get_enum_size (p); i++)
 
821
         {
 
822
         if (strcmp (cpl_parameter_get_enum_string (p, i), value) == 0)
 
823
            {
 
824
            cpl_parameter_set_string (p, value);
 
825
            f_found = 1;
 
826
            }
 
827
         }
 
828
      if (! f_found )
 
829
         {
 
830
         cpl_error_set (er_func, CPL_ERROR_INCOMPATIBLE_INPUT);
 
831
         return -1;
 
832
         }
 
833
 
 
834
      break;
 
835
 
 
836
     default:
 
837
      break;
 
838
      }
 
839
 
 
840
   break;
 
841
 
 
842
 
 
843
  default:
 
844
   break;
 
845
 
 
846
   }                            /* End of switch(pclass) */
 
847
 
 
848
return 0;
 
849
 
 
850
}
 
851
 
 
852
/**@}*/
 
853
 
 
854
 
 
855
/* End of file */