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

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

Stop blowing the stack in ulinet. Add API function addToMapWithSize for assigning binary data to map. Add support for binary inputs/outputs for the PHP language.

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