📄 tabview.cpp
字号:
// tabview.cpp: implementation of the TABView class.////////////////////////////////////////////////////////////////////////#include "tabview.h"#include "ugk_errhandle.h"#include "ugk_memopr.h"#include "ugk_string.h"/********************************************************************** * TABView::TABView() * * Constructor. **********************************************************************/TABView::TABView(){ m_pszFname = NULL; m_eAccessMode = TABRead; m_papszTABFile = NULL; m_pszVersion = NULL; m_pszCharset = NULL; m_numTABFiles = 0; m_papszTABFnames = NULL; m_papoTABFiles = NULL; m_nMainTableIndex = -1; m_papszFieldNames = NULL; m_papszWhereClause = NULL; m_poRelation = NULL; m_bRelFieldsCreated = FALSE;}/********************************************************************** * TABView::~TABView() * * Destructor. **********************************************************************/TABView::~TABView(){ Close();}int TABView::GetFeatureCount (int bForce){ if (m_nMainTableIndex != -1) return m_papoTABFiles[m_nMainTableIndex]->GetFeatureCount( bForce ); return 0;}void TABView::ResetReading(){ if (m_nMainTableIndex != -1) m_papoTABFiles[m_nMainTableIndex]->ResetReading();}/********************************************************************** * TABView::Open() * * Open a .TAB dataset and the associated files, and initialize the * structures to be ready to read features from it. * * This class is used to open .TAB files that define a view on * two other .TAB files. Regular .TAB datasets should be opened using * the TABFile class instead. * * Set bTestOpenNoError=TRUE to silently return -1 with no error message * if the file cannot be opened. This is intended to be used in the * context of a TestOpen() function. The default value is FALSE which * means that an error is reported if the file cannot be opened. * * Returns 0 on success, -1 on error. **********************************************************************/int TABView::Open(const char *pszFname, const char *pszAccess, UGKBool bTestOpenNoError /*= FALSE*/ ){ char nStatus = 0; if (m_numTABFiles > 0) { UGKError(ET_Failure, UGKErr_AssertionFailed, "Open() failed: object already contains an open file"); return -1; } /*----------------------------------------------------------------- * Validate access mode and call the right open method *----------------------------------------------------------------*/ if (EQUALN(pszAccess, "r", 1)) { m_eAccessMode = TABRead; nStatus = OpenForRead(pszFname, bTestOpenNoError); } else if (EQUALN(pszAccess, "w", 1)) { m_eAccessMode = TABWrite; nStatus = OpenForWrite(pszFname); } else { UGKError(ET_Failure, UGKErr_NotSupported, "Open() failed: access mode \"%s\" not supported", pszAccess); return -1; } return nStatus;}/********************************************************************** * TABView::OpenForRead() * * Open for reading * * Returns 0 on success, -1 on error. **********************************************************************/int TABView::OpenForRead(const char *pszFname, UGKBool bTestOpenNoError /*= FALSE*/ ){ char *pszPath = NULL; int nFnameLen = 0; m_eAccessMode = TABRead; /*----------------------------------------------------------------- * Read main .TAB (text) file *----------------------------------------------------------------*/ m_pszFname = UGKStrdup(pszFname);#ifndef WIN32APP /*----------------------------------------------------------------- * On Unix, make sure extension uses the right cases * We do it even for write access because if a file with the same * extension already exists we want to overwrite it. *----------------------------------------------------------------*/ TABAdjustFilenameExtension(m_pszFname);#endif /*----------------------------------------------------------------- * Open .TAB file... since it's a small text file, we will just load * it as a stringlist in memory. *----------------------------------------------------------------*/ m_papszTABFile = LoadFileToList(m_pszFname); if (m_papszTABFile == NULL) { if (!bTestOpenNoError) { UGKError(ET_Failure, UGKErr_FileIO, "Failed opening %s.", m_pszFname); } UGK_Free(m_pszFname); return -1; } /*------------------------------------------------------------- * Look for a line with the "create view" keyword. * If there is no "create view", then we may have a valid .TAB file, * but we do not support it in this class. *------------------------------------------------------------*/ UGKBool bCreateViewFound = FALSE; for (int i=0; !bCreateViewFound && m_papszTABFile && m_papszTABFile[i]; i++) { const char *pszStr = m_papszTABFile[i]; while(*pszStr != '\0' && isspace(*pszStr)) pszStr++; if (EQUALN(pszStr, "create view", 11)) bCreateViewFound = TRUE; } if ( !bCreateViewFound ) { if (!bTestOpenNoError) UGKError(ET_Failure, UGKErr_NotSupported, "%s contains no table view definition. " "This type of .TAB file cannot be read by this library.", m_pszFname); else UGKErrorReset(); UGK_Free(m_pszFname); return -1; } /*----------------------------------------------------------------- * OK, this appears to be a valid TAB view dataset... * Extract the path component from the main .TAB filename * to build the filename of the sub-tables *----------------------------------------------------------------*/ pszPath = UGKStrdup(m_pszFname); nFnameLen = strlen(pszPath); for( ; nFnameLen > 0; nFnameLen--) { if (pszPath[nFnameLen-1] == '/' || pszPath[nFnameLen-1] == '\\' ) { break; } pszPath[nFnameLen-1] = '\0'; } /*----------------------------------------------------------------- * Extract the useful info from the TAB header *----------------------------------------------------------------*/ if (ParseTABFile(pszPath, bTestOpenNoError) != 0) { // Failed parsing... an error has already been produced if necessary UGK_Free(pszPath); Close(); return -1; } UGK_Free(pszPath); pszPath = NULL; /*----------------------------------------------------------------- * __TODO__ For now, we support only 2 files linked through a single * field... so we'll do some validation first to make sure * that what we found in the header respects these limitations. *----------------------------------------------------------------*/ if (m_numTABFiles != 2) { if (!bTestOpenNoError) UGKError(ET_Failure, UGKErr_NotSupported, "Open Failed: Dataset %s defines a view on %d tables. " "This is not currently supported.", m_pszFname, m_numTABFiles); Close(); return -1; } /*----------------------------------------------------------------- * Open all the tab files listed in the view *----------------------------------------------------------------*/ m_papoTABFiles = (TABFile**)UGK_Calloc(m_numTABFiles, sizeof(TABFile*)); for (int iFile=0; iFile < m_numTABFiles; iFile++) {#ifndef WIN32APP TABAdjustFilenameExtension(m_papszTABFnames[iFile]);#endif m_papoTABFiles[iFile] = new TABFile; if ( m_papoTABFiles[iFile]->Open(m_papszTABFnames[iFile], "rb", bTestOpenNoError) != 0) { // Open Failed... an error has already been reported, just return. if (bTestOpenNoError) UGKErrorReset(); Close(); return -1; } } /*----------------------------------------------------------------- * Create TABRelation... this will build FeatureDefn, etc. * __TODO__ For now this assumes only 2 tables in the view... *----------------------------------------------------------------*/ m_poRelation = new TABRelation; assert(m_nMainTableIndex == 0); assert(CountOfList(m_papszWhereClause) == 5); char *pszTableName = TABGetBasename(m_pszFname); if ( m_poRelation->Init(pszTableName, m_papoTABFiles[0], m_papoTABFiles[1], m_papszWhereClause[4], m_papszWhereClause[2], m_papszFieldNames) != 0 ) { // An error should already have been reported UGK_Free(pszTableName); Close(); return -1; } UGK_Free(pszTableName); return 0;}/********************************************************************** * TABView::OpenForWrite() * * Create a new TABView dataset * * Returns 0 on success, -1 on error. **********************************************************************/int TABView::OpenForWrite(const char *pszFname){ int nFnameLen = 0; m_eAccessMode = TABWrite; /*----------------------------------------------------------------- * Read main .TAB (text) file *----------------------------------------------------------------*/ m_pszFname = UGKStrdup(pszFname);#ifndef WIN32APP /*----------------------------------------------------------------- * On Unix, make sure extension uses the right cases * We do it even for write access because if a file with the same * extension already exists we want to overwrite it. *----------------------------------------------------------------*/ TABAdjustFilenameExtension(m_pszFname);#endif /*----------------------------------------------------------------- * Extract the path component from the main .TAB filename *----------------------------------------------------------------*/ char *pszPath = UGKStrdup(m_pszFname); nFnameLen = strlen(pszPath); for( ; nFnameLen > 0; nFnameLen--) { if (pszPath[nFnameLen-1] == '/' || pszPath[nFnameLen-1] == '\\' ) { break; } pszPath[nFnameLen-1] = '\0'; } char *pszBasename = TABGetBasename(m_pszFname); /*----------------------------------------------------------------- * Create the 2 TAB files for the view. * * __TODO__ For now, we support only 2 files linked through a single * field... not sure if anything else than that can be useful * anyways. *----------------------------------------------------------------*/ m_numTABFiles = 2; m_papszTABFnames = NULL; m_nMainTableIndex = 0; m_bRelFieldsCreated = FALSE; m_papoTABFiles = (TABFile**)UGK_Calloc(m_numTABFiles, sizeof(TABFile*)); for (int iFile=0; iFile < m_numTABFiles; iFile++) { m_papszTABFnames = AppendPrintf(m_papszTABFnames, "%s%s%d.tab", pszPath, pszBasename, iFile+1);#ifndef WIN32APP TABAdjustFilenameExtension(m_papszTABFnames[iFile]);#endif m_papoTABFiles[iFile] = new TABFile; if ( m_papoTABFiles[iFile]->Open(m_papszTABFnames[iFile], "wb") != 0) { // Open Failed... an error has already been reported, just return. UGK_Free(pszPath); UGK_Free(pszBasename); Close(); return -1; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -