1 | /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc. |
---|
2 | See the COPYRIGHT file for more information. */ |
---|
3 | |
---|
4 | #include "config.h" |
---|
5 | #include "ocinternal.h" |
---|
6 | #include "ocdebug.h" |
---|
7 | |
---|
8 | #define LBRACKET '[' |
---|
9 | #define RBRACKET ']' |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | /* |
---|
14 | Client parameters are assumed to be |
---|
15 | one or more instances of bracketed pairs: |
---|
16 | e.g "[...][...]...". |
---|
17 | The bracket content in turn is assumed to be a |
---|
18 | comma separated list of <name>=<value> pairs. |
---|
19 | e.g. x=y,z=,a=b. |
---|
20 | If the same parameter is specifed more than once, |
---|
21 | then the first occurrence is used; this is so that |
---|
22 | is possible to forcibly override user specified |
---|
23 | parameters by prefixing. |
---|
24 | IMPORTANT: client parameter string is assumed to |
---|
25 | have blanks compress out. |
---|
26 | */ |
---|
27 | |
---|
28 | int |
---|
29 | ocparamdecode(OCstate* state) |
---|
30 | { |
---|
31 | int i; |
---|
32 | i = ocuridecodeparams(state->uri); |
---|
33 | return i?OC_NOERR:OC_EBADURL; |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | const char* |
---|
38 | ocparamlookup(OCstate* state, const char* key) |
---|
39 | { |
---|
40 | if(state == NULL || key == NULL || state->uri == NULL) return NULL; |
---|
41 | return ocurilookup(state->uri,key); |
---|
42 | } |
---|
43 | |
---|
44 | int |
---|
45 | ocparamset(OCstate* state, const char* params) |
---|
46 | { |
---|
47 | int i; |
---|
48 | i = ocurisetparams(state->uri,params); |
---|
49 | return i?OC_NOERR:OC_EBADURL; |
---|
50 | } |
---|
51 | |
---|
52 | #ifdef OCIGNORE |
---|
53 | void |
---|
54 | ocparamfree(OClist* params) |
---|
55 | { |
---|
56 | int i; |
---|
57 | if(params == NULL) return; |
---|
58 | for(i=0;i<oclistlength(params);i++) { |
---|
59 | char* s = (char*)oclistget(params,i); |
---|
60 | if(s != NULL) free((void*)s); |
---|
61 | } |
---|
62 | oclistfree(params); |
---|
63 | } |
---|
64 | |
---|
65 | /* |
---|
66 | Delete the entry. |
---|
67 | return value = 1 => found and deleted; |
---|
68 | 0 => param not found |
---|
69 | */ |
---|
70 | int |
---|
71 | ocparamdelete(OClist* params, const char* clientparam) |
---|
72 | { |
---|
73 | int i,found = 0; |
---|
74 | if(params == NULL || clientparam == NULL) return 0; |
---|
75 | for(i=0;i<oclistlength(params);i+=2) { |
---|
76 | char* name = (char*)oclistget(params,i); |
---|
77 | if(strcmp(clientparam,name)==0) {found=1; break;} |
---|
78 | } |
---|
79 | if(found) { |
---|
80 | oclistremove(params,i+1); /* remove value */ |
---|
81 | oclistremove(params,i); /* remove name */ |
---|
82 | } |
---|
83 | return found; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | /* |
---|
88 | Insert new client param (name,value); |
---|
89 | return value = 1 => not already defined |
---|
90 | 0 => param already defined (no change) |
---|
91 | */ |
---|
92 | int |
---|
93 | ocparaminsert(OClist* params, const char* clientparam, const char* value) |
---|
94 | { |
---|
95 | int i; |
---|
96 | if(params == NULL || clientparam == NULL) return 0; |
---|
97 | for(i=0;i<oclistlength(params);i+=2) { |
---|
98 | char* name = (char*)oclistget(params,i); |
---|
99 | if(strcmp(clientparam,name)==0) return 0; |
---|
100 | } |
---|
101 | /* not found, append */ |
---|
102 | oclistpush(params,(ocelem)nulldup(clientparam)); |
---|
103 | oclistpush(params,(ocelem)nulldup(value)); |
---|
104 | return 1; |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /* |
---|
109 | Replace new client param (name,value); |
---|
110 | return value = 1 => replacement performed |
---|
111 | 0 => insertion performed |
---|
112 | */ |
---|
113 | int |
---|
114 | ocparamreplace(OClist* params, const char* clientparam, const char* value) |
---|
115 | { |
---|
116 | int i; |
---|
117 | if(params == NULL || clientparam == NULL) return 0; |
---|
118 | for(i=0;i<oclistlength(params);i+=2) { |
---|
119 | char* name = (char*)oclistget(params,i); |
---|
120 | if(strcmp(clientparam,name)==0) { |
---|
121 | oclistinsert(params,i+1,(ocelem)nulldup(value)); |
---|
122 | return 1; |
---|
123 | } |
---|
124 | } |
---|
125 | ocparaminsert(params,clientparam,value); |
---|
126 | return 0; |
---|
127 | } |
---|
128 | #endif |
---|