Index: trunk/zoo-kernel/service.h
===================================================================
--- trunk/zoo-kernel/service.h	(revision 1)
+++ trunk/zoo-kernel/service.h	(revision 9)
@@ -40,4 +40,5 @@
 #define true 1
 #define false -1
+
 #define SERVICE_ACCEPTED 0
 #define SERVICE_STARTED 1
@@ -45,10 +46,13 @@
 #define SERVICE_SUCCEEDED 3
 #define SERVICE_FAILED 4
+
 #define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*2)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+sizeof(elements*))
-#define MAP_SIZE (2*sizeof(char*))+sizeof(map*)
-#define MAPS_SIZE (2*sizeof(char*))+sizeof(map*)+sizeof(maps*)
-
-
-  static char* mtoupper(char* str){
+#define MAP_SIZE (2*sizeof(char*))+sizeof(NULL)
+#define IOTYPE_SIZE MAP_SIZE+sizeof(NULL)
+#define MAPS_SIZE (2*sizeof(char*))+sizeof(map*)+MAP_SIZE
+#define SERVICE_SIZE (ELEMENTS_SIZE*2)+(MAP_SIZE*2)+sizeof(char*)
+
+
+  /*  static char* mtoupper(char* str){
     char* tmp=strdup(str);
     if(tmp){
@@ -63,16 +67,28 @@
       tmp[0]=0;
     return tmp;
-  }
-
+    }*/
+
+  /**
+   * \struct maps
+   * \brief linked list of map pointer
+   *
+   * Small object to store WPS KVP set.
+   */
   typedef struct maps{
-    char* name;
-    struct map* content;
-    struct maps* next;
+    char* name;          
+    struct map* content; 
+    struct maps* next;   
   } maps;
 
+  /**
+   * \struct map
+   * \brief KVP linked list
+   *
+   * Deal with WPS KVP (name,value).
+   */
   typedef struct map{
-    char* name;
-    char* value;
-    struct map* next;
+    char* name;       /* The key */
+    char* value;      /* The value */
+    struct map* next; /* Next couple */
   } map;
 
@@ -83,5 +99,5 @@
 #endif
 
-  static void* _dumpMap(map* t){
+  static void _dumpMap(map* t){
     if(t!=NULL){
       fprintf(stderr,"[%s] => [%s] \n",t->name,t->value);
@@ -91,8 +107,7 @@
       fflush(stderr);
     }
-    return NULL;
-  }
-
-  static void* dumpMap(map* t){
+  }
+
+  static void dumpMap(map* t){
     map* tmp=t;
     while(tmp!=NULL){
@@ -100,5 +115,4 @@
       tmp=tmp->next;
     }
-    return NULL;
   }
 
@@ -112,5 +126,5 @@
   }
 
-  static map* createMap(char* name,char* value){
+  static map* createMap(const char* name,const char* value){
     map* tmp=(map *)malloc(MAP_SIZE);
     tmp->name=strdup(name);
@@ -130,8 +144,8 @@
   }
     
-  static bool hasKey(map* m,char *key){
+  static bool hasKey(map* m,const char *key){
     map* tmp=m;
     while(tmp!=NULL){
-      if(strcmp(mtoupper(tmp->name),mtoupper(key))==0)
+      if(strncasecmp(tmp->name,key,strlen(key))==0)
 	return true;
       tmp=tmp->next;
@@ -143,9 +157,10 @@
   }
 
-  static maps* getMaps(maps* m,char *key){
+  static maps* getMaps(maps* m,const char *key){
     maps* tmp=m;
     while(tmp!=NULL){
-      if(strcmp(mtoupper(tmp->name),mtoupper(key))==0)
+      if(strncasecmp(tmp->name,key,strlen(key))==0){
 	return tmp;
+      }
       tmp=tmp->next;
     }
@@ -153,9 +168,10 @@
   }
 
-  static map* getMap(map* m,char *key){
+  static map* getMap(map* m,const char *key){
     map* tmp=m;
     while(tmp!=NULL){
-      if(strcmp(mtoupper(tmp->name),mtoupper(key))==0)
+      if(strncasecmp(tmp->name,key,strlen(key))==0){
 	return tmp;
+      }
       tmp=tmp->next;
     }
@@ -166,13 +182,16 @@
     maps* _tmpm=getMaps(m,key);
     if(_tmpm!=NULL){
-      map* tmpm=getMap(_tmpm->content,mtoupper(subkey));
-      return tmpm;
+      map* _ztmpm=getMap(_tmpm->content,subkey);
+      return _ztmpm;
     }
     else return NULL;
   }
 
-  static void* freeMap(map** mo){
+  static void freeMap(map** mo){
     map* _cursor=*mo;
     if(_cursor!=NULL){
+#ifdef DEBUG
+      fprintf(stderr,"freeMap\n");
+#endif
       free(_cursor->name);
       free(_cursor->value);
@@ -182,7 +201,24 @@
       }
     }
-	return NULL;
-  }
-
+  }
+
+  static void freeMaps(maps** mo){
+    maps* _cursor=*mo;
+    fflush(stderr);
+    if(_cursor && _cursor!=NULL){
+#ifdef DEBUG
+      fprintf(stderr,"freeMaps\n");
+#endif
+      free(_cursor->name);
+      if(_cursor->content!=NULL){
+      	freeMap(&_cursor->content);
+      	free(_cursor->content);
+      }
+      if(_cursor->next!=NULL){
+	freeMaps(&_cursor->next);
+	free(_cursor->next);
+      }
+    }
+  }
 
   typedef struct iotype{
@@ -217,7 +253,5 @@
     elements* tmp=e;
     while(tmp!=NULL){
-      mtoupper(key);
-      mtoupper(tmp->name);
-      if(strcmp(key,tmp->name)==0)
+      if(strncasecmp(key,tmp->name,strlen(key))==0)
 	return true;
       tmp=tmp->next;
@@ -229,7 +263,5 @@
     elements* tmp=m;
     while(tmp!=NULL){
-      mtoupper(tmp->name);
-      mtoupper(key);
-      if(strcmp(tmp->name,key)==0)
+      if(strncasecmp(tmp->name,key,strlen(tmp->name))==0)
 	return tmp;
       tmp=tmp->next;
@@ -239,13 +271,14 @@
 
 
-  static void* freeIOType(iotype** i){
+  static void freeIOType(iotype** i){
     iotype* _cursor=*i;
     if(_cursor!=NULL){
       freeMap(&_cursor->content);
       free(_cursor->content);
-      freeIOType(&_cursor->next);
-      free(_cursor->next);
-    }
-	return NULL;
+      if(_cursor->next!=NULL){
+	freeIOType(&_cursor->next);
+	free(_cursor->next);
+      }
+    }
   }
 
@@ -253,41 +286,25 @@
     elements* tmp=*e;
     if(tmp!=NULL){
-#ifdef DEBUG
-      fprintf(stderr,"FREE 1");
-#endif
       free(tmp->name);
       freeMap(&tmp->content);
-#ifdef DEBUG
-      fprintf(stderr,"FREE 2");
-#endif
       free(tmp->content);
       freeMap(&tmp->metadata);
-#ifdef DEBUG
-      fprintf(stderr,"FREE 3");
-#endif
       free(tmp->metadata);
-#ifdef DEBUG
-      fprintf(stderr,"FREE 4");
-#endif
       free(tmp->format);
       freeIOType(&tmp->defaults);
-#ifdef DEBUG
-      fprintf(stderr,"FREE 5");
-#endif
       if(tmp->defaults!=NULL)
 	free(tmp->defaults);
       freeIOType(&tmp->supported);
-#ifdef DEBUG
-      fprintf(stderr,"FREE 6");
-#endif
       if(tmp->supported!=NULL)
 	free(tmp->supported);
-      tmp=tmp->next;
-    }
-  }
-
-  static void* freeService(service** s){
+      freeElements(&tmp->next);
+    }
+  }
+
+  static void freeService(service** s){
     service* tmp=*s;
     if(tmp!=NULL){
+      if(tmp->name!=NULL)
+	free(tmp->name);
       freeMap(&tmp->content);
       if(tmp->content!=NULL)
@@ -305,58 +322,5 @@
   }
 
-  static void* addMapToMap(map** mo,map* mi){
-    map* tmp=mi;
-    map* _cursor=*mo;
-    while(tmp!=NULL){
-      if(_cursor==NULL){
-	*mo=(map*)malloc(MAP_SIZE);
-	*mo=createMap(tmp->name,tmp->value);
-	_cursor=*mo;
-	_cursor->next=NULL;
-      }
-      else{
-	while(_cursor->next!=NULL)
-	  _cursor=_cursor->next;
-	_cursor->next=createMap(tmp->name,tmp->value);
-      }
-      tmp=tmp->next;
-    }
-    return NULL;
-  }
-
-  static void* addMapsToMaps(maps** mo,maps* mi){
-    maps* tmp=mi;
-    maps* _cursor=*mo;
-    while(tmp!=NULL){
-      if(_cursor==NULL){
-	*mo=(maps*)malloc(MAP_SIZE);
-	*mo=mi;
-	_cursor=*mo;
-      }
-      else{
-	while(_cursor->next!=NULL)
-	  _cursor=_cursor->next;
-	_cursor->next=tmp;
-      }
-      tmp=tmp->next;
-    }
-    return NULL;
-  }
-
-  static void* addMapToIotype(iotype** io,map* mi){
-    iotype* tmp=*io;
-    while(tmp!=NULL){
-      fprintf(stderr,">> CURRENT MAP");
-      dumpMap(tmp->content);
-      tmp=tmp->next;
-    }
-    tmp=(iotype*)malloc(ELEMENTS_SIZE);
-    tmp->content=NULL;
-    tmp->next=NULL;
-    addMapToMap(&tmp->content,mi);
-    return NULL;
-  }
-
-  static void* addToMap(map* m,char* n,char* v){
+  static void addToMap(map* m,const char* n,const char* v){
     if(hasKey(m,n)==false){
       map* _cursor=m;
@@ -367,11 +331,115 @@
     else{
       map *tmp=getMap(m,n);
+      if(tmp->value!=NULL)
+      	free(tmp->value);
       tmp->value=strdup(v);
     }
-    return NULL;
-  }
-
-
-  static void* dumpElements(elements* e){
+  }
+
+  static void addMapToMap(map** mo,map* mi){
+    map* tmp=mi;
+    map* _cursor=*mo;
+    if(tmp==NULL){
+      if(_cursor!=NULL){
+	while(_cursor!=NULL)
+	  _cursor=_cursor->next;
+	_cursor=NULL;
+      }else
+	*mo=NULL;
+    }
+    while(tmp!=NULL){
+      if(_cursor==NULL){
+	if(*mo==NULL)
+	  *mo=createMap(tmp->name,tmp->value);
+	else
+	  addToMap(*mo,tmp->name,tmp->value);
+      }
+      else{
+#ifdef DEBUG
+	fprintf(stderr,"_CURSOR\n");
+	dumpMap(_cursor);
+#endif
+	while(_cursor!=NULL)
+	  _cursor=_cursor->next;
+	_cursor=createMap(tmp->name,tmp->value);
+	_cursor->next=NULL;
+      }
+      tmp=tmp->next;
+#ifdef DEBUG
+      fprintf(stderr,"MO\n");
+      dumpMap(*mo);
+#endif
+    }
+  }
+
+  static void addMapToIoType(iotype** io,map* mi){
+    iotype* tmp=*io;
+    while(tmp!=NULL){
+#ifdef DEBUG
+      fprintf(stderr,">> CURRENT MAP");
+      dumpMap(tmp->content);
+#endif
+      tmp=tmp->next;
+    }
+#ifdef DEBUG
+    fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
+    fflush(stderr);
+#endif
+    tmp=(iotype*)malloc(IOTYPE_SIZE);
+#ifdef DEBUG
+    fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
+    fflush(stderr);
+#endif
+    tmp->content=NULL;
+#ifdef DEBUG
+    fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
+    fflush(stderr);
+#endif
+    addMapToMap(&tmp->content,mi);
+#ifdef DEBUG
+    fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
+    fflush(stderr);
+#endif
+    dumpMap(tmp->content);
+    tmp->next=NULL;
+  }
+
+
+  static maps* dupMaps(maps** mo){
+    maps* _cursor=*mo;
+    maps* res=NULL;
+    if(_cursor!=NULL){
+      res=(maps*)malloc(MAPS_SIZE);
+      res->name=strdup(_cursor->name);
+      res->content=NULL;
+      res->next=NULL;
+      map* mc=_cursor->content;
+      if(mc!=NULL){
+	addMapToMap(&res->content,mc);
+      }
+      res->next=dupMaps(&_cursor->next);
+    }
+    return res;
+  }
+
+  static void addMapsToMaps(maps** mo,maps* mi){
+    maps* tmp=mi;
+    maps* _cursor=*mo;
+    while(tmp!=NULL){
+      if(_cursor==NULL){
+	*mo=dupMaps(&mi);
+	(*mo)->next=NULL;
+      }
+      else{
+	while(_cursor->next!=NULL)
+	  _cursor=_cursor->next;
+	_cursor->next=dupMaps(&tmp);
+      }
+      tmp=tmp->next;
+    }
+  }
+
+
+  static void dumpElements(elements* e){
     elements* tmp=e;
     while(tmp!=NULL){
@@ -401,14 +469,16 @@
       tmp=tmp->next;
     }
-    return NULL;
-  }
-
+  }
 
   static elements* dupElements(elements* e){
-    if(e!=NULL){
-#ifdef DEBUG
+    elements* cursor=e;
+    elements* tmp=NULL;
+    if(cursor!=NULL){
+#ifdef DEBUG
+      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
       dumpElements(e);
-#endif
-      elements* tmp=(elements*)malloc(ELEMENTS_SIZE);
+      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
+#endif
+      tmp=(elements*)malloc(ELEMENTS_SIZE);
       tmp->name=strdup(e->name);
       tmp->content=NULL;
@@ -418,45 +488,54 @@
       tmp->format=strdup(e->format);
       if(e->defaults!=NULL){
-	tmp->defaults=(iotype*)malloc(MAP_SIZE);
+	tmp->defaults=(iotype*)malloc(IOTYPE_SIZE);
 	tmp->defaults->content=NULL;
+	addMapToMap(&tmp->defaults->content,e->defaults->content);
 	tmp->defaults->next=NULL;
-	addMapToMap(&tmp->defaults->content,e->defaults->content);
-	while(e->defaults->next!=NULL){
-	  tmp->defaults->next=(iotype*)malloc(MAP_SIZE);
-	  addMapToMap(&tmp->defaults->next->content,e->defaults->content);
-	}
-      }
+#ifdef DEBUG
+	fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
+	dumpMap(tmp->defaults->content);
+#endif
+      }else
+	tmp->defaults=NULL;
       if(e->supported!=NULL){
-	tmp->supported=(iotype*)malloc(MAP_SIZE);
+	tmp->supported=(iotype*)malloc(IOTYPE_SIZE);
 	tmp->supported->content=NULL;
 	tmp->supported->next=NULL;
 	addMapToMap(&tmp->supported->content,e->supported->content);
+	iotype *etmp=*(&tmp->supported->next) ;
 	iotype *tmp2=e->supported->next;
 	while(tmp2!=NULL){
-	  tmp->supported->next=(iotype*)malloc(MAP_SIZE);
-	  addMapToMap(&tmp->supported->next->content,tmp2->content);
+	  etmp=(iotype*)malloc(IOTYPE_SIZE);
+	  etmp->content=NULL;
+	  addMapToMap(&etmp->content,tmp2->content);
+	  etmp->next=NULL;
+#ifdef DEBUG
+	  fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
+	  dumpMap(tmp->defaults->content);
+#endif
 	  tmp2=tmp2->next;
+	  etmp=etmp->next;
 	}
       }
-      tmp->next=NULL;
-      return tmp;
-    }
-    return NULL;
-  }
-
-  static void* addToElements(elements* m,elements* e){
-    elements* _cursor=m;
+      else
+	tmp->supported=NULL;
+      tmp->next=dupElements(cursor->next);
+    }
+    return tmp;
+  }
+
+  static void addToElements(elements** m,elements* e){
+    elements* _cursor=*m;
     elements* tmp=e;
     if(_cursor==NULL){
-      m=dupElements(tmp);
-      return NULL;
-    }
-    while(_cursor->next!=NULL)
-      _cursor=_cursor->next;
-    _cursor->next=dupElements(tmp);
-    return NULL;
-  }
-
-  static void* dumpService(service* s){
+      *m=dupElements(tmp);
+    }else{
+      while(_cursor->next!=NULL)
+	_cursor=_cursor->next;
+      _cursor->next=dupElements(tmp);
+    }
+  }
+
+  static void dumpService(service* s){
     fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name);
     if(s->content!=NULL){
@@ -475,8 +554,7 @@
     }
     fprintf(stderr,"++++++++++++++++++\n");
-    return NULL;
-  }
-
-  static void* mapsToCharXXX(maps* m,char*** c){
+  }
+
+  static void mapsToCharXXX(maps* m,char*** c){
     maps* tm=m;
     int i=0;
@@ -506,8 +584,7 @@
     }
     memcpy(c,tmp,10*10*1024);
-	return NULL;
-  }
-
-  static void* charxxxToMaps(char*** c,maps**m){
+  }
+
+  static void charxxxToMaps(char*** c,maps**m){
     maps* trorf=*m;
     int i,j;
@@ -526,10 +603,9 @@
 	  trorf->content=createMap(tmp[i][j],tmp[i][j+1]);
 	else
-	addMapToMap(&trorf->content,createMap(tmp[i][j],tmp[i][j+1]));
+	  addToMap(trorf->content,tmp[i][j],tmp[i][j+1]);
       }
       trorf=trorf->next;
     }
     m=&trorf;
-    return NULL;
   }
 
