From c57be6c315a6fa8f0df621b7ff3c7be0e70aa693 Mon Sep 17 00:00:00 2001 From: BensonLaur <1035069088@qq.com> Date: Mon, 10 Jul 2017 14:40:50 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=9C=A8=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E7=A8=8B=E5=BA=8F15=E7=A7=92=E5=90=8E=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BesLyric version log.txt | 5 - BesLyric/AutoUpdateThread.cpp | 253 ++++++++++++++++++++++++++++ BesLyric/AutoUpdateThread.h | 47 ++++++ BesLyric/BSMessageBox.cpp | 9 + BesLyric/BSMessageBox.h | 6 +- BesLyric/BesLyric.aps | Bin 2672 -> 2672 bytes BesLyric/BesLyric.rc | Bin 5068 -> 5068 bytes BesLyric/BesLyric.vcxproj | 5 + BesLyric/BesLyric.vcxproj.filters | 15 ++ BesLyric/Define.h | 14 +- BesLyric/MainDlg.cpp | Bin 67508 -> 67464 bytes BesLyric/MainDlg.h | Bin 12522 -> 12298 bytes BesLyric/SSingleton.h | Bin 0 -> 2486 bytes BesLyric/WinFile.cpp | 268 ++++++++++++++++++++++++++++++ BesLyric/WinFile.h | 68 ++++++++ BesLyric/res/resource.h | Bin 27256 -> 27256 bytes versionLog.txt | 8 + 17 files changed, 691 insertions(+), 7 deletions(-) delete mode 100644 BesLyric version log.txt create mode 100644 BesLyric/AutoUpdateThread.cpp create mode 100644 BesLyric/AutoUpdateThread.h create mode 100644 BesLyric/BSMessageBox.cpp create mode 100644 BesLyric/SSingleton.h create mode 100644 BesLyric/WinFile.cpp create mode 100644 BesLyric/WinFile.h create mode 100644 versionLog.txt diff --git a/BesLyric version log.txt b/BesLyric version log.txt deleted file mode 100644 index 5984774..0000000 --- a/BesLyric version log.txt +++ /dev/null @@ -1,5 +0,0 @@ -版本号:1.0.1 -1、歌词原文件在原来支持 ascii 码的基础上,支持windows文本另外3种编码:Unicode 、Unicode little endian 、UTF-8 - -版本号:1.0.2 -1、添加设置页面,在设置页面可以设置 插入歌词时间时提前的毫秒数 \ No newline at end of file diff --git a/BesLyric/AutoUpdateThread.cpp b/BesLyric/AutoUpdateThread.cpp new file mode 100644 index 0000000..860f6a0 --- /dev/null +++ b/BesLyric/AutoUpdateThread.cpp @@ -0,0 +1,253 @@ +#include "stdafx.h" +#include "AutoUpdateThread.h" + +#include //链接网络 +#pragma comment( lib, "wininet.lib" ) +#define MAXBLOCKSIZE 1024 + +using namespace SOUI; +extern int _MessageBox(HWND hwnd,LPCTSTR content, LPCTSTR tiltle, UINT uType); + +AutoUpdateThread* AutoUpdateThread::ms_Singleton = NULL; + +//开始线程 +bool AutoUpdateThread::Start() +{ +#ifdef _DEBUG + //调试模式不记录登录信息 + m_bFirstEnter = false; +#endif + + if(m_bFirstEnter) + { + //发送登录信息记录 + SendLoginInfo(); + + m_bFirstEnter = false; + } + + //启动线程 + if(m_handleThread != NULL) + return true; + + m_handleThread = ::CreateThread(NULL, 0, ThreadProc, this, 0 ,NULL); + + return m_handleThread != NULL; +} + + +//挂起线程 +DWORD AutoUpdateThread::Pause() +{ + return ::SuspendThread(m_handleThread); +} + +//恢复线程 +DWORD AutoUpdateThread::Resume() +{ + return ::ResumeThread(m_handleThread); +} + +//停止线程 +void AutoUpdateThread::Stop() +{ + if(m_handleThread == NULL) + return; + + m_bKeepUpdate = false; + + + ::WaitForSingleObject(m_handleThread, INFINITE); + + CloseHandle(m_handleThread); +} + +//线程执行地址 +DWORD WINAPI AutoUpdateThread::ThreadProc(LPVOID pParam) +{ + AutoUpdateThread* pThread = static_cast(pParam); + + //启动之后15秒才开始更新。 + //发布新版本时,即在Realease 下,会自动更新,为了编译后不立刻更新导致版本被自动被替换成较低版本,设置15秒延时,在15秒内关闭则不会被自动替换 + Sleep(15000); + +#if _KEEP_UPDATE == 0 + pThread->m_bKeepUpdate = false; +#endif + + while(pThread->m_bKeepUpdate) + { + //如果还没更新 + if(!pThread->m_bHasUpdate) + pThread->AutoUpdate(); + + Sleep(pThread->m_nDelay); + } + + return 0; +} + +//自动更新执行函数 +bool AutoUpdateThread::AutoUpdate() +{ + bool bRet; + string strVersionFile = FileHelper::GetCurrentDirectoryStr()+ "version"; + string strLastExe = FileHelper::GetCurrentDirectoryStr()+ "BesLyric"; + + /*下载最新版本配置信息 */ + bRet = DownloadFile(L"http://files.cnblogs.com/files/BensonLaur/lastVersion.zip", wstring(S_CA2W( SStringA(strVersionFile.c_str()).GetBuffer(1) ))); + if(bRet == false) + return false; + + SStringW strVersion = L""; + vector vecLineW; + FileOperator::ReadAllLinesW(strVersionFile, &vecLineW); + + if(vecLineW.begin() != vecLineW.end()) + strVersion = * vecLineW.begin(); + + //识别文件不存在情况 + if(vecLineW.size() >= 2) + { + SStringW secondLine = *(vecLineW.begin()+1); + if(secondLine.GetLength()!=0) + { + if(secondLine.GetLength() == 6 && secondLine == L"") + { + //为页面不存在的提示,即版本文件不存在,设置为已更新 + m_bHasUpdate = true; + + return false; + } + } + } + + if(VERSION_NUMBER == strVersion) // 版本一致 + { + m_bHasUpdate = true; + return true; + } + + /*有新版本,则直接下载最新的版本 执行文件 */ + //下载新的版本日志文件 versionLog.txt (服务器的名称为 versionLog.zip) + string strVersionLog = FileHelper::GetCurrentDirectoryStr()+ "versionLog.txt"; + if(FileHelper::CheckFileExist(strVersionLog)) + remove(strVersionLog.c_str()); + bRet = DownloadFile(L"http://files.cnblogs.com/files/BensonLaur/versionLog.zip",wstring(S_CA2W( SStringA(strVersionLog.c_str()).GetBuffer(1) ))); + if(bRet == false) + return false; + + //下载最新的执行文件 BesLyric.exe(服务器的名称为 BesLyricExe.zip) 到 strLastExe (BesLyric)中 + bRet = DownloadFile(L"http://files.cnblogs.com/files/BensonLaur/BesLyricExe.zip",wstring(S_CA2W( SStringA(strLastExe.c_str()).GetBuffer(1) ))); + if(bRet == false) + return false; + + /*修改文件名称,达到替换旧版本目的 */ + string strCurrentExe = strLastExe + ".exe"; + string strBackupExe = strLastExe+ "."+ S_CW2A(VERSION_NUMBER).GetBuffer(1) ;//+ ".exe"; + + if(FileHelper::CheckFileExist(strBackupExe))//删除可能存在的备份文件 + remove(strBackupExe.c_str()); + rename(strCurrentExe.c_str(),strBackupExe.c_str()); + rename(strLastExe.c_str(),strCurrentExe.c_str()); + + + /*发送消息,提示用户重启以使用新版本 */ + //获取更新的内容记录 + SStringW strUpdateInfo = L""; + if(vecLineW.size() == 1 || vecLineW.size() == 2) + strUpdateInfo = L"更新信息不见了!不要在意这些细节 :)"; + else + { + int nLine = 0; + for(auto iter = vecLineW.begin(); iter!= vecLineW.end(); iter++,nLine++) + { + if(nLine >= 2) + { + strUpdateInfo += *iter; + strUpdateInfo += L"\\n"; + } + } + } + + SStringW strShownInfo = L"软件有更新,更新将在下一次启动生效 :) \\n\\n最新版本更新内容:\\n"; + strShownInfo += strUpdateInfo; + _MessageBox(NULL, strShownInfo, _T("软件更新提示"),MB_OK| MB_ICONINFORMATION); + + + + //设置为已更新 + m_bHasUpdate = true; + + return true; +} + +/*将Url指向的地址的文件下载到save_as指向的本地文件*/ +bool AutoUpdateThread::DownloadFile(const wstring strUrl, const wstring strSaveAs) +{ + byte Temp[MAXBLOCKSIZE]; + ULONG Number = 1; + + FILE *stream; + HINTERNET hSession = InternetOpen(L"RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); + if (hSession != NULL) + { + HINTERNET handle2 = InternetOpenUrl(hSession, strUrl.c_str(), NULL, 0, INTERNET_FLAG_DONT_CACHE, 0); + if (handle2 != NULL) + { + if ((_wfopen_s(&stream, strSaveAs.c_str(), L"wb")) == 0) + { + while (Number > 0) + { + InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number); + + fwrite(Temp, sizeof (char), Number, stream); + } + fclose(stream); + } + else + return false; + + InternetCloseHandle(handle2); + handle2 = NULL; + } + else + return false; + InternetCloseHandle(hSession); + hSession = NULL; + } + + return true; +} + + +//发送登录信息(ip地址) +void AutoUpdateThread::SendLoginInfo() +{ + //获得ip地址 + //DownloadFile(L"https://whatismyipaddress.com/",L"E://git//BesLyric//Release//ip.txt"); + //DownloadFile(L"http://ip.qq.com/",L"E://git//BesLyric//Release//ip.txt"); + + string strTempFile = FileHelper::GetCurrentDirectoryStr()+ "temp"; + if(FileHelper::CheckFileExist(strTempFile)) + remove(strTempFile.c_str()); + DownloadFile(L"http://www.ip138.com/ip2city.asp",wstring(S_CA2W( SStringA(strTempFile.c_str()).GetBuffer(1) ))); + + string strIP=""; + vector vecLine; + FileOperator::ReadAllLines(strTempFile,&vecLine); + for(auto iter = vecLine.begin(); iter != vecLine.end(); iter++) + { + if(iter->find_first_of('[') != string::npos) + { + auto beg = iter->find_first_of('[')+1; + auto end = iter->find_first_of(']'); + strIP = iter->substr(beg, end - beg); + break; + } + } + + //访问链接,服务端负责记录登录信息 + string strSendLink = "http://bensonlaur.vicp.io/BesBlog/beslyric/login.action?ip=" + strIP; + DownloadFile(wstring(S_CA2W( SStringA(strSendLink.c_str()).GetBuffer(1) )),wstring(S_CA2W( SStringA(strTempFile.c_str()).GetBuffer(1) ))); +} diff --git a/BesLyric/AutoUpdateThread.h b/BesLyric/AutoUpdateThread.h new file mode 100644 index 0000000..b2eacda --- /dev/null +++ b/BesLyric/AutoUpdateThread.h @@ -0,0 +1,47 @@ +#pragma once +#include "stdafx.h" +#include "WinFile.h" +#include "SSingleton.h" +using namespace SOUI; + + +class AutoUpdateThread : public Singleton +{ +public: + AutoUpdateThread(int nDelay = 1000):m_nDelay(nDelay),m_handleThread(NULL),m_bKeepUpdate(true),m_bHasUpdate(false),m_bFirstEnter(true){} + + //开始线程 + bool Start(); + + //挂起线程 + DWORD Pause(); + + //恢复线程 + DWORD Resume(); + + //停止线程 + void Stop(); + +private: + + //线程执行地址 + static DWORD WINAPI ThreadProc(LPVOID pParam); + + //自动更新执行函数 + bool AutoUpdate(); + + //从网络下载文件 + bool DownloadFile(const wstring strUrl, const wstring strSaveAs); + + //发送登录信息(ip地址) + void SendLoginInfo(); + + bool m_bFirstEnter; /* 第一次启动线程 */ +private: + + HANDLE m_handleThread; /* 当前线程句柄 */ + bool m_bKeepUpdate; /* 保持更新标志 */ + bool m_bHasUpdate; /* 是否已经更新 */ + int m_nDelay; /* 检查更新的延时,单位ms */ +}; + diff --git a/BesLyric/BSMessageBox.cpp b/BesLyric/BSMessageBox.cpp new file mode 100644 index 0000000..6f7deee --- /dev/null +++ b/BesLyric/BSMessageBox.cpp @@ -0,0 +1,9 @@ +#include "stdafx.h" +#include "BSMessageBox.h" + +//发送自定义图标的消息 +int _MessageBox(HWND hwnd,LPCTSTR content, LPCTSTR tiltle, UINT uType) +{ + BSMessageBox m; + return m.MessageBoxW(hwnd,content,tiltle,uType); +} \ No newline at end of file diff --git a/BesLyric/BSMessageBox.h b/BesLyric/BSMessageBox.h index 97ee85f..2b0534d 100644 --- a/BesLyric/BSMessageBox.h +++ b/BesLyric/BSMessageBox.h @@ -89,4 +89,8 @@ class BSMessageBox: public SMessageBoxImpl } return TRUE; } -}; \ No newline at end of file +}; + + +//发送自定义图标的消息 +int _MessageBox(HWND hwnd,LPCTSTR content, LPCTSTR tiltle, UINT uType); \ No newline at end of file diff --git a/BesLyric/BesLyric.aps b/BesLyric/BesLyric.aps index 3152464f2839171f0d23dc5e9bce8d0816e4d6c0..8a3398642bbf62d3d54643a6b17e4283a7778ca2 100644 GIT binary patch delta 55 rcmew$@?A?0VVYqj5bf?dCiF6PM*jky?Ftj G4l4jdN)KuP diff --git a/BesLyric/BesLyric.vcxproj b/BesLyric/BesLyric.vcxproj index 1777666..abdd1ee 100644 --- a/BesLyric/BesLyric.vcxproj +++ b/BesLyric/BesLyric.vcxproj @@ -113,7 +113,9 @@ + + @@ -124,11 +126,13 @@ Create Create + + @@ -139,6 +143,7 @@ + diff --git a/BesLyric/BesLyric.vcxproj.filters b/BesLyric/BesLyric.vcxproj.filters index eb37d6b..8d5a443 100644 --- a/BesLyric/BesLyric.vcxproj.filters +++ b/BesLyric/BesLyric.vcxproj.filters @@ -70,6 +70,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files + @@ -107,6 +116,12 @@ Header Files + + Header Files + + + Header Files + diff --git a/BesLyric/Define.h b/BesLyric/Define.h index bcf84e3..272891b 100644 --- a/BesLyric/Define.h +++ b/BesLyric/Define.h @@ -27,6 +27,18 @@ #include using namespace std; +/* 宏定义 */ +#ifdef _DEBUG + +#define _KEEP_UPDATE 0 /* 是否在启动时更新程序 */ + +#else + +#define _KEEP_UPDATE 1 + +#endif + + /* 结构定义 */ @@ -51,7 +63,7 @@ typedef struct _PATH_STATE /* 全局变量定义 */ -static const SStringW VERSION_NUMBER = L"1.0.2"; //版本号 +static const SStringW VERSION_NUMBER = L"2.0.0"; //版本号(注意每次更改版本号时需要更改2处,1处是这里,1处是 BesLyric.rc 中的Version) static const string SETTING_FILE_NAME = "setting"; //设置文件名 diff --git a/BesLyric/MainDlg.cpp b/BesLyric/MainDlg.cpp index 8718234fb1c35d30963e85726244a697b777b97d..7b47ff524f13da3ef83d0910d64cfe8d98658fa9 100644 GIT binary patch delta 274 zcmdne&(hJ)vS9(!X5lc_j?N44DjhKz0t8 wmk(rXFlaL90mVyzwiN+MAb;|DcE?Fi*(S|pNu-rs`9R--Y_{I~n*HQ|09ZLiZ~y=R delta 304 zcmeC^XW7!vvS9(!=6g)%7>#)uxES<-@M19IgjbETqCNfog|TO}ly&6hCxGNK8S)rP z7!(+S8T=VS89W)R7_2654416;Up2^x)m5ofn+5^0Z?3%0pyNqFv-c_1T@{3 zA$4*qlOVGKL+<22PI2a32EECROroaY3>rwTLAM>%F1Y!VA1)Q1e2HCT^8uE7jI6aF TYbIZ07oBXvuCVzC`;q?u*?mMX diff --git a/BesLyric/MainDlg.h b/BesLyric/MainDlg.h index 32b47458a705b0237fa3f5bc36a58bb9693f87b9..a9b9b54849e81969a73a0a7abc9a0807733ce15b 100644 GIT binary patch delta 7 OcmaEr*p;w>%K!imIRhX7 delta 233 zcmYk1I|{;35JiubL~su&6$I_=)WU#9@mpA!5Q7o?fqz6qun;S2SK(INMiwF7q!4+$ z;qva8JM->N=)O{g^Bk8MJ}Z_|o|H+O23;(Uq)ElsAg}XoCm!MDIk4Oo)d$x{5SeUknHn-(kb diff --git a/BesLyric/SSingleton.h b/BesLyric/SSingleton.h new file mode 100644 index 0000000000000000000000000000000000000000..b5511798b2cf18e040007d9f41394ec62d4f3d73 GIT binary patch literal 2486 zcmb_e-%A@o5S~{H{tqh!xdt>=DwI}3O8e3mEkyBAn#*~)l3c?5D$+(@{6G6&^rcY! zW;b)$%k5gxa&UW@ogd$PGdr8Rzg_C!*`ybEEBZ(?n$s0!lu%4#Dj*9WeT8+O_UJXe zp&k8ofM2S%`#=*s%CgJ#uZVJrAg%Bo(Qd6T@%nU$NE7U;#xsC41p?kFfGLITxVG-o zP)CYjZQHH9a=Z+&6KyLx74YwB+#*?J(5J6dBGwodndNmrCv--iA&DWML1(_)T}2zg zwrJ%be034=E%AH5&OmHG!78UvXEU@sNYp<+e_ih!-<$;HZFM&IeZ2MU{M^`fwJ-jD z2}BtX#5BcQ>5N18;yts^T!!GXL=BkfL+Itp$`qQ}Uag&CZ>mwv@x^`=2^#kHqt+A- zR~;l0?xensXyS;R*LG#ut7sTYcXdtvH}Sgu`fnenO+CY(#O3Nu!Q%uun@S#Hy@DhH zcZLx~r_=*d+YhrUIX;}3;Z5Rk#b!EEsNW-c*YGQPId&OQ0yafpERMGd3O-a%nA1_a@Wf>Isc7S;TCQBTP)8f SGq}Kig>HT855k= +using namespace std; + +/* FileOperator */ + +bool FileOperator::ReadAllText(const string file, string& fileContent) +{ + ifstream in(file, ios_base::in); + string temp = ""; + if (in) + { + while (!in.eof()) + { + getline(in, temp); + temp += "\n"; + fileContent += temp; + } + return false; + } + else + return false; +} + +bool FileOperator::ReadAllLines(const string file, OUT vector *lines) +{ + ifstream in(file, ios_base::in); + string temp = ""; + if (in) + { + while (!in.eof()) + { + getline(in, temp); + lines->push_back(temp); + } + return false; + } + else + return false; +} + +bool FileOperator::ReadAllLinesW(const string file, OUT vector *lines) +{ + File encodingFile( S_CA2W(SStringA(file.c_str())), L"r"); + if(!encodingFile.isValidFile()) + return false; + + if(encodingFile.m_encodingType == ENCODING_TYPE::UTF_8 || encodingFile.m_encodingType == ENCODING_TYPE::UNICODE_BIG_ENDIAN + || encodingFile.m_encodingType == ENCODING_TYPE::UNICODE_LITTLE_ENDIAN ) + { + wchar_t line[MAX_WCHAR_COUNT_OF_LINE+1]; + + // 从文件中读取歌词,并将非空行加入到 maker.m_vLyricOrigin 向量中 + while(fgetws(line,MAX_WCHAR_COUNT_OF_LINE,encodingFile.m_pf)) + { + line[MAX_CHAR_COUNT_OF_LINE]='\0';//保证在最后一个字符处截断字符串 + + //由变长字符转换为 unicode 宽字节字符 + //MultiByteToWideChar(CP_ACP,MB_COMPOSITE,_line,strlen(_line)+1,line,MAX_WCHAR_COUNT_OF_LINE+1); + + SStringW aLine(line); + aLine.Trim('\n'); + aLine.Trim(' '); + aLine.Trim('\t'); + if(aLine.GetLength() ==0) + continue; + + lines->push_back(aLine); + } + } + else //if(encodingFile.m_encodingType == ENCODING_TYPE::ASCII 或 其他 + { + char _line[MAX_CHAR_COUNT_OF_LINE+1]; + wchar_t line[MAX_WCHAR_COUNT_OF_LINE+1]; + + // 从文件中读取歌词,并将非空行加入到 maker.m_vLyricOrigin 向量中 + while(fgets(_line,MAX_WCHAR_COUNT_OF_LINE,encodingFile.m_pf)) + { + _line[MAX_CHAR_COUNT_OF_LINE]='\0';//保证在最后一个字符处截断字符串 + + //由变长字符转换为 unicode 宽字节字符 + MultiByteToWideChar(CP_ACP,MB_COMPOSITE,_line,strlen(_line)+1,line,MAX_WCHAR_COUNT_OF_LINE+1); + SStringT aLine(line); + aLine.Trim('\n'); + aLine.Trim(' '); + aLine.Trim('\t'); + if(aLine.GetLength() ==0) + continue; + + lines->push_back(aLine); + } + } + + return true; +} + +bool FileOperator::SaveToFile(const string file, string& fileContent) +{ + ofstream out(file, ios_base::out); + if (out) + { + out << fileContent; + return true; + } + else + return false; +} + + +/* FileHelper */ +string FileHelper::GetCurrentDirectoryStr() +{ + wchar_t exeFullPath[MAX_PATH]; // Full path + + GetModuleFileName(NULL, exeFullPath, MAX_PATH); + + string strPath(S_CW2A(SStringW(exeFullPath)).GetBuffer(1)); + strPath = strPath.substr(0, strPath.find_last_of("\\")+1); + return strPath; +} + +bool FileHelper::CheckPathName(LPCTSTR format, LPCTSTR toChecked) +{ + int i; + bool isFloder = false; + //TODO:异常抛出处理 + int len = _tcslen(format); + if (_tcscmp(format, _T("..")) == 0) + { + isFloder = true; + } + else if (len < 3 || format[0] != _T('*') || format[1] != _T('.')) + return false; //TODO:异常 + + + //获取并检查 被检查的路径字符串 toChecked 的信息 + TCHAR pathName[_MAX_PATH]; + TCHAR ext[_MAX_EXT]; + + int lenPathName = 0, pos = -1; + + _tcscpy(pathName, toChecked); + lenPathName = _tcslen(pathName); //得到路径总长 + if (!lenPathName) + return false; + + //得到路径中最后一个“.”的位置置于pos中 + for (i = 0; i< lenPathName; i++) + { + if (_T('.') == pathName[i]) + pos = i; + } + + if (isFloder) //检查文件夹类型 + { + return IsDirectory(pathName); + } + else //检查普通后缀名类型 + { + _tcscpy(ext, &pathName[pos + 1]); //得到路径的后缀(不包含“.”) + if (_tcscmp(&format[2], ext) == 0) //和 参数提供的后缀对比 + return true; + else + return false; + } +} + +bool FileHelper::IsDirectory(SStringW path) +{ + //查找panelFloder是否存在 + WIN32_FIND_DATA FindFileData; + BOOL bValue = false; + HANDLE hFind = FindFirstFile(path, &FindFileData); + if ((hFind != INVALID_HANDLE_VALUE) && (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + { + bValue = true; + } + FindClose(hFind); + + return bValue; +} + +bool FileHelper::CheckFileExist(const string &strPath) +{ + WIN32_FIND_DATA FindFileData; + HANDLE hFind; + + hFind = FindFirstFile(S_CA2W(SStringA(strPath.c_str())), &FindFileData); + + if (hFind == INVALID_HANDLE_VALUE) { + return false; + } + else { + FindClose(hFind); + return true; + } +} + +bool FileHelper::CheckFolderExist(const string &strPath) +{ + WIN32_FIND_DATA wfd; + bool rValue = false; + HANDLE hFind = FindFirstFile(S_CA2W(SStringA(strPath.c_str())), &wfd); + if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + { + rValue = true; + } + FindClose(hFind); + + return rValue; +} + +void FileHelper::SplitPath(const string& strPathName, OUT string *pstrDrive, OUT string *pstrDirectory, OUT string* pstrName, OUT string* pstrExt) +{ + string _strDrive = ""; + string _strDirectory = ""; + string _strName = ""; + string _strExt = ""; + + //分号位置 + auto posColon = strPathName.find_first_of(':'); + + if (posColon != string::npos) + _strDrive = strPathName.substr(0, posColon); + + //最后一个分隔符号位置 + auto posLastSep = strPathName.find_last_of("/\\"); + if (posLastSep != string::npos) + { + _strDirectory = strPathName.substr(0, posLastSep +1); + + posLastSep++; + } + else + posLastSep = 0; + + //字符串结尾 + auto posEnd = strPathName.size(); + + //寻找拓展名的"."位置 + auto posDot = strPathName.find_last_of('.'); + if (posDot != string::npos) + { + //特殊处理,路径中含有 . 的情况可能有 ..\\test, 所以只有当 获得的“.”位置大于 posLastSep 时, 获得的“.”位置才是拓展名的"."位置 + if (posDot > posLastSep) + _strExt = strPathName.substr(posDot, posEnd - posDot); + else + posDot = posEnd; + } + else + posDot = posEnd; + + + _strName = strPathName.substr(posLastSep, posDot - posLastSep); + + if (pstrDrive != nullptr) + *pstrDrive = _strDrive; + if (pstrDirectory != nullptr) + *pstrDirectory = _strDirectory; + if (pstrName != nullptr) + *pstrName = _strName; + if (pstrExt != nullptr) + *pstrExt = _strExt; +} + diff --git a/BesLyric/WinFile.h b/BesLyric/WinFile.h new file mode 100644 index 0000000..fa3204b --- /dev/null +++ b/BesLyric/WinFile.h @@ -0,0 +1,68 @@ +#pragma once +#include "stdafx.h" +#include +#include +#include +using namespace std; + +/* +* @brief 负责文件的读写操作 +*/ +class FileOperator +{ +public: + static bool ReadAllText(const string file, string& fileContent); + + static bool ReadAllLines(const string file, OUT vector *lines); + + static bool ReadAllLinesW(const string file, OUT vector *lines); + + static bool SaveToFile(const string file, string& fileContent); + +}; + + +/* +* @biref 辅助文件操作,如获得路径,判断路径是否为文件夹等 +*/ +class FileHelper +{ +public: + /* 获得应用程序当前的路径 */ + static string GetCurrentDirectoryStr(); + + /** + * @brief 检查文件名是否符合要求格式(仅仅检查名字上的格式) + * @param format 支持的检查格式 普通文件格式:如 *.txt、 *.mp3 ("*." 是必须的;且后缀必须至少有一个字符) + * 文件夹格式:.. + * toChecked 被检查的路径字符串 + * @return TRUE 符合要求 + * @note + */ + static bool CheckPathName(LPCTSTR format, LPCTSTR toChecked); + + /* + * @brief 判断传递的路径是否为路径 + */ + static bool IsDirectory(SStringW path); + + + /* + * @brief 查询文件是否存在 + */ + static bool CheckFileExist(const string &strPath); + + /* + * @brief 查询文件夹是否存在 + */ + static bool CheckFolderExist(const string &strPath); + + /** + * @brief 分割路径名 + * @note 路径可以是全路径,如 C:\\document\\desktop\\test.pan + * 也可以不是, 如 ..\..\test\test.pan + */ + static void SplitPath(const string& strPathName, OUT string *strDrive = NULL, OUT string *strDirectory = NULL, OUT string* strName = NULL, OUT string* strExt = NULL); +}; + + diff --git a/BesLyric/res/resource.h b/BesLyric/res/resource.h index a135d79074483210d7211daf05fa03be2d4a8a6d..770345fb3872641ad38fc7285530f86b8328068e 100644 GIT binary patch delta 34 lcmexyh4IG~#tAZ_Neo5|DGaGVY|3B^W*ak@ZZvey0sy|*2^IhV delta 34 mcmexyh4IG~#tAZ_mJCS@MhwXe1`LS|#y~m=#NTM>o&^BCO9?yx diff --git a/versionLog.txt b/versionLog.txt new file mode 100644 index 0000000..3345fca --- /dev/null +++ b/versionLog.txt @@ -0,0 +1,8 @@ +锘跨増鏈彿锛1.0.1 +1銆佹瓕璇嶅師鏂囦欢鍦ㄥ師鏉ユ敮鎸 ascii 鐮佺殑鍩虹涓婏紝鏀寔windows鏂囨湰鐨勫彟澶3绉嶇紪鐮侊細Unicode銆乁nicode little endian銆 UTF-8 + +鐗堟湰鍙凤細1.0.2 +1銆佹坊鍔犺缃〉闈紝鍦ㄨ缃〉闈㈠彲浠ヨ缃 鎻掑叆姝岃瘝鏃堕棿鏃舵彁鍓嶇殑姣鏁 + +鐗堟湰鍙凤細2.0.0 +1銆佹坊鍔犺嚜鍔ㄥ崌绾у姛鑳斤紝瀹炵幇鍚庡彴鑷姩鍗囩骇 \ No newline at end of file From 0b2baa552c20eca78a6f15bf3ddd51331dfb73a0 Mon Sep 17 00:00:00 2001 From: BensonLaur <1035069088@qq.com> Date: Mon, 10 Jul 2017 15:55:04 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E6=A8=A1=E5=9D=97=EF=BC=8C=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=89=88=E6=9C=AC=E5=8F=B7=E5=B0=8F?= =?UTF-8?q?=E4=BA=8E=E5=BC=80=E5=8F=91=E7=89=88=E6=9C=AC=E5=8F=B7=E6=97=B6?= =?UTF-8?q?=E4=BB=8D=E7=84=B6=E6=9B=B4=E6=96=B0=E7=9A=84bug;=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0Document=20=E7=9B=AE=E5=BD=95=EF=BC=8C=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=BC=80=E5=8F=91=E8=80=85=E8=AE=B0=E5=BD=95=E5=92=8C?= =?UTF-8?q?=E4=BA=A4=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BesLyric/AutoUpdateThread.cpp | 40 ++++++++++++++++--- BesLyric/AutoUpdateThread.h | 4 ++ Document/AutoUpdate.txt | 33 +++++++++++++++ ...6\345\275\225\350\257\264\346\230\216.txt" | 5 +++ 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 Document/AutoUpdate.txt create mode 100644 "Document/_\347\233\256\345\275\225\350\257\264\346\230\216.txt" diff --git a/BesLyric/AutoUpdateThread.cpp b/BesLyric/AutoUpdateThread.cpp index 860f6a0..3d2cb37 100644 --- a/BesLyric/AutoUpdateThread.cpp +++ b/BesLyric/AutoUpdateThread.cpp @@ -67,10 +67,6 @@ DWORD WINAPI AutoUpdateThread::ThreadProc(LPVOID pParam) { AutoUpdateThread* pThread = static_cast(pParam); - //启动之后15秒才开始更新。 - //发布新版本时,即在Realease 下,会自动更新,为了编译后不立刻更新导致版本被自动被替换成较低版本,设置15秒延时,在15秒内关闭则不会被自动替换 - Sleep(15000); - #if _KEEP_UPDATE == 0 pThread->m_bKeepUpdate = false; #endif @@ -122,7 +118,7 @@ bool AutoUpdateThread::AutoUpdate() } } - if(VERSION_NUMBER == strVersion) // 版本一致 + if(VersionCompare(VERSION_NUMBER, strVersion) >= 0 ) // 版本一致 或者 当前版本大于服务器版本(处于开发状态) { m_bHasUpdate = true; return true; @@ -182,6 +178,38 @@ bool AutoUpdateThread::AutoUpdate() return true; } +//比较2个字符串版本号的大小, +int AutoUpdateThread::VersionCompare(const SStringW v1, const SStringW v2) +{ + int nMainNum1 = 0, nSubNum1= 0, nModifidNum1= 0; + int nMainNum2 = 0, nSubNum2= 0, nModifidNum2= 0; + swscanf(v1,L"%d.%d.%d",&nMainNum1, &nSubNum1, &nModifidNum1); + swscanf(v2,L"%d.%d.%d",&nMainNum2, &nSubNum2, &nModifidNum2); + + if(nMainNum1 > nMainNum2) + return 1; + else if(nMainNum1 < nMainNum2) + return -1; + else//主版本号相同 + { + if(nSubNum1 > nSubNum2) + return 1; + else if(nSubNum1 < nSubNum2) + return -1; + else//次版本号相同 + { + if(nModifidNum1 > nModifidNum2) + return 1; + else if(nModifidNum1 < nModifidNum2) + return -1; + else + return 0; + } + } + +} + + /*将Url指向的地址的文件下载到save_as指向的本地文件*/ bool AutoUpdateThread::DownloadFile(const wstring strUrl, const wstring strSaveAs) { @@ -221,6 +249,8 @@ bool AutoUpdateThread::DownloadFile(const wstring strUrl, const wstring strSaveA } + + //发送登录信息(ip地址) void AutoUpdateThread::SendLoginInfo() { diff --git a/BesLyric/AutoUpdateThread.h b/BesLyric/AutoUpdateThread.h index b2eacda..5fd7cf1 100644 --- a/BesLyric/AutoUpdateThread.h +++ b/BesLyric/AutoUpdateThread.h @@ -30,9 +30,13 @@ class AutoUpdateThread : public Singleton //自动更新执行函数 bool AutoUpdate(); + //比较2个字符串版本号的大小, + int VersionCompare(const SStringW v1, const SStringW v2); + //从网络下载文件 bool DownloadFile(const wstring strUrl, const wstring strSaveAs); + //发送登录信息(ip地址) void SendLoginInfo(); diff --git a/Document/AutoUpdate.txt b/Document/AutoUpdate.txt new file mode 100644 index 0000000..fff14df --- /dev/null +++ b/Document/AutoUpdate.txt @@ -0,0 +1,33 @@ +自动升级模块 +==================================================================================== +模块说明: + 该模块实现自动升级功能,在程序启动后启动一个线程来自动升级程序;以静默升级方 +式,在线程启动后,下载最新配置文件,发现有新的版本,则下载最新的可执行文件和最新 +版本信息文件。以重命名的方式,升级文件。 + + +------------------------------------------------------------------------------------- +主要代码文件: +../BesLyric/BesAutoUpdateThread.cpp +../BesLyric/AutoUpdateThread.h + +------------------------------------------------------------------------------------- +升级程序说明: + +服务器上储存着4个文件,这里使用博客园的文件服务器,由于文件类型限制,后缀使用zip,具体 +文件如下: +http://files.cnblogs.com/files/BensonLaur/Beslyric.zip + +http://files.cnblogs.com/files/BensonLaur/lastVersion.zip +http://files.cnblogs.com/files/BensonLaur/BesLyricExe.zip +http://files.cnblogs.com/files/BensonLaur/versionLog.zip + +文件具体说明: +Beslyric.zip,该文件为压缩包文件,储存最新的完整的整个程序,下载链接实际下载文件 + +lastVersion.zip,该文件为txt文件,即 lastVersion.txt,储存最新版本号和最新更新内容, + 自动升级程序时,首先下载这个配置文件 +BesLyricExe.zip,该文件为exe文件,即BesLyric.exe,用于自动更新时下载来替换当前版本 +versionLog.zip,该文件为txt文件,即 versionLog.txt,用于自动更新时下载来替换旧的版 + 本记录。 + diff --git "a/Document/_\347\233\256\345\275\225\350\257\264\346\230\216.txt" "b/Document/_\347\233\256\345\275\225\350\257\264\346\230\216.txt" new file mode 100644 index 0000000..bdc2a47 --- /dev/null +++ "b/Document/_\347\233\256\345\275\225\350\257\264\346\230\216.txt" @@ -0,0 +1,5 @@ +Document + 该目录用于储存说明文档,用于记录不同模块开发过程说明和注意事项 + +Document/AutoUpdate.txt + 自动升级模块 \ No newline at end of file From 445faaa4ae6ee664d1d2ec0a6b2104057e9beb54 Mon Sep 17 00:00:00 2001 From: BensonLaur <1035069088@qq.com> Date: Mon, 10 Jul 2017 17:12:19 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9Document=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F=E4=B8=BAUTF-8=E6=9C=89BOM=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E4=BB=A5=E4=BE=BF=E5=9C=A8Github=E4=B8=8A?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Document/AutoUpdate.txt | 32 +++++++++---------- ...6\345\275\225\350\257\264\346\230\216.txt" | 6 ++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Document/AutoUpdate.txt b/Document/AutoUpdate.txt index fff14df..fc17cad 100644 --- a/Document/AutoUpdate.txt +++ b/Document/AutoUpdate.txt @@ -1,33 +1,33 @@ -自动升级模块 +锘胯嚜鍔ㄥ崌绾фā鍧 ==================================================================================== -模块说明: - 该模块实现自动升级功能,在程序启动后启动一个线程来自动升级程序;以静默升级方 -式,在线程启动后,下载最新配置文件,发现有新的版本,则下载最新的可执行文件和最新 -版本信息文件。以重命名的方式,升级文件。 +妯″潡璇存槑锛 + 璇ユā鍧楀疄鐜拌嚜鍔ㄥ崌绾у姛鑳斤紝鍦ㄧ▼搴忓惎鍔ㄥ悗鍚姩涓涓嚎绋嬫潵鑷姩鍗囩骇绋嬪簭锛涗互闈欓粯鍗囩骇鏂 +寮忥紝鍦ㄧ嚎绋嬪惎鍔ㄥ悗锛屼笅杞芥渶鏂伴厤缃枃浠讹紝鍙戠幇鏈夋柊鐨勭増鏈紝鍒欎笅杞芥渶鏂扮殑鍙墽琛屾枃浠跺拰鏈鏂 +鐗堟湰淇℃伅鏂囦欢銆備互閲嶅懡鍚嶇殑鏂瑰紡锛屽崌绾ф枃浠躲 ------------------------------------------------------------------------------------- -主要代码文件: +涓昏浠g爜鏂囦欢锛 ../BesLyric/BesAutoUpdateThread.cpp ../BesLyric/AutoUpdateThread.h ------------------------------------------------------------------------------------- -升级程序说明: +鍗囩骇绋嬪簭璇存槑锛 -服务器上储存着4个文件,这里使用博客园的文件服务器,由于文件类型限制,后缀使用zip,具体 -文件如下: +鏈嶅姟鍣ㄤ笂鍌ㄥ瓨鐫4涓枃浠讹紝杩欓噷浣跨敤鍗氬鍥殑鏂囦欢鏈嶅姟鍣紝鐢变簬鏂囦欢绫诲瀷闄愬埗锛屽悗缂浣跨敤zip锛屽叿浣 +鏂囦欢濡備笅锛 http://files.cnblogs.com/files/BensonLaur/Beslyric.zip http://files.cnblogs.com/files/BensonLaur/lastVersion.zip http://files.cnblogs.com/files/BensonLaur/BesLyricExe.zip http://files.cnblogs.com/files/BensonLaur/versionLog.zip -文件具体说明: -Beslyric.zip,该文件为压缩包文件,储存最新的完整的整个程序,下载链接实际下载文件 +鏂囦欢鍏蜂綋璇存槑锛 +Beslyric.zip锛岃鏂囦欢涓哄帇缂╁寘鏂囦欢锛屽偍瀛樻渶鏂扮殑瀹屾暣鐨勬暣涓▼搴忥紝涓嬭浇閾炬帴瀹為檯涓嬭浇鏂囦欢 -lastVersion.zip,该文件为txt文件,即 lastVersion.txt,储存最新版本号和最新更新内容, - 自动升级程序时,首先下载这个配置文件 -BesLyricExe.zip,该文件为exe文件,即BesLyric.exe,用于自动更新时下载来替换当前版本 -versionLog.zip,该文件为txt文件,即 versionLog.txt,用于自动更新时下载来替换旧的版 - 本记录。 +lastVersion.zip,璇ユ枃浠朵负txt鏂囦欢锛屽嵆 lastVersion.txt锛屽偍瀛樻渶鏂扮増鏈彿鍜屾渶鏂版洿鏂板唴瀹癸紝 + 鑷姩鍗囩骇绋嬪簭鏃讹紝棣栧厛涓嬭浇杩欎釜閰嶇疆鏂囦欢 +BesLyricExe.zip锛岃鏂囦欢涓篹xe鏂囦欢锛屽嵆BesLyric.exe锛岀敤浜庤嚜鍔ㄦ洿鏂版椂涓嬭浇鏉ユ浛鎹㈠綋鍓嶇増鏈 +versionLog.zip锛岃鏂囦欢涓簍xt鏂囦欢锛屽嵆 versionLog.txt锛岀敤浜庤嚜鍔ㄦ洿鏂版椂涓嬭浇鏉ユ浛鎹㈡棫鐨勭増 + 鏈褰曘 diff --git "a/Document/_\347\233\256\345\275\225\350\257\264\346\230\216.txt" "b/Document/_\347\233\256\345\275\225\350\257\264\346\230\216.txt" index bdc2a47..ebd3aa6 100644 --- "a/Document/_\347\233\256\345\275\225\350\257\264\346\230\216.txt" +++ "b/Document/_\347\233\256\345\275\225\350\257\264\346\230\216.txt" @@ -1,5 +1,5 @@ -Document - 该目录用于储存说明文档,用于记录不同模块开发过程说明和注意事项 +锘緿ocument + 璇ョ洰褰曠敤浜庡偍瀛樿鏄庢枃妗o紝鐢ㄤ簬璁板綍涓嶅悓妯″潡寮鍙戣繃绋嬭鏄庡拰娉ㄦ剰浜嬮」 Document/AutoUpdate.txt - 自动升级模块 \ No newline at end of file + 鑷姩鍗囩骇妯″潡 \ No newline at end of file