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
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);
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";
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()));
+
+}
+
// 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
{
CPPUNIT_TEST( test_FieldTypeListDbf );
CPPUNIT_TEST( test_NbRecordsDbf );
CPPUNIT_TEST( test_GetAttrListIndex );
+ CPPUNIT_TEST( test_DbfWrite );
CPPUNIT_TEST_SUITE_END();
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 );