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

Last change on this file since 757 was 757, checked in by djay, 8 years ago

Update the makefiles and small fixe for windows platform.

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