Skip to content

Commit

Permalink
remove hard code
Browse files Browse the repository at this point in the history
  • Loading branch information
fanlumaster committed Feb 12, 2025
1 parent 468cac1 commit c829d53
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 51 deletions.
1 change: 1 addition & 0 deletions .clangd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CompileFlags:
"-IC:\\Users\\SonnyCalcr\\EDisk\\CppCodes\\IMECodes\\DeerWritingBrush\\src\\Register",
"-IC:\\Users\\SonnyCalcr\\EDisk\\CppCodes\\IMECodes\\DeerWritingBrush\\src\\Tf",
"-IC:\\Users\\SonnyCalcr\\EDisk\\CppCodes\\IMECodes\\DeerWritingBrush\\src\\Thread",
"-IC:\\Users\\SonnyCalcr\\EDisk\\CppCodes\\IMECodes\\DeerWritingBrush\\src\\Utils",
"-IC:\\Users\\SonnyCalcr\\EDisk\\CppCodes\\IMECodes\\DeerWritingBrush\\src\\Window",
"-IC:\\Users\\SonnyCalcr\\EDisk\\CppCodes\\IMECodes\\DeerWritingBrush\\sqlite",
-DUNICODE,
Expand Down
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ add_definitions(
)

set(SQLITE_INCLUDE_DIR "./sqlite")
set(BOOST_INCLUDE_DIR "C:\\Users\\SonnyCalcr\\scoop\\apps\\boost\\1.87.0")
set(USERNAME "$ENV{USERNAME}")
set(BOOST_VERSION "1.87.0")
set(BOOST_INCLUDE_DIR "C:/Users/${USERNAME}/scoop/apps/boost/${BOOST_VERSION}")

