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