-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathCReadManager.cpp
194 lines (177 loc) · 5.8 KB
/
CReadManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*! @file */
/*
Copyright (C) 2008, kobake
Copyright (C) 2018-2022, Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "StdAfx.h"
#include <io.h> // _access
#include "CReadManager.h"
#include "CEditApp.h" // CAppExitException
#include "window/CEditWnd.h"
#include "charset/CCodeMediator.h"
#include "io/CFileLoad.h"
#include "util/window.h"
#include "CSelectLang.h"
#include "String_define.h"
/*!
ファイルを読み込んで格納する(分割読み込みテスト版)
@version 2.0
@note Windows用にコーディングしてある
@retval TRUE 正常読み込み
@retval FALSE エラー(またはユーザーによるキャンセル?)
@date 2002/08/30 Moca 旧ReadFileを元に作成 ファイルアクセスに関する部分をCFileLoadで行う
@date 2003/07/26 ryoji BOMの状態の取得を追加
*/
EConvertResult CReadManager::ReadFile_To_CDocLineMgr(
CDocLineMgr* pcDocLineMgr, //!< [out]
const SLoadInfo& sLoadInfo, //!< [in]
SFileInfo* pFileInfo //!< [out]
)
{
LPCWSTR pszPath = sLoadInfo.cFilePath.c_str();
// 文字コード種別
const STypeConfigMini* type = NULL;
if( !CDocTypeManager().GetTypeConfigMini( sLoadInfo.nType, &type ) ){
return RESULT_FAILURE;
}
ECodeType eCharCode = sLoadInfo.eCharCode;
if (CODE_AUTODETECT == eCharCode) {
CCodeMediator cmediator( type->m_encoding );
eCharCode = cmediator.CheckKanjiCodeOfFile( pszPath );
}
if (!IsValidCodeOrCPType( eCharCode )) {
eCharCode = type->m_encoding.m_eDefaultCodetype; // 2011.01.24 ryoji デフォルト文字コード
}
bool bBom;
if (eCharCode == type->m_encoding.m_eDefaultCodetype) {
bBom = type->m_encoding.m_bDefaultBom; // 2011.01.24 ryoji デフォルトBOM
}
else{
bBom = CCodeTypeName( eCharCode ).IsBomDefOn();
}
pFileInfo->SetCodeSet( eCharCode, bBom );
/* 既存データのクリア */
pcDocLineMgr->DeleteAllLine();
/* 処理中のユーザー操作を可能にする */
if( !::BlockingHook( NULL ) ){
return RESULT_FAILURE; //######INTERRUPT
}
EConvertResult eRet = RESULT_COMPLETE;
try{
CFileLoad cfl(type->m_encoding);
bool bBigFile;
#ifdef _WIN64
bBigFile = true;
#else
bBigFile = false;
#endif
// ファイルを開く
// ファイルを閉じるにはFileCloseメンバ又はデストラクタのどちらかで処理できます
// Jul. 28, 2003 ryoji BOMパラメータ追加
cfl.FileOpen( pszPath, bBigFile, eCharCode, GetDllShareData().m_Common.m_sFile.GetAutoMIMEdecode(), &bBom );
pFileInfo->SetBomExist( bBom );
/* ファイル時刻の取得 */
FILETIME FileTime;
if( cfl.GetFileTime( NULL, NULL, &FileTime ) ){
pFileInfo->SetFileTime( FileTime );
}
// ReadLineはファイルから 文字コード変換された1行を読み出します
// エラー時はthrow CError_FileRead を投げます
CEol cEol;
CNativeW cUnicodeBuffer;
EConvertResult eRead;
constexpr DWORD timeInterval = 33;
ULONGLONG nextTime = GetTickCount64() + timeInterval;
while( RESULT_FAILURE != (eRead = cfl.ReadLine( &cUnicodeBuffer, &cEol )) ){
if(eRead==RESULT_LOSESOME){
eRet = RESULT_LOSESOME;
}
const wchar_t* pLine = cUnicodeBuffer.GetStringPtr();
int nLineLen = cUnicodeBuffer.GetStringLength();
CDocEditAgent(pcDocLineMgr).AddLineStrX( pLine, nLineLen );
//経過通知
ULONGLONG currTime = GetTickCount64();
if(currTime >= nextTime){
nextTime += timeInterval;
NotifyProgress(cfl.GetPercent());
// 処理中のユーザー操作を可能にする
if( !::BlockingHook( NULL ) ){
throw CAppExitException(); //中断検出
}
}
}
// ファイルをクローズする
cfl.FileClose();
}
catch(const CAppExitException&){
//WM_QUITが発生した
return RESULT_FAILURE;
}
catch( const CError_FileOpen& ex ){
eRet = RESULT_FAILURE;
if (ex.Reason() == CError_FileOpen::TOO_BIG) {
// ファイルサイズが大きすぎる (32bit 版の場合は 2GB あたりが上限)
ErrorMessage(
CEditWnd::getInstance()->GetHwnd(),
LS(STR_ERR_DLGDOCLM_TOOBIG),
pszPath
);
}
else if( !fexist( pszPath )){
// ファイルがない
ErrorMessage(
CEditWnd::getInstance()->GetHwnd(),
LS(STR_ERR_DLGDOCLM1), //Mar. 24, 2001 jepro 若干修正
pszPath
);
}
else if( -1 == _waccess( pszPath, 4 )){
// 読み込みアクセス権がない
ErrorMessage(
CEditWnd::getInstance()->GetHwnd(),
LS(STR_ERR_DLGDOCLM2),
pszPath
);
}
else{
ErrorMessage(
CEditWnd::getInstance()->GetHwnd(),
LS(STR_ERR_DLGDOCLM3),
pszPath
);
}
}
catch( const CError_FileRead& ){
eRet = RESULT_FAILURE;
ErrorMessage(
CEditWnd::getInstance()->GetHwnd(),
LS(STR_ERR_DLGDOCLM4),
pszPath
);
/* 既存データのクリア */
pcDocLineMgr->DeleteAllLine();
} // 例外処理終わり
NotifyProgress(0);
/* 処理中のユーザー操作を可能にする */
if( !::BlockingHook( NULL ) ){
return RESULT_FAILURE; //####INTERRUPT
}
/* 行変更状態をすべてリセット */
// CModifyVisitor().ResetAllModifyFlag(pcDocLineMgr, 0);
return eRet;
}