1 | /* |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2015 GeoLabs SARL |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "service.h" |
---|
26 | |
---|
27 | |
---|
28 | #if defined(_MSC_VER) && _MSC_VER < 1800 |
---|
29 | #include <stdarg.h> |
---|
30 | /** |
---|
31 | * snprintf for Visual Studio compiler. |
---|
32 | * |
---|
33 | * See https://dxr.mozilla.org/mozilla-central/source/media/mtransport/third_party/nrappkit/src/util/util.c |
---|
34 | */ |
---|
35 | int snprintf(char *buffer, size_t n, const char *format, ...) |
---|
36 | { |
---|
37 | va_list argp; |
---|
38 | int ret; |
---|
39 | va_start(argp, format); |
---|
40 | ret = _vscprintf(format, argp); |
---|
41 | vsnprintf_s(buffer, n, _TRUNCATE, format, argp); |
---|
42 | va_end(argp); |
---|
43 | return ret; |
---|
44 | } |
---|
45 | #endif |
---|
46 | |
---|
47 | /** |
---|
48 | * Dump a map on stderr |
---|
49 | * |
---|
50 | * @param t the map to dump |
---|
51 | */ |
---|
52 | void _dumpMap(map* t){ |
---|
53 | if(t!=NULL){ |
---|
54 | fprintf(stderr,"%s: %s\n",t->name,t->value); |
---|
55 | fflush(stderr); |
---|
56 | }else{ |
---|
57 | fprintf(stderr,"NULL\n"); |
---|
58 | fflush(stderr); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Dump a map on stderr, see _dumpMap() |
---|
64 | * |
---|
65 | * @param t the map to dump |
---|
66 | */ |
---|
67 | void dumpMap(map* t){ |
---|
68 | map* tmp=t; |
---|
69 | while(tmp!=NULL){ |
---|
70 | _dumpMap(tmp); |
---|
71 | tmp=tmp->next; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Dump a map to a file |
---|
77 | * |
---|
78 | * @param t the map to dump to file |
---|
79 | * @param file the file pointer to store the map |
---|
80 | */ |
---|
81 | void dumpMapToFile(map* t,FILE* file){ |
---|
82 | map* tmp=t; |
---|
83 | while(tmp!=NULL){ |
---|
84 | fprintf(file,"%s = %s\n",tmp->name,tmp->value); |
---|
85 | tmp=tmp->next; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Dump a maps on stderr, see dumpMap(). |
---|
91 | * |
---|
92 | * @param m the map to dump |
---|
93 | */ |
---|
94 | void dumpMaps(maps* m){ |
---|
95 | maps* tmp=m; |
---|
96 | while(tmp!=NULL){ |
---|
97 | fprintf(stderr,"MAP => [%s] \n",tmp->name); |
---|
98 | fprintf(stderr," * CONTENT [%s] \n",tmp->name); |
---|
99 | dumpMap(tmp->content); |
---|
100 | if(tmp->child!=NULL){ |
---|
101 | fprintf(stderr," * CHILD [%s] \n",tmp->name); |
---|
102 | dumpMaps(tmp->child); |
---|
103 | fprintf(stderr," * /CHILD [%s] \n",tmp->name); |
---|
104 | } |
---|
105 | tmp=tmp->next; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Dump a maps to a file, see dumpMapToFile(). |
---|
111 | * |
---|
112 | * @param m the map to dump |
---|
113 | * @param file the the file pointer to store the map |
---|
114 | */ |
---|
115 | void _dumpMapsToFile(maps* m,FILE* file,int limit){ |
---|
116 | maps* tmp=m; |
---|
117 | int cnt=0; |
---|
118 | while(tmp!=NULL){ |
---|
119 | fprintf(file,"[%s]\n",tmp->name); |
---|
120 | if(tmp->child!=NULL){ |
---|
121 | _dumpMapsToFile(tmp->child,file,limit); |
---|
122 | }else |
---|
123 | dumpMapToFile(tmp->content,file); |
---|
124 | fflush(file); |
---|
125 | tmp=tmp->next; |
---|
126 | if(limit>=0 && cnt==limit) |
---|
127 | tmp=NULL; |
---|
128 | cnt++; |
---|
129 | } |
---|
130 | fflush(file); |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Dump a maps to a file, see _dumpMapsToFile(). |
---|
135 | * |
---|
136 | * @param m the map to dump |
---|
137 | * @param file_path the full path to the file name to store the map |
---|
138 | * @param limit the number limiting the maps to be dumped |
---|
139 | */ |
---|
140 | void dumpMapsToFile(maps* m,char* file_path,int limit){ |
---|
141 | FILE* file=fopen(file_path,"w+"); |
---|
142 | _dumpMapsToFile(m,file,limit); |
---|
143 | fflush(file); |
---|
144 | fclose(file); |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Create a new iotype* |
---|
149 | * |
---|
150 | * @return a pointer to the allocated iotype |
---|
151 | */ |
---|
152 | iotype* createIoType(){ |
---|
153 | iotype* io=(iotype*)malloc(IOTYPE_SIZE); |
---|
154 | io->content=NULL; |
---|
155 | io->next=NULL; |
---|
156 | return io; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * Create a new map |
---|
161 | * |
---|
162 | * @param name the key to add to the map |
---|
163 | * @param value the corresponding value to add to the map |
---|
164 | * @return a pointer to the allocated map |
---|
165 | */ |
---|
166 | map* createMap(const char* name,const char* value){ |
---|
167 | map* tmp=(map *)malloc(MAP_SIZE); |
---|
168 | tmp->name=zStrdup(name); |
---|
169 | tmp->value=zStrdup(value); |
---|
170 | tmp->next=NULL; |
---|
171 | return tmp; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * Create a new maps with the given name |
---|
176 | * |
---|
177 | * @param name of the maps |
---|
178 | * @return the allocated map |
---|
179 | */ |
---|
180 | maps* createMaps(const char* name){ |
---|
181 | maps* tmp = (maps *) malloc (MAPS_SIZE); |
---|
182 | tmp->name = zStrdup (name); |
---|
183 | tmp->content = NULL; |
---|
184 | tmp->child = NULL; |
---|
185 | tmp->next = NULL; |
---|
186 | return tmp; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * Count number of map in a map |
---|
191 | * |
---|
192 | * @param m the map to count |
---|
193 | * @return number of map in a map |
---|
194 | */ |
---|
195 | int count(map* m){ |
---|
196 | map* tmp=m; |
---|
197 | int c=0; |
---|
198 | while(tmp!=NULL){ |
---|
199 | c++; |
---|
200 | tmp=tmp->next; |
---|
201 | } |
---|
202 | return c; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Count number of maps in a maps |
---|
207 | * |
---|
208 | * @param m the maps to count |
---|
209 | * @return number of maps in a maps |
---|
210 | */ |
---|
211 | int maps_length(maps* m){ |
---|
212 | maps* tmp=m; |
---|
213 | int c=0; |
---|
214 | while(tmp!=NULL){ |
---|
215 | c++; |
---|
216 | tmp=tmp->next; |
---|
217 | } |
---|
218 | return c; |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * Verify if a key exist in a map |
---|
223 | * |
---|
224 | * @param m the map to search for the key |
---|
225 | * @param key the key to search in the map |
---|
226 | * @return true if the key wwas found, false in other case |
---|
227 | */ |
---|
228 | bool hasKey(map* m,const char *key){ |
---|
229 | map* tmp=m; |
---|
230 | while(tmp!=NULL){ |
---|
231 | if(strcasecmp(tmp->name,key)==0) |
---|
232 | return true; |
---|
233 | tmp=tmp->next; |
---|
234 | } |
---|
235 | #ifdef DEBUG_MAP |
---|
236 | fprintf(stderr,"NOT FOUND \n"); |
---|
237 | #endif |
---|
238 | return false; |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Access a specific maps |
---|
243 | * |
---|
244 | * @param m the maps to search for the key |
---|
245 | * @param key the key to search in the maps |
---|
246 | * @return a pointer on the maps found or NULL if not found |
---|
247 | */ |
---|
248 | maps* getMaps(maps* m,const char *key){ |
---|
249 | maps* tmp=m; |
---|
250 | while(tmp!=NULL){ |
---|
251 | if(strcasecmp(tmp->name,key)==0){ |
---|
252 | return tmp; |
---|
253 | } |
---|
254 | tmp=tmp->next; |
---|
255 | } |
---|
256 | return NULL; |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * Access a specific map |
---|
261 | * |
---|
262 | * @param m the map to search for the key |
---|
263 | * @param key the key to search in the map |
---|
264 | * @return a pointer on the map found or NULL if not found |
---|
265 | */ |
---|
266 | map* getMap(map* m,const char *key){ |
---|
267 | map* tmp=m; |
---|
268 | while(tmp!=NULL){ |
---|
269 | if(strcasecmp(tmp->name,key)==0){ |
---|
270 | return tmp; |
---|
271 | } |
---|
272 | tmp=tmp->next; |
---|
273 | } |
---|
274 | return NULL; |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | /** |
---|
279 | * Access the last map |
---|
280 | * |
---|
281 | * @param m the map to search for the lastest map |
---|
282 | * @return a pointer on the lastest map found or NULL if not found |
---|
283 | */ |
---|
284 | map* getLastMap(map* m){ |
---|
285 | map* tmp=m; |
---|
286 | while(tmp!=NULL){ |
---|
287 | if(tmp->next==NULL){ |
---|
288 | return tmp; |
---|
289 | } |
---|
290 | tmp=tmp->next; |
---|
291 | } |
---|
292 | return NULL; |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * Access a specific map from a maps |
---|
297 | * |
---|
298 | * @param m the maps to search for the key |
---|
299 | * @param key the key to search in the maps |
---|
300 | * @param subkey the key to search in the map (found for the key, if any) |
---|
301 | * @return a pointer on the map found or NULL if not found |
---|
302 | */ |
---|
303 | map* getMapFromMaps(maps* m,const char* key,const char* subkey){ |
---|
304 | maps* _tmpm=getMaps(m,key); |
---|
305 | if(_tmpm!=NULL){ |
---|
306 | map* _ztmpm=getMap(_tmpm->content,subkey); |
---|
307 | return _ztmpm; |
---|
308 | } |
---|
309 | else return NULL; |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | * Free allocated memory of a map. |
---|
314 | * Require to call free on mo after calling this function. |
---|
315 | * |
---|
316 | * @param mo the map to free |
---|
317 | */ |
---|
318 | void freeMap(map** mo){ |
---|
319 | map* _cursor=*mo; |
---|
320 | if(_cursor!=NULL){ |
---|
321 | #ifdef DEBUG |
---|
322 | fprintf(stderr,"freeMap\n"); |
---|
323 | #endif |
---|
324 | free(_cursor->name); |
---|
325 | free(_cursor->value); |
---|
326 | if(_cursor->next!=NULL){ |
---|
327 | freeMap(&_cursor->next); |
---|
328 | free(_cursor->next); |
---|
329 | } |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * Free allocated memory of a maps. |
---|
335 | * Require to call free on mo after calling this function. |
---|
336 | * |
---|
337 | * @param mo the maps to free |
---|
338 | */ |
---|
339 | void freeMaps(maps** mo){ |
---|
340 | maps* _cursor=*mo; |
---|
341 | if(_cursor && _cursor!=NULL){ |
---|
342 | #ifdef DEBUG |
---|
343 | fprintf(stderr,"freeMaps\n"); |
---|
344 | #endif |
---|
345 | free(_cursor->name); |
---|
346 | if(_cursor->content!=NULL){ |
---|
347 | freeMap(&_cursor->content); |
---|
348 | free(_cursor->content); |
---|
349 | } |
---|
350 | if(_cursor->child!=NULL){ |
---|
351 | freeMaps(&_cursor->child); |
---|
352 | free(_cursor->child); |
---|
353 | } |
---|
354 | if(_cursor->next!=NULL){ |
---|
355 | freeMaps(&_cursor->next); |
---|
356 | free(_cursor->next); |
---|
357 | } |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | /** |
---|
362 | * Verify if an elements contains a name equal to the given key. |
---|
363 | * |
---|
364 | * @param e the elements to search for the key |
---|
365 | * @param key the elements name to search |
---|
366 | * @return true if the elements contains the name, false in other cases. |
---|
367 | */ |
---|
368 | bool hasElement(elements* e,const char* key){ |
---|
369 | elements* tmp=e; |
---|
370 | while(tmp!=NULL){ |
---|
371 | if(strcasecmp(key,tmp->name)==0) |
---|
372 | return true; |
---|
373 | tmp=tmp->next; |
---|
374 | } |
---|
375 | return false; |
---|
376 | } |
---|
377 | |
---|
378 | /** |
---|
379 | * Access a specific elements named key. |
---|
380 | * |
---|
381 | * @param m the elements to search |
---|
382 | * @param key the elements name to search |
---|
383 | * @return a pointer to the specific element if found, NULL in other case. |
---|
384 | */ |
---|
385 | elements* getElements(elements* m,char *key){ |
---|
386 | elements* tmp=m; |
---|
387 | while(tmp!=NULL){ |
---|
388 | if(strcasecmp(tmp->name,key)==0) |
---|
389 | return tmp; |
---|
390 | tmp=tmp->next; |
---|
391 | } |
---|
392 | return NULL; |
---|
393 | } |
---|
394 | |
---|
395 | /** |
---|
396 | * Free allocated memory of an iotype. |
---|
397 | * Require to call free on i after calling this function. |
---|
398 | * |
---|
399 | * @param i the iotype to free |
---|
400 | */ |
---|
401 | void freeIOType(iotype** i){ |
---|
402 | iotype* _cursor=*i; |
---|
403 | if(_cursor!=NULL){ |
---|
404 | if(_cursor->next!=NULL){ |
---|
405 | freeIOType(&_cursor->next); |
---|
406 | free(_cursor->next); |
---|
407 | } |
---|
408 | freeMap(&_cursor->content); |
---|
409 | free(_cursor->content); |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | /** |
---|
414 | * Free allocated memory of an elements. |
---|
415 | * Require to call free on e after calling this function. |
---|
416 | * |
---|
417 | * @param e the iotype to free |
---|
418 | */ |
---|
419 | void freeElements(elements** e){ |
---|
420 | elements* tmp=*e; |
---|
421 | if(tmp!=NULL){ |
---|
422 | if(tmp->name!=NULL) |
---|
423 | free(tmp->name); |
---|
424 | freeMap(&tmp->content); |
---|
425 | if(tmp->content!=NULL) |
---|
426 | free(tmp->content); |
---|
427 | freeMap(&tmp->metadata); |
---|
428 | if(tmp->metadata!=NULL) |
---|
429 | free(tmp->metadata); |
---|
430 | freeMap(&tmp->additional_parameters); |
---|
431 | if(tmp->additional_parameters!=NULL) |
---|
432 | free(tmp->additional_parameters); |
---|
433 | if(tmp->format!=NULL) |
---|
434 | free(tmp->format); |
---|
435 | freeElements(&tmp->child); |
---|
436 | if(tmp->child!=NULL){ |
---|
437 | free(tmp->child); |
---|
438 | } |
---|
439 | if(tmp->defaults!=NULL){ |
---|
440 | freeIOType(&tmp->defaults); |
---|
441 | free(tmp->defaults); |
---|
442 | } |
---|
443 | if(tmp->supported!=NULL){ |
---|
444 | freeIOType(&tmp->supported); |
---|
445 | free(tmp->supported); |
---|
446 | } |
---|
447 | if(tmp->next!=NULL){ |
---|
448 | freeElements(&tmp->next); |
---|
449 | free(tmp->next); |
---|
450 | } |
---|
451 | } |
---|
452 | } |
---|
453 | |
---|
454 | /** |
---|
455 | * Free allocated memory of a service. |
---|
456 | * Require to call free on e after calling this function. |
---|
457 | * |
---|
458 | * @param s the service to free |
---|
459 | */ |
---|
460 | void freeService(service** s){ |
---|
461 | service* tmp=*s; |
---|
462 | if(tmp!=NULL){ |
---|
463 | if(tmp->name!=NULL) |
---|
464 | free(tmp->name); |
---|
465 | freeMap(&tmp->content); |
---|
466 | if(tmp->content!=NULL) |
---|
467 | free(tmp->content); |
---|
468 | freeMap(&tmp->metadata); |
---|
469 | if(tmp->metadata!=NULL) |
---|
470 | free(tmp->metadata); |
---|
471 | freeMap(&tmp->additional_parameters); |
---|
472 | if(tmp->additional_parameters!=NULL) |
---|
473 | free(tmp->additional_parameters); |
---|
474 | freeElements(&tmp->inputs); |
---|
475 | if(tmp->inputs!=NULL) |
---|
476 | free(tmp->inputs); |
---|
477 | freeElements(&tmp->outputs); |
---|
478 | if(tmp->outputs!=NULL) |
---|
479 | free(tmp->outputs); |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | /** |
---|
484 | * Add key value pair to an existing map. |
---|
485 | * |
---|
486 | * @param m the map to add the KVP |
---|
487 | * @param n the key to add |
---|
488 | * @param v the corresponding value to add |
---|
489 | */ |
---|
490 | void addToMap(map* m,const char* n,const char* v){ |
---|
491 | if(hasKey(m,n)==false){ |
---|
492 | map* _cursor=m; |
---|
493 | while(_cursor->next!=NULL){ |
---|
494 | _cursor=_cursor->next; |
---|
495 | } |
---|
496 | _cursor->next=createMap(n,v); |
---|
497 | } |
---|
498 | else{ |
---|
499 | map *tmp=getMap(m,n); |
---|
500 | if(tmp->value!=NULL) |
---|
501 | free(tmp->value); |
---|
502 | tmp->value=zStrdup(v); |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | /** |
---|
507 | * Add a key and an integer value to an existing map. |
---|
508 | * |
---|
509 | * @param m the map to add the KVP |
---|
510 | * @param n the key to add |
---|
511 | * @param v the corresponding value to add |
---|
512 | */ |
---|
513 | void addIntToMap(map* m,const char* n,const int v){ |
---|
514 | char svalue[10]; |
---|
515 | sprintf(svalue,"%d",v); |
---|
516 | if(hasKey(m,n)==false){ |
---|
517 | map* _cursor=m; |
---|
518 | while(_cursor->next!=NULL){ |
---|
519 | _cursor=_cursor->next; |
---|
520 | } |
---|
521 | _cursor->next=createMap(n,svalue); |
---|
522 | } |
---|
523 | else{ |
---|
524 | map *tmp=getMap(m,n); |
---|
525 | if(tmp->value!=NULL) |
---|
526 | free(tmp->value); |
---|
527 | tmp->value=zStrdup(svalue); |
---|
528 | } |
---|
529 | } |
---|
530 | |
---|
531 | /** |
---|
532 | * Add a key and a binary value to an existing map. |
---|
533 | * |
---|
534 | * @param m the map to add the KVP |
---|
535 | * @param n the key to add |
---|
536 | * @param v the corresponding value to add |
---|
537 | * @param size the size of the given value |
---|
538 | * @return a pointer to the updated map m |
---|
539 | */ |
---|
540 | map* addToMapWithSize(map* m,const char* n,const char* v,int size){ |
---|
541 | if(hasKey(m,n)==false){ |
---|
542 | map* _cursor=m; |
---|
543 | if(_cursor!=NULL){ |
---|
544 | addToMap(m,n,""); |
---|
545 | }else{ |
---|
546 | m=createMap(n,""); |
---|
547 | } |
---|
548 | } |
---|
549 | char sname[10]="size"; |
---|
550 | if(strlen(n)>5) |
---|
551 | sprintf(sname,"size_%s",n+6); |
---|
552 | map *tmp=getMap(m,n); |
---|
553 | if(tmp->value!=NULL) |
---|
554 | free(tmp->value); |
---|
555 | tmp->value=(char*)malloc((size+1)*sizeof(char)); |
---|
556 | if(v!=NULL) |
---|
557 | memmove(tmp->value,v,size*sizeof(char)); |
---|
558 | tmp->value[size]=0; |
---|
559 | char sin[128]; |
---|
560 | sprintf(sin,"%d",size); |
---|
561 | addToMap(m,sname,sin); |
---|
562 | return m; |
---|
563 | } |
---|
564 | |
---|
565 | /** |
---|
566 | * Add a map at the end of another map. |
---|
567 | * |
---|
568 | * @param mo the map to add mi |
---|
569 | * @param mi the map to add to mo |
---|
570 | */ |
---|
571 | void addMapToMap(map** mo,map* mi){ |
---|
572 | map* tmp=mi; |
---|
573 | map* _cursor=*mo; |
---|
574 | while(tmp!=NULL){ |
---|
575 | if(_cursor==NULL){ |
---|
576 | *mo=createMap(tmp->name,tmp->value); |
---|
577 | (*mo)->next=NULL; |
---|
578 | } |
---|
579 | else{ |
---|
580 | #ifdef DEBUG |
---|
581 | fprintf(stderr,"_CURSOR\n"); |
---|
582 | dumpMap(_cursor); |
---|
583 | #endif |
---|
584 | while(_cursor->next!=NULL) |
---|
585 | _cursor=_cursor->next; |
---|
586 | map* tmp1=getMap(*mo,tmp->name); |
---|
587 | if(tmp1==NULL){ |
---|
588 | _cursor->next=createMap(tmp->name,tmp->value); |
---|
589 | } |
---|
590 | else{ |
---|
591 | addToMap(*mo,tmp->name,tmp->value); |
---|
592 | } |
---|
593 | } |
---|
594 | _cursor=*mo; |
---|
595 | tmp=tmp->next; |
---|
596 | #ifdef DEBUG |
---|
597 | fprintf(stderr,"MO\n"); |
---|
598 | dumpMap(*mo); |
---|
599 | #endif |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | /** |
---|
604 | * Add a map to iotype. |
---|
605 | * |
---|
606 | * @param io the iotype to add the map |
---|
607 | * @param mi the map to add to io |
---|
608 | */ |
---|
609 | void addMapToIoType(iotype** io,map* mi){ |
---|
610 | iotype* tmp=*io; |
---|
611 | while(tmp->next!=NULL){ |
---|
612 | tmp=tmp->next; |
---|
613 | } |
---|
614 | tmp->next=(iotype*)malloc(IOTYPE_SIZE); |
---|
615 | tmp->next->content=NULL; |
---|
616 | addMapToMap(&tmp->next->content,mi); |
---|
617 | tmp->next->next=NULL; |
---|
618 | } |
---|
619 | |
---|
620 | /** |
---|
621 | * Access a specific map or set its value. |
---|
622 | * |
---|
623 | * @param m the map to search for the key |
---|
624 | * @param key the key to search/add in the map |
---|
625 | * @param value the value to add if the key does not exist |
---|
626 | * @return a pointer on the map found or NULL if not found |
---|
627 | */ |
---|
628 | map* getMapOrFill(map** m,const char *key,const char* value){ |
---|
629 | map* tmp=*m; |
---|
630 | map* tmpMap=getMap(tmp,key); |
---|
631 | if(tmpMap==NULL){ |
---|
632 | if(tmp!=NULL){ |
---|
633 | addToMap((*m),key,value); |
---|
634 | } |
---|
635 | else |
---|
636 | (*m)=createMap(key,value); |
---|
637 | tmpMap=getMap(*m,key); |
---|
638 | } |
---|
639 | return tmpMap; |
---|
640 | } |
---|
641 | |
---|
642 | /** |
---|
643 | * Verify if a map is contained in another map. |
---|
644 | * |
---|
645 | * @param m the map to search for i |
---|
646 | * @param i the map to search in m |
---|
647 | * @return true if i was found in m, false in other case |
---|
648 | */ |
---|
649 | bool contains(map* m,map* i){ |
---|
650 | while(i!=NULL){ |
---|
651 | if(strcasecmp(i->name,"value")!=0 && |
---|
652 | strcasecmp(i->name,"xlink:href")!=0 && |
---|
653 | strcasecmp(i->name,"useMapServer")!=0 && |
---|
654 | strcasecmp(i->name,"asReference")!=0){ |
---|
655 | map *tmp; |
---|
656 | if(hasKey(m,i->name) && (tmp=getMap(m,i->name))!=NULL && |
---|
657 | strcasecmp(i->value,tmp->value)!=0) |
---|
658 | return false; |
---|
659 | } |
---|
660 | i=i->next; |
---|
661 | } |
---|
662 | return true; |
---|
663 | } |
---|
664 | |
---|
665 | /** |
---|
666 | * Access a specific iotype from an elements. |
---|
667 | * |
---|
668 | * @param e the elements to search for the name |
---|
669 | * @param name the name to search in the elements e |
---|
670 | * @param values the map to verify it was contained in the defaults or |
---|
671 | * supported content of the elements e |
---|
672 | * @return a pointer on the iotype found or NULL if not found |
---|
673 | */ |
---|
674 | iotype* getIoTypeFromElement(elements* e,char *name, map* values){ |
---|
675 | elements* cursor=e; |
---|
676 | if(values!=NULL){ |
---|
677 | while(cursor!=NULL){ |
---|
678 | if(strcasecmp(cursor->name,name)==0 && (cursor->defaults!=NULL || cursor->supported!=NULL)){ |
---|
679 | if(contains(cursor->defaults->content,values)==true) |
---|
680 | return cursor->defaults; |
---|
681 | else{ |
---|
682 | iotype* tmp=cursor->supported; |
---|
683 | while(tmp!=NULL){ |
---|
684 | if(contains(tmp->content,values)==true) |
---|
685 | return tmp; |
---|
686 | tmp=tmp->next; |
---|
687 | } |
---|
688 | } |
---|
689 | } |
---|
690 | cursor=cursor->next; |
---|
691 | } |
---|
692 | }else{ |
---|
693 | while(cursor!=NULL){ |
---|
694 | if(strcasecmp(cursor->name,name)==0 && cursor->defaults!=NULL){ |
---|
695 | return cursor->defaults; |
---|
696 | } |
---|
697 | cursor=cursor->next; |
---|
698 | } |
---|
699 | } |
---|
700 | return NULL; |
---|
701 | } |
---|
702 | |
---|
703 | /** |
---|
704 | * Load binary values from a map (in) and add them to another map (out) |
---|
705 | * |
---|
706 | * @param out the map to add binaries values |
---|
707 | * @param in the map containing the binary values to add ti out |
---|
708 | * @param pos index of the binary in an array (in case of "MapArray") |
---|
709 | */ |
---|
710 | void loadMapBinary(map** out,map* in,int pos){ |
---|
711 | map* size=getMap(in,"size"); |
---|
712 | map *lout=*out; |
---|
713 | if(size!=NULL && pos>0){ |
---|
714 | char tmp[11]; |
---|
715 | sprintf(tmp,"size_%d",pos); |
---|
716 | size=getMap(in,tmp); |
---|
717 | sprintf(tmp,"value_%d",pos); |
---|
718 | map* tmpVin=getMap(in,tmp); |
---|
719 | map* tmpVout=getMap(lout,tmp); |
---|
720 | free(tmpVout->value); |
---|
721 | tmpVout->value=(char*)malloc((atoi(size->value)+1)*sizeof(char)); |
---|
722 | memmove(tmpVout->value,tmpVin->value,atoi(size->value)*sizeof(char)); |
---|
723 | tmpVout->value[atoi(size->value)]=0; |
---|
724 | }else{ |
---|
725 | if(size!=NULL){ |
---|
726 | map* tmpVin=getMap(in,"value"); |
---|
727 | map* tmpVout=getMap(lout,"value"); |
---|
728 | free(tmpVout->value); |
---|
729 | tmpVout->value=(char*)malloc((atoi(size->value)+1)*sizeof(char)); |
---|
730 | memmove(tmpVout->value,tmpVin->value,atoi(size->value)*sizeof(char)); |
---|
731 | tmpVout->value[atoi(size->value)]=0; |
---|
732 | } |
---|
733 | } |
---|
734 | } |
---|
735 | |
---|
736 | /** |
---|
737 | * Load binary values from a map (in) and add them to another map (out). |
---|
738 | * This function will take care of MapArray. |
---|
739 | * @see loadMapBinary |
---|
740 | * |
---|
741 | * @param out the map to add binaries values |
---|
742 | * @param in the map containing the binary values to add ti out |
---|
743 | */ |
---|
744 | void loadMapBinaries(map** out,map* in){ |
---|
745 | map* size=getMap(in,"size"); |
---|
746 | map* length=getMap(in,"length"); |
---|
747 | if(length!=NULL){ |
---|
748 | int len=atoi(length->value); |
---|
749 | int i=0; |
---|
750 | for(i=0;i<len;i++){ |
---|
751 | loadMapBinary(out,in,i); |
---|
752 | } |
---|
753 | } |
---|
754 | else |
---|
755 | if(size!=NULL) |
---|
756 | loadMapBinary(out,in,-1); |
---|
757 | } |
---|
758 | |
---|
759 | /** |
---|
760 | * Duplicate a Maps |
---|
761 | * |
---|
762 | * @param mo the maps to clone |
---|
763 | * @return the allocated maps containing a copy of the mo maps |
---|
764 | */ |
---|
765 | maps* dupMaps(maps** mo){ |
---|
766 | maps* _cursor=*mo; |
---|
767 | maps* res=NULL; |
---|
768 | if(_cursor!=NULL){ |
---|
769 | res=createMaps(_cursor->name); |
---|
770 | map* mc=_cursor->content; |
---|
771 | if(mc!=NULL){ |
---|
772 | addMapToMap(&res->content,mc); |
---|
773 | loadMapBinaries(&res->content,mc); |
---|
774 | } |
---|
775 | maps* mcs=_cursor->child; |
---|
776 | if(mcs!=NULL){ |
---|
777 | res->child=dupMaps(&mcs); |
---|
778 | } |
---|
779 | res->next=dupMaps(&_cursor->next); |
---|
780 | } |
---|
781 | return res; |
---|
782 | } |
---|
783 | |
---|
784 | /** |
---|
785 | * Add a maps at the end of another maps. |
---|
786 | * |
---|
787 | * @see addMapToMap, dupMaps, getMaps |
---|
788 | * @param mo the maps to add mi |
---|
789 | * @param mi the maps to add to mo |
---|
790 | */ |
---|
791 | void addMapsToMaps(maps** mo,maps* mi){ |
---|
792 | maps* tmp=mi; |
---|
793 | maps* _cursor=*mo; |
---|
794 | while(tmp!=NULL){ |
---|
795 | if(_cursor==NULL){ |
---|
796 | *mo=dupMaps(&mi); |
---|
797 | } |
---|
798 | else{ |
---|
799 | while(_cursor->next!=NULL) |
---|
800 | _cursor=_cursor->next; |
---|
801 | maps* tmp1=getMaps(*mo,tmp->name); |
---|
802 | if(tmp1==NULL){ |
---|
803 | _cursor->next=dupMaps(&tmp); |
---|
804 | if(tmp->child!=NULL) |
---|
805 | _cursor->next->child=dupMaps(&tmp->child); |
---|
806 | else |
---|
807 | _cursor->next->child=NULL; |
---|
808 | } |
---|
809 | else{ |
---|
810 | addMapToMap(&tmp1->content,tmp->content); |
---|
811 | if(tmp->child!=NULL) |
---|
812 | tmp1->child=dupMaps(&tmp->child); |
---|
813 | else |
---|
814 | tmp1->child=NULL; |
---|
815 | } |
---|
816 | _cursor=*mo; |
---|
817 | } |
---|
818 | tmp=tmp->next; |
---|
819 | } |
---|
820 | } |
---|
821 | |
---|
822 | /** |
---|
823 | * Access a specific map array element |
---|
824 | * |
---|
825 | * @param m the map to search for the key |
---|
826 | * @param key the key to search in the map |
---|
827 | * @param index of the MapArray |
---|
828 | * @return a pointer on the map found or NULL if not found |
---|
829 | */ |
---|
830 | map* getMapArray(map* m,const char* key,int index){ |
---|
831 | char tmp[1024]; |
---|
832 | if(index>0) |
---|
833 | sprintf(tmp,"%s_%d",key,index); |
---|
834 | else |
---|
835 | sprintf(tmp,"%s",key); |
---|
836 | #ifdef DEBUG |
---|
837 | fprintf(stderr,"** KEY %s\n",tmp); |
---|
838 | #endif |
---|
839 | map* tmpMap=getMap(m,tmp); |
---|
840 | #ifdef DEBUG |
---|
841 | if(tmpMap!=NULL) |
---|
842 | dumpMap(tmpMap); |
---|
843 | #endif |
---|
844 | return tmpMap; |
---|
845 | } |
---|
846 | |
---|
847 | /** |
---|
848 | * Add a key value in a MapArray for a specific index |
---|
849 | * |
---|
850 | * @param m the map to search for the key |
---|
851 | * @param key the key to search in the map |
---|
852 | * @param index the index of the MapArray |
---|
853 | * @param value the value to set in the MapArray |
---|
854 | * @return a pointer on the map found or NULL if not found |
---|
855 | */ |
---|
856 | void setMapArray(map* m,const char* key,int index,const char* value){ |
---|
857 | char tmp[1024]; |
---|
858 | if(index>0){ |
---|
859 | sprintf(tmp,"%s_%d",key,index); |
---|
860 | map* len=getMap(m,"length"); |
---|
861 | if((len!=NULL && atoi(len->value)<index+1) || len==NULL){ |
---|
862 | char tmp0[5]; |
---|
863 | sprintf(tmp0,"%d",index+1); |
---|
864 | addToMap(m,"length",tmp0); |
---|
865 | } |
---|
866 | } |
---|
867 | else{ |
---|
868 | sprintf(tmp,"%s",key); |
---|
869 | addToMap(m,"length","1"); |
---|
870 | } |
---|
871 | map* tmpSize=getMapArray(m,"size",index); |
---|
872 | if(tmpSize!=NULL && strncasecmp(key,"value",5)==0){ |
---|
873 | #ifdef DEBUG |
---|
874 | fprintf(stderr,"%s\n",tmpSize->value); |
---|
875 | #endif |
---|
876 | map* ptr=getMapOrFill(&m,tmp,(char *)""); |
---|
877 | free(ptr->value); |
---|
878 | ptr->value=(char*)malloc((atoi(tmpSize->value)+1)*sizeof(char)); |
---|
879 | memcpy(ptr->value,value,atoi(tmpSize->value)); |
---|
880 | } |
---|
881 | else |
---|
882 | addToMap(m,tmp,value); |
---|
883 | } |
---|
884 | |
---|
885 | /** |
---|
886 | * Add a key and an integer value to an existing map array. |
---|
887 | * |
---|
888 | * @param m the map to add the KVP |
---|
889 | * @param n the key to add |
---|
890 | * @param index the index of the MapArray |
---|
891 | * @param v the corresponding value to add |
---|
892 | */ |
---|
893 | void addIntToMapArray(map* m,const char* n,int index,const int v){ |
---|
894 | char svalue[10]; |
---|
895 | sprintf(svalue,"%d",v); |
---|
896 | setMapArray(m,n,index,svalue); |
---|
897 | } |
---|
898 | |
---|
899 | /** |
---|
900 | * Access the map "type" |
---|
901 | * |
---|
902 | * @param mt the map |
---|
903 | * @return a pointer on the map for mimeType/dataType/CRS if found, NULL in |
---|
904 | * other case |
---|
905 | */ |
---|
906 | map* getMapType(map* mt){ |
---|
907 | map* tmap=getMap(mt,(char *)"mimeType"); |
---|
908 | if(tmap==NULL){ |
---|
909 | tmap=getMap(mt,"dataType"); |
---|
910 | if(tmap==NULL){ |
---|
911 | tmap=getMap(mt,"CRS"); |
---|
912 | } |
---|
913 | } |
---|
914 | #ifdef DEBUG |
---|
915 | dumpMap(tmap); |
---|
916 | #endif |
---|
917 | return tmap; |
---|
918 | } |
---|
919 | |
---|
920 | /** |
---|
921 | * Add a Maps containing a MapArray to a Maps |
---|
922 | * |
---|
923 | * @see getMapType |
---|
924 | * @param mo the maps |
---|
925 | * @param mi the maps |
---|
926 | * @param typ the map "type" |
---|
927 | * @return |
---|
928 | */ |
---|
929 | int addMapsArrayToMaps(maps** mo,maps* mi,char* typ){ |
---|
930 | maps* tmp=mi; |
---|
931 | maps* _cursor=getMaps(*mo,tmp->name); |
---|
932 | |
---|
933 | if(_cursor==NULL) |
---|
934 | return -1; |
---|
935 | |
---|
936 | map* tmpLength=getMap(_cursor->content,"length"); |
---|
937 | char tmpLen[10]; |
---|
938 | int len=1; |
---|
939 | if(tmpLength!=NULL){ |
---|
940 | len=atoi(tmpLength->value); |
---|
941 | } |
---|
942 | |
---|
943 | char *tmpV[14]={ |
---|
944 | (char*)"size", |
---|
945 | (char*)"value", |
---|
946 | (char*)"uom", |
---|
947 | (char*)"Reference", |
---|
948 | (char*)"Order", |
---|
949 | (char*)"cache_file", |
---|
950 | (char*)"fmimeType", |
---|
951 | (char*)"xlink:href", |
---|
952 | typ, |
---|
953 | (char*)"schema", |
---|
954 | (char*)"encoding", |
---|
955 | (char*)"isCached", |
---|
956 | (char*)"LowerCorner", |
---|
957 | (char*)"UpperCorner" |
---|
958 | }; |
---|
959 | sprintf(tmpLen,"%d",len+1); |
---|
960 | addToMap(_cursor->content,"length",tmpLen); |
---|
961 | int i=0; |
---|
962 | for(i=0;i<14;i++){ |
---|
963 | map* tmpVI=getMap(tmp->content,tmpV[i]); |
---|
964 | if(tmpVI!=NULL){ |
---|
965 | #ifdef DEBUG |
---|
966 | fprintf(stderr,"%s = %s\n",tmpV[i],tmpVI->value); |
---|
967 | #endif |
---|
968 | setMapArray(_cursor->content,tmpV[i],len,tmpVI->value); |
---|
969 | } |
---|
970 | } |
---|
971 | |
---|
972 | addToMap(_cursor->content,"isArray","true"); |
---|
973 | return 0; |
---|
974 | } |
---|
975 | |
---|
976 | /** |
---|
977 | * Set a key value pair to a map contained in a Maps |
---|
978 | * |
---|
979 | * @param m the maps |
---|
980 | * @param key the maps name |
---|
981 | * @param subkey the map name included in the maps corresponding to key |
---|
982 | * @param value the corresponding value to add in the map |
---|
983 | */ |
---|
984 | void setMapInMaps(maps* m,const char* key,const char* subkey,const char *value){ |
---|
985 | maps* _tmpm=getMaps(m,key); |
---|
986 | if(_tmpm!=NULL){ |
---|
987 | map* _ztmpm=getMap(_tmpm->content,subkey); |
---|
988 | if(_ztmpm!=NULL){ |
---|
989 | if(_ztmpm->value!=NULL) |
---|
990 | free(_ztmpm->value); |
---|
991 | _ztmpm->value=zStrdup(value); |
---|
992 | }else{ |
---|
993 | maps *tmp=createMaps(key); |
---|
994 | tmp->content=createMap(subkey,value); |
---|
995 | addMapsToMaps(&_tmpm,tmp); |
---|
996 | freeMaps(&tmp); |
---|
997 | free(tmp); |
---|
998 | } |
---|
999 | }else{ |
---|
1000 | maps *tmp=createMaps(key); |
---|
1001 | tmp->content=createMap(subkey,value); |
---|
1002 | addMapsToMaps(&m,tmp); |
---|
1003 | freeMaps(&tmp); |
---|
1004 | free(tmp); |
---|
1005 | } |
---|
1006 | } |
---|
1007 | |
---|
1008 | /** |
---|
1009 | * Create an empty elements |
---|
1010 | * |
---|
1011 | * @return a pointer to the allocated elements |
---|
1012 | */ |
---|
1013 | elements* createEmptyElements(){ |
---|
1014 | elements* res=(elements*)malloc(ELEMENTS_SIZE); |
---|
1015 | res->name=NULL; |
---|
1016 | res->content=NULL; |
---|
1017 | res->metadata=NULL; |
---|
1018 | res->additional_parameters=NULL; |
---|
1019 | res->format=NULL; |
---|
1020 | res->defaults=NULL; |
---|
1021 | res->supported=NULL; |
---|
1022 | res->child=NULL; |
---|
1023 | res->next=NULL; |
---|
1024 | return res; |
---|
1025 | } |
---|
1026 | |
---|
1027 | /** |
---|
1028 | * Create a named elements |
---|
1029 | * |
---|
1030 | * @param name the elements name |
---|
1031 | * @return a pointer to the allocated elements |
---|
1032 | */ |
---|
1033 | elements* createElements(const char* name){ |
---|
1034 | elements* res=(elements*)malloc(ELEMENTS_SIZE); |
---|
1035 | res->name=zStrdup(name); |
---|
1036 | res->content=NULL; |
---|
1037 | res->metadata=NULL; |
---|
1038 | res->additional_parameters=NULL; |
---|
1039 | res->format=NULL; |
---|
1040 | res->defaults=NULL; |
---|
1041 | res->supported=NULL; |
---|
1042 | res->child=NULL; |
---|
1043 | res->next=NULL; |
---|
1044 | return res; |
---|
1045 | } |
---|
1046 | |
---|
1047 | /** |
---|
1048 | * Set the name of an elements |
---|
1049 | * |
---|
1050 | * @param name the elements name |
---|
1051 | * @return a pointer to the allocated elements |
---|
1052 | */ |
---|
1053 | void setElementsName(elements** elem,char* name){ |
---|
1054 | elements* res=*elem; |
---|
1055 | res->name=zStrdup(name); |
---|
1056 | res->content=NULL; |
---|
1057 | res->metadata=NULL; |
---|
1058 | res->format=NULL; |
---|
1059 | res->defaults=NULL; |
---|
1060 | res->supported=NULL; |
---|
1061 | res->child=NULL; |
---|
1062 | res->next=NULL; |
---|
1063 | } |
---|
1064 | |
---|
1065 | /** |
---|
1066 | * Dump an elements on stderr |
---|
1067 | * |
---|
1068 | * @param e the elements to dump |
---|
1069 | */ |
---|
1070 | void dumpElements(elements* e){ |
---|
1071 | elements* tmp=e; |
---|
1072 | while(tmp!=NULL){ |
---|
1073 | fprintf(stderr,"ELEMENT [%s]\n",tmp->name); |
---|
1074 | fprintf(stderr," > CONTENT [%s]\n",tmp->name); |
---|
1075 | dumpMap(tmp->content); |
---|
1076 | fprintf(stderr," > METADATA [%s]\n",tmp->name); |
---|
1077 | dumpMap(tmp->metadata); |
---|
1078 | fprintf(stderr," > ADDITIONAL PARAMETERS [%s]\n",tmp->name); |
---|
1079 | dumpMap(tmp->additional_parameters); |
---|
1080 | fprintf(stderr," > FORMAT [%s]\n",tmp->format); |
---|
1081 | iotype* tmpio=tmp->defaults; |
---|
1082 | int ioc=0; |
---|
1083 | while(tmpio!=NULL){ |
---|
1084 | fprintf(stderr," > DEFAULTS [%s] (%i)\n",tmp->name,ioc); |
---|
1085 | dumpMap(tmpio->content); |
---|
1086 | tmpio=tmpio->next; |
---|
1087 | ioc++; |
---|
1088 | } |
---|
1089 | tmpio=tmp->supported; |
---|
1090 | ioc=0; |
---|
1091 | while(tmpio!=NULL){ |
---|
1092 | fprintf(stderr," > SUPPORTED [%s] (%i)\n",tmp->name,ioc); |
---|
1093 | dumpMap(tmpio->content); |
---|
1094 | tmpio=tmpio->next; |
---|
1095 | ioc++; |
---|
1096 | } |
---|
1097 | if(tmp->child!=NULL){ |
---|
1098 | fprintf(stderr," > CHILD \n"); |
---|
1099 | dumpElements(tmp->child); |
---|
1100 | } |
---|
1101 | fprintf(stderr,"------------------\n"); |
---|
1102 | tmp=tmp->next; |
---|
1103 | } |
---|
1104 | } |
---|
1105 | |
---|
1106 | /** |
---|
1107 | * Dump an elements on stderr using the YAML syntaxe |
---|
1108 | * |
---|
1109 | * @param e the elements to dump |
---|
1110 | */ |
---|
1111 | void dumpElementsAsYAML(elements* e,int level){ |
---|
1112 | elements* tmp=e; |
---|
1113 | int i; |
---|
1114 | while(tmp!=NULL){ |
---|
1115 | for(i=0;i<2+(4*level);i++) |
---|
1116 | fprintf(stderr," "); |
---|
1117 | fprintf(stderr,"%s:\n",tmp->name); |
---|
1118 | map* mcurs=tmp->content; |
---|
1119 | while(mcurs!=NULL){ |
---|
1120 | for(i=0;i<4+(4*level);i++) |
---|
1121 | fprintf(stderr," "); |
---|
1122 | _dumpMap(mcurs); |
---|
1123 | mcurs=mcurs->next; |
---|
1124 | } |
---|
1125 | mcurs=tmp->metadata; |
---|
1126 | if(mcurs!=NULL){ |
---|
1127 | for(i=0;i<4+(4*level);i++) |
---|
1128 | fprintf(stderr," "); |
---|
1129 | fprintf(stderr,"MetaData:\n"); |
---|
1130 | while(mcurs!=NULL){ |
---|
1131 | for(i=0;i<6+(4*level);i++) |
---|
1132 | fprintf(stderr," "); |
---|
1133 | _dumpMap(mcurs); |
---|
1134 | mcurs=mcurs->next; |
---|
1135 | } |
---|
1136 | } |
---|
1137 | for(i=0;i<4+(4*level);i++) |
---|
1138 | fprintf(stderr," "); |
---|
1139 | if(tmp->format!=NULL) |
---|
1140 | fprintf(stderr,"%s:\n",tmp->format); |
---|
1141 | else{ |
---|
1142 | fprintf(stderr,"Child:\n"); |
---|
1143 | if(tmp->child!=NULL) |
---|
1144 | dumpElementsAsYAML(tmp->child,level+1); |
---|
1145 | } |
---|
1146 | iotype* tmpio=tmp->defaults; |
---|
1147 | int ioc=0; |
---|
1148 | while(tmpio!=NULL){ |
---|
1149 | for(i=0;i<6+(4*level);i++) |
---|
1150 | fprintf(stderr," "); |
---|
1151 | fprintf(stderr,"default:\n"); |
---|
1152 | mcurs=tmpio->content; |
---|
1153 | while(mcurs!=NULL){ |
---|
1154 | for(i=0;i<8+(4*level);i++) |
---|
1155 | fprintf(stderr," "); |
---|
1156 | if(strcasecmp(mcurs->name,"range")==0){ |
---|
1157 | fprintf(stderr,"range: \"%s\"\n",mcurs->value); |
---|
1158 | }else |
---|
1159 | _dumpMap(mcurs); |
---|
1160 | mcurs=mcurs->next; |
---|
1161 | } |
---|
1162 | tmpio=tmpio->next; |
---|
1163 | ioc++; |
---|
1164 | } |
---|
1165 | tmpio=tmp->supported; |
---|
1166 | ioc=0; |
---|
1167 | while(tmpio!=NULL){ |
---|
1168 | for(i=0;i<6+(4*level);i++) |
---|
1169 | fprintf(stderr," "); |
---|
1170 | fprintf(stderr,"supported:\n"); |
---|
1171 | mcurs=tmpio->content; |
---|
1172 | while(mcurs!=NULL){ |
---|
1173 | for(i=0;i<8+(4*level);i++) |
---|
1174 | fprintf(stderr," "); |
---|
1175 | if(strcasecmp(mcurs->name,"range")==0){ |
---|
1176 | fprintf(stderr,"range: \"%s\"\n",mcurs->value); |
---|
1177 | }else |
---|
1178 | _dumpMap(mcurs); |
---|
1179 | mcurs=mcurs->next; |
---|
1180 | } |
---|
1181 | tmpio=tmpio->next; |
---|
1182 | ioc++; |
---|
1183 | } |
---|
1184 | tmp=tmp->next; |
---|
1185 | } |
---|
1186 | } |
---|
1187 | |
---|
1188 | /** |
---|
1189 | * Duplicate an elements |
---|
1190 | * |
---|
1191 | * @param e the elements to clone |
---|
1192 | * @return the allocated elements containing a copy of the elements e |
---|
1193 | */ |
---|
1194 | elements* dupElements(elements* e){ |
---|
1195 | elements* cursor=e; |
---|
1196 | elements* tmp=NULL; |
---|
1197 | if(cursor!=NULL && cursor->name!=NULL){ |
---|
1198 | #ifdef DEBUG |
---|
1199 | fprintf(stderr,">> %s %i\n",__FILE__,__LINE__); |
---|
1200 | dumpElements(e); |
---|
1201 | fprintf(stderr,">> %s %i\n",__FILE__,__LINE__); |
---|
1202 | #endif |
---|
1203 | tmp=(elements*)malloc(ELEMENTS_SIZE); |
---|
1204 | tmp->name=zStrdup(cursor->name); |
---|
1205 | tmp->content=NULL; |
---|
1206 | addMapToMap(&tmp->content,cursor->content); |
---|
1207 | tmp->metadata=NULL; |
---|
1208 | addMapToMap(&tmp->metadata,cursor->metadata); |
---|
1209 | tmp->additional_parameters=NULL; |
---|
1210 | addMapToMap(&tmp->additional_parameters,cursor->additional_parameters); |
---|
1211 | if(cursor->format!=NULL) |
---|
1212 | tmp->format=zStrdup(cursor->format); |
---|
1213 | else |
---|
1214 | tmp->format=NULL; |
---|
1215 | if(cursor->defaults!=NULL){ |
---|
1216 | tmp->defaults=(iotype*)malloc(IOTYPE_SIZE); |
---|
1217 | tmp->defaults->content=NULL; |
---|
1218 | addMapToMap(&tmp->defaults->content,cursor->defaults->content); |
---|
1219 | tmp->defaults->next=NULL; |
---|
1220 | #ifdef DEBUG |
---|
1221 | fprintf(stderr,">> %s %i\n",__FILE__,__LINE__); |
---|
1222 | dumpMap(tmp->defaults->content); |
---|
1223 | #endif |
---|
1224 | }else |
---|
1225 | tmp->defaults=NULL; |
---|
1226 | if(cursor->supported!=NULL && cursor->supported->content!=NULL){ |
---|
1227 | tmp->supported=(iotype*)malloc(IOTYPE_SIZE); |
---|
1228 | tmp->supported->content=NULL; |
---|
1229 | addMapToMap(&tmp->supported->content,cursor->supported->content); |
---|
1230 | tmp->supported->next=NULL; |
---|
1231 | iotype *tmp2=cursor->supported->next; |
---|
1232 | while(tmp2!=NULL){ |
---|
1233 | addMapToIoType(&tmp->supported,tmp2->content); |
---|
1234 | #ifdef DEBUG |
---|
1235 | fprintf(stderr,">> %s %i\n",__FILE__,__LINE__); |
---|
1236 | dumpMap(tmp->defaults->content); |
---|
1237 | #endif |
---|
1238 | tmp2=tmp2->next; |
---|
1239 | } |
---|
1240 | } |
---|
1241 | else |
---|
1242 | tmp->supported=NULL; |
---|
1243 | if(cursor->child!=NULL) |
---|
1244 | tmp->child=dupElements(cursor->child); |
---|
1245 | else |
---|
1246 | tmp->child=NULL; |
---|
1247 | if(cursor->next!=NULL) |
---|
1248 | tmp->next=dupElements(cursor->next); |
---|
1249 | else |
---|
1250 | tmp->next=NULL; |
---|
1251 | } |
---|
1252 | return tmp; |
---|
1253 | } |
---|
1254 | |
---|
1255 | /** |
---|
1256 | * Add an elements to another elements. |
---|
1257 | * |
---|
1258 | * @see dupElements |
---|
1259 | * @param m the elements to add the e |
---|
1260 | * @param e the elements to be added to m |
---|
1261 | */ |
---|
1262 | void addToElements(elements** m,elements* e){ |
---|
1263 | elements* tmp=e; |
---|
1264 | if(*m==NULL){ |
---|
1265 | (*m)=dupElements(tmp); |
---|
1266 | }else{ |
---|
1267 | addToElements(&(*m)->next,tmp); |
---|
1268 | } |
---|
1269 | } |
---|
1270 | |
---|
1271 | /** |
---|
1272 | * Set the name of a service |
---|
1273 | * |
---|
1274 | * @param name the service name |
---|
1275 | */ |
---|
1276 | void setServiceName(service** serv,char* name){ |
---|
1277 | service* res=*serv; |
---|
1278 | res->name=zStrdup(name); |
---|
1279 | res->content=NULL; |
---|
1280 | res->metadata=NULL; |
---|
1281 | res->inputs=NULL; |
---|
1282 | res->outputs=NULL; |
---|
1283 | } |
---|
1284 | |
---|
1285 | /** |
---|
1286 | * Dump a service on stderr |
---|
1287 | * |
---|
1288 | * @param s the service to dump |
---|
1289 | */ |
---|
1290 | void dumpService(service* s){ |
---|
1291 | if(s==NULL) |
---|
1292 | return; |
---|
1293 | fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name); |
---|
1294 | if(s->content!=NULL){ |
---|
1295 | fprintf(stderr,"CONTENT MAP\n"); |
---|
1296 | dumpMap(s->content); |
---|
1297 | if(s->metadata!=NULL) |
---|
1298 | fprintf(stderr,"CONTENT METADATA\n"); |
---|
1299 | dumpMap(s->metadata); |
---|
1300 | if(s->additional_parameters!=NULL) |
---|
1301 | fprintf(stderr,"CONTENT AdditionalParameters\n"); |
---|
1302 | dumpMap(s->additional_parameters); |
---|
1303 | } |
---|
1304 | if(s->inputs!=NULL){ |
---|
1305 | fprintf(stderr,"INPUT ELEMENTS [%s]\n------------------\n",s->name); |
---|
1306 | dumpElements(s->inputs); |
---|
1307 | } |
---|
1308 | if(s->outputs!=NULL){ |
---|
1309 | fprintf(stderr,"OUTPUT ELEMENTS [%s]\n------------------\n",s->name); |
---|
1310 | dumpElements(s->outputs); |
---|
1311 | } |
---|
1312 | fprintf(stderr,"++++++++++++++++++\n"); |
---|
1313 | } |
---|
1314 | |
---|
1315 | /** |
---|
1316 | * Dump a service on stderr using the YAML syntaxe |
---|
1317 | * |
---|
1318 | * @param s the service to dump |
---|
1319 | */ |
---|
1320 | void dumpServiceAsYAML(service* s){ |
---|
1321 | int i; |
---|
1322 | fprintf(stderr,"# %s\n\n",s->name); |
---|
1323 | if(s->content!=NULL){ |
---|
1324 | map* mcurs=s->content; |
---|
1325 | dumpMap(mcurs); |
---|
1326 | mcurs=s->metadata; |
---|
1327 | if(mcurs!=NULL){ |
---|
1328 | fprintf(stderr,"MetaData:\n"); |
---|
1329 | while(mcurs!=NULL){ |
---|
1330 | for(i=0;i<2;i++) |
---|
1331 | fprintf(stderr," "); |
---|
1332 | _dumpMap(mcurs); |
---|
1333 | mcurs=mcurs->next; |
---|
1334 | } |
---|
1335 | } |
---|
1336 | } |
---|
1337 | if(s->inputs!=NULL){ |
---|
1338 | fprintf(stderr,"\ninputs:\n"); |
---|
1339 | dumpElementsAsYAML(s->inputs,0); |
---|
1340 | } |
---|
1341 | if(s->outputs!=NULL){ |
---|
1342 | fprintf(stderr,"\noutputs:\n"); |
---|
1343 | dumpElementsAsYAML(s->outputs,0); |
---|
1344 | } |
---|
1345 | } |
---|
1346 | |
---|
1347 | /** |
---|
1348 | * Duplicate a service |
---|
1349 | * |
---|
1350 | * @param s the service to clone |
---|
1351 | * @return the allocated service containing a copy of the serfvice s |
---|
1352 | */ |
---|
1353 | service* dupService(service* s){ |
---|
1354 | service *res=(service*)malloc(SERVICE_SIZE); |
---|
1355 | res->name=zStrdup(s->name); |
---|
1356 | res->content=NULL; |
---|
1357 | addMapToMap(&res->content,s->content); |
---|
1358 | res->metadata=NULL; |
---|
1359 | addMapToMap(&res->metadata,s->metadata); |
---|
1360 | res->additional_parameters=NULL; |
---|
1361 | addMapToMap(&res->additional_parameters,s->additional_parameters); |
---|
1362 | res->inputs=dupElements(s->inputs); |
---|
1363 | res->outputs=dupElements(s->outputs); |
---|
1364 | return res; |
---|
1365 | } |
---|
1366 | |
---|
1367 | /** |
---|
1368 | * Print the registry on stderr. |
---|
1369 | * |
---|
1370 | * @param r the registry |
---|
1371 | */ |
---|
1372 | void dumpRegistry(registry* r){ |
---|
1373 | registry* p=r; |
---|
1374 | while(p!=NULL){ |
---|
1375 | fprintf(stderr,"%s \n",p->name); |
---|
1376 | services* s=p->content; |
---|
1377 | s=p->content; |
---|
1378 | while(s!=NULL){ |
---|
1379 | dumpService(s->content); |
---|
1380 | s=s->next; |
---|
1381 | } |
---|
1382 | p=p->next; |
---|
1383 | } |
---|
1384 | } |
---|
1385 | |
---|
1386 | /** |
---|
1387 | * Add a service to the registry |
---|
1388 | * |
---|
1389 | * @param reg the resgitry to add the service |
---|
1390 | * @param name the registry name to update |
---|
1391 | * @param content the service to add |
---|
1392 | */ |
---|
1393 | bool addServiceToRegistry(registry** reg,char* name,service* content){ |
---|
1394 | registry *l=*reg; |
---|
1395 | int isInitial=-1; |
---|
1396 | if(l==NULL){ |
---|
1397 | l=(registry*)malloc(REGISTRY_SIZE); |
---|
1398 | isInitial=1; |
---|
1399 | } |
---|
1400 | if(l!=NULL){ |
---|
1401 | int hasLevel=-1; |
---|
1402 | while(isInitial<0 && l!=NULL){ |
---|
1403 | if(l->name!=NULL && strcasecmp(name,l->name)==0){ |
---|
1404 | hasLevel=1; |
---|
1405 | break; |
---|
1406 | } |
---|
1407 | l=l->next; |
---|
1408 | } |
---|
1409 | if(hasLevel<0){ |
---|
1410 | if(isInitial<0) |
---|
1411 | l=(registry*)malloc(REGISTRY_SIZE); |
---|
1412 | l->name=zStrdup(name); |
---|
1413 | l->content=NULL; |
---|
1414 | l->next=NULL; |
---|
1415 | } |
---|
1416 | if(l->content==NULL){ |
---|
1417 | l->content=(services*)malloc(SERVICES_SIZE); |
---|
1418 | l->content->content=dupService(content); |
---|
1419 | l->content->next=NULL; |
---|
1420 | } |
---|
1421 | else{ |
---|
1422 | services* s=l->content; |
---|
1423 | while(s->next!=NULL) |
---|
1424 | s=s->next; |
---|
1425 | s->next=(services*)malloc(SERVICES_SIZE); |
---|
1426 | s->next->content=dupService(content); |
---|
1427 | s->next->next=NULL; |
---|
1428 | } |
---|
1429 | l->next=NULL; |
---|
1430 | if(isInitial>0) |
---|
1431 | *reg=l; |
---|
1432 | else{ |
---|
1433 | registry *r=*reg; |
---|
1434 | while(r->next!=NULL) |
---|
1435 | r=r->next; |
---|
1436 | r->next=l; |
---|
1437 | r->next->next=NULL; |
---|
1438 | } |
---|
1439 | return true; |
---|
1440 | } |
---|
1441 | else |
---|
1442 | return false; |
---|
1443 | } |
---|
1444 | |
---|
1445 | /** |
---|
1446 | * Free memory allocated for the registry |
---|
1447 | * |
---|
1448 | * @param r the registry |
---|
1449 | */ |
---|
1450 | void freeRegistry(registry** r){ |
---|
1451 | registry* lr=*r; |
---|
1452 | while(lr!=NULL){ |
---|
1453 | services* s=lr->content; |
---|
1454 | free(lr->name); |
---|
1455 | while(s!=NULL){ |
---|
1456 | service* s1=s->content; |
---|
1457 | s=s->next; |
---|
1458 | if(s1!=NULL){ |
---|
1459 | freeService(&s1); |
---|
1460 | free(s1); |
---|
1461 | s1=NULL; |
---|
1462 | } |
---|
1463 | } |
---|
1464 | lr=lr->next; |
---|
1465 | } |
---|
1466 | } |
---|
1467 | |
---|
1468 | /** |
---|
1469 | * Access a service in the registry |
---|
1470 | * |
---|
1471 | * @param r the registry |
---|
1472 | * @param level the regitry to search ("concept", "generic" or "implementation") |
---|
1473 | * @param sname the service name |
---|
1474 | * @return the service pointer if a corresponding service was found or NULL |
---|
1475 | */ |
---|
1476 | service* getServiceFromRegistry(registry* r,char *level,char* sname){ |
---|
1477 | registry *lr=r; |
---|
1478 | while(lr!=NULL){ |
---|
1479 | if(strcasecmp(lr->name,level)==0){ |
---|
1480 | services* s=lr->content; |
---|
1481 | while(s!=NULL){ |
---|
1482 | if(s->content!=NULL && strcasecmp(s->content->name,sname)==0) |
---|
1483 | return s->content; |
---|
1484 | s=s->next; |
---|
1485 | } |
---|
1486 | break; |
---|
1487 | } |
---|
1488 | lr=lr->next; |
---|
1489 | } |
---|
1490 | return NULL; |
---|
1491 | } |
---|
1492 | |
---|
1493 | /** |
---|
1494 | * Apply inheritance to an out map from a reference in map |
---|
1495 | * |
---|
1496 | * @param out the map to update |
---|
1497 | * @param in the reference map (containing inherited properties) |
---|
1498 | */ |
---|
1499 | void inheritMap(map** out,map* in){ |
---|
1500 | map* content=in; |
---|
1501 | if((*out)==NULL){ |
---|
1502 | addMapToMap(out,in); |
---|
1503 | return; |
---|
1504 | } |
---|
1505 | while(content!=NULL){ |
---|
1506 | map* cmap=getMap(*out,content->name); |
---|
1507 | if(cmap==NULL) |
---|
1508 | addToMap(*out,content->name,content->value); |
---|
1509 | content=content->next; |
---|
1510 | } |
---|
1511 | } |
---|
1512 | |
---|
1513 | /** |
---|
1514 | * Apply inheritance to an out iotype from a reference in iotype |
---|
1515 | * |
---|
1516 | * @param out the iotype to update |
---|
1517 | * @param in the reference iotype (containing inherited properties) |
---|
1518 | */ |
---|
1519 | void inheritIOType(iotype** out,iotype* in){ |
---|
1520 | iotype* io=in; |
---|
1521 | iotype* oio=*out; |
---|
1522 | if(io!=NULL){ |
---|
1523 | if(*out==NULL){ |
---|
1524 | *out=(iotype*)malloc(IOTYPE_SIZE); |
---|
1525 | (*out)->content=NULL; |
---|
1526 | addMapToMap(&(*out)->content,io->content); |
---|
1527 | (*out)->next=NULL; |
---|
1528 | oio=*out; |
---|
1529 | inheritIOType(&oio->next,io->next); |
---|
1530 | }else{ |
---|
1531 | inheritIOType(&oio->next,io->next); |
---|
1532 | } |
---|
1533 | } |
---|
1534 | } |
---|
1535 | |
---|
1536 | /** |
---|
1537 | * Apply inheritance to an out elements from a reference in elements |
---|
1538 | * |
---|
1539 | * @param out the elements to update |
---|
1540 | * @param in the reference elements (containing inherited properties) |
---|
1541 | */ |
---|
1542 | void inheritElements(elements** out,elements* in){ |
---|
1543 | elements* content=in; |
---|
1544 | while(content!=NULL && *out!=NULL){ |
---|
1545 | elements* cmap=getElements(*out,content->name); |
---|
1546 | if(cmap==NULL) |
---|
1547 | addToElements(out,content); |
---|
1548 | else{ |
---|
1549 | inheritMap(&cmap->content,content->content); |
---|
1550 | inheritMap(&cmap->metadata,content->metadata); |
---|
1551 | if(cmap->format==NULL && content->format!=NULL) |
---|
1552 | cmap->format=zStrdup(content->format); |
---|
1553 | inheritIOType(&cmap->defaults,content->defaults); |
---|
1554 | if(cmap->supported==NULL) |
---|
1555 | inheritIOType(&cmap->supported,content->supported); |
---|
1556 | else{ |
---|
1557 | iotype* p=content->supported; |
---|
1558 | while(p!=NULL){ |
---|
1559 | addMapToIoType(&cmap->supported,p->content); |
---|
1560 | p=p->next; |
---|
1561 | } |
---|
1562 | } |
---|
1563 | } |
---|
1564 | content=content->next; |
---|
1565 | } |
---|
1566 | } |
---|
1567 | |
---|
1568 | /** |
---|
1569 | * Apply inheritance to a service based on a registry |
---|
1570 | * |
---|
1571 | * @param r the registry storing profiles hierarchy |
---|
1572 | * @param s the service to update depending on its inheritance |
---|
1573 | */ |
---|
1574 | void inheritance(registry *r,service** s){ |
---|
1575 | if(r==NULL) |
---|
1576 | return; |
---|
1577 | service* ls=*s; |
---|
1578 | if(ls->content==NULL) |
---|
1579 | return; |
---|
1580 | map* profile=getMap(ls->content,"extend"); |
---|
1581 | map* level=getMap(ls->content,"level"); |
---|
1582 | if(profile!=NULL&&level!=NULL){ |
---|
1583 | service* s1; |
---|
1584 | if(strncasecmp(level->value,"profile",7)==0) |
---|
1585 | s1=getServiceFromRegistry(r,(char*)"generic",profile->value); |
---|
1586 | else |
---|
1587 | s1=getServiceFromRegistry(r,level->value,profile->value); |
---|
1588 | |
---|
1589 | inheritMap(&ls->content,s1->content); |
---|
1590 | inheritMap(&ls->metadata,s1->metadata); |
---|
1591 | if(ls->inputs==NULL && s1->inputs!=NULL){ |
---|
1592 | ls->inputs=dupElements(s1->inputs); |
---|
1593 | }else{ |
---|
1594 | inheritElements(&ls->inputs,s1->inputs); |
---|
1595 | } |
---|
1596 | if(ls->outputs==NULL && s1->outputs!=NULL){ |
---|
1597 | ls->outputs=dupElements(s1->outputs); |
---|
1598 | }else |
---|
1599 | inheritElements(&ls->outputs,s1->outputs); |
---|
1600 | } |
---|
1601 | } |
---|
1602 | |
---|
1603 | /** |
---|
1604 | * Convert a maps to a char*** (only used for Fortran support) |
---|
1605 | * |
---|
1606 | * @param m the maps to convert |
---|
1607 | * @param c the resulting array |
---|
1608 | */ |
---|
1609 | void mapsToCharXXX(maps* m,char*** c){ |
---|
1610 | maps* tm=m; |
---|
1611 | int i=0; |
---|
1612 | int j=0; |
---|
1613 | char tmp[10][30][1024]; |
---|
1614 | memset(tmp,0,1024*10*10); |
---|
1615 | while(tm!=NULL){ |
---|
1616 | if(i>=10) |
---|
1617 | break; |
---|
1618 | strcpy(tmp[i][j],"name"); |
---|
1619 | j++; |
---|
1620 | strcpy(tmp[i][j],tm->name); |
---|
1621 | j++; |
---|
1622 | map* tc=tm->content; |
---|
1623 | while(tc!=NULL){ |
---|
1624 | if(j>=30) |
---|
1625 | break; |
---|
1626 | strcpy(tmp[i][j],tc->name); |
---|
1627 | j++; |
---|
1628 | strcpy(tmp[i][j],tc->value); |
---|
1629 | j++; |
---|
1630 | tc=tc->next; |
---|
1631 | } |
---|
1632 | tm=tm->next; |
---|
1633 | j=0; |
---|
1634 | i++; |
---|
1635 | } |
---|
1636 | memcpy(c,tmp,10*10*1024); |
---|
1637 | } |
---|
1638 | |
---|
1639 | /** |
---|
1640 | * Convert a char*** to a maps (only used for Fortran support) |
---|
1641 | * |
---|
1642 | * @param c the array to convert |
---|
1643 | * @param m the resulting maps |
---|
1644 | */ |
---|
1645 | void charxxxToMaps(char*** c,maps**m){ |
---|
1646 | maps* trorf=*m; |
---|
1647 | int i,j; |
---|
1648 | char tmp[10][30][1024]; |
---|
1649 | memcpy(tmp,c,10*30*1024); |
---|
1650 | for(i=0;i<10;i++){ |
---|
1651 | if(strlen(tmp[i][1])==0) |
---|
1652 | break; |
---|
1653 | trorf->name=tmp[i][1]; |
---|
1654 | trorf->content=NULL; |
---|
1655 | trorf->next=NULL; |
---|
1656 | for(j=2;j<29;j+=2){ |
---|
1657 | if(strlen(tmp[i][j+1])==0) |
---|
1658 | break; |
---|
1659 | if(trorf->content==NULL) |
---|
1660 | trorf->content=createMap(tmp[i][j],tmp[i][j+1]); |
---|
1661 | else |
---|
1662 | addToMap(trorf->content,tmp[i][j],tmp[i][j+1]); |
---|
1663 | } |
---|
1664 | trorf=trorf->next; |
---|
1665 | } |
---|
1666 | m=&trorf; |
---|
1667 | } |
---|