Skip to content

Commit

Permalink
Improve CCUtil::main_setup (fixes issue #4230)
Browse files Browse the repository at this point in the history
Conda installations patch TESSDATA_PREFIX in the binary.
That does not work for std::string because the length
won't be patched, so use a normal C string which can be
patched.

Simplify also the code which checks the last character
of datadir.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil authored May 12, 2024
1 parent 6648d5b commit 6a31e36
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/ccutil/ccutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ CCUtil::~CCUtil() = default;
void CCUtil::main_setup(const std::string &argv0, const std::string &basename) {
imagebasename = basename; /**< name of image */

char *tessdata_prefix = getenv("TESSDATA_PREFIX");
const char *tessdata_prefix = getenv("TESSDATA_PREFIX");

if (!argv0.empty()) {
/* Use tessdata prefix from the command line. */
Expand Down Expand Up @@ -77,17 +77,20 @@ void CCUtil::main_setup(const std::string &argv0, const std::string &basename) {
if (datadir.empty()) {
#if defined(TESSDATA_PREFIX)
// Use tessdata prefix which was compiled in.
datadir = TESSDATA_PREFIX "/tessdata";
// Note that some software (for example conda) patches TESSDATA_PREFIX
// in the binary, so it should not be used directly with a std::string.
tessdata_prefix = TESSDATA_PREFIX;
datadir = tessdata_prefix;
datadir += "/tessdata/";
#else
datadir = "./";
#endif /* TESSDATA_PREFIX */
}

// check for missing directory separator
const char *lastchar = datadir.c_str();
lastchar += datadir.length() - 1;
if ((strcmp(lastchar, "/") != 0) && (strcmp(lastchar, "\\") != 0)) {
datadir += "/";
const char lastchar = datadir.back();
if (lastchar != '/' && lastchar != '\\') {
datadir += '/';
}
}

Expand Down

0 comments on commit 6a31e36

Please sign in to comment.