Merge pull request #133 from RaiKoHoff/WarnDlg

show warning before loading unknown file
This commit is contained in:
Derick Payne 2017-10-18 09:57:55 +02:00 committed by GitHub
commit 9f5a259e1c
8 changed files with 59 additions and 27 deletions

View File

@ -692,10 +692,15 @@ BOOL EditLoadFile(
int* iEncoding,
int* iEOLMode,
BOOL *pbUnicodeErr,
BOOL *pbFileTooBig)
BOOL *pbFileTooBig,
BOOL *pbUnkownExt)
{
*pbUnicodeErr = FALSE;
*pbFileTooBig = FALSE;
if (pbUnicodeErr)
*pbUnicodeErr = FALSE;
if (pbFileTooBig)
*pbFileTooBig = FALSE;
if (pbUnkownExt)
*pbUnkownExt = FALSE;
HANDLE hFile = CreateFile(pszFile,
GENERIC_READ,
@ -716,12 +721,26 @@ BOOL EditLoadFile(
DWORD dwFileSize = GetFileSize(hFile,NULL);
DWORD dwBufSize = dwFileSize + 16;
// check for unknown extension
LPWSTR lpszExt = PathFindExtension(pszFile);
if (!Style_HasLexerForExt(lpszExt)) {
if (InfoBox(MBYESNO,L"MsgFileUnknownExt",IDS_WARN_UNKNOWN_EXT,lpszExt) != IDYES) {
CloseHandle(hFile);
if (pbUnkownExt)
*pbUnkownExt = TRUE;
Encoding_Source(CPI_NONE);
Encoding_SrcWeak(CPI_NONE);
return FALSE;
}
}
// Check if a warning message should be displayed for large files
DWORD dwFileSizeLimit = IniGetInt(L"Settings2",L"FileLoadWarningMB",1);
if (dwFileSizeLimit != 0 && dwFileSizeLimit * 1024 * 1024 < dwFileSize) {
if (InfoBox(MBYESNO,L"MsgFileSizeWarning",IDS_WARNLOADBIGFILE) != IDYES) {
if (InfoBox(MBYESNO,L"MsgFileSizeWarning",IDS_WARN_LOAD_BIG_FILE) != IDYES) {
CloseHandle(hFile);
*pbFileTooBig = TRUE;
if (pbFileTooBig)
*pbFileTooBig = TRUE;
Encoding_Source(CPI_NONE);
Encoding_SrcWeak(CPI_NONE);
return FALSE;
@ -734,7 +753,8 @@ BOOL EditLoadFile(
if (!lpData)
{
CloseHandle(hFile);
*pbFileTooBig = FALSE;
if (pbFileTooBig)
*pbFileTooBig = FALSE;
Encoding_Source(CPI_NONE);
Encoding_SrcWeak(CPI_NONE);
return FALSE;
@ -755,8 +775,7 @@ BOOL EditLoadFile(
BOOL bPreferOEM = FALSE;
if (bLoadNFOasOEM)
{
PCWSTR pszExt = pszFile + StringCchLenN(pszFile,MAX_PATH) - 4;
if (pszExt >= pszFile && !(StringCchCompareIX(pszExt,L".nfo") && StringCchCompareIX(pszExt,L".diz")))
if (lpszExt && !(StringCchCompareIX(lpszExt,L".nfo") && StringCchCompareIX(lpszExt,L".diz")))
bPreferOEM = TRUE;
}
@ -822,7 +841,8 @@ BOOL EditLoadFile(
(bBOM) ? (cbData)/sizeof(WCHAR) : cbData/sizeof(WCHAR) + 1,lpDataUTF8,(int)GlobalSize(lpDataUTF8),NULL,NULL);
if (convCnt == 0) {
*pbUnicodeErr = TRUE;
if (pbUnicodeErr)
*pbUnicodeErr = TRUE;
convCnt = (DWORD)WideCharToMultiByte(CP_ACP,0,(bBOM) ? (LPWSTR)lpData + 1 : (LPWSTR)lpData,
(-1),lpDataUTF8,(int)GlobalSize(lpDataUTF8),NULL,NULL);
}

View File

@ -53,7 +53,7 @@ BOOL EditIsRecodingNeeded(WCHAR*,int);
char* EditGetClipboardText(HWND,BOOL,int*,int*);
BOOL EditCopyAppend(HWND);
int EditDetectEOLMode(HWND,char*,DWORD);
BOOL EditLoadFile(HWND,LPCWSTR,BOOL,int*,int*,BOOL*,BOOL*);
BOOL EditLoadFile(HWND,LPCWSTR,BOOL,int*,int*,BOOL*,BOOL*,BOOL*);
BOOL EditSaveFile(HWND,LPCWSTR,int,BOOL*,BOOL);
void EditInvertCase(HWND);

View File

@ -7330,7 +7330,7 @@ int UndoRedoSelectionMap(int token, UndoRedoSelection_t* selection)
//
//
BOOL FileIO(BOOL fLoad,LPCWSTR pszFileName,BOOL bNoEncDetect,int *ienc,int *ieol,
BOOL *pbUnicodeErr,BOOL *pbFileTooBig,
BOOL *pbUnicodeErr,BOOL *pbFileTooBig, BOOL* pbUnknownExt,
BOOL *pbCancelDataLoss,BOOL bSaveCopy)
{
WCHAR tch[MAX_PATH+40];
@ -7348,7 +7348,7 @@ BOOL FileIO(BOOL fLoad,LPCWSTR pszFileName,BOOL bNoEncDetect,int *ienc,int *ieol
UpdateWindow(hwndStatus);
if (fLoad) {
fSuccess = EditLoadFile(hwndEdit,pszFileName,bNoEncDetect,ienc,ieol,pbUnicodeErr,pbFileTooBig);
fSuccess = EditLoadFile(hwndEdit,pszFileName,bNoEncDetect,ienc,ieol,pbUnicodeErr,pbFileTooBig,pbUnknownExt);
}
else {
int idx;
@ -7379,9 +7379,10 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp
{
WCHAR tch[MAX_PATH] = { L'\0' };
WCHAR szFileName[MAX_PATH] = { L'\0' };
BOOL fSuccess;
BOOL bUnicodeErr = FALSE;
BOOL bFileTooBig = FALSE;
BOOL bUnknownExt = FALSE;
BOOL fSuccess;
int fileEncoding = CPI_ANSI_DEFAULT;
if (!bDontSave)
@ -7501,7 +7502,7 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp
else
fileEncoding = Encoding_Current(CPI_GET);
fSuccess = FileIO(TRUE,szFileName,bNoEncDetect,&fileEncoding,&iEOLMode,&bUnicodeErr,&bFileTooBig,NULL,FALSE);
fSuccess = FileIO(TRUE,szFileName,bNoEncDetect,&fileEncoding,&iEOLMode,&bUnicodeErr,&bFileTooBig,&bUnknownExt,NULL,FALSE);
if (fSuccess)
Encoding_Current(fileEncoding); // load may change encoding
}
@ -7570,7 +7571,7 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp
MsgBox(MBWARN,IDS_ERR_UNICODE);
}
else if (!bFileTooBig)
else if (!(bFileTooBig || bUnknownExt))
MsgBox(MBWARN,IDS_ERR_LOADFILE,szFileName);
return(fSuccess);
@ -7708,7 +7709,7 @@ BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy)
if (SaveFileDlg(hwndMain,tchFile,COUNTOF(tchFile),tchInitialDir))
{
int fileEncoding = Encoding_Current(CPI_GET);
fSuccess = FileIO(FALSE, tchFile, FALSE, &fileEncoding, &iEOLMode, NULL, NULL, &bCancelDataLoss, bSaveCopy);
fSuccess = FileIO(FALSE, tchFile, FALSE, &fileEncoding, &iEOLMode, NULL, NULL, NULL, &bCancelDataLoss, bSaveCopy);
//~if (fSuccess) Encoding_Current(fileEncoding); // save should not change encoding
if (fSuccess)
{
@ -7734,7 +7735,7 @@ BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy)
}
else {
int fileEncoding = Encoding_Current(CPI_GET);
fSuccess = FileIO(FALSE,szCurFile,FALSE,&fileEncoding,&iEOLMode,NULL,NULL,&bCancelDataLoss,FALSE);
fSuccess = FileIO(FALSE,szCurFile,FALSE,&fileEncoding,&iEOLMode,NULL,NULL,NULL,&bCancelDataLoss,FALSE);
//~if (fSuccess) Encoding_Current(fileEncoding); // save should not change encoding
}
@ -7771,7 +7772,7 @@ BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy)
if (GetTempPath(MAX_PATH,lpTempPathBuffer) &&
GetTempFileName(lpTempPathBuffer,TEXT("NP3"),0,szTempFileName)) {
int fileEncoding = Encoding_Current(CPI_GET);
if (FileIO(FALSE,szTempFileName,FALSE,&fileEncoding,&iEOLMode,NULL,NULL,&bCancelDataLoss,TRUE)) {
if (FileIO(FALSE,szTempFileName,FALSE,&fileEncoding,&iEOLMode,NULL,NULL,NULL,&bCancelDataLoss,TRUE)) {
//~Encoding_Current(fileEncoding); // save should not change encoding
WCHAR szArguments[2048] = { L'\0' };
LPWSTR lpCmdLine = GetCommandLine();

View File

@ -151,7 +151,7 @@ void RestoreSelectionAction(int,DoAction);
int UndoRedoSelectionMap(int,UndoRedoSelection_t*);
BOOL FileIO(BOOL,LPCWSTR,BOOL,int*,int*,BOOL*,BOOL*,BOOL*,BOOL);
BOOL FileIO(BOOL,LPCWSTR,BOOL,int*,int*,BOOL*,BOOL*,BOOL*,BOOL*,BOOL);
BOOL FileLoad(BOOL,BOOL,BOOL,BOOL,LPCWSTR);
BOOL FileRevert(LPCWSTR);
BOOL FileSave(BOOL,BOOL,BOOL,BOOL);

Binary file not shown.

View File

@ -3413,10 +3413,9 @@ PEDITLEXER __fastcall Style_SniffShebang(char *pchText)
//
// Style_MatchLexer()
//
PEDITLEXER __fastcall Style_MatchLexer(LPCWSTR lpszMatch,BOOL bCheckNames)
{
PEDITLEXER __fastcall Style_MatchLexer(LPCWSTR lpszMatch,BOOL bCheckNames) {
int i;
WCHAR tch[256+16] = { L'\0' };
WCHAR tch[256 + 16] = { L'\0' };
WCHAR *p1,*p2;
if (!bCheckNames) {
@ -3426,7 +3425,7 @@ PEDITLEXER __fastcall Style_MatchLexer(LPCWSTR lpszMatch,BOOL bCheckNames)
StringCchCopy(tch,COUNTOF(tch),pLexArray[i]->szExtensions);
p1 = tch;
while (*p1) {
p2 = StrChr(p1, L';');
p2 = StrChr(p1,L';');
if (p2)
*p2 = L'\0';
else
@ -3434,7 +3433,7 @@ PEDITLEXER __fastcall Style_MatchLexer(LPCWSTR lpszMatch,BOOL bCheckNames)
StrTrim(p1,L" .");
if (StringCchCompareIX(p1,lpszMatch) == 0)
return(pLexArray[i]);
p1 = p2+1;
p1 = p2 + 1;
}
}
}
@ -3454,6 +3453,17 @@ PEDITLEXER __fastcall Style_MatchLexer(LPCWSTR lpszMatch,BOOL bCheckNames)
}
//=============================================================================
//
// Style_HasLexerForExt()
//
BOOL Style_HasLexerForExt(LPCWSTR lpszExt)
{
if (lpszExt && (*lpszExt == L'.')) ++lpszExt;
return (lpszExt && Style_MatchLexer(lpszExt,FALSE)) ? TRUE : FALSE;
}
//=============================================================================
//
// Style_SetLexerFromFile()
@ -3512,8 +3522,7 @@ void Style_SetLexerFromFile(HWND hwnd,LPCWSTR lpszFile)
if (!bFound && bAutoSelect && /* bAutoSelect == FALSE skips lexer search */
(lpszFile && StringCchLenN(lpszFile,MAX_PATH) > 0 && *lpszExt)) {
if (*lpszExt == L'.')
lpszExt++;
if (*lpszExt == L'.') ++lpszExt;
if (!fNoCGIGuess && (StringCchCompareIX(lpszExt,L"cgi") == 0 || StringCchCompareIX(lpszExt,L"fcgi") == 0)) {
char tchText[256] = { L'\0' };

View File

@ -88,6 +88,7 @@ void Style_SetStyles(HWND,int,LPCWSTR);
void Style_SetFontQuality(HWND,LPCWSTR);
void Style_GetCurrentLexerName(LPWSTR,int);
int Style_GetLexerIconId(PEDITLEXER);
BOOL Style_HasLexerForExt(LPCWSTR);
HTREEITEM Style_AddLexerToTreeView(HWND,PEDITLEXER);
INT_PTR CALLBACK Styles_ConfigDlgProc(HWND,UINT,WPARAM,LPARAM);
void Style_ConfigDlg(HWND);

View File

@ -432,7 +432,7 @@
#define IDS_ERR_ENCODINGNA 50014
#define IDS_ERR_UNICODE 50015
#define IDS_ERR_UNICODE2 50016
#define IDS_WARNLOADBIGFILE 50017
#define IDS_WARN_LOAD_BIG_FILE 50017
#define IDS_ERR_DROP 50018
#define IDS_ASK_SAVE 50019
#define IDS_ASK_REVERT 50020
@ -457,6 +457,7 @@
#define IDS_SETTINGSNOTSAVED 50039
#define IDS_EXPORT_FAIL 50040
#define IDS_ERR_ACCESSDENIED 50041
#define IDS_WARN_UNKNOWN_EXT 50042
#define IDS_CMDLINEHELP 60000
#define IDM_EDIT_INSERT_GUID 60001
#define IDC_STATIC -1