Exemplo n.º 1
0
/**********************************************************************
 *                   TABRawBinBlock::CommitAsDeleted()
 *
 * Commit current block to file using block type 4 (garbage block)
 *
 * Returns 0 if successful or -1 if an error happened, in which case
 * CPLError() will have been called.
 **********************************************************************/
int     TABRawBinBlock::CommitAsDeleted(GInt32 nNextBlockPtr)
{
    CPLErrorReset();

    if ( m_pabyBuf == nullptr )
    {
        CPLError(CE_Failure, CPLE_AssertionFailed,
                 "CommitAsDeleted(): Block has not been initialized yet!");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Create deleted block header
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x000);
    WriteInt16(TABMAP_GARB_BLOCK);    // Block type code
    WriteInt32(nNextBlockPtr);

    int nStatus = CPLGetLastErrorType() == CE_Failure ? -1 : 0;

    /*-----------------------------------------------------------------
     * OK, call the base class to write the block to disk.
     *----------------------------------------------------------------*/
    if (nStatus == 0)
    {
#ifdef DEBUG_VERBOSE
        CPLDebug("MITAB", "Committing GARBAGE block to offset %d", m_nFileOffset);
#endif
        nStatus = TABRawBinBlock::CommitToFile();
        m_nSizeUsed = 0;
    }

    return nStatus;
}
Exemplo n.º 2
0
/**********************************************************************
 *                   TABMAPToolBlock::InitNewBlock()
 *
 * Initialize a newly created block so that it knows to which file it
 * is attached, its block size, etc . and then perform any specific
 * initialization for this block type, including writing a default
 * block header, etc. and leave the block ready to receive data.
 *
 * This is an alternative to calling ReadFromFile() or InitBlockFromData()
 * that puts the block in a stable state without loading any initial
 * data in it.
 *
 * Returns 0 if successful or -1 if an error happened, in which case
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPToolBlock::InitNewBlock(VSILFILE *fpSrc, int nBlockSize,
                                        int nFileOffset /* = 0*/)
{
#ifdef DEBUG_VERBOSE
    CPLDebug( "MITAB",
              "Instantiating new TOOL block at offset %d", nFileOffset);
#endif

    /*-----------------------------------------------------------------
     * Start with the default initialization
     *----------------------------------------------------------------*/
    if ( TABRawBinBlock::InitNewBlock(fpSrc, nBlockSize, nFileOffset) != 0)
        return -1;

    /*-----------------------------------------------------------------
     * And then set default values for the block header.
     *----------------------------------------------------------------*/
    m_nNextToolBlock = 0;

    m_numDataBytes = 0;

    GotoByteInBlock(0x000);

    if (m_eAccess != TABRead)
    {
        WriteInt16(TABMAP_TOOL_BLOCK); // Block type code
        WriteInt16(0);                 // num. bytes used, excluding header
        WriteInt32(0);                 // Pointer to next tool block
    }

    if (CPLGetLastErrorNo() != 0)
        return -1;

    return 0;
}
/**********************************************************************
 *                   TABRawBinBlock::CommitAsDeleted()
 *
 * Commit current block to file using block type 4 (garbage block)
 *
 * Returns 0 if succesful or -1 if an error happened, in which case 
 * CPLError() will have been called.
 **********************************************************************/
