📄 tabfile.cpp
字号:
} /*----------------------------------------------------------------- * Keep a reference to the UGKFeatureDefn... we'll have to take the * reference count into account when we are done with it. *----------------------------------------------------------------*/ if (m_poDefn && m_poDefn->Dereference() == 0) delete m_poDefn; m_poDefn = poFeatureDefn; m_poDefn->Reference(); /*----------------------------------------------------------------- * Pass field information to the .DAT file, after making sure that * it has been created and that it does not contain any field * definition yet. *----------------------------------------------------------------*/ if (m_poDATFile== NULL || m_poDATFile->GetNumFields() > 0 ) { UGKError(ET_Failure, UGKErr_AssertionFailed, "SetFeatureDefn() can be called only once in a newly " "created dataset."); return -1; } numFields = poFeatureDefn->GetFieldCount(); for(iField=0; nStatus==0 && iField < numFields; iField++) { poFieldDefn = m_poDefn->GetFieldDefn(iField); /*------------------------------------------------------------- * Make sure field name is valid... check for special chars, etc. *------------------------------------------------------------*/ char *pszCleanName = TABCleanFieldName(poFieldDefn->GetNameRef()); if (!EQUAL(pszCleanName, poFieldDefn->GetNameRef())) poFieldDefn->SetName(pszCleanName); UGK_Free(pszCleanName); pszCleanName = NULL; if (paeMapInfoNativeFieldTypes) { eMapInfoType = paeMapInfoNativeFieldTypes[iField]; } else { /*--------------------------------------------------------- * Map UGKFieldTypes to MapInfo native types *--------------------------------------------------------*/ switch(poFieldDefn->GetType()) { case OFTInteger: eMapInfoType = TABFInteger; break; case OFTReal: eMapInfoType = TABFFloat; break; case OFTString: default: eMapInfoType = TABFChar; } } nStatus = m_poDATFile->AddField(poFieldDefn->GetNameRef(), eMapInfoType, poFieldDefn->GetWidth(), poFieldDefn->GetPrecision()); } /*----------------------------------------------------------------- * Alloc the array to keep track of indexed fields (default=NOT indexed) *----------------------------------------------------------------*/ m_panIndexNo = (int *)UGK_Calloc(numFields, sizeof(int)); return nStatus;}/********************************************************************** * TABFile::AddFieldNative() * * Create a new field using a native mapinfo data type... this is an * alternative to defining fields through the UGK interface. * This function should be called after creating a new dataset, but before * writing the first feature. * * This function will build/update the UGKFeatureDefn that will have to be * used when writing features to this dataset. * * A reference to the UGKFeatureDefn can be obtained using GetLayerDefn(). * * Note: The bUnique flag has no effect on TABFiles. See the TABView class. * * Returns 0 on success, -1 on error. **********************************************************************/int TABFile::AddFieldNative(const char *pszName, TABFieldType eMapInfoType, int nWidth /*=0*/, int nPrecision /*=0*/, UGKBool bIndexed /*=FALSE*/, UGKBool /*bUnique=FALSE*/){ UGKFieldDefn *poFieldDefn; int nStatus = 0; char *pszCleanName = NULL; if (m_eAccessMode != TABWrite) { UGKError(ET_Failure, UGKErr_NotSupported, "AddFieldNative() can be used only with Write access."); return -1; } /*----------------------------------------------------------------- * Check that call happens at the right time in dataset's life. *----------------------------------------------------------------*/ if (m_eAccessMode != TABWrite || m_nLastFeatureId > 0 || m_poDATFile == NULL) { UGKError(ET_Failure, UGKErr_AssertionFailed, "AddFieldNative() must be called after opening a new " "dataset, but before writing the first feature to it."); return -1; } /*----------------------------------------------------------------- * Create new UGKFeatureDefn if not done yet... *----------------------------------------------------------------*/ if (m_poDefn== NULL) { char *pszFeatureClassName = TABGetBasename(m_pszFname); m_poDefn = new UGKFeatureDefn(pszFeatureClassName); UGK_Free(pszFeatureClassName); // Ref count defaults to 0... set it to 1 m_poDefn->Reference(); } /*----------------------------------------------------------------- * Validate field width... must be <= 254 *----------------------------------------------------------------*/ if (nWidth > 254) { UGKError(ET_Warning, UGKErr_IllegalArg, "Invalid size (%d) for field '%s'. " "Size must be 254 or less.", nWidth, pszName); nWidth=254; } /*----------------------------------------------------------------- * Map fields with width=0 (variable length in UGK) to a valid default *----------------------------------------------------------------*/ if (eMapInfoType == TABFDecimal && nWidth == 0) nWidth=20; else if (nWidth == 0) nWidth=254; /* char fields */ /*----------------------------------------------------------------- * Make sure field name is valid... check for special chars, etc. * (pszCleanName will have to be freed.) *----------------------------------------------------------------*/ pszCleanName = TABCleanFieldName(pszName); /*----------------------------------------------------------------- * Map MapInfo native types to UGK types *----------------------------------------------------------------*/ poFieldDefn = NULL; switch(eMapInfoType) { case TABFChar: /*------------------------------------------------- * CHAR type *------------------------------------------------*/ poFieldDefn = new UGKFieldDefn(pszCleanName, OFTString); poFieldDefn->SetWidth(nWidth); break; case TABFInteger: /*------------------------------------------------- * INTEGER type *------------------------------------------------*/ poFieldDefn = new UGKFieldDefn(pszCleanName, OFTInteger); break; case TABFSmallInt: /*------------------------------------------------- * SMALLINT type *------------------------------------------------*/ poFieldDefn = new UGKFieldDefn(pszCleanName, OFTInteger); break; case TABFDecimal: /*------------------------------------------------- * DECIMAL type *------------------------------------------------*/ poFieldDefn = new UGKFieldDefn(pszCleanName, OFTReal); poFieldDefn->SetWidth(nWidth); poFieldDefn->SetPrecision(nPrecision); break; case TABFFloat: /*------------------------------------------------- * FLOAT type *------------------------------------------------*/ poFieldDefn = new UGKFieldDefn(pszCleanName, OFTReal); break; case TABFDate: /*------------------------------------------------- * DATE type (returned as a string: "DD/MM/YYYY") *------------------------------------------------*/ poFieldDefn = new UGKFieldDefn(pszCleanName, OFTString); poFieldDefn->SetWidth(10); break; case TABFLogical: /*------------------------------------------------- * LOGICAL type (value "T" or "F") *------------------------------------------------*/ poFieldDefn = new UGKFieldDefn(pszCleanName, OFTString); poFieldDefn->SetWidth(1); break; default: UGKError(ET_Failure, UGKErr_NotSupported, "Unsupported type for field %s", pszCleanName); UGK_Free(pszCleanName); return -1; } /*----------------------------------------------------- * Add the FieldDefn to the FeatureDefn *----------------------------------------------------*/ m_poDefn->AddFieldDefn(poFieldDefn); delete poFieldDefn; /*----------------------------------------------------- * ... and pass field info to the .DAT file. *----------------------------------------------------*/ nStatus = m_poDATFile->AddField(pszCleanName, eMapInfoType, nWidth, nPrecision); /*----------------------------------------------------------------- * Extend the array to keep track of indexed fields (default=NOT indexed) *----------------------------------------------------------------*/ m_panIndexNo = (int *)UGK_Realloc(m_panIndexNo, m_poDefn->GetFieldCount()*sizeof(int)); m_panIndexNo[m_poDefn->GetFieldCount()-1] = 0; /*----------------------------------------------------------------- * Index the field if requested *----------------------------------------------------------------*/ if (nStatus == 0 && bIndexed) nStatus = SetFieldIndexed(m_poDefn->GetFieldCount()-1); UGK_Free(pszCleanName); return nStatus;}/********************************************************************** * TABFile::GetNativeFieldType() * * Returns the native MapInfo field type for the specified field. * * Returns TABFUnknown if file is not opened, or if specified field index is * invalid. * * Note that field ids are positive and start at 0. **********************************************************************/TABFieldType TABFile::GetNativeFieldType(int nFieldId){ if (m_poDATFile) { return m_poDATFile->GetFieldType(nFieldId); } return TABFUnknown;}/********************************************************************** * TABFile::GetFieldIndexNumber() * * Returns the field's index number that was specified in the .TAB header * or 0 if the specified field is not indexed. * * Note that field ids are positive and start at 0 * and valid index ids are positive and start at 1. **********************************************************************/int TABFile::GetFieldIndexNumber(int nFieldId){ if (m_panIndexNo == NULL || nFieldId < 0 || m_poDATFile== NULL || nFieldId >= m_poDATFile->GetNumFields()) return 0; // no index return m_panIndexNo[nFieldId];}/************************************************************************ * TABFile::SetFieldIndexed() * * Request that a field be indexed. This will create the .IND file if * necessary, etc. * * Note that field ids are positive and start at 0. * * Returns 0 on success, -1 on error. ************************************************************************/int TABFile::SetFieldIndexed( int nFieldId ){ /*----------------------------------------------------------------- * Make sure things are OK *----------------------------------------------------------------*/ if (m_pszFname == NULL || m_eAccessMode != TABWrite || m_poDefn == NULL) { UGKError(ET_Failure, UGKErr_AssertionFailed, "SetFieldIndexed() must be called after opening a new " "dataset, but before writing the first feature to it."); return -1; } if (m_panIndexNo == NULL || nFieldId < 0 || m_poDATFile== NULL || nFieldId >= m_poDATFile->GetNumFields()) { UGKError(ET_Failure, UGKErr_AssertionFailed, "Invalid field number in SetFieldIndexed()."); return -1; } /*----------------------------------------------------------------- * If field is already indexed then just return *----------------------------------------------------------------*/ if (m_panIndexNo[nFieldId] != 0) return 0; // Nothing to do /*----------------------------------------------------------------- * Create .IND file if it's not done yet. * * Note: We can pass the .TAB's filename directly and the * TABINDFile class will automagically adjust the extension. *----------------------------------------------------------------*/ if (m_poINDFile == NULL) { m_poINDFile = new TABINDFile; if ( m_poINDFile->Open(m_pszFname, "w", TRUE) != 0) { // File could not be opened... delete m_poINDFile; m_poINDFile = NULL; return -1; } } /*----------------------------------------------------------------- * Init new index. *----------------------------------------------------------------*/ int nNewIndexNo; UGKFieldDefn *poFie
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -