📄 helpers.c
字号:
{
char *p = malloc(nOutput);
if (GetString(uIdFormat,p,nOutput))
wvsprintf(lpOutput,p,(LPVOID)(&uIdFormat+1));
free(p);
return lstrlen(lpOutput);
}
//=============================================================================
//
// FormatBytes()
//
void FormatBytes(LPSTR lpOutput,int nOutput,DWORD dwBytes)
{
char tch[256];
int i;
double dBytes = dwBytes;
static const char *pBytes[] = { "Bytes","KB","MB","GB" };
if (dwBytes > 1023)
{
for (i = 0; i < 4; i++)
{
if (dBytes >= 1024.0)
dBytes /= 1024.0;
else
break;
}
sprintf(tch,"%.2f",dBytes);
GetNumberFormat(LOCALE_USER_DEFAULT,0,tch,NULL,lpOutput,nOutput);
lstrcat(lpOutput," ");
lstrcat(lpOutput,pBytes[i]);
}
else
{
sprintf(lpOutput,"%i",dwBytes);
FormatNumberStr(lpOutput);
lstrcat(lpOutput," ");
lstrcat(lpOutput,pBytes[0]);
}
}
///////////////////////////////////////////////////////////////////////////////
//
//
// Name: PathIsLnkFile()
//
// Purpose: Determine wheter pszPath is a Windows Shell Link File by
// comparing the filename extension with ".lnk"
//
// Manipulates:
//
BOOL PathIsLnkFile(LPCSTR pszPath)
{
//char *pszExt;
char tchResPath[256];
if (!pszPath || !*pszPath)
return FALSE;
/*pszExt = strrchr(pszPath,'.');
if (!pszExt)
return FALSE;
if (!lstrcmpi(pszExt,".lnk"))
return TRUE;
else
return FALSE;*/
//if (!lstrcmpi(PathFindExtension(pszPath),".lnk"))
// return TRUE;
//else
// return FALSE;
if (lstrcmpi(PathFindExtension(pszPath),".lnk"))
return FALSE;
else
return PathGetLnkPath(pszPath,tchResPath,COUNTOF(tchResPath));
}
///////////////////////////////////////////////////////////////////////////////
//
//
// Name: PathGetLnkPath()
//
// Purpose: Try to get the path to which a lnk-file is linked
//
//
// Manipulates: pszResPath
//
BOOL PathGetLnkPath(LPCSTR pszLnkFile,LPSTR pszResPath,int cchResPath)
{
IShellLink *psl;
WIN32_FIND_DATA fd;
BOOL bSucceeded = FALSE;
if (SUCCEEDED(CoCreateInstance(&CLSID_ShellLink,NULL,
CLSCTX_INPROC_SERVER,
&IID_IShellLink,&psl)))
{
IPersistFile *ppf;
if (SUCCEEDED(psl->lpVtbl->QueryInterface(psl,&IID_IPersistFile,&ppf)))
{
WORD wsz[MAX_PATH];
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,
pszLnkFile,-1,wsz,MAX_PATH);
if (SUCCEEDED(ppf->lpVtbl->Load(ppf,wsz,STGM_READ)))
{
if (NOERROR == psl->lpVtbl->GetPath(psl,pszResPath,cchResPath,&fd,0))
bSucceeded = TRUE;
}
ppf->lpVtbl->Release(ppf);
}
psl->lpVtbl->Release(psl);
}
// This additional check seems reasonable
if (!lstrlen(pszResPath))
bSucceeded = FALSE;
if (bSucceeded) {
ExpandEnvironmentStringsEx(pszResPath,cchResPath);
PathCanonicalizeEx(pszResPath);
}
return(bSucceeded);
}
///////////////////////////////////////////////////////////////////////////////
//
//
// Name: PathIsLnkToDirectory()
//
// Purpose: Determine wheter pszPath is a Windows Shell Link File which
// refers to a directory
//
// Manipulates: pszResPath
//
PathIsLnkToDirectory(LPCSTR pszPath,LPSTR pszResPath,int cchResPath)
{
char tchResPath[MAX_PATH];
if (PathIsLnkFile(pszPath)) {
if (PathGetLnkPath(pszPath,tchResPath,sizeof(char)*COUNTOF(tchResPath))) {
if (PathIsDirectory(tchResPath)) {
lstrcpyn(pszResPath,tchResPath,cchResPath);
return (TRUE);
}
else
return FALSE;
}
else
return FALSE;
}
else
return FALSE;
}
///////////////////////////////////////////////////////////////////////////////
//
//
// Name: PathCreateDeskLnk()
//
// Purpose: Modified to create a desktop link to Notepad2
//
// Manipulates:
//
BOOL PathCreateDeskLnk(LPCSTR pszDocument)
{
char tchExeFile[MAX_PATH];
char tchDocTemp[MAX_PATH];
char tchArguments[MAX_PATH+16];
char tchLinkDir[MAX_PATH];
char tchDescription[64];
char tchLnkFileName[MAX_PATH];
IShellLink *psl;
BOOL bSucceeded = FALSE;
BOOL fMustCopy;
if (!pszDocument || lstrlen(pszDocument) == 0)
return TRUE;
// init strings
GetModuleFileName(NULL,tchExeFile,COUNTOF(tchExeFile));
lstrcpy(tchDocTemp,pszDocument);
PathQuoteSpaces(tchDocTemp);
lstrcpy(tchArguments,"-n ");
lstrcat(tchArguments,tchDocTemp);
SHGetSpecialFolderPath(NULL,tchLinkDir,CSIDL_DESKTOP,TRUE);
GetString(IDS_LINKDESCRIPTION,tchDescription,COUNTOF(tchDescription));
// Try to construct a valid filename...
if (!SHGetNewLinkInfo(pszDocument,tchLinkDir,tchLnkFileName,&fMustCopy,SHGNLI_PREFIXNAME))
return(FALSE);
if (SUCCEEDED(CoCreateInstance(&CLSID_ShellLink,NULL,
CLSCTX_INPROC_SERVER,
&IID_IShellLink,&psl)))
{
IPersistFile *ppf;
if (SUCCEEDED(psl->lpVtbl->QueryInterface(psl,&IID_IPersistFile,&ppf)))
{
WORD wsz[MAX_PATH];
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,
tchLnkFileName,-1,wsz,MAX_PATH);
psl->lpVtbl->SetPath(psl,tchExeFile);
psl->lpVtbl->SetArguments(psl,tchArguments);
psl->lpVtbl->SetDescription(psl,tchDescription);
if (SUCCEEDED(ppf->lpVtbl->Save(ppf,wsz,TRUE)))
bSucceeded = TRUE;
ppf->lpVtbl->Release(ppf);
}
psl->lpVtbl->Release(psl);
}
return(bSucceeded);
}
///////////////////////////////////////////////////////////////////////////////
//
//
// Name: PathCreateFavLnk()
//
// Purpose: Modified to create a Notepad2 favorites link
//
// Manipulates:
//
BOOL PathCreateFavLnk(LPCSTR pszName,LPCSTR pszTarget,LPCSTR pszDir)
{
char tchLnkFileName[MAX_PATH];
IShellLink *psl;
BOOL bSucceeded = FALSE;
if (!pszName || lstrlen(pszName) == 0)
return TRUE;
lstrcpy(tchLnkFileName,pszDir);
PathAppend(tchLnkFileName,pszName);
lstrcat(tchLnkFileName,".lnk");
if (PathFileExists(tchLnkFileName))
return FALSE;
if (SUCCEEDED(CoCreateInstance(&CLSID_ShellLink,NULL,
CLSCTX_INPROC_SERVER,
&IID_IShellLink,&psl)))
{
IPersistFile *ppf;
if (SUCCEEDED(psl->lpVtbl->QueryInterface(psl,&IID_IPersistFile,&ppf)))
{
WORD wsz[MAX_PATH];
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,
tchLnkFileName,-1,wsz,MAX_PATH);
psl->lpVtbl->SetPath(psl,pszTarget);
if (SUCCEEDED(ppf->lpVtbl->Save(ppf,wsz,TRUE)))
bSucceeded = TRUE;
ppf->lpVtbl->Release(ppf);
}
psl->lpVtbl->Release(psl);
}
return(bSucceeded);
}
//=============================================================================
//
// TrimString()
//
BOOL TrimString(LPSTR lpString)
{
LPSTR psz;
if (!lpString || !*lpString)
return FALSE;
// Trim left
psz = lpString;
while (*psz == ' ')
psz = CharNext(psz);
MoveMemory(lpString,psz,lstrlen(psz) + 1);
// Trim right
psz = StrEnd(lpString);
while (*(psz = CharPrev(lpString,psz)) == ' ')
*psz = '\0';
return TRUE;
}
//=============================================================================
//
// ExtractFirstArgument()
//
BOOL ExtractFirstArgument(LPCSTR lpArgs,LPSTR lpArg1,LPSTR lpArg2)
{
LPSTR psz;
BOOL bQuoted = FALSE;
lstrcpy(lpArg1,lpArgs);
if (lpArg2)
*lpArg2 = '\0';
if (!TrimString(lpArg1))
return FALSE;
if (*lpArg1 == '\"')
{
*lpArg1 = ' ';
TrimString(lpArg1);
bQuoted = TRUE;
}
if (bQuoted)
psz = strchr(lpArg1,'\"');
else
psz = strchr(lpArg1,' ');;
if (psz)
{
*psz = '\0';
if (lpArg2)
lstrcpy(lpArg2,psz + 1);
}
TrimString(lpArg1);
if (lpArg2)
TrimString(lpArg2);
return TRUE;
}
//=============================================================================
//
// PrepareFilterStr()
//
void PrepareFilterStr(LPSTR lpFilter)
{
LPSTR psz = StrEnd(lpFilter);
while (psz != lpFilter)
{
if (*(psz = CharPrev(lpFilter,psz)) == '|')
*psz = '\0';
}
}
//=============================================================================
//
// StrTab2Space() - in place conversion
//
void StrTab2Space(LPSTR lpsz)
{
char *c;
while (c = strchr(lpsz,'\t'))
*c = ' ';
}
//=============================================================================
//
// ExpandEnvironmentStringsEx()
//
// Adjusted for Windows 95
//
void ExpandEnvironmentStringsEx(LPSTR lpSrc,DWORD dwSrc)
{
char szBuf[312];
if (ExpandEnvironmentStrings(lpSrc,szBuf,COUNTOF(szBuf)))
lstrcpyn(lpSrc,szBuf,dwSrc);
}
//=============================================================================
//
// PathCanonicalizeEx()
//
//
void PathCanonicalizeEx(LPSTR lpSrc)
{
char szDst[MAX_PATH];
if (PathCanonicalize(szDst,lpSrc))
lstrcpy(lpSrc,szDst);
}
//=============================================================================
//
// GetLongPathNameEx()
//
// Works fine with Windows 95!
//
extern LPMALLOC g_lpMalloc;
DWORD GetLongPathNameEx(LPCSTR lpszShortPath,LPSTR lpszLongPath,DWORD cchBuffer)
{
WCHAR wszShortPath[MAX_PATH];
LPSHELLFOLDER lpsfDesktop;
ULONG chParsed = 0;
ULONG dwAttributes = 0;
LPITEMIDLIST pidl = NULL;
BOOL fSucceeded = FALSE;
//MessageBox(GetFocus(),lpszShortPath,"GetLongPathNameEx(): lpszShortPath",0);
// Convert lpszShortPath to a UNICODE string
MultiByteToWideChar(
CP_ACP,MB_PRECOMPOSED,lpszShortPath,-1,wszShortPath,MAX_PATH);
// Get Desktop Folder
if (NOERROR == SHGetDesktopFolder(&lpsfDesktop))
{
// Convert wszShortPath to a pidl
if (NOERROR == lpsfDesktop->lpVtbl->ParseDisplayName(
lpsfDesktop,NULL,NULL,wszShortPath,&chParsed,&pidl,&dwAttributes))
{
if (SHGetPathFromIDList(pidl,lpszLongPath))
fSucceeded = FALSE;
g_lpMalloc->lpVtbl->Free(g_lpMalloc,pidl);
}
}
//MessageBox(GetFocus(),lpszLongPath,"GetLongPathNameEx(): lpszLongPath",0);
if (fSucceeded)
return(lstrlen(lpszLongPath));
else {
if (lpszShortPath != lpszLongPath)
lstrcpy(lpszLongPath,lpszShortPath);
return(0);
}
cchBuffer;
}
//=============================================================================
//
// SHGetFileInfo2() - return a default name when the file has been removed
//
DWORD_PTR SHGetFileInfo2(LPCTSTR pszPath,DWORD dwFileAttributes,
SHFILEINFO *psfi,UINT cbFileInfo,UINT uFlags)
{
if (PathFileExists(pszPath))
return SHGetFileInfo(pszPath,dwFileAttributes,psfi,cbFileInfo,uFlags);
else
return SHGetFileInfo(pszPath,FILE_ATTRIBUTE_NORMAL,
psfi,cbFileInfo,uFlags|SHGFI_USEFILEATTRIBUTES);
}
//=============================================================================
//
// FormatNumberStr()
//
int FormatNumberStr(LPSTR lpNumberStr)
{
char *c;
char szSep[8];
int i = 0;
if (!lstrlen(lpNumberStr))
return(0);
if (!GetLocaleInfo(LOCALE_USER_DEFAULT,
LOCALE_STHOUSAND,
szSep,
COUNTOF(szSep)))
szSep[0] = '\'';
c = StrEnd(lpNumberStr);
while ((c = CharPrev(lpNumberStr,c)) != lpNumberStr)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -