⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabidfile.cpp

📁 linux下一款GIS程序源码
💻 CPP
字号:
// tabidfile.cpp: implementation of the TABIDFile class.////////////////////////////////////////////////////////////////////////#include "tabidfile.h"#include "ugk_errhandle.h"#include "ugk_memopr.h"#include "ugk_string.h"/********************************************************************** *                   TABIDFile::TABIDFile() * * Constructor. **********************************************************************/TABIDFile::TABIDFile(){    m_fp = NULL;    m_pszFname = NULL;    m_poIDBlock = NULL;    m_nMaxId = -1;}/********************************************************************** *                   TABIDFile::~TABIDFile() * * Destructor. **********************************************************************/TABIDFile::~TABIDFile(){    Close();}/********************************************************************** *                   TABIDFile::Open() * * Open a .ID file, and initialize the structures to be ready to read * objects from it. * * If the filename that is passed in contains a .MAP extension then * the extension will be changed to .ID before trying to open the file. * * Returns 0 on success, -1 on error. **********************************************************************/int TABIDFile::Open(const char *pszFname, const char *pszAccess){    int         nLen;    if (m_fp)    {        UGKError(ET_Failure, UGKErr_FileIO,                 "Open() failed: object already contains an open file");        return -1;    }    /*-----------------------------------------------------------------     * Validate access mode and make sure we use binary access.     *----------------------------------------------------------------*/    if (EQUALN(pszAccess, "r", 1))    {        m_eAccessMode = TABRead;        pszAccess = "rb";    }    else if (EQUALN(pszAccess, "w", 1))    {        m_eAccessMode = TABWrite;        pszAccess = "wb";    }    else    {        UGKError(ET_Failure, UGKErr_FileIO,                 "Open() failed: access mode \"%s\" not supported", pszAccess);        return -1;    }    /*-----------------------------------------------------------------     * Change .MAP extension to .ID if necessary     *----------------------------------------------------------------*/    m_pszFname = UGKStrdup(pszFname); //复制文件名    nLen = strlen(m_pszFname);    if (nLen > 4 && strcmp(m_pszFname+nLen-4, ".MAP")==0)        strcpy(m_pszFname+nLen-4, ".ID");    else if (nLen > 4 && strcmp(m_pszFname+nLen-4, ".map")==0)        strcpy(m_pszFname+nLen-4, ".id");    /*-----------------------------------------------------------------     * Change .MAP extension to .ID if necessary     *----------------------------------------------------------------*/ //暂时注释掉#ifndef WIN32APP    TABAdjustFilenameExtension(m_pszFname);#endif    /*-----------------------------------------------------------------     * Open file     *----------------------------------------------------------------*/    m_fp = fopen(m_pszFname, pszAccess); //打开文件    if (m_fp == NULL)    {        UGKError(ET_Failure, UGKErr_FileIO,                "Open() failed for %s", m_pszFname);        UGK_Free(m_pszFname);        m_pszFname = NULL;        return -1;    }    if (m_eAccessMode == TABRead)    {        /*-------------------------------------------------------------         * READ access:         * Establish the number of object IDs from the size of the file		 * 通过文件属性中文件大小来确定IDs的数目         *------------------------------------------------------------*/        struct stat  sStatBuf;        if ( stat(m_pszFname, &sStatBuf) == -1 )        {            UGKError(ET_Failure, UGKErr_FileIO,                      "stat() failed for %s\n", m_pszFname);            Close();            return -1;        }        m_nMaxId = sStatBuf.st_size/4; 		//如果总共大小比1024小,就全部load,要不就load1024字节        m_nBlockSize = MIN(1024, m_nMaxId*4);          /*-------------------------------------------------------------         * Read the first block from the file         *------------------------------------------------------------*/        m_poIDBlock = new TABRawBinBlock(m_eAccessMode, FALSE);        if (m_nMaxId == 0)        {            // .ID file size = 0 ... just allocate a blank block but            // it won't get really used anyways.            m_nBlockSize = 512;            m_poIDBlock->InitNewBlock(m_fp, m_nBlockSize, 0);        }        else if (m_poIDBlock->ReadFromFile(m_fp, 0, m_nBlockSize) != 0)        {            // UGKError() has already been called.            Close();            return -1;        }    }    else    {        /*-------------------------------------------------------------         * WRITE access:         * Get ready to write to the file         *------------------------------------------------------------*/        m_poIDBlock = new TABRawBinBlock(m_eAccessMode, FALSE);        m_nMaxId = 0;        m_nBlockSize = 1024;        m_poIDBlock->InitNewBlock(m_fp, m_nBlockSize, 0);    }    return 0;}/********************************************************************** *                   TABIDFile::Close() * * Close current file, and release all memory used. * * Returns 0 on success, -1 on error. **********************************************************************/int TABIDFile::Close(){    if (m_fp == NULL)        return 0;    /*----------------------------------------------------------------     * Write access: commit latest changes to the file.     *---------------------------------------------------------------*/    if (m_eAccessMode == TABWrite && m_poIDBlock)    {        m_poIDBlock->CommitToFile();    }        // Delete all structures     delete m_poIDBlock;    m_poIDBlock = NULL;    // Close file    fclose(m_fp);    m_fp = NULL;    UGK_Free(m_pszFname);    m_pszFname = NULL;    return 0;}/********************************************************************** *                   TABIDFile::GetObjPtr() * * Return the offset in the .MAP file where the map object with the * specified id is located. * 返回指定map对象在.MAP文件中位置 * Note that object ids are positive and start at 1. * * An object Id of '0' means that object has no geometry. * * Returns a value >= 0 on success, -1 on error. **********************************************************************/UGKInt32 TABIDFile::GetObjPtr(UGKInt32 nObjId){    if (m_poIDBlock == NULL)        return -1;    if (nObjId < 1 || nObjId > m_nMaxId)    {        UGKError(ET_Failure, UGKErr_IllegalArg,                 "GetObjPtr(): Invalid object ID %d (valid range is [1..%d])",                 nObjId, m_nMaxId);        return -1;    }    if (m_poIDBlock->GotoByteInFile( (nObjId-1)*4 ) != 0)        return -1;    return m_poIDBlock->ReadInt32();}/********************************************************************** *                   TABIDFile::SetObjPtr() * * Set the offset in the .MAP file where the map object with the * specified id is located. * * Note that object ids are positive and start at 1. * * An object Id of '0' means that object has no geometry. * * Returns a value of 0 on success, -1 on error. **********************************************************************/int TABIDFile::SetObjPtr(UGKInt32 nObjId, UGKInt32 nObjPtr){    if (m_poIDBlock == NULL)        return -1;    if (m_eAccessMode != TABWrite)    {        UGKError(ET_Failure, UGKErr_NotSupported,                 "SetObjPtr() can be used only with Write access.");        return -1;    }    if (nObjId < 1)    {        UGKError(ET_Failure, UGKErr_IllegalArg,               "GetObjPtr(): Invalid object ID %d (must be greater than zero)",                 nObjId);        return -1;    }    /*-----------------------------------------------------------------     * GotoByteInFile() will automagically commit current block and init     * a new one if necessary.     *----------------------------------------------------------------*/    if (m_poIDBlock->GotoByteInFile( (nObjId-1)*4 ) != 0)        return -1;    m_nMaxId = MAX(m_nMaxId, nObjId);    return m_poIDBlock->WriteInt32(nObjPtr);}/********************************************************************** *                   TABIDFile::GetMaxObjId() * * Return the value of the biggest valid object id. * * Note that object ids are positive and start at 1. * * Returns a value >= 0 on success, -1 on error. **********************************************************************/UGKInt32 TABIDFile::GetMaxObjId(){    return m_nMaxId;}/********************************************************************** *                   TABIDFile::Dump() * * Dump block contents... available only in DEBUG mode. **********************************************************************/void TABIDFile::Dump(FILE *fpOut /*=NULL*/){    if (fpOut == NULL)        fpOut = stdout;    fprintf(fpOut, "----- TABIDFile::Dump() -----\n");    if (m_fp == NULL)    {        fprintf(fpOut, "File is not opened.\n");    }    else    {        fprintf(fpOut, "File is opened: %s\n", m_pszFname);        fprintf(fpOut, "Current index block follows ...\n\n");        m_poIDBlock->Dump(fpOut);        fprintf(fpOut, "... end of index block.\n\n");    }    fflush(fpOut);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -