svn-gvsig-desktop / trunk / libraries / libjni-proj4 / src / pj_param.c @ 7098
History | View | Annotate | Download (2.95 KB)
1 |
/* put parameters in linked list and retrieve */
|
---|---|
2 |
#ifndef lint
|
3 |
static const char SCCSID[]="@(#)pj_param.c 4.4 93/06/12 GIE REL"; |
4 |
#endif
|
5 |
#include <projects.h> |
6 |
#include <stdio.h> |
7 |
#include <string.h> |
8 |
paralist * /* create parameter list entry */
|
9 |
pj_mkparam(char *str) {
|
10 |
paralist *newitem; |
11 |
|
12 |
if (newitem = (paralist *)pj_malloc(sizeof(paralist) + strlen(str))) { |
13 |
newitem->used = 0;
|
14 |
newitem->next = 0;
|
15 |
if (*str == '+') |
16 |
++str; |
17 |
(void)strcpy(newitem->param, str);
|
18 |
} |
19 |
return newitem;
|
20 |
} |
21 |
|
22 |
/************************************************************************/
|
23 |
/* pj_param() */
|
24 |
/* */
|
25 |
/* Test for presence or get parameter value. The first */
|
26 |
/* character in `opt' is a parameter type which can take the */
|
27 |
/* values: */
|
28 |
/* */
|
29 |
/* `t' - test for presence, return TRUE/FALSE in PVALUE.i */
|
30 |
/* `i' - integer value returned in PVALUE.i */
|
31 |
/* `d' - simple valued real input returned in PVALUE.f */
|
32 |
/* `r' - degrees (DMS translation applied), returned as */
|
33 |
/* radians in PVALUE.f */
|
34 |
/* `s' - string returned in PVALUE.s */
|
35 |
/* `b' - test for t/T/f/F, return in PVALUE.i */
|
36 |
/* */
|
37 |
/************************************************************************/
|
38 |
|
39 |
PVALUE /* test for presence or get parameter value */
|
40 |
pj_param(paralist *pl, char *opt) {
|
41 |
int type;
|
42 |
unsigned l;
|
43 |
PVALUE value; |
44 |
|
45 |
type = *opt++; |
46 |
/* simple linear lookup */
|
47 |
l = strlen(opt); |
48 |
while (pl && !(!strncmp(pl->param, opt, l) &&
|
49 |
(!pl->param[l] || pl->param[l] == '=')))
|
50 |
pl = pl->next; |
51 |
if (type == 't') |
52 |
value.i = pl != 0;
|
53 |
else if (pl) { |
54 |
pl->used |= 1;
|
55 |
opt = pl->param + l; |
56 |
if (*opt == '=') |
57 |
++opt; |
58 |
switch (type) {
|
59 |
case 'i': /* integer input */ |
60 |
value.i = atoi(opt); |
61 |
break;
|
62 |
case 'd': /* simple real input */ |
63 |
value.f = atof(opt); |
64 |
break;
|
65 |
case 'r': /* degrees input */ |
66 |
value.f = dmstor(opt, 0);
|
67 |
break;
|
68 |
case 's': /* char string */ |
69 |
value.s = opt; |
70 |
break;
|
71 |
case 'b': /* boolean */ |
72 |
switch (*opt) {
|
73 |
case 'F': case 'f': |
74 |
value.i = 0;
|
75 |
break;
|
76 |
case '\0': case 'T': case 't': |
77 |
value.i = 1;
|
78 |
break;
|
79 |
default:
|
80 |
pj_errno = -8;
|
81 |
value.i = 0;
|
82 |
break;
|
83 |
} |
84 |
break;
|
85 |
default:
|
86 |
bum_type: /* note: this is an error in parameter, not a user error */ |
87 |
fprintf(stderr, "invalid request to pj_param, fatal\n");
|
88 |
exit(1);
|
89 |
} |
90 |
} else /* not given */ |
91 |
switch (type) {
|
92 |
case 'b': |
93 |
case 'i': |
94 |
value.i = 0;
|
95 |
break;
|
96 |
case 'd': |
97 |
case 'r': |
98 |
value.f = 0.;
|
99 |
break;
|
100 |
case 's': |
101 |
value.s = 0;
|
102 |
break;
|
103 |
default:
|
104 |
goto bum_type;
|
105 |
} |
106 |
return value;
|
107 |
} |