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

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

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

  • 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#ifndef bool
64#define bool int
65#endif
66#ifndef true
67#define true 1
68#define false -1
69#endif
70#else
71  //#include <stdbool.h>
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 addMapToMap(map** mo,map* mi){
442    map* tmp=mi;
443    map* _cursor=*mo;
444    while(tmp!=NULL){
445      if(_cursor==NULL){
446        *mo=createMap(tmp->name,tmp->value);
447        (*mo)->next=NULL;
448      }
449      else{
450#ifdef DEBUG
451        fprintf(stderr,"_CURSOR\n");
452        dumpMap(_cursor);
453#endif
454        while(_cursor->next!=NULL)
455          _cursor=_cursor->next;
456        map* tmp1=getMap(*mo,tmp->name);
457        if(tmp1==NULL){
458          _cursor->next=createMap(tmp->name,tmp->value);
459        }
460        else{
461          addToMap(*mo,tmp->name,tmp->value);
462        }
463      }
464      _cursor=*mo;
465      tmp=tmp->next;
466#ifdef DEBUG
467      fprintf(stderr,"MO\n");
468      dumpMap(*mo);
469#endif
470    }
471  }
472
473  static void addMapToIoType(iotype** io,map* mi){
474    iotype* tmp=*io;
475    while(tmp->next!=NULL){
476      tmp=tmp->next;
477    }
478    tmp->next=(iotype*)malloc(IOTYPE_SIZE);
479    tmp->next->content=NULL;
480    addMapToMap(&tmp->next->content,mi);
481    tmp->next->next=NULL;
482  }
483
484  static map* getMapOrFill(map** m,const char *key,const char* value){
485    map* tmp=*m;
486    map* tmpMap=getMap(tmp,key);
487    if(tmpMap==NULL){
488      if(tmp!=NULL){
489        addToMap((*m),key,value);
490      }
491      else
492        (*m)=createMap(key,value);
493      tmpMap=getMap(*m,key);
494    }
495    return tmpMap;
496  }
497
498  static bool contains(map* m,map* i){
499    while(i!=NULL){     
500      if(strcasecmp(i->name,"value")!=0 &&
501         strcasecmp(i->name,"xlink:href")!=0 &&
502         strcasecmp(i->name,"useMapServer")!=0 &&
503         strcasecmp(i->name,"asReference")!=0){
504        map *tmp;
505        if(hasKey(m,i->name) && (tmp=getMap(m,i->name))!=NULL && 
506           strcasecmp(i->value,tmp->value)!=0)
507          return false;
508      }
509      i=i->next;
510    }
511    return true;
512  }
513
514  static iotype* getIoTypeFromElement(elements* e,char *name, map* values){
515    elements* cursor=e;
516    while(cursor!=NULL){
517      if(strcasecmp(cursor->name,name)==0){
518        if(contains(cursor->defaults->content,values)==true)
519          return cursor->defaults;
520        else{
521          iotype* tmp=cursor->supported;
522          while(tmp!=NULL){
523            if(contains(tmp->content,values)==true)
524              return tmp;           
525            tmp=tmp->next;
526          }
527        }
528      }
529      cursor=cursor->next;
530    }
531    return NULL;
532  }
533
534  static maps* dupMaps(maps** mo){
535    maps* _cursor=*mo;
536    maps* res=NULL;
537    if(_cursor!=NULL){
538      res=(maps*)malloc(MAPS_SIZE);
539      res->name=zStrdup(_cursor->name);
540      res->content=NULL;
541      res->next=NULL;
542      map* mc=_cursor->content;
543      map* tmp=getMap(mc,"size");
544      char* tmpSized=NULL;
545      if(tmp!=NULL){
546        map* tmpV=getMap(mc,"value");
547        tmpSized=(char*)malloc((atoi(tmp->value)+1)*sizeof(char));
548        memmove(tmpSized,tmpV->value,atoi(tmp->value)*sizeof(char));
549      }
550      if(mc!=NULL){
551        addMapToMap(&res->content,mc);
552      }
553      if(tmp!=NULL){
554        map* tmpV=getMap(res->content,"value");
555        free(tmpV->value);
556        tmpV->value=(char*)malloc((atoi(tmp->value)+1)*sizeof(char));
557        memmove(tmpV->value,tmpSized,atoi(tmp->value)*sizeof(char));
558        tmpV->value[atoi(tmp->value)]=0;
559        free(tmpSized);
560      }
561      res->next=dupMaps(&_cursor->next);
562    }
563    return res;
564  }
565
566  static void addMapsToMaps(maps** mo,maps* mi){
567    maps* tmp=mi;
568    maps* _cursor=*mo;
569    while(tmp!=NULL){
570      if(_cursor==NULL){
571        *mo=dupMaps(&mi);
572      }
573      else{
574        while(_cursor->next!=NULL)
575          _cursor=_cursor->next;
576        maps* tmp1=getMaps(*mo,tmp->name);
577        if(tmp1==NULL)
578          _cursor->next=dupMaps(&tmp);
579        else
580          addMapToMap(&tmp1->content,tmp->content);
581        _cursor=*mo;
582      }
583      tmp=tmp->next;
584    }
585  }
586
587  static map* getMapArray(map* m,const char* key,int index){
588    char tmp[1024];
589    if(index>0)
590      sprintf(tmp,"%s_%d",key,index);
591    else
592      sprintf(tmp,"%s",key);
593#ifdef DEBUG
594    fprintf(stderr,"** KEY %s\n",tmp);
595#endif
596    map* tmpMap=getMap(m,tmp);
597#ifdef DEBUG
598    if(tmpMap!=NULL)
599      dumpMap(tmpMap);
600#endif
601    return tmpMap;
602  }
603
604
605  static void setMapArray(map* m,char* key,int index,char* value){
606    char tmp[1024];
607    if(index>0)
608      sprintf(tmp,"%s_%d",key,index);
609    else
610      sprintf(tmp,"%s",key);
611    map* tmpSize=getMapArray(m,(char*)"size",index);
612    if(tmpSize!=NULL && strncasecmp(key,"value",5)==0){
613#ifdef DEBUG
614      fprintf(stderr,"%s\n",tmpSize->value);
615#endif
616      map* ptr=getMapOrFill(&m,tmp,(char *)"");
617      free(ptr->value);
618      ptr->value=(char*)malloc((atoi(tmpSize->value)+1)*sizeof(char));
619      memcpy(ptr->value,value,atoi(tmpSize->value)); 
620    }
621    else
622      addToMap(m,tmp,value);
623  }
624
625  static map* getMapType(map* mt){
626    map* tmap=getMap(mt,(char *)"mimeType");
627    if(tmap==NULL){
628      tmap=getMap(mt,"dataType");
629      if(tmap==NULL){
630        tmap=getMap(mt,"CRS");
631      }
632    }
633#ifdef DEBUG
634        dumpMap(tmap);
635#endif
636    return tmap;
637  }
638
639  static int addMapsArrayToMaps(maps** mo,maps* mi,char* typ){
640    maps* tmp=mi;   
641    maps* _cursor=getMaps(*mo,tmp->name);
642
643    if(_cursor==NULL)
644      return -1;
645
646    map* tmpLength=getMap(_cursor->content,"length");
647    char tmpLen[10];
648    int len=1;
649    if(tmpLength!=NULL){
650      len=atoi(tmpLength->value);
651    }
652
653    char *tmpV[8]={
654      (char*)"size",
655      (char*)"value",
656      (char*)"uom",
657      (char*)"Reference",
658      (char*)"xlink:href",
659      typ,
660      (char*)"schema",
661      (char*)"encoding"
662    };
663    sprintf(tmpLen,"%d",len+1);
664    addToMap(_cursor->content,"length",tmpLen);
665    int i=0;
666    for(i=0;i<8;i++){
667      map* tmpVI=getMap(tmp->content,tmpV[i]);
668      if(tmpVI!=NULL){
669#ifdef DEBUG
670        fprintf(stderr,"%s = %s\n",tmpV[i],tmpVI->value);
671#endif
672        if(i<5)
673          setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
674        else
675          if(strncasecmp(tmpV[5],"mimeType",8)==0)
676            setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
677      }
678    }
679   
680    addToMap(_cursor->content,"isArray","true");
681    return 0;
682  }
683
684  static void setMapInMaps(maps* m,const char* key,const char* subkey,const char *value){
685    maps* _tmpm=getMaps(m,key);
686    if(_tmpm!=NULL){
687      map* _ztmpm=getMap(_tmpm->content,subkey);
688      if(_ztmpm!=NULL){
689        if(_ztmpm->value!=NULL)
690          free(_ztmpm->value);
691        _ztmpm->value=zStrdup(value);
692      }else{
693        maps *tmp=(maps*)malloc(MAPS_SIZE);
694        tmp->name=zStrdup(key);
695        tmp->content=createMap(subkey,value);
696        tmp->next=NULL;
697        addMapsToMaps(&_tmpm,tmp);
698        freeMaps(&tmp);
699        free(tmp);
700      }
701    }else{
702      maps *tmp=(maps*)malloc(MAPS_SIZE);
703      tmp->name=zStrdup(key);
704      tmp->content=createMap(subkey,value);
705      tmp->next=NULL;
706      addMapsToMaps(&m,tmp);
707      freeMaps(&tmp);
708      free(tmp);
709    }
710  }
711
712
713  static void dumpElements(elements* e){
714    elements* tmp=e;
715    while(tmp!=NULL){
716      fprintf(stderr,"ELEMENT [%s]\n",tmp->name);
717      fprintf(stderr," > CONTENT [%s]\n",tmp->name);
718      dumpMap(tmp->content);
719      fprintf(stderr," > METADATA [%s]\n",tmp->name);
720      dumpMap(tmp->metadata);
721      fprintf(stderr," > FORMAT [%s]\n",tmp->format);
722      iotype* tmpio=tmp->defaults;
723      int ioc=0;
724      while(tmpio!=NULL){
725        fprintf(stderr," > DEFAULTS [%s] (%i)\n",tmp->name,ioc);
726        dumpMap(tmpio->content);
727        tmpio=tmpio->next;
728        ioc++;
729      }
730      tmpio=tmp->supported;
731      ioc=0;
732      while(tmpio!=NULL){
733        fprintf(stderr," > SUPPORTED [%s] (%i)\n",tmp->name,ioc);
734        dumpMap(tmpio->content);
735        tmpio=tmpio->next;
736        ioc++;
737      }
738      fprintf(stderr,"------------------\n");
739      tmp=tmp->next;
740    }
741  }
742
743  static void dumpElementsAsYAML(elements* e){
744    elements* tmp=e;
745    int i;
746    while(tmp!=NULL){
747      for(i=0;i<2;i++)
748        fprintf(stderr," ");
749      fprintf(stderr,"%s:\n",tmp->name);
750      map* mcurs=tmp->content;
751      while(mcurs!=NULL){
752        for(i=0;i<4;i++)
753          fprintf(stderr," ");
754        _dumpMap(mcurs);
755        mcurs=mcurs->next;
756      }
757      mcurs=tmp->metadata;
758      if(mcurs!=NULL){
759        for(i=0;i<4;i++)
760          fprintf(stderr," ");
761        fprintf(stderr,"MetaData:\n");
762        while(mcurs!=NULL){
763          for(i=0;i<6;i++)
764            fprintf(stderr," ");
765          _dumpMap(mcurs);
766          mcurs=mcurs->next;
767        }
768      }
769      for(i=0;i<4;i++)
770        fprintf(stderr," ");
771      fprintf(stderr,"%s:\n",tmp->format);
772      iotype* tmpio=tmp->defaults;
773      int ioc=0;
774      while(tmpio!=NULL){
775        for(i=0;i<6;i++)
776          fprintf(stderr," ");
777        fprintf(stderr,"default:\n");
778        mcurs=tmpio->content;
779        while(mcurs!=NULL){
780          for(i=0;i<8;i++)
781            fprintf(stderr," ");
782          if(strcasecmp(mcurs->name,"range")==0){
783            fprintf(stderr,"range: \"%s\"\n",mcurs->value);
784          }else
785            _dumpMap(mcurs);
786          mcurs=mcurs->next;
787        }
788        tmpio=tmpio->next;
789        ioc++;
790      }
791      tmpio=tmp->supported;
792      ioc=0;
793      while(tmpio!=NULL){
794        for(i=0;i<6;i++)
795          fprintf(stderr," ");
796        fprintf(stderr,"supported:\n");
797        mcurs=tmpio->content;
798        while(mcurs!=NULL){
799          for(i=0;i<8;i++)
800            fprintf(stderr," ");
801          if(strcasecmp(mcurs->name,"range")==0){
802            fprintf(stderr,"range: \"%s\"\n",mcurs->value);
803          }else
804            _dumpMap(mcurs);
805          mcurs=mcurs->next;
806        }
807        tmpio=tmpio->next;
808        ioc++;
809      }
810      tmp=tmp->next;
811    }
812  }
813
814
815  static elements* dupElements(elements* e){
816    elements* cursor=e;
817    elements* tmp=NULL;
818    if(cursor!=NULL){
819#ifdef DEBUG
820      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
821      dumpElements(e);
822      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
823#endif
824      tmp=(elements*)malloc(ELEMENTS_SIZE);
825      tmp->name=zStrdup(e->name);
826      tmp->content=NULL;
827      addMapToMap(&tmp->content,e->content);
828      tmp->metadata=NULL;
829      addMapToMap(&tmp->metadata,e->metadata);
830      tmp->format=zStrdup(e->format);
831      if(e->defaults!=NULL){
832        tmp->defaults=(iotype*)malloc(IOTYPE_SIZE);
833        tmp->defaults->content=NULL;
834        addMapToMap(&tmp->defaults->content,e->defaults->content);
835        tmp->defaults->next=NULL;
836#ifdef DEBUG
837        fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
838        dumpMap(tmp->defaults->content);
839#endif
840      }else
841        tmp->defaults=NULL;
842      if(e->supported!=NULL){
843        tmp->supported=(iotype*)malloc(IOTYPE_SIZE);
844        tmp->supported->content=NULL;
845        addMapToMap(&tmp->supported->content,e->supported->content);
846        tmp->supported->next=NULL;
847        iotype *tmp2=e->supported->next;
848        while(tmp2!=NULL){
849          addMapToIoType(&tmp->supported,tmp2->content);
850#ifdef DEBUG
851          fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
852          dumpMap(tmp->defaults->content);
853#endif
854          tmp2=tmp2->next;
855        }
856      }
857      else
858        tmp->supported=NULL;
859      tmp->next=dupElements(cursor->next);
860    }
861    return tmp;
862  }
863
864  static void addToElements(elements** m,elements* e){
865    elements* tmp=e;
866    if(*m==NULL){
867      *m=dupElements(tmp);
868    }else{
869      addToElements(&(*m)->next,tmp);
870    }
871  }
872
873  static void dumpService(service* s){
874    fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name);
875    if(s->content!=NULL){
876      fprintf(stderr,"CONTENT MAP\n");
877      dumpMap(s->content);
878      fprintf(stderr,"CONTENT METADATA\n");
879      dumpMap(s->metadata);
880    }
881    if(s->inputs!=NULL){
882      fprintf(stderr,"INPUT ELEMENTS [%s]\n------------------\n",s->name);
883      dumpElements(s->inputs);
884    }
885    if(s->outputs!=NULL){
886      fprintf(stderr,"OUTPUT ELEMENTS [%s]\n------------------\n",s->name);
887      dumpElements(s->outputs);
888    }
889    fprintf(stderr,"++++++++++++++++++\n");
890  }
891
892  static void dumpServiceAsYAML(service* s){
893    int i;
894    fprintf(stderr,"# %s\n\n",s->name);
895    if(s->content!=NULL){
896      map* mcurs=s->content;
897      dumpMap(mcurs);
898      mcurs=s->metadata;
899      if(mcurs!=NULL){
900        fprintf(stderr,"MetaData:\n");
901        while(mcurs!=NULL){
902          for(i=0;i<2;i++)
903            fprintf(stderr," ");
904          _dumpMap(mcurs);
905          mcurs=mcurs->next;
906        }
907      }
908    }
909    if(s->inputs!=NULL){
910      fprintf(stderr,"\ninputs:\n");
911      dumpElementsAsYAML(s->inputs);
912    }
913    if(s->outputs!=NULL){
914      fprintf(stderr,"\noutputs:\n");
915      dumpElementsAsYAML(s->outputs);
916    }
917  }
918
919  static void mapsToCharXXX(maps* m,char*** c){
920    maps* tm=m;
921    int i=0;
922    int j=0;
923    char tmp[10][30][1024];
924    memset(tmp,0,1024*10*10);
925    while(tm!=NULL){
926      if(i>=10)
927        break;
928      strcpy(tmp[i][j],"name");
929      j++;
930      strcpy(tmp[i][j],tm->name);
931      j++;
932      map* tc=tm->content;
933      while(tc!=NULL){
934        if(j>=30)
935          break;
936        strcpy(tmp[i][j],tc->name);
937        j++;
938        strcpy(tmp[i][j],tc->value);
939        j++;
940        tc=tc->next;
941      }
942      tm=tm->next;
943      j=0;
944      i++;
945    }
946    memcpy(c,tmp,10*10*1024);
947  }
948
949  static void charxxxToMaps(char*** c,maps**m){
950    maps* trorf=*m;
951    int i,j;
952    char tmp[10][30][1024];
953    memcpy(tmp,c,10*30*1024);
954    for(i=0;i<10;i++){
955      if(strlen(tmp[i][1])==0)
956        break;
957      trorf->name=tmp[i][1];
958      trorf->content=NULL;
959      trorf->next=NULL;
960      for(j=2;j<29;j+=2){
961        if(strlen(tmp[i][j+1])==0)
962          break;
963        if(trorf->content==NULL)
964          trorf->content=createMap(tmp[i][j],tmp[i][j+1]);
965        else
966          addToMap(trorf->content,tmp[i][j],tmp[i][j+1]);
967      }
968      trorf=trorf->next;
969    }
970    m=&trorf;
971  }
972
973#ifdef WIN32
974  extern char *url_encode(char *);
975
976  static char* getMapsAsKVP(maps* m,int length,int type){
977    char *dataInputsKVP=(char*) malloc(length*sizeof(char));
978    char *dataInputsKVPi=NULL;
979    maps* curs=m;
980    int i=0;
981    while(curs!=NULL){
982      map *inRequest=getMap(curs->content,"inRequest");
983      map *hasLength=getMap(curs->content,"length");
984      if((inRequest!=NULL && strncasecmp(inRequest->value,"true",4)==0) ||
985         inRequest==NULL){
986        if(i==0)
987          if(type==0){
988            sprintf(dataInputsKVP,"%s=",curs->name);
989            if(hasLength!=NULL){
990              dataInputsKVPi=(char*)malloc((strlen(curs->name)+2)*sizeof(char));
991              sprintf(dataInputsKVPi,"%s=",curs->name);
992            }
993          }
994          else
995            sprintf(dataInputsKVP,"%s",curs->name);
996        else{
997          char *temp=zStrdup(dataInputsKVP);
998          if(type==0)
999            sprintf(dataInputsKVP,"%s;%s=",temp,curs->name);
1000          else
1001            sprintf(dataInputsKVP,"%s;%s",temp,curs->name);
1002        }
1003        map* icurs=curs->content;
1004        if(type==0){
1005          char *temp=zStrdup(dataInputsKVP);
1006          if(getMap(curs->content,"xlink:href")!=NULL)
1007            sprintf(dataInputsKVP,"%sReference",temp);
1008          else{
1009            if(hasLength!=NULL){
1010              int j;
1011              for(j=0;j<atoi(hasLength->value);j++){
1012                map* tmp0=getMapArray(curs->content,"value",j);
1013                if(j==0)
1014                  free(temp);
1015                temp=zStrdup(dataInputsKVP);
1016                if(j==0)
1017                  sprintf(dataInputsKVP,"%s%s",temp,tmp0->value);
1018                else
1019                  sprintf(dataInputsKVP,"%s;%s%s",temp,dataInputsKVPi,tmp0->value);
1020              }
1021            }
1022            else
1023              sprintf(dataInputsKVP,"%s%s",temp,icurs->value);
1024          }
1025          free(temp);
1026        }
1027        while(icurs!=NULL){
1028          if(strncasecmp(icurs->name,"value",5)!=0 &&
1029             strncasecmp(icurs->name,"mimeType_",9)!=0 &&
1030             strncasecmp(icurs->name,"dataType_",9)!=0 &&
1031             strncasecmp(icurs->name,"size",4)!=0 &&
1032             strncasecmp(icurs->name,"length",4)!=0 &&
1033             strncasecmp(icurs->name,"isArray",7)!=0 &&
1034             strcasecmp(icurs->name,"Reference")!=0 &&
1035             strcasecmp(icurs->name,"minOccurs")!=0 &&
1036             strcasecmp(icurs->name,"maxOccurs")!=0 &&
1037             strncasecmp(icurs->name,"fmimeType",9)!=0 &&
1038             strcasecmp(icurs->name,"inRequest")!=0){
1039            char *itemp=zStrdup(dataInputsKVP);
1040            if(strcasecmp(icurs->name,"xlink:href")!=0)
1041              sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,icurs->value);
1042            else
1043              sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,url_encode(icurs->value));
1044            free(itemp);
1045          }
1046          icurs=icurs->next;
1047        }
1048      }
1049      curs=curs->next;
1050      i++;
1051    }
1052    return dataInputsKVP;
1053  }
1054#endif
1055
1056#ifdef __cplusplus
1057}
1058#endif
1059
1060#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