source: trunk/zoo-kernel/service.h @ 59

Last change on this file since 59 was 59, checked in by djay, 13 years ago

Small fix for binary string support in dupMaps function to ensure that the binary value willbe passed to various laguages correctly. Memory cleanup.

File size: 14.5 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2010 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#ifndef ZOO_SERVICE_H
26#define ZOO_SERVICE_H 1
27
28#pragma once
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include <stdlib.h>
35#include <ctype.h>
36#include <stdio.h>
37#include <string.h>
38
39#define bool int
40#define true 1
41#define false -1
42
43#define SERVICE_ACCEPTED 0
44#define SERVICE_STARTED 1
45#define SERVICE_PAUSED 2
46#define SERVICE_SUCCEEDED 3
47#define SERVICE_FAILED 4
48
49#define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*2)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+sizeof(elements*))
50#define MAP_SIZE (2*sizeof(char*))+sizeof(NULL)
51#define IOTYPE_SIZE MAP_SIZE+sizeof(NULL)
52#define MAPS_SIZE (2*sizeof(char*))+sizeof(map*)+MAP_SIZE
53#define SERVICE_SIZE (ELEMENTS_SIZE*2)+(MAP_SIZE*2)+sizeof(char*)
54
55#define SHMSZ     27
56
57  /**
58   * \struct maps
59   * \brief linked list of map pointer
60   *
61   * Small object to store WPS KVP set.
62   */
63  typedef struct maps{
64    char* name;         
65    struct map* content; 
66    struct maps* next;   
67  } maps;
68
69  /**
70   * \struct map
71   * \brief KVP linked list
72   *
73   * Deal with WPS KVP (name,value).
74   */
75  typedef struct map{
76    char* name;       /* The key */
77    char* value;      /* The value */
78    struct map* next; /* Next couple */
79  } map;
80
81#ifdef WIN32
82#define NULLMAP ((map*) 0)
83#else
84#define NULLMAP NULL
85#endif
86
87  static void _dumpMap(map* t){
88    if(t!=NULL){
89      fprintf(stderr,"[%s] => [%s] \n",t->name,t->value);
90      fflush(stderr);
91    }else{
92      fprintf(stderr,"NULL\n");
93      fflush(stderr);
94    }
95  }
96
97  static void dumpMap(map* t){
98    map* tmp=t;
99    while(tmp!=NULL){
100      _dumpMap(tmp);
101      tmp=tmp->next;
102    }
103  }
104
105  static void dumpMaps(maps* m){
106    maps* tmp=m;
107    while(tmp!=NULL){
108      fprintf(stderr,"MAP => [%s] \n",tmp->name);
109      dumpMap(tmp->content);
110      tmp=tmp->next;
111    }
112  }
113
114  static map* createMap(const char* name,const char* value){
115    map* tmp=(map *)malloc(MAP_SIZE);
116    tmp->name=strdup(name);
117    tmp->value=strdup(value);
118    tmp->next=NULL;
119    return tmp;
120  }
121
122  static int count(map* m){
123    map* tmp=m;
124    int c=0;
125    while(tmp!=NULL){
126      c++;
127      tmp=tmp->next;
128    }
129    return c;
130  }
131   
132  static bool hasKey(map* m,const char *key){
133    map* tmp=m;
134    while(tmp!=NULL){
135      if(strcasecmp(tmp->name,key)==0)
136        return true;
137      tmp=tmp->next;
138    }
139#ifdef DEBUG_MAP
140    fprintf(stderr,"NOT FOUND \n");
141#endif
142    return false;
143  }
144
145  static maps* getMaps(maps* m,const char *key){
146    maps* tmp=m;
147    while(tmp!=NULL){
148      if(strcasecmp(tmp->name,key)==0){
149        return tmp;
150      }
151      tmp=tmp->next;
152    }
153    return NULL;
154  }
155
156  static map* getMap(map* m,const char *key){
157    map* tmp=m;
158    while(tmp!=NULL){
159      if(strcasecmp(tmp->name,key)==0){
160        return tmp;
161      }
162      tmp=tmp->next;
163    }
164    return NULL;
165  }
166
167  static map* getMapFromMaps(maps* m,char* key,char* subkey){
168    maps* _tmpm=getMaps(m,key);
169    if(_tmpm!=NULL){
170      map* _ztmpm=getMap(_tmpm->content,subkey);
171      return _ztmpm;
172    }
173    else return NULL;
174  }
175
176  static void freeMap(map** mo){
177    map* _cursor=*mo;
178    if(_cursor!=NULL){
179#ifdef DEBUG
180      fprintf(stderr,"freeMap\n");
181#endif
182      free(_cursor->name);
183      free(_cursor->value);
184      if(_cursor->next!=NULL){
185        freeMap(&_cursor->next);
186        free(_cursor->next);
187      }
188    }
189  }
190
191  static void freeMaps(maps** mo){
192    maps* _cursor=*mo;
193    fflush(stderr);
194    if(_cursor && _cursor!=NULL){
195#ifdef DEBUG
196      fprintf(stderr,"freeMaps\n");
197#endif
198      free(_cursor->name);
199      if(_cursor->content!=NULL){
200        freeMap(&_cursor->content);
201        free(_cursor->content);
202      }
203      if(_cursor->next!=NULL){
204        freeMaps(&_cursor->next);
205        free(_cursor->next);
206      }
207    }
208  }
209
210  typedef struct iotype{
211    struct map* content;
212    struct iotype* next;
213  } iotype;
214
215  typedef struct elements{
216    char* name;
217    struct map* content;
218    struct map* metadata;
219    char* format;
220    struct iotype* defaults;
221    struct iotype* supported;
222    struct elements* next;
223  } elements;
224
225  typedef struct service{
226    char* name;
227    struct map* content;
228    struct map* metadata;
229    struct elements* inputs;
230    struct elements* outputs; 
231  } service;
232
233  typedef struct services{
234    struct service* content; 
235    struct services* next; 
236  } services;
237
238  static bool hasElement(elements* e,char* key){
239    elements* tmp=e;
240    while(tmp!=NULL){
241      if(strcasecmp(key,tmp->name)==0)
242        return true;
243      tmp=tmp->next;
244    }
245    return false;
246  }
247
248  static elements* getElements(elements* m,char *key){
249    elements* tmp=m;
250    while(tmp!=NULL){
251      if(strcasecmp(tmp->name,key)==0)
252        return tmp;
253      tmp=tmp->next;
254    }
255    return NULL;
256  }
257
258
259  static void freeIOType(iotype** i){
260    iotype* _cursor=*i;
261    if(_cursor!=NULL){
262      if(_cursor->next!=NULL){
263        freeIOType(&_cursor->next);
264        free(_cursor->next);
265      }
266      freeMap(&_cursor->content);
267      free(_cursor->content);
268    }
269  }
270
271  static void freeElements(elements** e){
272    elements* tmp=*e;
273    if(tmp!=NULL){
274      if(tmp->name!=NULL)
275        free(tmp->name);
276      freeMap(&tmp->content);
277      if(tmp->content!=NULL)
278        free(tmp->content);
279      freeMap(&tmp->metadata);
280      if(tmp->metadata!=NULL)
281        free(tmp->metadata);
282      if(tmp->format!=NULL)
283        free(tmp->format);
284      freeIOType(&tmp->defaults);
285      if(tmp->defaults!=NULL)
286        free(tmp->defaults);
287      freeIOType(&tmp->supported);
288      if(tmp->supported!=NULL){
289        free(tmp->supported);
290      }
291      freeElements(&tmp->next);
292    }
293  }
294
295  static void freeService(service** s){
296    service* tmp=*s;
297    if(tmp!=NULL){
298      if(tmp->name!=NULL)
299        free(tmp->name);
300      freeMap(&tmp->content);
301      if(tmp->content!=NULL)
302        free(tmp->content);
303      freeMap(&tmp->metadata);
304      if(tmp->metadata!=NULL)
305        free(tmp->metadata);
306      freeElements(&tmp->inputs);
307      if(tmp->inputs!=NULL)
308        free(tmp->inputs);
309      freeElements(&tmp->outputs);
310      if(tmp->outputs!=NULL)
311        free(tmp->outputs);
312    }
313  }
314
315  static void addToMap(map* m,const char* n,const char* v){
316    if(hasKey(m,n)==false){
317      map* _cursor=m;
318      while(_cursor->next!=NULL)
319        _cursor=_cursor->next;
320      _cursor->next=createMap(n,v);
321    }
322    else{
323      map *tmp=getMap(m,n);
324      if(tmp->value!=NULL)
325        free(tmp->value);
326      tmp->value=strdup(v);
327    }
328  }
329
330  static void addMapToMap(map** mo,map* mi){
331    map* tmp=mi;
332    map* _cursor=*mo;
333    if(tmp==NULL){
334      if(_cursor!=NULL){
335        while(_cursor!=NULL)
336          _cursor=_cursor->next;
337        _cursor=NULL;
338      }else
339        *mo=NULL;
340    }
341    while(tmp!=NULL){
342      if(_cursor==NULL){
343        if(*mo==NULL)
344          *mo=createMap(tmp->name,tmp->value);
345        else
346          addToMap(*mo,tmp->name,tmp->value);
347      }
348      else{
349#ifdef DEBUG
350        fprintf(stderr,"_CURSOR\n");
351        dumpMap(_cursor);
352#endif
353        while(_cursor!=NULL)
354          _cursor=_cursor->next;
355        _cursor=createMap(tmp->name,tmp->value);
356        _cursor->next=NULL;
357      }
358      tmp=tmp->next;
359#ifdef DEBUG
360      fprintf(stderr,"MO\n");
361      dumpMap(*mo);
362#endif
363    }
364  }
365
366  static void addMapToIoType(iotype** io,map* mi){
367    iotype* tmp=*io;
368    while(tmp->next!=NULL){
369      tmp=tmp->next;
370    }
371    tmp->next=(iotype*)malloc(IOTYPE_SIZE);
372    tmp->next->content=NULL;
373    addMapToMap(&tmp->next->content,mi);
374    tmp->next->next=NULL;
375  }
376
377  static bool contains(map* m,map* i){
378    while(i!=NULL){     
379      if(strcasecmp(i->name,"value")!=0 &&
380         strcasecmp(i->name,"xlink:href")!=0){
381        map *tmp;
382        if(hasKey(m,i->name) && (tmp=getMap(m,i->name))!=NULL && 
383           strcasecmp(i->value,tmp->value)!=0)
384          return false;
385      }
386      i=i->next;
387    }
388    return true;
389  }
390
391  static iotype* getIoTypeFromElement(elements* e,char *name, map* values){
392    elements* cursor=e;
393    while(cursor!=NULL){
394      if(strcasecmp(cursor->name,name)==0){
395        if(contains(cursor->defaults->content,values)==true)
396          return cursor->defaults;
397        else{
398          iotype* tmp=cursor->supported;
399          while(tmp!=NULL){
400            if(contains(tmp->content,values)==true)
401              return tmp;           
402            tmp=tmp->next;
403          }
404        }
405      }
406      cursor=cursor->next;
407    }
408    return NULL;
409  }
410
411  static maps* dupMaps(maps** mo){
412    maps* _cursor=*mo;
413    maps* res=NULL;
414    if(_cursor!=NULL){
415      res=(maps*)malloc(MAPS_SIZE);
416      res->name=strdup(_cursor->name);
417      res->content=NULL;
418      res->next=NULL;
419      map* mc=_cursor->content;
420      map* tmp=getMap(mc,"size");
421      char* tmpSized=NULL;
422      if(tmp!=NULL){
423        map* tmpV=getMap(mc,"value");
424        tmpSized=(char*)malloc((atoi(tmp->value)+1)*sizeof(char));
425        memmove(tmpSized,tmpV->value,atoi(tmp->value)*sizeof(char));
426      }
427      if(mc!=NULL){
428        addMapToMap(&res->content,mc);
429      }
430      if(tmp!=NULL){
431        map* tmpV=getMap(res->content,"value");
432        free(tmpV->value);
433        tmpV->value=(char*)malloc(atoi(tmp->value)*sizeof(char));
434        memmove(tmpV->value,tmpSized,atoi(tmp->value)*sizeof(char));
435        free(tmpSized);
436      }
437      res->next=dupMaps(&_cursor->next);
438    }
439    return res;
440  }
441
442  static void addMapsToMaps(maps** mo,maps* mi){
443    maps* tmp=mi;
444    maps* _cursor=*mo;
445    while(tmp!=NULL){
446      if(_cursor==NULL){
447        *mo=dupMaps(&mi);
448        (*mo)->next=NULL;
449      }
450      else{
451        while(_cursor->next!=NULL)
452          _cursor=_cursor->next;
453        _cursor->next=dupMaps(&tmp);
454      }
455      tmp=tmp->next;
456    }
457  }
458
459
460  static void* setMapInMaps(maps* m,char* key,char* subkey,char *value){
461    maps* _tmpm=getMaps(m,key);
462    if(_tmpm!=NULL){
463      map* _ztmpm=getMap(_tmpm->content,subkey);
464      if(_ztmpm!=NULL){
465        free(_ztmpm->value);
466        _ztmpm->value=strdup(value);
467      }else{
468        addToMap(_tmpm->content,subkey,value);
469      }
470    }
471  }
472
473
474  static void dumpElements(elements* e){
475    elements* tmp=e;
476    while(tmp!=NULL){
477      fprintf(stderr,"ELEMENT [%s]\n",tmp->name);
478      fprintf(stderr," > CONTENT [%s]\n",tmp->name);
479      dumpMap(tmp->content);
480      fprintf(stderr," > METADATA [%s]\n",tmp->name);
481      dumpMap(tmp->metadata);
482      fprintf(stderr," > FORMAT [%s]\n",tmp->format);
483      iotype* tmpio=tmp->defaults;
484      int ioc=0;
485      while(tmpio!=NULL){
486        fprintf(stderr," > DEFAULTS [%s] (%i)\n",tmp->name,ioc);
487        dumpMap(tmpio->content);
488        tmpio=tmpio->next;
489        ioc++;
490      }
491      tmpio=tmp->supported;
492      ioc=0;
493      while(tmpio!=NULL){
494        fprintf(stderr," > SUPPORTED [%s] (%i)\n",tmp->name,ioc);
495        dumpMap(tmpio->content);
496        tmpio=tmpio->next;
497        ioc++;
498      }
499      fprintf(stderr,"------------------\n");
500      tmp=tmp->next;
501    }
502  }
503
504  static elements* dupElements(elements* e){
505    elements* cursor=e;
506    elements* tmp=NULL;
507    if(cursor!=NULL){
508#ifdef DEBUG
509      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
510      dumpElements(e);
511      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
512#endif
513      tmp=(elements*)malloc(ELEMENTS_SIZE);
514      tmp->name=strdup(e->name);
515      tmp->content=NULL;
516      addMapToMap(&tmp->content,e->content);
517      tmp->metadata=NULL;
518      addMapToMap(&tmp->metadata,e->metadata);
519      tmp->format=strdup(e->format);
520      if(e->defaults!=NULL){
521        tmp->defaults=(iotype*)malloc(IOTYPE_SIZE);
522        tmp->defaults->content=NULL;
523        addMapToMap(&tmp->defaults->content,e->defaults->content);
524        tmp->defaults->next=NULL;
525#ifdef DEBUG
526        fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
527        dumpMap(tmp->defaults->content);
528#endif
529      }else
530        tmp->defaults=NULL;
531      if(e->supported!=NULL){
532        tmp->supported=(iotype*)malloc(IOTYPE_SIZE);
533        tmp->supported->content=NULL;
534        addMapToMap(&tmp->supported->content,e->supported->content);
535        tmp->supported->next=NULL;
536        iotype *tmp2=e->supported->next;
537        while(tmp2!=NULL){
538          addMapToIoType(&tmp->supported,tmp2->content);
539#ifdef DEBUG
540          fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
541          dumpMap(tmp->defaults->content);
542#endif
543          tmp2=tmp2->next;
544        }
545      }
546      else
547        tmp->supported=NULL;
548      tmp->next=dupElements(cursor->next);
549    }
550    return tmp;
551  }
552
553  static void addToElements(elements** m,elements* e){
554    elements* _cursor=*m;
555    elements* tmp=e;
556    if(_cursor==NULL){
557      *m=dupElements(tmp);
558    }else{
559      addToElements(&(*m)->next,tmp);
560    }
561  }
562
563  static void dumpService(service* s){
564    fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name);
565    if(s->content!=NULL){
566      fprintf(stderr,"CONTENT MAP\n");
567      dumpMap(s->content);
568      fprintf(stderr,"CONTENT METADATA\n");
569      dumpMap(s->metadata);
570    }
571    if(s->inputs!=NULL){
572      fprintf(stderr,"INPUT ELEMENTS [%s]\n------------------\n",s->name);
573      dumpElements(s->inputs);
574    }
575    if(s->outputs!=NULL){
576      fprintf(stderr,"OUTPUT ELEMENTS [%s]\n------------------\n",s->name);
577      dumpElements(s->outputs);
578    }
579    fprintf(stderr,"++++++++++++++++++\n");
580  }
581
582  static void mapsToCharXXX(maps* m,char*** c){
583    maps* tm=m;
584    int i=0;
585    int j=0;
586    char tmp[10][30][1024];
587    memset(tmp,0,1024*10*10);
588    while(tm!=NULL){
589      if(i>=10)
590        break;
591      strcpy(tmp[i][j],"name");
592      j++;
593      strcpy(tmp[i][j],tm->name);
594      j++;
595      map* tc=tm->content;
596      while(tc!=NULL){
597        if(j>=30)
598          break;
599        strcpy(tmp[i][j],tc->name);
600        j++;
601        strcpy(tmp[i][j],tc->value);
602        j++;
603        tc=tc->next;
604      }
605      tm=tm->next;
606      j=0;
607      i++;
608    }
609    memcpy(c,tmp,10*10*1024);
610  }
611
612  static void charxxxToMaps(char*** c,maps**m){
613    maps* trorf=*m;
614    int i,j;
615    char tmp[10][30][1024];
616    memcpy(tmp,c,10*30*1024);
617    for(i=0;i<10;i++){
618      if(strlen(tmp[i][1])==0)
619        break;
620      trorf->name=tmp[i][1];
621      trorf->content=NULL;
622      trorf->next=NULL;
623      for(j=2;j<29;j+=2){
624        if(strlen(tmp[i][j+1])==0)
625          break;
626        if(trorf->content==NULL)
627          trorf->content=createMap(tmp[i][j],tmp[i][j+1]);
628        else
629          addToMap(trorf->content,tmp[i][j],tmp[i][j+1]);
630      }
631      trorf=trorf->next;
632    }
633    m=&trorf;
634  }
635
636#ifdef __cplusplus
637}
638#endif
639
640#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