📄 function.cpp
字号:
#include "stdafx.h"
#include "GreenBrowser.h"
#include "resource.h" // main symbols
#include "wininet.h" //for INTERNET_MAX_PATH_LENGTH
#include "mainfrm.h"
#include "MenuData.h"
//
#include <sys\stat.h> //for struct stat
#include <direct.h> //for mkdir
//##############################################################
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//##############################################################
#define MAX_INI 512
#define _NULL -1
//##############################################################
//for plugin
CString _GetPlugInSection(CString strFileName, CString strSection)
{
CString strOut;
strOut.Empty();
char tmp[INTERNET_MAX_PATH_LENGTH];
if (::GetPrivateProfileString("General", strSection, NULL, tmp, INTERNET_MAX_PATH_LENGTH, strFileName))
strOut = tmp;
return strOut;
}
//0:not 1:script 2:exe
int _GetPlugInType(CString strFileName)
{
CString strTemp2 = GET_FILE(strFileName);
strTemp2.MakeLower();
if (strTemp2.Left(10)=="plugin.ini")
{
CString strType = _GetPlugInSection(strFileName,"Type");
strType.MakeUpper();
if (strType=="BUTTON" || strType=="M2PLUGIN_BUTTON")
{
CString strSection = _GetPlugInSection(strFileName,"ModuleType");
strSection.MakeUpper();
if (strSection=="SCRIPT")
return 1;
else if (strSection=="EXE")
return 2;
}
}
return 0;
}
CString _GetPlugInIconPath(CString strFileName)
{
CString strOut;
strOut.Empty();
char tmp[INTERNET_MAX_PATH_LENGTH];
if (::GetPrivateProfileString("General", "HotIcon", NULL, tmp, INTERNET_MAX_PATH_LENGTH, strFileName)==FALSE)
{
if (::GetPrivateProfileString("General", "Icon", NULL, tmp, INTERNET_MAX_PATH_LENGTH, strFileName)==FALSE)
return strOut;
}
strOut = strFileName.Left(strFileName.ReverseFind('\\')+1) + tmp;
return strOut;
}
CString _GetPlugInName(CString strFileName)
{
return _GetPlugInSection(strFileName, "Name" );
}
CString _GetPlugInComments(CString strFileName)
{
return _GetPlugInSection(strFileName, "Comments" );
}
CString _GetPlugInFileName(CString strFileName)
{
return _GetPlugInSection(strFileName, "FileName" );
}
//##############################################################
//file common function
char* _FileGetBuffer(CString filename)
{
CFile f;
if( f.Open(filename, CFile::modeRead|CFile::shareDenyNone) == FALSE)
return NULL;
DWORD len = f.GetLength();
if (len<=0)
return NULL;
char* tmp = (char*)malloc(len+1);
f.Read(tmp, len);
tmp[len]=0;
f.Close();
return tmp;
}
BOOL _BrowserPath(CWnd* pParentWnd, CString& m_strPath)
{
LPMALLOC pMalloc;
if (::SHGetMalloc(&pMalloc)!= NOERROR)
return FALSE;
CString str;
LOADSTR(str ,IDS_SELECT_PATH);
BROWSEINFO bInfo;
LPITEMIDLIST pidl;
ZeroMemory ( (PVOID) &bInfo,sizeof (BROWSEINFO));
bInfo.hwndOwner = pParentWnd->GetSafeHwnd();
bInfo.pszDisplayName = m_strPath.GetBuffer (MAX_PATH);
bInfo.lpszTitle = str;//_T("Select Path:");
bInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
bInfo.lpfn = NULL;//!BrowseCtrlCallback; // address of callback function
bInfo.lParam = NULL;//!(LPARAM)this; // pass address of object to callback function
if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL)
return FALSE;
m_strPath.ReleaseBuffer();
//!m_iImageIndex = bInfo.iImage;
if (::SHGetPathFromIDList(pidl,m_strPath.GetBuffer(MAX_PATH)) == FALSE)
{
pMalloc ->Free(pidl);
pMalloc ->Release();
return FALSE;
}
m_strPath.ReleaseBuffer();
pMalloc ->Free(pidl);
pMalloc ->Release();
return TRUE;
}
//##############################################################
BOOL _FileIsExist(CString strPath)
{
//必须这样做,因为 末尾带 \ 的 FindFirstFile stat 中不能用
int nSlash = strPath.ReverseFind('\\');
if( nSlash == strPath.GetLength()-1)
strPath = strPath.Left(nSlash);
/*
WIN32_FIND_DATA fd;
HANDLE hFind = FindFirstFile( strPath, &fd );
if ( hFind != INVALID_HANDLE_VALUE )
FindClose( hFind );
return hFind != INVALID_HANDLE_VALUE;
*/
struct stat stat_buf;
if (stat(strPath, &stat_buf) >= 0)
return TRUE;
else
return FALSE;
}
void _CreateFullDir(const char *dir)
{
struct stat stat_buf;
char *p;
char parent[MAX_PATH];
int length;
if (_FileIsExist(dir))
return;
if ( strlen(dir) == 0)
return;
p = strchr(dir,'\\');
while(p)
{
length = p-dir;
STRNCPY(parent,dir,length);
//make dir
if (stat(parent, &stat_buf) < 0 && strlen(parent) > 2)//can not mkdir("c:")
{
if ( mkdir(parent)!=0 )
return;
}
//move to next
if (*(p+1)==0)
return;
p = strchr(p+1,'\\');
}
//for c:\\aa\bb\cc\\dd,mow create dd
if (stat(dir, &stat_buf) < 0 && strlen(dir) > 2)//can not mkdir("c:")
{
if ( mkdir(dir)!=0 )
return;
}
}
//#######################################################################
//国际 URL
#define IS_1URL(strUrl) ( strUrl.Right(5)==".com/" || \
strUrl.Right(5)==".net/" || \
strUrl.Right(5)==".org/" || \
strUrl.Right(5)==".edu/" || \
strUrl.Right(4)==".com" || \
strUrl.Right(4)==".net" || \
strUrl.Right(4)==".org" || \
strUrl.Right(4)==".edu" )
//2级 URL
#define IS_2URL(nDot,strUrl) ( (nDot=strUrl.Find(".com."))>0 || \
(nDot=strUrl.Find(".net."))>0 || \
(nDot=strUrl.Find(".org."))>0 || \
(nDot=strUrl.Find(".edu."))>0 )
//##############################################################
CString _StringGetUpUrl(CString strUrl)
{
if (!strUrl || strUrl.GetLength()<=0)
return strUrl;
if (strUrl=="about:blank")
return strUrl;
//if (strUrl.Left(6)=="res://")
// return strUrl;
// must have / and \\
int c,b;
c = strUrl.Find('/');
if (c==5 || c==6 || c==4) // file:/// http:// https:// ftp://
{
c = '/';
b = 8; // https://
}
else if (strUrl.Find('\\')>=0)
{
c = '\\';
if (strUrl.GetAt(1)==':')
b = 3; // c:\a\b
else
b = 2; // \\server1
}
else
return strUrl;
//
int len = strUrl.GetLength();
int pos1 = strUrl.Find(c,b); //get first c
int pos2 = strUrl.ReverseFind(c);
if ( (pos1<0) || (pos1+1==len && b!=3) )// \\server1 \\server1\ ,must except c:\temp\
{
if (c=='/')
{
// http://mail.qwe.com/ , http://ror.cn/, http://mail.qwe.com.cn/
if( (strUrl.Left(7)=="http://" && strUrl.Left(11)!="http://www.") ||
(strUrl.Left(8)=="https://" && strUrl.Left(12)!="https://www.")
)
{
int nDot=0;
CString strTemp;
if ( IS_1URL(strUrl) )
{
// http://login.bj.yahoo.com/
nDot = strUrl.ReverseFind('.');
strTemp = strUrl.Left(nDot);
nDot = strTemp.ReverseFind('.');
}
else if ( IS_2URL(nDot, strUrl) )
{
strTemp = strUrl.Left(nDot);
nDot = strTemp.ReverseFind('.');
}
else if ( strUrl.Find('.') < strUrl.ReverseFind('.')) // http://news.sh.cn/
{
int pos1 = strUrl.Find("://");
pos1 += 3;
strTemp = strUrl.Mid(pos1);
if (strTemp.FindOneOf("abcdefghijklmnopqrstuvwxyz")>=0) //http://127.0.0.1/
nDot = strUrl.Find('.',pos1);
}
//replace str as www
if (nDot>0)
{
// http://news.newscn.com/ => http://www.newscn.com/
int pos1 = strUrl.Find("://");
pos1 += 3;
strTemp = strUrl.Left(pos1);
strTemp += "www";
strTemp += strUrl.Mid(nDot);
strUrl = strTemp;
}
}
}
return strUrl;
}
else if (pos2+1==len)// \\server1\mail\xxx\
{
strUrl = strUrl.Left(pos2);
pos2 = strUrl.ReverseFind(c);
}
strUrl = strUrl.Left(pos2+1); // \\server1\mail\xxx
return strUrl;
}
CString _StringGetRootUrl(CString strUrl)
{
if (!strUrl || strUrl.GetLength()<1)
return strUrl;
if (!IS_URL(strUrl))
return strUrl;
int nIndex = 0;
CString strUrlUp;
while(nIndex < 100)
{
strUrlUp = _StringGetUpUrl(strUrl);
if (strUrlUp == strUrl)
break;
strUrl = strUrlUp;
//
nIndex++;
}
return strUrl;
}
//only for http:// and https://
CString _StringGetTopUrl(CString strUrl)
{
CString strTop;
strTop.Empty();
if (!IS_HTTP(strUrl))
return strTop;
int p1 = strUrl.Find("://");
p1+=3;
int p2 = strUrl.Find("/",p1);
if (p2<0)
{
strTop = strUrl + "/";
return strTop;
}
strTop = strUrl.Left(p2+1);
return strTop;
}
//##############################################################
//return add ok or not
BOOL _GroupAddItem(CString strName, CString strUrl, BOOL bState, DWORD dwProperty, CString filename)
{
char state[10]="state",name[9]="name",url[8]="url",download[13]="download"; // x+5
char num[15];
int i=0;
char tmp[INTERNET_MAX_PATH_LENGTH];
BOOL r = TRUE, bNeedAdd=TRUE;
DWORD dwPropertyTemp;
CString strNameTemp,strUrlTemp;
//get if exist the url
while(r)
{
itoa(i, name+4, 10);
itoa(i, url+3, 10);
r = ::GetPrivateProfileString("Group", name, NULL, tmp, INTERNET_MAX_PATH_LENGTH, filename);
if (r)
{
strNameTemp = tmp;
r = ::GetPrivateProfileString("Group", url, NULL, tmp, INTERNET_MAX_PATH_LENGTH, filename);
if (r)
{
strUrlTemp = tmp;
itoa(i, download+8, 10);
dwPropertyTemp = ::GetPrivateProfileInt("Group", download, DEFAULT_PROPERTY, filename);
if (strName==strNameTemp && strUrl==strUrlTemp && dwProperty==dwPropertyTemp)
{
r = FALSE;//no use add the item
bNeedAdd = FALSE;
}
}
}
i++;
}
if (bNeedAdd)
{
if (i>0)
i--;
itoa(i, state+5, 10);
itoa(i, name+4, 10);
itoa(i, url+3, 10);
itoa(i, download+8, 10);
//
::WritePrivateProfileString("Group", name, strName, filename);
::WritePrivateProfileString("Group", url, strUrl, filename);
if (bState==0)
{
itoa( 0, num, 10);
::WritePrivateProfileString("Group", state, num, filename);
}
if (dwProperty != DEFAULT_PROPERTY)
{
ultoa( dwProperty, num, 10);
::WritePrivateProfileString("Group", download, num, filename);
}
}
return bNeedAdd;
}
//##############################################################
DWORD _RegGetDword(HKEY hKeyRoot, LPCSTR lpSubKey, LPCSTR lpValueName)
{
DWORD sz;
HKEY hKey;
DWORD dwSize = sizeof(sz);
if(RegOpenKey(hKeyRoot, _T(lpSubKey), &hKey) != ERROR_SUCCESS)
return NULL;
if (RegQueryValueEx(hKey, _T(lpValueName), NULL, NULL, (LPBYTE)&sz, &dwSize) != ERROR_SUCCESS)
sz = NULL;
RegCloseKey(hKey);
return sz;
}
BOOL _RegSetDword(HKEY hKeyRoot, LPCSTR lpSubKey, LPCSTR lpValueName, DWORD dwValue)
{
BOOL bRet=FALSE;
HKEY hKey;
DWORD dwSize = sizeof(dwValue);
if(RegOpenKey(hKeyRoot, _T(lpSubKey), &hKey) != ERROR_SUCCESS)
{
if(RegCreateKey(hKeyRoot, _T(lpSubKey), &hKey) != ERROR_SUCCESS)
return bRet;
}
if (RegSetValueEx(hKey, _T(lpValueName), NULL, REG_DWORD, (LPBYTE)(&dwValue), dwSize) == ERROR_SUCCESS)
bRet = TRUE;
RegCloseKey(hKey);
return bRet;
}
CString _RegGetString(HKEY hKeyRoot, LPCSTR lpSubKey, LPCSTR lpValueName)
{
CString strRet;
HKEY hKey;
TCHAR sz[MAX_PATH];
DWORD dwSize = sizeof(sz);
strRet.Empty();
memset(sz,0,MAX_PATH);
if(RegOpenKey(hKeyRoot, _T(lpSubKey), &hKey) != ERROR_SUCCESS)
return strRet;
if (RegQueryValueEx(hKey, _T(lpValueName), NULL, NULL, (LPBYTE)sz, &dwSize) == ERROR_SUCCESS)
strRet = sz;
RegCloseKey(hKey);
return strRet;
}
BOOL _RegSetString(HKEY hKeyRoot, LPCSTR lpSubKey, LPCSTR lpValueName, LPCSTR lpValue)
{
HKEY hKey;
if(RegOpenKey(hKeyRoot, _T(lpSubKey), &hKey) != ERROR_SUCCESS)
{
if(RegCreateKey(hKeyRoot, _T(lpSubKey), &hKey) != ERROR_SUCCESS)
return FALSE;
}
RegSetValueEx(hKey, _T(lpValueName), 0, REG_SZ, (LPBYTE)lpValue, strlen(lpValue));
RegCloseKey(hKey);
return TRUE;
}
void _RegDeletePath(HKEY hKey, CString strPath)
{
TCHAR sz[MAX_PATH];
HKEY hKeySub;
DWORD dwSize = MAX_PATH;
LONG lRet;
if (RegOpenKey(hKey, strPath, &hKeySub) != ERROR_SUCCESS)
return ;
//not i++, for that have delete one
while(RegEnumKey(hKeySub, 0, sz, dwSize) == ERROR_SUCCESS)
{
lRet = RegDeleteKey(hKeySub,sz);
if (lRet != ERROR_SUCCESS)
_RegDeletePath(hKeySub, sz);
}
RegCloseKey(hKeySub);
//delete the path
RegDeleteKey(hKey,strPath);
}
//##############################################################
void _SetClipString(CString str)
{
int len = str.GetLength();
if ( !::OpenClipboard(NULL) )
return;
// Remove the current Clipboard contents
if( !EmptyClipboard() )
return;
HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, len+1);
if (hData == NULL)
return;
strcpy((LPSTR)GlobalLock(hData), (LPCSTR)str);
GlobalUnlock(hData);
// For the appropriate data formats...
SetClipboardData( CF_TEXT, hData );
CloseClipboard();
//GlobalFree(hData);
}
CString _GetClipString()
{
CString strUrl;
strUrl.Empty();
if ( !::OpenClipboard(NULL) )
return strUrl;
// For the appropriate data formats...
HGLOBAL hData = ::GetClipboardData( CF_TEXT);
if ( hData == NULL )
{
CloseClipboard();
return strUrl;
}
// ...
DWORD nSize = GlobalSize(hData);
if (nSize > 0)
{
char *str = (char*)GlobalLock(hData);
strUrl = str;
}
GlobalUnlock(hData);
CloseClipboard();
return strUrl;
}
//##############################################################
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -