| 1 | /** |
---|
| 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
| 4 | * Copyright (c) 2009-2012 GeoLabs SARL |
---|
| 5 | * |
---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 7 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 8 | * in the Software without restriction, including without limitation the rights |
---|
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 10 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 11 | * furnished to do so, subject to the following conditions: |
---|
| 12 | * |
---|
| 13 | * The above copyright notice and this permission notice shall be included in |
---|
| 14 | * all copies or substantial portions of the Software. |
---|
| 15 | * |
---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 22 | * THE SOFTWARE. |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #ifndef ZOO_SERVICE_H |
---|
| 26 | #define ZOO_SERVICE_H 1 |
---|
| 27 | |
---|
| 28 | #pragma once |
---|
| 29 | |
---|
| 30 | #ifdef WIN32 |
---|
| 31 | #ifndef USE_MS |
---|
| 32 | #define strncasecmp _strnicmp |
---|
| 33 | #define strcasecmp _stricmp |
---|
| 34 | #endif |
---|
| 35 | #ifndef snprintf |
---|
| 36 | #define snprintf sprintf_s |
---|
| 37 | #else |
---|
| 38 | |
---|
| 39 | #endif |
---|
| 40 | #endif |
---|
| 41 | |
---|
| 42 | #ifdef __cplusplus |
---|
| 43 | extern "C" { |
---|
| 44 | #endif |
---|
| 45 | |
---|
| 46 | #ifdef WIN32 |
---|
| 47 | #ifdef USE_MS |
---|
| 48 | #include <mapserver.h> |
---|
| 49 | #endif |
---|
| 50 | #endif |
---|
| 51 | #include <stdlib.h> |
---|
| 52 | #include <ctype.h> |
---|
| 53 | #include <stdio.h> |
---|
| 54 | #include <string.h> |
---|
| 55 | |
---|
| 56 | #ifndef WIN32 |
---|
| 57 | #define bool int |
---|
| 58 | #define true 1 |
---|
| 59 | #define false -1 |
---|
| 60 | #else |
---|
| 61 | //#include <stdbool.h> |
---|
| 62 | #endif |
---|
| 63 | |
---|
| 64 | #define SERVICE_ACCEPTED 0 |
---|
| 65 | #define SERVICE_STARTED 1 |
---|
| 66 | #define SERVICE_PAUSED 2 |
---|
| 67 | #define SERVICE_SUCCEEDED 3 |
---|
| 68 | #define SERVICE_FAILED 4 |
---|
| 69 | |
---|
| 70 | #define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*2)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+sizeof(elements*)) |
---|
| 71 | #define MAP_SIZE (2*sizeof(char*))+sizeof(NULL) |
---|
| 72 | #define IOTYPE_SIZE MAP_SIZE+sizeof(NULL) |
---|
| 73 | #define MAPS_SIZE (2*sizeof(char*))+sizeof(map*)+MAP_SIZE |
---|
| 74 | #define SERVICE_SIZE (ELEMENTS_SIZE*2)+(MAP_SIZE*2)+sizeof(char*) |
---|
| 75 | |
---|
| 76 | #define SHMSZ 27 |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | #ifdef DEBUG_STACK |
---|
| 80 | void debugStack(const char* file,const int line){ |
---|
| 81 | int stack; |
---|
| 82 | fprintf(stderr,"stack %p (%s: %d) \n",&stack,file,line); |
---|
| 83 | } |
---|
| 84 | #endif |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * \struct map |
---|
| 88 | * \brief KVP linked list |
---|
| 89 | * |
---|
| 90 | * Deal with WPS KVP (name,value). |
---|
| 91 | * A map is defined as: |
---|
| 92 | * - name : a key, |
---|
| 93 | * - value: a value, |
---|
| 94 | * - next : a pointer to the next map if any. |
---|
| 95 | */ |
---|
| 96 | typedef struct map{ |
---|
| 97 | char* name; |
---|
| 98 | char* value; |
---|
| 99 | struct map* next; |
---|
| 100 | } map; |
---|
| 101 | |
---|
| 102 | #ifdef WIN32 |
---|
| 103 | #define NULLMAP ((map*) 0) |
---|
| 104 | #else |
---|
| 105 | #define NULLMAP NULL |
---|
| 106 | #endif |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * \struct maps |
---|
| 110 | * \brief linked list of map pointer |
---|
| 111 | * |
---|
| 112 | * Small object to store WPS KVP set. |
---|
| 113 | * Maps is defined as: |
---|
| 114 | * - a name, |
---|
| 115 | * - a content map, |
---|
| 116 | * - a pointer to the next maps if any. |
---|
| 117 | */ |
---|
| 118 | typedef struct maps{ |
---|
| 119 | char* name; |
---|
| 120 | struct map* content; |
---|
| 121 | struct maps* next; |
---|
| 122 | } maps; |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | * \brief Dump a map on stderr |
---|
| 126 | */ |
---|
| 127 | static void _dumpMap(map* t){ |
---|
| 128 | if(t!=NULL){ |
---|
| 129 | fprintf(stderr,"[%s] => [%s] \n",t->name,t->value); |
---|
| 130 | fflush(stderr); |
---|
| 131 | }else{ |
---|
| 132 | fprintf(stderr,"NULL\n"); |
---|
| 133 | fflush(stderr); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | static void dumpMap(map* t){ |
---|
| 138 | map* tmp=t; |
---|
| 139 | while(tmp!=NULL){ |
---|
| 140 | _dumpMap(tmp); |
---|
| 141 | tmp=tmp->next; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | static void dumpMapToFile(map* t,FILE* file){ |
---|
| 146 | map* tmp=t; |
---|
| 147 | while(tmp!=NULL){ |
---|
| 148 | #ifdef DEBUG |
---|
| 149 | fprintf(stderr,"%s = %s\n",tmp->name,tmp->value); |
---|
| 150 | #endif |
---|
| 151 | fprintf(file,"%s = %s\n",tmp->name,tmp->value); |
---|
| 152 | tmp=tmp->next; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | static void dumpMaps(maps* m){ |
---|
| 157 | maps* tmp=m; |
---|
| 158 | while(tmp!=NULL){ |
---|
| 159 | fprintf(stderr,"MAP => [%s] \n",tmp->name); |
---|
| 160 | dumpMap(tmp->content); |
---|
| 161 | tmp=tmp->next; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | static void dumpMapsToFile(maps* m,char* file_path){ |
---|
| 166 | FILE* file=fopen(file_path,"w"); |
---|
| 167 | maps* tmp=m; |
---|
| 168 | if(tmp!=NULL){ |
---|
| 169 | fprintf(file,"[%s]\n",tmp->name); |
---|
| 170 | dumpMapToFile(tmp->content,file); |
---|
| 171 | fflush(file); |
---|
| 172 | } |
---|
| 173 | fclose(file); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | static map* createMap(const char* name,const char* value){ |
---|
| 177 | map* tmp=(map *)malloc(MAP_SIZE); |
---|
| 178 | tmp->name=strdup(name); |
---|
| 179 | tmp->value=strdup(value); |
---|
| 180 | tmp->next=NULL; |
---|
| 181 | return tmp; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | static int count(map* m){ |
---|
| 185 | map* tmp=m; |
---|
| 186 | int c=0; |
---|
| 187 | while(tmp!=NULL){ |
---|
| 188 | c++; |
---|
| 189 | tmp=tmp->next; |
---|
| 190 | } |
---|
| 191 | return c; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | static bool hasKey(map* m,const char *key){ |
---|
| 195 | map* tmp=m; |
---|
| 196 | while(tmp!=NULL){ |
---|
| 197 | if(strcasecmp(tmp->name,key)==0) |
---|
| 198 | return true; |
---|
| 199 | tmp=tmp->next; |
---|
| 200 | } |
---|
| 201 | #ifdef DEBUG_MAP |
---|
| 202 | fprintf(stderr,"NOT FOUND \n"); |
---|
| 203 | #endif |
---|
| 204 | return false; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | static maps* getMaps(maps* m,const char *key){ |
---|
| 208 | maps* tmp=m; |
---|
| 209 | while(tmp!=NULL){ |
---|
| 210 | if(strcasecmp(tmp->name,key)==0){ |
---|
| 211 | return tmp; |
---|
| 212 | } |
---|
| 213 | tmp=tmp->next; |
---|
| 214 | } |
---|
| 215 | return NULL; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | static map* getMap(map* m,const char *key){ |
---|
| 219 | map* tmp=m; |
---|
| 220 | while(tmp!=NULL){ |
---|
| 221 | if(strcasecmp(tmp->name,key)==0){ |
---|
| 222 | return tmp; |
---|
| 223 | } |
---|
| 224 | tmp=tmp->next; |
---|
| 225 | } |
---|
| 226 | return NULL; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | static map* getLastMap(map* m){ |
---|
| 231 | map* tmp=m; |
---|
| 232 | while(tmp!=NULL){ |
---|
| 233 | if(tmp->next==NULL){ |
---|
| 234 | return tmp; |
---|
| 235 | } |
---|
| 236 | tmp=tmp->next; |
---|
| 237 | } |
---|
| 238 | return NULL; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | static map* getMapFromMaps(maps* m,const char* key,const char* subkey){ |
---|
| 242 | maps* _tmpm=getMaps(m,key); |
---|
| 243 | if(_tmpm!=NULL){ |
---|
| 244 | map* _ztmpm=getMap(_tmpm->content,subkey); |
---|
| 245 | return _ztmpm; |
---|
| 246 | } |
---|
| 247 | else return NULL; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | static char* getMapsAsKVP(maps* m,int length,int type){ |
---|
| 251 | char *dataInputsKVP=(char*) malloc(length*sizeof(char)); |
---|
| 252 | maps* curs=m; |
---|
| 253 | int i=0; |
---|
| 254 | while(curs!=NULL){ |
---|
| 255 | if(i==0) |
---|
| 256 | if(type==0) |
---|
| 257 | sprintf(dataInputsKVP,"%s=",curs->name); |
---|
| 258 | else |
---|
| 259 | sprintf(dataInputsKVP,"%s",curs->name); |
---|
| 260 | else{ |
---|
| 261 | char *temp=strdup(dataInputsKVP); |
---|
| 262 | if(type==0) |
---|
| 263 | sprintf(dataInputsKVP,"%s;%s=",temp,curs->name); |
---|
| 264 | else |
---|
| 265 | sprintf(dataInputsKVP,"%s;%s",temp,curs->name); |
---|
| 266 | free(temp); |
---|
| 267 | } |
---|
| 268 | map* icurs=curs->content; |
---|
| 269 | if(type==0){ |
---|
| 270 | map* tmp=getMap(curs->content,"value"); |
---|
| 271 | char *temp=strdup(dataInputsKVP); |
---|
| 272 | if(getMap(m->content,"xlink:href")!=NULL) |
---|
| 273 | sprintf(dataInputsKVP,"%sReference",temp); |
---|
| 274 | else |
---|
| 275 | sprintf(dataInputsKVP,"%s%s",temp,icurs->value); |
---|
| 276 | free(temp); |
---|
| 277 | } |
---|
| 278 | int j=0; |
---|
| 279 | while(icurs!=NULL){ |
---|
| 280 | if(strcasecmp(icurs->name,"value")!=0 && |
---|
| 281 | strcasecmp(icurs->name,"Reference")!=0 && |
---|
| 282 | strcasecmp(icurs->name,"minOccurs")!=0 && |
---|
| 283 | strcasecmp(icurs->name,"maxOccurs")!=0 && |
---|
| 284 | strcasecmp(icurs->name,"inRequest")!=0){ |
---|
| 285 | char *itemp=strdup(dataInputsKVP); |
---|
| 286 | sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,icurs->value); |
---|
| 287 | free(itemp); |
---|
| 288 | } |
---|
| 289 | icurs=icurs->next; |
---|
| 290 | } |
---|
| 291 | curs=curs->next; |
---|
| 292 | i++; |
---|
| 293 | } |
---|
| 294 | return dataInputsKVP; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | |
---|
| 298 | static void freeMap(map** mo){ |
---|
| 299 | map* _cursor=*mo; |
---|
| 300 | if(_cursor!=NULL){ |
---|
| 301 | #ifdef DEBUG |
---|
| 302 | fprintf(stderr,"freeMap\n"); |
---|
| 303 | #endif |
---|
| 304 | free(_cursor->name); |
---|
| 305 | free(_cursor->value); |
---|
| 306 | if(_cursor->next!=NULL){ |
---|
| 307 | freeMap(&_cursor->next); |
---|
| 308 | free(_cursor->next); |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | static void freeMaps(maps** mo){ |
---|
| 314 | maps* _cursor=*mo; |
---|
| 315 | fflush(stderr); |
---|
| 316 | if(_cursor && _cursor!=NULL){ |
---|
| 317 | #ifdef DEBUG |
---|
| 318 | fprintf(stderr,"freeMaps\n"); |
---|
| 319 | #endif |
---|
| 320 | free(_cursor->name); |
---|
| 321 | if(_cursor->content!=NULL){ |
---|
| 322 | freeMap(&_cursor->content); |
---|
| 323 | free(_cursor->content); |
---|
| 324 | } |
---|
| 325 | if(_cursor->next!=NULL){ |
---|
| 326 | freeMaps(&_cursor->next); |
---|
| 327 | free(_cursor->next); |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | /** |
---|
| 333 | * \brief Not named linked list |
---|
| 334 | * |
---|
| 335 | * Used to store informations about formats, such as mimeType, encoding ... |
---|
| 336 | * |
---|
| 337 | * An iotype is defined as : |
---|
| 338 | * - a content map, |
---|
| 339 | * - a pointer to the next iotype if any. |
---|
| 340 | */ |
---|
| 341 | typedef struct iotype{ |
---|
| 342 | struct map* content; |
---|
| 343 | struct iotype* next; |
---|
| 344 | } iotype; |
---|
| 345 | |
---|
| 346 | /** |
---|
| 347 | * \brief Metadata information about input or output. |
---|
| 348 | * |
---|
| 349 | * The elements are used to store metadata informations defined in the ZCFG. |
---|
| 350 | * |
---|
| 351 | * An elements is defined as : |
---|
| 352 | * - a name, |
---|
| 353 | * - a content map, |
---|
| 354 | * - a metadata map, |
---|
| 355 | * - a format (possible values are LiteralData, ComplexData or |
---|
| 356 | * BoundingBoxData), |
---|
| 357 | * - a default iotype, |
---|
| 358 | * - a pointer to the next elements id any. |
---|
| 359 | */ |
---|
| 360 | typedef struct elements{ |
---|
| 361 | char* name; |
---|
| 362 | struct map* content; |
---|
| 363 | struct map* metadata; |
---|
| 364 | char* format; |
---|
| 365 | struct iotype* defaults; |
---|
| 366 | struct iotype* supported; |
---|
| 367 | struct elements* next; |
---|
| 368 | } elements; |
---|
| 369 | |
---|
| 370 | typedef struct service{ |
---|
| 371 | char* name; |
---|
| 372 | struct map* content; |
---|
| 373 | struct map* metadata; |
---|
| 374 | struct elements* inputs; |
---|
| 375 | struct elements* outputs; |
---|
| 376 | } service; |
---|
| 377 | |
---|
| 378 | typedef struct services{ |
---|
| 379 | struct service* content; |
---|
| 380 | struct services* next; |
---|
| 381 | } services; |
---|
| 382 | |
---|
| 383 | static bool hasElement(elements* e,const char* key){ |
---|
| 384 | elements* tmp=e; |
---|
| 385 | while(tmp!=NULL){ |
---|
| 386 | if(strcasecmp(key,tmp->name)==0) |
---|
| 387 | return true; |
---|
| 388 | tmp=tmp->next; |
---|
| 389 | } |
---|
| 390 | return false; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | static elements* getElements(elements* m,char *key){ |
---|
| 394 | elements* tmp=m; |
---|
| 395 | while(tmp!=NULL){ |
---|
| 396 | if(strcasecmp(tmp->name,key)==0) |
---|
| 397 | return tmp; |
---|
| 398 | tmp=tmp->next; |
---|
| 399 | } |
---|
| 400 | return NULL; |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | |
---|
| 404 | static void freeIOType(iotype** i){ |
---|
| 405 | iotype* _cursor=*i; |
---|
| 406 | if(_cursor!=NULL){ |
---|
| 407 | if(_cursor->next!=NULL){ |
---|
| 408 | freeIOType(&_cursor->next); |
---|
| 409 | free(_cursor->next); |
---|
| 410 | } |
---|
| 411 | freeMap(&_cursor->content); |
---|
| 412 | free(_cursor->content); |
---|
| 413 | } |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | static void freeElements(elements** e){ |
---|
| 417 | elements* tmp=*e; |
---|
| 418 | if(tmp!=NULL){ |
---|
| 419 | if(tmp->name!=NULL) |
---|
| 420 | free(tmp->name); |
---|
| 421 | freeMap(&tmp->content); |
---|
| 422 | if(tmp->content!=NULL) |
---|
| 423 | free(tmp->content); |
---|
| 424 | freeMap(&tmp->metadata); |
---|
| 425 | if(tmp->metadata!=NULL) |
---|
| 426 | free(tmp->metadata); |
---|
| 427 | if(tmp->format!=NULL) |
---|
| 428 | free(tmp->format); |
---|
| 429 | freeIOType(&tmp->defaults); |
---|
| 430 | if(tmp->defaults!=NULL) |
---|
| 431 | free(tmp->defaults); |
---|
| 432 | freeIOType(&tmp->supported); |
---|
| 433 | if(tmp->supported!=NULL){ |
---|
| 434 | free(tmp->supported); |
---|
| 435 | } |
---|
| 436 | freeElements(&tmp->next); |
---|
| 437 | if(tmp->next!=NULL) |
---|
| 438 | free(tmp->next); |
---|
| 439 | } |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | static void freeService(service** s){ |
---|
| 443 | service* tmp=*s; |
---|
| 444 | if(tmp!=NULL){ |
---|
| 445 | if(tmp->name!=NULL) |
---|
| 446 | free(tmp->name); |
---|
| 447 | freeMap(&tmp->content); |
---|
| 448 | if(tmp->content!=NULL) |
---|
| 449 | free(tmp->content); |
---|
| 450 | freeMap(&tmp->metadata); |
---|
| 451 | if(tmp->metadata!=NULL) |
---|
| 452 | free(tmp->metadata); |
---|
| 453 | freeElements(&tmp->inputs); |
---|
| 454 | if(tmp->inputs!=NULL) |
---|
| 455 | free(tmp->inputs); |
---|
| 456 | freeElements(&tmp->outputs); |
---|
| 457 | if(tmp->outputs!=NULL) |
---|
| 458 | free(tmp->outputs); |
---|
| 459 | } |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | static void addToMap(map* m,const char* n,const char* v){ |
---|
| 463 | if(hasKey(m,n)==false){ |
---|
| 464 | map* _cursor=m; |
---|
| 465 | while(_cursor->next!=NULL) |
---|
| 466 | _cursor=_cursor->next; |
---|
| 467 | _cursor->next=createMap(n,v); |
---|
| 468 | } |
---|
| 469 | else{ |
---|
| 470 | map *tmp=getMap(m,n); |
---|
| 471 | if(tmp->value!=NULL) |
---|
| 472 | free(tmp->value); |
---|
| 473 | tmp->value=strdup(v); |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | static void addMapToMap(map** mo,map* mi){ |
---|
| 478 | map* tmp=mi; |
---|
| 479 | map* _cursor=*mo; |
---|
| 480 | if(tmp==NULL){ |
---|
| 481 | if(_cursor!=NULL){ |
---|
| 482 | while(_cursor!=NULL) |
---|
| 483 | _cursor=_cursor->next; |
---|
| 484 | _cursor=NULL; |
---|
| 485 | }else |
---|
| 486 | *mo=NULL; |
---|
| 487 | } |
---|
| 488 | while(tmp!=NULL){ |
---|
| 489 | if(_cursor==NULL){ |
---|
| 490 | if(*mo==NULL) |
---|
| 491 | *mo=createMap(tmp->name,tmp->value); |
---|
| 492 | else |
---|
| 493 | addToMap(*mo,tmp->name,tmp->value); |
---|
| 494 | } |
---|
| 495 | else{ |
---|
| 496 | #ifdef DEBUG |
---|
| 497 | fprintf(stderr,"_CURSOR\n"); |
---|
| 498 | dumpMap(_cursor); |
---|
| 499 | #endif |
---|
| 500 | while(_cursor!=NULL) |
---|
| 501 | _cursor=_cursor->next; |
---|
| 502 | _cursor=createMap(tmp->name,tmp->value); |
---|
| 503 | _cursor->next=NULL; |
---|
| 504 | } |
---|
| 505 | tmp=tmp->next; |
---|
| 506 | #ifdef DEBUG |
---|
| 507 | fprintf(stderr,"MO\n"); |
---|
| 508 | dumpMap(*mo); |
---|
| 509 | #endif |
---|
| 510 | } |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | static void addMapToIoType(iotype** io,map* mi){ |
---|
| 514 | iotype* tmp=*io; |
---|
| 515 | while(tmp->next!=NULL){ |
---|
| 516 | tmp=tmp->next; |
---|
| 517 | } |
---|
| 518 | tmp->next=(iotype*)malloc(IOTYPE_SIZE); |
---|
| 519 | tmp->next->content=NULL; |
---|
| 520 | addMapToMap(&tmp->next->content,mi); |
---|
| 521 | tmp->next->next=NULL; |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | static map* getMapOrFill(map* m,const char *key,char* value){ |
---|
| 525 | map* tmp=m; |
---|
| 526 | map* tmpMap=getMap(tmp,key); |
---|
| 527 | if(tmpMap==NULL){ |
---|
| 528 | if(tmp!=NULL) |
---|
| 529 | addToMap(tmp,key,value); |
---|
| 530 | else |
---|
| 531 | tmp=createMap(key,value); |
---|
| 532 | tmpMap=getMap(tmp,key); |
---|
| 533 | } |
---|
| 534 | return tmpMap; |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | static bool contains(map* m,map* i){ |
---|
| 538 | while(i!=NULL){ |
---|
| 539 | if(strcasecmp(i->name,"value")!=0 && |
---|
| 540 | strcasecmp(i->name,"xlink:href")!=0 && |
---|
| 541 | strcasecmp(i->name,"useMapServer")!=0 && |
---|
| 542 | strcasecmp(i->name,"asReference")!=0){ |
---|
| 543 | map *tmp; |
---|
| 544 | if(hasKey(m,i->name) && (tmp=getMap(m,i->name))!=NULL && |
---|
| 545 | strcasecmp(i->value,tmp->value)!=0) |
---|
| 546 | return false; |
---|
| 547 | } |
---|
| 548 | i=i->next; |
---|
| 549 | } |
---|
| 550 | return true; |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | static iotype* getIoTypeFromElement(elements* e,char *name, map* values){ |
---|
| 554 | elements* cursor=e; |
---|
| 555 | while(cursor!=NULL){ |
---|
| 556 | if(strcasecmp(cursor->name,name)==0){ |
---|
| 557 | if(contains(cursor->defaults->content,values)==true) |
---|
| 558 | return cursor->defaults; |
---|
| 559 | else{ |
---|
| 560 | iotype* tmp=cursor->supported; |
---|
| 561 | while(tmp!=NULL){ |
---|
| 562 | if(contains(tmp->content,values)==true) |
---|
| 563 | return tmp; |
---|
| 564 | tmp=tmp->next; |
---|
| 565 | } |
---|
| 566 | } |
---|
| 567 | } |
---|
| 568 | cursor=cursor->next; |
---|
| 569 | } |
---|
| 570 | return NULL; |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | static maps* dupMaps(maps** mo){ |
---|
| 574 | maps* _cursor=*mo; |
---|
| 575 | maps* res=NULL; |
---|
| 576 | if(_cursor!=NULL){ |
---|
| 577 | res=(maps*)malloc(MAPS_SIZE); |
---|
| 578 | res->name=strdup(_cursor->name); |
---|
| 579 | res->content=NULL; |
---|
| 580 | res->next=NULL; |
---|
| 581 | map* mc=_cursor->content; |
---|
| 582 | map* tmp=getMap(mc,"size"); |
---|
| 583 | char* tmpSized=NULL; |
---|
| 584 | if(tmp!=NULL){ |
---|
| 585 | map* tmpV=getMap(mc,"value"); |
---|
| 586 | tmpSized=(char*)malloc((atoi(tmp->value)+1)*sizeof(char)); |
---|
| 587 | memmove(tmpSized,tmpV->value,atoi(tmp->value)*sizeof(char)); |
---|
| 588 | } |
---|
| 589 | if(mc!=NULL){ |
---|
| 590 | addMapToMap(&res->content,mc); |
---|
| 591 | } |
---|
| 592 | if(tmp!=NULL){ |
---|
| 593 | map* tmpV=getMap(res->content,"value"); |
---|
| 594 | free(tmpV->value); |
---|
| 595 | tmpV->value=(char*)malloc((atoi(tmp->value)+1)*sizeof(char)); |
---|
| 596 | memmove(tmpV->value,tmpSized,atoi(tmp->value)*sizeof(char)); |
---|
| 597 | tmpV->value[atoi(tmp->value)]=0; |
---|
| 598 | free(tmpSized); |
---|
| 599 | } |
---|
| 600 | res->next=dupMaps(&_cursor->next); |
---|
| 601 | } |
---|
| 602 | return res; |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | static void addMapsToMaps(maps** mo,maps* mi){ |
---|
| 606 | maps* tmp=mi; |
---|
| 607 | maps* _cursor=*mo; |
---|
| 608 | while(tmp!=NULL){ |
---|
| 609 | if(_cursor==NULL){ |
---|
| 610 | *mo=dupMaps(&mi); |
---|
| 611 | (*mo)->next=NULL; |
---|
| 612 | } |
---|
| 613 | else{ |
---|
| 614 | while(_cursor->next!=NULL) |
---|
| 615 | _cursor=_cursor->next; |
---|
| 616 | _cursor->next=dupMaps(&tmp); |
---|
| 617 | } |
---|
| 618 | tmp=tmp->next; |
---|
| 619 | } |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | static map* getMapArray(map* m,char* key,int index){ |
---|
| 623 | char tmp[1024]; |
---|
| 624 | if(index>0) |
---|
| 625 | sprintf(tmp,"%s_%d",key,index); |
---|
| 626 | else |
---|
| 627 | sprintf(tmp,"%s",key); |
---|
| 628 | #ifdef DEBUG |
---|
| 629 | fprintf(stderr,"** KEY %s\n",tmp); |
---|
| 630 | #endif |
---|
| 631 | map* tmpMap=getMap(m,tmp); |
---|
| 632 | #ifdef DEBUG |
---|
| 633 | if(tmpMap!=NULL) |
---|
| 634 | dumpMap(tmpMap); |
---|
| 635 | #endif |
---|
| 636 | return tmpMap; |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | |
---|
| 640 | static void setMapArray(map* m,char* key,int index,char* value){ |
---|
| 641 | char tmp[1024]; |
---|
| 642 | if(index>0) |
---|
| 643 | sprintf(tmp,"%s_%d",key,index); |
---|
| 644 | else |
---|
| 645 | sprintf(tmp,"%s",key); |
---|
| 646 | map* tmpSize=getMapArray(m,"size",index); |
---|
| 647 | if(tmpSize!=NULL && strncasecmp(key,"value",5)==0){ |
---|
| 648 | #ifdef DEBUG |
---|
| 649 | fprintf(stderr,"%s\n",tmpSize->value); |
---|
| 650 | #endif |
---|
| 651 | map* ptr=getMapOrFill(m,tmp,""); |
---|
| 652 | free(ptr->value); |
---|
| 653 | ptr->value=(char*)malloc((atoi(tmpSize->value)+1)*sizeof(char)); |
---|
| 654 | memcpy(ptr->value,value,atoi(tmpSize->value)); |
---|
| 655 | } |
---|
| 656 | else |
---|
| 657 | addToMap(m,tmp,value); |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | static map* getMapType(map* mt){ |
---|
| 661 | map* tmap=getMap(mt,"mimeType"); |
---|
| 662 | if(tmap==NULL){ |
---|
| 663 | tmap=getMap(mt,"dataType"); |
---|
| 664 | if(tmap==NULL){ |
---|
| 665 | tmap=getMap(mt,"CRS"); |
---|
| 666 | } |
---|
| 667 | } |
---|
| 668 | #ifdef DEBUG |
---|
| 669 | dumpMap(tmap); |
---|
| 670 | #endif |
---|
| 671 | return tmap; |
---|
| 672 | } |
---|
| 673 | |
---|
| 674 | static int addMapsArrayToMaps(maps** mo,maps* mi,char* typ){ |
---|
| 675 | maps* tmp=mi; |
---|
| 676 | maps* _cursor=getMaps(*mo,tmp->name); |
---|
| 677 | |
---|
| 678 | if(_cursor==NULL) |
---|
| 679 | return -1; |
---|
| 680 | |
---|
| 681 | map* tmpLength=getMap(_cursor->content,"length"); |
---|
| 682 | char tmpLen[10]; |
---|
| 683 | int len=1; |
---|
| 684 | if(tmpLength!=NULL){ |
---|
| 685 | len=atoi(tmpLength->value); |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | map* tmpValI=getMap(tmp->content,"value"); |
---|
| 689 | char *tmpV[8]={ |
---|
| 690 | "size", |
---|
| 691 | "value", |
---|
| 692 | "uom", |
---|
| 693 | "Reference", |
---|
| 694 | "xlink:href", |
---|
| 695 | typ, |
---|
| 696 | "schema", |
---|
| 697 | "encoding" |
---|
| 698 | }; |
---|
| 699 | sprintf(tmpLen,"%d",len+1); |
---|
| 700 | addToMap(_cursor->content,"length",tmpLen); |
---|
| 701 | int i=0; |
---|
| 702 | map* tmpSizeI=getMap(tmp->content,tmpV[i]); |
---|
| 703 | for(0;i<8;i++){ |
---|
| 704 | map* tmpVI=getMap(tmp->content,tmpV[i]); |
---|
| 705 | if(tmpVI!=NULL){ |
---|
| 706 | #ifdef DEBUG |
---|
| 707 | fprintf(stderr,"%s = %s\n",tmpV[i],tmpVI->value); |
---|
| 708 | #endif |
---|
| 709 | if(i<5) |
---|
| 710 | setMapArray(_cursor->content,tmpV[i],len,tmpVI->value); |
---|
| 711 | else |
---|
| 712 | if(strncasecmp(tmpV[5],"mimeType",8)==0) |
---|
| 713 | setMapArray(_cursor->content,tmpV[i],len,tmpVI->value); |
---|
| 714 | } |
---|
| 715 | } |
---|
| 716 | |
---|
| 717 | addToMap(_cursor->content,"isArray","true"); |
---|
| 718 | return 0; |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | static void setMapInMaps(maps* m,const char* key,const char* subkey,const char *value){ |
---|
| 722 | maps* _tmpm=getMaps(m,key); |
---|
| 723 | if(_tmpm!=NULL){ |
---|
| 724 | map* _ztmpm=getMap(_tmpm->content,subkey); |
---|
| 725 | if(_ztmpm!=NULL){ |
---|
| 726 | if(_ztmpm->value!=NULL) |
---|
| 727 | free(_ztmpm->value); |
---|
| 728 | _ztmpm->value=strdup(value); |
---|
| 729 | }else{ |
---|
| 730 | addToMap(_tmpm->content,subkey,value); |
---|
| 731 | } |
---|
| 732 | }else{ |
---|
| 733 | maps *tmp=(maps*)malloc(MAPS_SIZE); |
---|
| 734 | tmp->name=strdup(key); |
---|
| 735 | tmp->content=createMap(subkey,value); |
---|
| 736 | tmp->next=NULL; |
---|
| 737 | addMapsToMaps(&m,tmp); |
---|
| 738 | freeMaps(&tmp); |
---|
| 739 | free(tmp); |
---|
| 740 | } |
---|
| 741 | } |
---|
| 742 | |
---|
| 743 | |
---|
| 744 | static void dumpElements(elements* e){ |
---|
| 745 | elements* tmp=e; |
---|
| 746 | while(tmp!=NULL){ |
---|
| 747 | fprintf(stderr,"ELEMENT [%s]\n",tmp->name); |
---|
| 748 | fprintf(stderr," > CONTENT [%s]\n",tmp->name); |
---|
| 749 | dumpMap(tmp->content); |
---|
| 750 | fprintf(stderr," > METADATA [%s]\n",tmp->name); |
---|
| 751 | dumpMap(tmp->metadata); |
---|
| 752 | fprintf(stderr," > FORMAT [%s]\n",tmp->format); |
---|
| 753 | iotype* tmpio=tmp->defaults; |
---|
| 754 | int ioc=0; |
---|
| 755 | while(tmpio!=NULL){ |
---|
| 756 | fprintf(stderr," > DEFAULTS [%s] (%i)\n",tmp->name,ioc); |
---|
| 757 | dumpMap(tmpio->content); |
---|
| 758 | tmpio=tmpio->next; |
---|
| 759 | ioc++; |
---|
| 760 | } |
---|
| 761 | tmpio=tmp->supported; |
---|
| 762 | ioc=0; |
---|
| 763 | while(tmpio!=NULL){ |
---|
| 764 | fprintf(stderr," > SUPPORTED [%s] (%i)\n",tmp->name,ioc); |
---|
| 765 | dumpMap(tmpio->content); |
---|
| 766 | tmpio=tmpio->next; |
---|
| 767 | ioc++; |
---|
| 768 | } |
---|
| 769 | fprintf(stderr,"------------------\n"); |
---|
| 770 | tmp=tmp->next; |
---|
| 771 | } |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | static elements* dupElements(elements* e){ |
---|
| 775 | elements* cursor=e; |
---|
| 776 | elements* tmp=NULL; |
---|
| 777 | if(cursor!=NULL){ |
---|
| 778 | #ifdef DEBUG |
---|
| 779 | fprintf(stderr,">> %s %i\n",__FILE__,__LINE__); |
---|
| 780 | dumpElements(e); |
---|
| 781 | fprintf(stderr,">> %s %i\n",__FILE__,__LINE__); |
---|
| 782 | #endif |
---|
| 783 | tmp=(elements*)malloc(ELEMENTS_SIZE); |
---|
| 784 | tmp->name=strdup(e->name); |
---|
| 785 | tmp->content=NULL; |
---|
| 786 | addMapToMap(&tmp->content,e->content); |
---|
| 787 | tmp->metadata=NULL; |
---|
| 788 | addMapToMap(&tmp->metadata,e->metadata); |
---|
| 789 | tmp->format=strdup(e->format); |
---|
| 790 | if(e->defaults!=NULL){ |
---|
| 791 | tmp->defaults=(iotype*)malloc(IOTYPE_SIZE); |
---|
| 792 | tmp->defaults->content=NULL; |
---|
| 793 | addMapToMap(&tmp->defaults->content,e->defaults->content); |
---|
| 794 | tmp->defaults->next=NULL; |
---|
| 795 | #ifdef DEBUG |
---|
| 796 | fprintf(stderr,">> %s %i\n",__FILE__,__LINE__); |
---|
| 797 | dumpMap(tmp->defaults->content); |
---|
| 798 | #endif |
---|
| 799 | }else |
---|
| 800 | tmp->defaults=NULL; |
---|
| 801 | if(e->supported!=NULL){ |
---|
| 802 | tmp->supported=(iotype*)malloc(IOTYPE_SIZE); |
---|
| 803 | tmp->supported->content=NULL; |
---|
| 804 | addMapToMap(&tmp->supported->content,e->supported->content); |
---|
| 805 | tmp->supported->next=NULL; |
---|
| 806 | iotype *tmp2=e->supported->next; |
---|
| 807 | while(tmp2!=NULL){ |
---|
| 808 | addMapToIoType(&tmp->supported,tmp2->content); |
---|
| 809 | #ifdef DEBUG |
---|
| 810 | fprintf(stderr,">> %s %i\n",__FILE__,__LINE__); |
---|
| 811 | dumpMap(tmp->defaults->content); |
---|
| 812 | #endif |
---|
| 813 | tmp2=tmp2->next; |
---|
| 814 | } |
---|
| 815 | } |
---|
| 816 | else |
---|
| 817 | tmp->supported=NULL; |
---|
| 818 | tmp->next=dupElements(cursor->next); |
---|
| 819 | } |
---|
| 820 | return tmp; |
---|
| 821 | } |
---|
| 822 | |
---|
| 823 | static void addToElements(elements** m,elements* e){ |
---|
| 824 | elements* tmp=e; |
---|
| 825 | if(*m==NULL){ |
---|
| 826 | *m=dupElements(tmp); |
---|
| 827 | }else{ |
---|
| 828 | addToElements(&(*m)->next,tmp); |
---|
| 829 | } |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | static void dumpService(service* s){ |
---|
| 833 | fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name); |
---|
| 834 | if(s->content!=NULL){ |
---|
| 835 | fprintf(stderr,"CONTENT MAP\n"); |
---|
| 836 | dumpMap(s->content); |
---|
| 837 | fprintf(stderr,"CONTENT METADATA\n"); |
---|
| 838 | dumpMap(s->metadata); |
---|
| 839 | } |
---|
| 840 | if(s->inputs!=NULL){ |
---|
| 841 | fprintf(stderr,"INPUT ELEMENTS [%s]\n------------------\n",s->name); |
---|
| 842 | dumpElements(s->inputs); |
---|
| 843 | } |
---|
| 844 | if(s->outputs!=NULL){ |
---|
| 845 | fprintf(stderr,"OUTPUT ELEMENTS [%s]\n------------------\n",s->name); |
---|
| 846 | dumpElements(s->outputs); |
---|
| 847 | } |
---|
| 848 | fprintf(stderr,"++++++++++++++++++\n"); |
---|
| 849 | } |
---|
| 850 | |
---|
| 851 | static void mapsToCharXXX(maps* m,char*** c){ |
---|
| 852 | maps* tm=m; |
---|
| 853 | int i=0; |
---|
| 854 | int j=0; |
---|
| 855 | char tmp[10][30][1024]; |
---|
| 856 | memset(tmp,0,1024*10*10); |
---|
| 857 | while(tm!=NULL){ |
---|
| 858 | if(i>=10) |
---|
| 859 | break; |
---|
| 860 | strcpy(tmp[i][j],"name"); |
---|
| 861 | j++; |
---|
| 862 | strcpy(tmp[i][j],tm->name); |
---|
| 863 | j++; |
---|
| 864 | map* tc=tm->content; |
---|
| 865 | while(tc!=NULL){ |
---|
| 866 | if(j>=30) |
---|
| 867 | break; |
---|
| 868 | strcpy(tmp[i][j],tc->name); |
---|
| 869 | j++; |
---|
| 870 | strcpy(tmp[i][j],tc->value); |
---|
| 871 | j++; |
---|
| 872 | tc=tc->next; |
---|
| 873 | } |
---|
| 874 | tm=tm->next; |
---|
| 875 | j=0; |
---|
| 876 | i++; |
---|
| 877 | } |
---|
| 878 | memcpy(c,tmp,10*10*1024); |
---|
| 879 | } |
---|
| 880 | |
---|
| 881 | static void charxxxToMaps(char*** c,maps**m){ |
---|
| 882 | maps* trorf=*m; |
---|
| 883 | int i,j; |
---|
| 884 | char tmp[10][30][1024]; |
---|
| 885 | memcpy(tmp,c,10*30*1024); |
---|
| 886 | for(i=0;i<10;i++){ |
---|
| 887 | if(strlen(tmp[i][1])==0) |
---|
| 888 | break; |
---|
| 889 | trorf->name=tmp[i][1]; |
---|
| 890 | trorf->content=NULL; |
---|
| 891 | trorf->next=NULL; |
---|
| 892 | for(j=2;j<29;j+=2){ |
---|
| 893 | if(strlen(tmp[i][j+1])==0) |
---|
| 894 | break; |
---|
| 895 | if(trorf->content==NULL) |
---|
| 896 | trorf->content=createMap(tmp[i][j],tmp[i][j+1]); |
---|
| 897 | else |
---|
| 898 | addToMap(trorf->content,tmp[i][j],tmp[i][j+1]); |
---|
| 899 | } |
---|
| 900 | trorf=trorf->next; |
---|
| 901 | } |
---|
| 902 | m=&trorf; |
---|
| 903 | } |
---|
| 904 | |
---|
| 905 | #ifdef __cplusplus |
---|
| 906 | } |
---|
| 907 | #endif |
---|
| 908 | |
---|
| 909 | #endif |
---|