Skip to content

Commit 711ec1f

Browse files
committed
Added error handling in SaveToFile if DBFAddField() fails.
1 parent 5a5fe72 commit 711ec1f

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/COM classes/TableClass.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,9 @@ bool CTableClass::SaveToFile(const CStringW& dbfFilename, bool updateFileInPlace
782782
}
783783
}
784784

785-
DBFAddField(newdbfHandle, OLE2CA(fname), (DBFFieldType)type, width, precision);
785+
int ret = DBFAddField(newdbfHandle, OLE2CA(fname), (DBFFieldType)type, width, precision);
786+
if (ret == -1)
787+
ErrorMessage(tkDBF_CANT_ADD_DBF_FIELD);
786788
field->Release();
787789
}
788790

@@ -822,6 +824,15 @@ bool CTableClass::SaveToFile(const CStringW& dbfFilename, bool updateFileInPlace
822824
if (!updateFileInPlace)
823825
DBFClose(newdbfHandle);
824826

827+
// Set byte 29 to 0x00 in the .dbf file (Codepage mark)
828+
FILE* dbfFile = _wfopen(dbfFilename, L"r+");
829+
if (dbfFile != NULL) {
830+
fseek(dbfFile, 29, SEEK_SET);
831+
fputc('\0', dbfFile);
832+
fflush(dbfFile);
833+
fclose(dbfFile);
834+
}
835+
825836
return true;
826837
}
827838

@@ -863,6 +874,9 @@ STDMETHODIMP CTableClass::SaveAs(BSTR dbfFilename, ICallback *cBack, VARIANT_BOO
863874
// **************************************************************
864875
void CTableClass::ClearFields()
865876
{
877+
//if(_triggerDebug)
878+
//DebugBreak();
879+
866880
for (int i = 0; i < FieldCount(); i++)
867881
{
868882
if (_fields[i]->field != NULL)
@@ -884,6 +898,9 @@ STDMETHODIMP CTableClass::Close(VARIANT_BOOL *retval)
884898

885899
*retval = VARIANT_TRUE;
886900

901+
//if(_triggerDebug)
902+
//DebugBreak();
903+
887904
StopAllJoins();
888905

889906
ClearFields();
@@ -1474,7 +1491,9 @@ bool CTableClass::WriteRecord(DBFInfo* dbfHandle, long fromRowIndex, long toRowI
14741491
if (val.vt == VT_BSTR)
14751492
{
14761493
nonstackString = Utility::ConvertBSTRToLPSTR(val.bstrVal, (isUTF8 ? CP_UTF8 : CP_ACP)); // ((LPCSTR)Utility::ConvertToUtf8(val.bstrVal)); // Utility::SYS2A(val.bstrVal);
1494+
int fieldCount = DBFGetFieldCount(dbfHandle);
14771495
DBFWriteStringAttribute(dbfHandle, toRowIndex, i, nonstackString);
1496+
//::OutputDebugString("DBFWriteStringAttribute() done!");
14781497
delete[] nonstackString;
14791498
nonstackString = NULL;
14801499
}
@@ -1764,7 +1783,7 @@ STDMETHODIMP CTableClass::EditCellValue(long FieldIndex, long RowIndex, VARIANT
17641783

17651784
// Darrel Brown, 10/16/2003 Added support for null cell values
17661785
// jf, 2/17/2018, added support for Dates and Booleans
1767-
if (newVal.vt != VT_I4 && newVal.vt != VT_R8 && newVal.vt != VT_BSTR && newVal.vt != VT_DATE && newVal.vt != VT_BOOL && newVal.vt != VT_NULL)
1786+
if (newVal.vt != VT_I4 && newVal.vt != VT_R8 && newVal.vt != VT_BSTR && newVal.vt != VT_DATE && newVal.vt != VT_BOOL && newVal.vt != VT_NULL && newVal.vt != VT_EMPTY)
17681787
{
17691788
ErrorMessage(tkINCORRECT_VARIANT_TYPE);
17701789
return S_OK;
@@ -3296,7 +3315,7 @@ STDMETHODIMP CTableClass::StopJoin(int joinIndex, VARIANT_BOOL* retVal)
32963315
STDMETHODIMP CTableClass::get_IsJoined(VARIANT_BOOL* retVal)
32973316
{
32983317
AFX_MANAGE_STATE(AfxGetStaticModuleState());
3299-
for (size_t i = _fields.size() - 1; i >= 0; i--)
3318+
for (size_t i = _fields.size() - 1; i >= 0 && _fields.size() > i; i--)
33003319
{
33013320
if (_fields[i]->Joined()) {
33023321
*retVal = VARIANT_TRUE;

src/COM classes/TableClass.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ATL_NO_VTABLE CTableClass :
4646
_pUnkMarshaler = NULL;
4747
_key = SysAllocString(L"");
4848
_lastRecordIndex = -1;
49+
_triggerDebug = false;
4950
gReferenceCounter.AddRef(tkInterface::idTable);
5051
}
5152

@@ -194,6 +195,7 @@ class ATL_NO_VTABLE CTableClass :
194195
int _lastRecordIndex; // last index accessed with get_CellValue
195196
bool _appendMode;
196197
int _appendStartShapeCount;
198+
bool _triggerDebug;
197199

198200
public:
199201
bool m_needToSaveAsNewFile;

0 commit comments

Comments
 (0)