int     TABRawBinBlock::CommitAsDeleted(GInt32 nNextBlockPtr)
{
    int nStatus = 0;

    CPLErrorReset();

    if ( m_pabyBuf == NULL )
    {
        CPLError(CE_Failure, CPLE_AssertionFailed, 
                 "CommitAsDeleted(): Block has not been initialized yet!");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Create deleted block header
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x000);
    WriteInt32(nNextBlockPtr);

    if( CPLGetLastErrorType() == CE_Failure )
        nStatus = CPLGetLastErrorNo();

    /*-----------------------------------------------------------------
     * OK, call the base class to write the block to disk.
     *----------------------------------------------------------------*/
    if (nStatus == 0)
        nStatus = TABRawBinBlock::CommitToFile();

    return nStatus;
}
Exemplo n.º 4
0
/**********************************************************************
 *                   TABMAPIndexBlock::InitNewBlock()
 *
 * Initialize a newly created block so that it knows to which file it
 * is attached, its block size, etc . and then perform any specific 
 * initialization for this block type, including writing a default 
 * block header, etc. and leave the block ready to receive data.
 *
 * This is an alternative to calling ReadFromFile() or InitBlockFromData()
 * that puts the block in a stable state without loading any initial
 * data in it.
 *
 * Returns 0 if succesful or -1 if an error happened, in which case 
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPIndexBlock::InitNewBlock(FILE *fpSrc, int nBlockSize, 
                                        int nFileOffset /* = 0*/)
{
    /*-----------------------------------------------------------------
     * Start with the default initialisation
     *----------------------------------------------------------------*/
    if ( TABRawBinBlock::InitNewBlock(fpSrc, nBlockSize, nFileOffset) != 0)
        return -1;

    /*-----------------------------------------------------------------
     * And then set default values for the block header.
     *----------------------------------------------------------------*/
    m_numEntries = 0;

    m_nMinX = 1000000000;
    m_nMinY = 1000000000;
    m_nMaxX = -1000000000;
    m_nMaxY = -1000000000;

    if (m_eAccess != TABRead)
    {
        GotoByteInBlock(0x000);

        WriteInt16(TABMAP_INDEX_BLOCK);     // Block type code
        WriteInt16(0);                      // num. index entries
    }

    if (CPLGetLastErrorNo() != 0)
        return -1;

    return 0;
}
Exemplo n.º 5
0
/**********************************************************************
 *                   TABMAPToolBlock::InitBlockFromData()
 *
 * Perform some initialization on the block after its binary data has
 * been set or changed (or loaded from a file).
 *
 * Returns 0 if succesful or -1 if an error happened, in which case 
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPToolBlock::InitBlockFromData(GByte *pabyBuf,
                                           int nBlockSize, int nSizeUsed, 
                                           GBool bMakeCopy /* = TRUE */,
                                           VSILFILE *fpSrc /* = NULL */, 
                                           int nOffset /* = 0 */)
{
    int nStatus;

    /*-----------------------------------------------------------------
     * First of all, we must call the base class' InitBlockFromData()
     *----------------------------------------------------------------*/
    nStatus = TABRawBinBlock::InitBlockFromData(pabyBuf, nBlockSize, nSizeUsed,
                                                bMakeCopy, fpSrc, nOffset);
    if (nStatus != 0)   
        return nStatus;

    /*-----------------------------------------------------------------
     * Validate block type
     *----------------------------------------------------------------*/
    if (m_nBlockType != TABMAP_TOOL_BLOCK)
    {
        CPLError(CE_Failure, CPLE_FileIO,
                 "InitBlockFromData(): Invalid Block Type: got %d expected %d",
                 m_nBlockType, TABMAP_TOOL_BLOCK);
        CPLFree(m_pabyBuf);
        m_pabyBuf = NULL;
        return -1;
    }

    /*-----------------------------------------------------------------
     * Init member variables
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x002);
    m_numDataBytes = ReadInt16();       /* Excluding 8 bytes header */

    m_nNextToolBlock = ReadInt32();

    /*-----------------------------------------------------------------
     * The read ptr is now located at the beginning of the data part.
     *----------------------------------------------------------------*/
    GotoByteInBlock(MAP_TOOL_HEADER_SIZE);

    return 0;
}
Exemplo n.º 6
0
/**********************************************************************
 *                   TABMAPIndexBlock::CommitToFile()
 *
 * Commit the current state of the binary block to the file to which 
 * it has been previously attached.
 *
 * This method makes sure all values are properly set in the map object
 * block header and then calls TABRawBinBlock::CommitToFile() to do
 * the actual writing to disk.
 *
 * Returns 0 if succesful or -1 if an error happened, in which case 
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPIndexBlock::CommitToFile()
{
    int nStatus = 0;

    if ( m_pabyBuf == NULL )
    {
        CPLError(CE_Failure, CPLE_AssertionFailed, 
                 "CommitToFile(): Block has not been initialized yet!");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Commit child first
     *----------------------------------------------------------------*/
    if (m_poCurChild)
    {
        if (m_poCurChild->CommitToFile() != 0)
            return -1;
    }

    /*-----------------------------------------------------------------
     * Make sure 4 bytes block header is up to date.
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x000);

    WriteInt16(TABMAP_INDEX_BLOCK);    // Block type code
    WriteInt16(m_numEntries);

    nStatus = CPLGetLastErrorNo();

    /*-----------------------------------------------------------------
     * Loop through all entries, writing each of them, and calling
     * CommitToFile() (recursively) on any child index entries we may
     * encounter.
     *----------------------------------------------------------------*/
    for(int i=0; nStatus == 0 && i<m_numEntries; i++)
    {
        if (nStatus == 0)
            nStatus = WriteNextEntry(&(m_asEntries[i]));
    }


    /*-----------------------------------------------------------------
     * OK, call the base class to write the block to disk.
     *----------------------------------------------------------------*/
    if (nStatus == 0)
        nStatus = TABRawBinBlock::CommitToFile();

    return nStatus;
}
Exemplo n.º 7
0
/**********************************************************************
 *                   TABMAPIndexBlock::WriteNextEntry()
 *
 * Write the sEntry index entry at current position in the block.
 *
 * Returns 0 if succesful or -1 if we reached the end of the block.
 **********************************************************************/