include_directories("./src")
include_directories("./src/Candidate")
Expand Down Expand Up @@ -127,6 +129,9 @@ add_library(DeerWritingBrush SHARED
# Thread
"./src/Thread/ThreadFocusSink.cpp"
"./src/Thread/ThreadMgrEventSink.cpp"
# Utils
"./src/Utils/FanyUtils.cpp"
"./src/Utils/FanyUtils.h"
# Window
"./src/Window/BaseWindow.cpp"
"./src/Window/BaseWindow.h"
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## What is FanIME

Based on Microsoft [TSF IME Demo](https://github.com/microsoft/Windows-classic-samples/tree/main/Samples/IME/cpp/SampleIME)
Based on Microsoft [TSF IME Demo](https://github.com/microsoft/Windows-classic-samples/tree/main/Samples/IME/cpp/SampleIME)

Mainly to develop a Chinese Input Method Editor without ads and likely some other disturbing things we users hate really.

Expand All @@ -10,13 +10,21 @@ The roadmap is as follows.

### How to build

Run the following command,
Before we start, make sure you have already installed boost,

```powershell
scoop install boost
```

Then, replace boost version in `./CMakeLists.txt`,

And thne, run the following command,

```powershell
./lcompile.ps1
```

we also need to build one 32-bit version,
We also need to build one 32-bit version,

```powershell
./lcompile.ps1 32
Expand Down
14 changes: 0 additions & 14 deletions src/DllMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@
//
//----------------------------------------------------------------------------

#include <iostream>
#include <fstream>

void RedirectWcoutToFile(const std::wstring& filename) {
static std::wofstream file;
file.open(filename);
if (file.is_open()) {
std::wcout.rdbuf(file.rdbuf()); // 将 wcout 的缓冲区绑定到文件流的缓冲区
} else {
std::wcerr << L"Failed to open log file" << std::endl;
}
}

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID pvReserved)
{
pvReserved; // 空操作,用于消除某些编译器的警告
Expand All @@ -35,7 +22,6 @@ BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID pvReserved)
{
case DLL_PROCESS_ATTACH: // dll 被加载时执行

// RedirectWcoutToFile(L"C:/Users/SonnyCalcr/AppData/Local/DeerWritingBrush/fanydebug.log");
Global::dllInstanceHandle = hInstance; // 将 dll 实例句柄存储在全局变量中

// 初始化一个临界区,用于多线程同步
Expand Down
49 changes: 16 additions & 33 deletions src/Global/Globals.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved

#include "Globals.h"
#include "Private.h"
#include "resource.h"
Expand All @@ -15,6 +8,7 @@
#include <fstream>
#include <string>
#include <ctime>
#include "FanyUtils.h"

namespace Global
{
Expand Down Expand Up @@ -401,56 +395,48 @@ BOOL CompareElements(LCID locale, const CStringRange *pElement1, const CStringRa

void LogMessage(const char *message)
{
// 获取当前时间
// Get local time
std::time_t now = std::time(nullptr);
std::tm *localTime = std::localtime(&now);

// 格式化时间
// format time
char timeBuffer[80];
std::strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %H:%M:%S", localTime);

// 打开日志文件
std::ofstream logFile("C:/Users/SonnyCalcr/AppData/Local/DeerWritingBrush/log/fanydebug.log",
std::ios_base::app); // 以追加模式打开
std::ofstream logFile(FanyUtuils::GetLogFilePath(), std::ios_base::app); // Append mode

if (logFile.is_open())
{
logFile << "[" << timeBuffer << "] " << message << std::endl; // 写入日志
logFile.close(); // 关闭文件
logFile << "[" << timeBuffer << "] " << message << std::endl; // Write to log
logFile.close();
}
}

void LogMessageW(const wchar_t *message)
{
// 获取当前时间
std::time_t now = std::time(nullptr);
std::tm *localTime = std::localtime(&now);

// 格式化时间
wchar_t timeBuffer[80];
wcsftime(timeBuffer, sizeof(timeBuffer) / sizeof(wchar_t), L"%Y-%m-%d %H:%M:%S", localTime);

// 打开日志文件
std::wofstream logFile(L"C:/Users/SonnyCalcr/AppData/Local/DeerWritingBrush/log/fanydebug_w.log",
std::ios_base::app); // 以追加模式打开
std::wofstream logFile(FanyUtuils::GetLogFilePathW(), std::ios_base::app);
if (logFile.is_open())
{
logFile.imbue(std::locale("Chinese_China.65001"));
logFile << L"[" << timeBuffer << L"] " << message;
logFile << std::endl; // 写入日志
logFile.close(); // 关闭文件
logFile << std::endl;
logFile.close();
}
}

// 写入 pwch 和 dwLength 到日志文件
// Write wide string and its length to log
void LogWideString(const WCHAR *pwch, DWORD_PTR dwLength)
{
// 将 pwch 和 dwLength 格式化为日志消息
std::wstring logMessage = L"pwch: ";
logMessage.append(pwch, dwLength); // 添加 pwch 的内容
logMessage.append(pwch, dwLength);
logMessage += L", dwLength: ";
logMessage += std::to_wstring(dwLength); // 添加 dwLength 的值
logMessage += std::to_wstring(dwLength);

// 调用日志函数写入文件
LogMessageW(logMessage.c_str());
}

Expand All @@ -459,17 +445,17 @@ std::wstring string_to_wstring(const std::string &str)
if (str.empty())
return L"";

// 获取所需缓冲区大小
// Get buffer size needed
int size_needed = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), nullptr, 0);
if (size_needed <= 0)
{
throw std::runtime_error("MultiByteToWideChar failed");
}

// 分配缓冲区
// allocate size for buffer
std::wstring wstr(size_needed, 0);

// 执行转换
// Do convert here
int result = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), &wstr[0], size_needed);
if (result <= 0)
{
Expand All @@ -484,17 +470,14 @@ std::string wstring_to_string(const std::wstring &wstr)
if (wstr.empty())
return "";

// 获取所需缓冲区大小
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), nullptr, 0, nullptr, nullptr);
if (size_needed <= 0)
{
throw std::runtime_error("WideCharToMultiByte failed");
}

// 分配缓冲区
std::string str(size_needed, 0);

// 执行转换
int result =
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), &str[0], size_needed, nullptr, nullptr);
if (result <= 0)
Expand Down
4 changes: 4 additions & 0 deletions src/Header/Define.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
#pragma once
#include "resource.h"

#define IME_NAME L"DeerWritingBrush"

#define TEXTSERVICE_MODEL L"Apartment"
#define TEXTSERVICE_LANGID MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED)
#define TEXTSERVICE_ICON_INDEX -IDIS_SAMPLEIME
#define TEXTSERVICE_DIC L"SampleIMESimplifiedQuanPin.txt"
#define TEXTSERVICE_DIC_DB L"cutted_flyciku_with_jp.db"
#define FANYLOGFILE_ "fanydebug.log"
#define FANYLOGFILE L"fanydebug_w.log"

#define IME_MODE_ON_ICON_INDEX IDI_IME_MODE_ON
#define IME_MODE_OFF_ICON_INDEX IDI_IME_MODE_OFF
Expand Down
26 changes: 26 additions & 0 deletions src/Utils/FanyUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "FanyUtils.h"
#include "Define.h"
#include "Globals.h"

namespace FanyUtuils
{
std::string GetIMEDataDirPath()
{
const char *localAppDataPath = std::getenv("LOCALAPPDATA");
std::string IMEDataPath = std::string(localAppDataPath) + "\\" + Global::wstring_to_string(std::wstring(IME_NAME));
return IMEDataPath;
}

std::string GetLogFilePath()
{
const char *localAppDataPath = std::getenv("LOCALAPPDATA");
std::string logPath = GetIMEDataDirPath() + "\\log\\" + FANYLOGFILE_;
return logPath;
}

std::wstring GetLogFilePathW()
{
std::wstring logPathW = Global::string_to_wstring(GetIMEDataDirPath()) + L"\\log\\" + FANYLOGFILE;
return logPathW;
}
} // namespace FanyUtuils
8 changes: 8 additions & 0 deletions src/Utils/FanyUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <string>

namespace FanyUtuils
{
std::string GetIMEDataDirPath();
std::string GetLogFilePath();
std::wstring GetLogFilePathW();
} // namespace FanyUtuils

0 comments on commit c829d53

Please sign in to comment.