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

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

Various fixes for Windows support: generate uuid using UuidCreate?, pass usid to the created process, call TerminateProcess? on dismiss request.

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