]> SALOME platform Git repositories - modules/hydro.git/commitdiff
Salome HOME
dbf writer #1
authorisn <isn@opencascade.com>
Fri, 16 Oct 2015 16:27:09 +0000 (19:27 +0300)
committerisn <isn@opencascade.com>
Fri, 16 Oct 2015 16:27:09 +0000 (19:27 +0300)
src/HYDROData/HYDROData_ShapeFile.cxx
src/HYDROData/HYDROData_ShapeFile.h
src/HYDRO_tests/reference_data/ref_dbf1.dbf [new file with mode: 0644]
src/HYDRO_tests/test_HYDROData_ShapeFile.cxx
src/HYDRO_tests/test_HYDROData_ShapeFile.h

index 10b47b36b7bdf2585258cf640600f3d8d6e1e6d1..1a9e9251776ed1983943c11e2b136dc773d0327b 100644 (file)
@@ -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<DBF_AttrValue>& 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
index ad1df754ffb7140a0224ac98a606969553a5f952..e4296d2fc2785ce7567919f6ec09f9310400754a 100644 (file)
@@ -110,6 +110,7 @@ public:
   int DBF_GetNbRecords();
   void DBF_CloseDBF();
   void DBF_GetAttributeList(int theIndexOfField, std::vector<DBF_AttrValue>& theAttrV);
+  bool DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& 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 (file)
index 0000000..b195b8f
Binary files /dev/null and b/src/HYDRO_tests/reference_data/ref_dbf1.dbf differ
index ccbb8fad8c1249413f160f9117f8800f72285931..c90791f60e2bfa9cac788a9981975ce86c6b1b79 100644 (file)
 
 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<HYDROData_ShapeFile::DBF_AttrValue> 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()));
+
+}
+
index 8a56ab9fc444fbe49b8ee633897b17bd05fb88c5..04e54f8988e7013471708ade5c9aa3847ae51bb6 100644 (file)
@@ -16,6 +16,7 @@
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 #include <cppunit/extensions/HelperMacros.h>
+#include <QString>
 
 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 );