Skip to content

Commit

Permalink
On Linux look in both places for words file
Browse files Browse the repository at this point in the history
  • Loading branch information
dmdmdm authored Dec 20, 2024
1 parent b75fef0 commit 4752659
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#include "windows.h"
#else
// Linux
static const char *main_dict = "/usr/share/dict/words";
static const char *main_dict1 = "/usr/share/dict/words";
static const char *main_dict2 = "/usr/share/words";
#endif

typedef std::unordered_map<std::string, bool> DICT;
Expand Down Expand Up @@ -68,36 +69,40 @@ static bool IsExists(const char *file)

static void LoadDict()
{
char buf[MAX_PATH] = "";
char local_words[MAX_PATH] = "";

#ifdef _WIN32
// Windows
LPSTR p;

GetModuleFileName(NULL, buf, sizeof(buf));
if ((p = strrchr(buf, '\\')) == NULL)
{
p = &buf[lstrlen(buf) - 1]; // Last char
GetModuleFileName(NULL, local_words, sizeof(local_words));
if ((p = strrchr(local_words, '\\')) == NULL) {
p = &local_words[lstrlen(local_words) - 1]; // Last char
}
lstrcpyn(p + 1, "words.txt", sizeof(buf));
lstrcpyn(p + 1, "words.txt", sizeof(local_words));

if (IsExists(buf))
{
LoadDict(buf);
if (IsExists(local_words)) {
LoadDict(local_words);
}

#else
// Unix
LoadDict(main_dict);
snprintf(buf, sizeof(buf), "%s/words", getenv("HOME"));
if (IsExists(main_dict1)) {
LoadDict(main_dict1);
}
else if (IsExists(main_dict2)) {
LoadDict(main_dict2);
}
else {
fprintf(stderr, "Could not load %s or %s\n", main_dict1, main_dict2);
}

if (IsExists(buf))
{
LoadDict(buf);
snprintf(local_words, sizeof(local_words), "%s/words", getenv("HOME"));
if (IsExists(local_words)) {
LoadDict(local_words);
}

if (IsExists("words"))
{
if (IsExists("words")) {
LoadDict("words");
}
#endif
Expand Down

0 comments on commit 4752659

Please sign in to comment.