source: trunk/zoo-project/zoo-kernel/service.h @ 480

Last change on this file since 480 was 474, checked in by djay, 10 years ago

Fix issue with Python support: remove unecessary Py_DECREF calls.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr
File size: 24.2 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2012 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 WIN32
31#ifndef USE_MS
32#define strncasecmp _strnicmp
33#define strcasecmp _stricmp
34#endif
35#ifndef snprintf
36#define snprintf sprintf_s
37#endif
38#define zStrdup _strdup
39#define zMkdir _mkdir
40#define zOpen _open
41#define zWrite _write
42#else
43#define zStrdup strdup
44#define zMkdir mkdir
45#define zOpen open
46#define zWrite write
47#endif
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53#ifdef WIN32
54#ifdef USE_MS
55#include <mapserver.h>
56#endif
57#endif
58#include <stdlib.h>
59#include <ctype.h>
60#include <stdio.h>
61#include <string.h>
62#ifndef WIN32
63#define bool int
64#define true 1
65#define false -1
66#else
67  //#include <stdbool.h>
68#endif
69
70#define SERVICE_ACCEPTED 0
71#define SERVICE_STARTED 1
72#define SERVICE_PAUSED 2
73#define SERVICE_SUCCEEDED 3
74#define SERVICE_FAILED 4
75
76#define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*2)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+sizeof(elements*))
77#define MAP_SIZE (2*sizeof(char*))+sizeof(NULL)
78#define IOTYPE_SIZE MAP_SIZE+sizeof(NULL)
79#define MAPS_SIZE (2*sizeof(char*))+sizeof(map*)+MAP_SIZE
80#define SERVICE_SIZE (ELEMENTS_SIZE*2)+(MAP_SIZE*2)+sizeof(char*)
81
82#define SHMSZ     27
83
84#include "version.h"
85
86#ifdef DEBUG_STACK
87  void debugStack(const char* file,const int line){
88    int stack;
89    fprintf(stderr,"stack %p (%s: %d) \n",&stack,file,line);
90  }
91#endif
92
93  /**
94   * \struct map
95   * \brief KVP linked list
96   *
97   * Deal with WPS KVP (name,value).
98   * A map is defined as:
99   *  - name : a key,
100   *  - value: a value,
101   *  - next : a pointer to the next map if any.
102   */
103  typedef struct map{
104    char* name;
105    char* value;
106    struct map* next;
107  } map;
108
109#ifdef WIN32
110#define NULLMAP ((map*) 0)
111#else
112#define NULLMAP NULL
113#endif
114
115  /**
116   * \struct maps
117   * \brief linked list of map pointer
118   *
119   * Small object to store WPS KVP set.
120   * Maps is defined as:
121   *  - a name,
122   *  - a content map,
123   *  - a pointer to the next maps if any.
124   */
125  typedef struct maps{
126    char* name;         
127    struct map* content; 
128    struct maps* next;   
129  } maps;
130
131  /**
132   * \brief Dump a map on stderr
133   */
134  static void _dumpMap(map* t){
135    if(t!=NULL){
136      fprintf(stderr,"%s: %s\n",t->name,t->value);
137      fflush(stderr);
138        }else{
139      fprintf(stderr,"NULL\n");
140      fflush(stderr);
141    }
142  }
143
144  static void dumpMap(map* t){
145    map* tmp=t;
146    while(tmp!=NULL){
147      _dumpMap(tmp);
148      tmp=tmp->next;
149    }
150  }
151
152  static void dumpMapToFile(map* t,FILE* file){
153    map* tmp=t;
154    while(tmp!=NULL){
155#ifdef DEBUG
156      fprintf(stderr,"%s = %s\n",tmp->name,tmp->value);
157#endif
158      fprintf(file,"%s = %s\n",tmp->name,tmp->value);
159      tmp=tmp->next;
160    }
161  }
162
163  static void dumpMaps(maps* m){
164    maps* tmp=m;
165    while(tmp!=NULL){
166      fprintf(stderr,"MAP => [%s] \n",tmp->name);
167      dumpMap(tmp->content);
168      tmp=tmp->next;
169    }
170  }
171
172  static void dumpMapsToFile(maps* m,char* file_path){
173    FILE* file=fopen(file_path,"w");
174    maps* tmp=m;
175    if(tmp!=NULL){
176      fprintf(file,"[%s]\n",tmp->name);
177      dumpMapToFile(tmp->content,file);
178      fflush(file);
179    }
180    fclose(file);
181  }
182
183  static map* createMap(const char* name,const char* value){
184    map* tmp=(map *)malloc(MAP_SIZE);
185    tmp->name=zStrdup(name);
186    tmp->value=zStrdup(value);
187    tmp->next=NULL;
188    return tmp;
189  }
190
191  static int count(map* m){
192    map* tmp=m;
193    int c=0;
194    while(tmp!=NULL){
195      c++;
196      tmp=tmp->next;
197    }
198    return c;
199  }
200   
201  static bool hasKey(map* m,const char *key){
202    map* tmp=m;
203    while(tmp!=NULL){
204      if(strcasecmp(tmp->name,key)==0)
205        return true;
206      tmp=tmp->next;
207    }
208#ifdef DEBUG_MAP
209    fprintf(stderr,"NOT FOUND \n");
210#endif
211    return false;
212  }
213
214  static maps* getMaps(maps* m,const char *key){
215    maps* tmp=m;
216    while(tmp!=NULL){
217      if(strcasecmp(tmp->name,key)==0){
218        return tmp;
219      }
220      tmp=tmp->next;
221    }
222    return NULL;
223  }
224
225  static map* getMap(map* m,const char *key){
226    map* tmp=m;
227    while(tmp!=NULL){
228      if(strcasecmp(tmp->name,key)==0){
229        return tmp;
230      }
231      tmp=tmp->next;
232    }
233    return NULL;
234  }
235
236
237  static map* getLastMap(map* m){
238    map* tmp=m;
239    while(tmp!=NULL){
240      if(tmp->next==NULL){
241        return tmp;
242      }
243      tmp=tmp->next;
244    }
245    return NULL;
246  }
247
248  static map* getMapFromMaps(maps* m,const char* key,const char* subkey){
249    maps* _tmpm=getMaps(m,key);
250    if(_tmpm!=NULL){
251      map* _ztmpm=getMap(_tmpm->content,subkey);
252      return _ztmpm;
253    }
254    else return NULL;
255  }
256
257
258  static void freeMap(map** mo){
259    map* _cursor=*mo;
260    if(_cursor!=NULL){
261#ifdef DEBUG
262      fprintf(stderr,"freeMap\n");
263#endif
264      free(_cursor->name);
265      free(_cursor->value);
266      if(_cursor->next!=NULL){
267        freeMap(&_cursor->next);
268        free(_cursor->next);
269      }
270    }
271  }
272
273  static void freeMaps(maps** mo){
274    maps* _cursor=*mo;
275    if(_cursor && _cursor!=NULL){
276#ifdef DEBUG
277      fprintf(stderr,"freeMaps\n");
278#endif
279      free(_cursor->name);
280      if(_cursor->content!=NULL){
281        freeMap(&_cursor->content);
282        free(_cursor->content);
283      }
284      if(_cursor->next!=NULL){
285        freeMaps(&_cursor->next);
286        free(_cursor->next);
287      }
288    }
289  }
290
291  /**
292   * \brief Not named linked list
293   *
294   * Used to store informations about formats, such as mimeType, encoding ...
295   *
296   * An iotype is defined as :
297   *  - a content map,
298   *  - a pointer to the next iotype if any.
299   */
300  typedef struct iotype{
301    struct map* content;
302    struct iotype* next;
303  } iotype;
304
305  /**
306   * \brief Metadata information about input or output.
307   *
308   * The elements are used to store metadata informations defined in the ZCFG.
309   *
310   * An elements is defined as :
311   *  - a name,
312   *  - a content map,
313   *  - a metadata map,
314   *  - a format (possible values are LiteralData, ComplexData or
315   * BoundingBoxData),
316   *  - a default iotype,
317   *  - a pointer to the next elements id any.
318   */
319  typedef struct elements{
320    char* name;
321    struct map* content;
322    struct map* metadata;
323    char* format;
324    struct iotype* defaults;
325    struct iotype* supported;
326    struct elements* next;
327  } elements;
328
329  typedef struct service{
330    char* name;
331    struct map* content;
332    struct map* metadata;
333    struct elements* inputs;
334    struct elements* outputs; 
335  } service;
336
337  typedef struct services{
338    struct service* content; 
339    struct services* next; 
340  } services;
341
342  static bool hasElement(elements* e,const char* key){
343    elements* tmp=e;
344    while(tmp!=NULL){
345      if(strcasecmp(key,tmp->name)==0)
346        return true;
347      tmp=tmp->next;
348    }
349    return false;
350  }
351
352  static elements* getElements(elements* m,char *key){
353    elements* tmp=m;
354    while(tmp!=NULL){
355      if(strcasecmp(tmp->name,key)==0)
356        return tmp;
357      tmp=tmp->next;
358    }
359    return NULL;
360  }
361
362
363  static void freeIOType(iotype** i){
364    iotype* _cursor=*i;
365    if(_cursor!=NULL){
366      if(_cursor->next!=NULL){
367        freeIOType(&_cursor->next);
368        free(_cursor->next);
369      }
370      freeMap(&_cursor->content);
371      free(_cursor->content);
372    }
373  }
374
375  static void freeElements(elements** e){
376    elements* tmp=*e;
377    if(tmp!=NULL){
378      if(tmp->name!=NULL)
379        free(tmp->name);
380      freeMap(&tmp->content);
381      if(tmp->content!=NULL)
382        free(tmp->content);
383      freeMap(&tmp->metadata);
384      if(tmp->metadata!=NULL)
385        free(tmp->metadata);
386      if(tmp->format!=NULL)
387        free(tmp->format);
388      freeIOType(&tmp->defaults);
389      if(tmp->defaults!=NULL)
390        free(tmp->defaults);
391      freeIOType(&tmp->supported);
392      if(tmp->supported!=NULL){
393        free(tmp->supported);
394      }
395      freeElements(&tmp->next);
396      if(tmp->next!=NULL)
397        free(tmp->next);
398    }
399  }
400
401  static void freeService(service** s){
402    service* tmp=*s;
403    if(tmp!=NULL){
404      if(tmp->name!=NULL)
405        free(tmp->name);
406      freeMap(&tmp->content);
407      if(tmp->content!=NULL)
408        free(tmp->content);
409      freeMap(&tmp->metadata);
410      if(tmp->metadata!=NULL)
411        free(tmp->metadata);
412      freeElements(&tmp->inputs);
413      if(tmp->inputs!=NULL)
414        free(tmp->inputs);
415      freeElements(&tmp->outputs);
416      if(tmp->outputs!=NULL)
417        free(tmp->outputs);
418    }
419  }
420
421  static void addToMap(map* m,const char* n,const char* v){
422    if(hasKey(m,n)==false){
423      map* _cursor=m;
424      if(_cursor->next!=NULL){
425        addToMap(_cursor->next,n,v);
426      }else
427        _cursor->next=createMap(n,v);
428    }
429    else{
430      map *tmp=getMap(m,n);
431      if(tmp->value!=NULL)
432        free(tmp->value);
433      tmp->value=zStrdup(v);
434    }
435  }
436
437  static void addMapToMap(map** mo,map* mi){
438    map* tmp=mi;
439    map* _cursor=*mo;
440    while(tmp!=NULL){
441      if(_cursor==NULL){
442        *mo=createMap(tmp->name,tmp->value);
443        (*mo)->next=NULL;
444      }
445      else{
446#ifdef DEBUG
447        fprintf(stderr,"_CURSOR\n");
448        dumpMap(_cursor);
449#endif
450        while(_cursor->next!=NULL)
451          _cursor=_cursor->next;
452        map* tmp1=getMap(*mo,tmp->name);
453        if(tmp1==NULL){
454          _cursor->next=createMap(tmp->name,tmp->value);
455        }
456        else{
457          addToMap(*mo,tmp->name,tmp->value);
458        }
459      }
460      _cursor=*mo;
461      tmp=tmp->next;
462#ifdef DEBUG
463      fprintf(stderr,"MO\n");
464      dumpMap(*mo);
465#endif
466    }
467  }
468
469  static void addMapToIoType(iotype** io,map* mi){
470    iotype* tmp=*io;
471    while(tmp->next!=NULL){
472      tmp=tmp->next;
473    }
474    tmp->next=(iotype*)malloc(IOTYPE_SIZE);
475    tmp->next->content=NULL;
476    addMapToMap(&tmp->next->content,mi);
477    tmp->next->next=NULL;
478  }
479
480  static map* getMapOrFill(map** m,const char *key,char* value){
481    map* tmp=*m;
482    map* tmpMap=getMap(tmp,key);
483    if(tmpMap==NULL){
484      if(tmp!=NULL)
485        addToMap(*m,key,value);
486      else
487        (*m)=createMap(key,value);
488      tmpMap=getMap(*m,key);
489    }
490    return tmpMap;
491  }
492
493  static bool contains(map* m,map* i){
494    while(i!=NULL){     
495      if(strcasecmp(i->name,"value")!=0 &&
496         strcasecmp(i->name,"xlink:href")!=0 &&
497         strcasecmp(i->name,"useMapServer")!=0 &&
498         strcasecmp(i->name,"asReference")!=0){
499        map *tmp;
500        if(hasKey(m,i->name) && (tmp=getMap(m,i->name))!=NULL && 
501           strcasecmp(i->value,tmp->value)!=0)
502          return false;
503      }
504      i=i->next;
505    }
506    return true;
507  }
508
509  static iotype* getIoTypeFromElement(elements* e,char *name, map* values){
510    elements* cursor=e;
511    while(cursor!=NULL){
512      if(strcasecmp(cursor->name,name)==0){
513        if(contains(cursor->defaults->content,values)==true)
514          return cursor->defaults;
515        else{
516          iotype* tmp=cursor->supported;
517          while(tmp!=NULL){
518            if(contains(tmp->content,values)==true)
519              return tmp;           
520            tmp=tmp->next;
521          }
522        }
523      }
524      cursor=cursor->next;
525    }
526    return NULL;
527  }
528
529  static maps* dupMaps(maps** mo){
530    maps* _cursor=*mo;
531    maps* res=NULL;
532    if(_cursor!=NULL){
533      res=(maps*)malloc(MAPS_SIZE);
534      res->name=zStrdup(_cursor->name);
535      res->content=NULL;
536      res->next=NULL;
537      map* mc=_cursor->content;
538      map* tmp=getMap(mc,"size");
539      char* tmpSized=NULL;
540      if(tmp!=NULL){
541        map* tmpV=getMap(mc,"value");
542        tmpSized=(char*)malloc((atoi(tmp->value)+1)*sizeof(char));
543        memmove(tmpSized,tmpV->value,atoi(tmp->value)*sizeof(char));
544      }
545      if(mc!=NULL){
546        addMapToMap(&res->content,mc);
547      }
548      if(tmp!=NULL){
549        map* tmpV=getMap(res->content,"value");
550        free(tmpV->value);
551        tmpV->value=(char*)malloc((atoi(tmp->value)+1)*sizeof(char));
552        memmove(tmpV->value,tmpSized,atoi(tmp->value)*sizeof(char));
553        tmpV->value[atoi(tmp->value)]=0;
554        free(tmpSized);
555      }
556      res->next=dupMaps(&_cursor->next);
557    }
558    return res;
559  }
560
561  static void addMapsToMaps(maps** mo,maps* mi){
562    maps* tmp=mi;
563    maps* _cursor=*mo;
564    while(tmp!=NULL){
565      if(_cursor==NULL){
566        *mo=dupMaps(&mi);
567      }
568      else{
569        while(_cursor->next!=NULL)
570          _cursor=_cursor->next;
571        maps* tmp1=getMaps(*mo,tmp->name);
572        if(tmp1==NULL)
573          _cursor->next=dupMaps(&tmp);
574        else
575          addMapToMap(&tmp1->content,tmp->content);
576        _cursor=*mo;
577      }
578      tmp=tmp->next;
579    }
580  }
581
582  static map* getMapArray(map* m,char* key,int index){
583    char tmp[1024];
584    if(index>0)
585      sprintf(tmp,"%s_%d",key,index);
586    else
587      sprintf(tmp,"%s",key);
588#ifdef DEBUG
589    fprintf(stderr,"** KEY %s\n",tmp);
590#endif
591    map* tmpMap=getMap(m,tmp);
592#ifdef DEBUG
593    if(tmpMap!=NULL)
594      dumpMap(tmpMap);
595#endif
596    return tmpMap;
597  }
598
599
600  static void setMapArray(map* m,char* key,int index,char* value){
601    char tmp[1024];
602    if(index>0)
603      sprintf(tmp,"%s_%d",key,index);
604    else
605      sprintf(tmp,"%s",key);
606    map* tmpSize=getMapArray(m,(char*)"size",index);
607    if(tmpSize!=NULL && strncasecmp(key,"value",5)==0){
608#ifdef DEBUG
609      fprintf(stderr,"%s\n",tmpSize->value);
610#endif
611      map* ptr=getMapOrFill(&m,tmp,(char *)"");
612      free(ptr->value);
613      ptr->value=(char*)malloc((atoi(tmpSize->value)+1)*sizeof(char));
614      memcpy(ptr->value,value,atoi(tmpSize->value)); 
615    }
616    else
617      addToMap(m,tmp,value);
618  }
619
620  static map* getMapType(map* mt){
621    map* tmap=getMap(mt,(char *)"mimeType");
622    if(tmap==NULL){
623      tmap=getMap(mt,"dataType");
624      if(tmap==NULL){
625        tmap=getMap(mt,"CRS");
626      }
627    }
628#ifdef DEBUG
629        dumpMap(tmap);
630#endif
631    return tmap;
632  }
633
634  static int addMapsArrayToMaps(maps** mo,maps* mi,char* typ){
635    maps* tmp=mi;   
636    maps* _cursor=getMaps(*mo,tmp->name);
637
638    if(_cursor==NULL)
639      return -1;
640
641    map* tmpLength=getMap(_cursor->content,"length");
642    char tmpLen[10];
643    int len=1;
644    if(tmpLength!=NULL){
645      len=atoi(tmpLength->value);
646    }
647
648    char *tmpV[8]={
649      (char*)"size",
650      (char*)"value",
651      (char*)"uom",
652      (char*)"Reference",
653      (char*)"xlink:href",
654      typ,
655      (char*)"schema",
656      (char*)"encoding"
657    };
658    sprintf(tmpLen,"%d",len+1);
659    addToMap(_cursor->content,"length",tmpLen);
660    int i=0;
661    map* tmpSizeI=getMap(tmp->content,tmpV[i]);
662    for(i=0;i<8;i++){
663      map* tmpVI=getMap(tmp->content,tmpV[i]);
664      if(tmpVI!=NULL){
665#ifdef DEBUG
666        fprintf(stderr,"%s = %s\n",tmpV[i],tmpVI->value);
667#endif
668        if(i<5)
669          setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
670        else
671          if(strncasecmp(tmpV[5],"mimeType",8)==0)
672            setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
673      }
674    }
675   
676    addToMap(_cursor->content,"isArray","true");
677    return 0;
678  }
679
680  static void setMapInMaps(maps* m,const char* key,const char* subkey,const char *value){
681    maps* _tmpm=getMaps(m,key);
682    if(_tmpm!=NULL){
683      map* _ztmpm=getMap(_tmpm->content,subkey);
684      if(_ztmpm!=NULL){
685        if(_ztmpm->value!=NULL)
686          free(_ztmpm->value);
687        _ztmpm->value=zStrdup(value);
688      }else{
689        maps *tmp=(maps*)malloc(MAPS_SIZE);
690        tmp->name=zStrdup(key);
691        tmp->content=createMap(subkey,value);
692        tmp->next=NULL;
693        addMapsToMaps(&_tmpm,tmp);
694        freeMaps(&tmp);
695        free(tmp);
696      }
697    }else{
698      maps *tmp=(maps*)malloc(MAPS_SIZE);
699      tmp->name=zStrdup(key);
700      tmp->content=createMap(subkey,value);
701      tmp->next=NULL;
702      addMapsToMaps(&m,tmp);
703      freeMaps(&tmp);
704      free(tmp);
705    }
706  }
707
708
709  static void dumpElements(elements* e){
710    elements* tmp=e;
711    while(tmp!=NULL){
712      fprintf(stderr,"ELEMENT [%s]\n",tmp->name);
713      fprintf(stderr," > CONTENT [%s]\n",tmp->name);
714      dumpMap(tmp->content);
715      fprintf(stderr," > METADATA [%s]\n",tmp->name);
716      dumpMap(tmp->metadata);
717      fprintf(stderr," > FORMAT [%s]\n",tmp->format);
718      iotype* tmpio=tmp->defaults;
719      int ioc=0;
720      while(tmpio!=NULL){
721        fprintf(stderr," > DEFAULTS [%s] (%i)\n",tmp->name,ioc);
722        dumpMap(tmpio->content);
723        tmpio=tmpio->next;
724        ioc++;
725      }
726      tmpio=tmp->supported;
727      ioc=0;
728      while(tmpio!=NULL){
729        fprintf(stderr," > SUPPORTED [%s] (%i)\n",tmp->name,ioc);
730        dumpMap(tmpio->content);
731        tmpio=tmpio->next;
732        ioc++;
733      }
734      fprintf(stderr,"------------------\n");
735      tmp=tmp->next;
736    }
737  }
738
739  static void dumpElementsAsYAML(elements* e){
740    elements* tmp=e;
741    int i;
742    while(tmp!=NULL){
743      for(i=0;i<2;i++)
744        fprintf(stderr," ");
745      fprintf(stderr,"%s:\n",tmp->name);
746      map* mcurs=tmp->content;
747      while(mcurs!=NULL){
748        for(i=0;i<4;i++)
749          fprintf(stderr," ");
750        _dumpMap(mcurs);
751        mcurs=mcurs->next;
752      }
753      mcurs=tmp->metadata;
754      if(mcurs!=NULL){
755        for(i=0;i<4;i++)
756          fprintf(stderr," ");
757        fprintf(stderr,"MetaData:\n");
758        while(mcurs!=NULL){
759          for(i=0;i<6;i++)
760            fprintf(stderr," ");
761          _dumpMap(mcurs);
762          mcurs=mcurs->next;
763        }
764      }
765      for(i=0;i<4;i++)
766        fprintf(stderr," ");
767      fprintf(stderr,"%s:\n",tmp->format);
768      iotype* tmpio=tmp->defaults;
769      int ioc=0;
770      while(tmpio!=NULL){
771        for(i=0;i<6;i++)
772          fprintf(stderr," ");
773        fprintf(stderr,"default:\n");
774        mcurs=tmpio->content;
775        while(mcurs!=NULL){
776          for(i=0;i<8;i++)
777            fprintf(stderr," ");
778          if(strcasecmp(mcurs->name,"range")==0){
779            fprintf(stderr,"range: \"%s\"\n",mcurs->value);
780          }else
781            _dumpMap(mcurs);
782          mcurs=mcurs->next;
783        }
784        tmpio=tmpio->next;
785        ioc++;
786      }
787      tmpio=tmp->supported;
788      ioc=0;
789      while(tmpio!=NULL){
790        for(i=0;i<6;i++)
791          fprintf(stderr," ");
792        fprintf(stderr,"supported:\n");
793        mcurs=tmpio->content;
794        while(mcurs!=NULL){
795          for(i=0;i<8;i++)
796            fprintf(stderr," ");
797          if(strcasecmp(mcurs->name,"range")==0){
798            fprintf(stderr,"range: \"%s\"\n",mcurs->value);
799          }else
800            _dumpMap(mcurs);
801          mcurs=mcurs->next;
802        }
803        tmpio=tmpio->next;
804        ioc++;
805      }
806      tmp=tmp->next;
807    }
808  }
809
810
811  static elements* dupElements(elements* e){
812    elements* cursor=e;
813    elements* tmp=NULL;
814    if(cursor!=NULL){
815#ifdef DEBUG
816      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
817      dumpElements(e);
818      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
819#endif
820      tmp=(elements*)malloc(ELEMENTS_SIZE);
821      tmp->name=zStrdup(e->name);
822      tmp->content=NULL;
823      addMapToMap(&tmp->content,e->content);
824      tmp->metadata=NULL;
825      addMapToMap(&tmp->metadata,e->metadata);
826      tmp->format=zStrdup(e->format);
827      if(e->defaults!=NULL){
828        tmp->defaults=(iotype*)malloc(IOTYPE_SIZE);
829        tmp->defaults->content=NULL;
830        addMapToMap(&tmp->defaults->content,e->defaults->content);
831        tmp->defaults->next=NULL;
832#ifdef DEBUG
833        fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
834        dumpMap(tmp->defaults->content);
835#endif
836      }else
837        tmp->defaults=NULL;
838      if(e->supported!=NULL){
839        tmp->supported=(iotype*)malloc(IOTYPE_SIZE);
840        tmp->supported->content=NULL;
841        addMapToMap(&tmp->supported->content,e->supported->content);
842        tmp->supported->next=NULL;
843        iotype *tmp2=e->supported->next;
844        while(tmp2!=NULL){
845          addMapToIoType(&tmp->supported,tmp2->content);
846#ifdef DEBUG
847          fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
848          dumpMap(tmp->defaults->content);
849#endif
850          tmp2=tmp2->next;
851        }
852      }
853      else
854        tmp->supported=NULL;
855      tmp->next=dupElements(cursor->next);
856    }
857    return tmp;
858  }
859
860  static void addToElements(elements** m,elements* e){
861    elements* tmp=e;
862    if(*m==NULL){
863      *m=dupElements(tmp);
864    }else{
865      addToElements(&(*m)->next,tmp);
866    }
867  }
868
869  static void dumpService(service* s){
870    fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name);
871    if(s->content!=NULL){
872      fprintf(stderr,"CONTENT MAP\n");
873      dumpMap(s->content);
874      fprintf(stderr,"CONTENT METADATA\n");
875      dumpMap(s->metadata);
876    }
877    if(s->inputs!=NULL){
878      fprintf(stderr,"INPUT ELEMENTS [%s]\n------------------\n",s->name);
879      dumpElements(s->inputs);
880    }
881    if(s->outputs!=NULL){
882      fprintf(stderr,"OUTPUT ELEMENTS [%s]\n------------------\n",s->name);
883      dumpElements(s->outputs);
884    }
885    fprintf(stderr,"++++++++++++++++++\n");
886  }
887
888  static void dumpServiceAsYAML(service* s){
889    int level=0;
890    int i;
891    fprintf(stderr,"# %s\n\n",s->name);
892    if(s->content!=NULL){
893      map* mcurs=s->content;
894      dumpMap(mcurs);
895      mcurs=s->metadata;
896      if(mcurs!=NULL){
897        fprintf(stderr,"MetaData:\n");
898        while(mcurs!=NULL){
899          for(i=0;i<2;i++)
900            fprintf(stderr," ");
901          _dumpMap(mcurs);
902          mcurs=mcurs->next;
903        }
904      }
905    }
906    if(s->inputs!=NULL){
907      fprintf(stderr,"\ninputs:\n",s->name);
908      dumpElementsAsYAML(s->inputs);
909    }
910    if(s->outputs!=NULL){
911      fprintf(stderr,"\noutputs:\n",s->name);
912      dumpElementsAsYAML(s->outputs);
913    }
914  }
915
916  static void mapsToCharXXX(maps* m,char*** c){
917    maps* tm=m;
918    int i=0;
919    int j=0;
920    char tmp[10][30][1024];
921    memset(tmp,0,1024*10*10);
922    while(tm!=NULL){
923      if(i>=10)
924        break;
925      strcpy(tmp[i][j],"name");
926      j++;
927      strcpy(tmp[i][j],tm->name);
928      j++;
929      map* tc=tm->content;
930      while(tc!=NULL){
931        if(j>=30)
932          break;
933        strcpy(tmp[i][j],tc->name);
934        j++;
935        strcpy(tmp[i][j],tc->value);
936        j++;
937        tc=tc->next;
938      }
939      tm=tm->next;
940      j=0;
941      i++;
942    }
943    memcpy(c,tmp,10*10*1024);
944  }
945
946  static void charxxxToMaps(char*** c,maps**m){
947    maps* trorf=*m;
948    int i,j;
949    char tmp[10][30][1024];
950    memcpy(tmp,c,10*30*1024);
951    for(i=0;i<10;i++){
952      if(strlen(tmp[i][1])==0)
953        break;
954      trorf->name=tmp[i][1];
955      trorf->content=NULL;
956      trorf->next=NULL;
957      for(j=2;j<29;j+=2){
958        if(strlen(tmp[i][j+1])==0)
959          break;
960        if(trorf->content==NULL)
961          trorf->content=createMap(tmp[i][j],tmp[i][j+1]);
962        else
963          addToMap(trorf->content,tmp[i][j],tmp[i][j+1]);
964      }
965      trorf=trorf->next;
966    }
967    m=&trorf;
968  }
969
970#ifdef WIN32
971  extern char *url_encode(char *);
972
973  static char* getMapsAsKVP(maps* m,int length,int type){
974    char *dataInputsKVP=(char*) malloc(length*sizeof(char));
975    char *dataInputsKVPi=NULL;
976    maps* curs=m;
977    int i=0;
978    while(curs!=NULL){
979      map *inRequest=getMap(curs->content,"inRequest");
980      map *hasLength=getMap(curs->content,"length");
981      if((inRequest!=NULL && strncasecmp(inRequest->value,"true",4)==0) ||
982         inRequest==NULL){
983        if(i==0)
984          if(type==0){
985            sprintf(dataInputsKVP,"%s=",curs->name);
986            if(hasLength!=NULL){
987              dataInputsKVPi=(char*)malloc((strlen(curs->name)+2)*sizeof(char));
988              sprintf(dataInputsKVPi,"%s=",curs->name);
989            }
990          }
991          else
992            sprintf(dataInputsKVP,"%s",curs->name);
993        else{
994          char *temp=zStrdup(dataInputsKVP);
995          if(type==0)
996            sprintf(dataInputsKVP,"%s;%s=",temp,curs->name);
997          else
998            sprintf(dataInputsKVP,"%s;%s",temp,curs->name);
999        }
1000        map* icurs=curs->content;
1001        if(type==0){
1002          char *temp=zStrdup(dataInputsKVP);
1003          if(getMap(curs->content,"xlink:href")!=NULL)
1004            sprintf(dataInputsKVP,"%sReference",temp);
1005          else{
1006            if(hasLength!=NULL){
1007              int j;
1008              for(j=0;j<atoi(hasLength->value);j++){
1009                map* tmp0=getMapArray(curs->content,"value",j);
1010                if(j==0)
1011                  free(temp);
1012                temp=zStrdup(dataInputsKVP);
1013                if(j==0)
1014                  sprintf(dataInputsKVP,"%s%s",temp,tmp0->value);
1015                else
1016                  sprintf(dataInputsKVP,"%s;%s%s",temp,dataInputsKVPi,tmp0->value);
1017              }
1018            }
1019            else
1020              sprintf(dataInputsKVP,"%s%s",temp,icurs->value);
1021          }
1022          free(temp);
1023        }
1024        while(icurs!=NULL){
1025          if(strncasecmp(icurs->name,"value",5)!=0 &&
1026             strncasecmp(icurs->name,"mimeType_",9)!=0 &&
1027             strncasecmp(icurs->name,"dataType_",9)!=0 &&
1028             strncasecmp(icurs->name,"size",4)!=0 &&
1029             strncasecmp(icurs->name,"length",4)!=0 &&
1030             strncasecmp(icurs->name,"isArray",7)!=0 &&
1031             strcasecmp(icurs->name,"Reference")!=0 &&
1032             strcasecmp(icurs->name,"minOccurs")!=0 &&
1033             strcasecmp(icurs->name,"maxOccurs")!=0 &&
1034             strncasecmp(icurs->name,"fmimeType",9)!=0 &&
1035             strcasecmp(icurs->name,"inRequest")!=0){
1036            char *itemp=zStrdup(dataInputsKVP);
1037            if(strcasecmp(icurs->name,"xlink:href")!=0)
1038              sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,icurs->value);
1039            else
1040              sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,url_encode(icurs->value));
1041            free(itemp);
1042          }
1043          icurs=icurs->next;
1044        }
1045      }
1046      curs=curs->next;
1047      i++;
1048    }
1049    return dataInputsKVP;
1050  }
1051#endif
1052
1053#ifdef __cplusplus
1054}
1055#endif
1056
1057#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