Skip to content

Commit

Permalink
change the way to get the home directory. (Test: #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
pit-ray committed Feb 23, 2021
1 parent 6bd561b commit d34eab1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.0.0)
project(win-vind VERSION 3.2.1)
project(win-vind VERSION 3.2.2)

if(NOT BIT_TYPE)
set(BIT_TYPE 64)
Expand Down
4 changes: 2 additions & 2 deletions build_resources/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
IDI_ICON1 ICON DISCARDABLE "icon512.ico"

VS_VERSION_INFO VERSIONINFO
FILEVERSION 3,2,1
FILEVERSION 3,2,2
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
FILEFLAGS VS_FF_PRERELEASE
FILEOS VOS_NT_WINDOWS32
Expand All @@ -19,7 +19,7 @@ BEGIN
VALUE "InternalName", "win-vind (32-bit)\0"
VALUE "OriginalFilename", "win-vind.exe\0"
VALUE "ProductName", "win-vind (32bit)\0"
VALUE "ProductVersion", "3.2.1\0"
VALUE "ProductVersion", "3.2.2\0"
END
END
BLOCK "VarFileInfo"
Expand Down
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ add_library(Common STATIC
src/common/uia.cpp
src/common/vkc_converter.cpp
)
target_link_libraries(Common Userenv)
enable_cotire(Common)

add_library(CmnBindings STATIC
Expand Down
26 changes: 17 additions & 9 deletions core/include/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif

#include <windows.h>
#include <userenv.h>

#include "i_params.hpp"
#include "utility.hpp"
Expand All @@ -26,17 +27,24 @@ namespace Path
#endif
}

static inline const auto _get_home_path() {
WCHAR home_path[MAX_PATH] = {0} ;
if(!GetEnvironmentVariableW(L"HOMEPATH", home_path, MAX_PATH)) {
throw RUNTIME_EXCEPT("Cannot find %HOMEPATH% with GetEnviromentVariable.") ;
}
inline static const auto& HOME_PATH() {
static const auto obj = [] {
HANDLE token ;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) {
throw RUNTIME_EXCEPT("Could not open process token.") ;
}

return Utility::ws_to_s(home_path) + '\\' ;
}
WCHAR path[MAX_PATH] = {0} ;
DWORD size = MAX_PATH ;
if(!GetUserProfileDirectoryW(token, path, &size)) {
CloseHandle(token) ;
throw RUNTIME_EXCEPT("Could not get the home directory.") ;
}
CloseHandle(token) ;

return Utility::ws_to_s(path) + '\\' ;
} () ;

inline static const auto& HOME_PATH() {
static const auto obj = _get_home_path() ;
return obj ;
}

Expand Down
12 changes: 6 additions & 6 deletions core/src/msg_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ namespace Logger
static std::ofstream _init_msg_stream ;
static std::ofstream msg_stream ;

static const std::string log_dir = Path::ROOT_PATH() + "log\\" ;

inline static void remove_files_over(const std::string pattern_withex, const std::size_t num)
inline static void remove_files_over(const std::string& log_dir, const std::string pattern_withex, const std::size_t num)
{
std::vector<std::wstring> files ;

WIN32_FIND_DATAW wfd = {} ;
auto handle = FindFirstFileW(Utility::s_to_ws(pattern_withex).c_str(), &wfd) ;
auto handle = FindFirstFileW(Utility::s_to_ws(log_dir + pattern_withex).c_str(), &wfd) ;
if(handle == INVALID_HANDLE_VALUE) {
return ;
}
Expand All @@ -53,6 +51,8 @@ namespace Logger
}

void initialize() {
const std::string log_dir = Path::ROOT_PATH() + "log\\" ;

SYSTEMTIME stime ;
GetLocalTime(&stime) ;

Expand All @@ -77,8 +77,8 @@ namespace Logger
msg_stream.open(Path::to_u8path(mfile), std::ios::app) ;

//If the log files exists over five, remove old files.
remove_files_over(log_dir + "error_*.log", KEEPING_LOG_COUNT) ;
remove_files_over(log_dir + "message_*.log", KEEPING_LOG_COUNT) ;
remove_files_over(log_dir, "error_*.log", KEEPING_LOG_COUNT) ;
remove_files_over(log_dir, "message_*.log", KEEPING_LOG_COUNT) ;
}

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion wxgui/include/version.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef _VERSION_HPP
#define _VERSION_HPP

#define WIN_VIND_VERSION "3.2.1"
#define WIN_VIND_VERSION "3.2.2"

#endif
8 changes: 5 additions & 3 deletions wxgui/src/wxgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ namespace wxGUI
}) ;
}
catch(const std::exception& e) {
ERROR_PRINT(e.what()) ;
error_box(wxString::FromUTF8(e.what()) \
+ wxT(" Could not initialize win-vind, so terminate." \
+ wxT(" (Windows Error Code: ") + std::to_string(GetLastError()) + ")")) ;
}
return true ;
}
Expand Down Expand Up @@ -287,8 +289,8 @@ namespace wxGUI
}
}
catch(const std::exception& e) {
error_box(wxString(e.what()) \
+ wxT(". So could not create setting files of win-vind, so terminate." \
error_box(wxString::FromUTF8(e.what()) \
+ wxT(" Could not create setting files of win-vind, so terminate." \
+ wxT(" (Windows Error Code: ") + std::to_string(GetLastError()) + ")")) ;
return false ;
}
Expand Down

0 comments on commit d34eab1

Please sign in to comment.