int     TABMAPIndexBlock::WriteNextEntry(TABMAPIndexEntry *psEntry)
{
    if (m_nCurPos < 4)
        GotoByteInBlock( 0x004 );

    WriteInt32(psEntry->XMin);
    WriteInt32(psEntry->YMin);
    WriteInt32(psEntry->XMax);
    WriteInt32(psEntry->YMax);
    WriteInt32(psEntry->nBlockPtr);

    if (CPLGetLastErrorNo() != 0)
        return -1;

    return 0;
}
Exemplo n.º 8
0
/**********************************************************************
 *                   TABMAPIndexBlock::ReadAllEntries()
 *
 * Init the block by reading all entries from the data block.
 *
 * Returns 0 if succesful or -1 on error.
 **********************************************************************/
int     TABMAPIndexBlock::ReadAllEntries()
{
    CPLAssert(m_numEntries <= TAB_MAX_ENTRIES_INDEX_BLOCK);
    if (m_numEntries == 0)
        return 0;
    
    if (GotoByteInBlock( 0x004 ) != 0)
        return -1;

    for(int i=0; i<m_numEntries; i++)
    {
        if ( ReadNextEntry(&(m_asEntries[i])) != 0)
            return -1;
    }

    return 0;
}
Exemplo n.º 9
0
/**********************************************************************
 *                   TABMAPToolBlock::ReadBytes()
 *
 * Cover function for TABRawBinBlock::ReadBytes() that will automagically
 * load the next coordinate block in the chain before reading the
 * requested bytes if we are at the end of the current block and if
 * m_nNextToolBlock is a valid block.
 *
 * Then the control is passed to TABRawBinBlock::ReadBytes() to finish the
 * work:
 * Copy the number of bytes from the data block's internal buffer to
 * the user's buffer pointed by pabyDstBuf.
 *
 * Passing pabyDstBuf = NULL will only move the read pointer by the
 * specified number of bytes as if the copy had happened... but it
 * won't crash.
 *
 * Returns 0 if successful or -1 if an error happened, in which case
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPToolBlock::ReadBytes(int numBytes, GByte *pabyDstBuf)
{
    if (m_pabyBuf &&
        m_nCurPos >= (m_numDataBytes+MAP_TOOL_HEADER_SIZE) &&
        m_nNextToolBlock > 0)
    {
        int nStatus = GotoByteInFile(m_nNextToolBlock);
        if( nStatus != 0 )
        {
            // Failed.... an error has already been reported.
            return nStatus;
        }

        GotoByteInBlock(MAP_TOOL_HEADER_SIZE);  // Move pointer past header
        m_numBlocksInChain++;
    }

    return TABRawBinBlock::ReadBytes(numBytes, pabyDstBuf);
}
Exemplo n.º 10
0
/**********************************************************************
 *                   TABMAPToolBlock::CommitToFile()
 *
 * Commit the current state of the binary block to the file to which
 * it has been previously attached.
 *
 * This method makes sure all values are properly set in the map object
 * block header and then calls TABRawBinBlock::CommitToFile() to do
 * the actual writing to disk.
 *
 * Returns 0 if successful or -1 if an error happened, in which case
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPToolBlock::CommitToFile()
{
    int nStatus = 0;

    if ( m_pabyBuf == NULL )
    {
        CPLError(CE_Failure, CPLE_AssertionFailed,
                 "CommitToFile(): Block has not been initialized yet!");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Nothing to do here if block has not been modified
     *----------------------------------------------------------------*/
    if (!m_bModified)
        return 0;

    /*-----------------------------------------------------------------
     * Make sure 8 bytes block header is up to date.
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x000);

    WriteInt16(TABMAP_TOOL_BLOCK);    // Block type code
    CPLAssert(m_nSizeUsed >= MAP_TOOL_HEADER_SIZE && m_nSizeUsed < MAP_TOOL_HEADER_SIZE + 32768);
    WriteInt16((GInt16)(m_nSizeUsed - MAP_TOOL_HEADER_SIZE)); // num. bytes used
    WriteInt32(m_nNextToolBlock);

    nStatus = CPLGetLastErrorNo();

    /*-----------------------------------------------------------------
     * OK, call the base class to write the block to disk.
     *----------------------------------------------------------------*/
    if (nStatus == 0)
    {
#ifdef DEBUG_VERBOSE
        CPLDebug("MITAB", "Committing TOOL block to offset %d", m_nFileOffset);
#endif
        nStatus = TABRawBinBlock::CommitToFile();
    }

    return nStatus;
}
Exemplo n.º 11
0
/**********************************************************************
 *                   TABMAPIndexBlock::ReadNextEntry()
 *
 * Read the next index entry from the block and fill the sEntry
 * structure. 
 *
 * Returns 0 if succesful or -1 if we reached the end of the block.
 **********************************************************************/
