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

Last change on this file since 570 was 554, checked in by knut, 10 years ago

Changed the WIN32 version of function zGettimeofday. Changed return type for getShmLockId (WIN32). Changed type of _HINTERNET.mimeType from unsigned char* to char*. Fixed interconnected memory issues in functions getKeyValue and getShmLockId (WIN32). Added code to transfer the correct unique process identifier (usid) to background processes (applies to WIN32 version).

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr
File size: 25.9 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#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,"%ld",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    char* tmpSized=NULL;
614   
615  }
616
617  static maps* dupMaps(maps** mo){
618    maps* _cursor=*mo;
619    maps* res=NULL;
620    if(_cursor!=NULL){
621      res=(maps*)malloc(MAPS_SIZE);
622      res->name=zStrdup(_cursor->name);
623      res->content=NULL;
624      res->next=NULL;
625      map* mc=_cursor->content;
626      if(mc!=NULL){
627        addMapToMap(&res->content,mc);
628        loadMapBinaries(&res->content,mc);
629      }
630      res->next=dupMaps(&_cursor->next);
631    }
632    return res;
633  }
634
635  static void addMapsToMaps(maps** mo,maps* mi){
636    maps* tmp=mi;
637    maps* _cursor=*mo;
638    while(tmp!=NULL){
639      if(_cursor==NULL){
640        *mo=dupMaps(&mi);
641      }
642      else{
643        while(_cursor->next!=NULL)
644          _cursor=_cursor->next;
645        maps* tmp1=getMaps(*mo,tmp->name);
646        if(tmp1==NULL)
647          _cursor->next=dupMaps(&tmp);
648        else
649          addMapToMap(&tmp1->content,tmp->content);
650        _cursor=*mo;
651      }
652      tmp=tmp->next;
653    }
654  }
655
656  static map* getMapArray(map* m,const char* key,int index){
657    char tmp[1024];
658    if(index>0)
659      sprintf(tmp,"%s_%d",key,index);
660    else
661      sprintf(tmp,"%s",key);
662#ifdef DEBUG
663    fprintf(stderr,"** KEY %s\n",tmp);
664#endif
665    map* tmpMap=getMap(m,tmp);
666#ifdef DEBUG
667    if(tmpMap!=NULL)
668      dumpMap(tmpMap);
669#endif
670    return tmpMap;
671  }
672
673
674  static void setMapArray(map* m,char* key,int index,char* value){
675    char tmp[1024];
676    if(index>0)
677      sprintf(tmp,"%s_%d",key,index);
678    else
679      sprintf(tmp,"%s",key);
680    map* tmpSize=getMapArray(m,"size",index);
681    if(tmpSize!=NULL && strncasecmp(key,"value",5)==0){
682#ifdef DEBUG
683      fprintf(stderr,"%s\n",tmpSize->value);
684#endif
685      map* ptr=getMapOrFill(&m,tmp,(char *)"");
686      free(ptr->value);
687      ptr->value=(char*)malloc((atoi(tmpSize->value)+1)*sizeof(char));
688      memcpy(ptr->value,value,atoi(tmpSize->value)); 
689    }
690    else
691      addToMap(m,tmp,value);
692  }
693
694  static map* getMapType(map* mt){
695    map* tmap=getMap(mt,(char *)"mimeType");
696    if(tmap==NULL){
697      tmap=getMap(mt,"dataType");
698      if(tmap==NULL){
699        tmap=getMap(mt,"CRS");
700      }
701    }
702#ifdef DEBUG
703        dumpMap(tmap);
704#endif
705    return tmap;
706  }
707
708  static int addMapsArrayToMaps(maps** mo,maps* mi,char* typ){
709    maps* tmp=mi;   
710    maps* _cursor=getMaps(*mo,tmp->name);
711
712    if(_cursor==NULL)
713      return -1;
714
715    map* tmpLength=getMap(_cursor->content,"length");
716    char tmpLen[10];
717    int len=1;
718    if(tmpLength!=NULL){
719      len=atoi(tmpLength->value);
720    }
721
722    char *tmpV[11]={
723      (char*)"size",
724      (char*)"value",
725      (char*)"uom",
726      (char*)"Reference",
727      (char*)"cache_file",
728      (char*)"fmimeType",
729      (char*)"xlink:href",
730      typ,
731      (char*)"schema",
732      (char*)"encoding",
733      (char*)"isCached"
734    };
735    sprintf(tmpLen,"%d",len+1);
736    addToMap(_cursor->content,"length",tmpLen);
737    int i=0;
738    for(i=0;i<11;i++){
739      map* tmpVI=getMap(tmp->content,tmpV[i]);
740      if(tmpVI!=NULL){
741#ifdef DEBUG
742        fprintf(stderr,"%s = %s\n",tmpV[i],tmpVI->value);
743#endif
744        if(i<7)
745          setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
746        else
747          if(strncasecmp(tmpV[7],"mimeType",8)==0)
748            setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
749      }
750    }
751   
752    addToMap(_cursor->content,"isArray","true");
753    return 0;
754  }
755
756  static void setMapInMaps(maps* m,const char* key,const char* subkey,const char *value){
757    maps* _tmpm=getMaps(m,key);
758    if(_tmpm!=NULL){
759      map* _ztmpm=getMap(_tmpm->content,subkey);
760      if(_ztmpm!=NULL){
761        if(_ztmpm->value!=NULL)
762          free(_ztmpm->value);
763        _ztmpm->value=zStrdup(value);
764      }else{
765        maps *tmp=(maps*)malloc(MAPS_SIZE);
766        tmp->name=zStrdup(key);
767        tmp->content=createMap(subkey,value);
768        tmp->next=NULL;
769        addMapsToMaps(&_tmpm,tmp);
770        freeMaps(&tmp);
771        free(tmp);
772      }
773    }else{
774      maps *tmp=(maps*)malloc(MAPS_SIZE);
775      tmp->name=zStrdup(key);
776      tmp->content=createMap(subkey,value);
777      tmp->next=NULL;
778      addMapsToMaps(&m,tmp);
779      freeMaps(&tmp);
780      free(tmp);
781    }
782  }
783
784
785  static void dumpElements(elements* e){
786    elements* tmp=e;
787    while(tmp!=NULL){
788      fprintf(stderr,"ELEMENT [%s]\n",tmp->name);
789      fprintf(stderr," > CONTENT [%s]\n",tmp->name);
790      dumpMap(tmp->content);
791      fprintf(stderr," > METADATA [%s]\n",tmp->name);
792      dumpMap(tmp->metadata);
793      fprintf(stderr," > FORMAT [%s]\n",tmp->format);
794      iotype* tmpio=tmp->defaults;
795      int ioc=0;
796      while(tmpio!=NULL){
797        fprintf(stderr," > DEFAULTS [%s] (%i)\n",tmp->name,ioc);
798        dumpMap(tmpio->content);
799        tmpio=tmpio->next;
800        ioc++;
801      }
802      tmpio=tmp->supported;
803      ioc=0;
804      while(tmpio!=NULL){
805        fprintf(stderr," > SUPPORTED [%s] (%i)\n",tmp->name,ioc);
806        dumpMap(tmpio->content);
807        tmpio=tmpio->next;
808        ioc++;
809      }
810      fprintf(stderr,"------------------\n");
811      tmp=tmp->next;
812    }
813  }
814
815  static void dumpElementsAsYAML(elements* e){
816    elements* tmp=e;
817    int i;
818    while(tmp!=NULL){
819      for(i=0;i<2;i++)
820        fprintf(stderr," ");
821      fprintf(stderr,"%s:\n",tmp->name);
822      map* mcurs=tmp->content;
823      while(mcurs!=NULL){
824        for(i=0;i<4;i++)
825          fprintf(stderr," ");
826        _dumpMap(mcurs);
827        mcurs=mcurs->next;
828      }
829      mcurs=tmp->metadata;
830      if(mcurs!=NULL){
831        for(i=0;i<4;i++)
832          fprintf(stderr," ");
833        fprintf(stderr,"MetaData:\n");
834        while(mcurs!=NULL){
835          for(i=0;i<6;i++)
836            fprintf(stderr," ");
837          _dumpMap(mcurs);
838          mcurs=mcurs->next;
839        }
840      }
841      for(i=0;i<4;i++)
842        fprintf(stderr," ");
843      fprintf(stderr,"%s:\n",tmp->format);
844      iotype* tmpio=tmp->defaults;
845      int ioc=0;
846      while(tmpio!=NULL){
847        for(i=0;i<6;i++)
848          fprintf(stderr," ");
849        fprintf(stderr,"default:\n");
850        mcurs=tmpio->content;
851        while(mcurs!=NULL){
852          for(i=0;i<8;i++)
853            fprintf(stderr," ");
854          if(strcasecmp(mcurs->name,"range")==0){
855            fprintf(stderr,"range: \"%s\"\n",mcurs->value);
856          }else
857            _dumpMap(mcurs);
858          mcurs=mcurs->next;
859        }
860        tmpio=tmpio->next;
861        ioc++;
862      }
863      tmpio=tmp->supported;
864      ioc=0;
865      while(tmpio!=NULL){
866        for(i=0;i<6;i++)
867          fprintf(stderr," ");
868        fprintf(stderr,"supported:\n");
869        mcurs=tmpio->content;
870        while(mcurs!=NULL){
871          for(i=0;i<8;i++)
872            fprintf(stderr," ");
873          if(strcasecmp(mcurs->name,"range")==0){
874            fprintf(stderr,"range: \"%s\"\n",mcurs->value);
875          }else
876            _dumpMap(mcurs);
877          mcurs=mcurs->next;
878        }
879        tmpio=tmpio->next;
880        ioc++;
881      }
882      tmp=tmp->next;
883    }
884  }
885
886
887  static elements* dupElements(elements* e){
888    elements* cursor=e;
889    elements* tmp=NULL;
890    if(cursor!=NULL){
891#ifdef DEBUG
892      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
893      dumpElements(e);
894      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
895#endif
896      tmp=(elements*)malloc(ELEMENTS_SIZE);
897      tmp->name=zStrdup(e->name);
898      tmp->content=NULL;
899      addMapToMap(&tmp->content,e->content);
900      tmp->metadata=NULL;
901      addMapToMap(&tmp->metadata,e->metadata);
902      tmp->format=zStrdup(e->format);
903      if(e->defaults!=NULL){
904        tmp->defaults=(iotype*)malloc(IOTYPE_SIZE);
905        tmp->defaults->content=NULL;
906        addMapToMap(&tmp->defaults->content,e->defaults->content);
907        tmp->defaults->next=NULL;
908#ifdef DEBUG
909        fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
910        dumpMap(tmp->defaults->content);
911#endif
912      }else
913        tmp->defaults=NULL;
914      if(e->supported!=NULL){
915        tmp->supported=(iotype*)malloc(IOTYPE_SIZE);
916        tmp->supported->content=NULL;
917        addMapToMap(&tmp->supported->content,e->supported->content);
918        tmp->supported->next=NULL;
919        iotype *tmp2=e->supported->next;
920        while(tmp2!=NULL){
921          addMapToIoType(&tmp->supported,tmp2->content);
922#ifdef DEBUG
923          fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
924          dumpMap(tmp->defaults->content);
925#endif
926          tmp2=tmp2->next;
927        }
928      }
929      else
930        tmp->supported=NULL;
931      tmp->next=dupElements(cursor->next);
932    }
933    return tmp;
934  }
935
936  static void addToElements(elements** m,elements* e){
937    elements* tmp=e;
938    if(*m==NULL){
939      *m=dupElements(tmp);
940    }else{
941      addToElements(&(*m)->next,tmp);
942    }
943  }
944
945  static void dumpService(service* s){
946    fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name);
947    if(s->content!=NULL){
948      fprintf(stderr,"CONTENT MAP\n");
949      dumpMap(s->content);
950      fprintf(stderr,"CONTENT METADATA\n");
951      dumpMap(s->metadata);
952    }
953    if(s->inputs!=NULL){
954      fprintf(stderr,"INPUT ELEMENTS [%s]\n------------------\n",s->name);
955      dumpElements(s->inputs);
956    }
957    if(s->outputs!=NULL){
958      fprintf(stderr,"OUTPUT ELEMENTS [%s]\n------------------\n",s->name);
959      dumpElements(s->outputs);
960    }
961    fprintf(stderr,"++++++++++++++++++\n");
962  }
963
964  static void dumpServiceAsYAML(service* s){
965    int i;
966    fprintf(stderr,"# %s\n\n",s->name);
967    if(s->content!=NULL){
968      map* mcurs=s->content;
969      dumpMap(mcurs);
970      mcurs=s->metadata;
971      if(mcurs!=NULL){
972        fprintf(stderr,"MetaData:\n");
973        while(mcurs!=NULL){
974          for(i=0;i<2;i++)
975            fprintf(stderr," ");
976          _dumpMap(mcurs);
977          mcurs=mcurs->next;
978        }
979      }
980    }
981    if(s->inputs!=NULL){
982      fprintf(stderr,"\ninputs:\n");
983      dumpElementsAsYAML(s->inputs);
984    }
985    if(s->outputs!=NULL){
986      fprintf(stderr,"\noutputs:\n");
987      dumpElementsAsYAML(s->outputs);
988    }
989  }
990
991  static void mapsToCharXXX(maps* m,char*** c){
992    maps* tm=m;
993    int i=0;
994    int j=0;
995    char tmp[10][30][1024];
996    memset(tmp,0,1024*10*10);
997    while(tm!=NULL){
998      if(i>=10)
999        break;
1000      strcpy(tmp[i][j],"name");
1001      j++;
1002      strcpy(tmp[i][j],tm->name);
1003      j++;
1004      map* tc=tm->content;
1005      while(tc!=NULL){
1006        if(j>=30)
1007          break;
1008        strcpy(tmp[i][j],tc->name);
1009        j++;
1010        strcpy(tmp[i][j],tc->value);
1011        j++;
1012        tc=tc->next;
1013      }
1014      tm=tm->next;
1015      j=0;
1016      i++;
1017    }
1018    memcpy(c,tmp,10*10*1024);
1019  }
1020
1021  static void charxxxToMaps(char*** c,maps**m){
1022    maps* trorf=*m;
1023    int i,j;
1024    char tmp[10][30][1024];
1025    memcpy(tmp,c,10*30*1024);
1026    for(i=0;i<10;i++){
1027      if(strlen(tmp[i][1])==0)
1028        break;
1029      trorf->name=tmp[i][1];
1030      trorf->content=NULL;
1031      trorf->next=NULL;
1032      for(j=2;j<29;j+=2){
1033        if(strlen(tmp[i][j+1])==0)
1034          break;
1035        if(trorf->content==NULL)
1036          trorf->content=createMap(tmp[i][j],tmp[i][j+1]);
1037        else
1038          addToMap(trorf->content,tmp[i][j],tmp[i][j+1]);
1039      }
1040      trorf=trorf->next;
1041    }
1042    m=&trorf;
1043  }
1044
1045#ifdef WIN32
1046  extern char *url_encode(char *);
1047
1048  static char* getMapsAsKVP(maps* m,int length,int type){
1049    char *dataInputsKVP=(char*) malloc(length*sizeof(char));
1050    char *dataInputsKVPi=NULL;
1051    maps* curs=m;
1052    int i=0;
1053    while(curs!=NULL){
1054      map *inRequest=getMap(curs->content,"inRequest");
1055      map *hasLength=getMap(curs->content,"length");
1056      if((inRequest!=NULL && strncasecmp(inRequest->value,"true",4)==0) ||
1057         inRequest==NULL){
1058        if(i==0)
1059          if(type==0){
1060            sprintf(dataInputsKVP,"%s=",curs->name);
1061            if(hasLength!=NULL){
1062              dataInputsKVPi=(char*)malloc((strlen(curs->name)+2)*sizeof(char));
1063              sprintf(dataInputsKVPi,"%s=",curs->name);
1064            }
1065          }
1066          else
1067            sprintf(dataInputsKVP,"%s",curs->name);
1068        else{
1069          char *temp=zStrdup(dataInputsKVP);
1070          if(type==0)
1071            sprintf(dataInputsKVP,"%s;%s=",temp,curs->name);
1072          else
1073            sprintf(dataInputsKVP,"%s;%s",temp,curs->name);
1074        }
1075        map* icurs=curs->content;
1076        if(type==0){
1077          char *temp=zStrdup(dataInputsKVP);
1078          if(getMap(curs->content,"xlink:href")!=NULL)
1079            sprintf(dataInputsKVP,"%sReference",temp);
1080          else{
1081            if(hasLength!=NULL){
1082              int j;
1083              for(j=0;j<atoi(hasLength->value);j++){
1084                map* tmp0=getMapArray(curs->content,"value",j);
1085                if(j==0)
1086                  free(temp);
1087                temp=zStrdup(dataInputsKVP);
1088                if(j==0)
1089                  sprintf(dataInputsKVP,"%s%s",temp,tmp0->value);
1090                else
1091                  sprintf(dataInputsKVP,"%s;%s%s",temp,dataInputsKVPi,tmp0->value);
1092              }
1093            }
1094            else
1095              sprintf(dataInputsKVP,"%s%s",temp,icurs->value);
1096          }
1097          free(temp);
1098        }
1099        while(icurs!=NULL){
1100          if(strncasecmp(icurs->name,"value",5)!=0 &&
1101             strncasecmp(icurs->name,"mimeType_",9)!=0 &&
1102             strncasecmp(icurs->name,"dataType_",9)!=0 &&
1103             strncasecmp(icurs->name,"size",4)!=0 &&
1104             strncasecmp(icurs->name,"length",4)!=0 &&
1105             strncasecmp(icurs->name,"isArray",7)!=0 &&
1106             strcasecmp(icurs->name,"Reference")!=0 &&
1107             strcasecmp(icurs->name,"minOccurs")!=0 &&
1108             strcasecmp(icurs->name,"maxOccurs")!=0 &&
1109             strncasecmp(icurs->name,"fmimeType",9)!=0 &&
1110             strcasecmp(icurs->name,"inRequest")!=0){
1111            char *itemp=zStrdup(dataInputsKVP);
1112            if(strcasecmp(icurs->name,"xlink:href")!=0)
1113              sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,icurs->value);
1114            else
1115              sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,url_encode(icurs->value));
1116            free(itemp);
1117          }
1118          icurs=icurs->next;
1119        }
1120      }
1121      curs=curs->next;
1122      i++;
1123    }
1124    return dataInputsKVP;
1125  }
1126#endif
1127
1128#ifdef __cplusplus
1129}
1130#endif
1131
1132#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