-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCWriteManager.cpp
176 lines (160 loc) · 4.96 KB
/
CWriteManager.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
/*! @file */
/*
Copyright (C) 2018-2021, 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 "CWriteManager.h"
#include <list>
#include "doc/logic/CDocLineMgr.h"
#include "doc/logic/CDocLine.h"
#include "CEditApp.h" // CAppExitException
#include "window/CEditWnd.h"
#include "charset/CCodeFactory.h"
#include "charset/CCodeBase.h"
#include "charset/CUnicode.h"
#include "io/CIoBridge.h"
#include "io/CBinaryStream.h"
#include "util/window.h"
/*! バッファ内容をファイルに書き出す (テスト用)
@note Windows用にコーディングしてある
@date 2003.07.26 ryoji BOM引数追加
*/
EConvertResult CWriteManager::WriteFile_From_CDocLineMgr(
const CDocLineMgr& pcDocLineMgr, //!< [in]
const SSaveInfo& sSaveInfo //!< [in]
)
{
EConvertResult nRetVal = RESULT_COMPLETE;
std::unique_ptr<CCodeBase> pcCodeBase( CCodeFactory::CreateCodeBase(sSaveInfo.eCharCode,0) );
{
// 変換テスト
CNativeW buffer = L"abcde";
CMemory tmp;
EConvertResult e = pcCodeBase->UnicodeToCode( buffer, &tmp );
if(e==RESULT_FAILURE){
nRetVal=RESULT_FAILURE;
ErrorMessage(
CEditWnd::getInstance()->GetHwnd(),
LS(STR_FILESAVE_CONVERT_ERROR),
sSaveInfo.cFilePath.c_str()
);
return nRetVal;
}
}
try
{
//ファイルオープン
CBinaryOutputStream out(sSaveInfo.cFilePath,true);
//各行出力
int nLineNumber = 0;
const CDocLine* pcDocLine = pcDocLineMgr.GetDocLineTop();
// 1行目
{
++nLineNumber;
CMemory cmemOutputBuffer;
{
CNativeW cstrSrc;
CMemory cstrBomCheck;
pcCodeBase->GetBom( &cstrBomCheck );
if( sSaveInfo.bBomExist && 0 < cstrBomCheck.GetRawLength() ){
// 1行目にはBOMを付加する。エンコーダでbomがある場合のみ付加する。
CUnicode().GetBom( cstrSrc._GetMemory() );
}
if( pcDocLine ){
cstrSrc.AppendNativeData( pcDocLine->_GetDocLineDataWithEOL() );
}
EConvertResult e = pcCodeBase->UnicodeToCode( cstrSrc, &cmemOutputBuffer );
if(e==RESULT_LOSESOME){
nRetVal=RESULT_LOSESOME;
}
if(e==RESULT_FAILURE){
nRetVal=RESULT_FAILURE;
ErrorMessage(
CEditWnd::getInstance()->GetHwnd(),
LS(STR_FILESAVE_CONVERT_ERROR),
sSaveInfo.cFilePath.c_str()
);
throw CError_FileWrite();
}
}
out.Write(cmemOutputBuffer.GetRawPtr(), cmemOutputBuffer.GetRawLength());
if( pcDocLine ){
pcDocLine = pcDocLine->GetNextLine();
}
}
CMemory cmemOutputBuffer;
constexpr DWORD userInterfaceInterval = 33;
DWORD prevTime = GetTickCount() + userInterfaceInterval;
while( pcDocLine ){
++nLineNumber;
//経過通知
DWORD currTime = GetTickCount();
DWORD diffTime = currTime - prevTime;
if(diffTime >= userInterfaceInterval){
prevTime = currTime;
NotifyProgress(nLineNumber * 100 / pcDocLineMgr.GetLineCount());
// 処理中のユーザー操作を可能にする
if( !::BlockingHook( NULL ) ){
throw CAppExitException(); //中断検出
}
}
//1行出力 -> cmemOutputBuffer
{
// 書き込み時のコード変換 cstrSrc -> cmemOutputBuffer
EConvertResult e = pcCodeBase->UnicodeToCode(
pcDocLine->_GetDocLineDataWithEOL(),
&cmemOutputBuffer
);
if(e==RESULT_LOSESOME){
if(nRetVal==RESULT_COMPLETE)nRetVal=RESULT_LOSESOME;
}
if(e==RESULT_FAILURE){
nRetVal=RESULT_FAILURE;
ErrorMessage(
CEditWnd::getInstance()->GetHwnd(),
LS(STR_FILESAVE_CONVERT_ERROR),
sSaveInfo.cFilePath.c_str()
);
break;
}
}
//ファイルに出力 cmemOutputBuffer -> fp
out.Write(cmemOutputBuffer.GetRawPtr(), cmemOutputBuffer.GetRawLength());
//次の行へ
pcDocLine = pcDocLine->GetNextLine();
}
//ファイルクローズ
out.Close();
}
catch(const CError_FileOpen&){ //########### 現時点では、この例外が発生した場合は正常に動作できない
ErrorMessage(
CEditWnd::getInstance()->GetHwnd(),
LS(STR_SAVEAGENT_OTHER_APP),
sSaveInfo.cFilePath.c_str()
);
nRetVal = RESULT_FAILURE;
}
catch(const CError_FileWrite&){
nRetVal = RESULT_FAILURE;
}
catch(const CAppExitException&){
//中断検出
return RESULT_FAILURE;
}
return nRetVal;
}