📄 ueutil.cpp
字号:
// ueutil.cpp: ueutil 僋儔僗偺僀儞僾儕儊儞僥乕僔儑儞
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ueutil.h"
#include "conv.h"
bool ueutil::b_config;
char ueutil::filename_buf[MAX_PATH];
char ueutil::pPath[MAX_PATH];
static HANDLE hFiledebug;
/// added by uema2.
#define ZeroMemory(p, s) memset(p, 0, s)
#define IS_LEAP_YEAR(y) (((y) & 3) == 0)
#define BASE_DOW 4 // 1/1/1970 was a Thursday.
#define SECONDS_IN_A_DAY (24L * 60L * 60L) // Number of seconds in one day.
// Month to Year Day conversion array.
int M2YD[] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
};
// Month to Leap Year Day conversion array.
int M2LYD[] = {
0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366
};
char* ueutil::CharNext(char* lpsz)
{
if(*lpsz=='\0') return lpsz;
//俀僶僀僩暥帤偺愭摢偼僗僉僢僾
else if(_IsDBCSLeadByte((unsigned char)*lpsz) == 0){
//巜掕偝傟偨暥帤偑愭峴僶僀僩偱偼側偄応崌
if(*lpsz=='\r'){ ++lpsz; return ++lpsz; }
else return ++lpsz;
}else{
//巜掕偝傟偨暥帤偑愭峴僶僀僩偱偁傞応崌
if(*(lpsz+2)=='\0'){ ++lpsz; return ++lpsz;}
else{ lpsz++; lpsz++; return lpsz;}
}
}
///////////////////////////////////////////
BOOL ueutil::DosDateTimeToFileTime(
WORD wFatDate, // 16-bit MS-DOS date
WORD wFatTime, // 16-bit MS-DOS time
LPFILETIME lpFileTime // pointer to buffer for 64-bit file time
)
{
SYSTEMTIME sys;
BOOL bc;
// by uema2.
sys.wYear = ((wFatDate & 0xFE00)>>9);
sys.wYear += 1980;
sys.wMonth = (wFatDate & 0x01E0)>>5;
sys.wDay = wFatDate & 0x001F;
sys.wHour = (wFatTime & 0xF800)>>11;
sys.wMinute = (wFatTime & 0x07E0)>>5;
sys.wSecond = (short)((wFatTime & 0x001F)/2);
sys.wMilliseconds = 0;
bc = SystemTimeToFileTime(&sys, lpFileTime);
return bc;
}
//******************************************************************************
void SafeGetTimeZoneInformation(TIME_ZONE_INFORMATION *ptzi)
{
ZeroMemory(ptzi, sizeof(TIME_ZONE_INFORMATION));
// Ask the OS for the standard/daylight rules for the current time zone.
if ((GetTimeZoneInformation(ptzi) == 0xFFFFFFFF) ||
(ptzi->StandardDate.wMonth > 12) || (ptzi->DaylightDate.wMonth > 12))
{
// If the OS fails us, we default to the United States' rules.
ZeroMemory(ptzi, sizeof(TIME_ZONE_INFORMATION));
ptzi->StandardDate.wMonth = 10; // October
ptzi->StandardDate.wDay = 5; // Last Sunday (DOW == 0)
ptzi->StandardDate.wHour = 2; // At 2:00 AM
ptzi->DaylightBias = -60; // One hour difference
ptzi->DaylightDate.wMonth = 4; // April
ptzi->DaylightDate.wDay = 1; // First Sunday (DOW == 0)
ptzi->DaylightDate.wHour = 2; // At 2:00 AM
}
}
//******************************************************************************
time_t GetTransitionTimeT(TIME_ZONE_INFORMATION *ptzi, int year, BOOL fStartDST) {
// We only handle years within the range that time_t supports. We need to
// handle the very end of 1969 since the local time could be up to 13 hours
// into the previous year. In this case, our code will actually return a
// negative value, but it will be compared to another negative value and is
// handled correctly. The same goes for the 13 hours past a the max time_t
// value of 0x7FFFFFFF (in the year 2038). Again, these values are handled
// correctly as well.
if ((year < 1969) || (year > 2038)) {
return (time_t)0;
}
SYSTEMTIME *pst = fStartDST ? &ptzi->DaylightDate : &ptzi->StandardDate;
// WORD wYear Year (0000 == 0)
// WORD wMonth Month (January == 1)
// WORD wDayOfWeek Day of week (Sunday == 0)
// WORD wDay Month day (1 - 31)
// WORD wHour Hour (0 - 23)
// WORD wMinute Minute (0 - 59)
// WORD wSecond Second (0 - 59)
// WORD wMilliseconds Milliseconds (0 - 999)
// Compute the number of days since 1/1/1970 to the beginning of this year.
long daysToYear = ((year - 1970) * 365) // Tally up previous years.
+ ((year - 1969) >> 2); // Add few extra for the leap years.
// Compute the number of days since the beginning of this year to the
// beginning of the month. We will add to this value to get the actual
// year day.
long yearDay = IS_LEAP_YEAR(year) ? M2LYD[pst->wMonth - 1] :
M2YD [pst->wMonth - 1];
// Check for day-in-month format.
if (pst->wYear == 0) {
// Compute the week day for the first day of the month (Sunday == 0).
long monthDOW = (daysToYear + yearDay + BASE_DOW) % 7;
// Add the day offset of the transition day to the year day.
if (monthDOW < pst->wDayOfWeek) {
yearDay += (pst->wDayOfWeek - monthDOW) + (pst->wDay - 1) * 7;
} else {
yearDay += (pst->wDayOfWeek - monthDOW) + pst->wDay * 7;
}
// It is possible that we overshot the month, especially if pst->wDay
// is 5 (which means the last instance of the day in the month). Check
// if the year-day has exceeded the month and adjust accordingly.
if ((pst->wDay == 5) &&
(yearDay >= (IS_LEAP_YEAR(year) ? M2LYD[pst->wMonth] :
M2YD [pst->wMonth])))
{
yearDay -= 7;
}
// If not day-in-month format, then we assume an absolute date.
} else {
// Simply add the month day to the current year day.
yearDay += pst->wDay - 1;
}
// Tally up all our days, hours, minutes, and seconds since 1970.
long seconds = ((SECONDS_IN_A_DAY * (daysToYear + yearDay)) +
(3600L * (long)pst->wHour) +
(60L * (long)pst->wMinute) +
(long)pst->wSecond);
// If we are checking for the end of DST, then we need to add the DST bias
// since we are in DST when we chack this time stamp.
if (!fStartDST) {
seconds += ptzi->DaylightBias * 60L;
}
return (time_t)seconds;
}
BOOL IsDST(TIME_ZONE_INFORMATION *ptzi, time_t localTime) {
// If either of the months is 0, then this usually means that the time zone
// does not use DST. Unfortunately, Windows CE since it has a bug where it
// never really fills in these fields with the correct values, so it appears
// like we are never in DST. This is supposed to be fixed in future releases,
// so hopefully this code will get some use then.
if ((ptzi->StandardDate.wMonth == 0) || (ptzi->DaylightDate.wMonth == 0)) {
return FALSE;
}
// time_t is a 32-bit value for the seconds since January 1, 1970
// FILETIME is a 64-bit value for the number of 100-nanosecond intervals
// since January 1, 1601
// Compute the FILETIME for the given local time.
DWORDLONG dwl = ((DWORDLONG)116444736000000000 +
((DWORDLONG)localTime * (DWORDLONG)10000000));
FILETIME ft = *(FILETIME*)&dwl;
// Convert the FILETIME to a SYSTEMTIME.
SYSTEMTIME st;
ZeroMemory(&st, sizeof(st));
FileTimeToSystemTime(&ft, &st);
// Get our start and end daylight savings times.
time_t timeStart = GetTransitionTimeT(ptzi, (int)st.wYear, TRUE);
time_t timeEnd = GetTransitionTimeT(ptzi, (int)st.wYear, FALSE);
// Check what hemisphere we are in.
if (timeStart < timeEnd) {
// Northern hemisphere ordering.
return ((localTime >= timeStart) && (localTime < timeEnd));
} else if (timeStart > timeEnd) {
// Southern hemisphere ordering.
return ((localTime < timeEnd) || (localTime >= timeStart));
}
// If timeStart equals timeEnd then this time zone does not support DST.
return FALSE;
}
struct tm * ueutil::localtime(const time_t *timer) {
// Return value for localtime(). Source currently never references
// more than one "tm" at a time, so the single return structure is ok.
static struct tm g_tm;
ZeroMemory(&g_tm, sizeof(g_tm));
// Get our time zone information.
TIME_ZONE_INFORMATION tzi;
SafeGetTimeZoneInformation(&tzi);
// Create a time_t that has been corrected for our time zone.
time_t localTime = *timer - (tzi.Bias * 60L);
// Decide if value is in Daylight Savings Time.
if (g_tm.tm_isdst = (int)IsDST(&tzi, localTime)) {
localTime -= tzi.DaylightBias * 60L; // usually 60 minutes
} else {
localTime -= tzi.StandardBias * 60L; // usually 0 minutes
}
// time_t is a 32-bit value for the seconds since January 1, 1970
// FILETIME is a 64-bit value for the number of 100-nanosecond intervals
// since January 1, 1601
// Compute the FILETIME for the given local time.
DWORDLONG dwl = ((DWORDLONG)116444736000000000 +
((DWORDLONG)localTime * (DWORDLONG)10000000));
FILETIME ft = *(FILETIME*)&dwl;
// Convert the FILETIME to a SYSTEMTIME.
SYSTEMTIME st;
ZeroMemory(&st, sizeof(st));
FileTimeToSystemTime(&ft, &st);
// Finish filling in our "tm" structure.
g_tm.tm_sec = (int)st.wSecond;
g_tm.tm_min = (int)st.wMinute;
g_tm.tm_hour = (int)st.wHour;
g_tm.tm_mday = (int)st.wDay;
g_tm.tm_mon = (int)st.wMonth - 1;
g_tm.tm_year = (int)st.wYear - 1900;
return &g_tm;
}
char* ueutil::GetExtractPath()
{
HKEY hk ;
LONG lret ;
LPBYTE lpData ;
DWORD dwType, cbData ;
char *plm, lm[MAX_PATH];
TCHAR t_empty[MAX_PATH];
LPCTSTR value=t_empty;
//SK char path[MAX_PATH];
char* path;
/* 儗僕僗僩儕僉乕傪僆乕僾儞偡傞 */
lret = RegOpenKeyEx( HKEY_CURRENT_USER,
L"Software\\sk\\XacRettCE",
0,
KEY_QUERY_VALUE,
&hk ) ;
if ( lret != ERROR_SUCCESS ) {
return "\\My Documents";
}
dwType = REG_SZ ;
lpData = (LPBYTE) value ;
cbData = MAX_PATH * sizeof (*value) ;
lret = RegQueryValueEx( hk, L"ExtractPath", NULL, &dwType, lpData, &cbData ) ;
RegCloseKey( hk ) ;
if ( lret != ERROR_SUCCESS ) {
//幐攕偟偨傜儖乕僩僨傿儗僋僩儕傪曉偡
return "\\My Documents";
} else if ( lret == ERROR_MORE_DATA ) {
//僶僢僼傽偑懌傝側偄丠
return "\\My Documents";
}
plm=lm;
//SK
//DWORD charlength = _WideCharToMultiByte(CP_ACP, 0, (LPCTSTR)lpData,
// -1, NULL, 0, NULL, NULL);
//_WideCharToMultiByte(CP_ACP, 0, (LPCTSTR)lpData,
// -1, path, charlength, NULL, NULL);
path=tchar2char((unsigned short *)lpData); //SK
strcpy(plm,path);
path=plm;
// 僨傿儗僋僩儕偺帺摦惗惉偑ON丄偐偮愝掕夋柺偱側偗傟偽
// 僼傽僀儖柤傪偔偭偮偗傞
if(GetMakeDirectory() && !ueutil::b_config){
strcat(path, "\\");
strcat(path, filename_buf);
}
// return path;
// strcpy(pPath, path);
return path;
}
BOOL ueutil::SetExtractPath(LPCTSTR lpPath)
{
HKEY hk ;
LONG lret ;
LPBYTE lpData ;
DWORD ret, dwType, cbData ;
/* 儗僕僗僩儕僉乕傪嶌惉偡傞 */
lret = RegCreateKeyEx( HKEY_CURRENT_USER, L"Software\\sk\\XacRettCE", 0, TEXT(""), 0,
0, NULL, &hk, &ret ) ;
if ( lret != ERROR_SUCCESS ) {
return FALSE ;
}
// if ( *value ) {
dwType = REG_SZ ;
lpData = (LPBYTE) lpPath ;
cbData = (_tcslen(lpPath) + 1) * sizeof (*lpPath) ;
lret = RegSetValueEx( hk, L"ExtractPath", 0, dwType, lpData, cbData ) ;
// } else {
// lret = RegDeleteValue( hk, L"ExtractPath" ) ;
// }
RegCloseKey( hk ) ;
return lret == ERROR_SUCCESS ;
}
bool ueutil::GetMakeDirectory()
{
HKEY hKey;
LPBYTE lpData;
DWORD dwSize, dwValue, dwType;
LONG hRes;
hRes = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\sk\\XacRettCE",
0, KEY_QUERY_VALUE, &hKey);
if(hRes != ERROR_SUCCESS) return false;
dwType = REG_DWORD ;
lpData = (LPBYTE)&dwValue;
dwSize = sizeof(DWORD)+1;
hRes = RegQueryValueEx(hKey, L"MakeDir", 0, &dwType, lpData, &dwSize);
RegCloseKey(hKey);
if(hRes != ERROR_SUCCESS){
return false;
}
return dwValue==1L?true:false;
}
bool ueutil::SetMakeDirectory(bool b_ret)
{
HKEY hKey;
DWORD dwDisp, dwState;
LONG hRes;
hRes = RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\\sk\\XacRettCE",
0, L"", 0, 0, NULL, &hKey, &dwDisp);
if(hRes != ERROR_SUCCESS) return false;
dwState = b_ret==true?1:0;
hRes = RegSetValueEx(hKey, L"MakeDir", 0, REG_DWORD, (LPBYTE)&dwState, sizeof(DWORD)+1);
RegCloseKey(hKey);
return true;
}
bool ueutil::GetOpenDirectory()
{
HKEY hKey;
LPBYTE lpData;
DWORD dwSize, dwValue, dwType;
LONG hRes;
hRes = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\sk\\XacRettCE",
0, KEY_QUERY_VALUE, &hKey);
if(hRes != ERROR_SUCCESS) return false;
dwType = REG_DWORD ;
lpData = (LPBYTE)&dwValue;
dwSize = sizeof(DWORD)+1;
hRes = RegQueryValueEx(hKey, L"OpenDir", 0, &dwType, lpData, &dwSize);
RegCloseKey(hKey);
if(hRes != ERROR_SUCCESS){
return false;
}
return dwValue==1L?true:false;
}
bool ueutil::SetOpenDirectory(bool b_ret)
{
HKEY hKey;
DWORD dwDisp, dwState;
LONG hRes;
hRes = RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\\sk\\XacRettCE",
0, L"", 0, 0, NULL, &hKey, &dwDisp);
if(hRes != ERROR_SUCCESS) return false;
dwState = b_ret==true?1:0;
hRes = RegSetValueEx(hKey, L"OpenDir", 0, REG_DWORD, (LPBYTE)&dwState, sizeof(DWORD)+1);
RegCloseKey(hKey);
return true;
}
void ueutil::CopyToFilename(char* name)
{
strcpy(filename_buf, name);
}
//SK
char *tchar2char(TCHAR *str)
{
char *cchar;
int len;
len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
cchar = (char *)LocalAlloc(LPTR, len + 1);
if(cchar == NULL){
return NULL;
}
WideCharToMultiByte(CP_ACP, 0, str, -1, cchar, len, NULL, NULL);
return cchar;
}
TCHAR *char2tchar(char *str)
{
TCHAR *tchar;
int len;
len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
tchar = (TCHAR *)LocalAlloc(LPTR,sizeof(TCHAR) * (len + 1));
if(tchar == NULL){
return NULL;
}
MultiByteToWideChar(CP_ACP, 0, str, -1, tchar, len);
return tchar;
}
void fdinit(){
hFiledebug = CreateFile(TEXT("\\hackrett.txt"),
GENERIC_WRITE|GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
0,
NULL);
}
void fdclose(){
CloseHandle(hFiledebug);
}
void debugout(char *szText){
DWORD dwFileLen;
WriteFile(hFiledebug, szText, strlen(szText), &dwFileLen, 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -