]> SALOME platform Git repositories - modules/hydro.git/commitdiff
Salome HOME
dbf writer #2
authorisn <isn@opencascade.com>
Fri, 16 Oct 2015 17:25:48 +0000 (20:25 +0300)
committerisn <isn@opencascade.com>
Fri, 16 Oct 2015 17:25:48 +0000 (20:25 +0300)
src/HYDROData/HYDROData_ShapeFile.cxx
src/HYDROData/HYDROData_ShapeFile.h
src/HYDRO_tests/test_HYDROData_ShapeFile.cxx

index 1a9e9251776ed1983943c11e2b136dc773d0327b..7db8a1442ffcd10709168a4a5c102e4bf4a1ee31 100644 (file)
@@ -768,7 +768,7 @@ void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<
           anAttr.myIsNull = false;
           anAttr.myFieldType = DBF_FieldType_Integer;
           anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
-          anAttr.myStrVal = QString::number(iAttr);
+          anAttr.myIntVal = iAttr;
           break;
         }
           
@@ -778,7 +778,7 @@ void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<
           anAttr.myIsNull = false;
           anAttr.myFieldType = DBF_FieldType_Double;
           anAttr.myRawValue = DBFReadStringAttribute( myHDBF, i, theIndexOfField );
-          anAttr.myStrVal = QString::number(dAttr, 'g', 20);
+          anAttr.myDoubleVal = dAttr;
           break;
         }
         default:
@@ -790,7 +790,7 @@ void HYDROData_ShapeFile::DBF_GetAttributeList(int theIndexOfField, std::vector<
 
 }
 
-bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseStrValue)
+bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, const QString& theFieldName, DBF_FieldType theType, const std::vector<DBF_AttrValue>& theAttrV, bool bUseRawValue)
 {
   // Check that given field type is equal to field types of attributes values
   for (size_t i = 0; i < theAttrV.size(); i++)
@@ -846,18 +846,57 @@ bool HYDROData_ShapeFile::DBF_WriteFieldAndValues(const QString& theFileName, co
   }
   int stat = -1;
 
-  //use raw values only //TODO
-  for (size_t i = 0; i < theAttrV.size(); i++)
+  if (bUseRawValue)
   {
-    if (!theAttrV[i].myIsNull)
-      stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myRawValue.c_str());
-    else
-      stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
+    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)
+      if (stat != 1)
+      {
+        DBFClose( hDBF );
+        return false;
+      }
+    }
+  }
+  else
+  {
+    for (size_t i = 0; i < theAttrV.size(); i++)
     {
-      DBFClose( hDBF );
-      return false;
+      if (!theAttrV[i].myIsNull)
+        switch( theType )
+        {
+          case DBF_FieldType_String:
+          {
+            stat = DBFWriteStringAttribute(hDBF, (int)i, 0, theAttrV[i].myStrVal.toStdString().c_str());
+            break;
+          }
+         
+          case DBF_FieldType_Integer:
+          {
+            stat = DBFWriteIntegerAttribute(hDBF, (int)i, 0, theAttrV[i].myIntVal);
+            break;
+          }
+            
+          case DBF_FieldType_Double:
+          {
+            stat = DBFWriteDoubleAttribute(hDBF, (int)i, 0, theAttrV[i].myDoubleVal);
+            break;
+          }
+          default:
+            break;
+        }
+      else
+        stat = DBFWriteNULLAttribute(hDBF, (int)i, 0 );
+
+      if (stat != 1)
+      {
+        DBFClose( hDBF );
+        return false;
+      }
     }
   }
 
index e4296d2fc2785ce7567919f6ec09f9310400754a..96cd08ec6762fe66ccecac8086a5e990a1949e69 100644 (file)
@@ -64,10 +64,14 @@ struct DBF_AttrValue
 {
   DBF_AttrValue() 
   {
+    myIntVal = -1;
+    myDoubleVal = -1.0;
     myIsNull = true;
     myFieldType = DBF_FieldType_None;
   }
   QString myStrVal;
+  int myIntVal;
+  double myDoubleVal;
   std::string myRawValue;
   DBF_FieldType myFieldType;
   bool myIsNull;
index c90791f60e2bfa9cac788a9981975ce86c6b1b79..e0369578ef897484ed4439ba8ce1bec0f607c839 100644 (file)
@@ -150,7 +150,7 @@ void test_HYDROData_ShapeFile::test_GetAttrListIndex()
     aSHPFile.DBF_GetAttributeList(2, theAttrV ); 
     CPPUNIT_ASSERT_EQUAL( 3269, (int)theAttrV.size() ); 
     CPPUNIT_ASSERT_EQUAL( true, std::string("6621.1654324936000000000000000000") == theAttrV[2].myRawValue);
-    CPPUNIT_ASSERT_EQUAL( true, QString("6621.1654324935998375") == theAttrV[2].myStrVal);
+    CPPUNIT_ASSERT_EQUAL( 6621.1654324935998375, theAttrV[2].myDoubleVal);
     CPPUNIT_ASSERT_EQUAL( false, theAttrV[2].myIsNull);
     CPPUNIT_ASSERT_EQUAL( HYDROData_ShapeFile::DBF_FieldType_Double, theAttrV[2].myFieldType);
 
@@ -161,27 +161,61 @@ void test_HYDROData_ShapeFile::test_GetAttrListIndex()
 
 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()));
+  {
+    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);
+    //
+    CPPUNIT_ASSERT(aSHPFile.DBF_WriteFieldAndValues(tempFN, "test_field_name", HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, true));
+
+    CPPUNIT_ASSERT(compare_two_files(tempFN, refFN));
+    CPPUNIT_ASSERT_EQUAL( 0, remove (tempFN.toStdString().c_str()));
+  }
+
+  {
+    HYDROData_ShapeFile aSHPFile; 
+    QString tempFN = REF_PATH + "/temp_dbf2.dbf";
+    QString refFN = REF_PATH + "/ref_dbf2.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.myIsNull = false;
+    theAttr2.myRawValue = "test_value_K";
+    theAttr2.myStrVal = "test_value_K";
+    theAttr2.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
+    for (int ii = 0; ii < 100; ii++)
+      theAttrV.push_back(theAttr2);
+
+    HYDROData_ShapeFile::DBF_AttrValue theAttr3;
+    theAttr3.myIsNull = true;
+    theAttr3.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
+    for (int ii = 0; ii < 100; ii++)
+      theAttrV.push_back(theAttr3);
+    //
+    CPPUNIT_ASSERT(aSHPFile.DBF_WriteFieldAndValues(tempFN, "test_field_name", HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, true));
+
+    CPPUNIT_ASSERT(compare_two_files(tempFN, refFN));
+    CPPUNIT_ASSERT_EQUAL( 0, remove (tempFN.toStdString().c_str()));
+  }
 
 }