From d510af9a152c641cfb2efb9b4da7428d972f1c56 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 19 May 2014 22:38:55 +0200 Subject: [PATCH] Support opening .mif without .mid (http://trac.osgeo.org/gdal/ticket/5141) --- mitab/mitab_miffile.cpp | 95 +++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/mitab/mitab_miffile.cpp b/mitab/mitab_miffile.cpp index 9c0898c..d5912e2 100644 --- a/mitab/mitab_miffile.cpp +++ b/mitab/mitab_miffile.cpp @@ -270,7 +270,7 @@ int MIFFile::Open(const char *pszFname, const char *pszAccess, CPLErrorReset(); - if (m_poMIDFile) + if (m_poMIFFile) { CPLError(CE_Failure, CPLE_FileIO, "Open() failed: object already contains an open file"); @@ -362,53 +362,57 @@ int MIFFile::Open(const char *pszFname, const char *pszAccess, } /*----------------------------------------------------------------- - * Open .MID file + * Read MIF File Header *----------------------------------------------------------------*/ - if (nFnameLen > 4 && strcmp(pszTmpFname+nFnameLen-4, ".MIF")==0) - strcpy(pszTmpFname+nFnameLen-4, ".MID"); - else - strcpy(pszTmpFname+nFnameLen-4, ".mid"); - -#ifndef _WIN32 - TABAdjustFilenameExtension(pszTmpFname); -#endif - - m_poMIDFile = new MIDDATAFile; - - if (m_poMIDFile->Open(pszTmpFname, pszAccess) !=0) + if (m_eAccessMode == TABRead && ParseMIFHeader() != 0) { + Close(); + if (!bTestOpenNoError) CPLError(CE_Failure, CPLE_NotSupported, - "Unable to open %s.", pszTmpFname); + "Failed parsing header in %s.", m_pszFname); else CPLErrorReset(); CPLFree(pszTmpFname); - Close(); return -1; } + if ( m_nAttribut > 0 || m_eAccessMode == TABWrite ) + { + /*----------------------------------------------------------------- + * Open .MID file + *----------------------------------------------------------------*/ + if (nFnameLen > 4 && strcmp(pszTmpFname+nFnameLen-4, ".MIF")==0) + strcpy(pszTmpFname+nFnameLen-4, ".MID"); + else + strcpy(pszTmpFname+nFnameLen-4, ".mid"); - CPLFree(pszTmpFname); - pszTmpFname = NULL; +#ifndef _WIN32 + TABAdjustFilenameExtension(pszTmpFname); +#endif - /*----------------------------------------------------------------- - * Read MIF File Header - *----------------------------------------------------------------*/ - if (m_eAccessMode == TABRead && ParseMIFHeader() != 0) - { - Close(); + m_poMIDFile = new MIDDATAFile; - if (!bTestOpenNoError) - CPLError(CE_Failure, CPLE_NotSupported, - "Failed parsing header in %s.", m_pszFname); - else - CPLErrorReset(); + if (m_poMIDFile->Open(pszTmpFname, pszAccess) !=0) + { + if (!bTestOpenNoError) + CPLError(CE_Failure, CPLE_NotSupported, + "Unable to open %s.", pszTmpFname); + else + CPLErrorReset(); - return -1; + CPLFree(pszTmpFname); + Close(); + + return -1; + } } + CPLFree(pszTmpFname); + pszTmpFname = NULL; + /*----------------------------------------------------------------- * In write access, set some defaults *----------------------------------------------------------------*/ @@ -419,7 +423,7 @@ int MIFFile::Open(const char *pszFname, const char *pszAccess, } /* Put the MID file at the correct location, on the first feature */ - if (m_eAccessMode == TABRead && (m_poMIDFile->GetLine() == NULL)) + if (m_eAccessMode == TABRead && (m_poMIDFile != NULL && m_poMIDFile->GetLine() == NULL)) { Close(); @@ -431,10 +435,12 @@ int MIFFile::Open(const char *pszFname, const char *pszAccess, m_poMIFFile->SetTranslation(m_dfXMultiplier,m_dfYMultiplier, m_dfXDisplacement, m_dfYDisplacement); - m_poMIDFile->SetTranslation(m_dfXMultiplier,m_dfYMultiplier, - m_dfXDisplacement, m_dfYDisplacement); + if( m_poMIDFile != NULL ) + m_poMIDFile->SetTranslation(m_dfXMultiplier,m_dfYMultiplier, + m_dfXDisplacement, m_dfYDisplacement); m_poMIFFile->SetDelimiter(m_pszDelimiter); - m_poMIDFile->SetDelimiter(m_pszDelimiter); + if( m_poMIDFile != NULL ) + m_poMIDFile->SetDelimiter(m_pszDelimiter); /*------------------------------------------------------------- * Set geometry type if the geometry objects are uniform. @@ -839,8 +845,11 @@ void MIFFile::ResetReading() break; } - m_poMIDFile->Rewind(); - m_poMIDFile->GetLine(); + if( m_poMIDFile != NULL ) + { + m_poMIDFile->Rewind(); + m_poMIDFile->GetLine(); + } // We're positioned on first feature. Feature Ids start at 1. if (m_poCurFeature) @@ -965,8 +974,11 @@ void MIFFile::PreParseFile() break; } - m_poMIDFile->Rewind(); - m_poMIDFile->GetLine(); + if( m_poMIDFile != NULL ) + { + m_poMIDFile->Rewind(); + m_poMIDFile->GetLine(); + } m_bPreParsed = TRUE; @@ -1267,7 +1279,8 @@ GBool MIFFile::NextFeature() { if (m_poMIFFile->IsValidFeature(pszLine)) { - m_poMIDFile->GetLine(); + if( m_poMIDFile != NULL ) + m_poMIDFile->GetLine(); m_nPreloadedId++; return TRUE; } @@ -1304,7 +1317,7 @@ TABFeature *MIFFile::GetFeatureRef(int nFeatureId) * Make sure file is opened and Validate feature id by positioning * the read pointers for the .MAP and .DAT files to this feature id. *----------------------------------------------------------------*/ - if (m_poMIDFile == NULL) + if (m_poMIFFile == NULL) { CPLError(CE_Failure, CPLE_IllegalArg, "GetFeatureRef() failed: file is not opened!"); @@ -1444,7 +1457,7 @@ TABFeature *MIFFile::GetFeatureRef(int nFeatureId) * Read fields from the .DAT file * GetRecordBlock() has already been called above... *----------------------------------------------------------------*/ - if (m_poCurFeature->ReadRecordFromMIDFile(m_poMIDFile) != 0) + if (m_poMIDFile != NULL && m_poCurFeature->ReadRecordFromMIDFile(m_poMIDFile) != 0) { CPLError(CE_Failure, CPLE_NotSupported, "Error during reading Record.");