Skip to content

Commit

Permalink
Replace access/_access by std::filesystem::exists (#4307)
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed Aug 25, 2024
1 parent ee80dfe commit fc50324
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 39 deletions.
8 changes: 2 additions & 6 deletions src/ccutil/ccutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if defined(_WIN32)
# include <io.h> // for _access
#endif

#include "ccutil.h"
#include "tprintf.h" // for tprintf

Expand Down Expand Up @@ -63,7 +59,7 @@ void CCUtil::main_setup(const std::string &argv0, const std::string &basename) {
/* Use tessdata prefix from the environment. */
datadir = tessdata_prefix;
#if defined(_WIN32)
} else if (datadir.empty() || _access(datadir.c_str(), 0) != 0) {
} else if (datadir.empty() || !std::filesystem::exists(datadir)) {
/* Look for tessdata in directory of executable. */
char path[_MAX_PATH];
DWORD length = GetModuleFileName(nullptr, path, sizeof(path));
Expand All @@ -73,7 +69,7 @@ void CCUtil::main_setup(const std::string &argv0, const std::string &basename) {
*separator = '\0';
std::string subdir = path;
subdir += "/tessdata";
if (_access(subdir.c_str(), 0) == 0) {
if (std::filesystem::exists(subdir)) {
datadir = subdir;
}
}
Expand Down
17 changes: 2 additions & 15 deletions unittest/pagesegmode_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if defined(_WIN32)
# include <io.h> // for _access
#else
# include <unistd.h> // for access
#endif
#include <allheaders.h>
#include <tesseract/baseapi.h>
#include <filesystem>
#include <string>
#include "helpers.h"
#include "include_gunit.h"
Expand All @@ -24,15 +20,6 @@

namespace tesseract {

// Replacement for std::filesystem::exists (C++-17)
static bool file_exists(const char *filename) {
#if defined(_WIN32)
return _access(filename, 0) == 0;
#else
return access(filename, 0) == 0;
#endif
}

// The fixture for testing Tesseract.
class PageSegModeTest : public testing::Test {
protected:
Expand Down Expand Up @@ -86,7 +73,7 @@ class PageSegModeTest : public testing::Test {
// and differently to line and block mode.
TEST_F(PageSegModeTest, WordTest) {
std::string filename = file::JoinPath(TESTING_DIR, "segmodeimg.tif");
if (!file_exists(filename.c_str())) {
if (!std::filesystem::exists(filename)) {
LOG(INFO) << "Skip test because of missing " << filename << '\n';
GTEST_SKIP();
} else {
Expand Down
22 changes: 4 additions & 18 deletions unittest/tatweel_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if defined(_WIN32)
# include <io.h> // for _access
#else
# include <unistd.h> // for access
#endif

#include <filesystem>
#include "dawg.h"
#include "include_gunit.h"
#include "trie.h"
Expand All @@ -23,15 +18,6 @@

namespace tesseract {

// Replacement for std::filesystem::exists (C++-17)
static bool file_exists(const char *filename) {
#if defined(_WIN32)
return _access(filename, 0) == 0;
#else
return access(filename, 0) == 0;
#endif
}

class TatweelTest : public ::testing::Test {
protected:
void SetUp() override {
Expand All @@ -41,7 +27,7 @@ class TatweelTest : public ::testing::Test {

TatweelTest() {
std::string filename = TestDataNameToPath("ara.wordlist");
if (file_exists(filename.c_str())) {
if (std::filesystem::exists(filename)) {
std::string wordlist("\u0640");
CHECK_OK(file::GetContents(filename, &wordlist, file::Defaults()));
// Put all the unicodes in the unicharset_.
Expand Down Expand Up @@ -77,7 +63,7 @@ TEST_F(TatweelTest, DictIgnoresTatweel) {
// This test verifies that the dictionary ignores the Tatweel character.
tesseract::Trie trie(tesseract::DAWG_TYPE_WORD, "ara", SYSTEM_DAWG_PERM, unicharset_.size(), 0);
std::string filename = TestDataNameToPath("ara.wordlist");
if (!file_exists(filename.c_str())) {
if (!std::filesystem::exists(filename)) {
LOG(INFO) << "Skip test because of missing " << filename;
GTEST_SKIP();
} else {
Expand All @@ -91,7 +77,7 @@ TEST_F(TatweelTest, UnicharsetLoadKeepsTatweel) {
// This test verifies that a load of an existing unicharset keeps any
// existing tatweel for backwards compatibility.
std::string filename = TestDataNameToPath("ara.unicharset");
if (!file_exists(filename.c_str())) {
if (!std::filesystem::exists(filename)) {
LOG(INFO) << "Skip test because of missing " << filename;
GTEST_SKIP();
} else {
Expand Down

0 comments on commit fc50324

Please sign in to comment.