From a9b9345d9e3ee5d9d42e76ec20a3195e63aafc0f Mon Sep 17 00:00:00 2001 From: isn Date: Fri, 16 Oct 2015 19:27:09 +0300 Subject: [PATCH] dbf writer #1 --- src/HYDROData/HYDROData_ShapeFile.cxx | 76 +++++++++++++++++++ src/HYDROData/HYDROData_ShapeFile.h | 1 + src/HYDRO_tests/reference_data/ref_dbf1.dbf | Bin 0 -> 107 bytes src/HYDRO_tests/test_HYDROData_ShapeFile.cxx | 51 +++++++++++++ src/HYDRO_tests/test_HYDROData_ShapeFile.h | 5 ++ 5 files changed, 133 insertions(+) create mode 100644 src/HYDRO_tests/reference_data/ref_dbf1.dbf diff --git a/src/HYDROData/HYDROData_ShapeFile.cxx b/src/HYDROData/HYDROData_ShapeFile.cxx index 10b47b36..1a9e9251 100644 --- a/src/HYDROData/HYDROData_ShapeFile.cxx +++ b/src/HYDROData/HYDROData_ShapeFile.cxx @@ -788,4 +788,80 @@ void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector< theAttrV.push_back(anAttr); } +} + +bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector& theAttrV, bool bUseStrValue) +{ + // Check that given field type is equal to field types of attributes values + for (size_t i = 0; i < theAttrV.size(); i++) + { + if (theAttrV[i].myFieldType != theType) + return false; + } + + DBFHandle hDBF; + hDBF = DBFCreate( theFileName.toStdString().c_str() ); + if( hDBF == NULL ) + return false; + + if (theType != DBF_FieldType_String && theType != DBF_FieldType_Integer && theType != DBF_FieldType_Double) + { + DBFClose( hDBF ); + return false; //cant handle any other cases + } + + int nWidth = 20; + switch( theType ) + { + case DBF_FieldType_String: + { + DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTString, nWidth, 0); + break; + } + + case DBF_FieldType_Integer: + { + DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTInteger, nWidth, 0); + break; + } + + case DBF_FieldType_Double: + { + DBFAddField (hDBF, theFieldName.toStdString().c_str(), FTDouble, nWidth, 0); + break; + } + default: + break; + } + + if (DBFGetFieldCount( hDBF ) != 1) + { + DBFClose( hDBF ); + return false; + } + if (DBFGetRecordCount( hDBF ) != 0) + { + DBFClose( hDBF ); + return false; + } + int stat = -1; + + //use raw values only //TODO + for (size_t i = 0; i < theAttrV.size(); i++) + { + if (!theAttrV[i].myIsNull) + stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str()); + else + stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 ); + + if (stat != 1) + { + DBFClose( hDBF ); + return false; + } + } + + DBFClose( hDBF ); + return true; + } \ No newline at end of file diff --git a/src/HYDROData/HYDROData_ShapeFile.h b/src/HYDROData/HYDROData_ShapeFile.h index ad1df754..e4296d2f 100644 --- a/src/HYDROData/HYDROData_ShapeFile.h +++ b/src/HYDROData/HYDROData_ShapeFile.h @@ -110,6 +110,7 @@ public: int DBF_GetNbRecords(); void DBF_CloseDBF(); void DBF_GetAttributeList(int theIndexOfField, std::vector& theAttrV); + bool DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector& theAttrV, bool bUseStrValue); private: void ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle); diff --git a/src/HYDRO_tests/reference_data/ref_dbf1.dbf b/src/HYDRO_tests/reference_data/ref_dbf1.dbf new file mode 100644 index 0000000000000000000000000000000000000000..b195b8fdc8aa977283ec7074c56fdb6d0db0b4f2 GIT binary patch literal 107 zcmZRMXP07PU|?`$5CxKGAe@1rB(=CCJ}omfCxyWoBrAfZgjWHqqAW3|G}TZ64iJJy Ga4`VAv=J2m literal 0 HcmV?d00001 diff --git a/src/HYDRO_tests/test_HYDROData_ShapeFile.cxx b/src/HYDRO_tests/test_HYDROData_ShapeFile.cxx index ccbb8fad..c90791f6 100644 --- a/src/HYDRO_tests/test_HYDROData_ShapeFile.cxx +++ b/src/HYDRO_tests/test_HYDROData_ShapeFile.cxx @@ -23,6 +23,29 @@ const QString REF_PATH = qgetenv( "HYDRO_REFERENCE_DATA" ); +bool test_HYDROData_ShapeFile::compare_two_files(const QString& File1, const QString& File2) +{ + FILE *fp1, *fp2; + fp1 = fopen(File1.toStdString().c_str(), "r"); + fp2 = fopen(File2.toStdString().c_str(), "r"); + CPPUNIT_ASSERT(fp1); + CPPUNIT_ASSERT(fp2); + char ch1, ch2; + while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF)) + { + if (!ch1 == ch2) + { + fclose(fp1); + fclose(fp2); + return false; + } + } + + fclose(fp1); + fclose(fp2); + return true; +} + void test_HYDROData_ShapeFile::test_openDbf() { QString aDBFPath = REF_PATH + "/cyprus_natural.dbf"; @@ -134,3 +157,31 @@ void test_HYDROData_ShapeFile::test_GetAttrListIndex() aSHPFile.DBF_CloseDBF(); } } + + +void test_HYDROData_ShapeFile::test_DbfWrite() +{ + HYDROData_ShapeFile aSHPFile; + QString tempFN = REF_PATH + "/temp_dbf1.dbf"; + QString refFN = REF_PATH + "/ref_dbf1.dbf"; + std::vector theAttrV; + HYDROData_ShapeFile::DBF_AttrValue theAttr1; + theAttr1.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String; + theAttr1.myIsNull = false; + theAttr1.myRawValue = "test_value1"; + theAttr1.myStrVal = "test_value1"; + theAttrV.push_back(theAttr1); + HYDROData_ShapeFile::DBF_AttrValue theAttr2; + theAttr2.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String; + theAttr2.myIsNull = false; + theAttr2.myRawValue = "test_value2"; + theAttr2.myStrVal = "test_value2"; + theAttrV.push_back(theAttr2); + // + aSHPFile.DBF_WriteFieldAndValues(tempFN, "test_field_name", HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false); + + CPPUNIT_ASSERT(compare_two_files(tempFN, refFN)); + CPPUNIT_ASSERT_EQUAL( 0, remove (tempFN.toStdString().c_str())); + +} + diff --git a/src/HYDRO_tests/test_HYDROData_ShapeFile.h b/src/HYDRO_tests/test_HYDROData_ShapeFile.h index 8a56ab9f..04e54f89 100644 --- a/src/HYDRO_tests/test_HYDROData_ShapeFile.h +++ b/src/HYDRO_tests/test_HYDROData_ShapeFile.h @@ -16,6 +16,7 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // #include +#include class test_HYDROData_ShapeFile : public CppUnit::TestFixture { @@ -26,6 +27,7 @@ class test_HYDROData_ShapeFile : public CppUnit::TestFixture CPPUNIT_TEST( test_FieldTypeListDbf ); CPPUNIT_TEST( test_NbRecordsDbf ); CPPUNIT_TEST( test_GetAttrListIndex ); + CPPUNIT_TEST( test_DbfWrite ); CPPUNIT_TEST_SUITE_END(); @@ -36,6 +38,9 @@ public: void test_FieldTypeListDbf(); void test_NbRecordsDbf(); void test_GetAttrListIndex (); + void test_DbfWrite(); +private: + bool compare_two_files(const QString& File1, const QString& File2); }; CPPUNIT_TEST_SUITE_REGISTRATION( test_HYDROData_ShapeFile ); -- 2.39.2