Skip to content

Commit 450efa6

Browse files
committed
Get tessdata prefix from executable path (only for Windows)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent d4e0c64 commit 450efa6

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

ccutil/mainblk.cpp

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**********************************************************************
22
* File: mainblk.c (Formerly main.c)
33
* Description: Function to call from main() to setup.
4-
* Author: Ray Smith
5-
* Created: Tue Oct 22 11:09:40 BST 1991
4+
* Author: Ray Smith
5+
* Created: Tue Oct 22 11:09:40 BST 1991
66
*
77
* (C) Copyright 1991, Hewlett-Packard Ltd.
88
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -47,26 +47,40 @@ namespace tesseract {
4747
* @param argv0 - paths to the directory with language files and config files.
4848
* An actual value of argv0 is used if not NULL, otherwise TESSDATA_PREFIX is
4949
* used if not NULL, next try to use compiled in -DTESSDATA_PREFIX. If previous
50-
* is not sucessul - use current directory.
50+
* is not successful - use current directory.
5151
* @param basename - name of image
5252
*/
5353
void CCUtil::main_setup(const char *argv0, const char *basename) {
5454
imagebasename = basename; /**< name of image */
5555

56+
char *tessdata_prefix = getenv("TESSDATA_PREFIX");
57+
5658
if (argv0 != NULL) {
59+
/* Use tessdata prefix from the command line. */
5760
datadir = argv0;
61+
} else if (tessdata_prefix) {
62+
/* Use tessdata prefix from the environment. */
63+
datadir = tessdata_prefix;
64+
#if defined(_WIN32)
65+
} else if (datadir == NULL || access(datadir.string(), 0) != 0) {
66+
/* Look for tessdata in directory of executable. */
67+
static char dir[128];
68+
static char exe[128];
69+
DWORD length = GetModuleFileName(NULL, exe, sizeof(exe));
70+
if (length > 0 && length < sizeof(exe)) {
71+
_splitpath(exe, NULL, dir, NULL, NULL);
72+
datadir = dir;
73+
}
74+
#endif /* _WIN32 */
75+
#if defined(TESSDATA_PREFIX)
5876
} else {
59-
if (getenv("TESSDATA_PREFIX")) {
60-
datadir = getenv("TESSDATA_PREFIX");
61-
} else {
62-
#ifdef TESSDATA_PREFIX
77+
/* Use tessdata prefix which was compiled in. */
6378
#define _STR(a) #a
6479
#define _XSTR(a) _STR(a)
6580
datadir = _XSTR(TESSDATA_PREFIX);
6681
#undef _XSTR
6782
#undef _STR
6883
#endif
69-
}
7084
}
7185

7286
// datadir may still be empty:

0 commit comments

Comments
 (0)