source: trunk/zoo-project/zoo-kernel/service.c @ 645

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

Adding missing files

  • Property svn:keywords set to Id
File size: 34.7 KB
RevLine 
[645]1/*
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 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#include "service.h"
26
27/**
28 * Dump a map on stderr
29 *
30 * @param t the map to dump
31 */
32void _dumpMap(map* t){
33  if(t!=NULL){
34    fprintf(stderr,"%s: %s\n",t->name,t->value);
35    fflush(stderr);
36  }else{
37    fprintf(stderr,"NULL\n");
38    fflush(stderr);
39  }
40}
41
42/**
43 * Dump a map on stderr, see _dumpMap()
44 *
45 * @param t the map to dump
46 */
47void dumpMap(map* t){
48  map* tmp=t;
49  while(tmp!=NULL){
50    _dumpMap(tmp);
51    tmp=tmp->next;
52  }
53}
54
55/**
56 * Dump a map to a file
57 *
58 * @param t the map to dump to file
59 * @param file the file to store the map
60 */
61void dumpMapToFile(map* t,FILE* file){
62  map* tmp=t;
63  while(tmp!=NULL){
64#ifdef DEBUG
65    fprintf(stderr,"%s = %s\n",tmp->name,tmp->value);
66#endif
67    fprintf(file,"%s = %s\n",tmp->name,tmp->value);
68    tmp=tmp->next;
69  }
70}
71
72/**
73 * Dump a maps on stderr, see dumpMap().
74 *
75 * @param m the map to dump
76 */
77void dumpMaps(maps* m){
78  maps* tmp=m;
79  while(tmp!=NULL){
80    fprintf(stderr,"MAP => [%s] \n",tmp->name);
81    dumpMap(tmp->content);
82    tmp=tmp->next;
83  }
84}
85
86/**
87 * Dump a maps to a file, see dumpMapToFile().
88 *
89 * @param m the map to dump
90 * @param file_path the full path to the file name to store the map
91 */
92void dumpMapsToFile(maps* m,char* file_path){
93  FILE* file=fopen(file_path,"w");
94  maps* tmp=m;
95  if(tmp!=NULL){
96    fprintf(file,"[%s]\n",tmp->name);
97      dumpMapToFile(tmp->content,file);
98      fflush(file);
99    }
100    fclose(file);
101  }
102 
103/**
104 * Create a new map
105 *
106 * @param name the key to add to the map
107 * @param value the corresponding value to add to the map
108 * @return the allocated map
109 */
110map* createMap(const char* name,const char* value){
111  map* tmp=(map *)malloc(MAP_SIZE);
112  tmp->name=zStrdup(name);
113  tmp->value=zStrdup(value);
114  tmp->next=NULL;
115  return tmp;
116}
117
118/**
119 * Count number of map in a map
120 *
121 * @param m the maps to count
122 * @return number of map in a map
123 */
124int count(map* m){
125  map* tmp=m;
126  int c=0;
127  while(tmp!=NULL){
128    c++;
129    tmp=tmp->next;
130  }
131  return c;
132}
133
134/**
135 * Verify if a key exist in a map
136 *
137 * @param m the map to search for the key
138 * @param key the key to search in the map
139 * @return true if the key wwas found, false in other case
140 */
141bool hasKey(map* m,const char *key){
142  map* tmp=m;
143  while(tmp!=NULL){
144    if(strcasecmp(tmp->name,key)==0)
145      return true;
146    tmp=tmp->next;
147  }
148#ifdef DEBUG_MAP
149  fprintf(stderr,"NOT FOUND \n");
150#endif
151  return false;
152}
153
154/**
155 * Access a specific maps
156 *
157 * @param m the maps to search for the key
158 * @param key the key to search in the maps
159 * @return a pointer on the maps found or NULL if not found
160 */
161maps* getMaps(maps* m,const char *key){
162  maps* tmp=m;
163  while(tmp!=NULL){
164    if(strcasecmp(tmp->name,key)==0){
165      return tmp;
166    }
167    tmp=tmp->next;
168  }
169  return NULL;
170}
171
172/**
173 * Access a specific map
174 *
175 * @param m the map to search for the key
176 * @param key the key to search in the map
177 * @return a pointer on the map found or NULL if not found
178 */
179map* getMap(map* m,const char *key){
180  map* tmp=m;
181  while(tmp!=NULL){
182    if(strcasecmp(tmp->name,key)==0){
183      return tmp;
184    }
185    tmp=tmp->next;
186  }
187  return NULL;
188}
189
190
191/**
192 * Access the last map
193 *
194 * @param m the map to search for the lastest map
195 * @return a pointer on the lastest map found or NULL if not found
196 */
197map* getLastMap(map* m){
198  map* tmp=m;
199  while(tmp!=NULL){
200    if(tmp->next==NULL){
201      return tmp;
202    }
203    tmp=tmp->next;
204  }
205  return NULL;
206}
207
208/**
209 * Access a specific map from a maps
210 *
211 * @param m the maps to search for the key
212 * @param key the key to search in the maps
213 * @param subkey the key to search in the map (found for the key, if any)
214 * @return a pointer on the map found or NULL if not found
215 */
216map* getMapFromMaps(maps* m,const char* key,const char* subkey){
217  maps* _tmpm=getMaps(m,key);
218  if(_tmpm!=NULL){
219    map* _ztmpm=getMap(_tmpm->content,subkey);
220    return _ztmpm;
221  }
222  else return NULL;
223}
224
225/**
226 * Free allocated memory of a map.
227 * Require to call free on mo after calling this function.
228 *
229 * @param mo the map to free
230 */
231void freeMap(map** mo){
232  map* _cursor=*mo;
233  if(_cursor!=NULL){
234#ifdef DEBUG
235    fprintf(stderr,"freeMap\n");
236#endif
237    free(_cursor->name);
238    free(_cursor->value);
239    if(_cursor->next!=NULL){
240      freeMap(&_cursor->next);
241      free(_cursor->next);
242    }
243  }
244}
245
246/**
247 * Free allocated memory of a maps.
248 * Require to call free on mo after calling this function.
249 *
250 * @param mo the maps to free
251 */
252void freeMaps(maps** mo){
253  maps* _cursor=*mo;
254  if(_cursor && _cursor!=NULL){
255#ifdef DEBUG
256    fprintf(stderr,"freeMaps\n");
257#endif
258    free(_cursor->name);
259    if(_cursor->content!=NULL){
260      freeMap(&_cursor->content);
261      free(_cursor->content);
262    }
263    if(_cursor->next!=NULL){
264      freeMaps(&_cursor->next);
265      free(_cursor->next);
266    }
267  }
268}
269
270/**
271 * Verify if an elements contains a name equal to the given key.
272 *
273 * @param e the elements to search for the key
274 * @param key the elements name to search
275 * @return true if the elements contains the name, false in other cases.
276 */ 
277bool hasElement(elements* e,const char* key){
278  elements* tmp=e;
279  while(tmp!=NULL){
280    if(strcasecmp(key,tmp->name)==0)
281      return true;
282    tmp=tmp->next;
283  }
284  return false;
285}
286
287/**
288 * Access a specific elements named key.
289 *
290 * @param m the elements to search
291 * @param key the elements name to search
292 * @return a pointer to the specific element if found, NULL in other case.
293 */ 
294elements* getElements(elements* m,char *key){
295  elements* tmp=m;
296  while(tmp!=NULL){
297    if(strcasecmp(tmp->name,key)==0)
298      return tmp;
299    tmp=tmp->next;
300  }
301  return NULL;
302}
303
304/**
305 * Free allocated memory of an iotype.
306 * Require to call free on i after calling this function.
307 *
308 * @param i the iotype to free
309 */
310void freeIOType(iotype** i){
311  iotype* _cursor=*i;
312  if(_cursor!=NULL){
313    if(_cursor->next!=NULL){
314      freeIOType(&_cursor->next);
315      free(_cursor->next);
316    }
317    freeMap(&_cursor->content);
318    free(_cursor->content);
319  }
320}
321
322/**
323 * Free allocated memory of an elements.
324 * Require to call free on e after calling this function.
325 *
326 * @param e the iotype to free
327 */
328void freeElements(elements** e){
329  elements* tmp=*e;
330  if(tmp!=NULL){
331    if(tmp->name!=NULL)
332      free(tmp->name);
333    freeMap(&tmp->content);
334    if(tmp->content!=NULL)
335      free(tmp->content);
336    freeMap(&tmp->metadata);
337    if(tmp->metadata!=NULL)
338      free(tmp->metadata);
339    if(tmp->format!=NULL)
340      free(tmp->format);
341    freeIOType(&tmp->defaults);
342    if(tmp->defaults!=NULL)
343      free(tmp->defaults);
344    freeIOType(&tmp->supported);
345    if(tmp->supported!=NULL){
346      free(tmp->supported);
347    }
348    freeElements(&tmp->next);
349    if(tmp->next!=NULL)
350      free(tmp->next);
351  }
352}
353
354/**
355 * Free allocated memory of a service.
356 * Require to call free on e after calling this function.
357 *
358 * @param s the service to free
359 */
360void freeService(service** s){
361  service* tmp=*s;
362  if(tmp!=NULL){
363    if(tmp->name!=NULL)
364      free(tmp->name);
365    freeMap(&tmp->content);
366    if(tmp->content!=NULL)
367      free(tmp->content);
368    freeMap(&tmp->metadata);
369    if(tmp->metadata!=NULL)
370      free(tmp->metadata);
371    freeElements(&tmp->inputs);
372    if(tmp->inputs!=NULL)
373      free(tmp->inputs);
374    freeElements(&tmp->outputs);
375    if(tmp->outputs!=NULL)
376      free(tmp->outputs);
377  }
378}
379
380/**
381 * Add key value pair to an existing map.
382 *
383 * @param m the map to add the KVP
384 * @param n the key to add
385 * @param v the corresponding value to add
386 */
387void addToMap(map* m,const char* n,const char* v){
388  if(hasKey(m,n)==false){
389    map* _cursor=m;
390    while(_cursor->next!=NULL){
391      _cursor=_cursor->next;
392    }
393    _cursor->next=createMap(n,v);
394  }
395  else{
396    map *tmp=getMap(m,n);
397    if(tmp->value!=NULL)
398      free(tmp->value);
399    tmp->value=zStrdup(v);
400  }
401}
402
403/**
404 * Add a key and an integer value to an existing map.
405 *
406 * @param m the map to add the KVP
407 * @param n the key to add
408 * @param v the corresponding value to add
409 */
410void addIntToMap(map* m,const char* n,const int v){
411  char svalue[10];
412  sprintf(svalue,"%d",v);
413  if(hasKey(m,n)==false){
414    map* _cursor=m;
415    while(_cursor->next!=NULL){
416      _cursor=_cursor->next;
417    }
418    _cursor->next=createMap(n,svalue);
419  }
420  else{
421    map *tmp=getMap(m,n);
422    if(tmp->value!=NULL)
423      free(tmp->value);
424    tmp->value=zStrdup(svalue);
425  }
426}
427
428/**
429 * Add a key and a binary value to an existing map.
430 *
431 * @param m the map to add the KVP
432 * @param n the key to add
433 * @param v the corresponding value to add
434 * @param size the size of the given value
435 */
436void addToMapWithSize(map* m,const char* n,const char* v,int size){
437  if(hasKey(m,n)==false){
438    map* _cursor=m;
439    if(_cursor!=NULL){
440      addToMap(m,n,"");
441    }else{
442      m=createMap(n,"");
443    }
444  }
445  char sname[10]="size";
446  if(strlen(n)>5)
447    sprintf(sname,"size_%s",n+6);
448  map *tmp=getMap(m,n);
449  if(tmp->value!=NULL)
450    free(tmp->value);
451  tmp->value=(char*)malloc((size+1)*sizeof(char));
452  memmove(tmp->value,v,size*sizeof(char));
453  tmp->value[size]=0;
454  char sin[128];
455  sprintf(sin,"%d",size);
456  addToMap(m,sname,sin);
457}
458
459/**
460 * Add a map at the end of another map.
461 *
462 * @param mo the map to add mi
463 * @param mi the map to add to mo
464 */
465void addMapToMap(map** mo,map* mi){
466  map* tmp=mi;
467  map* _cursor=*mo;
468  while(tmp!=NULL){
469    if(_cursor==NULL){
470      *mo=createMap(tmp->name,tmp->value);
471      (*mo)->next=NULL;
472    }
473    else{
474#ifdef DEBUG
475      fprintf(stderr,"_CURSOR\n");
476      dumpMap(_cursor);
477#endif
478      while(_cursor->next!=NULL)
479        _cursor=_cursor->next;
480      map* tmp1=getMap(*mo,tmp->name);
481      if(tmp1==NULL){
482        _cursor->next=createMap(tmp->name,tmp->value);
483      }
484      else{
485        addToMap(*mo,tmp->name,tmp->value);
486      }
487    }
488    _cursor=*mo;
489    tmp=tmp->next;
490#ifdef DEBUG
491    fprintf(stderr,"MO\n");
492    dumpMap(*mo);
493#endif
494  }
495}
496
497/**
498 * Add a map to iotype.
499 *
500 * @param io the iotype to add the map
501 * @param mi the map to add to io
502 */
503void addMapToIoType(iotype** io,map* mi){
504  iotype* tmp=*io;
505  while(tmp->next!=NULL){
506    tmp=tmp->next;
507  }
508  tmp->next=(iotype*)malloc(IOTYPE_SIZE);
509  tmp->next->content=NULL;
510  addMapToMap(&tmp->next->content,mi);
511  tmp->next->next=NULL;
512}
513
514/**
515 * Access a specific map or set its value.
516 *
517 * @param m the map to search for the key
518 * @param key the key to search/add in the map
519 * @param value the value to add if the key does not exist
520 * @return a pointer on the map found or NULL if not found
521 */
522map* getMapOrFill(map** m,const char *key,const char* value){
523  map* tmp=*m;
524  map* tmpMap=getMap(tmp,key);
525  if(tmpMap==NULL){
526    if(tmp!=NULL){
527      addToMap((*m),key,value);
528    }
529    else
530      (*m)=createMap(key,value);
531    tmpMap=getMap(*m,key);
532  }
533  return tmpMap;
534}
535
536/**
537 * Verify if a map is contained in another map.
538 *
539 * @param m the map to search for i
540 * @param i the map to search in m
541 * @return true if i was found in m, false in other case
542 */
543bool contains(map* m,map* i){
544  while(i!=NULL){     
545    if(strcasecmp(i->name,"value")!=0 &&
546       strcasecmp(i->name,"xlink:href")!=0 &&
547       strcasecmp(i->name,"useMapServer")!=0 &&
548       strcasecmp(i->name,"asReference")!=0){
549      map *tmp;
550      if(hasKey(m,i->name) && (tmp=getMap(m,i->name))!=NULL && 
551         strcasecmp(i->value,tmp->value)!=0)
552        return false;
553    }
554    i=i->next;
555  }
556  return true;
557}
558
559/**
560 * Access a specific iotype from an elements.
561 *
562 * @param e the elements to search for the name
563 * @param name the name to search in the elements e
564 * @param values the map to verify it was contained in the defaults or
565 *  supported content of the elements e
566 * @return a pointer on the iotype found or NULL if not found
567 */
568iotype* getIoTypeFromElement(elements* e,char *name, map* values){
569  elements* cursor=e;
570  while(cursor!=NULL){
571    if(strcasecmp(cursor->name,name)==0){
572      if(contains(cursor->defaults->content,values)==true)
573        return cursor->defaults;
574      else{
575        iotype* tmp=cursor->supported;
576        while(tmp!=NULL){
577          if(contains(tmp->content,values)==true)
578            return tmp;     
579          tmp=tmp->next;
580        }
581      }
582    }
583    cursor=cursor->next;
584  }
585  return NULL;
586}
587
588/**
589 * Load binary values from a map (in) and add them to another map (out)
590 *
591 * @param out the map to add binaries values
592 * @param in the map containing the binary values to add ti out
593 * @param pos index of the binary in an array (in case of "MapArray")
594 */
595void loadMapBinary(map** out,map* in,int pos){
596  map* size=getMap(in,"size");
597  map *lout=*out;
598  if(size!=NULL && pos>0){
599    char tmp[11];
600    sprintf(tmp,"size_%d",pos);
601    size=getMap(in,tmp);
602    sprintf(tmp,"value_%d",pos);
603    map* tmpVin=getMap(in,tmp);
604    map* tmpVout=getMap(lout,tmp);
605    free(tmpVout->value);
606    tmpVout->value=(char*)malloc((atoi(size->value)+1)*sizeof(char));
607    memmove(tmpVout->value,tmpVin->value,atoi(size->value)*sizeof(char));
608    tmpVout->value[atoi(size->value)]=0;
609  }else{
610    if(size!=NULL){
611      map* tmpVin=getMap(in,"value");
612      map* tmpVout=getMap(lout,"value");
613      free(tmpVout->value);
614      tmpVout->value=(char*)malloc((atoi(size->value)+1)*sizeof(char));
615      memmove(tmpVout->value,tmpVin->value,atoi(size->value)*sizeof(char));
616      tmpVout->value[atoi(size->value)]=0;
617    }
618  }
619}
620 
621/**
622 * Load binary values from a map (in) and add them to another map (out).
623 * This function will take care of MapArray.
624 * @see loadMapBinary
625 *
626 * @param out the map to add binaries values
627 * @param in the map containing the binary values to add ti out
628 */
629void loadMapBinaries(map** out,map* in){
630  map* size=getMap(in,"size");
631  map* length=getMap(in,"length");
632  if(length!=NULL){
633    int len=atoi(length->value);
634    int i=0;
635    for(i=0;i<len;i++){
636      loadMapBinary(out,in,i);
637    }
638  }
639  else
640    if(size!=NULL)
641      loadMapBinary(out,in,-1);
642}
643
644/**
645 * Duplicate a Maps
646 *
647 * @param mo the maps to clone
648 * @return the allocated maps containing a copy of the mo maps
649 */
650maps* dupMaps(maps** mo){
651  maps* _cursor=*mo;
652  maps* res=NULL;
653  if(_cursor!=NULL){
654    res=(maps*)malloc(MAPS_SIZE);
655    res->name=zStrdup(_cursor->name);
656    res->content=NULL;
657    res->next=NULL;
658    map* mc=_cursor->content;
659    if(mc!=NULL){
660      addMapToMap(&res->content,mc);
661      loadMapBinaries(&res->content,mc);
662    }
663    res->next=dupMaps(&_cursor->next);
664  }
665  return res;
666}
667
668/**
669 * Add a maps at the end of another maps.
670 *
671 * @see addMapToMap, dupMaps, getMaps
672 * @param mo the maps to add mi
673 * @param mi the maps to add to mo
674 */
675void addMapsToMaps(maps** mo,maps* mi){
676  maps* tmp=mi;
677  maps* _cursor=*mo;
678  while(tmp!=NULL){
679    if(_cursor==NULL){
680      *mo=dupMaps(&mi);
681    }
682    else{
683      while(_cursor->next!=NULL)
684        _cursor=_cursor->next;
685      maps* tmp1=getMaps(*mo,tmp->name);
686      if(tmp1==NULL)
687        _cursor->next=dupMaps(&tmp);
688      else
689        addMapToMap(&tmp1->content,tmp->content);
690      _cursor=*mo;
691    }
692    tmp=tmp->next;
693  }
694}
695
696/**
697 * Access a specific map array element
698 *
699 * @param m the map to search for the key
700 * @param key the key to search in the map
701 * @param index of the MapArray
702 * @return a pointer on the map found or NULL if not found
703 */
704map* getMapArray(map* m,const char* key,int index){
705  char tmp[1024];
706  if(index>0)
707    sprintf(tmp,"%s_%d",key,index);
708  else
709    sprintf(tmp,"%s",key);
710#ifdef DEBUG
711  fprintf(stderr,"** KEY %s\n",tmp);
712#endif
713  map* tmpMap=getMap(m,tmp);
714#ifdef DEBUG
715  if(tmpMap!=NULL)
716    dumpMap(tmpMap);
717#endif
718  return tmpMap;
719}
720
721/**
722 * Add a key value in a MapArray for a specific index
723 *
724 * @param m the map to search for the key
725 * @param key the key to search in the map
726 * @param index the index of the MapArray
727 * @param value the value to set in the MapArray
728 * @return a pointer on the map found or NULL if not found
729 */
730void setMapArray(map* m,const char* key,int index,const char* value){
731  char tmp[1024];
732  if(index>0){
733    sprintf(tmp,"%s_%d",key,index);
734    map* len=getMap(m,"length");
735    if((len!=NULL && atoi(len->value)<index+1) || len==NULL){
736      char tmp0[5];
737      sprintf(tmp0,"%d",index+1);
738      addToMap(m,"length",tmp0);
739    }
740  }
741  else
742    sprintf(tmp,"%s",key);
743  map* tmpSize=getMapArray(m,"size",index);
744  if(tmpSize!=NULL && strncasecmp(key,"value",5)==0){
745#ifdef DEBUG
746    fprintf(stderr,"%s\n",tmpSize->value);
747#endif
748    map* ptr=getMapOrFill(&m,tmp,(char *)"");
749    free(ptr->value);
750    ptr->value=(char*)malloc((atoi(tmpSize->value)+1)*sizeof(char));
751    memcpy(ptr->value,value,atoi(tmpSize->value)); 
752  }
753  else
754    addToMap(m,tmp,value);
755}
756
757/**
758 * Access the map "type"
759 *
760 * @param mt the map
761 * @return a pointer on the map for mimeType/dataType/CRS if found, NULL in
762 *  other case
763 */
764map* getMapType(map* mt){
765  map* tmap=getMap(mt,(char *)"mimeType");
766  if(tmap==NULL){
767    tmap=getMap(mt,"dataType");
768    if(tmap==NULL){
769      tmap=getMap(mt,"CRS");
770    }
771  }
772#ifdef DEBUG
773  dumpMap(tmap);
774#endif
775  return tmap;
776}
777
778/**
779 * Add a Maps containing a MapArray to a Maps
780 *
781 * @see getMapType
782 * @param mo the maps
783 * @param mi the maps
784 * @param typ the map "type"
785 * @return
786 */
787int addMapsArrayToMaps(maps** mo,maps* mi,char* typ){
788  maps* tmp=mi;   
789  maps* _cursor=getMaps(*mo,tmp->name);
790
791  if(_cursor==NULL)
792    return -1;
793
794  map* tmpLength=getMap(_cursor->content,"length");
795  char tmpLen[10];
796  int len=1;
797  if(tmpLength!=NULL){
798    len=atoi(tmpLength->value);
799  }
800
801  char *tmpV[12]={
802    (char*)"size",
803    (char*)"value",
804    (char*)"uom",
805    (char*)"Reference",
806    (char*)"Order",
807    (char*)"cache_file",
808    (char*)"fmimeType",
809    (char*)"xlink:href",
810    typ,
811    (char*)"schema",
812    (char*)"encoding",
813    (char*)"isCached"
814  };
815  sprintf(tmpLen,"%d",len+1);
816  addToMap(_cursor->content,"length",tmpLen);
817  int i=0;
818  for(i=0;i<12;i++){
819    map* tmpVI=getMap(tmp->content,tmpV[i]);
820    if(tmpVI!=NULL){
821#ifdef DEBUG
822      fprintf(stderr,"%s = %s\n",tmpV[i],tmpVI->value);
823#endif
824      setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
825    }
826  }
827   
828  addToMap(_cursor->content,"isArray","true");
829  return 0;
830}
831
832/**
833 * Set a key value pair to a map contained in a Maps
834 *
835 * @param m the maps
836 * @param key the maps name
837 * @param subkey the map name included in the maps corresponding to key
838 * @param value the corresponding value to add in the map
839 */
840void setMapInMaps(maps* m,const char* key,const char* subkey,const char *value){
841  maps* _tmpm=getMaps(m,key);
842  if(_tmpm!=NULL){
843    map* _ztmpm=getMap(_tmpm->content,subkey);
844    if(_ztmpm!=NULL){
845      if(_ztmpm->value!=NULL)
846        free(_ztmpm->value);
847      _ztmpm->value=zStrdup(value);
848    }else{
849      maps *tmp=(maps*)malloc(MAPS_SIZE);
850      tmp->name=zStrdup(key);
851      tmp->content=createMap(subkey,value);
852      tmp->next=NULL;
853      addMapsToMaps(&_tmpm,tmp);
854      freeMaps(&tmp);
855      free(tmp);
856    }
857  }else{
858    maps *tmp=(maps*)malloc(MAPS_SIZE);
859    tmp->name=zStrdup(key);
860    tmp->content=createMap(subkey,value);
861    tmp->next=NULL;
862    addMapsToMaps(&m,tmp);
863    freeMaps(&tmp);
864    free(tmp);
865  }
866}
867
868/**
869 * Dump an elements on stderr
870 *
871 * @param e the elements to dump
872 */
873void dumpElements(elements* e){
874  elements* tmp=e;
875  while(tmp!=NULL){
876    fprintf(stderr,"ELEMENT [%s]\n",tmp->name);
877    fprintf(stderr," > CONTENT [%s]\n",tmp->name);
878    dumpMap(tmp->content);
879    fprintf(stderr," > METADATA [%s]\n",tmp->name);
880    dumpMap(tmp->metadata);
881    fprintf(stderr," > FORMAT [%s]\n",tmp->format);
882    iotype* tmpio=tmp->defaults;
883    int ioc=0;
884    while(tmpio!=NULL){
885      fprintf(stderr," > DEFAULTS [%s] (%i)\n",tmp->name,ioc);
886      dumpMap(tmpio->content);
887      tmpio=tmpio->next;
888      ioc++;
889    }
890    tmpio=tmp->supported;
891    ioc=0;
892    while(tmpio!=NULL){
893      fprintf(stderr," > SUPPORTED [%s] (%i)\n",tmp->name,ioc);
894      dumpMap(tmpio->content);
895      tmpio=tmpio->next;
896      ioc++;
897    }
898    fprintf(stderr,"------------------\n");
899    tmp=tmp->next;
900  }
901}
902
903/**
904 * Dump an elements on stderr using the YAML syntaxe
905 *
906 * @param e the elements to dump
907 */
908void dumpElementsAsYAML(elements* e){
909  elements* tmp=e;
910  int i;
911  while(tmp!=NULL){
912    for(i=0;i<2;i++)
913      fprintf(stderr," ");
914    fprintf(stderr,"%s:\n",tmp->name);
915    map* mcurs=tmp->content;
916    while(mcurs!=NULL){
917      for(i=0;i<4;i++)
918        fprintf(stderr," ");
919      _dumpMap(mcurs);
920      mcurs=mcurs->next;
921    }
922    mcurs=tmp->metadata;
923    if(mcurs!=NULL){
924      for(i=0;i<4;i++)
925        fprintf(stderr," ");
926      fprintf(stderr,"MetaData:\n");
927      while(mcurs!=NULL){
928        for(i=0;i<6;i++)
929          fprintf(stderr," ");
930        _dumpMap(mcurs);
931        mcurs=mcurs->next;
932      }
933    }
934    for(i=0;i<4;i++)
935      fprintf(stderr," ");
936    fprintf(stderr,"%s:\n",tmp->format);
937    iotype* tmpio=tmp->defaults;
938    int ioc=0;
939    while(tmpio!=NULL){
940      for(i=0;i<6;i++)
941        fprintf(stderr," ");
942      fprintf(stderr,"default:\n");
943      mcurs=tmpio->content;
944      while(mcurs!=NULL){
945        for(i=0;i<8;i++)
946          fprintf(stderr," ");
947        if(strcasecmp(mcurs->name,"range")==0){
948          fprintf(stderr,"range: \"%s\"\n",mcurs->value);
949        }else
950          _dumpMap(mcurs);
951        mcurs=mcurs->next;
952      }
953      tmpio=tmpio->next;
954      ioc++;
955    }
956    tmpio=tmp->supported;
957    ioc=0;
958    while(tmpio!=NULL){
959      for(i=0;i<6;i++)
960        fprintf(stderr," ");
961      fprintf(stderr,"supported:\n");
962      mcurs=tmpio->content;
963      while(mcurs!=NULL){
964        for(i=0;i<8;i++)
965          fprintf(stderr," ");
966        if(strcasecmp(mcurs->name,"range")==0){
967          fprintf(stderr,"range: \"%s\"\n",mcurs->value);
968        }else
969          _dumpMap(mcurs);
970        mcurs=mcurs->next;
971      }
972      tmpio=tmpio->next;
973      ioc++;
974    }
975    tmp=tmp->next;
976  }
977}
978
979/**
980 * Duplicate an elements
981 *
982 * @param e the elements to clone
983 * @return the allocated elements containing a copy of the elements e
984 */
985elements* dupElements(elements* e){
986  elements* cursor=e;
987  elements* tmp=NULL;
988  if(cursor!=NULL){
989#ifdef DEBUG
990    fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
991    dumpElements(e);
992    fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
993#endif
994    tmp=(elements*)malloc(ELEMENTS_SIZE);
995    tmp->name=zStrdup(e->name);
996    tmp->content=NULL;
997    addMapToMap(&tmp->content,e->content);
998    tmp->metadata=NULL;
999    addMapToMap(&tmp->metadata,e->metadata);
1000    if(e->format!=NULL)
1001      tmp->format=zStrdup(e->format);
1002    else
1003      tmp->format=NULL;
1004    if(e->defaults!=NULL){
1005      tmp->defaults=(iotype*)malloc(IOTYPE_SIZE);
1006      tmp->defaults->content=NULL;
1007      addMapToMap(&tmp->defaults->content,e->defaults->content);
1008      tmp->defaults->next=NULL;
1009#ifdef DEBUG
1010      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
1011      dumpMap(tmp->defaults->content);
1012#endif
1013    }else
1014      tmp->defaults=NULL;
1015    if(e->supported!=NULL){
1016      tmp->supported=(iotype*)malloc(IOTYPE_SIZE);
1017      tmp->supported->content=NULL;
1018      addMapToMap(&tmp->supported->content,e->supported->content);
1019      tmp->supported->next=NULL;
1020      iotype *tmp2=e->supported->next;
1021      while(tmp2!=NULL){
1022        addMapToIoType(&tmp->supported,tmp2->content);
1023#ifdef DEBUG
1024        fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
1025        dumpMap(tmp->defaults->content);
1026#endif
1027        tmp2=tmp2->next;
1028      }
1029    }
1030    else
1031      tmp->supported=NULL;
1032    tmp->next=dupElements(cursor->next);
1033  }
1034  return tmp;
1035}
1036
1037/**
1038 * Add an elements to another elements.
1039 *
1040 * @see dupElements
1041 * @param m the elements to add the e
1042 * @param e the elements to be added to m
1043 */
1044void addToElements(elements** m,elements* e){
1045  elements* tmp=e;
1046  if(*m==NULL){
1047    *m=dupElements(tmp);
1048  }else{
1049    addToElements(&(*m)->next,tmp);
1050  }
1051}
1052 
1053/**
1054 * Dump a service on stderr
1055 *
1056 * @param s the service to dump
1057 */
1058void dumpService(service* s){
1059  if(s==NULL)
1060    return;
1061  fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name);
1062  if(s->content!=NULL){
1063    fprintf(stderr,"CONTENT MAP\n");
1064    dumpMap(s->content);
1065    fprintf(stderr,"CONTENT METADATA\n");
1066    dumpMap(s->metadata);
1067  }
1068  if(s->inputs!=NULL){
1069    fprintf(stderr,"INPUT ELEMENTS [%s]\n------------------\n",s->name);
1070    dumpElements(s->inputs);
1071  }
1072  if(s->outputs!=NULL){
1073    fprintf(stderr,"OUTPUT ELEMENTS [%s]\n------------------\n",s->name);
1074    dumpElements(s->outputs);
1075  }
1076  fprintf(stderr,"++++++++++++++++++\n");
1077}
1078
1079/**
1080 * Dump a service on stderr using the YAML syntaxe
1081 *
1082 * @param s the service to dump
1083 */
1084void dumpServiceAsYAML(service* s){
1085  int i;
1086  fprintf(stderr,"# %s\n\n",s->name);
1087  if(s->content!=NULL){
1088    map* mcurs=s->content;
1089    dumpMap(mcurs);
1090    mcurs=s->metadata;
1091    if(mcurs!=NULL){
1092      fprintf(stderr,"MetaData:\n");
1093      while(mcurs!=NULL){
1094        for(i=0;i<2;i++)
1095          fprintf(stderr," ");
1096        _dumpMap(mcurs);
1097        mcurs=mcurs->next;
1098      }
1099    }
1100  }
1101  if(s->inputs!=NULL){
1102    fprintf(stderr,"\ninputs:\n");
1103    dumpElementsAsYAML(s->inputs);
1104  }
1105  if(s->outputs!=NULL){
1106    fprintf(stderr,"\noutputs:\n");
1107    dumpElementsAsYAML(s->outputs);
1108  }
1109}
1110
1111/**
1112 * Duplicate a service
1113 *
1114 * @param s the service to clone
1115 * @return the allocated service containing a copy of the serfvice s
1116 */
1117service* dupService(service* s){
1118  service *res=(service*)malloc(SERVICE_SIZE);
1119  res->name=zStrdup(s->name);
1120  res->content=NULL;
1121  addMapToMap(&res->content,s->content);
1122  res->metadata=NULL;
1123  addMapToMap(&res->metadata,s->metadata);
1124  res->inputs=dupElements(s->inputs);
1125  res->outputs=dupElements(s->outputs);
1126  return res;
1127}
1128
1129/**
1130 * Print the registry on stderr.
1131 *
1132 * @param r the registry
1133 */
1134void dumpRegistry(registry* r){
1135  registry* p=r;
1136  while(p!=NULL){
1137    fprintf(stderr,"%s \n",p->name);
1138    services* s=p->content;
1139    s=p->content;
1140    while(s!=NULL){
1141      dumpService(s->content);
1142      s=s->next;
1143    }
1144    p=p->next;
1145  }
1146}
1147
1148/**
1149 * Add a service to the registry
1150 *
1151 * @param reg the resgitry to add the service
1152 * @param name the registry name to update
1153 * @param content the service to add
1154 */
1155bool addServiceToRegistry(registry** reg,char* name,service* content){
1156  registry *l=*reg;
1157  int isInitial=-1;
1158  if(l==NULL){
1159    l=(registry*)malloc(REGISTRY_SIZE);
1160    isInitial=1;
1161  }
1162  if(l!=NULL){
1163    int hasLevel=-1;
1164    while(isInitial<0 && l!=NULL){
1165      if(l->name!=NULL && strcasecmp(name,l->name)==0){
1166        hasLevel=1;
1167        break;
1168      }
1169      l=l->next;
1170    }
1171    if(hasLevel<0){
1172      if(isInitial<0)
1173        l=(registry*)malloc(REGISTRY_SIZE);
1174      l->name=zStrdup(name);
1175      l->content=NULL;
1176      l->next=NULL;
1177    }
1178    if(l->content==NULL){
1179      l->content=(services*)malloc(SERVICES_SIZE);
1180      l->content->content=dupService(content);
1181      l->content->next=NULL;
1182    }
1183    else{
1184      services* s=l->content;
1185      while(s->next!=NULL)
1186        s=s->next;
1187      s->next=(services*)malloc(SERVICES_SIZE);
1188      s->next->content=dupService(content);
1189      s->next->next=NULL;
1190    }
1191    l->next=NULL;
1192    if(isInitial>0)
1193      *reg=l;
1194    else{
1195      registry *r=*reg;
1196      while(r->next!=NULL)
1197        r=r->next;
1198      r->next=l;
1199      r->next->next=NULL;
1200    }
1201    return true;
1202  }
1203  else
1204    return false;
1205}
1206
1207/**
1208 * Free memory allocated for the registry
1209 *
1210 * @param r the registry
1211 */
1212void freeRegistry(registry** r){
1213  registry* lr=*r;
1214  while(lr!=NULL){
1215    services* s=lr->content;
1216    free(lr->name);
1217    while(s!=NULL){
1218      service* s1=s->content;
1219      s=s->next;
1220      if(s1!=NULL){
1221        freeService(&s1);
1222        free(s1);
1223        s1=NULL;
1224      }
1225    }
1226    lr=lr->next;
1227  }   
1228}
1229
1230/**
1231 * Access a service in the registry
1232 *
1233 * @param r the registry
1234 * @param level the regitry to search ("concept", "generic" or "implementation")
1235 * @param sname the service name
1236 * @return the service pointer if a corresponding service was found or NULL
1237 */
1238service* getServiceFromRegistry(registry* r,char  *level,char* sname){
1239  registry *lr=r;
1240  while(lr!=NULL){
1241    if(strcasecmp(lr->name,level)==0){
1242      services* s=lr->content;
1243      while(s!=NULL){
1244        if(s->content!=NULL && strcasecmp(s->content->name,sname)==0)
1245          return s->content;
1246        s=s->next;
1247      }
1248      break;
1249    }
1250    lr=lr->next;
1251  }
1252  return NULL;
1253}
1254
1255/**
1256 * Apply inheritance to an out map from a reference in map
1257 *
1258 * @param out the map to update
1259 * @param in the reference map (containing inherited properties)
1260 */
1261void inheritMap(map** out,map* in){
1262  map* content=in;
1263  while(content!=NULL && *out!=NULL){
1264    map* cmap=getMap(*out,content->name);
1265    if(cmap==NULL)
1266      addToMap(*out,content->name,content->value);
1267    content=content->next;
1268  }
1269}
1270
1271/**
1272 * Apply inheritance to an out iotype from a reference in iotype
1273 *
1274 * @param out the iotype to update
1275 * @param in the reference iotype (containing inherited properties)
1276 */
1277void inheritIOType(iotype** out,iotype* in){
1278  iotype* io=in;
1279  iotype* oio=*out;
1280  if(io!=NULL){
1281    if(*out==NULL){
1282      *out=(iotype*)malloc(IOTYPE_SIZE);
1283      (*out)->content=NULL;
1284      addMapToMap(&(*out)->content,io->content);
1285      (*out)->next=NULL;
1286      oio=*out;
1287      inheritIOType(&oio->next,io->next);
1288    }else{
1289      inheritIOType(&oio->next,io->next);
1290    }
1291  }
1292}
1293
1294/**
1295 * Apply inheritance to an out elements from a reference in elements
1296 *
1297 * @param out the elements to update
1298 * @param in the reference elements (containing inherited properties)
1299 */
1300void inheritElements(elements** out,elements* in){
1301  elements* content=in;
1302  while(content!=NULL && *out!=NULL){
1303    elements* cmap=getElements(*out,content->name);
1304    if(cmap==NULL)
1305      addToElements(out,content);
1306    else{
1307      inheritMap(&cmap->content,content->content);
1308      inheritMap(&cmap->metadata,content->metadata);
1309      if(cmap->format==NULL && content->format!=NULL)
1310        cmap->format=zStrdup(content->format);
1311      inheritIOType(&cmap->defaults,content->defaults);
1312      if(cmap->supported==NULL)
1313        inheritIOType(&cmap->supported,content->supported);
1314      else{
1315        iotype* p=content->supported;
1316        while(p!=NULL){
1317          addMapToIoType(&cmap->supported,p->content);
1318          p=p->next;
1319        }
1320      }
1321    }
1322    content=content->next;
1323  }
1324}
1325
1326/**
1327 * Apply inheritance to a service based on a registry
1328 *
1329 * @param r the registry storing profiles hierarchy
1330 * @param s the service to update depending on its inheritance
1331 */
1332void inheritance(registry *r,service** s){
1333  if(r==NULL)
1334    return;
1335  service* ls=*s;
1336  if(ls->content==NULL)
1337    return;
1338  map* profile=getMap(ls->content,"extend");
1339  map* level=getMap(ls->content,"level");
1340  if(profile!=NULL&&level!=NULL){
1341    service* s1;
1342    if(strncasecmp(level->value,"profile",7)==0)
1343      s1=getServiceFromRegistry(r,(char*)"generic",profile->value);
1344    else
1345      s1=getServiceFromRegistry(r,level->value,profile->value);
1346     
1347    inheritMap(&ls->content,s1->content);
1348    inheritMap(&ls->metadata,s1->metadata);
1349    if(ls->inputs==NULL && s1->inputs!=NULL){
1350      ls->inputs=dupElements(s1->inputs);
1351    }else{
1352      inheritElements(&ls->inputs,s1->inputs);
1353    }
1354    if(ls->outputs==NULL && s1->outputs!=NULL){
1355      ls->outputs=dupElements(s1->outputs);
1356    }else
1357      inheritElements(&ls->outputs,s1->outputs);
1358  }
1359}
1360
1361/**
1362 * Convert a maps to a char*** (only used for Fortran support)
1363 *
1364 * @param m the maps to convert
1365 * @param c the resulting array
1366 */
1367void mapsToCharXXX(maps* m,char*** c){
1368  maps* tm=m;
1369  int i=0;
1370  int j=0;
1371  char tmp[10][30][1024];
1372  memset(tmp,0,1024*10*10);
1373  while(tm!=NULL){
1374    if(i>=10)
1375      break;
1376    strcpy(tmp[i][j],"name");
1377    j++;
1378    strcpy(tmp[i][j],tm->name);
1379    j++;
1380    map* tc=tm->content;
1381    while(tc!=NULL){
1382      if(j>=30)
1383        break;
1384      strcpy(tmp[i][j],tc->name);
1385      j++;
1386      strcpy(tmp[i][j],tc->value);
1387      j++;
1388      tc=tc->next;
1389    }
1390    tm=tm->next;
1391    j=0;
1392    i++;
1393  }
1394  memcpy(c,tmp,10*10*1024);
1395}
1396
1397/**
1398 * Convert a char*** to a maps (only used for Fortran support)
1399 *
1400 * @param c the array to convert
1401 * @param m the resulting maps
1402 */
1403void charxxxToMaps(char*** c,maps**m){
1404  maps* trorf=*m;
1405  int i,j;
1406  char tmp[10][30][1024];
1407  memcpy(tmp,c,10*30*1024);
1408  for(i=0;i<10;i++){
1409    if(strlen(tmp[i][1])==0)
1410      break;
1411    trorf->name=tmp[i][1];
1412    trorf->content=NULL;
1413    trorf->next=NULL;
1414    for(j=2;j<29;j+=2){
1415      if(strlen(tmp[i][j+1])==0)
1416        break;
1417      if(trorf->content==NULL)
1418        trorf->content=createMap(tmp[i][j],tmp[i][j+1]);
1419      else
1420        addToMap(trorf->content,tmp[i][j],tmp[i][j+1]);
1421    }
1422    trorf=trorf->next;
1423  }
1424  m=&trorf;
1425}
1426
1427#ifdef WIN32
1428char* getMapsAsKVP(maps* m,int length,int type){
1429  char *dataInputsKVP=(char*) malloc(length*sizeof(char));
1430  char *dataInputsKVPi=NULL;
1431  maps* curs=m;
1432  int i=0;
1433  while(curs!=NULL){
1434    map *inRequest=getMap(curs->content,"inRequest");
1435    map *hasLength=getMap(curs->content,"length");
1436    if((inRequest!=NULL && strncasecmp(inRequest->value,"true",4)==0) ||
1437       inRequest==NULL){
1438      if(i==0)
1439        if(type==0){
1440          sprintf(dataInputsKVP,"%s=",curs->name);
1441          if(hasLength!=NULL){
1442            dataInputsKVPi=(char*)malloc((strlen(curs->name)+2)*sizeof(char));
1443            sprintf(dataInputsKVPi,"%s=",curs->name);
1444          }
1445        }
1446        else
1447          sprintf(dataInputsKVP,"%s",curs->name);
1448      else{
1449        char *temp=zStrdup(dataInputsKVP);
1450        if(type==0)
1451          sprintf(dataInputsKVP,"%s;%s=",temp,curs->name);
1452        else
1453          sprintf(dataInputsKVP,"%s;%s",temp,curs->name);
1454      }
1455      map* icurs=curs->content;
1456      if(type==0){
1457        char *temp=zStrdup(dataInputsKVP);
1458        if(getMap(curs->content,"xlink:href")!=NULL)
1459          sprintf(dataInputsKVP,"%sReference",temp);
1460        else{
1461          if(hasLength!=NULL){
1462            int j;
1463            for(j=0;j<atoi(hasLength->value);j++){
1464              map* tmp0=getMapArray(curs->content,"value",j);
1465              if(j==0)
1466                free(temp);
1467              temp=zStrdup(dataInputsKVP);
1468              if(j==0)
1469                sprintf(dataInputsKVP,"%s%s",temp,tmp0->value);
1470              else
1471                sprintf(dataInputsKVP,"%s;%s%s",temp,dataInputsKVPi,tmp0->value);
1472            }
1473          }
1474          else
1475            sprintf(dataInputsKVP,"%s%s",temp,icurs->value);
1476        }
1477        free(temp);
1478      }
1479      while(icurs!=NULL){
1480        if(strncasecmp(icurs->name,"value",5)!=0 &&
1481           strncasecmp(icurs->name,"mimeType_",9)!=0 &&
1482           strncasecmp(icurs->name,"dataType_",9)!=0 &&
1483           strncasecmp(icurs->name,"size",4)!=0 &&
1484           strncasecmp(icurs->name,"length",4)!=0 &&
1485           strncasecmp(icurs->name,"isArray",7)!=0 &&
1486           strcasecmp(icurs->name,"Reference")!=0 &&
1487           strcasecmp(icurs->name,"minOccurs")!=0 &&
1488           strcasecmp(icurs->name,"maxOccurs")!=0 &&
1489           strncasecmp(icurs->name,"fmimeType",9)!=0 &&
1490           strcasecmp(icurs->name,"inRequest")!=0){
1491          char *itemp=zStrdup(dataInputsKVP);
1492          if(strcasecmp(icurs->name,"xlink:href")!=0)
1493            sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,icurs->value);
1494          else
1495            sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,url_encode(icurs->value));
1496          free(itemp);
1497        }
1498        icurs=icurs->next;
1499      }
1500    }
1501    curs=curs->next;
1502    i++;
1503  }
1504  return dataInputsKVP;
1505}
1506#endif
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

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