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

Last change on this file since 468 was 465, checked in by djay, 11 years ago

Add the optional YAML ZCFG support #4 and the zcfg2yaml converter. Return error messages that enable the service provider to quickly identify the root cause of errors due to configuration file syntax #90. Fix logic in addMapToMap #91. Enable multiple range definition using default and supported blocks. Add the lastest revision number in version.h (available from Python ZOO-API as zoo.VERSION).

File size: 13.5 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(token.data.scalar.value);
147      }
148      if(ttype==1){
149        if(current_content==NULL){
150          current_content=createMap(cur_key,token.data.scalar.value);
151        }else{
152          addToMap(current_content,cur_key,token.data.scalar.value);
153        }
154        free(cur_key);
155        cur_key=NULL;
156      }
157
158      if(ttype==0 && blevel==0 && level==0 && strcasecmp(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(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(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(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(token.data.scalar.value,"default")==0){
232        ilevel=0;
233      }
234      if(level==1 && strcasecmp(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(token.data.scalar.value,"ComplexData",11)==0 || strncasecmp(token.data.scalar.value,"LiteralData",10)==0
274         || strncasecmp(token.data.scalar.value,"ComplexOutput",13)==0 || strncasecmp(token.data.scalar.value,"LiteralOutput",12)==0
275         || strncasecmp(token.data.scalar.value,"BoundingBoxOutput",13)==0 || strncasecmp(token.data.scalar.value,"BoundingBoxData",12)==0){
276        current_element->format=zStrdup(token.data.scalar.value);
277        free(cur_key);
278        cur_key=NULL;
279        if(wait_metadata>0 && current_content!=NULL){
280          addMapToMap(&current_element->metadata,current_content);
281          wait_metadata=-1;
282        }else{
283          if(current_content!=NULL){
284            addMapToMap(&current_element->content,current_content);
285          }
286        }
287#ifdef DEBUG_YAML
288        dumpMap(current_content);
289        fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
290#endif
291        freeMap(&current_content);
292        free(current_content);
293        current_content=NULL;
294#ifdef DEBUG_YAML
295        dumpElements(current_element);
296#endif
297      }
298
299      if(blevel==1 && level==1){
300        if(current_element!=NULL && current_content!=NULL){
301          if(current_element->defaults==NULL){
302            current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
303            current_element->defaults->content=NULL;
304            current_element->defaults->next=NULL;
305            addMapToMap(&current_element->defaults->content,current_content);
306          }else{
307            if(current_element->supported==NULL){
308              current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
309              current_element->supported->content=NULL;
310              current_element->supported->next=NULL;
311              addMapToMap(&current_element->supported->content,current_content);
312            }else
313              addMapToIoType(&current_element->supported,current_content);
314          }
315        }
316        if(current_element!=NULL){
317          if(my_service->inputs==NULL)
318            my_service->inputs=dupElements(current_element);
319          else
320            addToElements(&my_service->inputs,current_element);
321          freeElements(&current_element);
322          free(current_element);
323        }
324        plevel=level;
325        current_element=(elements*)malloc(ELEMENTS_SIZE);
326        current_element->name=strdup(token.data.scalar.value);
327        current_element->content=NULL;
328        current_element->metadata=NULL;
329        current_element->format=NULL;
330        current_element->defaults=NULL;
331        current_element->supported=NULL;
332        current_element->next=NULL;
333       
334      }
335      if(blevel==1 && level==2){
336        if(current_element!=NULL && current_content!=NULL){
337          if(current_element->defaults==NULL){
338            current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
339            current_element->defaults->content=NULL;
340            current_element->defaults->next=NULL;
341            addMapToMap(&current_element->defaults->content,current_content);
342          }else{
343            if(current_element->supported==NULL){
344              current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
345              current_element->supported->content=NULL;
346              current_element->supported->next=NULL;
347              addMapToMap(&current_element->supported->content,current_content);
348            }else
349              addMapToIoType(&current_element->supported,current_content);
350          }
351        }
352        if(current_element!=NULL){
353          if(plevel==level){
354            if(my_service->outputs==NULL)
355              my_service->outputs=dupElements(current_element);
356            else
357              addToElements(&my_service->outputs,current_element);
358          }else{
359            if(my_service->inputs==NULL)
360              my_service->inputs=dupElements(current_element);
361            else
362              addToElements(&my_service->inputs,current_element);
363          }
364          freeElements(&current_element);
365          free(current_element);
366        }
367        plevel=level;
368        current_element=(elements*)malloc(ELEMENTS_SIZE);
369        current_element->name=strdup(token.data.scalar.value);
370        current_element->content=NULL;
371        current_element->metadata=NULL;
372        current_element->format=NULL;
373        current_element->defaults=NULL;
374        current_element->supported=NULL;
375        current_element->next=NULL;
376       
377      }
378
379
380#ifdef DEBUG_YAML
381      printf("scalar %s (%d,%d,%d,%d,%d)\n", token.data.scalar.value,blevel,level,plevel,ilevel,ttype); 
382#endif
383      break;
384    /* Others */
385    default:
386      if(token.type==0){
387        char tmp[1024];
388        sprintf(tmp,"Wrong charater found in %s: \\t",name);
389        setMapInMaps(conf,"lenv","message",tmp);
390        return -1;
391      }
392#ifdef DEBUG_YAML
393      printf("Got token of type %d\n", token.type);
394#endif
395      break;
396    }
397    if(token.type != YAML_STREAM_END_TOKEN )
398      yaml_token_delete(&token);
399  } while(token.type != YAML_STREAM_END_TOKEN);
400  yaml_token_delete(&token);
401
402
403#ifdef DEBUG_YAML
404  fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
405#endif
406  if(current_element!=NULL && current_content!=NULL){
407    if(current_element->defaults==NULL){
408      current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
409      current_element->defaults->content=NULL;
410      current_element->defaults->next=NULL;
411      addMapToMap(&current_element->defaults->content,current_content);
412    }else{
413      if(current_element->supported==NULL){
414        current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
415        current_element->supported->content=NULL;
416        current_element->supported->next=NULL;
417        addMapToMap(&current_element->supported->content,current_content);
418      }else
419        addMapToIoType(&current_element->supported,current_content);
420    }
421#ifdef DEBUG_YAML
422    dumpMap(current_content);
423    fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
424#endif
425    freeMap(&current_content);
426    free(current_content);
427    current_content=NULL;
428  }
429  if(current_element!=NULL){
430    if(my_service->outputs==NULL)
431      my_service->outputs=dupElements(current_element);
432    else
433      addToElements(&my_service->outputs,current_element);
434    freeElements(&current_element);
435    free(current_element);
436    current_element=NULL;
437  }
438  /* END new code */
439
440  /* Cleanup */
441  yaml_parser_delete(&parser);
442  fclose(fh);
443
444#ifdef DEBUG_YAML
445  dumpService(my_service);
446#endif
447  *service=my_service;
448
449  return 1;
450}
451#ifdef __cplusplus
452}
453#endif
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

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