source: trunk/zoo-project/zoo-kernel/service_yaml.c @ 566

Last change on this file since 566 was 490, checked in by djay, 10 years ago

Remove memory leaks from ZOO-Kernel. Fix issue #99.

File size: 13.7 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 *  Copyright 2014 GeoLabs SARL. All rights reserved.
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 <stdio.h>
26#include <ctype.h>
27#include <service.h>
28#include <yaml.h>
29
30static service* my_service=NULL;
31static map* current_content=NULL;
32static elements* current_element=NULL;
33static char* curr_key;
34
35/**
36 * getServiceFromFile :
37 * set service given as second parameter with informations extracted from the
38 * definition file.
39 */
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44int getServiceFromYAML(maps* conf, char* file,service** service,char *name){
45  FILE *fh = fopen("test.yml", "r");
46  if(current_content!=NULL){
47    freeMap(&current_content);
48    free(current_content);
49    current_content=NULL;
50  }
51#ifdef DEBUG_SERVICE_CONF
52  fprintf(stderr,"(STARTING)FREE current_element\n");
53#endif
54  if(current_element!=NULL){
55    freeElements(&current_element);
56    free(current_element);
57    current_element=NULL;
58  }
59  my_service=NULL;
60 
61  my_service=*service;
62  my_service->name=strdup(name);
63  my_service->content=NULL;
64  my_service->metadata=NULL;
65  my_service->inputs=NULL;
66  my_service->outputs=NULL;
67  fh = fopen(file,"r");
68  if (fh==NULL){
69    fprintf(stderr,"error : file not found\n") ;
70    return -1;
71  }
72  yaml_parser_t parser;
73  yaml_token_t  token;   /* new variable */
74
75  /* Initialize parser */
76  if(!yaml_parser_initialize(&parser))
77    fputs("Failed to initialize parser!\n", stderr);
78  if(fh == NULL)
79    fputs("Failed to open file!\n", stderr);
80  /* Set input file */
81  yaml_parser_set_input_file(&parser, fh);
82  /* BEGIN new code */
83  int level=0;
84  int plevel=level;
85  int ilevel=-1;
86  int blevel=-1;
87  int ttype=0;
88  int wait_metadata=-1;
89  char *cur_key;
90  do {
91    yaml_parser_scan(&parser, &token);
92    switch(token.type)
93    {
94    /* Stream start/end */
95    case YAML_STREAM_START_TOKEN: 
96#ifdef DEBUG_YAML
97      puts("STREAM START"); 
98#endif
99      break;
100    case YAML_STREAM_END_TOKEN:   
101#ifdef DEBUG_YAML
102      puts("STREAM END");   
103#endif
104      break;
105    /* Token types (read before actual token) */
106    case YAML_KEY_TOKEN:   
107#ifdef DEBUG_YAML
108      printf("(Key token)   "); 
109#endif
110      ttype=0;
111      break;
112    case YAML_VALUE_TOKEN: 
113#ifdef DEBUG_YAML
114      printf("(Value token) "); 
115#endif
116      ttype=1;
117      break;
118    /* Block delimeters */
119    case YAML_BLOCK_SEQUENCE_START_TOKEN: 
120#ifdef DEBUG_YAML
121      puts("<b>Start Block (Sequence)</b>"); 
122#endif
123      break;
124    case YAML_BLOCK_ENTRY_TOKEN:         
125#ifdef DEBUG_YAML
126      puts("<b>Start Block (Entry)</b>");   
127#endif
128      break;
129    case YAML_BLOCK_END_TOKEN:     
130      blevel--;
131      if(ilevel>=0)
132        ilevel--;
133#ifdef DEBUG_YAML
134      printf("<b>End block</b> (%d,%d,%d,%d)\n", blevel,level,ilevel,ttype); 
135#endif
136      break;
137    /* Data */
138    case YAML_BLOCK_MAPPING_START_TOKEN: 
139#ifdef DEBUG_YAML
140      puts("[Block mapping]");           
141#endif
142      blevel++;
143      break;
144    case YAML_SCALAR_TOKEN: 
145      if(ttype==0){
146        cur_key=zStrdup((char *)token.data.scalar.value);
147      }
148      if(ttype==1){
149        if(current_content==NULL){
150          current_content=createMap(cur_key,(char *)token.data.scalar.value);
151        }else{
152          addToMap(current_content,cur_key,(char *)token.data.scalar.value);
153        }
154        free(cur_key);
155        cur_key=NULL;
156      }
157
158      if(ttype==0 && blevel==0 && level==0 && strcasecmp((char *)token.data.scalar.value,"MetaData")==0 && blevel==0){
159        addMapToMap(&my_service->content,current_content);
160#ifdef DEBUG_YAML
161        fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
162#endif
163        freeMap(&current_content);
164        free(current_content);
165        current_content=NULL;
166        wait_metadata=1;
167      }
168      if(ttype==0 && blevel>0 && level>0 && strcasecmp((char *)token.data.scalar.value,"MetaData")==0){
169        if(current_element->content==NULL && current_content!=NULL)
170          addMapToMap(&current_element->content,current_content);
171#ifdef DEBUG_YAML
172        dumpMap(current_content);
173        fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
174#endif
175        freeMap(&current_content);
176        free(current_content);
177        current_content=NULL;
178        wait_metadata=1;
179      }
180      if(ttype==0 && strcasecmp((char *)token.data.scalar.value,"inputs")==0 && blevel==0){
181        if(wait_metadata>0){
182          addMapToMap(&my_service->metadata,current_content);
183          wait_metadata=-1;
184        }else{
185          if(current_content!=NULL && my_service->content==NULL)
186            addMapToMap(&my_service->content,current_content);
187        }
188#ifdef DEBUG_YAML
189        dumpMap(current_content);
190        fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
191#endif
192        freeMap(&current_content);
193        free(current_content);
194        current_content=NULL;
195        wait_metadata=false;
196        level++;
197      }
198      if(ttype==0 && strcasecmp((char *)token.data.scalar.value,"outputs")==0 && blevel==1){
199        level++;
200#ifdef DEBUG_YAML
201        dumpMap(current_content);
202        printf("\n***\n%d (%d,%d,%d,%d)\n+++\n", current_element->defaults==NULL,blevel,level,ilevel,ttype); 
203#endif
204        if(current_element->defaults==NULL && current_content!=NULL && ilevel<0){
205          current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
206          current_element->defaults->content=NULL;
207          current_element->defaults->next=NULL;
208          addMapToMap(&current_element->defaults->content,current_content);
209#ifdef DEBUG_YAML
210          dumpElements(current_element);
211          dumpMap(current_content);
212          fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
213#endif
214          freeMap(&current_content);
215          free(current_content);
216          current_content=NULL;
217        }else{
218          if(current_content!=NULL && ilevel<=0){
219            addMapToIoType(&current_element->supported,current_content);
220#ifdef DEBUG_YAML
221            dumpElements(current_element);
222            dumpMap(current_content);
223            fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
224#endif
225            freeMap(&current_content);
226            free(current_content);
227            current_content=NULL;
228          }
229        }
230      }
231      if(level==1 && strcasecmp((char *)token.data.scalar.value,"default")==0){
232        ilevel=0;
233      }
234      if(level==1 && strcasecmp((char *)token.data.scalar.value,"supported")==0){
235#ifdef DEBUG_YAML
236        dumpMap(current_content);
237        printf("\n***\n%d (%d,%d,%d,%d)\n+++\n", current_element->defaults==NULL,blevel,level,ilevel,ttype); 
238#endif
239        if(current_element->defaults==NULL && current_content!=NULL && ilevel<0){
240          current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
241          current_element->defaults->content=NULL;
242          current_element->defaults->next=NULL;
243          addMapToMap(&current_element->defaults->content,current_content);
244#ifdef DEBUG_YAML
245          dumpElements(current_element);
246          dumpMap(current_content);
247          fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
248#endif
249          freeMap(&current_content);
250          free(current_content);
251          current_content=NULL;
252        }else{
253          if(current_content!=NULL && ilevel<=0){
254            if(current_element->supported==NULL){
255              current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
256              current_element->supported->content=NULL;
257              current_element->supported->next=NULL;
258            }
259            addMapToMap(&current_element->supported->content,current_content);
260#ifdef DEBUG_YAML
261            dumpElements(current_element);
262            fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
263#endif
264            freeMap(&current_content);
265            free(current_content);
266            current_content=NULL;
267          }
268        }
269        ilevel=1;
270      }
271
272
273      if(strncasecmp((char *)token.data.scalar.value,"ComplexData",11)==0 || 
274         strncasecmp((char *)token.data.scalar.value,"LiteralData",10)==0 || 
275         strncasecmp((char *)token.data.scalar.value,"ComplexOutput",13)==0 || 
276         strncasecmp((char *)token.data.scalar.value,"LiteralOutput",12)==0 || 
277         strncasecmp((char *)token.data.scalar.value,"BoundingBoxOutput",13)==0 || 
278         strncasecmp((char *)token.data.scalar.value,"BoundingBoxData",12)==0){
279        current_element->format=zStrdup((char *)token.data.scalar.value);
280        free(cur_key);
281        cur_key=NULL;
282        if(wait_metadata>0 && current_content!=NULL){
283          addMapToMap(&current_element->metadata,current_content);
284          wait_metadata=-1;
285        }else{
286          if(current_content!=NULL){
287            addMapToMap(&current_element->content,current_content);
288          }
289        }
290#ifdef DEBUG_YAML
291        dumpMap(current_content);
292        fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
293#endif
294        freeMap(&current_content);
295        free(current_content);
296        current_content=NULL;
297#ifdef DEBUG_YAML
298        dumpElements(current_element);
299#endif
300      }
301
302      if(blevel==1 && level==1){
303        if(current_element!=NULL && current_content!=NULL){
304          if(current_element->defaults==NULL){
305            current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
306            current_element->defaults->content=NULL;
307            current_element->defaults->next=NULL;
308            addMapToMap(&current_element->defaults->content,current_content);
309          }else{
310            if(current_element->supported==NULL){
311              current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
312              current_element->supported->content=NULL;
313              current_element->supported->next=NULL;
314              addMapToMap(&current_element->supported->content,current_content);
315            }else
316              addMapToIoType(&current_element->supported,current_content);
317          }
318        }
319        if(current_element!=NULL){
320          if(my_service->inputs==NULL)
321            my_service->inputs=dupElements(current_element);
322          else
323            addToElements(&my_service->inputs,current_element);
324          freeElements(&current_element);
325          free(current_element);
326        }
327        plevel=level;
328        current_element=(elements*)malloc(ELEMENTS_SIZE);
329        current_element->name=zStrdup((char *)token.data.scalar.value);
330        current_element->content=NULL;
331        current_element->metadata=NULL;
332        current_element->format=NULL;
333        current_element->defaults=NULL;
334        current_element->supported=NULL;
335        current_element->next=NULL;
336       
337      }
338      if(blevel==1 && level==2){
339        if(current_element!=NULL && current_content!=NULL){
340          if(current_element->defaults==NULL){
341            current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
342            current_element->defaults->content=NULL;
343            current_element->defaults->next=NULL;
344            addMapToMap(&current_element->defaults->content,current_content);
345          }else{
346            if(current_element->supported==NULL){
347              current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
348              current_element->supported->content=NULL;
349              current_element->supported->next=NULL;
350              addMapToMap(&current_element->supported->content,current_content);
351            }else
352              addMapToIoType(&current_element->supported,current_content);
353          }
354        }
355        if(current_element!=NULL){
356          if(plevel==level){
357            if(my_service->outputs==NULL)
358              my_service->outputs=dupElements(current_element);
359            else
360              addToElements(&my_service->outputs,current_element);
361          }else{
362            if(my_service->inputs==NULL)
363              my_service->inputs=dupElements(current_element);
364            else
365              addToElements(&my_service->inputs,current_element);
366          }
367          freeElements(&current_element);
368          free(current_element);
369        }
370        plevel=level;
371        current_element=(elements*)malloc(ELEMENTS_SIZE);
372        current_element->name=zStrdup((char *)token.data.scalar.value);
373        current_element->content=NULL;
374        current_element->metadata=NULL;
375        current_element->format=NULL;
376        current_element->defaults=NULL;
377        current_element->supported=NULL;
378        current_element->next=NULL;
379       
380      }
381
382
383#ifdef DEBUG_YAML
384      printf("scalar %s (%d,%d,%d,%d,%d)\n", token.data.scalar.value,blevel,level,plevel,ilevel,ttype); 
385#endif
386      break;
387    /* Others */
388    default:
389      if(token.type==0){
390        char tmp[1024];
391        sprintf(tmp,"Wrong charater found in %s: \\t",name);
392        setMapInMaps(conf,"lenv","message",tmp);
393        return -1;
394      }
395#ifdef DEBUG_YAML
396      printf("Got token of type %d\n", token.type);
397#endif
398      break;
399    }
400    if(token.type != YAML_STREAM_END_TOKEN )
401      yaml_token_delete(&token);
402  } while(token.type != YAML_STREAM_END_TOKEN);
403  yaml_token_delete(&token);
404
405
406#ifdef DEBUG_YAML
407  fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
408#endif
409  if(current_element!=NULL && current_content!=NULL){
410    if(current_element->defaults==NULL){
411      current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
412      current_element->defaults->content=NULL;
413      current_element->defaults->next=NULL;
414      addMapToMap(&current_element->defaults->content,current_content);
415    }else{
416      if(current_element->supported==NULL){
417        current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
418        current_element->supported->content=NULL;
419        current_element->supported->next=NULL;
420        addMapToMap(&current_element->supported->content,current_content);
421      }else
422        addMapToIoType(&current_element->supported,current_content);
423    }
424#ifdef DEBUG_YAML
425    dumpMap(current_content);
426    fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
427#endif
428    freeMap(&current_content);
429    free(current_content);
430    current_content=NULL;
431  }
432  if(current_element!=NULL){
433    if(my_service->outputs==NULL)
434      my_service->outputs=dupElements(current_element);
435    else
436      addToElements(&my_service->outputs,current_element);
437    freeElements(&current_element);
438    free(current_element);
439    current_element=NULL;
440  }
441  /* END new code */
442
443  /* Cleanup */
444  yaml_parser_delete(&parser);
445  fclose(fh);
446
447#ifdef DEBUG_YAML
448  dumpService(my_service);
449#endif
450  *service=my_service;
451
452  return 1;
453}
454#ifdef __cplusplus
455}
456#endif
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png