Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support opening .mif without .mid (http://trac.osgeo.org/gdal/ticket/5141) #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 54 additions & 41 deletions mitab/mitab_miffile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
*----------------------------------------------------------------*/
Expand All @@ -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();

Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -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.");
Expand Down