1 : /**********************************************************************
2 : * $Id: geoconcept.c
3 : *
4 : * Name: geoconcept.c
5 : * Project: OpenGIS Simple Features Reference Implementation
6 : * Purpose: Implements Physical Access class.
7 : * Language: C
8 : *
9 : **********************************************************************
10 : * Copyright (c) 2007, Geoconcept and IGN
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : **********************************************************************/
30 :
31 : #include <math.h>
32 : #include "geoconcept.h"
33 : #include "cpl_conv.h"
34 : #include "cpl_string.h"
35 : #include "ogr_core.h"
36 :
37 : GCIO_CVSID("$Id: geoconcept.c,v 1.0.0 2007-11-03 20:58:19 drichard Exp $")
38 :
39 : #define kItemSize_GCIO 256
40 : #define kExtraSize_GCIO 4096
41 : #define kIdSize_GCIO 12
42 : #define UNDEFINEDID_GCIO 199901L
43 :
44 : static char* gkGCCharset[]=
45 : {
46 : /* 0 */ "",
47 : /* 1 */ "ANSI",
48 : /* 2 */ "DOS",
49 : /* 3 */ "MAC"
50 : };
51 :
52 : static char* gkGCAccess[]=
53 : {
54 : /* 0 */ "",
55 : /* 1 */ "NO",
56 : /* 2 */ "READ",
57 : /* 3 */ "UPDATE",
58 : /* 4 */ "WRITE"
59 : };
60 :
61 : static char* gkGCStatus[]=
62 : {
63 : /* 0 */ "NONE",
64 : /* 1 */ "MEMO",
65 : /* 2 */ "EOF"
66 : };
67 :
68 : static char* gk3D[]=
69 : {
70 : /* 0 */ "",
71 : /* 1 */ "2D",
72 : /* 2 */ "3DM",
73 : /* 3 */ "3D"
74 : };
75 :
76 : static char* gkGCTypeKind[]=
77 : {
78 : /* 0 */ "",
79 : /* 1 */ "POINT",
80 : /* 2 */ "LINE",
81 : /* 3 */ "TEXT",
82 : /* 4 */ "POLYGON",
83 : /* 5 */ "MEMO",
84 : /* 6 */ "INT",
85 : /* 7 */ "REAL",
86 : /* 8 */ "LENGTH",
87 : /* 9 */ "AREA",
88 : /*10 */ "POSITION",
89 : /*11 */ "DATE",
90 : /*12 */ "TIME",
91 : /*13 */ "CHOICE",
92 : /*14 */ "MEMO"
93 : };
94 :
95 : /* -------------------------------------------------------------------- */
96 : /* GCIO API Prototypes */
97 : /* -------------------------------------------------------------------- */
98 :
99 : /* -------------------------------------------------------------------- */
100 0 : static char GCIOAPI_CALL1(*) _getHeaderValue_GCIO ( const char *s )
101 : {
102 : char *b, *e;
103 :
104 0 : if( (b= strchr(s,'='))==NULL ) return NULL;
105 0 : b++;
106 0 : while (isspace((unsigned char)*b)) b++;
107 0 : e= b;
108 0 : while (*e!='\0' && !isspace((unsigned char)*e)) e++;
109 0 : *e= '\0';
110 0 : return b;
111 : }/* _getHeaderValue_GCIO */
112 :
113 : /* -------------------------------------------------------------------- */
114 5 : const char GCIOAPI_CALL1(*) GCCharset2str_GCIO ( GCCharset cs )
115 : {
116 5 : switch (cs) {
117 : case vANSI_GCIO :
118 : case vDOS_GCIO :
119 : case vMAC_GCIO :
120 5 : return gkGCCharset[cs];
121 : default :
122 0 : return gkGCCharset[vUnknownCharset_GCIO];
123 : }
124 : }/* GCCharset2str_GCIO */
125 :
126 : /* -------------------------------------------------------------------- */
127 4 : GCCharset GCIOAPI_CALL str2GCCharset_GCIO ( const char* s)
128 : {
129 4 : if (strcmp(s,gkGCCharset[vANSI_GCIO])==0) return vANSI_GCIO;
130 0 : if (strcmp(s,gkGCCharset[vDOS_GCIO])==0) return vDOS_GCIO;
131 0 : if (strcmp(s,gkGCCharset[vMAC_GCIO])==0) return vMAC_GCIO;
132 0 : return vUnknownCharset_GCIO;
133 : }/* str2GCCharset_GCIO */
134 :
135 : /* -------------------------------------------------------------------- */
136 5 : const char GCIOAPI_CALL1(*) GCAccessMode2str_GCIO ( GCAccessMode mode )
137 : {
138 5 : switch (mode) {
139 : case vNoAccess_GCIO :
140 : case vReadAccess_GCIO :
141 : case vUpdateAccess_GCIO :
142 : case vWriteAccess_GCIO :
143 5 : return gkGCAccess[mode];
144 : default :
145 0 : return gkGCAccess[vUnknownAccessMode_GCIO];
146 : }
147 : }/* GCAccessMode2str_GCIO */
148 :
149 : /* -------------------------------------------------------------------- */
150 0 : GCAccessMode GCIOAPI_CALL str2GCAccessMode_GCIO ( const char* s)
151 : {
152 0 : if (strcmp(s,gkGCAccess[vNoAccess_GCIO])==0) return vNoAccess_GCIO;
153 0 : if (strcmp(s,gkGCAccess[vReadAccess_GCIO])==0) return vReadAccess_GCIO;
154 0 : if (strcmp(s,gkGCAccess[vUpdateAccess_GCIO])==0) return vUpdateAccess_GCIO;
155 0 : if (strcmp(s,gkGCAccess[vWriteAccess_GCIO])==0) return vWriteAccess_GCIO;
156 0 : return vUnknownAccessMode_GCIO;
157 : }/* str2GCAccessMode_GCIO */
158 :
159 : /* -------------------------------------------------------------------- */
160 5 : const char GCIOAPI_CALL1(*) GCAccessStatus2str_GCIO ( GCAccessStatus stts )
161 : {
162 5 : switch (stts) {
163 : case vMemoStatus_GCIO :
164 : case vEof_GCIO :
165 0 : return gkGCStatus[stts];
166 : default :
167 5 : return gkGCStatus[vNoStatus_GCIO];
168 : }
169 : }/* GCAccessStatus2str_GCIO */
170 :
171 : /* -------------------------------------------------------------------- */
172 0 : GCAccessStatus GCIOAPI_CALL str2GCAccessStatus_GCIO ( const char* s)
173 : {
174 0 : if (strcmp(s,gkGCStatus[vMemoStatus_GCIO])==0) return vMemoStatus_GCIO;
175 0 : if (strcmp(s,gkGCStatus[vEof_GCIO])==0) return vEof_GCIO;
176 0 : return vNoStatus_GCIO;
177 : }/* str2GCAccessStatus_GCIO */
178 :
179 : /* -------------------------------------------------------------------- */
180 0 : const char GCIOAPI_CALL1(*) GCDim2str_GCIO ( GCDim sys )
181 : {
182 0 : switch (sys) {
183 : case v2D_GCIO :
184 : case v3D_GCIO :
185 : case v3DM_GCIO :
186 0 : return gk3D[sys];
187 : default :
188 0 : return gk3D[vUnknown3D_GCIO];
189 : }
190 : }/* GCDim2str_GCIO */
191 :
192 : /* -------------------------------------------------------------------- */
193 0 : GCDim GCIOAPI_CALL str2GCDim ( const char* s )
194 : {
195 0 : if (strcmp(s,gk3D[v2D_GCIO])==0) return v2D_GCIO;
196 0 : if (strcmp(s,gk3D[v3D_GCIO])==0) return v3D_GCIO;
197 0 : if (strcmp(s,gk3D[v3DM_GCIO])==0) return v3DM_GCIO;
198 0 : return vUnknown3D_GCIO;
199 : }/* str2GCDim */
200 :
201 : /* -------------------------------------------------------------------- */
202 0 : const char GCIOAPI_CALL1(*) GCTypeKind2str_GCIO ( GCTypeKind item )
203 : {
204 0 : switch (item) {
205 : case vPoint_GCIO :
206 : case vLine_GCIO :
207 : case vText_GCIO :
208 : case vPoly_GCIO :
209 : case vMemoFld_GCIO :
210 : case vIntFld_GCIO :
211 : case vRealFld_GCIO :
212 : case vLengthFld_GCIO :
213 : case vAreaFld_GCIO :
214 : case vPositionFld_GCIO :
215 : case vDateFld_GCIO :
216 : case vTimeFld_GCIO :
217 : case vChoiceFld_GCIO :
218 : case vInterFld_GCIO :
219 0 : return gkGCTypeKind[item];
220 : default :
221 0 : return gkGCTypeKind[vUnknownItemType_GCIO];
222 : }
223 : }/* GCTypeKind2str_GCIO */
224 :
225 : /* -------------------------------------------------------------------- */
226 0 : GCTypeKind GCIOAPI_CALL str2GCTypeKind_GCIO ( const char *s )
227 : {
228 0 : if (strcmp(s,gkGCTypeKind[vPoint_GCIO])==0) return vPoint_GCIO;
229 0 : if (strcmp(s,gkGCTypeKind[vLine_GCIO])==0) return vLine_GCIO;
230 0 : if (strcmp(s,gkGCTypeKind[vText_GCIO])==0) return vText_GCIO;
231 0 : if (strcmp(s,gkGCTypeKind[vPoly_GCIO])==0) return vPoly_GCIO;
232 0 : if (strcmp(s,gkGCTypeKind[vMemoFld_GCIO])==0) return vMemoFld_GCIO;
233 0 : if (strcmp(s,gkGCTypeKind[vIntFld_GCIO])==0) return vIntFld_GCIO;
234 0 : if (strcmp(s,gkGCTypeKind[vRealFld_GCIO])==0) return vRealFld_GCIO;
235 0 : if (strcmp(s,gkGCTypeKind[vLengthFld_GCIO])==0) return vLengthFld_GCIO;
236 0 : if (strcmp(s,gkGCTypeKind[vAreaFld_GCIO])==0) return vAreaFld_GCIO;
237 0 : if (strcmp(s,gkGCTypeKind[vPositionFld_GCIO])==0) return vPositionFld_GCIO;
238 0 : if (strcmp(s,gkGCTypeKind[vDateFld_GCIO])==0) return vDateFld_GCIO;
239 0 : if (strcmp(s,gkGCTypeKind[vTimeFld_GCIO])==0) return vTimeFld_GCIO;
240 0 : if (strcmp(s,gkGCTypeKind[vChoiceFld_GCIO])==0) return vChoiceFld_GCIO;
241 0 : if (strcmp(s,gkGCTypeKind[vInterFld_GCIO])==0) return vInterFld_GCIO;
242 0 : return vUnknownItemType_GCIO;
243 : }/* str2GCTypeKind_GCIO */
244 :
245 : /* -------------------------------------------------------------------- */
246 1 : static const char GCIOAPI_CALL1(*) _metaDelimiter2str_GCIO ( char delim )
247 : {
248 1 : switch( delim ) {
249 : case '\t' :
250 1 : return "tab";
251 : default :
252 0 : return "\t";
253 : }
254 : }/* _metaDelimiter2str_GCIO */
255 :
256 : /* -------------------------------------------------------------------- */
257 107 : static long GCIOAPI_CALL _read_GCIO (
258 : GCExportFileH* hGXT
259 : )
260 : {
261 : FILE* h;
262 : long nread;
263 : int c;
264 : char *result;
265 :
266 107 : h= GetGCHandle_GCIO(hGXT);
267 107 : nread= 0L;
268 107 : result= GetGCCache_GCIO(hGXT);
269 107 : SetGCCurrentOffset_GCIO(hGXT, VSIFTell(h));/* keep offset of beginning of lines */
270 8361 : while ((c= VSIFGetc(h))!=EOF)
271 : {
272 8244 : c= (0x00FF & (unsigned char)(c));
273 8244 : switch (c) {
274 0 : case 0X1A : continue ; /* PC end-of-file */
275 : case '\r' : /* PC '\r\n' line, MAC '\r' */
276 0 : if ((c= VSIFGetc(h))!='\n')
277 : {
278 0 : VSIUngetc(c,h);
279 0 : c= '\n';
280 : }
281 : case '\n' :
282 97 : SetGCCurrentLinenum_GCIO(hGXT,GetGCCurrentLinenum_GCIO(hGXT)+1L);
283 97 : if (nread==0L) continue;
284 97 : *result= '\0';
285 97 : return nread;
286 : default :
287 8147 : *result= (char)c;
288 8147 : result++;
289 8147 : nread++;
290 8147 : if (nread == kCacheSize_GCIO)
291 : {
292 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
293 : "Too many characters at line %lu.\n", GetGCCurrentLinenum_GCIO(hGXT));
294 0 : return EOF;
295 : }
296 : }/* switch */
297 : }/* while */
298 10 : *result= '\0';
299 10 : if (c==EOF)
300 : {
301 10 : SetGCStatus_GCIO(hGXT, vEof_GCIO);
302 10 : if (nread==0L)
303 : {
304 10 : return EOF;
305 : }
306 : }
307 :
308 0 : return nread;
309 : }/* _read_GCIO */
310 :
311 : /* -------------------------------------------------------------------- */
312 107 : static long GCIOAPI_CALL _get_GCIO (
313 : GCExportFileH* hGXT
314 : )
315 : {
316 107 : if (GetGCStatus_GCIO(hGXT)==vEof_GCIO)
317 : {
318 0 : SetGCCache_GCIO(hGXT,"");
319 0 : SetGCWhatIs_GCIO(hGXT, vUnknownIO_ItemType_GCIO);
320 0 : return EOF;
321 : }
322 107 : if (GetGCStatus_GCIO(hGXT)==vMemoStatus_GCIO)
323 : {
324 0 : SetGCStatus_GCIO(hGXT, vNoStatus_GCIO);
325 0 : return GetGCCurrentOffset_GCIO(hGXT);
326 : }
327 107 : if (_read_GCIO(hGXT)==EOF)
328 : {
329 10 : SetGCWhatIs_GCIO(hGXT, vUnknownIO_ItemType_GCIO);
330 10 : return EOF;
331 : }
332 97 : SetGCWhatIs_GCIO(hGXT, vStdCol_GCIO);
333 97 : if (strstr(GetGCCache_GCIO(hGXT),kCom_GCIO)==GetGCCache_GCIO(hGXT))
334 : { /* // */
335 49 : SetGCWhatIs_GCIO(hGXT, vComType_GCIO);
336 49 : if (strstr(GetGCCache_GCIO(hGXT),kHeader_GCIO)==GetGCCache_GCIO(hGXT))
337 : { /* //# */
338 0 : SetGCWhatIs_GCIO(hGXT, vHeader_GCIO);
339 : }
340 : else
341 : {
342 49 : if (strstr(GetGCCache_GCIO(hGXT),kPragma_GCIO)==GetGCCache_GCIO(hGXT))
343 : { /* //$ */
344 49 : SetGCWhatIs_GCIO(hGXT, vPragma_GCIO);
345 : }
346 : }
347 : }
348 97 : return GetGCCurrentOffset_GCIO(hGXT);
349 : }/* _get_GCIO */
350 :
351 : /* -------------------------------------------------------------------- */
352 18 : static void GCIOAPI_CALL _InitExtent_GCIO (
353 : GCExtent* theExtent
354 : )
355 : {
356 18 : theExtent->XUL= HUGE_VAL;
357 18 : theExtent->YUL= -HUGE_VAL;
358 18 : theExtent->XLR= -HUGE_VAL;
359 18 : theExtent->YLR= HUGE_VAL;
360 18 : }/* _InitExtent_GCIO */
361 :
362 : /* -------------------------------------------------------------------- */
363 9 : GCExtent GCIOAPI_CALL1(*) CreateExtent_GCIO (
364 : double Xmin,
365 : double Ymin,
366 : double Xmax,
367 : double Ymax
368 : )
369 : {
370 : GCExtent* theExtent;
371 :
372 9 : if( !(theExtent= CPLMalloc(sizeof(GCExtent))) )
373 : {
374 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
375 : "failed to create a Geoconcept extent for '[%g %g,%g %g]'.\n",
376 : Xmin, Ymin,Xmax, Ymax);
377 0 : return NULL;
378 : }
379 9 : _InitExtent_GCIO(theExtent);
380 9 : theExtent->XUL= Xmin;
381 9 : theExtent->YUL= Ymax;
382 9 : theExtent->XLR= Xmax;
383 9 : theExtent->YLR= Ymin;
384 :
385 9 : return theExtent;
386 : }/* CreateExtent_GCIO */
387 :
388 : /* -------------------------------------------------------------------- */
389 9 : static void GCIOAPI_CALL _ReInitExtent_GCIO (
390 : GCExtent* theExtent
391 : )
392 : {
393 9 : _InitExtent_GCIO(theExtent);
394 9 : }/* _ReInitExtent_GCIO */
395 :
396 : /* -------------------------------------------------------------------- */
397 9 : void GCIOAPI_CALL DestroyExtent_GCIO (
398 : GCExtent** theExtent
399 : )
400 : {
401 9 : _ReInitExtent_GCIO(*theExtent);
402 9 : CPLFree(*theExtent);
403 9 : *theExtent= NULL;
404 9 : }/* DestroyExtent_GCIO */
405 :
406 : /* -------------------------------------------------------------------- */
407 104 : static void GCIOAPI_CALL _InitField_GCIO (
408 : GCField* theField
409 : )
410 : {
411 104 : SetFieldName_GCIO(theField, NULL);
412 104 : SetFieldID_GCIO(theField, UNDEFINEDID_GCIO);
413 104 : SetFieldKind_GCIO(theField, vUnknownItemType_GCIO);
414 104 : SetFieldExtra_GCIO(theField, NULL);
415 104 : SetFieldList_GCIO(theField, NULL);
416 104 : }/* _InitField_GCIO */
417 :
418 : /* -------------------------------------------------------------------- */
419 52 : static const char GCIOAPI_CALL1(*) _NormalizeFieldName_GCIO (
420 : const char* name
421 : )
422 : {
423 52 : if( name[0]=='@' )
424 : {
425 37 : if( EQUAL(name, "@Identificateur") || EQUAL(name, kIdentifier_GCIO) )
426 : {
427 5 : return kIdentifier_GCIO;
428 : }
429 32 : else if( EQUAL(name, "@Type") || EQUAL(name, kClass_GCIO) )
430 : {
431 5 : return kClass_GCIO;
432 : }
433 27 : else if( EQUAL(name, "@Sous-type") || EQUAL(name, kSubclass_GCIO) )
434 : {
435 5 : return kSubclass_GCIO;
436 : }
437 22 : else if( EQUAL(name, "@Nom") || EQUAL(name, kName_GCIO) )
438 : {
439 5 : return kName_GCIO;
440 : }
441 17 : else if( EQUAL(name, kNbFields_GCIO) )
442 : {
443 5 : return kNbFields_GCIO;
444 : }
445 12 : else if( EQUAL(name, kX_GCIO) )
446 : {
447 5 : return kX_GCIO;
448 : }
449 7 : else if( EQUAL(name, kY_GCIO) )
450 : {
451 5 : return kY_GCIO;
452 : }
453 2 : else if( EQUAL(name, "@X'") || EQUAL(name, kXP_GCIO) )
454 : {
455 0 : return kXP_GCIO;
456 : }
457 2 : else if( EQUAL(name, "@Y'") || EQUAL(name, kYP_GCIO) )
458 : {
459 0 : return kYP_GCIO;
460 : }
461 2 : else if( EQUAL(name, kGraphics_GCIO) )
462 : {
463 2 : return kGraphics_GCIO;
464 : }
465 0 : else if( EQUAL(name, kAngle_GCIO) )
466 : {
467 0 : return kAngle_GCIO;
468 : }
469 : else
470 : {
471 0 : return name;
472 : }
473 : }
474 : else
475 : {
476 15 : return name;
477 : }
478 : }/* _NormalizeFieldName_GCIO */
479 :
480 : /* -------------------------------------------------------------------- */
481 52 : static GCField GCIOAPI_CALL1(*) _CreateField_GCIO (
482 : const char* name,
483 : long id,
484 : GCTypeKind knd,
485 : const char* extra,
486 : const char* enums
487 : )
488 : {
489 : GCField* theField;
490 :
491 52 : if( !(theField= CPLMalloc(sizeof(GCField))) )
492 : {
493 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
494 : "failed to create a Geoconcept field for '%s'.\n",
495 : name);
496 0 : return NULL;
497 : }
498 52 : _InitField_GCIO(theField);
499 52 : SetFieldName_GCIO(theField, CPLStrdup(name));
500 52 : SetFieldID_GCIO(theField, id);
501 52 : SetFieldKind_GCIO(theField, knd);
502 52 : if( extra && extra[0]!='\0' ) SetFieldExtra_GCIO(theField, CPLStrdup(extra));
503 52 : if( enums && enums[0]!='\0' ) SetFieldList_GCIO(theField, CSLTokenizeString2(enums,";",0));
504 :
505 52 : return theField;
506 : }/* _CreateField_GCIO */
507 :
508 : /* -------------------------------------------------------------------- */
509 52 : static void GCIOAPI_CALL _ReInitField_GCIO (
510 : GCField* theField
511 : )
512 : {
513 52 : if( GetFieldName_GCIO(theField) )
514 : {
515 52 : CPLFree(GetFieldName_GCIO(theField));
516 : }
517 52 : if( GetFieldExtra_GCIO(theField) )
518 : {
519 0 : CPLFree( GetFieldExtra_GCIO(theField) );
520 : }
521 52 : if( GetFieldList_GCIO(theField) )
522 : {
523 0 : CSLDestroy( GetFieldList_GCIO(theField) );
524 : }
525 52 : _InitField_GCIO(theField);
526 52 : }/* _ReInitField_GCIO */
527 :
528 : /* -------------------------------------------------------------------- */
529 52 : static void GCIOAPI_CALL _DestroyField_GCIO (
530 : GCField** theField
531 : )
532 : {
533 52 : _ReInitField_GCIO(*theField);
534 52 : CPLFree(*theField);
535 52 : *theField= NULL;
536 52 : }/* _DestroyField_GCIO */
537 :
538 : /* -------------------------------------------------------------------- */
539 110 : static int GCIOAPI_CALL _findFieldByName_GCIO (
540 : CPLList* fields,
541 : const char* name
542 : )
543 : {
544 : GCField* theField;
545 :
546 110 : if( fields )
547 : {
548 : CPLList* e;
549 : int n, i;
550 105 : if( (n= CPLListCount(fields))>0 )
551 : {
552 582 : for( i= 0; i<n; i++)
553 : {
554 528 : if( (e= CPLListGet(fields,i)) )
555 : {
556 528 : if( (theField= (GCField*)CPLListGetData(e)) )
557 : {
558 528 : if( EQUAL(GetFieldName_GCIO(theField),name) )
559 : {
560 51 : return i;
561 : }
562 : }
563 : }
564 : }
565 : }
566 : }
567 59 : return -1;
568 : }/* _findFieldByName_GCIO */
569 :
570 : /* -------------------------------------------------------------------- */
571 0 : static GCField GCIOAPI_CALL1(*) _getField_GCIO (
572 : CPLList* fields,
573 : int where
574 : )
575 : {
576 : CPLList* e;
577 :
578 0 : if( (e= CPLListGet(fields,where)) )
579 0 : return (GCField*)CPLListGetData(e);
580 0 : return NULL;
581 : }/* _getField_GCIO */
582 :
583 : /* -------------------------------------------------------------------- */
584 10 : static void GCIOAPI_CALL _InitSubType_GCIO (
585 : GCSubType* theSubType
586 : )
587 : {
588 10 : SetSubTypeGCHandle_GCIO(theSubType, NULL);
589 10 : SetSubTypeType_GCIO(theSubType, NULL);
590 10 : SetSubTypeName_GCIO(theSubType, NULL);
591 10 : SetSubTypeFields_GCIO(theSubType, NULL); /* GCField */
592 10 : SetSubTypeFeatureDefn_GCIO(theSubType, NULL);
593 10 : SetSubTypeKind_GCIO(theSubType, vUnknownItemType_GCIO);
594 10 : SetSubTypeID_GCIO(theSubType, UNDEFINEDID_GCIO);
595 10 : SetSubTypeDim_GCIO(theSubType, v2D_GCIO);
596 10 : SetSubTypeNbFields_GCIO(theSubType, -1);
597 10 : SetSubTypeNbFeatures_GCIO(theSubType, 0L);
598 10 : SetSubTypeBOF_GCIO(theSubType, -1L);
599 10 : SetSubTypeBOFLinenum_GCIO(theSubType, 0L);
600 10 : SetSubTypeExtent_GCIO(theSubType, NULL);
601 10 : SetSubTypeHeaderWritten_GCIO(theSubType, FALSE);
602 10 : }/* _InitSubType_GCIO */
603 :
604 : /* -------------------------------------------------------------------- */
605 5 : static GCSubType GCIOAPI_CALL1(*) _CreateSubType_GCIO (
606 : const char* subtypName,
607 : long id,
608 : GCTypeKind knd,
609 : GCDim sys
610 : )
611 : {
612 : GCSubType* theSubType;
613 :
614 5 : if( !(theSubType= CPLMalloc(sizeof(GCSubType))) )
615 : {
616 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
617 : "failed to create a Geoconcept subtype for '%s'.\n",
618 : subtypName);
619 0 : return NULL;
620 : }
621 5 : _InitSubType_GCIO(theSubType);
622 5 : SetSubTypeName_GCIO(theSubType, CPLStrdup(subtypName));
623 5 : SetSubTypeID_GCIO(theSubType, id);
624 5 : SetSubTypeKind_GCIO(theSubType, knd);
625 5 : SetSubTypeDim_GCIO(theSubType, sys);
626 :
627 5 : return theSubType;
628 : }/* _CreateSubType_GCIO */
629 :
630 : /* -------------------------------------------------------------------- */
631 5 : static void GCIOAPI_CALL _ReInitSubType_GCIO (
632 : GCSubType* theSubType
633 : )
634 : {
635 5 : if( GetSubTypeFeatureDefn_GCIO(theSubType) )
636 : {
637 5 : OGR_FD_Release(GetSubTypeFeatureDefn_GCIO(theSubType));
638 : }
639 5 : if( GetSubTypeFields_GCIO(theSubType) )
640 : {
641 : CPLList* e;
642 : GCField* theField;
643 : int i, n;
644 5 : if( (n= CPLListCount(GetSubTypeFields_GCIO(theSubType)))>0 )
645 : {
646 57 : for (i= 0; i<n; i++)
647 : {
648 52 : if( (e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i)) )
649 : {
650 52 : if( (theField= (GCField*)CPLListGetData(e)) )
651 : {
652 52 : _DestroyField_GCIO(&theField);
653 : }
654 : }
655 : }
656 : }
657 5 : CPLListDestroy(GetSubTypeFields_GCIO(theSubType));
658 : }
659 5 : if( GetSubTypeName_GCIO(theSubType) )
660 : {
661 5 : CPLFree( GetSubTypeName_GCIO(theSubType) );
662 : }
663 5 : if( GetSubTypeExtent_GCIO(theSubType) )
664 : {
665 4 : DestroyExtent_GCIO(&(GetSubTypeExtent_GCIO(theSubType)));
666 : }
667 5 : _InitSubType_GCIO(theSubType);
668 5 : }/* _ReInitSubType_GCIO */
669 :
670 : /* -------------------------------------------------------------------- */
671 5 : static void GCIOAPI_CALL _DestroySubType_GCIO (
672 : GCSubType** theSubType
673 : )
674 : {
675 5 : _ReInitSubType_GCIO(*theSubType);
676 5 : CPLFree(*theSubType);
677 5 : *theSubType= NULL;
678 5 : }/* _DestroySubType_GCIO */
679 :
680 : /* -------------------------------------------------------------------- */
681 102 : static int GCIOAPI_CALL _findSubTypeByName_GCIO (
682 : GCType* theClass,
683 : const char* subtypName
684 : )
685 : {
686 : GCSubType* theSubType;
687 :
688 102 : if( GetTypeSubtypes_GCIO(theClass) )
689 : {
690 : CPLList* e;
691 : int n, i;
692 98 : if( (n= CPLListCount(GetTypeSubtypes_GCIO(theClass)))>0 )
693 : {
694 98 : if( *subtypName=='*' ) return 0;
695 98 : for( i = 0; i < n; i++)
696 : {
697 98 : if( (e= CPLListGet(GetTypeSubtypes_GCIO(theClass),i)) )
698 : {
699 98 : if( (theSubType= (GCSubType*)CPLListGetData(e)) )
700 : {
701 98 : if( EQUAL(GetSubTypeName_GCIO(theSubType),subtypName) )
702 : {
703 98 : return i;
704 : }
705 : }
706 : }
707 : }
708 : }
709 : }
710 4 : return -1;
711 : }/* _findSubTypeByName_GCIO */
712 :
713 : /* -------------------------------------------------------------------- */
714 98 : static GCSubType GCIOAPI_CALL1(*) _getSubType_GCIO (
715 : GCType* theClass,
716 : int where
717 : )
718 : {
719 : CPLList* e;
720 :
721 98 : if( (e= CPLListGet(GetTypeSubtypes_GCIO(theClass),where)) )
722 98 : return (GCSubType*)CPLListGetData(e);
723 0 : return NULL;
724 : }/* _getSubType_GCIO */
725 :
726 : /* -------------------------------------------------------------------- */
727 10 : static void GCIOAPI_CALL _InitType_GCIO (
728 : GCType* theClass
729 : )
730 : {
731 10 : SetTypeName_GCIO(theClass, NULL);
732 10 : SetTypeSubtypes_GCIO(theClass, NULL);/* GCSubType */
733 10 : SetTypeFields_GCIO(theClass, NULL); /* GCField */
734 10 : SetTypeID_GCIO(theClass, UNDEFINEDID_GCIO);
735 10 : }/* _InitType_GCIO */
736 :
737 : /* -------------------------------------------------------------------- */
738 5 : static GCType GCIOAPI_CALL1(*) _CreateType_GCIO (
739 : const char* typName,
740 : long id
741 : )
742 : {
743 : GCType* theClass;
744 :
745 5 : if( !(theClass= CPLMalloc(sizeof(GCType))) )
746 : {
747 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
748 : "failed to create a Geoconcept type for '%s#%ld'.\n",
749 : typName, id);
750 0 : return NULL;
751 : }
752 5 : _InitType_GCIO(theClass);
753 5 : SetTypeName_GCIO(theClass, CPLStrdup(typName));
754 5 : SetTypeID_GCIO(theClass, id);
755 :
756 5 : return theClass;
757 : }/* _CreateType_GCIO */
758 :
759 : /* -------------------------------------------------------------------- */
760 5 : static void GCIOAPI_CALL _ReInitType_GCIO (
761 : GCType* theClass
762 : )
763 : {
764 5 : if( GetTypeSubtypes_GCIO(theClass) )
765 : {
766 : CPLList* e;
767 : GCSubType* theSubType;
768 : int i, n;
769 5 : if( (n= CPLListCount(GetTypeSubtypes_GCIO(theClass)))>0 )
770 : {
771 10 : for (i= 0; i<n; i++)
772 : {
773 5 : if( (e= CPLListGet(GetTypeSubtypes_GCIO(theClass),i)) )
774 : {
775 5 : if( (theSubType= (GCSubType*)CPLListGetData(e)) )
776 : {
777 5 : _DestroySubType_GCIO(&theSubType);
778 : }
779 : }
780 : }
781 : }
782 5 : CPLListDestroy(GetTypeSubtypes_GCIO(theClass));
783 : }
784 5 : if( GetTypeFields_GCIO(theClass) )
785 : {
786 : CPLList* e;
787 : GCField* theField;
788 : int i, n;
789 0 : if( (n= CPLListCount(GetTypeFields_GCIO(theClass)))>0 )
790 : {
791 0 : for (i= 0; i<n; i++)
792 : {
793 0 : if( (e= CPLListGet(GetTypeFields_GCIO(theClass),i)) )
794 : {
795 0 : if( (theField= (GCField*)CPLListGetData(e)) )
796 : {
797 0 : _DestroyField_GCIO(&theField);
798 : }
799 : }
800 : }
801 : }
802 0 : CPLListDestroy(GetTypeFields_GCIO(theClass));
803 : }
804 5 : if( GetTypeName_GCIO(theClass) )
805 : {
806 5 : CPLFree( GetTypeName_GCIO(theClass) );
807 : }
808 5 : _InitType_GCIO(theClass);
809 5 : }/* _ReInitType_GCIO */
810 :
811 : /* -------------------------------------------------------------------- */
812 5 : static void GCIOAPI_CALL _DestroyType_GCIO (
813 : GCType** theClass
814 : )
815 : {
816 5 : _ReInitType_GCIO(*theClass);
817 5 : CPLFree(*theClass);
818 5 : *theClass= NULL;
819 5 : }/* _DestroyType_GCIO */
820 :
821 : /* -------------------------------------------------------------------- */
822 113 : static int GCIOAPI_CALL _findTypeByName_GCIO (
823 : GCExportFileH* hGXT,
824 : const char* typName
825 : )
826 : {
827 : GCType* theClass;
828 : GCExportFileMetadata* header;
829 :
830 113 : header= GetGCMeta_GCIO(hGXT);
831 113 : if( GetMetaTypes_GCIO(header) )
832 : {
833 : CPLList* e;
834 : int n, i;
835 103 : if( (n= CPLListCount(GetMetaTypes_GCIO(header)))>0 )
836 : {
837 103 : if( *typName=='*' ) return 0;
838 103 : for( i = 0; i < n; i++)
839 : {
840 103 : if( (e= CPLListGet(GetMetaTypes_GCIO(header),i)) )
841 : {
842 103 : if( (theClass= (GCType*)CPLListGetData(e)) )
843 : {
844 103 : if( EQUAL(GetTypeName_GCIO(theClass),typName) )
845 : {
846 103 : return i;
847 : }
848 : }
849 : }
850 : }
851 : }
852 : }
853 10 : return -1;
854 : }/* _findTypeByName_GCIO */
855 :
856 : /* -------------------------------------------------------------------- */
857 103 : static GCType GCIOAPI_CALL1(*) _getType_GCIO (
858 : GCExportFileH* hGXT,
859 : int where
860 : )
861 : {
862 : CPLList* e;
863 :
864 103 : if( (e= CPLListGet(GetMetaTypes_GCIO(GetGCMeta_GCIO(hGXT)),where)) )
865 103 : return (GCType*)CPLListGetData(e);
866 0 : return NULL;
867 : }/* _getType_GCIO */
868 :
869 : /* -------------------------------------------------------------------- */
870 10 : static void GCIOAPI_CALL _InitHeader_GCIO (
871 : GCExportFileMetadata* header
872 : )
873 : {
874 10 : SetMetaVersion_GCIO(header, NULL);
875 10 : SetMetaDelimiter_GCIO(header, kTAB_GCIO[0]);
876 10 : SetMetaQuotedText_GCIO(header, FALSE);
877 10 : SetMetaCharset_GCIO(header, vANSI_GCIO);
878 10 : SetMetaUnit_GCIO(header, "m");
879 10 : SetMetaFormat_GCIO(header, 2);
880 10 : SetMetaSysCoord_GCIO(header, NULL); /* GCSysCoord */
881 10 : SetMetaPlanarFormat_GCIO(header, 0);
882 10 : SetMetaHeightFormat_GCIO(header, 0);
883 10 : SetMetaSRS_GCIO(header, NULL);
884 10 : SetMetaTypes_GCIO(header, NULL); /* GCType */
885 10 : SetMetaFields_GCIO(header, NULL); /* GCField */
886 10 : SetMetaResolution_GCIO(header, 0.1);
887 10 : SetMetaExtent_GCIO(header, NULL);
888 10 : }/* _InitHeader_GCIO */
889 :
890 : /* -------------------------------------------------------------------- */
891 5 : GCExportFileMetadata GCIOAPI_CALL1(*) CreateHeader_GCIO ( )
892 : {
893 : GCExportFileMetadata* m;
894 :
895 5 : if( !(m= CPLMalloc(sizeof(GCExportFileMetadata)) ) )
896 : {
897 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
898 : "failed to create Geoconcept metadata.\n");
899 0 : return NULL;
900 : }
901 5 : _InitHeader_GCIO(m);
902 :
903 5 : return m;
904 : }/* CreateHeader_GCIO */
905 :
906 : /* -------------------------------------------------------------------- */
907 5 : static void GCIOAPI_CALL _ReInitHeader_GCIO (
908 : GCExportFileMetadata* header
909 : )
910 : {
911 5 : if( GetMetaVersion_GCIO(header) )
912 : {
913 0 : CPLFree( GetMetaVersion_GCIO(header) );
914 : }
915 5 : if( GetMetaExtent_GCIO(header) )
916 : {
917 5 : DestroyExtent_GCIO(&(GetMetaExtent_GCIO(header)));
918 : }
919 5 : if( GetMetaTypes_GCIO(header) )
920 : {
921 : CPLList* e;
922 : GCType* theClass;
923 : int i, n;
924 5 : if( (n= CPLListCount(GetMetaTypes_GCIO(header)))>0 )
925 : {
926 10 : for (i= 0; i<n; i++)
927 : {
928 5 : if( (e= CPLListGet(GetMetaTypes_GCIO(header),i)) )
929 : {
930 5 : if( (theClass= (GCType*)CPLListGetData(e)) )
931 : {
932 5 : _DestroyType_GCIO(&theClass);
933 : }
934 : }
935 : }
936 : }
937 5 : CPLListDestroy(GetMetaTypes_GCIO(header));
938 : }
939 5 : if( GetMetaFields_GCIO(header) )
940 : {
941 : CPLList* e;
942 : GCField* theField;
943 : int i, n;
944 0 : if( (n= CPLListCount(GetMetaFields_GCIO(header)))>0 )
945 : {
946 0 : for (i= 0; i<n; i++)
947 : {
948 0 : if( (e= CPLListGet(GetMetaFields_GCIO(header),i)) )
949 : {
950 0 : if( (theField= (GCField*)CPLListGetData(e)) )
951 : {
952 0 : _DestroyField_GCIO(&theField);
953 : }
954 : }
955 : }
956 : }
957 0 : CPLListDestroy(GetMetaFields_GCIO(header));
958 : }
959 5 : if( GetMetaSRS_GCIO(header) )
960 : {
961 5 : OSRRelease( GetMetaSRS_GCIO(header) );
962 : }
963 5 : if( GetMetaSysCoord_GCIO(header) )
964 : {
965 5 : DestroySysCoord_GCSRS(&(GetMetaSysCoord_GCIO(header)));
966 : }
967 :
968 5 : _InitHeader_GCIO(header);
969 5 : }/* _ReInitHeader_GCIO */
970 :
971 : /* -------------------------------------------------------------------- */
972 5 : void GCIOAPI_CALL DestroyHeader_GCIO (
973 : GCExportFileMetadata** m
974 : )
975 : {
976 5 : _ReInitHeader_GCIO(*m);
977 5 : CPLFree(*m);
978 5 : *m= NULL;
979 5 : }/* DestroyHeader_GCIO */
980 :
981 : /* -------------------------------------------------------------------- */
982 14 : static void GCIOAPI_CALL _Init_GCIO (
983 : GCExportFileH* H
984 : )
985 : {
986 14 : SetGCCache_GCIO(H,"");
987 14 : SetGCPath_GCIO(H, NULL);
988 14 : SetGCBasename_GCIO(H, NULL);
989 14 : SetGCExtension_GCIO(H, NULL);
990 14 : SetGCHandle_GCIO(H, NULL);
991 14 : SetGCCurrentOffset_GCIO(H, 0L);
992 14 : SetGCCurrentLinenum_GCIO(H, 0L);
993 14 : SetGCNbObjects_GCIO(H, 0L);
994 14 : SetGCMeta_GCIO(H, NULL);
995 14 : SetGCMode_GCIO(H, vNoAccess_GCIO);
996 14 : SetGCStatus_GCIO(H, vNoStatus_GCIO);
997 14 : SetGCWhatIs_GCIO(H, vUnknownIO_ItemType_GCIO);
998 14 : }/* _Init_GCIO */
999 :
1000 : /* -------------------------------------------------------------------- */
1001 7 : static GCExportFileH GCIOAPI_CALL1(*) _Create_GCIO (
1002 : const char* pszGeoconceptFile,
1003 : const char *ext,
1004 : const char* mode
1005 : )
1006 : {
1007 : GCExportFileH* hGXT;
1008 :
1009 7 : CPLDebug("GEOCONCEPT","allocating %d bytes for GCExportFileH", (int)sizeof(GCExportFileH));
1010 7 : if( !(hGXT= CPLMalloc(sizeof(GCExportFileH)) ) )
1011 : {
1012 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
1013 : "failed to create a Geoconcept handle for '%s' (%s).\n",
1014 : pszGeoconceptFile, mode);
1015 0 : return NULL;
1016 : }
1017 :
1018 7 : _Init_GCIO(hGXT);
1019 7 : SetGCPath_GCIO(hGXT, CPLStrdup(CPLGetDirname(pszGeoconceptFile)));
1020 7 : SetGCBasename_GCIO(hGXT, CPLStrdup(CPLGetBasename(pszGeoconceptFile)));
1021 7 : SetGCExtension_GCIO(hGXT, CPLStrdup(ext? ext:"gxt"));
1022 7 : SetGCMode_GCIO(hGXT, (mode[0]=='w'? vWriteAccess_GCIO : (mode[0]=='a'? vUpdateAccess_GCIO:vReadAccess_GCIO)));
1023 :
1024 7 : return hGXT;
1025 : }/* _Create_GCIO */
1026 :
1027 : /* -------------------------------------------------------------------- */
1028 7 : static void GCIOAPI_CALL _ReInit_GCIO (
1029 : GCExportFileH* hGXT
1030 : )
1031 : {
1032 7 : if( GetGCMeta_GCIO(hGXT) )
1033 : {
1034 5 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1035 : }
1036 7 : if( GetGCHandle_GCIO(hGXT) )
1037 : {
1038 7 : VSIFClose(GetGCHandle_GCIO(hGXT));
1039 : }
1040 7 : if( GetGCExtension_GCIO(hGXT) )
1041 : {
1042 7 : CPLFree(GetGCExtension_GCIO(hGXT));
1043 : }
1044 7 : if( GetGCBasename_GCIO(hGXT) )
1045 : {
1046 7 : CPLFree(GetGCBasename_GCIO(hGXT));
1047 : }
1048 7 : if( GetGCPath_GCIO(hGXT) )
1049 : {
1050 7 : CPLFree(GetGCPath_GCIO(hGXT));
1051 : }
1052 7 : SetGCCache_GCIO(hGXT,"");
1053 7 : _Init_GCIO(hGXT);
1054 7 : }/* _ReInit_GCIO */
1055 :
1056 : /* -------------------------------------------------------------------- */
1057 7 : static void GCIOAPI_CALL _Destroy_GCIO (
1058 : GCExportFileH** hGXT,
1059 : int delFile
1060 : )
1061 : {
1062 7 : if( delFile && GetGCMode_GCIO(*hGXT)==vWriteAccess_GCIO )
1063 : {
1064 0 : VSIFClose(GetGCHandle_GCIO(*hGXT));
1065 0 : SetGCHandle_GCIO(*hGXT, NULL);
1066 0 : VSIUnlink(CPLFormFilename(GetGCPath_GCIO(*hGXT),GetGCBasename_GCIO(*hGXT),GetGCExtension_GCIO(*hGXT)));
1067 : }
1068 7 : _ReInit_GCIO(*hGXT);
1069 7 : CPLFree(*hGXT);
1070 7 : *hGXT= NULL;
1071 7 : }/* _Destroy_GCIO */
1072 :
1073 : /* -------------------------------------------------------------------- */
1074 5 : static int _checkSchema_GCIO (
1075 : GCExportFileH* hGXT
1076 : )
1077 : {
1078 : GCExportFileMetadata* Meta;
1079 : int nT, iT, nS, iS, nF, iF, nU, iId, iCl, iSu, iNa, iNb, iX, iY, iXP, iYP, iGr, iAn;
1080 : GCField* theField;
1081 : GCSubType* theSubType;
1082 : GCType* theClass;
1083 : CPLList* e;
1084 :
1085 5 : if( !(Meta= GetGCMeta_GCIO(hGXT)) )
1086 : {
1087 1 : return TRUE; /* FIXME */
1088 : }
1089 4 : if( (nT= CPLListCount(GetMetaTypes_GCIO(Meta)))==0 )
1090 : {
1091 0 : CPLError( CE_Failure, CPLE_AppDefined,
1092 : "Geoconcept schema without types!\n" );
1093 0 : return FALSE;
1094 : }
1095 8 : for (iT= 0; iT<nT; iT++)
1096 : {
1097 4 : if( (e= CPLListGet(GetMetaTypes_GCIO(Meta),iT)) )
1098 : {
1099 4 : if( (theClass= (GCType*)CPLListGetData(e)) )
1100 : {
1101 4 : if( (nS= CPLListCount(GetTypeSubtypes_GCIO(theClass)))==0 )
1102 : {
1103 0 : CPLError( CE_Failure, CPLE_AppDefined,
1104 : "Geoconcept type %s without sub-types!\n",
1105 : GetTypeName_GCIO(theClass) );
1106 0 : return FALSE;
1107 : }
1108 8 : for (iS= 0; iS<nS; iS++)
1109 : {
1110 4 : if( (e= CPLListGet(GetTypeSubtypes_GCIO(theClass),iS)) )
1111 : {
1112 4 : if( (theSubType= (GCSubType*)CPLListGetData(e)) )
1113 : {
1114 4 : if( (nF= CPLListCount(GetSubTypeFields_GCIO(theSubType)))==0 )
1115 : {
1116 0 : CPLError( CE_Failure, CPLE_AppDefined,
1117 : "Geoconcept sub-type %s.%s without fields!\n",
1118 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1119 0 : return FALSE;
1120 : }
1121 4 : nU= 0;
1122 4 : iId= iCl= iSu= iNa= iNb= iX= iY= iXP= iYP= iGr= iAn= -1;
1123 46 : for (iF= 0; iF<nF; iF++)
1124 : {
1125 42 : if( (e= CPLListGet(GetSubTypeFields_GCIO(theSubType),iF)) )
1126 : {
1127 42 : if( (theField= (GCField*)CPLListGetData(e)) )
1128 : {
1129 42 : if( IsPrivateField_GCIO(theField) )
1130 : {
1131 30 : if( EQUAL(GetFieldName_GCIO(theField),kIdentifier_GCIO) )
1132 4 : iId= iF;
1133 26 : else if( EQUAL(GetFieldName_GCIO(theField),kClass_GCIO) )
1134 4 : iCl= iF;
1135 22 : else if( EQUAL(GetFieldName_GCIO(theField),kSubclass_GCIO) )
1136 4 : iSu= iF;
1137 18 : else if( EQUAL(GetFieldName_GCIO(theField),kName_GCIO) )
1138 4 : iNa= iF;
1139 14 : else if( EQUAL(GetFieldName_GCIO(theField),kNbFields_GCIO) )
1140 4 : iNb= iF;
1141 10 : else if( EQUAL(GetFieldName_GCIO(theField),kX_GCIO) )
1142 4 : iX= iF;
1143 6 : else if( EQUAL(GetFieldName_GCIO(theField),kY_GCIO) )
1144 4 : iY= iF;
1145 2 : else if( EQUAL(GetFieldName_GCIO(theField),kXP_GCIO) )
1146 0 : iXP= iF;
1147 2 : else if( EQUAL(GetFieldName_GCIO(theField),kYP_GCIO) )
1148 0 : iYP= iF;
1149 2 : else if( EQUAL(GetFieldName_GCIO(theField),kGraphics_GCIO) )
1150 2 : iGr= iF;
1151 0 : else if( EQUAL(GetFieldName_GCIO(theField),kAngle_GCIO) )
1152 0 : iAn= iF;
1153 : }
1154 : else
1155 : {
1156 12 : nU++;
1157 : }
1158 : }
1159 : }
1160 : }
1161 4 : if( iId==-1 )
1162 : {
1163 0 : CPLError( CE_Failure, CPLE_AppDefined,
1164 : "Geoconcept mandatory field %s is missing on %s.%s!\n",
1165 : kIdentifier_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1166 0 : return FALSE;
1167 : }
1168 4 : else if( iId!=0 )
1169 : {
1170 0 : CPLError( CE_Failure, CPLE_AppDefined,
1171 : "Geoconcept mandatory field %s must be the first field of %s.%s!\n",
1172 : kIdentifier_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1173 0 : return FALSE;
1174 : }
1175 4 : if( iCl==-1 )
1176 : {
1177 0 : CPLError( CE_Failure, CPLE_AppDefined,
1178 : "Geoconcept mandatory field %s is missing on %s.%s!\n",
1179 : kClass_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1180 0 : return FALSE;
1181 : }
1182 4 : else if( iCl-iId!=1 )
1183 : {
1184 0 : CPLError( CE_Failure, CPLE_AppDefined,
1185 : "Geoconcept mandatory field %s must be the second field of %s.%s!\n",
1186 : kClass_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1187 0 : return FALSE;
1188 : }
1189 4 : if( iSu==-1 )
1190 : {
1191 0 : CPLError( CE_Failure, CPLE_AppDefined,
1192 : "Geoconcept mandatory field %s is missing on %s.%s!\n",
1193 : kSubclass_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1194 0 : return FALSE;
1195 : }
1196 4 : else if( iSu-iCl!=1 )
1197 : {
1198 0 : CPLError( CE_Failure, CPLE_AppDefined,
1199 : "Geoconcept mandatory field %s must be the third field of %s.%s!\n",
1200 : kSubclass_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1201 0 : return FALSE;
1202 : }
1203 4 : if (iNa==-1 )
1204 : {
1205 0 : CPLError( CE_Failure, CPLE_AppDefined,
1206 : "Geoconcept mandatory field %s is missing on %s.%s!\n",
1207 : kName_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1208 0 : return FALSE;
1209 : }
1210 4 : else if( iNa-iSu!=1)
1211 : {
1212 0 : CPLError( CE_Failure, CPLE_AppDefined,
1213 : "Geoconcept mandatory field %s must be the forth field of %s.%s!\n",
1214 : kName_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1215 0 : return FALSE;
1216 : }
1217 4 : if( iNb==-1 )
1218 : {
1219 0 : CPLError( CE_Failure, CPLE_AppDefined,
1220 : "Geoconcept mandatory field %s is missing on %s.%s!\n",
1221 : kNbFields_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1222 0 : return FALSE;
1223 : }
1224 4 : if( iX==-1 )
1225 : {
1226 0 : CPLError( CE_Failure, CPLE_AppDefined,
1227 : "Geoconcept mandatory field %s is missing on %s.%s!\n",
1228 : kX_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1229 0 : return FALSE;
1230 : }
1231 4 : if( iY==-1 )
1232 : {
1233 0 : CPLError( CE_Failure, CPLE_AppDefined,
1234 : "Geoconcept mandatory field %s is missing on %s.%s!\n",
1235 : kY_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1236 0 : return FALSE;
1237 : }
1238 4 : if( iY-iX!=1 )
1239 : {
1240 0 : CPLError( CE_Failure, CPLE_AppDefined,
1241 : "Geoconcept geometry fields %s, %s must be consecutive for %s.%s!\n",
1242 : kX_GCIO, kY_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1243 0 : return FALSE;
1244 : }
1245 4 : if( GetSubTypeKind_GCIO(theSubType)==vLine_GCIO )
1246 : {
1247 0 : if( iXP==-1 )
1248 : {
1249 0 : CPLError( CE_Failure, CPLE_AppDefined,
1250 : "Geoconcept mandatory field %s is missing on %s.%s!\n",
1251 : kXP_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1252 0 : return FALSE;
1253 : }
1254 0 : if( iYP==-1 )
1255 : {
1256 0 : CPLError( CE_Failure, CPLE_AppDefined,
1257 : "Geoconcept mandatory field %s is missing on %s.%s!\n",
1258 : kYP_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1259 0 : return FALSE;
1260 : }
1261 0 : if( iYP-iXP!=1 )
1262 : {
1263 0 : CPLError( CE_Failure, CPLE_AppDefined,
1264 : "Geoconcept geometry fields %s, %s must be consecutive for %s.%s!\n",
1265 : kXP_GCIO, kYP_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1266 0 : return FALSE;
1267 : }
1268 0 : if( iXP-iY!=1 )
1269 : {
1270 0 : CPLError( CE_Failure, CPLE_AppDefined,
1271 : "Geoconcept geometry fields %s, %s, %s, %s must be consecutive for %s.%s!\n",
1272 : kX_GCIO, kY_GCIO, kXP_GCIO, kYP_GCIO,
1273 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1274 0 : return FALSE;
1275 : }
1276 : }
1277 : else
1278 : {
1279 4 : if( iXP!=-1 )
1280 : {
1281 0 : CPLError( CE_Failure, CPLE_AppDefined,
1282 : "Geoconcept sub-type %s.%s has a mandatory field %s only required on linear type!\n",
1283 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType), kXP_GCIO );
1284 0 : return FALSE;
1285 : }
1286 4 : if( iYP!=-1 )
1287 : {
1288 0 : CPLError( CE_Failure, CPLE_AppDefined,
1289 : "Geoconcept sub-type %s.%s has a mandatory field %s only required on linear type!\n",
1290 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType), kYP_GCIO );
1291 0 : return FALSE;
1292 : }
1293 : }
1294 10 : if( GetSubTypeKind_GCIO(theSubType)==vLine_GCIO ||
1295 4 : GetSubTypeKind_GCIO(theSubType)==vPoly_GCIO )
1296 : {
1297 2 : if( iGr==-1 )
1298 : {
1299 0 : CPLError( CE_Failure, CPLE_AppDefined,
1300 : "Geoconcept mandatory field %s is missing on %s.%s!\n",
1301 : kGraphics_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1302 0 : return FALSE;
1303 : }
1304 : else
1305 : {
1306 2 : if( !( ((iGr!=-1) && ( (iGr==iY+1) || (iGr==iYP+1) )) || (iGr==-1) ) )
1307 :
1308 : {
1309 0 : CPLError( CE_Failure, CPLE_AppDefined,
1310 : "Geoconcept geometry fields %s, %s must be consecutive for %s.%s!\n",
1311 : iYP!=-1? kYP_GCIO:kY_GCIO, kGraphics_GCIO, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType) );
1312 0 : return FALSE;
1313 : }
1314 : }
1315 2 : if( iAn!=-1 )
1316 : {
1317 0 : CPLError( CE_Failure, CPLE_AppDefined,
1318 : "Geoconcept sub-type %s.%s has a field %s only required on ponctual or text type!\n",
1319 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType), kAngle_GCIO );
1320 0 : return FALSE;
1321 : }
1322 : }
1323 : else
1324 : {
1325 2 : if( iGr!=-1 )
1326 : {
1327 0 : CPLError( CE_Failure, CPLE_AppDefined,
1328 : "Geoconcept sub-type %s.%s has a mandatory field %s only required on linear or polygonal type!\n",
1329 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType), kGraphics_GCIO );
1330 0 : return FALSE;
1331 : }
1332 : }
1333 4 : SetSubTypeNbFields_GCIO(theSubType,nU);
1334 4 : SetSubTypeGCHandle_GCIO(theSubType,hGXT);
1335 : }
1336 : }
1337 : }
1338 : }
1339 : }
1340 : }
1341 :
1342 4 : return TRUE;
1343 : }/* _checkSchema_GCIO */
1344 :
1345 : /* -------------------------------------------------------------------- */
1346 24 : static GCExportFileMetadata GCIOAPI_CALL1(*) _parsePragma_GCIO (
1347 : GCExportFileH* hGXT
1348 : )
1349 : {
1350 : GCExportFileMetadata* Meta;
1351 : char* p, *e;
1352 :
1353 24 : Meta= GetGCMeta_GCIO(hGXT);
1354 :
1355 24 : if( (p= strstr(GetGCCache_GCIO(hGXT),kMetadataVERSION_GCIO))!=NULL )
1356 : {
1357 : /* //$VERSION char* */
1358 0 : p+= strlen(kMetadataVERSION_GCIO);
1359 0 : while( isspace((unsigned char)*p) ) p++;
1360 0 : e= p;
1361 0 : while( isalpha(*p) ) p++;
1362 0 : *p= '\0';
1363 0 : SetMetaVersion_GCIO(Meta,CPLStrdup(e));
1364 0 : return Meta;
1365 : }
1366 24 : if( (p= strstr(GetGCCache_GCIO(hGXT),kMetadataDELIMITER_GCIO))!=NULL )
1367 : {
1368 : /* //$DELIMITER "char*" */
1369 0 : if( (p= strchr(p,'"')) )
1370 : {
1371 0 : p++;
1372 0 : e= p;
1373 0 : while( *p!='"' && *p!='\0' ) p++;
1374 0 : *p= '\0';
1375 0 : if( !( EQUAL(e,"tab") || EQUAL(e,kTAB_GCIO) ) )
1376 : {
1377 0 : CPLDebug("GEOCONCEPT","%s%s only supports \"tab\" value",
1378 : kPragma_GCIO, kMetadataDELIMITER_GCIO);
1379 0 : SetMetaDelimiter_GCIO(Meta,kTAB_GCIO[0]);
1380 : } else {
1381 0 : SetMetaDelimiter_GCIO(Meta,kTAB_GCIO[0]);
1382 : }
1383 : }
1384 0 : return Meta;
1385 : }
1386 24 : if( (p= strstr(GetGCCache_GCIO(hGXT),kMetadataQUOTEDTEXT_GCIO))!=NULL )
1387 : {
1388 : /* //$QUOTED-TEXT "char*" */
1389 4 : if( (p= strchr(p,'"')) )
1390 : {
1391 4 : p++;
1392 4 : e= p;
1393 4 : while( *p!='"' && *p!='\0' ) p++;
1394 4 : *p= '\0';
1395 4 : if( EQUAL(e,"no") )
1396 : {
1397 4 : SetMetaQuotedText_GCIO(Meta,FALSE);
1398 : }
1399 : else
1400 : {
1401 0 : SetMetaQuotedText_GCIO(Meta,TRUE);
1402 : }
1403 : }
1404 4 : return Meta;
1405 : }
1406 20 : if( (p= strstr(GetGCCache_GCIO(hGXT),kMetadataCHARSET_GCIO))!=NULL )
1407 : {
1408 : /* //$CHARSET char* */
1409 4 : p+= strlen(kMetadataCHARSET_GCIO);
1410 4 : while( isspace((unsigned char)*p) ) p++;
1411 4 : e= p;
1412 4 : while( isalpha(*p) ) p++;
1413 4 : *p= '\0';
1414 4 : SetMetaCharset_GCIO(Meta,str2GCCharset_GCIO(e));
1415 4 : return Meta;
1416 : }
1417 16 : if( (p= strstr(GetGCCache_GCIO(hGXT),kMetadataUNIT_GCIO))!=NULL )
1418 : {
1419 : /* //$UNIT Distance|Angle:char* */
1420 4 : if( (p= strchr(p,':')) )
1421 : {
1422 4 : p++;
1423 4 : while( isspace((unsigned char)*p) ) p++;
1424 4 : e= p;
1425 4 : while( isalpha(*p) || *p=='.' ) p++;
1426 4 : *p= '\0';
1427 4 : SetMetaUnit_GCIO(Meta,e);/* FIXME : check value ? */
1428 : }
1429 4 : return Meta;
1430 : }
1431 12 : if( (p= strstr(GetGCCache_GCIO(hGXT),kMetadataFORMAT_GCIO))!=NULL )
1432 : {
1433 : /* //$FORMAT 1|2 */
1434 4 : p+= strlen(kMetadataFORMAT_GCIO);
1435 4 : while( isspace((unsigned char)*p) ) p++;
1436 4 : e= p;
1437 4 : if( *e=='1' )
1438 : {
1439 0 : SetMetaFormat_GCIO(Meta,1);
1440 : }
1441 : else
1442 : {
1443 4 : SetMetaFormat_GCIO(Meta,2);
1444 : }
1445 4 : return Meta;
1446 : }
1447 8 : if( (p= strstr(GetGCCache_GCIO(hGXT),kMetadataSYSCOORD_GCIO))!=NULL )
1448 : {
1449 : int v, z;
1450 : GCSysCoord* syscoord;
1451 : /* //$SYSCOORD {Type: int} [ ; { TimeZone: TimeZoneValue } ] */
1452 4 : v= -1, z= -1;
1453 4 : if( (p= strchr(p,':')) )
1454 : {
1455 4 : p++;
1456 4 : while( isspace((unsigned char)*p) ) p++;
1457 4 : e= p;
1458 4 : if( *p=='-') p++; /* allow -1 as SysCoord */
1459 4 : while( isdigit(*p) ) p++;
1460 4 : *p= '\0';
1461 4 : if( sscanf(e,"%d",&v)!= 1 )
1462 : {
1463 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1464 0 : CPLError( CE_Failure, CPLE_AppDefined,
1465 : "Invalid SRS identifier. "
1466 : "Geoconcept export syntax error at line %ld.",
1467 : GetGCCurrentLinenum_GCIO(hGXT) );
1468 0 : return NULL;
1469 : }
1470 4 : if( (p= strrchr(GetGCCache_GCIO(hGXT),';')) )
1471 : {
1472 0 : if( (p= strchr(p,':')) )
1473 : {
1474 0 : p++;
1475 0 : while( isspace((unsigned char)*p) ) p++;
1476 0 : e= p;
1477 0 : if( *p=='-') p++; /* allow -1 as TimeZone */
1478 0 : while( isdigit(*p) ) p++;
1479 0 : *p= '\0';
1480 0 : if( sscanf(e,"%d",&z)!= 1 )
1481 : {
1482 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1483 0 : CPLError( CE_Failure, CPLE_AppDefined,
1484 : "Invalid TimeZone. "
1485 : "Geoconcept export syntax error at line %ld.",
1486 : GetGCCurrentLinenum_GCIO(hGXT) );
1487 0 : return NULL;
1488 : }
1489 : }
1490 : }
1491 4 : if( !(syscoord= CreateSysCoord_GCSRS(v,z)) )
1492 : {
1493 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1494 0 : return NULL;
1495 : }
1496 4 : SetMetaSysCoord_GCIO(Meta,syscoord);
1497 : }
1498 4 : return Meta;
1499 : }
1500 4 : if( (p= strstr(GetGCCache_GCIO(hGXT),kMetadataFIELDS_GCIO))!=NULL )
1501 : {
1502 : char **kv, **vl, *nm, **fl;
1503 4 : int whereClass, v, i, n, mask=CSLT_HONOURSTRINGS|CSLT_STRIPLEADSPACES|CSLT_STRIPENDSPACES;
1504 : GCType* theClass;
1505 : GCSubType* theSubType;
1506 : GCField* theField;
1507 : /* //$FIELDS +Class=char*; *Subclass=char*; *Kind=1..4; *Fields=(Private#)?char*(\t((Private#)?char*))* */
1508 4 : p+= strlen(kMetadataFIELDS_GCIO);
1509 4 : kv= CSLTokenizeString2(p,";",mask);
1510 4 : if( !kv || CSLCount(kv)!=4 )
1511 : {
1512 0 : CSLDestroy(kv);
1513 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1514 0 : CPLError( CE_Failure, CPLE_AppDefined,
1515 : "Expected: //$FIELDS +Class=char*; *Subclass=char*; *Kind=1..4; *Fields=(Private#)?char*(\\t((Private#)?char*))*\n"
1516 : "Found: [%s]\n"
1517 : "Geoconcept export syntax error at line %ld.\n",
1518 : p,
1519 : GetGCCurrentLinenum_GCIO(hGXT) );
1520 0 : return NULL;
1521 : }
1522 4 : for (i=0; i<4; i++) CPLDebug("GEOCONCEPT", "%d kv[%d]=[%s]\n", __LINE__, i, kv[i]);
1523 : /* Class=char* */
1524 4 : vl= CSLTokenizeString2(kv[0],"=",0);
1525 4 : if( !vl || CSLCount(vl)!=2 )
1526 : {
1527 0 : CPLError( CE_Failure, CPLE_AppDefined,
1528 : "Expected: Class=char*\n"
1529 : "Found: [%s]\n"
1530 : "Geoconcept export syntax error at line %ld.\n",
1531 : kv[0],
1532 : GetGCCurrentLinenum_GCIO(hGXT) );
1533 0 : CSLDestroy(vl);
1534 0 : CSLDestroy(kv);
1535 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1536 0 : return NULL;
1537 : }
1538 4 : for (i=0; i<2; i++) CPLDebug("GEOCONCEPT", "%d vl[%d]=[%s]\n", __LINE__, i, vl[i]);
1539 4 : if( !EQUAL(vl[0], "Class") )
1540 : {
1541 0 : CPLError( CE_Failure, CPLE_AppDefined,
1542 : "Expected: 'Class'\n"
1543 : "Found: [%s]\n"
1544 : "Geoconcept export syntax error at line %ld.\n",
1545 : vl[0],
1546 : GetGCCurrentLinenum_GCIO(hGXT) );
1547 0 : CSLDestroy(vl);
1548 0 : CSLDestroy(kv);
1549 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1550 0 : return NULL;
1551 : }
1552 4 : p= vl[1];
1553 4 : e= p;
1554 4 : if( (whereClass = _findTypeByName_GCIO(hGXT,e))==-1 )
1555 : {
1556 4 : if( !(theClass= AddType_GCIO(hGXT,e,-1)) )
1557 : {
1558 0 : CPLError( CE_Failure, CPLE_AppDefined,
1559 : "Geoconcept export syntax error at line %ld.\n",
1560 : GetGCCurrentLinenum_GCIO(hGXT) );
1561 0 : CSLDestroy(vl);
1562 0 : CSLDestroy(kv);
1563 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1564 0 : return NULL;
1565 : }
1566 : }
1567 : else
1568 : {
1569 0 : theClass= _getType_GCIO(hGXT,whereClass);
1570 : }
1571 4 : CSLDestroy(vl);
1572 : /* Subclass=char* */
1573 4 : vl= CSLTokenizeString2(kv[1],"=",mask);
1574 4 : if( !vl || CSLCount(vl)!=2 )
1575 : {
1576 0 : CPLError( CE_Failure, CPLE_AppDefined,
1577 : "Expected: Subclass=char*\n"
1578 : "Found: [%s]\n"
1579 : "Geoconcept export syntax error at line %ld.\n",
1580 0 : kv[1],
1581 : GetGCCurrentLinenum_GCIO(hGXT) );
1582 0 : CSLDestroy(vl);
1583 0 : CSLDestroy(kv);
1584 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1585 0 : return NULL;
1586 : }
1587 4 : for (i=0; i<2; i++) CPLDebug("GEOCONCEPT", "%d vl[%d]=[%s]\n", __LINE__, i, vl[i]);
1588 4 : p= vl[0];
1589 4 : if( !EQUAL(p, "Subclass") )
1590 : {
1591 0 : CPLError( CE_Failure, CPLE_AppDefined,
1592 : "Expected: 'Subclass'\n"
1593 : "Found: [%s]\n"
1594 : "Geoconcept export syntax error at line %ld.\n",
1595 : p,
1596 : GetGCCurrentLinenum_GCIO(hGXT) );
1597 0 : CSLDestroy(vl);
1598 0 : CSLDestroy(kv);
1599 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1600 0 : return NULL;
1601 : }
1602 4 : p= vl[1];
1603 4 : e= p;
1604 4 : if( _findSubTypeByName_GCIO(theClass,e)!=-1 )
1605 : {
1606 0 : CPLError( CE_Failure, CPLE_AppDefined,
1607 : "[%s] already exists.\n"
1608 : "Geoconcept export syntax error at line %ld.\n",
1609 : e, GetGCCurrentLinenum_GCIO(hGXT) );
1610 0 : CSLDestroy(vl);
1611 0 : CSLDestroy(kv);
1612 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1613 0 : return NULL;
1614 : }
1615 4 : nm= CPLStrdup(e);
1616 4 : CSLDestroy(vl);
1617 : /* Kind=1..4 */
1618 4 : vl= CSLTokenizeString2(kv[2],"=",mask);
1619 4 : if( !vl || CSLCount(vl)!=2 )
1620 : {
1621 0 : CPLError( CE_Failure, CPLE_AppDefined,
1622 : "Expected: Kind=1..4\n"
1623 : "Found: [%s]"
1624 : "Geoconcept export syntax error at line %ld.\n",
1625 0 : kv[2],
1626 : GetGCCurrentLinenum_GCIO(hGXT) );
1627 0 : CPLFree(nm);
1628 0 : CSLDestroy(vl);
1629 0 : CSLDestroy(kv);
1630 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1631 0 : return NULL;
1632 : }
1633 4 : for (i=0; i<2; i++) CPLDebug("GEOCONCEPT", "%d vl[%d]=[%s]\n", __LINE__, i, vl[i]);
1634 4 : p= vl[0];
1635 4 : if( !EQUAL(p, "Kind") )
1636 : {
1637 0 : CPLError( CE_Failure, CPLE_AppDefined,
1638 : "Expected: 'Kind'\n"
1639 : "Found: [%s]\n"
1640 : "Geoconcept export syntax error at line %ld.\n",
1641 : p,
1642 : GetGCCurrentLinenum_GCIO(hGXT) );
1643 0 : CPLFree(nm);
1644 0 : CSLDestroy(vl);
1645 0 : CSLDestroy(kv);
1646 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1647 0 : return NULL;
1648 : }
1649 4 : p= vl[1];
1650 4 : e= p;
1651 4 : while( isdigit(*p) ) p++;
1652 4 : *p= '\0';
1653 4 : if( sscanf(e,"%d",&v)!= 1 || v<1 || v>4 )
1654 : {
1655 0 : CPLError( CE_Failure, CPLE_AppDefined,
1656 : "Invalid Geometry type.\n"
1657 : "Geoconcept export syntax error at line %ld.\n",
1658 : GetGCCurrentLinenum_GCIO(hGXT) );
1659 0 : CPLFree(nm);
1660 0 : CSLDestroy(vl);
1661 0 : CSLDestroy(kv);
1662 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1663 0 : return NULL;
1664 : }
1665 4 : CSLDestroy(vl);
1666 4 : if( !(theSubType= AddSubType_GCIO(hGXT,GetTypeName_GCIO(theClass),
1667 : nm,
1668 : -1,
1669 : (GCTypeKind)v,
1670 : vUnknown3D_GCIO)) )
1671 : {
1672 0 : CPLFree(nm);
1673 0 : CSLDestroy(kv);
1674 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1675 0 : CPLError( CE_Failure, CPLE_AppDefined,
1676 : "Geoconcept export syntax error at line %ld.\n",
1677 : GetGCCurrentLinenum_GCIO(hGXT) );
1678 0 : return NULL;
1679 : }
1680 4 : CPLFree(nm);
1681 : /* Fields=(Private#)?char*(\s((Private#)?char*))* */
1682 4 : vl= CSLTokenizeString2(kv[3],"=",mask);
1683 4 : if( !vl || CSLCount(vl)!=2 )
1684 : {
1685 0 : CPLError( CE_Failure, CPLE_AppDefined,
1686 : "Expected: Fields=(Private#)?char*(\\t((Private#)?char*))*\n"
1687 : "Found: [%s]\n"
1688 : "Geoconcept export syntax error at line %ld.\n",
1689 0 : kv[3],
1690 : GetGCCurrentLinenum_GCIO(hGXT) );
1691 0 : CSLDestroy(vl);
1692 0 : CSLDestroy(kv);
1693 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1694 0 : return NULL;
1695 : }
1696 4 : for (i=0; i<2; i++) CPLDebug("GEOCONCEPT", "%d vl[%d]=[%s]\n", __LINE__, i, vl[i]);
1697 4 : CSLDestroy(kv);
1698 4 : p= vl[0];
1699 4 : if( !EQUAL(p, "Fields") )
1700 : {
1701 0 : CPLError( CE_Failure, CPLE_AppDefined,
1702 : "Expected: 'Fields'\n"
1703 : "Found: [%s]\n"
1704 : "Geoconcept export syntax error at line %ld.\n",
1705 : p,
1706 : GetGCCurrentLinenum_GCIO(hGXT) );
1707 0 : CSLDestroy(vl);
1708 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1709 0 : return NULL;
1710 : }
1711 4 : fl= CSLTokenizeString2(vl[1],"\t",mask);
1712 4 : if( !fl || (n= CSLCount(fl))==0 )
1713 : {
1714 0 : CPLError( CE_Failure, CPLE_AppDefined,
1715 : "Expected: (Private#)?char*(\\t((Private#)?char*))*\n"
1716 : "Found: [%s]\n"
1717 : "Geoconcept export syntax error at line %ld.\n",
1718 0 : vl[1],
1719 : GetGCCurrentLinenum_GCIO(hGXT) );
1720 0 : CSLDestroy(fl);
1721 0 : CSLDestroy(vl);
1722 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1723 0 : return NULL;
1724 : }
1725 4 : CSLDestroy(vl);
1726 46 : for (i= 0; i<n; i++)
1727 : {
1728 42 : p= fl[i];
1729 42 : CPLDebug("GEOCONCEPT", "%d fl[%d]=[%s]\n", __LINE__, i, p);
1730 42 : e= p;
1731 42 : if( EQUALN(p,kPrivate_GCIO,strlen(kPrivate_GCIO)) )
1732 : {
1733 30 : p+= strlen(kPrivate_GCIO);
1734 30 : e= p-1, *e= '@';
1735 : }
1736 42 : nm= CPLStrdup(e);
1737 42 : CPLDebug("GEOCONCEPT", "%d e=[%s]\n", __LINE__, e);
1738 42 : if( (theField= AddSubTypeField_GCIO(hGXT,GetTypeName_GCIO(theClass),
1739 42 : GetSubTypeName_GCIO(theSubType),
1740 : -1,
1741 : nm,
1742 : -1,
1743 : vUnknownItemType_GCIO,
1744 : NULL,
1745 : NULL))==NULL )
1746 : {
1747 0 : CPLError( CE_Failure, CPLE_AppDefined,
1748 : "Geoconcept export syntax error at line %ld.\n",
1749 : GetGCCurrentLinenum_GCIO(hGXT) );
1750 0 : CPLFree(nm);
1751 0 : CSLDestroy(fl);
1752 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
1753 0 : return NULL;
1754 : }
1755 42 : CPLDebug("GEOCONCEPT", "%d %s.%s@%s-1 added\n", __LINE__, GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType), nm);
1756 42 : CPLFree(nm);
1757 : }
1758 4 : CSLDestroy(fl);
1759 4 : SetSubTypeHeaderWritten_GCIO(theSubType,TRUE);
1760 4 : return Meta;
1761 : }
1762 : /* end of definitions ... */ /* FIXME */
1763 0 : if( (p= strstr(GetGCCache_GCIO(hGXT),k3DOBJECTMONO_GCIO)) ||
1764 0 : (p= strstr(GetGCCache_GCIO(hGXT),k3DOBJECT_GCIO)) ||
1765 0 : (p= strstr(GetGCCache_GCIO(hGXT),k2DOBJECT_GCIO)) )
1766 : /* next reading will be in cache ! */
1767 0 : SetGCStatus_GCIO(hGXT,vMemoStatus_GCIO);
1768 : /* unknown pragma ... */
1769 0 : return Meta;
1770 : }/* _parsePragma_GCIO */
1771 :
1772 : /* -------------------------------------------------------------------- */
1773 46 : static OGRGeometryH GCIOAPI_CALL _buildOGRGeometry_GCIO (
1774 : GCExportFileMetadata* Meta,
1775 : GCSubType* theSubType,
1776 : int i,
1777 : const char** pszFields,
1778 : int nbtp,
1779 : GCDim d,
1780 : OGREnvelope* bbox
1781 : )
1782 : {
1783 : OGRGeometryH g;
1784 : OGRwkbGeometryType gt;
1785 : double x, y, z;
1786 : int ip, np, buildGeom;
1787 :
1788 46 : g= NULL;
1789 46 : if( bbox==NULL )
1790 : {
1791 27 : buildGeom= TRUE;
1792 : }
1793 : else
1794 : {
1795 19 : buildGeom= FALSE;
1796 : }
1797 46 : x= y= z= 0.0;
1798 46 : switch( GetSubTypeKind_GCIO(theSubType) )
1799 : {
1800 : case vPoint_GCIO :
1801 : case vText_GCIO :/* FIXME : treat as point ? */
1802 14 : gt= wkbPoint;
1803 14 : break;
1804 : case vLine_GCIO :
1805 0 : gt= wkbLineString;
1806 0 : break;
1807 : case vPoly_GCIO :
1808 32 : gt= wkbMultiPolygon;
1809 32 : break;
1810 : default :
1811 0 : gt= wkbUnknown;
1812 : break;
1813 : }
1814 46 : if( buildGeom )
1815 : {
1816 27 : if( !(g= OGR_G_CreateGeometry(gt)) )
1817 : {
1818 0 : return NULL;
1819 : }
1820 27 : OGR_G_SetCoordinateDimension(g,d==v3D_GCIO||d==v3DM_GCIO? 3:2);
1821 : }
1822 46 : if( !GetMetaSRS_GCIO(Meta) && GetMetaSysCoord_GCIO(Meta) )
1823 : {
1824 4 : SetMetaSRS_GCIO(Meta, SysCoord2OGRSpatialReference_GCSRS(GetMetaSysCoord_GCIO(Meta)));
1825 : }
1826 46 : if( buildGeom )
1827 : {
1828 27 : if( GetMetaSRS_GCIO(Meta) )
1829 : {
1830 27 : OGR_G_AssignSpatialReference(g,GetMetaSRS_GCIO(Meta));
1831 : }
1832 : }
1833 :
1834 : /*
1835 : * General structure :
1836 : * X<>Y[<>Z]{<>More Graphics}
1837 : */
1838 :
1839 46 : if( gt==wkbPoint )
1840 : {
1841 : /*
1842 : * More Graphics :
1843 : * Angle
1844 : * Angle in tenth of degrees (counterclockwise) of the symbol
1845 : * displayed to represent the ponctual entity or angle of the text entity
1846 : * NOT IMPLEMENTED
1847 : */
1848 14 : x= CPLAtof(pszFields[i]), i++;
1849 14 : y= CPLAtof(pszFields[i]), i++;
1850 14 : if( d==v3D_GCIO||d==v3DM_GCIO )
1851 : {
1852 0 : z= CPLAtof(pszFields[i]), i++;
1853 : }
1854 14 : if( buildGeom )
1855 : {
1856 10 : if( OGR_G_GetCoordinateDimension(g)==3 )
1857 0 : OGR_G_AddPoint(g,x,y,z);
1858 : else
1859 10 : OGR_G_AddPoint_2D(g,x,y);
1860 : }
1861 : else
1862 : {
1863 4 : MergeOGREnvelope_GCIO(bbox,x,y);
1864 : }
1865 14 : return g;
1866 : }
1867 :
1868 32 : if( gt==wkbLineString )
1869 : {
1870 : /*
1871 : * More Graphics :
1872 : * XP<>YP[<>ZP]Nr points=k[<>X<>Y[<>Z]]k...
1873 : */
1874 0 : x= CPLAtof(pszFields[i]), i++;
1875 0 : y= CPLAtof(pszFields[i]), i++;
1876 0 : if( d==v3D_GCIO||d==v3DM_GCIO )
1877 : {
1878 0 : z= CPLAtof(pszFields[i]), i++;
1879 : }
1880 0 : if( buildGeom )
1881 : {
1882 0 : if( OGR_G_GetCoordinateDimension(g)==3 )
1883 0 : OGR_G_AddPoint(g,x,y,z);
1884 : else
1885 0 : OGR_G_AddPoint_2D(g,x,y);
1886 : }
1887 : else
1888 : {
1889 0 : MergeOGREnvelope_GCIO(bbox,x,y);
1890 : }
1891 : /* skip XP<>YP[<>ZP] : the last point is in k[<>X<>Y[<>Z]]k */
1892 0 : i++;
1893 0 : i++;
1894 0 : if( d==v3D_GCIO||d==v3DM_GCIO )
1895 : {
1896 0 : i++;
1897 : }
1898 0 : np= atoi(pszFields[i]), i++;
1899 0 : for( ip= 1; ip<=np; ip++ )
1900 : {
1901 0 : x= CPLAtof(pszFields[i]), i++;
1902 0 : y= CPLAtof(pszFields[i]), i++;
1903 0 : if( d==v3D_GCIO || d==v3DM_GCIO )
1904 : {
1905 0 : z= CPLAtof(pszFields[i]), i++;
1906 : }
1907 0 : if( buildGeom )
1908 : {
1909 0 : if( OGR_G_GetCoordinateDimension(g)==3 )
1910 0 : OGR_G_AddPoint(g,x,y,z);
1911 : else
1912 0 : OGR_G_AddPoint_2D(g,x,y);
1913 : }
1914 : else
1915 : {
1916 0 : MergeOGREnvelope_GCIO(bbox,x,y);
1917 : }
1918 : }
1919 0 : return g;
1920 : }
1921 :
1922 32 : if( gt==wkbMultiPolygon )
1923 : {
1924 : /*
1925 : * More Graphics :
1926 : * {Single Polygon{<>NrPolys=j[<>X<>Y[<>Z]<>Single Polygon]j}}
1927 : * with Single Polygon :
1928 : * Nr points=k[<>X<>Y[<>Z]]k...
1929 : */
1930 : CPLList* Lpo, *e;
1931 : OGRGeometryH outer, ring;
1932 : int npo, ipo, ilpo;
1933 :
1934 :
1935 32 : Lpo= e= NULL;
1936 32 : outer= ring= NULL;
1937 32 : if( buildGeom )
1938 : {
1939 17 : if( !(outer= OGR_G_CreateGeometry(wkbPolygon)) )
1940 : {
1941 0 : goto onError;
1942 : }
1943 17 : OGR_G_SetCoordinateDimension(outer,OGR_G_GetCoordinateDimension(g));
1944 17 : if( GetMetaSRS_GCIO(Meta) )
1945 : {
1946 17 : OGR_G_AssignSpatialReference(outer,GetMetaSRS_GCIO(Meta));
1947 : }
1948 17 : if( !(ring= OGR_G_CreateGeometry(wkbLinearRing)) )
1949 : {
1950 0 : OGR_G_DestroyGeometry(outer);
1951 0 : goto onError;
1952 : }
1953 17 : OGR_G_SetCoordinateDimension(ring,OGR_G_GetCoordinateDimension(g));
1954 17 : if( GetMetaSRS_GCIO(Meta) )
1955 : {
1956 17 : OGR_G_AssignSpatialReference(ring,GetMetaSRS_GCIO(Meta));
1957 : }
1958 : }
1959 32 : x= CPLAtof(pszFields[i]), i++;
1960 32 : y= CPLAtof(pszFields[i]), i++;
1961 32 : if( d==v3D_GCIO||d==v3DM_GCIO )
1962 : {
1963 0 : z= CPLAtof(pszFields[i]), i++;
1964 : }
1965 32 : if( buildGeom )
1966 : {
1967 17 : if( OGR_G_GetCoordinateDimension(g)==3 )
1968 0 : OGR_G_AddPoint(ring,x,y,z);
1969 : else
1970 17 : OGR_G_AddPoint_2D(ring,x,y);
1971 : }
1972 : else
1973 : {
1974 15 : MergeOGREnvelope_GCIO(bbox,x,y);
1975 : }
1976 32 : np= atoi(pszFields[i]), i++;
1977 160 : for( ip= 1; ip<=np; ip++ )
1978 : {
1979 128 : x= CPLAtof(pszFields[i]), i++;
1980 128 : y= CPLAtof(pszFields[i]), i++;
1981 128 : if( d==v3D_GCIO||d==v3DM_GCIO )
1982 : {
1983 0 : z= CPLAtof(pszFields[i]), i++;
1984 : }
1985 128 : if( buildGeom )
1986 : {
1987 68 : if( OGR_G_GetCoordinateDimension(g)==3 )
1988 0 : OGR_G_AddPoint(ring,x,y,z);
1989 : else
1990 68 : OGR_G_AddPoint_2D(ring,x,y);
1991 : }
1992 : else
1993 : {
1994 60 : MergeOGREnvelope_GCIO(bbox,x,y);
1995 : }
1996 : }
1997 32 : if( buildGeom )
1998 : {
1999 17 : OGR_G_AddGeometryDirectly(outer,ring);
2000 17 : if( (Lpo= CPLListAppend(Lpo,outer))==NULL )
2001 : {
2002 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
2003 : "failed to add a polygon to subtype '%s.%s'.\n",
2004 0 : GetTypeName_GCIO(GetSubTypeType_GCIO(theSubType)),
2005 : GetSubTypeName_GCIO(theSubType) );
2006 0 : OGR_G_DestroyGeometry(outer);
2007 0 : goto onError;
2008 : }
2009 : }
2010 : /* additionnal ring : either holes, or islands */
2011 32 : if( i < nbtp-1 )
2012 : {
2013 0 : npo= atoi(pszFields[i]), i++;
2014 0 : for( ipo= 1; ipo<=npo; ipo++ )
2015 : {
2016 0 : if( buildGeom )
2017 : {
2018 0 : if( !(ring= OGR_G_CreateGeometry(wkbLinearRing)) )
2019 : {
2020 0 : goto onError;
2021 : }
2022 0 : OGR_G_SetCoordinateDimension(ring,OGR_G_GetCoordinateDimension(g));
2023 0 : if( GetMetaSRS_GCIO(Meta) )
2024 : {
2025 0 : OGR_G_AssignSpatialReference(ring,GetMetaSRS_GCIO(Meta));
2026 : }
2027 : }
2028 0 : x= CPLAtof(pszFields[i]), i++;
2029 0 : y= CPLAtof(pszFields[i]), i++;
2030 0 : if( d==v3D_GCIO||d==v3DM_GCIO )
2031 : {
2032 0 : z= CPLAtof(pszFields[i]), i++;
2033 : }
2034 0 : if( buildGeom )
2035 : {
2036 0 : if( OGR_G_GetCoordinateDimension(g)==3 )
2037 0 : OGR_G_AddPoint(ring,x,y,z);
2038 : else
2039 0 : OGR_G_AddPoint_2D(ring,x,y);
2040 : }
2041 : else
2042 : {
2043 0 : MergeOGREnvelope_GCIO(bbox,x,y);
2044 : }
2045 0 : np= atoi(pszFields[i]), i++;
2046 0 : for( ip= 1; ip<=np; ip++ )
2047 : {
2048 0 : x= CPLAtof(pszFields[i]), i++;
2049 0 : y= CPLAtof(pszFields[i]), i++;
2050 0 : if( d==v3D_GCIO||d==v3DM_GCIO )
2051 : {
2052 0 : z= CPLAtof(pszFields[i]), i++;
2053 : }
2054 0 : if( buildGeom )
2055 : {
2056 0 : if( OGR_G_GetCoordinateDimension(g)==3 )
2057 0 : OGR_G_AddPoint(ring,x,y,z);
2058 : else
2059 0 : OGR_G_AddPoint_2D(ring,x,y);
2060 : }
2061 : else
2062 : {
2063 0 : MergeOGREnvelope_GCIO(bbox,x,y);
2064 : }
2065 : }
2066 0 : if( buildGeom )
2067 : {
2068 : /* is the ring of hole or another polygon ? */
2069 0 : for( ilpo= 0; ilpo<CPLListCount(Lpo); ilpo++)
2070 : {
2071 0 : if( (e= CPLListGet(Lpo,ilpo)) )
2072 : {
2073 0 : if( (outer= (OGRGeometryH)CPLListGetData(e)) )
2074 : {
2075 0 : if( OGR_G_Contains(outer,ring) )
2076 : {
2077 0 : OGR_G_AddGeometryDirectly(outer,ring);
2078 0 : ring= NULL;
2079 0 : break;
2080 : }
2081 : }
2082 : }
2083 : }
2084 0 : if( !ring )
2085 : {
2086 : /* new polygon */
2087 0 : if( !(outer= OGR_G_CreateGeometry(wkbPolygon)) )
2088 : {
2089 0 : OGR_G_DestroyGeometry(ring);
2090 0 : goto onError;
2091 : }
2092 0 : OGR_G_SetCoordinateDimension(outer,OGR_G_GetCoordinateDimension(g));
2093 0 : if( GetMetaSRS_GCIO(Meta) )
2094 : {
2095 0 : OGR_G_AssignSpatialReference(outer,GetMetaSRS_GCIO(Meta));
2096 : }
2097 0 : OGR_G_AddGeometryDirectly(outer,ring);
2098 0 : if( (Lpo= CPLListAppend(Lpo,outer))==NULL )
2099 : {
2100 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
2101 : "failed to add a polygon to subtype '%s.%s'.\n",
2102 0 : GetTypeName_GCIO(GetSubTypeType_GCIO(theSubType)),
2103 : GetSubTypeName_GCIO(theSubType) );
2104 0 : OGR_G_DestroyGeometry(outer);
2105 0 : goto onError;
2106 : }
2107 : }
2108 : }
2109 : }
2110 : }
2111 32 : if( Lpo )
2112 : {
2113 17 : if( (npo= CPLListCount(Lpo))>0 )
2114 : {
2115 34 : for (ipo= 0; ipo<npo; ipo++)
2116 : {
2117 17 : if( (e= CPLListGet(Lpo,ipo)) )
2118 : {
2119 17 : if( (outer= (OGRGeometryH)CPLListGetData(e)) )
2120 : {
2121 17 : OGR_G_AddGeometryDirectly(g,outer);
2122 : }
2123 : }
2124 : }
2125 : }
2126 17 : CPLListDestroy(Lpo);
2127 : }
2128 32 : return g;
2129 :
2130 : onError:
2131 0 : if( Lpo )
2132 : {
2133 0 : if( (npo= CPLListCount(Lpo))>0 )
2134 : {
2135 0 : for (ipo= 0; ipo<npo; ipo++)
2136 : {
2137 0 : if( (e= CPLListGet(Lpo,ipo)) )
2138 : {
2139 0 : if( (outer= (OGRGeometryH)CPLListGetData(e)) )
2140 : {
2141 0 : OGR_G_DestroyGeometry(outer);
2142 : }
2143 : }
2144 : }
2145 : }
2146 0 : CPLListDestroy(Lpo);
2147 : }
2148 0 : if( g ) OGR_G_DestroyGeometry(g);
2149 : }
2150 :
2151 0 : return NULL;
2152 : }/* _buildOGRGeometry_GCIO */
2153 :
2154 : /* -------------------------------------------------------------------- */
2155 46 : static OGRFeatureH GCIOAPI_CALL _buildOGRFeature_GCIO (
2156 : GCExportFileH* H,
2157 : GCSubType** theSubType,
2158 : GCDim d,
2159 : OGREnvelope* bbox
2160 : )
2161 : {
2162 : GCExportFileMetadata* Meta;
2163 : char **pszFields, delim[2], tdst[kItemSize_GCIO];
2164 : int whereClass, whereSubType, i, j, nbstf, nbf, nbtf, buildFeature;
2165 : GCType* theClass;
2166 : GCField* theField;
2167 : OGRFieldDefnH fld;
2168 : OGRFeatureDefnH fd;
2169 : OGRFeatureH f;
2170 : OGRGeometryH g;
2171 46 : int bTokenBehaviour= CSLT_ALLOWEMPTYTOKENS;
2172 :
2173 46 : fd= NULL;
2174 46 : f= NULL;
2175 46 : Meta= GetGCMeta_GCIO(H);
2176 46 : delim[0]= GetMetaDelimiter_GCIO(Meta), delim[1]= '\0';
2177 46 : if( d==vUnknown3D_GCIO) d= v2D_GCIO;
2178 46 : if( bbox==NULL )
2179 : {
2180 27 : buildFeature= TRUE;
2181 : }
2182 : else
2183 : {
2184 19 : buildFeature= FALSE;
2185 : }
2186 46 : CPLDebug("GEOCONCEPT", "buildFeature is %s", buildFeature? "true":"false");
2187 :
2188 : /* due to the order of fields, we know how to proceed : */
2189 : /* A.- Line syntax : */
2190 : /* Object internal identifier <delimiter> */
2191 : /* Class <delimiter> */
2192 : /* Subclass <delimiter> */
2193 : /* Name <delimiter> */
2194 : /* NbFields <delimiter> */
2195 : /* User's field <delimiter> [0..N] */
2196 : /* Graphics */
2197 : /* Graphics depends on the Kind of the */
2198 : /* B.- Algorithm : */
2199 : /* 1.- Get Class */
2200 : /* 2.- Get Subclass */
2201 : /* 3.- Find feature in schema */
2202 : /* 4.- Get Kind */
2203 : /* 5.- Get NbFields */
2204 : /* 6.- Get Geometry as 5+NbFields field */
2205 : /* 7.- Parse Geometry and build OGRGeometryH */
2206 : /* 8.- Compute extent and update file extent */
2207 : /* 9.- increment number of features */
2208 : /* FIXME : add index when reading feature to */
2209 : /* allow direct access ! */
2210 46 : if( GetMetaQuotedText_GCIO(Meta) )
2211 : {
2212 0 : bTokenBehaviour|= CSLT_HONOURSTRINGS;
2213 : }
2214 46 : CPLDebug("GEOCONCEPT","Cache=[%s] delim=[%s]", GetGCCache_GCIO(H), delim);
2215 46 : if( !(pszFields= CSLTokenizeString2(GetGCCache_GCIO(H),
2216 : delim,
2217 : bTokenBehaviour)) )
2218 : {
2219 0 : CPLError( CE_Failure, CPLE_AppDefined,
2220 : "Line %ld, Geoconcept line syntax is wrong.\n",
2221 : GetGCCurrentLinenum_GCIO(H) );
2222 0 : return NULL;
2223 : }
2224 46 : if( (nbtf= CSLCount(pszFields)) <= 5 )
2225 : {
2226 0 : CSLDestroy(pszFields);
2227 0 : CPLError( CE_Failure, CPLE_AppDefined,
2228 : "Line %ld, Missing fields (at least 5 are expected, %d found).\n",
2229 : GetGCCurrentLinenum_GCIO(H), nbtf );
2230 0 : return NULL;
2231 : }
2232 : /* Class */
2233 46 : if( (whereClass = _findTypeByName_GCIO(H,pszFields[1]))==-1 )
2234 : {
2235 0 : if( CPLListCount(GetMetaTypes_GCIO(Meta))==0 )
2236 : {
2237 0 : CPLError( CE_Failure, CPLE_AppDefined,
2238 : "Line %ld, %s%s pragma expected fro type definition before objects dump.",
2239 : GetGCCurrentLinenum_GCIO(H), kPragma_GCIO, kMetadataFIELDS_GCIO );
2240 : }
2241 : else
2242 : {
2243 0 : CPLError( CE_Failure, CPLE_AppDefined,
2244 : "Line %ld, Unknown type '%s'.\n",
2245 0 : GetGCCurrentLinenum_GCIO(H), pszFields[1] );
2246 : }
2247 0 : CSLDestroy(pszFields);
2248 0 : return NULL;
2249 : }
2250 46 : theClass= _getType_GCIO(H,whereClass);
2251 46 : if( *theSubType )
2252 : {
2253 : /* reading ... */
2254 27 : if( !EQUAL(GetTypeName_GCIO(GetSubTypeType_GCIO(*theSubType)),GetTypeName_GCIO(theClass)) )
2255 : {
2256 0 : CSLDestroy(pszFields);
2257 0 : return NULL;
2258 : }
2259 : }
2260 : /* Subclass */
2261 46 : if( (whereSubType= _findSubTypeByName_GCIO(theClass,pszFields[2]))==-1 )
2262 : {
2263 0 : CPLError( CE_Failure, CPLE_AppDefined,
2264 : "Line %ld, Unknown subtype found '%s' for type '%s'.\n",
2265 0 : GetGCCurrentLinenum_GCIO(H), pszFields[2], pszFields[1] );
2266 0 : CSLDestroy(pszFields);
2267 0 : return NULL;
2268 : }
2269 46 : if( *theSubType )
2270 : {
2271 27 : if( !EQUAL(GetSubTypeName_GCIO(_getSubType_GCIO(theClass,whereSubType)),GetSubTypeName_GCIO(*theSubType)) )
2272 : {
2273 0 : CSLDestroy(pszFields);
2274 0 : return NULL;
2275 : }
2276 : }
2277 : else
2278 : {
2279 19 : *theSubType= _getSubType_GCIO(theClass,whereSubType);
2280 : }
2281 46 : snprintf(tdst, kItemSize_GCIO-1, "%s.%s", GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(*theSubType));
2282 46 : tdst[kItemSize_GCIO-1]= '\0';
2283 : /* Name */
2284 46 : if( _findFieldByName_GCIO(GetSubTypeFields_GCIO(*theSubType),kName_GCIO)==-1 )
2285 : {
2286 0 : CPLError( CE_Failure, CPLE_AppDefined,
2287 : "Line %ld, missing mandatory field %s for type '%s'.\n",
2288 : GetGCCurrentLinenum_GCIO(H), kName_GCIO, tdst );
2289 0 : CSLDestroy(pszFields);
2290 0 : return NULL;
2291 : }
2292 46 : nbf= 4;
2293 : /* NbFields */
2294 46 : nbstf= GetSubTypeNbFields_GCIO(*theSubType);
2295 46 : if( nbstf==-1 )
2296 : {
2297 : /* figure out how many user's attributes we've got : */
2298 4 : i= 1 + nbf, nbstf= 0;
2299 20 : while( (theField= GetSubTypeField_GCIO(*theSubType,i)) )
2300 : {
2301 16 : if( IsPrivateField_GCIO(theField) ) { break; };//FIXME: could count geometry private fields ...
2302 12 : nbstf++;
2303 12 : SetSubTypeNbFields_GCIO(*theSubType, nbstf);
2304 12 : i++;
2305 : }
2306 : }
2307 46 : if( nbtf < 1 + nbf + nbstf + 1 )
2308 : {
2309 0 : CPLError( CE_Failure, CPLE_AppDefined,
2310 : "Line %ld, Total number of fields differs with type definition '%s' (%d found, at least %d expected).\n",
2311 0 : GetGCCurrentLinenum_GCIO(H), tdst, nbtf, 1+nbf+nbstf+1 );
2312 0 : CSLDestroy(pszFields);
2313 0 : return NULL;
2314 : }
2315 46 : i= atoi(pszFields[nbf]);
2316 46 : if( i!=nbstf )
2317 : {
2318 0 : CPLError( CE_Failure, CPLE_AppDefined,
2319 : "Line %ld, Number of user's fields differs with type definition '%s' (%d found, %d expected).\n",
2320 : GetGCCurrentLinenum_GCIO(H), tdst, i, nbstf );
2321 0 : CSLDestroy(pszFields);
2322 0 : return NULL;
2323 : }
2324 : /*
2325 : * the Subclass has no definition : let's build one
2326 : */
2327 46 : if( !(fd= GetSubTypeFeatureDefn_GCIO(*theSubType)) )
2328 : {
2329 4 : if( !(fd= OGR_FD_Create(tdst)) )
2330 : {
2331 0 : CSLDestroy(pszFields);
2332 0 : return NULL;
2333 : }
2334 :
2335 4 : switch( GetSubTypeKind_GCIO(*theSubType) )
2336 : {
2337 : case vPoint_GCIO :
2338 : case vText_GCIO :/* FIXME : treat as point ? */
2339 2 : switch( d )
2340 : {
2341 : case v3D_GCIO :
2342 : case v3DM_GCIO :
2343 0 : OGR_FD_SetGeomType(fd,wkbPoint25D);
2344 0 : break;
2345 : default :
2346 2 : OGR_FD_SetGeomType(fd,wkbPoint);
2347 : break;
2348 : }
2349 2 : break;
2350 : case vLine_GCIO :
2351 0 : switch( d )
2352 : {
2353 : case v3D_GCIO :
2354 : case v3DM_GCIO :
2355 0 : OGR_FD_SetGeomType(fd,wkbLineString25D);
2356 0 : break;
2357 : default :
2358 0 : OGR_FD_SetGeomType(fd,wkbLineString);
2359 : break;
2360 : }
2361 0 : break;
2362 : case vPoly_GCIO :
2363 2 : switch( d )
2364 : {
2365 : case v3D_GCIO :
2366 : case v3DM_GCIO :
2367 0 : OGR_FD_SetGeomType(fd,wkbMultiPolygon25D);
2368 0 : break;
2369 : default :
2370 2 : OGR_FD_SetGeomType(fd,wkbMultiPolygon);
2371 : break;
2372 : }
2373 2 : break;
2374 : default :
2375 0 : CSLDestroy(pszFields);
2376 0 : OGR_FD_Destroy(fd);
2377 0 : CPLError( CE_Failure, CPLE_NotSupported,
2378 : "Unknown Geoconcept type for '%s'.\n",
2379 : tdst );
2380 0 : return NULL;
2381 : }
2382 16 : for( i= 1 + nbf; i<1 + nbf + nbstf; i++ )
2383 : {
2384 12 : theField= GetSubTypeField_GCIO(*theSubType,i);
2385 12 : if( !(fld= OGR_Fld_Create(GetFieldName_GCIO(theField),OFTString)) ) /* FIXME */
2386 : {
2387 0 : CSLDestroy(pszFields);
2388 0 : OGR_FD_Destroy(fd);
2389 0 : return NULL;
2390 : }
2391 12 : OGR_FD_AddFieldDefn(fd,fld);
2392 12 : OGR_Fld_Destroy(fld);
2393 12 : fld= NULL;
2394 : }
2395 : }
2396 :
2397 : /*
2398 : * the Subclass is just under parsing via _parseObject_GCIO
2399 : */
2400 46 : if( buildFeature )
2401 : {
2402 27 : if( !(f= OGR_F_Create(fd)) )
2403 : {
2404 0 : if( !GetSubTypeFeatureDefn_GCIO(*theSubType) )
2405 0 : OGR_FD_Destroy(fd);
2406 0 : CSLDestroy(pszFields);
2407 0 : return NULL;
2408 : }
2409 27 : OGR_F_SetFID(f, atol(pszFields[0])); /* FID */
2410 27 : if( OGR_F_GetFID(f)==OGRNullFID )
2411 : {
2412 27 : OGR_F_SetFID(f, GetGCCurrentLinenum_GCIO(H));
2413 : }
2414 108 : for( i= 1 + nbf, j= 0; i<1 + nbf + nbstf; i++, j++ )
2415 : {
2416 81 : theField= GetSubTypeField_GCIO(*theSubType,i);
2417 81 : if( pszFields[i][0]=='\0' )
2418 22 : OGR_F_UnsetField(f,j);
2419 : else
2420 59 : OGR_F_SetFieldString(f,j,pszFields[i]);
2421 : }
2422 : }
2423 : else
2424 : {
2425 19 : i= 1 + nbf + nbstf;
2426 : }
2427 46 : CPLDebug("GEOCONCEPT", "%d %d/%d/%d/%d\n", __LINE__, i, nbf, nbstf, nbtf);
2428 46 : if( !(g= _buildOGRGeometry_GCIO(Meta,*theSubType,i,(const char **)pszFields,nbtf,d,bbox)) )
2429 : {
2430 : /*
2431 : * the Subclass is under reading via ReadNextFeature_GCIO
2432 : */
2433 19 : if( buildFeature )
2434 : {
2435 0 : CSLDestroy(pszFields);
2436 0 : if( f ) OGR_F_Destroy(f);
2437 0 : return NULL;
2438 : }
2439 : }
2440 46 : if( buildFeature )
2441 : {
2442 27 : if( OGR_F_SetGeometryDirectly(f,g)!=OGRERR_NONE )
2443 : {
2444 0 : CSLDestroy(pszFields);
2445 0 : if( f ) OGR_F_Destroy(f);
2446 0 : return NULL;
2447 : }
2448 : }
2449 46 : CSLDestroy(pszFields);
2450 :
2451 : /* Assign definition : */
2452 46 : if( !GetSubTypeFeatureDefn_GCIO(*theSubType) )
2453 : {
2454 4 : SetSubTypeFeatureDefn_GCIO(*theSubType, fd);
2455 4 : OGR_FD_Reference(fd);
2456 : }
2457 :
2458 : /*
2459 : * returns either the built object for ReadNextFeature_GCIO or
2460 : * the feature definition for _parseObject_GCIO :
2461 : */
2462 46 : return buildFeature? f:(OGRFeatureH)fd;
2463 : }/* _buildOGRFeature_GCIO */
2464 :
2465 : /* -------------------------------------------------------------------- */
2466 19 : static GCExportFileMetadata GCIOAPI_CALL1(*) _parseObject_GCIO (
2467 : GCExportFileH* H
2468 : )
2469 : {
2470 : GCExportFileMetadata* Meta;
2471 : GCSubType* theSubType;
2472 : GCDim d;
2473 : long coff;
2474 19 : OGREnvelope bbox, *pszBbox= &bbox;
2475 :
2476 19 : Meta= GetGCMeta_GCIO(H);
2477 :
2478 19 : InitOGREnvelope_GCIO(pszBbox);
2479 :
2480 19 : d= vUnknown3D_GCIO;
2481 19 : theSubType= NULL;
2482 19 : coff= -1L;
2483 : reloop:
2484 19 : if( GetGCWhatIs_GCIO(H)==vComType_GCIO )
2485 : {
2486 0 : if( _get_GCIO(H)==-1 )
2487 0 : return Meta;
2488 0 : goto reloop;
2489 : }
2490 : /* analyze the line according to schema : */
2491 19 : if( GetGCWhatIs_GCIO(H)==vPragma_GCIO )
2492 : {
2493 0 : if( strstr(GetGCCache_GCIO(H),k3DOBJECTMONO_GCIO) )
2494 : {
2495 0 : d= v3DM_GCIO;
2496 0 : coff= GetGCCurrentOffset_GCIO(H);
2497 : }
2498 0 : else if( strstr(GetGCCache_GCIO(H),k3DOBJECT_GCIO) )
2499 : {
2500 0 : d= v3D_GCIO;
2501 0 : coff= GetGCCurrentOffset_GCIO(H);
2502 : }
2503 0 : else if( strstr(GetGCCache_GCIO(H),k2DOBJECT_GCIO) )
2504 : {
2505 0 : d= v2D_GCIO;
2506 0 : coff= GetGCCurrentOffset_GCIO(H);
2507 : }
2508 : else
2509 : {
2510 : /* not an object pragma ... */
2511 0 : SetGCStatus_GCIO(H,vMemoStatus_GCIO);
2512 0 : return Meta;
2513 : }
2514 0 : if( _get_GCIO(H)==-1 )
2515 0 : return Meta;
2516 0 : goto reloop;
2517 : }
2518 19 : if( coff==-1L) coff= GetGCCurrentOffset_GCIO(H);
2519 19 : if( !_buildOGRFeature_GCIO(H,&theSubType,d,pszBbox) )
2520 : {
2521 0 : return NULL;
2522 : }
2523 19 : if( GetSubTypeBOF_GCIO(theSubType)==-1L )
2524 : {
2525 4 : SetSubTypeBOF_GCIO(theSubType,coff);/* Begin Of Features for the Class.Subclass */
2526 4 : SetSubTypeBOFLinenum_GCIO(theSubType, GetGCCurrentLinenum_GCIO(H));
2527 12 : CPLDebug("GEOCONCEPT","Feature Type [%s] starts at #%ld, line %ld\n",
2528 4 : GetSubTypeName_GCIO(theSubType),
2529 4 : GetSubTypeBOF_GCIO(theSubType),
2530 4 : GetSubTypeBOFLinenum_GCIO(theSubType));
2531 : }
2532 19 : SetSubTypeNbFeatures_GCIO(theSubType, GetSubTypeNbFeatures_GCIO(theSubType)+1L);
2533 19 : SetGCNbObjects_GCIO(H,GetGCNbObjects_GCIO(H)+1L);
2534 : /* update bbox of both feature and file */
2535 19 : SetExtentULAbscissa_GCIO(GetMetaExtent_GCIO(Meta),pszBbox->MinX);
2536 19 : SetExtentULOrdinate_GCIO(GetMetaExtent_GCIO(Meta),pszBbox->MaxY);
2537 19 : SetExtentLRAbscissa_GCIO(GetMetaExtent_GCIO(Meta),pszBbox->MaxX);
2538 19 : SetExtentLROrdinate_GCIO(GetMetaExtent_GCIO(Meta),pszBbox->MinY);
2539 19 : if( !GetSubTypeExtent_GCIO(theSubType) )
2540 : {
2541 4 : SetSubTypeExtent_GCIO(theSubType, CreateExtent_GCIO(HUGE_VAL,HUGE_VAL,-HUGE_VAL,-HUGE_VAL));
2542 : }
2543 19 : SetExtentULAbscissa_GCIO(GetSubTypeExtent_GCIO(theSubType),pszBbox->MinX);
2544 19 : SetExtentULOrdinate_GCIO(GetSubTypeExtent_GCIO(theSubType),pszBbox->MaxY);
2545 19 : SetExtentLRAbscissa_GCIO(GetSubTypeExtent_GCIO(theSubType),pszBbox->MaxX);
2546 19 : SetExtentLROrdinate_GCIO(GetSubTypeExtent_GCIO(theSubType),pszBbox->MinY);
2547 19 : if( d==vUnknown3D_GCIO && GetSubTypeDim_GCIO(theSubType)==vUnknown3D_GCIO )
2548 : {
2549 4 : switch( d )
2550 : {
2551 : case v3DM_GCIO :
2552 : case v3D_GCIO :
2553 0 : SetSubTypeDim_GCIO(theSubType,v3D_GCIO);
2554 0 : break;
2555 : default :
2556 4 : SetSubTypeDim_GCIO(theSubType,v2D_GCIO);
2557 : break;
2558 : }
2559 : }
2560 19 : d= vUnknown3D_GCIO;
2561 19 : theSubType= NULL;
2562 :
2563 19 : return Meta;
2564 : }/* _parseObject_GCIO */
2565 :
2566 : /* -------------------------------------------------------------------- */
2567 7 : GCExportFileH GCIOAPI_CALL1(*) Open_GCIO (
2568 : const char* pszGeoconceptFile,
2569 : const char* ext, /* gxt if NULL */
2570 : const char* mode,
2571 : const char* gctPath /* if !NULL, mode=w */
2572 : )
2573 : {
2574 : GCExportFileH* hGXT;
2575 :
2576 7 : CPLDebug( "GEOCONCEPT", "filename '%s' - '%s' - mode '%s' - config path '%s'",
2577 : pszGeoconceptFile? pszGeoconceptFile:"???",
2578 : ext? ext:"gxt",
2579 : mode? mode:"???",
2580 : gctPath? gctPath:"???" );
2581 :
2582 7 : if( !(hGXT= _Create_GCIO(pszGeoconceptFile,ext,mode)) )
2583 : {
2584 0 : return NULL;
2585 : }
2586 :
2587 7 : if( GetGCMode_GCIO(hGXT)==vUpdateAccess_GCIO )
2588 : {
2589 : FILE* h;
2590 :
2591 : /* file must exists ... */
2592 0 : if( !(h= VSIFOpen(CPLFormFilename(GetGCPath_GCIO(hGXT),GetGCBasename_GCIO(hGXT),GetGCExtension_GCIO(hGXT)), "rt")) )
2593 : {
2594 0 : _Destroy_GCIO(&hGXT,FALSE);
2595 0 : return NULL;
2596 : }
2597 : }
2598 :
2599 7 : SetGCHandle_GCIO(hGXT, VSIFOpen(CPLFormFilename(GetGCPath_GCIO(hGXT),GetGCBasename_GCIO(hGXT),GetGCExtension_GCIO(hGXT)), mode));
2600 7 : if( !GetGCHandle_GCIO(hGXT) )
2601 : {
2602 0 : _Destroy_GCIO(&hGXT,FALSE);
2603 0 : return NULL;
2604 : }
2605 :
2606 7 : if( GetGCMode_GCIO(hGXT)==vWriteAccess_GCIO )
2607 : {
2608 1 : if( gctPath!=NULL )
2609 : {
2610 : /* load Metadata */
2611 : GCExportFileH* hGCT;
2612 :
2613 0 : hGCT= _Create_GCIO(gctPath,"gct","-");
2614 0 : SetGCHandle_GCIO(hGCT, VSIFOpen(CPLFormFilename(GetGCPath_GCIO(hGCT),GetGCBasename_GCIO(hGCT),GetGCExtension_GCIO(hGCT)), "r"));
2615 0 : if( !GetGCHandle_GCIO(hGCT) )
2616 : {
2617 0 : CPLError( CE_Failure, CPLE_NotSupported,
2618 : "opening a Geoconcept config file '%s' failed.\n",
2619 : gctPath);
2620 0 : _Destroy_GCIO(&hGCT,FALSE);
2621 0 : _Destroy_GCIO(&hGXT,TRUE);
2622 0 : return NULL;
2623 : }
2624 0 : if( ReadConfig_GCIO(hGCT)==NULL )
2625 : {
2626 0 : _Destroy_GCIO(&hGCT,FALSE);
2627 0 : _Destroy_GCIO(&hGXT,TRUE);
2628 0 : return NULL;
2629 : }
2630 0 : SetGCMeta_GCIO(hGXT, GetGCMeta_GCIO(hGCT));
2631 0 : SetGCMeta_GCIO(hGCT, NULL);
2632 0 : _Destroy_GCIO(&hGCT,FALSE);
2633 0 : SetMetaExtent_GCIO(GetGCMeta_GCIO(hGXT), CreateExtent_GCIO(HUGE_VAL,HUGE_VAL,-HUGE_VAL,-HUGE_VAL));
2634 : }
2635 : }
2636 : else
2637 : {
2638 : /* read basic Metadata from export */
2639 : /* read and parse the export file ... */
2640 6 : if( ReadHeader_GCIO(hGXT)==NULL )
2641 : {
2642 2 : _Destroy_GCIO(&hGXT,FALSE);
2643 2 : return NULL;
2644 : }
2645 : }
2646 : /* check schema */
2647 5 : if( !_checkSchema_GCIO(hGXT) )
2648 : {
2649 0 : _Destroy_GCIO(&hGXT,GetGCMode_GCIO(hGXT)==vWriteAccess_GCIO? TRUE:FALSE);
2650 0 : return NULL;
2651 : }
2652 :
2653 25 : CPLDebug( "GEOCONCEPT",
2654 : "Export =(\n"
2655 : " Path : %s\n"
2656 : " Basename : %s\n"
2657 : " Extension : %s\n"
2658 : " Mode : %s\n"
2659 : " Status : %s\n"
2660 : ")",
2661 5 : GetGCPath_GCIO(hGXT),
2662 5 : GetGCBasename_GCIO(hGXT),
2663 5 : GetGCExtension_GCIO(hGXT),
2664 5 : GCAccessMode2str_GCIO(GetGCMode_GCIO(hGXT)),
2665 5 : GCAccessStatus2str_GCIO(GetGCStatus_GCIO(hGXT))
2666 : );
2667 :
2668 5 : return hGXT;
2669 : }/* Open_GCIO */
2670 :
2671 : /* -------------------------------------------------------------------- */
2672 5 : void GCIOAPI_CALL Close_GCIO (
2673 : GCExportFileH** hGXT
2674 : )
2675 : {
2676 5 : _Destroy_GCIO(hGXT,FALSE);
2677 5 : }/* Close_GCIO */
2678 :
2679 : /* -------------------------------------------------------------------- */
2680 16 : GCExportFileH GCIOAPI_CALL1(*) Rewind_GCIO (
2681 : GCExportFileH* hGXT,
2682 : GCSubType* theSubType
2683 : )
2684 : {
2685 16 : if( hGXT )
2686 : {
2687 16 : if( GetGCHandle_GCIO(hGXT) )
2688 : {
2689 16 : if( !theSubType )
2690 : {
2691 10 : VSIRewind(GetGCHandle_GCIO(hGXT));
2692 10 : SetGCCurrentLinenum_GCIO(hGXT, 0L);
2693 : }
2694 : else
2695 : {
2696 6 : VSIFSeek(GetGCHandle_GCIO(hGXT), GetSubTypeBOF_GCIO(theSubType), SEEK_SET);
2697 6 : SetGCCurrentLinenum_GCIO(hGXT, GetSubTypeBOFLinenum_GCIO(theSubType));
2698 : }
2699 16 : SetGCStatus_GCIO(hGXT,vNoStatus_GCIO);
2700 : }
2701 : }
2702 16 : return hGXT;
2703 : }/* Rewind_GCIO */
2704 :
2705 : /* -------------------------------------------------------------------- */
2706 0 : GCExportFileH GCIOAPI_CALL1(*) FFlush_GCIO (
2707 : GCExportFileH* hGXT
2708 : )
2709 : {
2710 0 : if( hGXT )
2711 : {
2712 0 : if( GetGCHandle_GCIO(hGXT) )
2713 : {
2714 0 : VSIFFlush(GetGCHandle_GCIO(hGXT));
2715 : }
2716 : }
2717 0 : return hGXT;
2718 : }/* FFlush_GCIO */
2719 :
2720 : /* -------------------------------------------------------------------- */
2721 0 : GCAccessMode GCIOAPI_CALL GetMode_GCIO (
2722 : GCExportFileH* hGXT
2723 : )
2724 : {
2725 0 : return hGXT? GetGCMode_GCIO(hGXT):vUnknownAccessMode_GCIO;
2726 : }/* GetMode_GCIO */
2727 :
2728 : /* -------------------------------------------------------------------- */
2729 5 : GCSubType GCIOAPI_CALL1(*) AddSubType_GCIO (
2730 : GCExportFileH* H,
2731 : const char* typName,
2732 : const char* subtypName,
2733 : long id,
2734 : GCTypeKind knd,
2735 : GCDim sys
2736 : )
2737 : {
2738 : int whereClass;
2739 : GCType* theClass;
2740 : GCSubType* theSubType;
2741 : CPLList* L;
2742 :
2743 5 : if( (whereClass = _findTypeByName_GCIO(H,typName))==-1 )
2744 : {
2745 0 : CPLError( CE_Failure, CPLE_AppDefined,
2746 : "failed to find a Geoconcept type for '%s.%s#%ld'.\n",
2747 : typName, subtypName, id);
2748 0 : return NULL;
2749 : }
2750 :
2751 5 : theClass= _getType_GCIO(H,whereClass);
2752 5 : if( GetTypeSubtypes_GCIO(theClass) )
2753 : {
2754 0 : if( _findSubTypeByName_GCIO(theClass,subtypName)!=-1 )
2755 : {
2756 0 : CPLError( CE_Failure, CPLE_AppDefined,
2757 : "Geoconcept subtype '%s.%s#%ld' already exists.\n",
2758 : typName, subtypName, id);
2759 0 : return NULL;
2760 : }
2761 : }
2762 :
2763 5 : if( !(theSubType= _CreateSubType_GCIO(subtypName,id,knd,sys)) )
2764 : {
2765 0 : return NULL;
2766 : }
2767 5 : if( (L= CPLListAppend(GetTypeSubtypes_GCIO(theClass),theSubType))==NULL )
2768 : {
2769 0 : _DestroySubType_GCIO(&theSubType);
2770 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
2771 : "failed to add a Geoconcept subtype for '%s.%s#%ld'.\n",
2772 : typName, subtypName, id);
2773 0 : return NULL;
2774 : }
2775 5 : SetTypeSubtypes_GCIO(theClass, L);
2776 5 : SetSubTypeType_GCIO(theSubType, theClass);
2777 :
2778 5 : CPLDebug("GEOCONCEPT", "SubType '%s.%s#%ld' added.", typName, subtypName, id);
2779 :
2780 5 : return theSubType;
2781 : }/* AddSubType_GCIO */
2782 :
2783 : /* -------------------------------------------------------------------- */
2784 0 : static void GCIOAPI_CALL _dropSubType_GCIO (
2785 : GCSubType** theSubType
2786 : )
2787 : {
2788 : GCType* theClass;
2789 : int where;
2790 :
2791 0 : if( !theSubType || !(*theSubType) ) return;
2792 0 : if( !(theClass= GetSubTypeType_GCIO(*theSubType)) ) return;
2793 0 : if( (where= _findSubTypeByName_GCIO(theClass,GetSubTypeName_GCIO(*theSubType)))==-1 )
2794 : {
2795 0 : CPLError( CE_Failure, CPLE_AppDefined,
2796 : "subtype %s does not exist.\n",
2797 0 : GetSubTypeName_GCIO(*theSubType)? GetSubTypeName_GCIO(*theSubType):"''");
2798 0 : return;
2799 : }
2800 0 : CPLListRemove(GetTypeSubtypes_GCIO(theClass),where);
2801 0 : _DestroySubType_GCIO(theSubType);
2802 :
2803 0 : return;
2804 : }/* _dropSubType_GCIO */
2805 :
2806 : /* -------------------------------------------------------------------- */
2807 5 : GCType GCIOAPI_CALL1(*) AddType_GCIO (
2808 : GCExportFileH* H,
2809 : const char* typName,
2810 : long id
2811 : )
2812 : {
2813 : GCType* theClass;
2814 : CPLList* L;
2815 :
2816 5 : if( _findTypeByName_GCIO(H,typName)!=-1 )
2817 : {
2818 0 : CPLError( CE_Failure, CPLE_AppDefined,
2819 : "type %s already exists.\n",
2820 : typName);
2821 0 : return NULL;
2822 : }
2823 :
2824 5 : if( !(theClass= _CreateType_GCIO(typName,id)) )
2825 : {
2826 0 : return NULL;
2827 : }
2828 5 : if( (L= CPLListAppend(GetMetaTypes_GCIO(GetGCMeta_GCIO(H)),theClass))==NULL )
2829 : {
2830 0 : _DestroyType_GCIO(&theClass);
2831 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
2832 : "failed to add a Geoconcept type for '%s#%ld'.\n",
2833 : typName, id);
2834 0 : return NULL;
2835 : }
2836 5 : SetMetaTypes_GCIO(GetGCMeta_GCIO(H), L);
2837 :
2838 5 : CPLDebug("GEOCONCEPT", "Type '%s#%ld' added.", typName, id);
2839 :
2840 5 : return theClass;
2841 : }/* AddType_GCIO */
2842 :
2843 : /* -------------------------------------------------------------------- */
2844 0 : static void GCIOAPI_CALL _dropType_GCIO (
2845 : GCExportFileH* H,
2846 : GCType **theClass
2847 : )
2848 : {
2849 : int where;
2850 :
2851 0 : if( !theClass || !(*theClass) ) return;
2852 0 : if( (where= _findTypeByName_GCIO(H,GetTypeName_GCIO(*theClass)))==-1 )
2853 : {
2854 0 : CPLError( CE_Failure, CPLE_AppDefined,
2855 : "type %s does not exist.\n",
2856 0 : GetTypeName_GCIO(*theClass)? GetTypeName_GCIO(*theClass):"''");
2857 0 : return;
2858 : }
2859 0 : CPLListRemove(GetMetaTypes_GCIO(GetGCMeta_GCIO(H)),where);
2860 0 : _DestroyType_GCIO(theClass);
2861 :
2862 0 : return;
2863 : }/* _dropType_GCIO */
2864 :
2865 : /* -------------------------------------------------------------------- */
2866 0 : GCField GCIOAPI_CALL1(*) AddTypeField_GCIO (
2867 : GCExportFileH* H,
2868 : const char* typName,
2869 : int where, /* -1 : in the end */
2870 : const char* name,
2871 : long id,
2872 : GCTypeKind knd,
2873 : const char* extra,
2874 : const char* enums
2875 : )
2876 : {
2877 : int whereClass;
2878 : GCType* theClass;
2879 : GCField* theField;
2880 : CPLList* L;
2881 : const char* normName;
2882 :
2883 0 : if( (whereClass = _findTypeByName_GCIO(H,typName))==-1 )
2884 : {
2885 0 : CPLError( CE_Failure, CPLE_AppDefined,
2886 : "failed to find a Geoconcept type for '%s@%s#%ld'.\n",
2887 : typName, name, id);
2888 0 : return NULL;
2889 : }
2890 0 : theClass= _getType_GCIO(H,whereClass);
2891 :
2892 0 : normName= _NormalizeFieldName_GCIO(name);
2893 0 : if( _findFieldByName_GCIO(GetTypeFields_GCIO(theClass),normName)!=-1 )
2894 : {
2895 0 : CPLError( CE_Failure, CPLE_AppDefined,
2896 : "field '%s@%s#%ld' already exists.\n",
2897 : typName, name, id);
2898 0 : return NULL;
2899 : }
2900 :
2901 0 : if( !(theField= _CreateField_GCIO(normName,id,knd,extra,enums)) )
2902 : {
2903 0 : return NULL;
2904 : }
2905 0 : if (
2906 : where==-1 ||
2907 0 : (where==0 && CPLListCount(GetTypeFields_GCIO(theClass))==0)
2908 : )
2909 : {
2910 0 : L= CPLListAppend(GetTypeFields_GCIO(theClass),theField);
2911 : }
2912 : else
2913 : {
2914 0 : L= CPLListInsert(GetTypeFields_GCIO(theClass),theField,where);
2915 : }
2916 0 : if ( !L )
2917 : {
2918 0 : _DestroyField_GCIO(&theField);
2919 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
2920 : "failed to add a Geoconcept field for '%s@%s#%ld'.\n",
2921 : typName, name, id);
2922 0 : return NULL;
2923 : }
2924 0 : SetTypeFields_GCIO(theClass, L);
2925 :
2926 0 : CPLDebug("GEOCONCEPT", "Field '%s@%s#%ld' added.", typName, name, id);
2927 :
2928 0 : return theField;
2929 : }/* AddTypeField_GCIO */
2930 :
2931 : /* -------------------------------------------------------------------- */
2932 52 : GCField GCIOAPI_CALL1(*) AddSubTypeField_GCIO (
2933 : GCExportFileH* H,
2934 : const char* typName,
2935 : const char* subtypName,
2936 : int where, /* -1 : in the end */
2937 : const char* name,
2938 : long id,
2939 : GCTypeKind knd,
2940 : const char* extra,
2941 : const char* enums
2942 : )
2943 : {
2944 : int whereClass, whereSubType;
2945 : GCType* theClass;
2946 : GCSubType* theSubType;
2947 : GCField* theField;
2948 : CPLList* L;
2949 : const char* normName;
2950 :
2951 52 : if( (whereClass= _findTypeByName_GCIO(H,typName))==-1 )
2952 : {
2953 0 : CPLError( CE_Failure, CPLE_AppDefined,
2954 : "failed to find a Geoconcept type for '%s.%s@%s#%ld'.\n",
2955 : typName, subtypName, name, id);
2956 0 : return NULL;
2957 : }
2958 52 : theClass= _getType_GCIO(H,whereClass);
2959 :
2960 52 : if( (whereSubType= _findSubTypeByName_GCIO(theClass,subtypName))==-1 )
2961 : {
2962 0 : CPLError( CE_Failure, CPLE_AppDefined,
2963 : "failed to find a Geoconcept subtype for '%s.%s@%s#%ld'.\n",
2964 : typName, subtypName, name, id);
2965 0 : return NULL;
2966 : }
2967 52 : theSubType= _getSubType_GCIO(theClass,whereSubType);
2968 :
2969 52 : normName= _NormalizeFieldName_GCIO(name);
2970 52 : if( _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),normName)!=-1 )
2971 : {
2972 0 : CPLError( CE_Failure, CPLE_AppDefined,
2973 : "field '%s.%s@%s#%ld' already exists.\n",
2974 : typName, subtypName, name, id);
2975 0 : return NULL;
2976 : }
2977 :
2978 52 : if( !(theField= _CreateField_GCIO(normName,id,knd,extra,enums)) )
2979 : {
2980 0 : return NULL;
2981 : }
2982 101 : if(
2983 : where==-1 ||
2984 0 : (where==0 && CPLListCount(GetSubTypeFields_GCIO(theSubType))==0)
2985 : )
2986 : {
2987 49 : L= CPLListAppend(GetSubTypeFields_GCIO(theSubType),theField);
2988 : }
2989 : else
2990 : {
2991 3 : L= CPLListInsert(GetSubTypeFields_GCIO(theSubType),theField,where);
2992 : }
2993 52 : if( !L )
2994 : {
2995 0 : _DestroyField_GCIO(&theField);
2996 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
2997 : "failed to add a Geoconcept field for '%s.%s@%s#%ld'.\n",
2998 : typName, subtypName, name, id);
2999 0 : return NULL;
3000 : }
3001 52 : SetSubTypeFields_GCIO(theSubType, L);
3002 :
3003 52 : CPLDebug("GEOCONCEPT", "Field '%s.%s@%s#%ld' added.", typName, subtypName, name, id);
3004 :
3005 52 : return theField;
3006 : }/* AddSubTypeField_GCIO */
3007 :
3008 : /* -------------------------------------------------------------------- */
3009 0 : static OGRErr GCIOAPI_CALL _readConfigField_GCIO (
3010 : GCExportFileH* hGCT
3011 : )
3012 : {
3013 : int eof, res;
3014 : char *k, n[kItemSize_GCIO], x[kExtraSize_GCIO], e[kExtraSize_GCIO];
3015 : const char* normName;
3016 : long id;
3017 : GCTypeKind knd;
3018 : CPLList* L;
3019 : GCField* theField;
3020 :
3021 0 : eof= 0;
3022 0 : n[0]= '\0';
3023 0 : x[0]= '\0';
3024 0 : e[0]= '\0';
3025 0 : id= UNDEFINEDID_GCIO;
3026 0 : knd= vUnknownItemType_GCIO;
3027 0 : theField= NULL;
3028 0 : while( _get_GCIO(hGCT)!=EOF )
3029 : {
3030 0 : if( GetGCWhatIs_GCIO(hGCT)==vComType_GCIO )
3031 : {
3032 0 : continue;
3033 : }
3034 0 : if( GetGCWhatIs_GCIO(hGCT)==vHeader_GCIO )
3035 : {
3036 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigEndField_GCIO)!=NULL)
3037 : {
3038 0 : eof= 1;
3039 0 : if( n[0]=='\0' || id==UNDEFINEDID_GCIO || knd==vUnknownItemType_GCIO )
3040 : {
3041 0 : CPLError( CE_Failure, CPLE_AppDefined,
3042 : "Missing %s.\n",
3043 0 : n[0]=='\0'? "Name": id==UNDEFINEDID_GCIO? "ID": "Kind");
3044 0 : res= OGRERR_CORRUPT_DATA;
3045 0 : goto onError;
3046 : }
3047 0 : normName= _NormalizeFieldName_GCIO(n);
3048 0 : if( _findFieldByName_GCIO(GetMetaFields_GCIO(GetGCMeta_GCIO(hGCT)),normName)!=-1 )
3049 : {
3050 0 : CPLError( CE_Failure, CPLE_AppDefined,
3051 : "field '@%s#%ld' already exists.\n",
3052 : n, id);
3053 0 : res= OGRERR_CORRUPT_DATA;
3054 0 : goto onError;
3055 : }
3056 0 : if( !(theField= _CreateField_GCIO(normName,id,knd,x,e)) )
3057 : {
3058 0 : res= OGRERR_CORRUPT_DATA;
3059 0 : goto onError;
3060 : }
3061 0 : if( (L= CPLListAppend(GetMetaFields_GCIO(GetGCMeta_GCIO(hGCT)),theField))==NULL )
3062 : {
3063 0 : _DestroyField_GCIO(&theField);
3064 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
3065 : "failed to add a Geoconcept field for '@%s#%ld'.\n",
3066 : n, id);
3067 0 : res= OGRERR_CORRUPT_DATA;
3068 0 : goto onError;
3069 : }
3070 0 : SetMetaFields_GCIO(GetGCMeta_GCIO(hGCT), L);
3071 0 : break;
3072 : }
3073 0 : res= OGRERR_NONE;
3074 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigName_GCIO))!=NULL )
3075 : {
3076 0 : if( n[0]!='\0' )
3077 : {
3078 0 : CPLError( CE_Failure, CPLE_AppDefined,
3079 : "Duplicate Name found : '%s'.\n",
3080 : GetGCCache_GCIO(hGCT));
3081 0 : res= OGRERR_CORRUPT_DATA;
3082 0 : goto onError;
3083 : }
3084 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3085 : {
3086 0 : CPLError( CE_Failure, CPLE_AppDefined,
3087 : "Invalid Name found : '%s'.\n",
3088 : GetGCCache_GCIO(hGCT));
3089 0 : res= OGRERR_CORRUPT_DATA;
3090 0 : goto onError;
3091 : }
3092 0 : strncpy(n,k,kItemSize_GCIO-1), n[kItemSize_GCIO-1]= '\0';
3093 : }
3094 : else
3095 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigID_GCIO))!=NULL )
3096 : {
3097 0 : if( id!=UNDEFINEDID_GCIO )
3098 : {
3099 0 : CPLError( CE_Failure, CPLE_AppDefined,
3100 : "Duplicate ID found : '%s'.\n",
3101 : GetGCCache_GCIO(hGCT));
3102 0 : res= OGRERR_CORRUPT_DATA;
3103 0 : goto onError;
3104 : }
3105 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3106 : {
3107 0 : CPLError( CE_Failure, CPLE_AppDefined,
3108 : "Invalid ID found : '%s'.\n",
3109 : GetGCCache_GCIO(hGCT));
3110 0 : res= OGRERR_CORRUPT_DATA;
3111 0 : goto onError;
3112 : }
3113 0 : if( sscanf(k,"%ld", &id)!=1 )
3114 : {
3115 0 : CPLError( CE_Failure, CPLE_AppDefined,
3116 : "Invalid ID found : '%s'.\n",
3117 : GetGCCache_GCIO(hGCT));
3118 0 : res= OGRERR_CORRUPT_DATA;
3119 0 : goto onError;
3120 : }
3121 : }
3122 : else
3123 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigKind_GCIO))!=NULL )
3124 : {
3125 0 : if( knd!=vUnknownItemType_GCIO )
3126 : {
3127 0 : CPLError( CE_Failure, CPLE_AppDefined,
3128 : "Duplicate Kind found : '%s'.\n",
3129 : GetGCCache_GCIO(hGCT));
3130 0 : res= OGRERR_CORRUPT_DATA;
3131 0 : goto onError;
3132 : }
3133 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3134 : {
3135 0 : CPLError( CE_Failure, CPLE_AppDefined,
3136 : "Invalid Kind found : '%s'.\n",
3137 : GetGCCache_GCIO(hGCT));
3138 0 : res= OGRERR_CORRUPT_DATA;
3139 0 : goto onError;
3140 : }
3141 0 : if( (knd= str2GCTypeKind_GCIO(k))==vUnknownItemType_GCIO )
3142 : {
3143 0 : CPLError( CE_Failure, CPLE_AppDefined,
3144 : "Not supported Kind found : '%s'.\n",
3145 : GetGCCache_GCIO(hGCT));
3146 0 : res= OGRERR_CORRUPT_DATA;
3147 0 : goto onError;
3148 : }
3149 : }
3150 : else
3151 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigExtra_GCIO))!=NULL ||
3152 0 : (k= strstr(GetGCCache_GCIO(hGCT),kConfigExtraText_GCIO))!=NULL )
3153 : {
3154 0 : if( x[0]!='\0' )
3155 : {
3156 0 : CPLError( CE_Failure, CPLE_AppDefined,
3157 : "Duplicate Extra information found : '%s'.\n",
3158 : GetGCCache_GCIO(hGCT));
3159 0 : res= OGRERR_CORRUPT_DATA;
3160 0 : goto onError;
3161 : }
3162 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3163 : {
3164 0 : CPLError( CE_Failure, CPLE_AppDefined,
3165 : "Invalid Extra information found : '%s'.\n",
3166 : GetGCCache_GCIO(hGCT));
3167 0 : res= OGRERR_CORRUPT_DATA;
3168 0 : goto onError;
3169 : }
3170 0 : strncpy(x,k,kExtraSize_GCIO-1), x[kExtraSize_GCIO-1]= '\0';
3171 : }
3172 : else
3173 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigList_GCIO))!=NULL )
3174 : {
3175 0 : if( e[0]!='\0' )
3176 : {
3177 0 : CPLError( CE_Failure, CPLE_AppDefined,
3178 : "Duplicate List found : '%s'.\n",
3179 : GetGCCache_GCIO(hGCT));
3180 0 : res= OGRERR_CORRUPT_DATA;
3181 0 : goto onError;
3182 : }
3183 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3184 : {
3185 0 : CPLError( CE_Failure, CPLE_AppDefined,
3186 : "Invalid List found : '%s'.\n",
3187 : GetGCCache_GCIO(hGCT));
3188 0 : res= OGRERR_CORRUPT_DATA;
3189 0 : goto onError;
3190 : }
3191 0 : strncpy(e,k,kExtraSize_GCIO-1), e[kExtraSize_GCIO-1]= '\0';
3192 : }
3193 : else
3194 : { /* Skipping ... */
3195 0 : res= OGRERR_NONE;
3196 : }
3197 0 : if( res != OGRERR_NONE )
3198 : {
3199 0 : goto onError;
3200 : }
3201 0 : continue;
3202 : }
3203 : onError:
3204 0 : return OGRERR_CORRUPT_DATA;
3205 : }
3206 0 : if (eof!=1)
3207 : {
3208 0 : CPLError( CE_Failure, CPLE_AppDefined,
3209 : "Geoconcept config field end block %s not found.\n",
3210 : kConfigEndField_GCIO);
3211 0 : return OGRERR_CORRUPT_DATA;
3212 : }
3213 :
3214 0 : return OGRERR_NONE;
3215 : }/* _readConfigField_GCIO */
3216 :
3217 : /* -------------------------------------------------------------------- */
3218 0 : static OGRErr GCIOAPI_CALL _readConfigFieldType_GCIO (
3219 : GCExportFileH* hGCT,
3220 : GCType* theClass
3221 : )
3222 : {
3223 : int eof, res;
3224 : char *k, n[kItemSize_GCIO], x[kExtraSize_GCIO], e[kExtraSize_GCIO];
3225 : long id;
3226 : GCTypeKind knd;
3227 : GCField* theField;
3228 :
3229 0 : eof= 0;
3230 0 : n[0]= '\0';
3231 0 : x[0]= '\0';
3232 0 : e[0]= '\0';
3233 0 : id= UNDEFINEDID_GCIO;
3234 0 : knd= vUnknownItemType_GCIO;
3235 0 : theField= NULL;
3236 0 : while( _get_GCIO(hGCT)!=EOF ) {
3237 0 : if( GetGCWhatIs_GCIO(hGCT)==vComType_GCIO )
3238 : {
3239 0 : continue;
3240 : }
3241 0 : if( GetGCWhatIs_GCIO(hGCT)==vHeader_GCIO )
3242 : {
3243 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigEndField_GCIO)!=NULL)
3244 : {
3245 0 : eof= 1;
3246 0 : if( n[0]=='\0' || id==UNDEFINEDID_GCIO || knd==vUnknownItemType_GCIO )
3247 : {
3248 0 : CPLError( CE_Failure, CPLE_AppDefined,
3249 : "Missing %s.\n",
3250 0 : n[0]=='\0'? "Name": id==UNDEFINEDID_GCIO? "ID": "Kind");
3251 0 : res= OGRERR_CORRUPT_DATA;
3252 0 : goto onError;
3253 : }
3254 0 : if( (theField= AddTypeField_GCIO(hGCT,GetTypeName_GCIO(theClass),-1,n,id,knd,x,e))==NULL )
3255 : {
3256 0 : res= OGRERR_CORRUPT_DATA;
3257 0 : goto onError;
3258 : }
3259 0 : break;
3260 : }
3261 0 : res= OGRERR_NONE;
3262 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigName_GCIO))!=NULL )
3263 : {
3264 0 : if( n[0]!='\0' )
3265 : {
3266 0 : CPLError( CE_Failure, CPLE_AppDefined,
3267 : "Duplicate Name found : '%s'.\n",
3268 : GetGCCache_GCIO(hGCT));
3269 0 : res= OGRERR_CORRUPT_DATA;
3270 0 : goto onError;
3271 : }
3272 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3273 : {
3274 0 : CPLError( CE_Failure, CPLE_AppDefined,
3275 : "Invalid Name found : '%s'.\n",
3276 : GetGCCache_GCIO(hGCT));
3277 0 : res= OGRERR_CORRUPT_DATA;
3278 0 : goto onError;
3279 : }
3280 0 : strncpy(n,k,kItemSize_GCIO-1), n[kItemSize_GCIO-1]= '\0';
3281 : }
3282 : else
3283 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigID_GCIO))!=NULL )
3284 : {
3285 0 : if( id!=UNDEFINEDID_GCIO )
3286 : {
3287 0 : CPLError( CE_Failure, CPLE_AppDefined,
3288 : "Duplicate ID found : '%s'.\n",
3289 : GetGCCache_GCIO(hGCT));
3290 0 : res= OGRERR_CORRUPT_DATA;
3291 0 : goto onError;
3292 : }
3293 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3294 : {
3295 0 : CPLError( CE_Failure, CPLE_AppDefined,
3296 : "Invalid ID found : '%s'.\n",
3297 : GetGCCache_GCIO(hGCT));
3298 0 : res= OGRERR_CORRUPT_DATA;
3299 0 : goto onError;
3300 : }
3301 0 : if( sscanf(k,"%ld", &id)!=1 )
3302 : {
3303 0 : CPLError( CE_Failure, CPLE_AppDefined,
3304 : "Invalid ID found : '%s'.\n",
3305 : GetGCCache_GCIO(hGCT));
3306 0 : res= OGRERR_CORRUPT_DATA;
3307 0 : goto onError;
3308 : }
3309 : }
3310 : else
3311 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigKind_GCIO))!=NULL )
3312 : {
3313 0 : if( knd!=vUnknownItemType_GCIO )
3314 : {
3315 0 : CPLError( CE_Failure, CPLE_AppDefined,
3316 : "Duplicate Kind found : '%s'.\n",
3317 : GetGCCache_GCIO(hGCT));
3318 0 : res= OGRERR_CORRUPT_DATA;
3319 0 : goto onError;
3320 : }
3321 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3322 : {
3323 0 : CPLError( CE_Failure, CPLE_AppDefined,
3324 : "Invalid Kind found : '%s'.\n",
3325 : GetGCCache_GCIO(hGCT));
3326 0 : res= OGRERR_CORRUPT_DATA;
3327 0 : goto onError;
3328 : }
3329 0 : if( (knd= str2GCTypeKind_GCIO(k))==vUnknownItemType_GCIO )
3330 : {
3331 0 : CPLError( CE_Failure, CPLE_AppDefined,
3332 : "Not supported Kind found : '%s'.\n",
3333 : GetGCCache_GCIO(hGCT));
3334 0 : res= OGRERR_CORRUPT_DATA;
3335 0 : goto onError;
3336 : }
3337 : }
3338 : else
3339 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigExtra_GCIO))!=NULL ||
3340 0 : (k= strstr(GetGCCache_GCIO(hGCT),kConfigExtraText_GCIO))!=NULL )
3341 : {
3342 0 : if( x[0]!='\0' )
3343 : {
3344 0 : CPLError( CE_Failure, CPLE_AppDefined,
3345 : "Duplicate Extra information found : '%s'.\n",
3346 : GetGCCache_GCIO(hGCT));
3347 0 : res= OGRERR_CORRUPT_DATA;
3348 0 : goto onError;
3349 : }
3350 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3351 : {
3352 0 : CPLError( CE_Failure, CPLE_AppDefined,
3353 : "Invalid extra information found : '%s'.\n",
3354 : GetGCCache_GCIO(hGCT));
3355 0 : res= OGRERR_CORRUPT_DATA;
3356 0 : goto onError;
3357 : }
3358 0 : strncpy(x,k,kExtraSize_GCIO-1), x[kExtraSize_GCIO-1]= '\0';
3359 : }
3360 : else
3361 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigList_GCIO))!=NULL )
3362 : {
3363 0 : if( e[0]!='\0' )
3364 : {
3365 0 : CPLError( CE_Failure, CPLE_AppDefined,
3366 : "Duplicate List found : '%s'.\n",
3367 : GetGCCache_GCIO(hGCT));
3368 0 : res= OGRERR_CORRUPT_DATA;
3369 0 : goto onError;
3370 : }
3371 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3372 : {
3373 0 : CPLError( CE_Failure, CPLE_AppDefined,
3374 : "Invalid List found : '%s'.\n",
3375 : GetGCCache_GCIO(hGCT));
3376 0 : res= OGRERR_CORRUPT_DATA;
3377 0 : goto onError;
3378 : }
3379 0 : strncpy(e,k,kExtraSize_GCIO-1), e[kExtraSize_GCIO-1]= '\0';
3380 : }
3381 : else
3382 : { /* Skipping ... */
3383 0 : res= OGRERR_NONE;
3384 : }
3385 0 : if( res != OGRERR_NONE )
3386 : {
3387 0 : goto onError;
3388 : }
3389 0 : continue;
3390 : }
3391 : onError:
3392 0 : return OGRERR_CORRUPT_DATA;
3393 : }
3394 0 : if (eof!=1)
3395 : {
3396 0 : CPLError( CE_Failure, CPLE_AppDefined,
3397 : "Geoconcept config field end block %s not found.\n",
3398 : kConfigEndField_GCIO);
3399 0 : return OGRERR_CORRUPT_DATA;
3400 : }
3401 :
3402 0 : return OGRERR_NONE;
3403 : }/* _readConfigFieldType_GCIO */
3404 :
3405 : /* -------------------------------------------------------------------- */
3406 0 : static OGRErr GCIOAPI_CALL _readConfigFieldSubType_GCIO (
3407 : GCExportFileH* hGCT,
3408 : GCType* theClass,
3409 : GCSubType* theSubType
3410 : )
3411 : {
3412 : int eof, res;
3413 : char *k, n[kItemSize_GCIO], x[kExtraSize_GCIO], e[kExtraSize_GCIO];
3414 : long id;
3415 : GCTypeKind knd;
3416 : GCField* theField;
3417 :
3418 0 : eof= 0;
3419 0 : n[0]= '\0';
3420 0 : x[0]= '\0';
3421 0 : e[0]= '\0';
3422 0 : id= UNDEFINEDID_GCIO;
3423 0 : knd= vUnknownItemType_GCIO;
3424 0 : theField= NULL;
3425 0 : while( _get_GCIO(hGCT)!=EOF ) {
3426 0 : if( GetGCWhatIs_GCIO(hGCT)==vComType_GCIO )
3427 : {
3428 0 : continue;
3429 : }
3430 0 : if( GetGCWhatIs_GCIO(hGCT)==vHeader_GCIO )
3431 : {
3432 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigEndField_GCIO)!=NULL)
3433 : {
3434 0 : eof= 1;
3435 0 : if( n[0]=='\0' || id==UNDEFINEDID_GCIO || knd==vUnknownItemType_GCIO )
3436 : {
3437 0 : CPLError( CE_Failure, CPLE_AppDefined,
3438 : "Missing %s.\n",
3439 0 : n[0]=='\0'? "Name": id==UNDEFINEDID_GCIO? "ID": "Kind");
3440 0 : res= OGRERR_CORRUPT_DATA;
3441 0 : goto onError;
3442 : }
3443 0 : if( (theField= AddSubTypeField_GCIO(hGCT,GetTypeName_GCIO(theClass),GetSubTypeName_GCIO(theSubType),-1,n,id,knd,x,e))==NULL )
3444 : {
3445 0 : res= OGRERR_CORRUPT_DATA;
3446 0 : goto onError;
3447 : }
3448 0 : break;
3449 : }
3450 0 : res= OGRERR_NONE;
3451 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigName_GCIO))!=NULL )
3452 : {
3453 0 : if( n[0]!='\0' )
3454 : {
3455 0 : CPLError( CE_Failure, CPLE_AppDefined,
3456 : "Duplicate Name found : '%s'.\n",
3457 : GetGCCache_GCIO(hGCT));
3458 0 : res= OGRERR_CORRUPT_DATA;
3459 0 : goto onError;
3460 : }
3461 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3462 : {
3463 0 : CPLError( CE_Failure, CPLE_AppDefined,
3464 : "Invalid Name found : '%s'.\n",
3465 : GetGCCache_GCIO(hGCT));
3466 0 : res= OGRERR_CORRUPT_DATA;
3467 0 : goto onError;
3468 : }
3469 0 : strncpy(n,k,kItemSize_GCIO-1), n[kItemSize_GCIO-1]= '\0';
3470 : }
3471 : else
3472 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigID_GCIO))!=NULL )
3473 : {
3474 0 : if( id!=UNDEFINEDID_GCIO )
3475 : {
3476 0 : CPLError( CE_Failure, CPLE_AppDefined,
3477 : "Duplicate ID found : '%s'.\n",
3478 : GetGCCache_GCIO(hGCT));
3479 0 : res= OGRERR_CORRUPT_DATA;
3480 0 : goto onError;
3481 : }
3482 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3483 : {
3484 0 : CPLError( CE_Failure, CPLE_AppDefined,
3485 : "Invalid ID found : '%s'.\n",
3486 : GetGCCache_GCIO(hGCT));
3487 0 : res= OGRERR_CORRUPT_DATA;
3488 0 : goto onError;
3489 : }
3490 0 : if( sscanf(k,"%ld", &id)!=1 )
3491 : {
3492 0 : CPLError( CE_Failure, CPLE_AppDefined,
3493 : "Invalid ID found : '%s'.\n",
3494 : GetGCCache_GCIO(hGCT));
3495 0 : res= OGRERR_CORRUPT_DATA;
3496 0 : goto onError;
3497 : }
3498 : }
3499 : else
3500 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigKind_GCIO))!=NULL )
3501 : {
3502 0 : if( knd!=vUnknownItemType_GCIO )
3503 : {
3504 0 : CPLError( CE_Failure, CPLE_AppDefined,
3505 : "Duplicate Kind found : '%s'.\n",
3506 : GetGCCache_GCIO(hGCT));
3507 0 : res= OGRERR_CORRUPT_DATA;
3508 0 : goto onError;
3509 : }
3510 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3511 : {
3512 0 : CPLError( CE_Failure, CPLE_AppDefined,
3513 : "Invalid Kind found : '%s'.\n",
3514 : GetGCCache_GCIO(hGCT));
3515 0 : res= OGRERR_CORRUPT_DATA;
3516 0 : goto onError;
3517 : }
3518 0 : if( (knd= str2GCTypeKind_GCIO(k))==vUnknownItemType_GCIO )
3519 : {
3520 0 : CPLError( CE_Failure, CPLE_AppDefined,
3521 : "Not supported Kind found : '%s'.\n",
3522 : GetGCCache_GCIO(hGCT));
3523 0 : res= OGRERR_CORRUPT_DATA;
3524 0 : goto onError;
3525 : }
3526 : }
3527 : else
3528 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigExtra_GCIO))!=NULL ||
3529 0 : (k= strstr(GetGCCache_GCIO(hGCT),kConfigExtraText_GCIO))!=NULL )
3530 : {
3531 0 : if( x[0]!='\0' )
3532 : {
3533 0 : CPLError( CE_Failure, CPLE_AppDefined,
3534 : "Duplicate Extra information found : '%s'.\n",
3535 : GetGCCache_GCIO(hGCT));
3536 0 : res= OGRERR_CORRUPT_DATA;
3537 0 : goto onError;
3538 : }
3539 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3540 : {
3541 0 : CPLError( CE_Failure, CPLE_AppDefined,
3542 : "Invalid extra information found : '%s'.\n",
3543 : GetGCCache_GCIO(hGCT));
3544 0 : res= OGRERR_CORRUPT_DATA;
3545 0 : goto onError;
3546 : }
3547 0 : strncpy(x,k,kExtraSize_GCIO-1), x[kExtraSize_GCIO-1]= '\0';
3548 : }
3549 : else
3550 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigList_GCIO))!=NULL )
3551 : {
3552 0 : if( e[0]!='\0' )
3553 : {
3554 0 : CPLError( CE_Failure, CPLE_AppDefined,
3555 : "Duplicate List found : '%s'.\n",
3556 : GetGCCache_GCIO(hGCT));
3557 0 : res= OGRERR_CORRUPT_DATA;
3558 0 : goto onError;
3559 : }
3560 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3561 : {
3562 0 : CPLError( CE_Failure, CPLE_AppDefined,
3563 : "Invalid List found : '%s'.\n",
3564 : GetGCCache_GCIO(hGCT));
3565 0 : res= OGRERR_CORRUPT_DATA;
3566 0 : goto onError;
3567 : }
3568 0 : strncpy(e,k,kExtraSize_GCIO-1), e[kExtraSize_GCIO-1]= '\0';
3569 : }
3570 : else
3571 : { /* Skipping ... */
3572 0 : res= OGRERR_NONE;
3573 : }
3574 0 : if( res != OGRERR_NONE )
3575 : {
3576 0 : goto onError;
3577 : }
3578 0 : continue;
3579 : }
3580 : onError:
3581 0 : return OGRERR_CORRUPT_DATA;
3582 : }
3583 0 : if (eof!=1)
3584 : {
3585 0 : CPLError( CE_Failure, CPLE_AppDefined,
3586 : "Geoconcept config field end block %s not found.\n",
3587 : kConfigEndField_GCIO);
3588 0 : return OGRERR_CORRUPT_DATA;
3589 : }
3590 :
3591 0 : return OGRERR_NONE;
3592 : }/* _readConfigFieldSubType_GCIO */
3593 :
3594 : /* -------------------------------------------------------------------- */
3595 0 : static OGRErr GCIOAPI_CALL _readConfigSubTypeType_GCIO (
3596 : GCExportFileH* hGCT,
3597 : GCType* theClass
3598 : )
3599 : {
3600 : int eost, res;
3601 : char *k, n[kItemSize_GCIO];
3602 : long id;
3603 : GCTypeKind knd;
3604 : GCDim sys;
3605 : GCSubType* theSubType;
3606 :
3607 0 : eost= 0;
3608 0 : n[0]= '\0';
3609 0 : id= UNDEFINEDID_GCIO;
3610 0 : knd= vUnknownItemType_GCIO;
3611 0 : sys= v2D_GCIO;
3612 0 : theSubType= NULL;
3613 0 : while( _get_GCIO(hGCT)!=EOF ) {
3614 0 : if( GetGCWhatIs_GCIO(hGCT)==vComType_GCIO )
3615 : {
3616 0 : continue;
3617 : }
3618 0 : if( GetGCWhatIs_GCIO(hGCT)==vHeader_GCIO )
3619 : {
3620 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigEndSubType_GCIO)!=NULL)
3621 : {
3622 0 : eost= 1;
3623 0 : break;
3624 : }
3625 0 : res= OGRERR_NONE;
3626 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigName_GCIO))!=NULL )
3627 : {
3628 0 : if( n[0]!='\0' )
3629 : {
3630 0 : CPLError( CE_Failure, CPLE_AppDefined,
3631 : "Duplicate Name found : '%s'.\n",
3632 : GetGCCache_GCIO(hGCT));
3633 0 : res= OGRERR_CORRUPT_DATA;
3634 0 : goto onError;
3635 : }
3636 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3637 : {
3638 0 : CPLError( CE_Failure, CPLE_AppDefined,
3639 : "Invalid Name found : '%s'.\n",
3640 : GetGCCache_GCIO(hGCT));
3641 0 : res= OGRERR_CORRUPT_DATA;
3642 0 : goto onError;
3643 : }
3644 0 : strncpy(n,k,kItemSize_GCIO-1), n[kItemSize_GCIO-1]= '\0';
3645 : }
3646 : else
3647 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigID_GCIO))!=NULL )
3648 : {
3649 0 : if( id!=UNDEFINEDID_GCIO )
3650 : {
3651 0 : CPLError( CE_Failure, CPLE_AppDefined,
3652 : "Duplicate ID found : '%s'.\n",
3653 : GetGCCache_GCIO(hGCT));
3654 0 : res= OGRERR_CORRUPT_DATA;
3655 0 : goto onError;
3656 : }
3657 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3658 : {
3659 0 : CPLError( CE_Failure, CPLE_AppDefined,
3660 : "Invalid ID found : '%s'.\n",
3661 : GetGCCache_GCIO(hGCT));
3662 0 : res= OGRERR_CORRUPT_DATA;
3663 0 : goto onError;
3664 : }
3665 0 : if( sscanf(k,"%ld", &id)!=1 )
3666 : {
3667 0 : CPLError( CE_Failure, CPLE_AppDefined,
3668 : "Invalid ID found : '%s'.\n",
3669 : GetGCCache_GCIO(hGCT));
3670 0 : res= OGRERR_CORRUPT_DATA;
3671 0 : goto onError;
3672 : }
3673 : }
3674 : else
3675 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigKind_GCIO))!=NULL )
3676 : {
3677 0 : if( knd!=vUnknownItemType_GCIO )
3678 : {
3679 0 : CPLError( CE_Failure, CPLE_AppDefined,
3680 : "Duplicate Kind found : '%s'.\n",
3681 : GetGCCache_GCIO(hGCT));
3682 0 : res= OGRERR_CORRUPT_DATA;
3683 0 : goto onError;
3684 : }
3685 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3686 : {
3687 0 : CPLError( CE_Failure, CPLE_AppDefined,
3688 : "Invalid Kind found : '%s'.\n",
3689 : GetGCCache_GCIO(hGCT));
3690 0 : res= OGRERR_CORRUPT_DATA;
3691 0 : goto onError;
3692 : }
3693 0 : if( (knd= str2GCTypeKind_GCIO(k))==vUnknownItemType_GCIO )
3694 : {
3695 0 : CPLError( CE_Failure, CPLE_AppDefined,
3696 : "Not supported Kind found : '%s'.\n",
3697 : GetGCCache_GCIO(hGCT));
3698 0 : res= OGRERR_CORRUPT_DATA;
3699 0 : goto onError;
3700 : }
3701 : }
3702 : else
3703 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfig3D_GCIO))!=NULL )
3704 : {
3705 0 : if( sys!=vUnknown3D_GCIO && sys!=v2D_GCIO)
3706 : {
3707 0 : CPLError( CE_Failure, CPLE_AppDefined,
3708 : "Duplicate Dimension found : '%s'.\n",
3709 : GetGCCache_GCIO(hGCT));
3710 0 : res= OGRERR_CORRUPT_DATA;
3711 0 : goto onError;
3712 : }
3713 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3714 : {
3715 0 : CPLError( CE_Failure, CPLE_AppDefined,
3716 : "Invalid Dimension found : '%s'.\n",
3717 : GetGCCache_GCIO(hGCT));
3718 0 : res= OGRERR_CORRUPT_DATA;
3719 0 : goto onError;
3720 : }
3721 0 : if( (sys= str2GCDim(k))==vUnknown3D_GCIO )
3722 : {
3723 0 : CPLError( CE_Failure, CPLE_AppDefined,
3724 : "Not supported Dimension found : '%s'.\n",
3725 : GetGCCache_GCIO(hGCT));
3726 0 : res= OGRERR_CORRUPT_DATA;
3727 0 : goto onError;
3728 : }
3729 : }
3730 : else
3731 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigBeginField_GCIO)!=NULL )
3732 : {
3733 0 : if( theSubType==NULL )
3734 : {
3735 0 : if( n[0]=='\0' || id==UNDEFINEDID_GCIO || knd==vUnknownItemType_GCIO || sys==vUnknown3D_GCIO )
3736 : {
3737 0 : CPLError( CE_Failure, CPLE_AppDefined,
3738 : "Missing %s.\n",
3739 0 : n[0]=='\0'? "Name": id==UNDEFINEDID_GCIO? "ID": knd==vUnknownItemType_GCIO? "Kind": "3D");
3740 0 : res= OGRERR_CORRUPT_DATA;
3741 0 : goto onError;
3742 : }
3743 0 : if( (theSubType= AddSubType_GCIO(hGCT,GetTypeName_GCIO(theClass),n,id,knd,sys))==NULL )
3744 : {
3745 0 : res= OGRERR_CORRUPT_DATA;
3746 0 : goto onError;
3747 : }
3748 : }
3749 0 : res= _readConfigFieldSubType_GCIO(hGCT,theClass,theSubType);
3750 : }
3751 : else
3752 : { /* Skipping ... */
3753 0 : res= OGRERR_NONE;
3754 : }
3755 0 : if( res != OGRERR_NONE )
3756 : {
3757 0 : goto onError;
3758 : }
3759 0 : continue;
3760 : }
3761 : onError:
3762 0 : if( theSubType )
3763 : {
3764 0 : _dropSubType_GCIO(&theSubType);
3765 : }
3766 0 : return OGRERR_CORRUPT_DATA;
3767 : }
3768 0 : if (eost!=1)
3769 : {
3770 0 : if( theSubType )
3771 : {
3772 0 : _dropSubType_GCIO(&theSubType);
3773 : }
3774 0 : CPLError( CE_Failure, CPLE_AppDefined,
3775 : "Geoconcept config subtype end block %s not found.\n",
3776 : kConfigEndSubType_GCIO);
3777 0 : return OGRERR_CORRUPT_DATA;
3778 : }
3779 :
3780 0 : return OGRERR_NONE;
3781 : }/* _readConfigSubTypeType_GCIO */
3782 :
3783 : /* -------------------------------------------------------------------- */
3784 0 : static OGRErr GCIOAPI_CALL _readConfigType_GCIO (
3785 : GCExportFileH* hGCT
3786 : )
3787 : {
3788 : int eot, res;
3789 : char *k, n[kItemSize_GCIO];
3790 : long id;
3791 : GCType *theClass;
3792 :
3793 0 : eot= 0;
3794 0 : n[0]= '\0';
3795 0 : id= UNDEFINEDID_GCIO;
3796 0 : theClass= NULL;
3797 0 : while( _get_GCIO(hGCT)!=EOF ) {
3798 0 : if( GetGCWhatIs_GCIO(hGCT)==vComType_GCIO )
3799 : {
3800 0 : continue;
3801 : }
3802 0 : if( GetGCWhatIs_GCIO(hGCT)==vHeader_GCIO )
3803 : {
3804 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigEndType_GCIO)!=NULL )
3805 : {
3806 0 : eot= 1;
3807 0 : break;
3808 : }
3809 0 : res= OGRERR_NONE;
3810 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigName_GCIO))!=NULL )
3811 : {
3812 0 : if( n[0]!='\0' )
3813 : {
3814 0 : CPLError( CE_Failure, CPLE_AppDefined,
3815 : "Duplicate Name found : '%s'.\n",
3816 : GetGCCache_GCIO(hGCT));
3817 0 : res= OGRERR_CORRUPT_DATA;
3818 0 : goto onError;
3819 : }
3820 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3821 : {
3822 0 : CPLError( CE_Failure, CPLE_AppDefined,
3823 : "Invalid Name found : '%s'.\n",
3824 : GetGCCache_GCIO(hGCT));
3825 0 : res= OGRERR_CORRUPT_DATA;
3826 0 : goto onError;
3827 : }
3828 0 : strncpy(n,k,kItemSize_GCIO-1), n[kItemSize_GCIO-1]= '\0';
3829 : }
3830 : else
3831 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigID_GCIO))!=NULL )
3832 : {
3833 0 : if( id!=UNDEFINEDID_GCIO )
3834 : {
3835 0 : CPLError( CE_Failure, CPLE_AppDefined,
3836 : "Duplicate ID found : '%s'.\n",
3837 : GetGCCache_GCIO(hGCT));
3838 0 : res= OGRERR_CORRUPT_DATA;
3839 0 : goto onError;
3840 : }
3841 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3842 : {
3843 0 : CPLError( CE_Failure, CPLE_AppDefined,
3844 : "Invalid ID found : '%s'.\n",
3845 : GetGCCache_GCIO(hGCT));
3846 0 : res= OGRERR_CORRUPT_DATA;
3847 0 : goto onError;
3848 : }
3849 0 : if( sscanf(k,"%ld", &id)!=1 )
3850 : {
3851 0 : CPLError( CE_Failure, CPLE_AppDefined,
3852 : "Not supported ID found : '%s'.\n",
3853 : GetGCCache_GCIO(hGCT));
3854 0 : res= OGRERR_CORRUPT_DATA;
3855 0 : goto onError;
3856 : }
3857 : }
3858 : else
3859 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigBeginSubType_GCIO)!=NULL )
3860 : {
3861 0 : if( theClass==NULL )
3862 : {
3863 0 : if( n[0]=='\0' || id==UNDEFINEDID_GCIO || (theClass= AddType_GCIO(hGCT,n,id))==NULL )
3864 : {
3865 0 : res= OGRERR_CORRUPT_DATA;
3866 0 : goto onError;
3867 : }
3868 : }
3869 0 : res= _readConfigSubTypeType_GCIO(hGCT,theClass);
3870 : }
3871 : else
3872 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigBeginField_GCIO)!=NULL)
3873 : {
3874 0 : if( theClass==NULL )
3875 : {
3876 0 : if( n[0]=='\0' || id==UNDEFINEDID_GCIO || (theClass= AddType_GCIO(hGCT,n,id))==NULL )
3877 : {
3878 0 : res= OGRERR_CORRUPT_DATA;
3879 0 : goto onError;
3880 : }
3881 : }
3882 0 : res= _readConfigFieldType_GCIO(hGCT,theClass);
3883 : }
3884 : else
3885 : { /* Skipping ... */
3886 0 : res= OGRERR_NONE;
3887 : }
3888 0 : if( res != OGRERR_NONE )
3889 : {
3890 0 : goto onError;
3891 : }
3892 0 : continue;
3893 : }
3894 : onError:
3895 0 : if( theClass )
3896 : {
3897 0 : _dropType_GCIO(hGCT,&theClass);
3898 : }
3899 0 : return OGRERR_CORRUPT_DATA;
3900 : }
3901 0 : if (eot!=1)
3902 : {
3903 0 : if( theClass )
3904 : {
3905 0 : _dropType_GCIO(hGCT,&theClass);
3906 : }
3907 0 : CPLError( CE_Failure, CPLE_AppDefined,
3908 : "Geoconcept config type end block %s not found.\n",
3909 : kConfigEndType_GCIO);
3910 0 : return OGRERR_CORRUPT_DATA;
3911 : }
3912 :
3913 0 : return OGRERR_NONE;
3914 : }/* _readConfigType_GCIO */
3915 :
3916 : /* -------------------------------------------------------------------- */
3917 0 : static OGRErr GCIOAPI_CALL _readConfigMap_GCIO (
3918 : GCExportFileH* hGCT
3919 : )
3920 : {
3921 : int eom, res;
3922 : char* k;
3923 :
3924 0 : eom= 0;
3925 0 : while( _get_GCIO(hGCT)!=EOF ) {
3926 0 : if( GetGCWhatIs_GCIO(hGCT)==vComType_GCIO )
3927 : {
3928 0 : continue;
3929 : }
3930 0 : if( GetGCWhatIs_GCIO(hGCT)==vHeader_GCIO )
3931 : {
3932 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigEndMap_GCIO)!=NULL )
3933 : {
3934 0 : eom= 1;
3935 0 : break;
3936 : }
3937 0 : res= OGRERR_NONE;
3938 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigUnit_GCIO))!=NULL &&
3939 0 : strstr(GetGCCache_GCIO(hGCT),kConfigZUnit_GCIO)==NULL )
3940 : {
3941 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3942 : {
3943 0 : CPLError( CE_Failure, CPLE_AppDefined,
3944 : "Invalid Unit found : '%s'.\n",
3945 : GetGCCache_GCIO(hGCT));
3946 0 : res= OGRERR_CORRUPT_DATA;
3947 0 : goto onError;
3948 : }
3949 0 : SetMetaUnit_GCIO(GetGCMeta_GCIO(hGCT),k);
3950 : }
3951 : else
3952 0 : if( (k= strstr(GetGCCache_GCIO(hGCT),kConfigPrecision_GCIO))!=NULL &&
3953 0 : strstr(GetGCCache_GCIO(hGCT),kConfigZPrecision_GCIO)==NULL )
3954 : {
3955 : double r;
3956 0 : if( (k= _getHeaderValue_GCIO(k))==NULL )
3957 : {
3958 0 : CPLError( CE_Failure, CPLE_AppDefined,
3959 : "Invalid Precision found : '%s'.\n",
3960 : GetGCCache_GCIO(hGCT));
3961 0 : res= OGRERR_CORRUPT_DATA;
3962 0 : goto onError;
3963 : }
3964 0 : if( sscanf(k,"%lf", &r)!=1 )
3965 : {
3966 0 : CPLError( CE_Failure, CPLE_AppDefined,
3967 : "Invalid Precision found : '%s'.\n",
3968 : GetGCCache_GCIO(hGCT));
3969 0 : res= OGRERR_CORRUPT_DATA;
3970 0 : goto onError;
3971 : }
3972 0 : SetMetaResolution_GCIO(GetGCMeta_GCIO(hGCT),r);
3973 : }
3974 : else
3975 : { /* Skipping ... */
3976 0 : res= OGRERR_NONE;
3977 : }
3978 0 : if( res != OGRERR_NONE )
3979 : {
3980 0 : goto onError;
3981 : }
3982 0 : continue;
3983 : }
3984 : onError:
3985 0 : return OGRERR_CORRUPT_DATA;
3986 : }
3987 0 : if (eom!=1)
3988 : {
3989 0 : CPLError( CE_Failure, CPLE_AppDefined,
3990 : "Geoconcept config map end block %s not found.\n",
3991 : kConfigEndMap_GCIO);
3992 0 : return OGRERR_CORRUPT_DATA;
3993 : }
3994 :
3995 0 : return OGRERR_NONE;
3996 : }/* _readConfigMap_GCIO */
3997 :
3998 : /* -------------------------------------------------------------------- */
3999 0 : GCExportFileMetadata GCIOAPI_CALL1(*) ReadConfig_GCIO (
4000 : GCExportFileH* hGCT
4001 : )
4002 : {
4003 : int eoc, res, it, nt;
4004 : int i, n, il, nl, ll;
4005 : int is, ns;
4006 : char l[kExtraSize_GCIO];
4007 : const char *v;
4008 : GCField* theField;
4009 : GCSubType* theSubType;
4010 : GCType* theClass;
4011 : CPLList *e, *es, *et;
4012 : GCExportFileMetadata* Meta;
4013 :
4014 0 : eoc= 0;
4015 0 : if( _get_GCIO(hGCT)==EOF )
4016 : {
4017 0 : return NULL;
4018 : }
4019 0 : if( GetGCWhatIs_GCIO(hGCT)!=vHeader_GCIO &&
4020 0 : strstr(GetGCCache_GCIO(hGCT),kConfigBeginConfig_GCIO)==NULL )
4021 : {
4022 0 : CPLError( CE_Failure, CPLE_AppDefined,
4023 : "Geoconcept config begin block %s not found.\n",
4024 : kConfigBeginConfig_GCIO);
4025 0 : return NULL;
4026 : }
4027 0 : SetGCMeta_GCIO(hGCT, CreateHeader_GCIO());
4028 0 : if( (Meta= GetGCMeta_GCIO(hGCT))==NULL )
4029 : {
4030 0 : return NULL;
4031 : }
4032 0 : while( _get_GCIO(hGCT)!=EOF )
4033 : {
4034 0 : if( GetGCWhatIs_GCIO(hGCT)==vComType_GCIO )
4035 : {
4036 0 : continue;
4037 : }
4038 0 : if( GetGCWhatIs_GCIO(hGCT)==vHeader_GCIO )
4039 : {
4040 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigEndConfig_GCIO)!=NULL )
4041 : {
4042 0 : eoc= 1;
4043 0 : break;
4044 : }
4045 0 : res= OGRERR_NONE;
4046 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigBeginMap_GCIO)!=NULL )
4047 : {
4048 0 : res= _readConfigMap_GCIO(hGCT);
4049 : }
4050 : else
4051 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigBeginType_GCIO)!=NULL )
4052 : {
4053 0 : res= _readConfigType_GCIO(hGCT);
4054 : }
4055 : else
4056 0 : if( strstr(GetGCCache_GCIO(hGCT),kConfigBeginField_GCIO)!=NULL )
4057 : {
4058 0 : res= _readConfigField_GCIO(hGCT);
4059 : }
4060 : else
4061 : { /* Skipping : Version, Origin, ... */
4062 0 : res= OGRERR_NONE;
4063 : }
4064 0 : if( res != OGRERR_NONE )
4065 : {
4066 0 : goto onError;
4067 : }
4068 0 : continue;
4069 : }
4070 : onError:
4071 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4072 0 : CPLError( CE_Failure, CPLE_AppDefined,
4073 : "Geoconcept config syntax error at line %ld.\n",
4074 : GetGCCurrentLinenum_GCIO(hGCT) );
4075 0 : return NULL;
4076 : }
4077 0 : if (eoc!=1)
4078 : {
4079 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4080 0 : CPLError( CE_Failure, CPLE_AppDefined,
4081 : "Geoconcept config end block %s not found.\n",
4082 : kConfigEndConfig_GCIO);
4083 0 : return NULL;
4084 : }
4085 :
4086 0 : if( (nt= CPLListCount(GetMetaTypes_GCIO(Meta)))==0 )
4087 : {
4088 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4089 0 : CPLError( CE_Failure, CPLE_AppDefined,
4090 : "No types found.\n");
4091 0 : return NULL;
4092 : }
4093 : /* for each general fields add it on top of types' fields list */
4094 0 : if( GetMetaFields_GCIO(Meta) )
4095 : {
4096 0 : if( (n= CPLListCount(GetMetaFields_GCIO(Meta)))>0 )
4097 : {
4098 0 : for (i= n-1; i>=0; i--)
4099 : {
4100 0 : if( (e= CPLListGet(GetMetaFields_GCIO(Meta),i)) )
4101 : {
4102 0 : if( (theField= (GCField*)CPLListGetData(e)) )
4103 : {
4104 0 : l[0]= '\0';
4105 0 : ll= 0;
4106 0 : if( (nl= CSLCount(GetFieldList_GCIO(theField)))>0 )
4107 : {
4108 0 : for (il= 0; il<nl; il++)
4109 : {
4110 0 : v= CSLGetField(GetFieldList_GCIO(theField),il);
4111 0 : snprintf(l+ll,kExtraSize_GCIO-ll-1,"%s;", v), l[kExtraSize_GCIO-1]= '\0';
4112 0 : ll+= strlen(v);
4113 : }
4114 : }
4115 0 : for (it= 0; it<nt; it++)
4116 : {
4117 0 : if( (et= CPLListGet(GetMetaTypes_GCIO(Meta),it)) )
4118 : {
4119 0 : if( (theClass= (GCType*)CPLListGetData(et)) )
4120 : {
4121 0 : if( AddTypeField_GCIO(hGCT,GetTypeName_GCIO(theClass),
4122 : 0,
4123 0 : GetFieldName_GCIO(theField),
4124 0 : GetFieldID_GCIO(theField),
4125 0 : GetFieldKind_GCIO(theField),
4126 0 : GetFieldExtra_GCIO(theField),
4127 : l)==NULL )
4128 : {
4129 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4130 0 : return NULL;
4131 : }
4132 : }
4133 : }
4134 : }
4135 : }
4136 : }
4137 : }
4138 0 : for (i= n-1; i>=0; i--)
4139 : {
4140 0 : if( (e= CPLListGet(GetMetaFields_GCIO(Meta),i)) )
4141 : {
4142 0 : if( (theField= (GCField*)CPLListGetData(e)) )
4143 : {
4144 0 : _DestroyField_GCIO(&theField);
4145 : }
4146 : }
4147 : }
4148 : }
4149 0 : CPLListDestroy(GetMetaFields_GCIO(Meta));
4150 0 : SetMetaFields_GCIO(Meta, NULL);
4151 : }
4152 :
4153 : /* for each field of types add it on top of types' subtypes field list */
4154 0 : for (it= 0; it<nt; it++)
4155 : {
4156 0 : if( (et= CPLListGet(GetMetaTypes_GCIO(Meta),it)) )
4157 : {
4158 0 : if( (theClass= (GCType*)CPLListGetData(et)) )
4159 : {
4160 0 : if( (ns= CPLListCount(GetTypeSubtypes_GCIO(theClass)))==0 )
4161 : {
4162 0 : CPLError( CE_Failure, CPLE_AppDefined,
4163 : "No subtypes found for type %s.\n",
4164 : GetTypeName_GCIO(theClass));
4165 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4166 0 : return NULL;
4167 : }
4168 0 : for (is= 0; is<ns; is++)
4169 : {
4170 0 : if( (es= CPLListGet(GetTypeSubtypes_GCIO(theClass),is)) )
4171 : {
4172 0 : if( (theSubType= (GCSubType*)CPLListGetData(es)) )
4173 : {
4174 0 : if( _findFieldByName_GCIO(GetTypeFields_GCIO(theClass),kNbFields_GCIO)==-1 &&
4175 0 : _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kNbFields_GCIO)==-1 )
4176 : {
4177 0 : if( AddSubTypeField_GCIO(hGCT,GetTypeName_GCIO(theClass),
4178 0 : GetSubTypeName_GCIO(theSubType),
4179 : 0,
4180 : kNbFields_GCIO,
4181 : -9999L,
4182 : vIntFld_GCIO,
4183 : NULL,
4184 : NULL)==NULL )
4185 : {
4186 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4187 0 : return NULL;
4188 : }
4189 : }
4190 0 : if( (n= CPLListCount(GetTypeFields_GCIO(theClass)))>0 )
4191 : {
4192 0 : for (i= n-1; i>=0; i--)
4193 : {
4194 0 : if( (e= CPLListGet(GetTypeFields_GCIO(theClass),i)) )
4195 : {
4196 0 : if( (theField= (GCField*)CPLListGetData(e)) )
4197 : {
4198 0 : l[0]= '\0';
4199 0 : ll= 0;
4200 0 : if( (nl= CSLCount(GetFieldList_GCIO(theField)))>0 )
4201 : {
4202 0 : for (il= 0; il<nl; il++)
4203 : {
4204 0 : v= CSLGetField(GetFieldList_GCIO(theField),il);
4205 0 : snprintf(l+ll,kExtraSize_GCIO-ll-1,"%s;", v), l[kExtraSize_GCIO-1]= '\0';
4206 0 : ll+= strlen(v);
4207 : }
4208 : }
4209 0 : if( AddSubTypeField_GCIO(hGCT,GetTypeName_GCIO(theClass),
4210 0 : GetSubTypeName_GCIO(theSubType),
4211 : 0,
4212 0 : GetFieldName_GCIO(theField),
4213 0 : GetFieldID_GCIO(theField),
4214 0 : GetFieldKind_GCIO(theField),
4215 0 : GetFieldExtra_GCIO(theField),
4216 : l)==NULL )
4217 : {
4218 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4219 0 : return NULL;
4220 : }
4221 : }
4222 : }
4223 : }
4224 : }
4225 : }
4226 : }
4227 : }
4228 0 : if( (n= CPLListCount(GetTypeFields_GCIO(theClass)))>0 )
4229 : {
4230 0 : for (i= n-1; i>=0; i--)
4231 : {
4232 0 : if( (e= CPLListGet(GetTypeFields_GCIO(theClass),i)) )
4233 : {
4234 0 : if( (theField= (GCField*)CPLListGetData(e)) )
4235 : {
4236 0 : _DestroyField_GCIO(&theField);
4237 : }
4238 : }
4239 : }
4240 : }
4241 0 : CPLListDestroy(GetTypeFields_GCIO(theClass));
4242 0 : SetTypeFields_GCIO(theClass, NULL);
4243 : }
4244 : }
4245 : }
4246 :
4247 : /* let's reorder sub-types fields : */
4248 0 : for (it= 0; it<nt; it++)
4249 : {
4250 0 : if( (et= CPLListGet(GetMetaTypes_GCIO(Meta),it)) )
4251 : {
4252 0 : if( (theClass= (GCType*)CPLListGetData(et)) )
4253 : {
4254 0 : ns= CPLListCount(GetTypeSubtypes_GCIO(theClass));
4255 0 : for (is= 0; is<ns; is++)
4256 : {
4257 0 : if( (es= CPLListGet(GetTypeSubtypes_GCIO(theClass),is)) )
4258 : {
4259 0 : if( (theSubType= (GCSubType*)CPLListGetData(es)) )
4260 : {
4261 0 : CPLList* orderedFields= NULL;
4262 0 : if( (n= CPLListCount(GetSubTypeFields_GCIO(theSubType)))>0 )
4263 : {
4264 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kIdentifier_GCIO))!=-1 )
4265 : {
4266 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4267 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4268 : {
4269 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4270 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4271 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4272 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4273 0 : return NULL;
4274 : }
4275 : }
4276 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kClass_GCIO))!=-1 )
4277 : {
4278 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4279 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4280 : {
4281 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4282 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4283 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4284 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4285 0 : return NULL;
4286 : }
4287 : }
4288 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kSubclass_GCIO))!=-1 )
4289 : {
4290 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4291 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4292 : {
4293 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4294 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4295 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4296 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4297 0 : return NULL;
4298 : }
4299 : }
4300 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kName_GCIO))!=-1 )
4301 : {
4302 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4303 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4304 : {
4305 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4306 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4307 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4308 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4309 0 : return NULL;
4310 : }
4311 : }
4312 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kNbFields_GCIO))!=-1 )
4313 : {
4314 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4315 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4316 : {
4317 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4318 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4319 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4320 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4321 0 : return NULL;
4322 : }
4323 : }
4324 0 : for( i= 0; i<n; i++ )
4325 : {
4326 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4327 0 : if( !IsPrivateField_GCIO((GCField*)CPLListGetData(e)) )
4328 : {
4329 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4330 : {
4331 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4332 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4333 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4334 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4335 0 : return NULL;
4336 : }
4337 : }
4338 : }
4339 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kX_GCIO))!=-1 )
4340 : {
4341 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4342 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4343 : {
4344 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4345 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4346 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4347 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4348 0 : return NULL;
4349 : }
4350 : }
4351 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kY_GCIO))!=-1 )
4352 : {
4353 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4354 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4355 : {
4356 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4357 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4358 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4359 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4360 0 : return NULL;
4361 : }
4362 : }
4363 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kXP_GCIO))!=-1 )
4364 : {
4365 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4366 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4367 : {
4368 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4369 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4370 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4371 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4372 0 : return NULL;
4373 : }
4374 : }
4375 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kYP_GCIO))!=-1 )
4376 : {
4377 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4378 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4379 : {
4380 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4381 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4382 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4383 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4384 0 : return NULL;
4385 : }
4386 : }
4387 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kGraphics_GCIO))!=-1 )
4388 : {
4389 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4390 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4391 : {
4392 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4393 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4394 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4395 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4396 0 : return NULL;
4397 : }
4398 : }
4399 0 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kAngle_GCIO))!=-1 )
4400 : {
4401 0 : e= CPLListGet(GetSubTypeFields_GCIO(theSubType),i);
4402 0 : if( !(orderedFields= CPLListAppend(orderedFields,(GCField*)CPLListGetData(e))) )
4403 : {
4404 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
4405 : "failed to arrange Geoconcept subtype '%s.%s' fields list.\n",
4406 : GetTypeName_GCIO(theClass), GetSubTypeName_GCIO(theSubType));
4407 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGCT)));
4408 0 : return NULL;
4409 : }
4410 : }
4411 0 : CPLListDestroy(GetSubTypeFields_GCIO(theSubType));
4412 0 : SetSubTypeFields_GCIO(theSubType,orderedFields);
4413 : }
4414 : }
4415 : }
4416 : }
4417 : }
4418 : }
4419 : }
4420 :
4421 0 : CPLDebug( "GEOCONCEPT",
4422 : "Metadata = (\n"
4423 : " nb Types : %d\n"
4424 : " Charset : %s\n"
4425 : " Delimiter : 0x%x\n"
4426 : " Unit : %s\n"
4427 : " Resolution : %g\n"
4428 : " Quoted-Text : %s\n"
4429 : " Format : %s\n"
4430 : " CoordSystemID : %d; TimeZoneValue : %d\n"
4431 : ")",
4432 : CPLListCount(GetMetaTypes_GCIO(Meta)),
4433 : GCCharset2str_GCIO(GetMetaCharset_GCIO(Meta)),
4434 0 : GetMetaDelimiter_GCIO(Meta),
4435 : GetMetaUnit_GCIO(Meta),
4436 : GetMetaResolution_GCIO(Meta),
4437 0 : GetMetaQuotedText_GCIO(Meta)? "yes":"no",
4438 0 : GetMetaFormat_GCIO(Meta)==1? "relative":"absolute",
4439 0 : GetMetaSysCoord_GCIO(Meta)? GetSysCoordSystemID_GCSRS(GetMetaSysCoord_GCIO(Meta)):-1,
4440 0 : GetMetaSysCoord_GCIO(Meta)? GetSysCoordTimeZone_GCSRS(GetMetaSysCoord_GCIO(Meta)):-1 );
4441 :
4442 0 : return Meta;
4443 : }/* ReadConfig_GCIO */
4444 :
4445 : /* -------------------------------------------------------------------- */
4446 1 : static FILE GCIOAPI_CALL1(*) _writeFieldsPragma_GCIO (
4447 : GCSubType* theSubType,
4448 : FILE* gc,
4449 : char delim
4450 : )
4451 : {
4452 : int nF, iF;
4453 : GCField* theField;
4454 : CPLList* e;
4455 :
4456 2 : fprintf(gc,"%s%s Class=%s;Subclass=%s;Kind=%d;Fields=",
4457 : kPragma_GCIO, kMetadataFIELDS_GCIO,
4458 1 : GetTypeName_GCIO(GetSubTypeType_GCIO(theSubType)),
4459 : GetSubTypeName_GCIO(theSubType),
4460 : (int)GetSubTypeKind_GCIO(theSubType));
4461 1 : if( (nF= CPLListCount(GetSubTypeFields_GCIO(theSubType)))>0 )
4462 : {
4463 11 : for (iF= 0; iF<nF; iF++)
4464 : {
4465 10 : if( (e= CPLListGet(GetSubTypeFields_GCIO(theSubType),iF)) )
4466 : {
4467 10 : if( (theField= (GCField*)CPLListGetData(e)) )
4468 : {
4469 10 : if (iF>0) fprintf(gc,"%c", delim);
4470 10 : if( IsPrivateField_GCIO(theField) )
4471 : {
4472 7 : fprintf(gc,"%s%s", kPrivate_GCIO, GetFieldName_GCIO(theField)+1);
4473 : }
4474 : else
4475 : {
4476 3 : fprintf(gc,"%s%s", kPublic_GCIO, GetFieldName_GCIO(theField));
4477 : }
4478 : }
4479 : }
4480 : }
4481 : }
4482 1 : fprintf(gc,"\n");
4483 1 : SetSubTypeHeaderWritten_GCIO(theSubType,TRUE);
4484 :
4485 1 : return gc;
4486 : }/* _writeFieldsPragma_GCIO */
4487 :
4488 : /* -------------------------------------------------------------------- */
4489 1 : GCExportFileH GCIOAPI_CALL1(*) WriteHeader_GCIO (
4490 : GCExportFileH* H
4491 : )
4492 : {
4493 : GCExportFileMetadata* Meta;
4494 : int nT, iT, nS, iS;
4495 : GCSubType* theSubType;
4496 : GCType* theClass;
4497 : CPLList* e;
4498 : FILE* gc;
4499 :
4500 : /* FIXME : howto change default values ? */
4501 : /* there seems to be no ways in Geoconcept to change them ... */
4502 1 : Meta= GetGCMeta_GCIO(H);
4503 1 : gc= GetGCHandle_GCIO(H);
4504 1 : if( GetMetaVersion_GCIO(Meta) )
4505 : {
4506 0 : fprintf(gc,"%s%s %s\n", kPragma_GCIO, kMetadataVERSION_GCIO, GetMetaVersion_GCIO(Meta));
4507 : }
4508 1 : fprintf(gc,"%s%s \"%s\"\n", kPragma_GCIO, kMetadataDELIMITER_GCIO, _metaDelimiter2str_GCIO(GetMetaDelimiter_GCIO(Meta)));
4509 1 : fprintf(gc,"%s%s \"%s\"\n", kPragma_GCIO, kMetadataQUOTEDTEXT_GCIO, GetMetaQuotedText_GCIO(Meta)? "yes":"no");
4510 1 : fprintf(gc,"%s%s %s\n", kPragma_GCIO, kMetadataCHARSET_GCIO, GCCharset2str_GCIO(GetMetaCharset_GCIO(Meta)));
4511 4 : if( strcmp(GetMetaUnit_GCIO(Meta),"deg")==0 ||
4512 1 : strcmp(GetMetaUnit_GCIO(Meta),"deg.min")==0 ||
4513 1 : strcmp(GetMetaUnit_GCIO(Meta),"rad")==0 ||
4514 1 : strcmp(GetMetaUnit_GCIO(Meta),"gr")==0 )
4515 : {
4516 0 : fprintf(gc,"%s%s Angle:%s\n", kPragma_GCIO, kMetadataUNIT_GCIO, GetMetaUnit_GCIO(Meta));
4517 : }
4518 : else
4519 : {
4520 1 : fprintf(gc,"%s%s Distance:%s\n", kPragma_GCIO, kMetadataUNIT_GCIO, GetMetaUnit_GCIO(Meta));
4521 : }
4522 1 : fprintf(gc,"%s%s %d\n", kPragma_GCIO, kMetadataFORMAT_GCIO, GetMetaFormat_GCIO(Meta));
4523 1 : if( GetMetaSysCoord_GCIO(Meta) )
4524 : {
4525 1 : fprintf(gc,"%s%s {Type: %d}", kPragma_GCIO,
4526 : kMetadataSYSCOORD_GCIO,
4527 1 : GetSysCoordSystemID_GCSRS(GetMetaSysCoord_GCIO(Meta)));
4528 1 : if( GetSysCoordTimeZone_GCSRS(GetMetaSysCoord_GCIO(Meta))!=-1 )
4529 : {
4530 0 : fprintf(gc,";{TimeZone: %d}", GetSysCoordTimeZone_GCSRS(GetMetaSysCoord_GCIO(Meta)));
4531 : }
4532 : }
4533 : else
4534 : {
4535 0 : fprintf(gc,"%s%s {Type: -1}", kPragma_GCIO, kMetadataSYSCOORD_GCIO);
4536 : }
4537 1 : fprintf(gc,"\n");
4538 :
4539 1 : if( (nT= CPLListCount(GetMetaTypes_GCIO(Meta)))>0 )
4540 : {
4541 2 : for (iT= 0; iT<nT; iT++)
4542 : {
4543 1 : if( (e= CPLListGet(GetMetaTypes_GCIO(Meta),iT)) )
4544 : {
4545 1 : if( (theClass= (GCType*)CPLListGetData(e)) )
4546 : {
4547 1 : if( (nS= CPLListCount(GetTypeSubtypes_GCIO(theClass)))>0 )
4548 : {
4549 2 : for (iS= 0; iS<nS; iS++)
4550 : {
4551 1 : if( (e= CPLListGet(GetTypeSubtypes_GCIO(theClass),iS)) )
4552 : {
4553 2 : if( (theSubType= (GCSubType*)CPLListGetData(e)) &&
4554 1 : !IsSubTypeHeaderWritten_GCIO(theSubType))
4555 : {
4556 1 : if( !_writeFieldsPragma_GCIO(theSubType,gc,GetMetaDelimiter_GCIO(Meta)) )
4557 : {
4558 0 : return NULL;
4559 : }
4560 : }
4561 : }
4562 : }
4563 : }
4564 : }
4565 : }
4566 : }
4567 : }
4568 :
4569 1 : return H;
4570 : }/* WriteHeader_GCIO */
4571 :
4572 : /* -------------------------------------------------------------------- */
4573 6 : GCExportFileMetadata GCIOAPI_CALL1(*) ReadHeader_GCIO (
4574 : GCExportFileH* hGXT
4575 : )
4576 : {
4577 : GCExportFileMetadata* Meta;
4578 :
4579 6 : if( _get_GCIO(hGXT)==EOF )
4580 : {
4581 0 : return NULL;
4582 : }
4583 6 : if( GetGCWhatIs_GCIO(hGXT)!=vPragma_GCIO )
4584 : {
4585 2 : CPLDebug( "GEOCONCEPT",
4586 : "Geoconcept export badly formatted :"
4587 : "%s expected.",
4588 : kPragma_GCIO);
4589 2 : return NULL;
4590 : }
4591 4 : SetGCMeta_GCIO(hGXT, CreateHeader_GCIO());
4592 4 : if( (Meta= GetGCMeta_GCIO(hGXT))==NULL )
4593 : {
4594 0 : return NULL;
4595 : }
4596 4 : SetMetaExtent_GCIO(Meta, CreateExtent_GCIO(HUGE_VAL,HUGE_VAL,-HUGE_VAL,-HUGE_VAL));
4597 51 : while( _get_GCIO(hGXT)!=EOF )
4598 : {
4599 43 : if( GetGCWhatIs_GCIO(hGXT)==vComType_GCIO )
4600 : {
4601 0 : continue;
4602 : }
4603 43 : if( GetGCWhatIs_GCIO(hGXT)==vPragma_GCIO )
4604 : {
4605 : /* try config header first ... */
4606 24 : if( !_parsePragma_GCIO(hGXT) )
4607 : {
4608 0 : return NULL;
4609 : }
4610 : /* in case of Memo read, we try parsing an object ... */
4611 24 : if( GetGCStatus_GCIO(hGXT)!=vMemoStatus_GCIO )
4612 : {
4613 24 : continue;
4614 : }
4615 : }
4616 : /* then object ... */
4617 19 : if( !_parseObject_GCIO(hGXT) )
4618 : {
4619 0 : return NULL;
4620 : }
4621 : }
4622 4 : if( CPLListCount(GetMetaTypes_GCIO(Meta))==0 )
4623 : {
4624 0 : DestroyHeader_GCIO(&(GetGCMeta_GCIO(hGXT)));
4625 0 : CPLError( CE_Failure, CPLE_AppDefined,
4626 : "Geoconcept export syntax error at line %ld.\n",
4627 : GetGCCurrentLinenum_GCIO(hGXT) );
4628 0 : return NULL;
4629 : }
4630 :
4631 4 : Rewind_GCIO(hGXT,NULL);
4632 :
4633 28 : CPLDebug( "GEOCONCEPT",
4634 : "Metadata = (\n"
4635 : " nb Types : %d\n"
4636 : " Charset : %s\n"
4637 : " Delimiter : 0x%x\n"
4638 : " Unit : %s\n"
4639 : " Resolution : %g\n"
4640 : " Quoted-Text : %s\n"
4641 : " Format : %s\n"
4642 : " CoordSystemID : %d; TimeZoneValue : %d\n"
4643 : ")",
4644 : CPLListCount(GetMetaTypes_GCIO(Meta)),
4645 : GCCharset2str_GCIO(GetMetaCharset_GCIO(Meta)),
4646 4 : GetMetaDelimiter_GCIO(Meta),
4647 : GetMetaUnit_GCIO(Meta),
4648 : GetMetaResolution_GCIO(Meta),
4649 4 : GetMetaQuotedText_GCIO(Meta)? "yes":"no",
4650 4 : GetMetaFormat_GCIO(Meta)==1? "relative":"absolute",
4651 8 : GetMetaSysCoord_GCIO(Meta)? GetSysCoordSystemID_GCSRS(GetMetaSysCoord_GCIO(Meta)):-1,
4652 8 : GetMetaSysCoord_GCIO(Meta)? GetSysCoordTimeZone_GCSRS(GetMetaSysCoord_GCIO(Meta)):-1 );
4653 :
4654 4 : return Meta;
4655 : }/* ReadHeader_GCIO */
4656 :
4657 : /* -------------------------------------------------------------------- */
4658 1 : GCSubType GCIOAPI_CALL1(*) FindFeature_GCIO (
4659 : GCExportFileH* hGCT,
4660 : const char* typDOTsubtypName
4661 : )
4662 : {
4663 : char **fe;
4664 : int whereClass, whereSubType;
4665 : GCType* theClass;
4666 : GCSubType* theSubType;
4667 1 : if( hGCT==NULL ) return NULL;
4668 1 : if( typDOTsubtypName==NULL ) return NULL;
4669 2 : if( !(fe= CSLTokenizeString2(typDOTsubtypName,".",0)) ||
4670 1 : CSLCount(fe)!=2 )
4671 : {
4672 0 : CSLDestroy(fe);
4673 0 : return NULL;
4674 : }
4675 1 : if( (whereClass= _findTypeByName_GCIO(hGCT,fe[0]))==-1 )
4676 : {
4677 1 : CSLDestroy(fe);
4678 1 : return NULL;
4679 : }
4680 0 : theClass= _getType_GCIO(hGCT,whereClass);
4681 0 : if( (whereSubType= _findSubTypeByName_GCIO(theClass,fe[1]))==-1 )
4682 : {
4683 0 : CSLDestroy(fe);
4684 0 : return NULL;
4685 : }
4686 0 : theSubType= _getSubType_GCIO(theClass,whereSubType);
4687 0 : CSLDestroy(fe);
4688 0 : return theSubType;
4689 : }/* FindFeature_GCIO */
4690 :
4691 : /* -------------------------------------------------------------------- */
4692 6 : int GCIOAPI_CALL FindFeatureFieldIndex_GCIO (
4693 : GCSubType* theSubType,
4694 : const char *fieldName
4695 : )
4696 : {
4697 6 : if( theSubType==NULL ) return -1;
4698 6 : if( fieldName==NULL ) return -1;
4699 6 : return _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),fieldName);
4700 : }/* FindFeatureFieldIndex_GCIO */
4701 :
4702 : /* -------------------------------------------------------------------- */
4703 3 : GCField GCIOAPI_CALL1(*) FindFeatureField_GCIO (
4704 : GCSubType* theSubType,
4705 : const char *fieldName
4706 : )
4707 : {
4708 : int whereField;
4709 : GCField* theField;
4710 3 : if( (whereField= FindFeatureFieldIndex_GCIO(theSubType,fieldName))==-1 )
4711 : {
4712 3 : return NULL;
4713 : }
4714 0 : theField= _getField_GCIO(GetSubTypeFields_GCIO(theSubType),whereField);
4715 0 : return theField;
4716 : }/* FindFeatureField_GCIO */
4717 :
4718 : /* -------------------------------------------------------------------- */
4719 12 : static char GCIOAPI_CALL1(*) _escapeString_GCIO (
4720 : GCExportFileH* H,
4721 : const char *theString
4722 : )
4723 : {
4724 : int l, i, o;
4725 : char *res;
4726 23 : if( !theString ||
4727 11 : (l= strlen(theString))==0 )
4728 : {
4729 1 : res= CPLStrdup(theString);
4730 1 : return res;
4731 : }
4732 11 : if( (res= (char *)CPLMalloc(l*2)) )
4733 : {
4734 67 : for (i= 0, o= 0; i<l; i++, o++)
4735 : {
4736 56 : switch( theString[i] )
4737 : {
4738 : case '\t' :
4739 0 : res[o]= '#';
4740 0 : o++;
4741 0 : res[o]= '#';
4742 0 : break;
4743 : case '\r' :
4744 : case '\n' :
4745 0 : res[o]= '@';
4746 0 : break;
4747 : default :
4748 56 : res[o]= theString[i];
4749 : break;
4750 : }
4751 : }
4752 11 : res[o]= '\0';
4753 : }
4754 11 : return res;
4755 : }/* _escapeString_GCIO */
4756 :
4757 : /* -------------------------------------------------------------------- */
4758 10 : static int GCIOAPI_CALL _findNextFeatureFieldToWrite_GCIO (
4759 : GCSubType* theSubType,
4760 : int from,
4761 : long id
4762 : )
4763 : {
4764 : GCExportFileH* H;
4765 : FILE *h;
4766 : int n, i;
4767 : GCField* theField;
4768 : char* fieldName, *quotes, *escapedValue, delim;
4769 :
4770 10 : if( (n= CountSubTypeFields_GCIO(theSubType))==0 )
4771 : {
4772 0 : return WRITECOMPLETED_GCIO;
4773 : }
4774 10 : if( !(from<n) )
4775 : {
4776 2 : return WRITECOMPLETED_GCIO;
4777 : }
4778 :
4779 8 : H= GetSubTypeGCHandle_GCIO(theSubType);
4780 8 : h= GetGCHandle_GCIO(H);
4781 : /* Dimension pragma for 3DM et 3D : */
4782 8 : if( from==0 )
4783 : {
4784 2 : if( GetSubTypeDim_GCIO(theSubType)==v3DM_GCIO )
4785 : {
4786 0 : if( VSIFPrintf(h,"%s%s\n", kPragma_GCIO, k3DOBJECTMONO_GCIO)<=0 )
4787 : {
4788 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
4789 0 : return WRITEERROR_GCIO;
4790 : }
4791 0 : SetGCCurrentLinenum_GCIO(H,GetGCCurrentLinenum_GCIO(H)+1L);
4792 : }
4793 2 : else if( GetSubTypeDim_GCIO(theSubType)==v3D_GCIO )
4794 : {
4795 0 : if( VSIFPrintf(h,"%s%s\n", kPragma_GCIO, k3DOBJECT_GCIO)<=0 )
4796 : {
4797 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
4798 0 : return WRITEERROR_GCIO;
4799 : }
4800 0 : SetGCCurrentLinenum_GCIO(H,GetGCCurrentLinenum_GCIO(H)+1L);
4801 : }
4802 : }
4803 8 : if( GetMetaQuotedText_GCIO(GetGCMeta_GCIO(H)) )
4804 : {
4805 0 : quotes= "\"";
4806 : }
4807 : else
4808 : {
4809 8 : quotes= "";
4810 : }
4811 8 : delim= GetMetaDelimiter_GCIO(GetGCMeta_GCIO(H));
4812 : /* Fields are written in the same order as in the sub-type definition. */
4813 : /* Check for Private# fields : */
4814 18 : for (i= from; i<n; i++)
4815 : {
4816 18 : theField= GetSubTypeField_GCIO(theSubType,i);
4817 18 : if( !IsPrivateField_GCIO(theField) )
4818 : {
4819 6 : return i;/* needs a call to WriteFeatureField_GCIO() for the ith field */
4820 : }
4821 12 : fieldName= GetFieldName_GCIO(theField);
4822 62 : if( EQUAL(fieldName,kX_GCIO) ||
4823 10 : EQUAL(fieldName,kY_GCIO) ||
4824 10 : EQUAL(fieldName,kXP_GCIO) ||
4825 10 : EQUAL(fieldName,kYP_GCIO) ||
4826 10 : EQUAL(fieldName,kGraphics_GCIO) ||
4827 10 : EQUAL(fieldName,kAngle_GCIO) )
4828 : {
4829 2 : return GEOMETRYEXPECTED_GCIO;/* needs a call to WriteFeatureGeometry_GCIO() now */
4830 : }
4831 10 : if( EQUAL(fieldName,kIdentifier_GCIO) )
4832 : {
4833 : /* long integer which GeoConcept may use as a key for the object it will create. */
4834 : /* If set to '-1', it will be ignored. */
4835 2 : if( VSIFPrintf(h,"%s%ld%s", quotes, id, quotes)<=0 )
4836 : {
4837 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
4838 0 : return WRITEERROR_GCIO;
4839 : }
4840 : }
4841 8 : else if( EQUAL(fieldName,kClass_GCIO) )
4842 : {
4843 2 : if( !(escapedValue= _escapeString_GCIO(H,GetTypeName_GCIO(GetSubTypeType_GCIO(theSubType)))) )
4844 : {
4845 0 : return WRITEERROR_GCIO;
4846 : }
4847 2 : if( VSIFPrintf(h,"%s%s%s", quotes, escapedValue, quotes)<=0 )
4848 : {
4849 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
4850 0 : return WRITEERROR_GCIO;
4851 : }
4852 2 : CPLFree(escapedValue);
4853 : }
4854 6 : else if( EQUAL(fieldName,kSubclass_GCIO) )
4855 : {
4856 2 : if( !(escapedValue= _escapeString_GCIO(H,GetSubTypeName_GCIO(theSubType))) )
4857 : {
4858 0 : return WRITEERROR_GCIO;
4859 : }
4860 2 : if( VSIFPrintf(h,"%s%s%s", quotes, escapedValue, quotes)<=0 )
4861 : {
4862 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
4863 0 : return WRITEERROR_GCIO;
4864 : }
4865 2 : CPLFree(escapedValue);
4866 : }
4867 4 : else if( EQUAL(fieldName,kName_GCIO) )
4868 : {
4869 2 : if( !(escapedValue= _escapeString_GCIO(H,GetSubTypeName_GCIO(theSubType))) )
4870 : {
4871 0 : return WRITEERROR_GCIO;
4872 : }
4873 2 : if( VSIFPrintf(h,"%s%s%s", quotes, escapedValue, quotes)<=0 )
4874 : {
4875 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
4876 0 : return WRITEERROR_GCIO;
4877 : }
4878 2 : CPLFree(escapedValue);
4879 : }
4880 2 : else if( EQUAL(fieldName,kNbFields_GCIO) )
4881 : {
4882 2 : if( VSIFPrintf(h,"%s%d%s", quotes, GetSubTypeNbFields_GCIO(theSubType), quotes)<=0 )
4883 : {
4884 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
4885 0 : return WRITEERROR_GCIO;
4886 : }
4887 : }
4888 : else
4889 : {
4890 0 : CPLError( CE_Failure, CPLE_NotSupported,
4891 : "Writing %s field is not implemented.\n",
4892 : fieldName );
4893 0 : return WRITEERROR_GCIO;
4894 : }
4895 10 : if( i!=n-1 )
4896 : {
4897 10 : if( VSIFPrintf(h,"%c", delim)<=0 )
4898 : {
4899 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
4900 0 : return WRITEERROR_GCIO;
4901 : }
4902 : }
4903 : }
4904 :
4905 0 : return WRITECOMPLETED_GCIO;
4906 : }/* _findNextFeatureFieldToWrite_GCIO */
4907 :
4908 : /* -------------------------------------------------------------------- */
4909 2 : int GCIOAPI_CALL StartWritingFeature_GCIO (
4910 : GCSubType* theSubType,
4911 : long id
4912 : )
4913 : {
4914 2 : if( !IsSubTypeHeaderWritten_GCIO(theSubType) )
4915 : {
4916 : GCExportFileH* H;
4917 : FILE *h;
4918 :
4919 0 : H= GetSubTypeGCHandle_GCIO(theSubType);
4920 0 : h= GetGCHandle_GCIO(H);
4921 0 : if( !_writeFieldsPragma_GCIO(theSubType,h,GetMetaDelimiter_GCIO(GetGCMeta_GCIO(H))) )
4922 : {
4923 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write Fields pragma failed for feature id %ld.\n", id);
4924 0 : return WRITEERROR_GCIO;
4925 : }
4926 : }
4927 2 : return _findNextFeatureFieldToWrite_GCIO(theSubType,0,id);
4928 : }/* StartWritingFeature_GCIO */
4929 :
4930 : /* -------------------------------------------------------------------- */
4931 2 : static int GCIOAPI_CALL _writePoint_GCIO (
4932 : FILE* h,
4933 : const char* quotes,
4934 : char delim,
4935 : double x, double y, double z,
4936 : GCDim dim,
4937 : GCExtent* e,
4938 : int pCS,
4939 : int hCS
4940 : )
4941 : {
4942 2 : SetExtentULAbscissa_GCIO(e,x);
4943 2 : SetExtentULOrdinate_GCIO(e,y);
4944 2 : SetExtentLRAbscissa_GCIO(e,x);
4945 2 : SetExtentLROrdinate_GCIO(e,y);
4946 2 : if( dim==v3DM_GCIO || dim==v3D_GCIO )
4947 : {
4948 0 : if( VSIFPrintf(h,"%s%.*f%s%c%s%.*f%s%c%s%.*f%s",
4949 : quotes, pCS, x, quotes,
4950 : delim,
4951 : quotes, pCS, y, quotes,
4952 : delim,
4953 : quotes, hCS, z, quotes)<=0 )
4954 : {
4955 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
4956 0 : return FALSE;
4957 : }
4958 : }
4959 : else
4960 : {
4961 2 : if( VSIFPrintf(h,"%s%.*f%s%c%s%.*f%s",
4962 : quotes, pCS, x, quotes,
4963 : delim,
4964 : quotes, pCS, y, quotes)<=0 )
4965 : {
4966 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
4967 0 : return FALSE;
4968 : }
4969 : }
4970 2 : return TRUE;
4971 : }/* _writePoint_GCIO */
4972 :
4973 : /* -------------------------------------------------------------------- */
4974 0 : static int GCIOAPI_CALL _writeLine_GCIO (
4975 : FILE* h,
4976 : const char* quotes,
4977 : char delim,
4978 : OGRGeometryH poArc,
4979 : GCTypeKind knd,
4980 : GCDim dim,
4981 : int fmt,
4982 : GCExtent* e,
4983 : int pCS,
4984 : int hCS
4985 : )
4986 : {
4987 : int iP, nP;
4988 : double dX, dY, dZ;
4989 : /* 1st point */
4990 0 : if( !_writePoint_GCIO(h,quotes,delim,
4991 : OGR_G_GetX(poArc,0),
4992 : OGR_G_GetY(poArc,0),
4993 : OGR_G_GetZ(poArc,0),
4994 : dim,e,pCS,hCS) )
4995 : {
4996 0 : return FALSE;
4997 : }
4998 0 : if( VSIFPrintf(h,"%c", delim)<=0 )
4999 : {
5000 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5001 0 : return FALSE;
5002 : }
5003 0 : nP= OGR_G_GetPointCount(poArc);
5004 0 : if( knd==vLine_GCIO )
5005 : {
5006 : /* last point */
5007 0 : if( !_writePoint_GCIO(h,quotes,delim,
5008 : OGR_G_GetX(poArc,nP-1),
5009 : OGR_G_GetY(poArc,nP-1),
5010 : OGR_G_GetZ(poArc,nP-1),
5011 : dim,e,pCS,hCS) )
5012 : {
5013 0 : return FALSE;
5014 : }
5015 0 : if( VSIFPrintf(h,"%c", delim)<=0 )
5016 : {
5017 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5018 0 : return FALSE;
5019 : }
5020 : }
5021 : /* number of remaining points : */
5022 0 : if( VSIFPrintf(h,"%s%d%s%c", quotes, nP-1, quotes, delim)<=0 )
5023 : {
5024 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5025 0 : return FALSE;
5026 : }
5027 : /* 2nd up to the last point ... */
5028 0 : for( iP= 1; iP<nP; iP++ )
5029 : {
5030 0 : if( fmt==1 )
5031 : { /* relative coordinates ... */
5032 0 : dX= OGR_G_GetX(poArc,iP-1) - OGR_G_GetX(poArc,iP);
5033 0 : dY= OGR_G_GetY(poArc,iP-1) - OGR_G_GetY(poArc,iP);
5034 0 : dZ= OGR_G_GetZ(poArc,iP-1) - OGR_G_GetZ(poArc,iP);
5035 : }
5036 : else
5037 : { /* absolute coordinates ... */
5038 0 : dX= OGR_G_GetX(poArc,iP);
5039 0 : dY= OGR_G_GetY(poArc,iP);
5040 0 : dZ= OGR_G_GetZ(poArc,iP);
5041 : }
5042 0 : if( !_writePoint_GCIO(h,quotes,delim,
5043 : dX,
5044 : dY,
5045 : dZ,
5046 : dim,e,pCS,hCS) )
5047 : {
5048 0 : return FALSE;
5049 : }
5050 0 : if( iP!=nP-1 )
5051 : {
5052 0 : if( VSIFPrintf(h,"%c", delim)<=0 )
5053 : {
5054 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5055 0 : return FALSE;
5056 : }
5057 : }
5058 : }
5059 0 : return TRUE;
5060 : }/* _writeLine_GCIO */
5061 :
5062 : /* -------------------------------------------------------------------- */
5063 0 : static int GCIOAPI_CALL _writePolygon_GCIO (
5064 : FILE* h,
5065 : const char* quotes,
5066 : char delim,
5067 : OGRGeometryH poPoly,
5068 : GCDim dim,
5069 : int fmt,
5070 : GCExtent* e,
5071 : int pCS,
5072 : int hCS
5073 : )
5074 : {
5075 : int iR, nR;
5076 : OGRGeometryH poRing;
5077 : /*
5078 : * X<>Y[<>Z]{Single Polygon{<>NrPolys=j[<>X<>Y[<>Z]<>Single Polygon]j}}
5079 : * with :
5080 : * Single Polygon = Nr points=k[<>PointX<>PointY[<>Z]]k...
5081 : */
5082 0 : if( (nR= OGR_G_GetGeometryCount(poPoly))==0 )
5083 : {
5084 0 : CPLError( CE_Warning, CPLE_AppDefined,
5085 : "Ignore POLYGON EMPTY in Geoconcept writer.\n" );
5086 0 : return TRUE;
5087 : }
5088 0 : poRing= OGR_G_GetGeometryRef(poPoly,0);
5089 0 : if( !_writeLine_GCIO(h,quotes,delim,poRing,vPoly_GCIO,dim,fmt,e,pCS,hCS) )
5090 : {
5091 0 : return FALSE;
5092 : }
5093 : /* number of interior rings : */
5094 0 : if( nR>1 )
5095 : {
5096 0 : if( VSIFPrintf(h,"%c%d%c", delim, nR-1, delim)<=0 )
5097 : {
5098 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5099 0 : return FALSE;
5100 : }
5101 0 : for( iR= 1; iR<nR; iR++ )
5102 : {
5103 0 : poRing= OGR_G_GetGeometryRef(poPoly,iR);
5104 0 : if( !_writeLine_GCIO(h,quotes,delim,poRing,vPoly_GCIO,dim,fmt,e,pCS,hCS) )
5105 : {
5106 0 : return FALSE;
5107 : }
5108 0 : if( iR!=nR-1 )
5109 : {
5110 0 : if( VSIFPrintf(h,"%c", delim)<=0 )
5111 : {
5112 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5113 0 : return FALSE;
5114 : }
5115 : }
5116 : }
5117 : }
5118 0 : return TRUE;
5119 : }/* _writePolygon_GCIO */
5120 :
5121 : /* -------------------------------------------------------------------- */
5122 2 : int GCIOAPI_CALL WriteFeatureGeometry_GCIO (
5123 : GCSubType* theSubType,
5124 : OGRGeometryH poGeom
5125 : )
5126 : {
5127 : GCExportFileH* H;
5128 : FILE *h;
5129 : int n, i, iAn, pCS, hCS;
5130 : char *quotes, delim;
5131 :
5132 2 : H= GetSubTypeGCHandle_GCIO(theSubType);
5133 2 : h= GetGCHandle_GCIO(H);
5134 2 : n= CountSubTypeFields_GCIO(theSubType);
5135 2 : iAn= -1;
5136 2 : pCS= hCS= 0;
5137 2 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kGraphics_GCIO))==-1 )
5138 : {
5139 2 : if( (i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kAngle_GCIO))==-1 )
5140 : {
5141 2 : i= _findFieldByName_GCIO(GetSubTypeFields_GCIO(theSubType),kY_GCIO);
5142 : }
5143 : else
5144 : {
5145 0 : iAn= i;
5146 : }
5147 : }
5148 :
5149 2 : if( GetMetaQuotedText_GCIO(GetGCMeta_GCIO(H)) )
5150 : {
5151 0 : quotes= "\"";
5152 : }
5153 : else
5154 : {
5155 2 : quotes= "";
5156 : }
5157 2 : delim= GetMetaDelimiter_GCIO(GetGCMeta_GCIO(H));
5158 :
5159 2 : if( (pCS= GetMetaPlanarFormat_GCIO(GetGCMeta_GCIO(H)))==0 )
5160 : {
5161 1 : if( OSRIsGeographic(GetMetaSRS_GCIO(GetGCMeta_GCIO(H))) )
5162 : {
5163 1 : pCS= kGeographicPlanarRadix;
5164 : }
5165 : else
5166 : {
5167 0 : pCS= kCartesianPlanarRadix;
5168 : }
5169 1 : SetMetaPlanarFormat_GCIO(GetGCMeta_GCIO(H), pCS);
5170 : }
5171 :
5172 2 : if (GetSubTypeDim_GCIO(theSubType)==v3D_GCIO &&
5173 0 : (hCS= GetMetaHeightFormat_GCIO(GetGCMeta_GCIO(H)))==0 )
5174 : {
5175 0 : hCS= kElevationRadix;
5176 0 : SetMetaHeightFormat_GCIO(GetGCMeta_GCIO(H), hCS);
5177 : }
5178 :
5179 2 : switch( OGR_G_GetGeometryType(poGeom) ) {
5180 : case wkbPoint :
5181 : case wkbPoint25D :
5182 2 : if( !_writePoint_GCIO(h,quotes,delim,
5183 : OGR_G_GetX(poGeom,0),
5184 : OGR_G_GetY(poGeom,0),
5185 : OGR_G_GetZ(poGeom,0),
5186 : GetSubTypeDim_GCIO(theSubType),
5187 2 : GetMetaExtent_GCIO(GetGCMeta_GCIO(H)),
5188 : pCS, hCS) )
5189 : {
5190 0 : return WRITEERROR_GCIO;
5191 : }
5192 2 : break;
5193 : case wkbLineString :
5194 : case wkbLineString25D :
5195 0 : if( !_writeLine_GCIO(h,quotes,delim,
5196 : poGeom,
5197 : vLine_GCIO,
5198 : GetSubTypeDim_GCIO(theSubType),
5199 0 : GetMetaFormat_GCIO(GetGCMeta_GCIO(H)),
5200 0 : GetMetaExtent_GCIO(GetGCMeta_GCIO(H)),
5201 : pCS, hCS) )
5202 : {
5203 0 : return WRITEERROR_GCIO;
5204 : }
5205 0 : break;
5206 : case wkbPolygon :
5207 : case wkbPolygon25D :
5208 0 : if( !_writePolygon_GCIO(h,quotes,delim,
5209 : poGeom,
5210 : GetSubTypeDim_GCIO(theSubType),
5211 0 : GetMetaFormat_GCIO(GetGCMeta_GCIO(H)),
5212 0 : GetMetaExtent_GCIO(GetGCMeta_GCIO(H)),
5213 : pCS, hCS) )
5214 : {
5215 0 : return WRITEERROR_GCIO;
5216 : }
5217 0 : break;
5218 : case wkbMultiPoint :
5219 : case wkbMultiPoint25D :
5220 : case wkbMultiLineString :
5221 : case wkbMultiLineString25D :
5222 : case wkbMultiPolygon :
5223 : case wkbMultiPolygon25D :
5224 : case wkbUnknown :
5225 : case wkbGeometryCollection :
5226 : case wkbGeometryCollection25D :
5227 : case wkbNone :
5228 : case wkbLinearRing :
5229 : default :
5230 0 : CPLError( CE_Warning, CPLE_AppDefined,
5231 : "Geometry type %d not supported in Geoconcept, feature skipped.\n",
5232 0 : OGR_G_GetGeometryType(poGeom) );
5233 : break;
5234 : }
5235 : /* Angle= 0 !! */
5236 2 : if( iAn!=-1 )
5237 : {
5238 0 : if( VSIFPrintf(h,"%c%s%1d%s", delim, quotes, 0, quotes)<=0 )
5239 : {
5240 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5241 0 : return WRITEERROR_GCIO;
5242 : }
5243 : }
5244 : /* if it is not the last field ... */
5245 2 : if( i!=n-1 )
5246 : {
5247 0 : if( VSIFPrintf(h,"%c", delim)<=0 )
5248 : {
5249 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5250 0 : return WRITEERROR_GCIO;
5251 : }
5252 : }
5253 :
5254 : /* find out next field to write ... */
5255 2 : return _findNextFeatureFieldToWrite_GCIO(theSubType,i+1,OGRNullFID);
5256 : }/* WriteFeatureGeometry_GCIO */
5257 :
5258 : /* -------------------------------------------------------------------- */
5259 6 : int GCIOAPI_CALL WriteFeatureFieldAsString_GCIO (
5260 : GCSubType* theSubType,
5261 : int iField,
5262 : const char* theValue
5263 : )
5264 : {
5265 : GCExportFileH* H;
5266 : FILE *h;
5267 : int n;
5268 : char *quotes, *escapedValue, delim;
5269 : GCField* theField;
5270 :
5271 6 : H= GetSubTypeGCHandle_GCIO(theSubType);
5272 6 : h= GetGCHandle_GCIO(H);
5273 6 : n= CountSubTypeFields_GCIO(theSubType);
5274 6 : if( GetMetaQuotedText_GCIO(GetGCMeta_GCIO(H)) )
5275 : {
5276 0 : quotes= "\"";
5277 : }
5278 : else
5279 : {
5280 6 : quotes= "";
5281 : }
5282 6 : delim= GetMetaDelimiter_GCIO(GetGCMeta_GCIO(H));
5283 6 : if( !(theField= GetSubTypeField_GCIO(theSubType,iField)) )
5284 : {
5285 0 : CPLError( CE_Failure, CPLE_NotSupported,
5286 : "Attempt to write a field #%d that does not exist on feature %s.%s.\n",
5287 : iField,
5288 0 : GetTypeName_GCIO(GetSubTypeType_GCIO(theSubType)),
5289 : GetSubTypeName_GCIO(theSubType) );
5290 0 : return WRITEERROR_GCIO;
5291 : }
5292 6 : if( !(escapedValue= _escapeString_GCIO(H,theValue)) )
5293 : {
5294 0 : return WRITEERROR_GCIO;
5295 : }
5296 6 : if( VSIFPrintf(h,"%s%s%s", quotes, escapedValue, quotes)<=0 )
5297 : {
5298 : /* it is really an error if one of the parameters is not empty ... */
5299 1 : if( *quotes!='\0' || *escapedValue!='\0')
5300 : {
5301 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5302 0 : return WRITEERROR_GCIO;
5303 : }
5304 : }
5305 6 : if( iField!=n-1 )
5306 : {
5307 6 : if( VSIFPrintf(h,"%c", delim)<=0 )
5308 : {
5309 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5310 0 : return WRITEERROR_GCIO;
5311 : }
5312 : }
5313 6 : CPLFree(escapedValue);
5314 :
5315 6 : return _findNextFeatureFieldToWrite_GCIO(theSubType,iField+1,OGRNullFID);
5316 : }/* WriteFeatureFieldAsString_GCIO */
5317 :
5318 : /* -------------------------------------------------------------------- */
5319 2 : void GCIOAPI_CALL StopWritingFeature_GCIO (
5320 : GCSubType* theSubType
5321 : )
5322 : {
5323 : GCExportFileH* H;
5324 :
5325 2 : H= GetSubTypeGCHandle_GCIO(theSubType);
5326 2 : if( VSIFPrintf(GetGCHandle_GCIO(H),"\n")<=0 )
5327 : {
5328 0 : CPLError( CE_Failure, CPLE_AppDefined, "Write failed.\n");
5329 : }
5330 2 : SetSubTypeNbFeatures_GCIO(theSubType, GetSubTypeNbFeatures_GCIO(theSubType)+1L);
5331 2 : SetGCNbObjects_GCIO(H, GetGCNbObjects_GCIO(H)+1L);
5332 2 : SetGCCurrentLinenum_GCIO(H,GetGCCurrentLinenum_GCIO(H)+1L);
5333 2 : }/* StopWritingFeature_GCIO */
5334 :
5335 : /* -------------------------------------------------------------------- */
5336 33 : OGRFeatureH GCIOAPI_CALL ReadNextFeature_GCIO (
5337 : GCSubType* theSubType
5338 : )
5339 : {
5340 : OGRFeatureH f;
5341 : GCExportFileH* H;
5342 : GCExportFileMetadata* Meta;
5343 : GCDim d;
5344 :
5345 33 : f= NULL;
5346 33 : H= GetSubTypeGCHandle_GCIO(theSubType);
5347 33 : if( !(Meta= GetGCMeta_GCIO(H)) )
5348 : {
5349 0 : return NULL;
5350 : }
5351 33 : d= vUnknown3D_GCIO;
5352 87 : while( _get_GCIO(H)!=EOF )
5353 : {
5354 48 : if( GetGCWhatIs_GCIO(H)==vComType_GCIO )
5355 : {
5356 0 : continue;
5357 : }
5358 : /* analyze the line according to schema : */
5359 48 : if( GetGCWhatIs_GCIO(H)==vPragma_GCIO )
5360 : {
5361 21 : if( strstr(GetGCCache_GCIO(H),k3DOBJECTMONO_GCIO) )
5362 : {
5363 0 : d= v3DM_GCIO;
5364 : }
5365 21 : else if( strstr(GetGCCache_GCIO(H),k3DOBJECT_GCIO) )
5366 : {
5367 0 : d= v3D_GCIO;
5368 : }
5369 21 : else if( strstr(GetGCCache_GCIO(H),k2DOBJECT_GCIO) )
5370 : {
5371 0 : d= v2D_GCIO;
5372 : }
5373 21 : continue;
5374 : }
5375 27 : if( (f= _buildOGRFeature_GCIO(H,&theSubType,d,NULL)) )
5376 : {
5377 27 : break;
5378 : }
5379 0 : d= vUnknown3D_GCIO;
5380 : }
5381 :
5382 33 : return f;
5383 : }/* ReadNextFeature_GCIO */
5384 :
|