Replace access/_access by std::filesystem::exists (#4307)

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2024-08-25 18:57:22 +02:00 committed by GitHub
parent ee80dfe509
commit fc50324986
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 39 deletions

View File

@ -10,10 +10,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#if defined(_WIN32)
# include <io.h> // for _access
#endif
#include "ccutil.h" #include "ccutil.h"
#include "tprintf.h" // for tprintf #include "tprintf.h" // for tprintf
@ -63,7 +59,7 @@ void CCUtil::main_setup(const std::string &argv0, const std::string &basename) {
/* Use tessdata prefix from the environment. */ /* Use tessdata prefix from the environment. */
datadir = tessdata_prefix; datadir = tessdata_prefix;
#if defined(_WIN32) #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. */ /* Look for tessdata in directory of executable. */
char path[_MAX_PATH]; char path[_MAX_PATH];
DWORD length = GetModuleFileName(nullptr, path, sizeof(path)); DWORD length = GetModuleFileName(nullptr, path, sizeof(path));
@ -73,7 +69,7 @@ void CCUtil::main_setup(const std::string &argv0, const std::string &basename) {
*separator = '\0'; *separator = '\0';
std::string subdir = path; std::string subdir = path;
subdir += "/tessdata"; subdir += "/tessdata";
if (_access(subdir.c_str(), 0) == 0) { if (std::filesystem::exists(subdir)) {
datadir = subdir; datadir = subdir;
} }
} }

View File

@ -9,13 +9,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#if defined(_WIN32)
# include <io.h> // for _access
#else
# include <unistd.h> // for access
#endif
#include <allheaders.h> #include <allheaders.h>
#include <tesseract/baseapi.h> #include <tesseract/baseapi.h>
#include <filesystem>
#include <string> #include <string>
#include "helpers.h" #include "helpers.h"
#include "include_gunit.h" #include "include_gunit.h"
@ -24,15 +20,6 @@
namespace tesseract { 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. // The fixture for testing Tesseract.
class PageSegModeTest : public testing::Test { class PageSegModeTest : public testing::Test {
protected: protected:
@ -86,7 +73,7 @@ protected:
// and differently to line and block mode. // and differently to line and block mode.
TEST_F(PageSegModeTest, WordTest) { TEST_F(PageSegModeTest, WordTest) {
std::string filename = file::JoinPath(TESTING_DIR, "segmodeimg.tif"); 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'; LOG(INFO) << "Skip test because of missing " << filename << '\n';
GTEST_SKIP(); GTEST_SKIP();
} else { } else {

View File

@ -9,12 +9,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#if defined(_WIN32) #include <filesystem>
# include <io.h> // for _access
#else
# include <unistd.h> // for access
#endif
#include "dawg.h" #include "dawg.h"
#include "include_gunit.h" #include "include_gunit.h"
#include "trie.h" #include "trie.h"
@ -23,15 +18,6 @@
namespace tesseract { 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 { class TatweelTest : public ::testing::Test {
protected: protected:
void SetUp() override { void SetUp() override {
@ -41,7 +27,7 @@ protected:
TatweelTest() { TatweelTest() {
std::string filename = TestDataNameToPath("ara.wordlist"); std::string filename = TestDataNameToPath("ara.wordlist");
if (file_exists(filename.c_str())) { if (std::filesystem::exists(filename)) {
std::string wordlist("\u0640"); std::string wordlist("\u0640");
CHECK_OK(file::GetContents(filename, &wordlist, file::Defaults())); CHECK_OK(file::GetContents(filename, &wordlist, file::Defaults()));
// Put all the unicodes in the unicharset_. // Put all the unicodes in the unicharset_.
@ -77,7 +63,7 @@ TEST_F(TatweelTest, DictIgnoresTatweel) {
// This test verifies that the dictionary ignores the Tatweel character. // This test verifies that the dictionary ignores the Tatweel character.
tesseract::Trie trie(tesseract::DAWG_TYPE_WORD, "ara", SYSTEM_DAWG_PERM, unicharset_.size(), 0); tesseract::Trie trie(tesseract::DAWG_TYPE_WORD, "ara", SYSTEM_DAWG_PERM, unicharset_.size(), 0);
std::string filename = TestDataNameToPath("ara.wordlist"); 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; LOG(INFO) << "Skip test because of missing " << filename;
GTEST_SKIP(); GTEST_SKIP();
} else { } else {
@ -91,7 +77,7 @@ TEST_F(TatweelTest, UnicharsetLoadKeepsTatweel) {
// This test verifies that a load of an existing unicharset keeps any // This test verifies that a load of an existing unicharset keeps any
// existing tatweel for backwards compatibility. // existing tatweel for backwards compatibility.
std::string filename = TestDataNameToPath("ara.unicharset"); 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; LOG(INFO) << "Skip test because of missing " << filename;
GTEST_SKIP(); GTEST_SKIP();
} else { } else {