adapt to win32

This commit is contained in:
fanlumaster
2025-01-02 06:19:47 +08:00
parent 155d6eb410
commit 12abe936bf
13 changed files with 2864 additions and 2714 deletions

View File

@ -18,7 +18,11 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include "../src/include/dicttrie.h"
using namespace ime_pinyin;

0
command/lcompile.sh → command/scripts/lcompile.sh Executable file → Normal file
View File

0
llaunch.sh → command/scripts/llaunch.sh Executable file → Normal file
View File

0
lrun.sh → command/scripts/lrun.sh Executable file → Normal file
View File

26
scripts/lcompile.ps1 Normal file
View File

@ -0,0 +1,26 @@
# generate compile to exe files
$currentDirectory = Get-Location
$cmakeListsPath = Join-Path -Path $currentDirectory -ChildPath "CMakeLists.txt"
if (-not (Test-Path $cmakeListsPath))
{
Write-Host("No CMakeLists.txt in current directory, please check.")
return
}
Write-Host "Start generating and compiling..."
$buildFolderPath = ".\build"
if (-not (Test-Path $buildFolderPath))
{
New-Item -ItemType Directory -Path $buildFolderPath | Out-Null
Write-Host "build folder created."
}
cmake -G "Visual Studio 17 2022" -A x64 -S . -B ./build/
if ($LASTEXITCODE -eq 0)
{
cmake --build ./build/ --config DEBUG
}

0
lcompile.sh → scripts/lcompile.sh Executable file → Normal file
View File

45
scripts/llaunch.ps1 Normal file
View File

@ -0,0 +1,45 @@
#
# generate, compile and run exe files
#
function getExePathFromCMakeLists() {
$content = Get-Content -Raw -Path "./CMakeLists.txt"
$exePath = ""
foreach ($line in $content -split "`n") {
if ($line -match 'set\(MY_EXECUTABLE_NAME[^\"]*\"([^\"]+)\"') {
$exeName = $matches[1]
$exePath = "./build/bin/Debug/$exeName" + ".exe"
break
}
}
return $exePath
}
$currentDirectory = Get-Location
$cmakeListsPath = Join-Path -Path $currentDirectory -ChildPath "CMakeLists.txt"
if (-not (Test-Path $cmakeListsPath)) {
Write-Host("No CMakeLists.txt in current directory, please check.")
return
}
Write-Host "Start generating and compiling..."
$buildFolderPath = ".\build"
if (-not (Test-Path $buildFolderPath)) {
New-Item -ItemType Directory -Path $buildFolderPath | Out-Null
Write-Host "build folder created."
}
cmake -G "Visual Studio 17 2022" -A x64 -S . -B ./build/
if ($LASTEXITCODE -eq 0) {
cmake --build ./build/ --config DEBUG
if ($LASTEXITCODE -eq 0) {
$exePath = getExePathFromCMakeLists
Write-Host "start running as follows..."
Write-Host "=================================================="
Invoke-Expression $exePath
}
}

0
command/llaunch.sh → scripts/llaunch.sh Executable file → Normal file
View File

20
scripts/lrun.ps1 Normal file
View File

@ -0,0 +1,20 @@
#
# run exe file that has already been compiled before
#
function getExePathFromCMakeLists() {
$content = Get-Content -Raw -Path "./CMakeLists.txt"
$exePath = ""
foreach ($line in $content -split "`n") {
if ($line -match 'set\(MY_EXECUTABLE_NAME[^\"]*\"([^\"]+)\"') {
$exeName = $matches[1]
$exePath = "./build/bin/Debug/$exeName" + ".exe"
break
}
}
return $exePath
}
$exePath = getExePathFromCMakeLists
#Write-Host "start running as follows..."
#Write-Host "=================================================="
Invoke-Expression $exePath

0
command/lrun.sh → scripts/lrun.sh Executable file → Normal file
View File

View File

@ -24,7 +24,13 @@
// Debug performance for operations
// #define ___DEBUG_PERF___
#ifdef _WIN32
#include <time.h>
#include <winsock.h> // timeval
#else
#include <pthread.h>
#include <sys/time.h>
#endif
#include "atomdictbase.h"
namespace ime_pinyin {

View File

@ -20,19 +20,56 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#ifdef _WIN32
#undef max
#undef min
#include <windows.h>
#else
#include <pthread.h>
#endif
#include <math.h>
namespace ime_pinyin {
#ifdef _WIN32
static int gettimeofday(struct timeval *tp, void *) {
if (!tp) {
return -1;
}
// 获取当前时间的 FILETIME
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
// 将 FILETIME 转换为 1970 年以来的时间Unix 时间)
ULARGE_INTEGER ull;
ull.LowPart = ft.dwLowDateTime;
ull.HighPart = ft.dwHighDateTime;
// FILETIME 是从 1601 年 1 月 1 日开始的 100 纳秒单位时间
const long long WINDOWS_TICKS_PER_SEC = 10000000LL; // 每秒 1e7 个 100 纳秒单位
const long long EPOCH_DIFFERENCE = 11644473600LL; // Unix epoch 到 Windows epoch 的秒数差
long long unix_time_in_microseconds = ull.QuadPart / 10 - EPOCH_DIFFERENCE * 1000000LL;
// 填充 timeval 结构
tp->tv_sec = (long)(unix_time_in_microseconds / 1000000LL);
tp->tv_usec = (long)(unix_time_in_microseconds % 1000000LL);
return 0;
}
#endif
#ifdef ___DEBUG_PERF___
static uint64 _ellapse_ = 0;
static struct timeval _tv_start_, _tv_end_;
@ -53,7 +90,15 @@ static struct timeval _tv_start_, _tv_end_;
#endif
// XXX File load and write are thread-safe by g_mutex_
#ifdef _WIN32
static CRITICAL_SECTION g_mutex_; // 使用 Windows 的临界区对象
#define pthread_mutex_lock(MUTEX) EnterCriticalSection(MUTEX)
#define pthread_mutex_unlock(MUTEX) LeaveCriticalSection(MUTEX)
#define pthread_mutex_trylock(MUTEX) (TryEnterCriticalSection(MUTEX) != 0)
#else
#include <pthread.h>
static pthread_mutex_t g_mutex_ = PTHREAD_MUTEX_INITIALIZER;
#endif
static struct timeval g_last_update_ = {0, 0};
inline uint32 UserDict::get_dict_file_size(UserDictInfo *info) {
@ -264,6 +309,7 @@ bool UserDict::load_dict(const char *file_name, LemmaIdType start_id, LemmaIdTyp
return true;
error:
free((void *)dict_file_);
dict_file_ = NULL;
start_id_ = 0;
return false;
}
@ -900,6 +946,7 @@ bool UserDict::remove_lemma(LemmaIdType lemma_id) {
void UserDict::flush_cache() {
LemmaIdType start_id = start_id_;
if (!dict_file_) return;
const char *file = strdup(dict_file_);
if (!file) return;
close_dict();
@ -1164,7 +1211,9 @@ void UserDict::write_back() {
// It seems truncate is not need on Linux, Windows except Mac
// I am doing it here anyway for safety.
off_t cur = lseek(fd, 0, SEEK_CUR);
#ifndef _WIN32
ftruncate(fd, cur);
#endif
close(fd);
state_ = USER_DICT_SYNC;
}