int     TABMAPIndexBlock::ReadNextEntry(TABMAPIndexEntry *psEntry)
{
    if (m_nCurPos < 4)
        GotoByteInBlock( 0x004 );

    if (m_nCurPos > 4+(20*m_numEntries) )
    {
        // End of BLock
        return -1;
    }

    psEntry->XMin = ReadInt32();
    psEntry->YMin = ReadInt32();
    psEntry->XMax = ReadInt32();
    psEntry->YMax = ReadInt32();
    psEntry->nBlockPtr = ReadInt32();

    if (CPLGetLastErrorNo() != 0)
        return -1;

    return 0;
}
Exemplo n.º 12
0
/**********************************************************************
 *                   TABMAPIndexBlock::InitBlockFromData()
 *
 * Perform some initialization on the block after its binary data has
 * been set or changed (or loaded from a file).
 *
 * Returns 0 if succesful or -1 if an error happened, in which case 
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPIndexBlock::InitBlockFromData(GByte *pabyBuf, int nSize, 
                                            GBool bMakeCopy /* = TRUE */,
                                            FILE *fpSrc /* = NULL */, 
                                            int nOffset /* = 0 */)
{
    int nStatus;

    /*-----------------------------------------------------------------
     * First of all, we must call the base class' InitBlockFromData()
     *----------------------------------------------------------------*/
    nStatus = TABRawBinBlock::InitBlockFromData(pabyBuf, nSize, bMakeCopy,
                                            fpSrc, nOffset);
    if (nStatus != 0)   
        return nStatus;

    /*-----------------------------------------------------------------
     * Validate block type
     *----------------------------------------------------------------*/
    if (m_nBlockType != TABMAP_INDEX_BLOCK)
    {
        CPLError(CE_Failure, CPLE_FileIO,
                 "InitBlockFromData(): Invalid Block Type: got %d expected %d",
                 m_nBlockType, TABMAP_INDEX_BLOCK);
        CPLFree(m_pabyBuf);
        m_pabyBuf = NULL;
        return -1;
    }

    /*-----------------------------------------------------------------
     * Init member variables
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x002);
    m_numEntries = ReadInt16();

    if (m_numEntries > 0)
        ReadAllEntries();

    return 0;
}
Exemplo n.º 13
0
/**********************************************************************
 *                   TABRawBinBlock::GotoByteRel()
 *
 * Move the block pointer by the specified number of bytes relative
 * to its current position.
 *
 * Returns 0 if succesful or -1 if an error happened, in which case 
 * CPLError() will have been called.
 **********************************************************************/
int     TABRawBinBlock::GotoByteRel(int nOffset)
{
    return GotoByteInBlock(m_nCurPos + nOffset);
}
Exemplo n.º 14
0
/**********************************************************************
 *                   TABMAPHeaderBlock::InitNewBlock()
 *
 * Initialize a newly created block so that it knows to which file it
 * is attached, its block size, etc . and then perform any specific 
 * initialization for this block type, including writing a default 
 * block header, etc. and leave the block ready to receive data.
 *
 * This is an alternative to calling ReadFromFile() or InitBlockFromData()
 * that puts the block in a stable state without loading any initial
 * data in it.
 *
 * Returns 0 if succesful or -1 if an error happened, in which case 
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPHeaderBlock::InitNewBlock(FILE *fpSrc, int nBlockSize, 
                                        int nFileOffset /* = 0*/)
{
    int i;
    /*-----------------------------------------------------------------
     * Start with the default initialisation
     *----------------------------------------------------------------*/
    if ( TABRawBinBlock::InitNewBlock(fpSrc, nBlockSize, nFileOffset) != 0)
        return -1;

    /*-----------------------------------------------------------------
     * Set acceptable default values for member vars.
     *----------------------------------------------------------------*/
    m_nMAPVersionNumber = HDR_VERSION_NUMBER;
    m_nBlockSize = HDR_DATA_BLOCK_SIZE;

    m_dCoordsys2DistUnits = 1.0;
    m_nXMin = -1000000000;
    m_nYMin = -1000000000;
    m_nXMax = 1000000000;
    m_nYMax = 1000000000;

    m_nFirstIndexBlock = 0;
    m_nFirstGarbageBlock = 0;
    m_nFirstToolBlock = 0;

    m_numPointObjects = 0;
    m_numLineObjects = 0;
    m_numRegionObjects = 0;
    m_numTextObjects = 0;
    m_nMaxCoordBufSize = 0;

    m_nDistUnitsCode = 7;       // Meters
    m_nMaxSpIndexDepth = 0;
    m_nCoordPrecision = 3;      // ??? 3 digits of precision
    m_nCoordOriginQuadrant = HDR_DEF_ORG_QUADRANT; // ??? N-E quadrant
    m_nReflectXAxisCoord = HDR_DEF_REFLECTXAXIS;
    m_nMaxObjLenArrayId = HDR_OBJ_LEN_ARRAY_SIZE-1;  // See gabyObjLenArray[]
    m_numPenDefs = 0;
    m_numBrushDefs = 0;
    m_numSymbolDefs = 0;
    m_numFontDefs = 0;
    m_numMapToolBlocks = 0;

    m_sProj.nProjId  = 0;
    m_sProj.nEllipsoidId = 0;
    m_sProj.nUnitsId = 7;
    m_XScale = 1000.0;  // Default coord range (before SetCoordSysBounds()) 
    m_YScale = 1000.0;  // will be [-1000000.000 .. 1000000.000]
    m_XDispl = 0.0;
    m_YDispl = 0.0;

    for(i=0; i<6; i++)
        m_sProj.adProjParams[i] = 0.0;

    m_sProj.dDatumShiftX = 0.0;
    m_sProj.dDatumShiftY = 0.0;
    m_sProj.dDatumShiftZ = 0.0;
    for(i=0; i<5; i++)
        m_sProj.adDatumParams[i] = 0.0;

    /*-----------------------------------------------------------------
     * And Set the map object length array in the buffer...
     *----------------------------------------------------------------*/
    if (m_eAccess != TABRead)
    {
        GotoByteInBlock(0x000);
        WriteBytes(HDR_OBJ_LEN_ARRAY_SIZE, gabyObjLenArray);
    }

    if (CPLGetLastErrorNo() != 0)
        return -1;

    return 0;
}
Exemplo n.º 15
0
/**********************************************************************
 *                   TABMAPHeaderBlock::CommitToFile()
 *
 * Commit the current state of the binary block to the file to which 
 * it has been previously attached.
 *
 * This method makes sure all values are properly set in the header
 * block buffer and then calls TABRawBinBlock::CommitToFile() to do
 * the actual writing to disk.
 *
 * Returns 0 if succesful or -1 if an error happened, in which case 
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPHeaderBlock::CommitToFile()
{
    int i, nStatus = 0;

    if ( m_pabyBuf == NULL || m_nBlockSize != HDR_DATA_BLOCK_SIZE )
    {
        CPLError(CE_Failure, CPLE_AssertionFailed, 
        "TABRawBinBlock::CommitToFile(): Block has not been initialized yet!");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Reconstruct header to make sure it is in sync with members variables.
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x000);
    WriteBytes(HDR_OBJ_LEN_ARRAY_SIZE, gabyObjLenArray);
    m_nMaxObjLenArrayId = HDR_OBJ_LEN_ARRAY_SIZE-1;

    GotoByteInBlock(0x100);
    WriteInt32(HDR_MAGIC_COOKIE);
    WriteInt16(m_nMAPVersionNumber);
    WriteInt16(HDR_DATA_BLOCK_SIZE);

    WriteDouble(m_dCoordsys2DistUnits);
    WriteInt32(m_nXMin);
    WriteInt32(m_nYMin);
    WriteInt32(m_nXMax);
    WriteInt32(m_nYMax);

    WriteZeros(16);     // ???

    WriteInt32(m_nFirstIndexBlock);
    WriteInt32(m_nFirstGarbageBlock);
    WriteInt32(m_nFirstToolBlock);

    WriteInt32(m_numPointObjects);
    WriteInt32(m_numLineObjects);
    WriteInt32(m_numRegionObjects);
    WriteInt32(m_numTextObjects);
    WriteInt32(m_nMaxCoordBufSize);

    WriteZeros(14);     // ???

    WriteByte(m_nDistUnitsCode);
    WriteByte(m_nMaxSpIndexDepth);
    WriteByte(m_nCoordPrecision);
    WriteByte(m_nCoordOriginQuadrant);
    WriteByte(m_nReflectXAxisCoord);
    WriteByte(m_nMaxObjLenArrayId);    // See gabyObjLenArray[]
    WriteByte(m_numPenDefs);
    WriteByte(m_numBrushDefs);
    WriteByte(m_numSymbolDefs);
    WriteByte(m_numFontDefs);
    WriteInt16(m_numMapToolBlocks);

    WriteZeros(3);      // ???

    WriteByte(m_sProj.nProjId);
    WriteByte(m_sProj.nEllipsoidId);
    WriteByte(m_sProj.nUnitsId);
    WriteDouble(m_XScale);
    WriteDouble(m_YScale);
    WriteDouble(m_XDispl);
    WriteDouble(m_YDispl);

    for(i=0; i<6; i++)
        WriteDouble(m_sProj.adProjParams[i]);

    WriteDouble(m_sProj.dDatumShiftX);
    WriteDouble(m_sProj.dDatumShiftY);
    WriteDouble(m_sProj.dDatumShiftZ);
    for(i=0; i<5; i++)
        WriteDouble(m_sProj.adDatumParams[i]);

    /*-----------------------------------------------------------------
     * OK, call the base class to write the block to disk.
     *----------------------------------------------------------------*/
    if (nStatus == 0)
        nStatus = TABRawBinBlock::CommitToFile();

    return nStatus;
}
Exemplo n.º 16
0
/**********************************************************************
 *                   TABMAPHeaderBlock::InitBlockFromData()
 *
 * Perform some initialization on the block after its binary data has
 * been set or changed (or loaded from a file).
 *
 * Returns 0 if succesful or -1 if an error happened, in which case 
 * CPLError() will have been called.
 **********************************************************************/
