Skip to content

Commit

Permalink
Merge pull request #117 from thbeu/fix-clone
Browse files Browse the repository at this point in the history
Utilize low-level API for DBFCloneEmpty to also pass I/O hooks
  • Loading branch information
rouault authored Mar 14, 2024
2 parents a8b3d89 + 5b8c378 commit 46ff64c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
10 changes: 6 additions & 4 deletions dbfopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ int SHPAPI_CALL DBFWriteDoubleAttribute(DBFHandle psDBF, int iRecord,
/************************************************************************/
/* DBFWriteIntegerAttribute() */
/* */
/* Write a integer attribute. */
/* Write an integer attribute. */
/************************************************************************/

int SHPAPI_CALL DBFWriteIntegerAttribute(DBFHandle psDBF, int iRecord,
Expand Down Expand Up @@ -1502,7 +1502,7 @@ int SHPAPI_CALL DBFWriteStringAttribute(DBFHandle psDBF, int iRecord,
/************************************************************************/
/* DBFWriteNULLAttribute() */
/* */
/* Write a string attribute. */
/* Write a NULL attribute. */
/************************************************************************/

int SHPAPI_CALL DBFWriteNULLAttribute(DBFHandle psDBF, int iRecord, int iField)
Expand Down Expand Up @@ -1620,13 +1620,15 @@ const char SHPAPI_CALL1(*) DBFReadTuple(DBFHandle psDBF, int hEntity)
/************************************************************************/
/* DBFCloneEmpty() */
/* */
/* Read one of the attribute fields of a record. */
/* Create a new .dbf file with same code page and field */
/* definitions as the given handle. */
/************************************************************************/

DBFHandle SHPAPI_CALL DBFCloneEmpty(const DBFHandle psDBF,
const char *pszFilename)
{
DBFHandle newDBF = DBFCreateEx(pszFilename, psDBF->pszCodePage);
DBFHandle newDBF =
DBFCreateLL(pszFilename, psDBF->pszCodePage, &psDBF->sHooks);
if (newDBF == SHPLIB_NULLPTR)
return SHPLIB_NULLPTR;

Expand Down
49 changes: 37 additions & 12 deletions tests/dbf_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,43 @@ TEST(DBFCreateTest, CreateAndClose)
fs::remove(filename);
}

static auto GenerateUniqueFilename(std::string_view ext) -> auto
{
const auto now = std::chrono::system_clock::now();
const auto timestamp =
std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch())
.count();
std::ostringstream oss;
oss << "test_" << timestamp << ext;
return oss.str();
}

TEST(DBFCreateTest, CloneEmpty)
{
const auto filename =
fs::temp_directory_path() / GenerateUniqueFilename(".clone.dbf");
const auto handle = [&]
{
const auto handle =
DBFOpen(fs::path(kTestData / "anno.dbf").string().c_str(), "rb");
EXPECT_NE(nullptr, handle);
const auto clonedhandle =
DBFCloneEmpty(handle, filename.string().c_str());
DBFClose(handle);
return clonedhandle;
}();
ASSERT_NE(nullptr, handle);
const auto fieldcount = DBFGetFieldCount(handle);
EXPECT_EQ(10, fieldcount);
const auto recordcount = DBFGetRecordCount(handle);
EXPECT_EQ(0, recordcount);
DBFClose(handle);
const auto size = fs::file_size(filename);
EXPECT_EQ(354, size);
fs::remove(filename);
}

static auto WriteDate(const fs::path &filename,
const std::unique_ptr<const SHPDate> &date)
{
Expand Down Expand Up @@ -121,18 +158,6 @@ static auto ReadDate(const fs::path &filename) -> auto
return std::make_unique<const SHPDate>(date);
}

static auto GenerateUniqueFilename(std::string_view ext) -> auto
{
const auto now = std::chrono::system_clock::now();
const auto timestamp =
std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch())
.count();
std::ostringstream oss;
oss << "test_" << timestamp << ext;
return oss.str();
}

TEST(DBFFieldTest, SetAndGetDateToday)
{
const auto filename =
Expand Down

0 comments on commit 46ff64c

Please sign in to comment.