-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathmitab_imapinfofile.cpp
745 lines (669 loc) · 24.8 KB
/
mitab_imapinfofile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/**********************************************************************
*
* Name: mitab_imapinfo
* Project: MapInfo mid/mif Tab Read/Write library
* Language: C++
* Purpose: Implementation of the IMapInfoFile class, super class of
* of MIFFile and TABFile
* Author: Daniel Morissette, dmorissette@dmsolutions.ca
*
**********************************************************************
* Copyright (c) 1999-2008, Daniel Morissette
* Copyright (c) 2014, Even Rouault <even.rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
**********************************************************************/
#include "cpl_port.h"
#include "mitab.h"
#include <cassert>
#include <cctype>
#include <cstring>
#include <algorithm>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_vsi.h"
#include "mitab_priv.h"
#include "mitab_utils.h"
#include "ogr_core.h"
#include "ogr_feature.h"
#include "ogr_geometry.h"
#ifdef __HP_aCC
#include <wchar.h> /* iswspace() */
#else
#include <wctype.h> /* iswspace() */
#endif
/**********************************************************************
* IMapInfoFile::IMapInfoFile()
*
* Constructor.
**********************************************************************/
IMapInfoFile::IMapInfoFile(GDALDataset *poDS)
: m_poDS(poDS), m_nCurFeatureId(0), m_poCurFeature(nullptr),
m_bBoundsSet(FALSE), m_pszCharset(nullptr)
{
}
/**********************************************************************
* IMapInfoFile::~IMapInfoFile()
*
* Destructor.
**********************************************************************/
IMapInfoFile::~IMapInfoFile()
{
if (m_poCurFeature)
{
delete m_poCurFeature;
m_poCurFeature = nullptr;
}
CPLFree(m_pszCharset);
m_pszCharset = nullptr;
}
/**********************************************************************
* IMapInfoFile::Open()
*
* Compatibility layer with new interface.
* Return 0 on success, -1 in case of failure.
**********************************************************************/
int IMapInfoFile::Open(const char *pszFname, const char *pszAccess,
GBool bTestOpenNoError, const char *pszCharset)
{
// cppcheck-suppress nullPointer
if (STARTS_WITH_CI(pszAccess, "r"))
return Open(pszFname, TABRead, bTestOpenNoError, pszCharset);
else if (STARTS_WITH_CI(pszAccess, "w"))
return Open(pszFname, TABWrite, bTestOpenNoError, pszCharset);
else
{
CPLError(CE_Failure, CPLE_FileIO,
"Open() failed: access mode \"%s\" not supported", pszAccess);
return -1;
}
}
/**********************************************************************
* IMapInfoFile::SmartOpen()
*
* Use this static method to automatically open any flavor of MapInfo
* dataset. This method will detect the file type, create an object
* of the right type, and open the file.
*
* Call GetFileClass() on the returned object if you need to find out
* its exact type. (To access format-specific methods for instance)
*
* Returns the new object ptr. , or NULL if the open failed.
**********************************************************************/
IMapInfoFile *IMapInfoFile::SmartOpen(GDALDataset *poDS, const char *pszFname,
GBool bUpdate,
GBool bTestOpenNoError /*=FALSE*/)
{
IMapInfoFile *poFile = nullptr;
int nLen = 0;
if (pszFname)
nLen = static_cast<int>(strlen(pszFname));
if (nLen > 4 && (EQUAL(pszFname + nLen - 4, ".MIF") ||
EQUAL(pszFname + nLen - 4, ".MID")))
{
/*-------------------------------------------------------------
* MIF/MID file
*------------------------------------------------------------*/
poFile = new MIFFile(poDS);
}
else if (nLen > 4 && EQUAL(pszFname + nLen - 4, ".TAB"))
{
/*-------------------------------------------------------------
* .TAB file ... is it a TABFileView or a TABFile?
* We have to read the .tab header to find out.
*------------------------------------------------------------*/
char *pszAdjFname = CPLStrdup(pszFname);
GBool bFoundFields = FALSE;
GBool bFoundView = FALSE;
GBool bFoundSeamless = FALSE;
TABAdjustFilenameExtension(pszAdjFname);
VSILFILE *fp = VSIFOpenL(pszAdjFname, "r");
const char *pszLine = nullptr;
while (fp && (pszLine = CPLReadLineL(fp)) != nullptr)
{
while (isspace(static_cast<unsigned char>(*pszLine)))
pszLine++;
if (STARTS_WITH_CI(pszLine, "Fields"))
bFoundFields = TRUE;
else if (STARTS_WITH_CI(pszLine, "create view"))
bFoundView = TRUE;
else if (STARTS_WITH_CI(pszLine, "\"\\IsSeamless\" = \"TRUE\""))
bFoundSeamless = TRUE;
}
if (bFoundView)
poFile = new TABView(poDS);
else if (bFoundFields && bFoundSeamless)
poFile = new TABSeamless(poDS);
else if (bFoundFields)
poFile = new TABFile(poDS);
if (fp)
VSIFCloseL(fp);
CPLFree(pszAdjFname);
}
/*-----------------------------------------------------------------
* Perform the open() call
*----------------------------------------------------------------*/
if (poFile && poFile->Open(pszFname, bUpdate ? TABReadWrite : TABRead,
bTestOpenNoError) != 0)
{
delete poFile;
poFile = nullptr;
}
if (!bTestOpenNoError && poFile == nullptr)
{
CPLError(CE_Failure, CPLE_FileIO,
"%s could not be opened as a MapInfo dataset.", pszFname);
}
return poFile;
}
/**********************************************************************
* IMapInfoFile::GetNextFeature()
*
* Standard OGR GetNextFeature implementation. This method is used
* to retrieve the next OGRFeature.
**********************************************************************/
OGRFeature *IMapInfoFile::GetNextFeature()
{
GIntBig nFeatureId = 0;
while ((nFeatureId = GetNextFeatureId(m_nCurFeatureId)) != -1)
{
OGRGeometry *poGeom = nullptr;
OGRFeature *poFeatureRef = GetFeatureRef(nFeatureId);
if (poFeatureRef == nullptr)
return nullptr;
else if ((m_poFilterGeom == nullptr ||
((poGeom = poFeatureRef->GetGeometryRef()) != nullptr &&
FilterGeometry(poGeom))) &&
(m_poAttrQuery == nullptr ||
m_poAttrQuery->Evaluate(poFeatureRef)))
{
// Avoid cloning feature... return the copy owned by the class
CPLAssert(poFeatureRef == m_poCurFeature);
m_poCurFeature = nullptr;
if (poFeatureRef->GetGeometryRef() != nullptr)
poFeatureRef->GetGeometryRef()->assignSpatialReference(
GetSpatialRef());
return poFeatureRef;
}
}
return nullptr;
}
/**********************************************************************
* IMapInfoFile::CreateTABFeature()
*
* Instantiate a TABFeature* from a OGRFeature* (or NULL on error)
**********************************************************************/
TABFeature *IMapInfoFile::CreateTABFeature(OGRFeature *poFeature)
{
TABFeature *poTABFeature = nullptr;
OGRwkbGeometryType eGType;
TABPoint *poTABPointFeature = nullptr;
TABRegion *poTABRegionFeature = nullptr;
TABPolyline *poTABPolylineFeature = nullptr;
/*-----------------------------------------------------------------
* MITAB won't accept new features unless they are in a type derived
* from TABFeature... so we have to do our best to map to the right
* feature type based on the geometry type.
*----------------------------------------------------------------*/
OGRGeometry *poGeom = poFeature->GetGeometryRef();
if (poGeom != nullptr)
eGType = poGeom->getGeometryType();
else
eGType = wkbNone;
switch (wkbFlatten(eGType))
{
/*-------------------------------------------------------------
* POINT
*------------------------------------------------------------*/
case wkbPoint:
{
const char *pszStyleString = poFeature->GetStyleString();
if (pszStyleString)
{
if (strstr(pszStyleString, "LABEL("))
{
auto poText = new TABText(poFeature->GetDefnRef());
poText->SetLabelFromStyleString(pszStyleString);
poTABFeature = poText;
if (strstr(pszStyleString, "SYMBOL("))
{
CPLError(CE_Warning, CPLE_AppDefined,
"OGR style string contains both "
"Label and Symbol parts. "
"Only Label taken into account due to "
"MapInfo TAB/MIF format limitations.");
}
}
else
{
TABFeatureClass featureClass =
ITABFeatureSymbol::GetSymbolFeatureClass(
pszStyleString);
if (featureClass == TABFCFontPoint)
{
poTABFeature =
new TABFontPoint(poFeature->GetDefnRef());
}
else if (featureClass == TABFCCustomPoint)
{
poTABFeature =
new TABCustomPoint(poFeature->GetDefnRef());
}
else
{
poTABFeature = new TABPoint(poFeature->GetDefnRef());
}
poTABPointFeature =
cpl::down_cast<TABPoint *>(poTABFeature);
poTABPointFeature->SetSymbolFromStyleString(
poFeature->GetStyleString());
}
}
else
{
poTABFeature = new TABPoint(poFeature->GetDefnRef());
}
break;
}
/*-------------------------------------------------------------
* REGION
*------------------------------------------------------------*/
case wkbPolygon:
case wkbMultiPolygon:
{
poTABFeature = new TABRegion(poFeature->GetDefnRef());
if (poFeature->GetStyleString())
{
poTABRegionFeature = cpl::down_cast<TABRegion *>(poTABFeature);
poTABRegionFeature->SetPenFromStyleString(
poFeature->GetStyleString());
poTABRegionFeature->SetBrushFromStyleString(
poFeature->GetStyleString());
}
break;
}
/*-------------------------------------------------------------
* LINE/PLINE/MULTIPLINE
*------------------------------------------------------------*/
case wkbLineString:
case wkbMultiLineString:
{
poTABFeature = new TABPolyline(poFeature->GetDefnRef());
if (poFeature->GetStyleString())
{
poTABPolylineFeature =
cpl::down_cast<TABPolyline *>(poTABFeature);
poTABPolylineFeature->SetPenFromStyleString(
poFeature->GetStyleString());
}
break;
}
/*-------------------------------------------------------------
* Collection types that are not directly supported... convert
* to multiple features in output file through recursive calls.
*------------------------------------------------------------*/
case wkbGeometryCollection:
case wkbMultiPoint:
{
OGRErr eStatus = OGRERR_NONE;
assert(poGeom); // for clang static analyzer
OGRGeometryCollection *poColl = poGeom->toGeometryCollection();
auto poTmpFeature = std::unique_ptr<OGRFeature>(poFeature->Clone());
for (int i = 0; eStatus == OGRERR_NONE && poColl != nullptr &&
i < poColl->getNumGeometries();
i++)
{
poTmpFeature->SetFID(OGRNullFID);
poTmpFeature->SetGeometry(poColl->getGeometryRef(i));
eStatus = ICreateFeature(poTmpFeature.get());
}
break;
}
/*-------------------------------------------------------------
* Unsupported type.... convert to MapInfo geometry NONE
*------------------------------------------------------------*/
default:
{
poTABFeature = new TABFeature(poFeature->GetDefnRef());
break;
}
}
if (poTABFeature)
{
if (poGeom != nullptr)
poTABFeature->SetGeometryDirectly(poGeom->clone());
for (int i = 0; i < poFeature->GetDefnRef()->GetFieldCount(); i++)
{
poTABFeature->SetField(i, poFeature->GetRawFieldRef(i));
}
poTABFeature->SetFID(poFeature->GetFID());
}
return poTABFeature;
}
/**********************************************************************
* IMapInfoFile::ICreateFeature()
*
* Standard OGR CreateFeature implementation. This method is used
* to create a new feature in current dataset
**********************************************************************/
OGRErr IMapInfoFile::ICreateFeature(OGRFeature *poFeature)
{
TABFeature *poTABFeature = CreateTABFeature(poFeature);
if (poTABFeature == nullptr) /* MultiGeometry */
return OGRERR_NONE;
OGRErr eErr = CreateFeature(poTABFeature);
if (eErr == OGRERR_NONE)
poFeature->SetFID(poTABFeature->GetFID());
delete poTABFeature;
return eErr;
}
/**********************************************************************
* IMapInfoFile::GetFeature()
*
* Standard OGR GetFeature implementation. This method is used
* to get the wanted (nFeatureId) feature, a NULL value will be
* returned on error.
**********************************************************************/
OGRFeature *IMapInfoFile::GetFeature(GIntBig nFeatureId)
{
/*fprintf(stderr, "GetFeature(%ld)\n", nFeatureId);*/
OGRFeature *poFeatureRef = GetFeatureRef(nFeatureId);
if (poFeatureRef)
{
// Avoid cloning feature... return the copy owned by the class
CPLAssert(poFeatureRef == m_poCurFeature);
m_poCurFeature = nullptr;
return poFeatureRef;
}
else
return nullptr;
}
/************************************************************************/
/* GetTABType() */
/* */
/* Create a native field based on a generic OGR definition. */
/************************************************************************/
int IMapInfoFile::GetTABType(const OGRFieldDefn *poField,
TABFieldType *peTABType, int *pnWidth,
int *pnPrecision)
{
TABFieldType eTABType;
int nWidth = poField->GetWidth();
int nPrecision =
poField->GetType() == OFTReal ? poField->GetPrecision() : 0;
if (poField->GetType() == OFTInteger)
{
if (poField->GetSubType() == OFSTBoolean)
{
eTABType = TABFLogical;
nWidth = 1;
}
else
{
eTABType = TABFInteger;
if (nWidth == 0)
nWidth = 12;
}
}
else if (poField->GetType() == OFTInteger64)
{
eTABType = TABFLargeInt;
if (nWidth == 0)
nWidth = 20;
}
else if (poField->GetType() == OFTReal)
{
if (nWidth == 0 && poField->GetPrecision() == 0)
{
eTABType = TABFFloat;
nWidth = 32;
}
else
{
eTABType = TABFDecimal;
// Enforce Mapinfo limits, otherwise MapInfo will crash (#6392)
if (nWidth > 20 || nWidth - nPrecision < 2 || nPrecision > 16)
{
if (nWidth > 20)
nWidth = 20;
if (nWidth - nPrecision < 2)
nPrecision = nWidth - 2;
if (nPrecision > 16)
nPrecision = 16;
CPLDebug("MITAB",
"Adjusting initial width,precision of %s from %d,%d "
"to %d,%d",
poField->GetNameRef(), poField->GetWidth(),
poField->GetPrecision(), nWidth, nPrecision);
}
}
}
else if (poField->GetType() == OFTDate)
{
eTABType = TABFDate;
if (nWidth == 0)
nWidth = 10;
}
else if (poField->GetType() == OFTTime)
{
eTABType = TABFTime;
if (nWidth == 0)
nWidth = 9;
}
else if (poField->GetType() == OFTDateTime)
{
eTABType = TABFDateTime;
if (nWidth == 0)
nWidth = 19;
}
else if (poField->GetType() == OFTString)
{
eTABType = TABFChar;
if (nWidth == 0)
nWidth = 254;
else
nWidth = std::min(254, nWidth);
}
else
{
CPLError(CE_Failure, CPLE_AppDefined,
"IMapInfoFile::CreateField() called with unsupported field"
" type %d.\n"
"Note that Mapinfo files don't support list field types.\n",
poField->GetType());
return -1;
}
if (peTABType)
*peTABType = eTABType;
if (pnWidth)
*pnWidth = nWidth;
if (pnPrecision)
*pnPrecision = nPrecision;
return 0;
}
/************************************************************************/
/* CreateField() */
/* */
/* Create a native field based on a generic OGR definition. */
/************************************************************************/
OGRErr IMapInfoFile::CreateField(const OGRFieldDefn *poField, int bApproxOK)
{
TABFieldType eTABType;
int nWidth = 0;
int nPrecision = 0;
if (GetTABType(poField, &eTABType, &nWidth, &nPrecision) < 0)
return OGRERR_FAILURE;
if (AddFieldNative(poField->GetNameRef(), eTABType, nWidth, nPrecision,
FALSE, FALSE, bApproxOK) > -1)
return OGRERR_NONE;
return OGRERR_FAILURE;
}
/**********************************************************************
* IMapInfoFile::SetCharset()
*
* Set the charset for the tab header.
*
*
* Returns 0 on success, -1 on error.
**********************************************************************/
int IMapInfoFile::SetCharset(const char *pszCharset)
{
if (pszCharset && strlen(pszCharset) > 0)
{
if (pszCharset == m_pszCharset)
{
return 0;
}
CPLFree(m_pszCharset);
m_pszCharset = CPLStrdup(pszCharset);
return 0;
}
return -1;
}
const char *IMapInfoFile::GetCharset() const
{
return m_pszCharset;
}
// Table is adopted from
// http://www.i-signum.com/Formation/download/MB_ReferenceGuide.pdf pp. 127-128
// NOTE: if modifying this table, please keep doc/source/drivers/vector/mapinfo_encodings.csv in sync
static const char *const apszCharsets[][2] = {
{"Neutral", ""}, // No character conversions performed.
{"ISO8859_1", "ISO-8859-1"}, // ISO 8859-1 (UNIX)
{"ISO8859_2", "ISO-8859-2"}, // ISO 8859-2 (UNIX)
{"ISO8859_3", "ISO-8859-3"}, // ISO 8859-3 (UNIX)
{"ISO8859_4", "ISO-8859-4"}, // ISO 8859-4 (UNIX)
{"ISO8859_5", "ISO-8859-5"}, // ISO 8859-5 (UNIX)
{"ISO8859_6", "ISO-8859-6"}, // ISO 8859-6 (UNIX)
{"ISO8859_7", "ISO-8859-7"}, // ISO 8859-7 (UNIX)
{"ISO8859_8", "ISO-8859-8"}, // ISO 8859-8 (UNIX)
{"ISO8859_9", "ISO-8859-9"}, // ISO 8859-9 (UNIX)
{"PackedEUCJapaese", "EUC-JP"}, // UNIX, standard Japanese implementation.
{"WindowsLatin1", "CP1252"},
{"WindowsLatin2", "CP1250"},
{"WindowsArabic", "CP1256"},
{"WindowsCyrillic", "CP1251"},
{"WindowsBalticRim", "CP1257"},
{"WindowsGreek", "CP1253"},
{"WindowsHebrew", "CP1255"},
{"WindowsTurkish", "CP1254"}, // Windows Eastern Europe
{"WindowsTradChinese", "CP950"}, // Windows Traditional Chinese
{"WindowsSimpChinese", "CP936"}, // Windows Simplified Chinese
{"WindowsJapanese", "CP932"},
{"WindowsKorean", "CP949"},
{"CodePage437", "CP437"}, // DOS Code Page 437 = IBM Extended ASCII
{"CodePage850", "CP850"}, // DOS Code Page 850 = Multilingual
{"CodePage852", "CP852"}, // DOS Code Page 852 = Eastern Europe
{"CodePage855", "CP855"}, // DOS Code Page 855 = Cyrillic
{"CodePage857", "CP857"},
{"CodePage860", "CP860"}, // DOS Code Page 860 = Portuguese
{"CodePage861", "CP861"}, // DOS Code Page 861 = Icelandic
{"CodePage863", "CP863"}, // DOS Code Page 863 = French Canadian
{"CodePage864", "CP864"}, // DOS Code Page 864 = Arabic
{"CodePage865", "CP865"}, // DOS Code Page 865 = Nordic
{"CodePage869", "CP869"}, // DOS Code Page 869 = Modern Greek
{"LICS", ""}, // Lotus worksheet release 1,2 character set
{"LMBCS", ""}, // Lotus worksheet release 3,4 character set
{"UTF-8", "UTF-8"},
{nullptr, nullptr}};
const char *IMapInfoFile::CharsetToEncoding(const char *pszCharset)
{
if (pszCharset == nullptr)
{
return apszCharsets[0][1];
}
for (size_t i = 0; apszCharsets[i][0] != nullptr; ++i)
{
if (EQUAL(pszCharset, apszCharsets[i][0]))
{
return apszCharsets[i][1];
}
}
CPLError(CE_Warning, CPLE_NotSupported,
"Cannot find iconv encoding corresponding to MapInfo %s charset",
pszCharset);
return apszCharsets[0][1];
}
const char *IMapInfoFile::EncodingToCharset(const char *pszEncoding)
{
if (pszEncoding == nullptr)
{
return apszCharsets[0][0];
}
for (size_t i = 0; apszCharsets[i][1] != nullptr; ++i)
{
if (EQUAL(pszEncoding, apszCharsets[i][1]))
{
return apszCharsets[i][0];
}
}
CPLError(CE_Warning, CPLE_NotSupported,
"Cannot find MapInfo charset corresponding to iconv %s encoding",
pszEncoding);
return apszCharsets[0][0];
}
const char *IMapInfoFile::GetEncoding() const
{
return CharsetToEncoding(GetCharset());
}
void IMapInfoFile::SetEncoding(const char *pszEncoding)
{
SetCharset(EncodingToCharset(pszEncoding));
}
void IMapInfoFile::SetStrictLaundering(bool bStrictLaundering)
{
m_bStrictLaundering = bStrictLaundering;
}
int IMapInfoFile::TestUtf8Capability() const
{
const char *pszEncoding(GetEncoding());
if (strlen(pszEncoding) == 0)
{
return FALSE;
}
return CPLCanRecode("test", GetEncoding(), CPL_ENC_UTF8);
}
CPLString IMapInfoFile::NormalizeFieldName(const char *pszName) const
{
CPLString osName(pszName);
if (strlen(GetEncoding()) > 0)
osName.Recode(CPL_ENC_UTF8, GetEncoding());
char szNewFieldName[31 + 1]; /* 31 is the max characters for a field name*/
unsigned int nRenameNum = 1;
strncpy(szNewFieldName, osName.c_str(), sizeof(szNewFieldName) - 1);
szNewFieldName[sizeof(szNewFieldName) - 1] = '\0';
while (m_oSetFields.find(CPLString(szNewFieldName).toupper()) !=
m_oSetFields.end() &&
nRenameNum < 10)
{
CPLsnprintf(szNewFieldName, sizeof(szNewFieldName), "%.29s_%.1u",
osName.c_str(), nRenameNum);
nRenameNum++;
}
while (m_oSetFields.find(CPLString(szNewFieldName).toupper()) !=
m_oSetFields.end() &&
nRenameNum < 100)
{
CPLsnprintf(szNewFieldName, sizeof(szNewFieldName), "%.29s%.2u",
osName.c_str(), nRenameNum);
nRenameNum++;
}
if (m_oSetFields.find(CPLString(szNewFieldName).toupper()) !=
m_oSetFields.end())
{
CPLError(CE_Failure, CPLE_NotSupported,
"Too many field names like '%s' when truncated to 31 letters "
"for MapInfo format.",
pszName);
}
CPLString osNewFieldName(szNewFieldName);
if (strlen(GetEncoding()) > 0)
osNewFieldName.Recode(GetEncoding(), CPL_ENC_UTF8);
if (!EQUAL(pszName, osNewFieldName.c_str()))
{
CPLError(CE_Warning, CPLE_NotSupported,
"Normalized/laundered field name: '%s' to '%s'", pszName,
osNewFieldName.c_str());
}
return osNewFieldName;
}