int     TABMAPHeaderBlock::InitBlockFromData(GByte *pabyBuf, int nSize, 
                                         GBool bMakeCopy /* = TRUE */,
                                         FILE *fpSrc /* = NULL */, 
                                         int nOffset /* = 0 */)
{
    int i, nStatus;
    GInt32 nMagicCookie;

    /*-----------------------------------------------------------------
     * First of all, we must call the base class' InitBlockFromData()
     *----------------------------------------------------------------*/
    nStatus = TABRawBinBlock::InitBlockFromData(pabyBuf, nSize, bMakeCopy,
                                            fpSrc, nOffset);
    if (nStatus != 0)   
        return nStatus;

    /*-----------------------------------------------------------------
     * Validate block type
     * Header blocks have a magic cookie at byte 0x100
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x100);
    nMagicCookie = ReadInt32();
    if (nMagicCookie != HDR_MAGIC_COOKIE)
    {
        CPLError(CE_Failure, CPLE_FileIO,
              "ReadFromFile(): Invalid Magic Cookie: got %d expected %d",
                 nMagicCookie, HDR_MAGIC_COOKIE);
        CPLFree(m_pabyBuf);
        m_pabyBuf = NULL;
        return -1;
    }

    /*-----------------------------------------------------------------
     * Init member variables
     * Instead of having over 30 get/set methods, we'll make all data 
     * members public and we will initialize them here.  
     * For this reason, this class should be used with care.
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x104);
    m_nMAPVersionNumber = ReadInt16();
    m_nBlockSize = ReadInt16();

    m_dCoordsys2DistUnits = ReadDouble();
    m_nXMin = ReadInt32();
    m_nYMin = ReadInt32();
    m_nXMax = ReadInt32();
    m_nYMax = ReadInt32();

    GotoByteInBlock(0x130);     // Skip 16 unknown bytes 

    m_nFirstIndexBlock = ReadInt32();
    m_nFirstGarbageBlock = ReadInt32();
    m_nFirstToolBlock = ReadInt32();

    m_numPointObjects = ReadInt32();
    m_numLineObjects = ReadInt32();
    m_numRegionObjects = ReadInt32();
    m_numTextObjects = ReadInt32();
    m_nMaxCoordBufSize = ReadInt32();

    GotoByteInBlock(0x15e);     // Skip 14 unknown bytes

    m_nDistUnitsCode = ReadByte();
    m_nMaxSpIndexDepth = ReadByte();
    m_nCoordPrecision = ReadByte();
    m_nCoordOriginQuadrant = ReadByte();
    m_nReflectXAxisCoord = ReadByte();
    m_nMaxObjLenArrayId = ReadByte();    // See gabyObjLenArray[]
    m_numPenDefs = ReadByte();
    m_numBrushDefs = ReadByte();
    m_numSymbolDefs = ReadByte();
    m_numFontDefs = ReadByte();
    m_numMapToolBlocks = ReadInt16();

    GotoByteInBlock(0x16d);     // Skip 3 unknown bytes

    m_sProj.nProjId  = ReadByte();
    m_sProj.nEllipsoidId = ReadByte();
    m_sProj.nUnitsId = ReadByte();
    m_XScale = ReadDouble();
    m_YScale = ReadDouble();
    m_XDispl = ReadDouble();
    m_YDispl = ReadDouble();

    /* In V.100 files, the scale and displacement do not appear to be set.
     * we'll use m_nCoordPrecision to define the scale factor instead.
     */
    if (m_nMAPVersionNumber <= 100)
    {
        m_XScale = m_YScale = pow(10.0, m_nCoordPrecision);
        m_XDispl = m_YDispl = 0.0;
    }

    for(i=0; i<6; i++)
        m_sProj.adProjParams[i] = ReadDouble();

    m_sProj.dDatumShiftX = ReadDouble();
    m_sProj.dDatumShiftY = ReadDouble();
    m_sProj.dDatumShiftZ = ReadDouble();
    for(i=0; i<5; i++)
    {
        /* In V.200 files, the next 5 datum params are unused and they
         * sometimes contain junk bytes... in this case we set adDatumParams[]
         * to 0 for the rest of the lib to be happy.
         */
        m_sProj.adDatumParams[i] = ReadDouble();
        if (m_nMAPVersionNumber <= 200)
            m_sProj.adDatumParams[i] = 0.0;
    }

    m_sProj.nAffineFlag = 0;
    if (m_nMAPVersionNumber >= 500 && m_nSizeUsed > 512)
    {
        // Read Affine parameters A,B,C,D,E,F 
        // only if version 500+ and block is larger than 512 bytes
        int nInUse = ReadByte();
        if (nInUse)
        {
            m_sProj.nAffineFlag = 1;
            m_sProj.nAffineUnits = ReadByte();
            GotoByteInBlock(0x0208); // Skip unused bytes
            m_sProj.dAffineParamA = ReadDouble();
            m_sProj.dAffineParamB = ReadDouble();
            m_sProj.dAffineParamC = ReadDouble();
            m_sProj.dAffineParamD = ReadDouble();
            m_sProj.dAffineParamE = ReadDouble();
            m_sProj.dAffineParamF = ReadDouble();
        }
    }

    return 0;
}