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

Last change on this file since 576 was 576, checked in by djay, 9 years ago

Code cleanup, description of some functon included in the code, addition of support for multiple error output, beter internal gesture of MapArray?.

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