Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/teraterm/teraterm/filesys.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10614 - (show annotations) (download) (as text)
Wed Mar 1 14:18:19 2023 UTC (12 months, 1 week ago) by zmatsuo
File MIME type: text/x-c++src
File size: 11702 byte(s)
lngファイル名変数を ANSI版から Unicode 版へ切り替え

- tttset.UILanguageFile 参照部分を tttset.UILanguageFileW へ変更
- SetI18nMenuStrs() -> SetI18nMenuStrsW()
- GetI18nStrWA() -> GetI18nStrWW()
- MessageBox() -> MessageBoxW()
- GetCommonDialogFilterW() -> GetCommonDialogFilterWW()
  - GetCommonDialogFilterW() は ANSI版lngファイル名を参照
  - GetCommonDialogFilterW() 削除
1 /*
2 * Copyright (C) 1994-1998 T. Teranishi
3 * (C) 2005- TeraTerm Project
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /* TERATERM.EXE, file transfer routines */
31 #include <stdio.h>
32 #include <windows.h>
33
34 #include "teraterm.h"
35 #include "tttypes.h"
36 #include "ftdlg.h"
37 #include "ttwinman.h"
38 #include "commlib.h"
39 #include "ttcommon.h"
40 #include "ttdde.h"
41 #include "vtterm.h"
42 #include "asprintf.h"
43 #include "codeconv.h"
44
45 #include "filesys.h"
46
47 typedef enum {
48 FS_BRACKET_NONE,
49 FS_BRACKET_START,
50 FS_BRACKET_END,
51 } FileBracketMode_t;
52
53 #define FILE_SEND_BUF_SIZE 8192
54 struct FileSendHandler {
55 CHAR buf[FILE_SEND_BUF_SIZE];
56 int pos;
57 int end;
58 };
59
60 typedef struct {
61 wchar_t *FullName;
62
63 HANDLE FileHandle;
64 LONG FileSize; // uint64_t FileSize; TODO
65 LONG ByteCount; // uint64_t ByteCount;
66
67 DWORD StartTime;
68 BOOL FilePause;
69
70 BOOL FileRetrySend, FileRetryEcho, FileCRSend, FileReadEOF, BinaryMode;
71
72 FileBracketMode_t FileBracketMode;
73 int FileBracketPtr;
74
75 PFileTransDlg SendDlg;
76 int FileDlgRefresh; // ?
77
78 wchar_t SendChar;
79
80 struct FileSendHandler FileSendHandler;
81 } TFileVar;
82 typedef TFileVar *PFileVar;
83
84 static PFileVar SendVar = NULL;
85 static BOOL FSend = FALSE;
86
87 static const char BracketStartStr[] = "\033[200~";
88 static const char BracketEndStr[] = "\033[201~";
89
90 static BOOL OpenFTDlg(PFileVar fv)
91 {
92 PFileTransDlg FTDlg;
93
94 FTDlg = new CFileTransDlg();
95 if (FTDlg == NULL) {
96 return FALSE;
97 }
98
99 wchar_t *DlgCaption;
100 wchar_t *uimsg = TTGetLangStrW("Tera Term", "FILEDLG_TRANS_TITLE_SENDFILE", L"Send file", ts.UILanguageFileW);
101 aswprintf(&DlgCaption, L"Tera Term: %s", uimsg);
102 free(uimsg);
103
104 CFileTransDlg::Info info;
105 info.UILanguageFileW = ts.UILanguageFileW;
106 info.OpId = CFileTransDlg::OpSendFile;
107 info.DlgCaption = DlgCaption;
108 info.FileName = NULL;
109 info.FullName = fv->FullName;
110 info.HideDialog = FALSE;
111 info.HMainWin = HVTWin;
112
113 FTDlg->Create(hInst, &info);
114 FTDlg->RefreshNum(0, fv->FileSize, fv->ByteCount);
115
116 free(DlgCaption);
117
118 fv->FilePause = FALSE;
119 fv->StartTime = GetTickCount();
120 fv->SendDlg = FTDlg; /* File send */
121
122 return TRUE;
123 }
124
125 #if 0
126 static void ShowFTDlg(WORD OpId)
127 {
128 if (fv->SendDlg != NULL) {
129 fv->SendDlg->ShowWindow(SW_SHOWNORMAL);
130 SetForegroundWindow(fv->SendDlg->GetSafeHwnd());
131 }
132 }
133 #endif
134
135 static BOOL NewFileVar(PFileVar *pfv)
136 {
137 if ((*pfv) != NULL) {
138 return TRUE;
139 }
140
141 PFileVar fv = (PFileVar)malloc(sizeof(TFileVar));
142 if (fv == NULL) {
143 return FALSE;
144 }
145 memset(fv, 0, sizeof(TFileVar));
146 fv->FileHandle = INVALID_HANDLE_VALUE;
147
148 *pfv = fv;
149 return TRUE;
150 }
151
152 static void FreeFileVar(PFileVar *pfv)
153 {
154 if ((*pfv)==NULL) {
155 return;
156 }
157
158 PFileVar fv = *pfv;
159 if (fv->FileHandle != INVALID_HANDLE_VALUE) {
160 CloseHandle(fv->FileHandle);
161 fv->FileHandle = INVALID_HANDLE_VALUE;
162 }
163 if (fv->FullName != NULL) {
164 free(fv->FullName);
165 fv->FullName = NULL;
166 }
167 free(fv);
168 *pfv = NULL;
169 }
170
171 /**
172 * �t�@�C�����M
173 */
174 BOOL FileSendStart(const wchar_t *filename, int binary)
175 {
176 if (SendVar != NULL) {
177 return FALSE;
178 }
179 if (! cv.Ready || FSend) {
180 return FALSE;
181 }
182 if (filename == NULL) {
183 return FALSE;
184 }
185 if (ProtoGetProtoFlag())
186 {
187 FreeFileVar(&SendVar);
188 return FALSE;
189 }
190 if (!NewFileVar(&SendVar)) {
191 return FALSE;
192 }
193
194 FSend = TRUE;
195 PFileVar fv = SendVar;
196
197 SendVar->FullName = _wcsdup(filename);
198 ts.TransBin = binary;
199
200 SendVar->FileHandle = CreateFileW(SendVar->FullName, GENERIC_READ, FILE_SHARE_READ, NULL,
201 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
202 if (SendVar->FileHandle == INVALID_HANDLE_VALUE) {
203 FileSendEnd();
204 return FALSE;
205 }
206 SendVar->ByteCount = 0;
207 SendVar->FileSize = GetFSize64H(SendVar->FileHandle);
208
209 TalkStatus = IdTalkFile;
210 fv->FileRetrySend = FALSE;
211 fv->FileRetryEcho = FALSE;
212 fv->FileCRSend = FALSE;
213 fv->FileReadEOF = FALSE;
214 fv->FileSendHandler.pos = 0;
215 fv->FileSendHandler.end = 0;
216 fv->FileDlgRefresh = 0;
217
218 if (BracketedPasteMode()) {
219 fv->FileBracketMode = FS_BRACKET_START;
220 fv->FileBracketPtr = 0;
221 fv->BinaryMode = TRUE;
222 }
223 else {
224 fv->FileBracketMode = FS_BRACKET_NONE;
225 fv->BinaryMode = ts.TransBin;
226 }
227
228 if (! OpenFTDlg(SendVar)) {
229 FileSendEnd();
230 return FALSE;
231 }
232
233 return TRUE;
234 }
235
236 void FileSendEnd(void)
237 {
238 if (FSend) {
239 PFileVar fv = SendVar;
240 FSend = FALSE;
241 TalkStatus = IdTalkKeyb;
242 if (fv->SendDlg!=NULL)
243 {
244 fv->SendDlg->DestroyWindow();
245 fv->SendDlg = NULL;
246 }
247 FreeFileVar(&SendVar);
248 }
249
250 EndDdeCmnd(0);
251 }
252
253 void FileSendPause(BOOL Pause)
254 {
255 PFileVar fv = SendVar;
256 if (Pause) {
257 fv->FilePause = TRUE;
258 }
259 else {
260 fv->FilePause = FALSE;
261 }
262 }
263
264 static int FSOut1(PFileVar fv)
265 {
266 if (fv->BinaryMode) {
267 BYTE b = (BYTE)fv->SendChar;
268 return CommBinaryOut(&cv, (PCHAR)&b, 1);
269 }
270 else {
271 wchar_t wc = fv->SendChar;
272 if ((wc >= 0x20) || (wc == 0x09) || (wc == 0x0A) || (wc == 0x0D)) {
273 return CommTextOutW(&cv, &wc, 1);
274 }
275 else {
276 return 1;
277 }
278 }
279 }
280
281 static int FSEcho1(PFileVar fv)
282 {
283 if (fv->BinaryMode) {
284 BYTE b = (BYTE)fv->SendChar;
285 return CommBinaryEcho(&cv,(PCHAR)&b,1);
286 } else {
287 return CommTextEchoW(&cv, &fv->SendChar, 1);
288 }
289 }
290
291 // �������������������������g��
292 // - BinaryMode == true
293 // - FileBracketMode == FS_BRACKET_NONE
294 // - cv.TelFlag == false
295 // - ts.LocalEcho == 0
296 static void FileSendBinayBoost(void)
297 {
298 WORD c, fc;
299 LONG BCOld;
300 DWORD read_bytes;
301 PFileVar fv = SendVar;
302
303 if ((fv->SendDlg == NULL) || (fv->FilePause == TRUE))
304 return;
305
306 BCOld = SendVar->ByteCount;
307
308 if (fv->FileRetrySend)
309 {
310 c = CommRawOut(&cv, &(fv->FileSendHandler.buf[fv->FileSendHandler.pos]),
311 fv->FileSendHandler.end - fv->FileSendHandler.pos);
312 fv->FileSendHandler.pos += c;
313 fv->FileRetrySend = (fv->FileSendHandler.end != fv->FileSendHandler.pos);
314 if (fv->FileRetrySend)
315 return;
316 }
317
318 do {
319 if (fv->FileSendHandler.pos == fv->FileSendHandler.end) {
320 ReadFile(SendVar->FileHandle, &(fv->FileSendHandler.buf[0]), sizeof(fv->FileSendHandler.buf), &read_bytes, NULL);
321 fc = LOWORD(read_bytes);
322 fv->FileSendHandler.pos = 0;
323 fv->FileSendHandler.end = fc;
324 } else {
325 fc = fv->FileSendHandler.end - fv->FileSendHandler.end;
326 }
327
328 if (fc != 0)
329 {
330 c = CommRawOut(&cv, &(fv->FileSendHandler.buf[fv->FileSendHandler.pos]),
331 fv->FileSendHandler.end - fv->FileSendHandler.pos);
332 fv->FileSendHandler.pos += c;
333 fv->FileRetrySend = (fv->FileSendHandler.end != fv->FileSendHandler.pos);
334 SendVar->ByteCount = SendVar->ByteCount + c;
335 if (fv->FileRetrySend)
336 {
337 if (SendVar->ByteCount != BCOld) {
338 fv->SendDlg->RefreshNum(SendVar->StartTime, SendVar->FileSize, SendVar->ByteCount);
339 }
340 return;
341 }
342 }
343 fv->FileDlgRefresh = SendVar->ByteCount;
344 fv->SendDlg->RefreshNum(SendVar->StartTime, SendVar->FileSize, SendVar->ByteCount);
345 BCOld = SendVar->ByteCount;
346 if (fc != 0)
347 return;
348 } while (fc != 0);
349
350 FileSendEnd();
351 }
352
353 void FileSend(void)
354 {
355 WORD c, fc;
356 LONG BCOld;
357 PFileVar fv = SendVar;
358
359 if (cv.PortType == IdSerial && ts.FileSendHighSpeedMode &&
360 fv->BinaryMode && fv->FileBracketMode == FS_BRACKET_NONE && !cv.TelFlag &&
361 (ts.LocalEcho == 0) && (ts.Baud >= 115200)) {
362 return FileSendBinayBoost();
363 }
364
365 if ((fv->SendDlg == NULL) || (fv->FilePause == TRUE))
366 return;
367
368 BCOld = SendVar->ByteCount;
369
370 if (fv->FileRetrySend)
371 {
372 fv->FileRetryEcho = (ts.LocalEcho>0);
373 c = FSOut1(fv);
374 fv->FileRetrySend = (c==0);
375 if (fv->FileRetrySend)
376 return;
377 }
378
379 if (fv->FileRetryEcho)
380 {
381 c = FSEcho1(fv);
382 fv->FileRetryEcho = (c==0);
383 if (fv->FileRetryEcho)
384 return;
385 }
386
387 do {
388 if (fv->FileBracketMode == FS_BRACKET_START) {
389 fv->SendChar = BracketStartStr[fv->FileBracketPtr++];
390 fc = 1;
391
392 if (fv->FileBracketPtr >= sizeof(BracketStartStr) - 1) {
393 fv->FileBracketMode = FS_BRACKET_END;
394 fv->FileBracketPtr = 0;
395 fv->BinaryMode = ts.TransBin;
396 }
397 }
398 else if (!fv->FileReadEOF) {
399 DWORD read_bytes;
400 BYTE FileByte;
401 ReadFile(SendVar->FileHandle, &FileByte, 1, &read_bytes, NULL);
402 fc = LOWORD(read_bytes);
403 SendVar->ByteCount = SendVar->ByteCount + fc;
404
405 if (FileByte==0x0A && fc == 1 && fv->FileCRSend) {
406 // CR ������������ 0x0A ������ -> �t�@�C������1�o�C�g����
407 ReadFile(SendVar->FileHandle, &FileByte, 1, &read_bytes, NULL);
408 fc = LOWORD(read_bytes);
409 SendVar->ByteCount = SendVar->ByteCount + fc;
410 }
411
412 if (IsDBCSLeadByte(FileByte)) {
413 // DBCS��1byte�������� -> �t�@�C������1�o�C�g������ Unicode ������
414 char dbcs[2];
415 dbcs[0] = FileByte;
416
417 ReadFile(SendVar->FileHandle, &dbcs[1], 1, &read_bytes ,NULL);
418 fc = LOWORD(read_bytes);
419 SendVar->ByteCount = SendVar->ByteCount + fc;
420
421 unsigned int u32;
422 MBCPToUTF32(dbcs, 2, CP_ACP, &u32);
423 fv->SendChar = u32;
424 } else {
425 fv->SendChar = FileByte;
426 }
427 }
428 else {
429 fc = 0;
430 }
431
432 if (fc == 0 && fv->FileBracketMode == FS_BRACKET_END) {
433 fv->FileReadEOF = TRUE;
434 fv->SendChar = BracketEndStr[fv->FileBracketPtr++];
435 fc = 1;
436 fv->BinaryMode = TRUE;
437
438 if (fv->FileBracketPtr >= sizeof(BracketEndStr) - 1) {
439 fv->FileBracketMode = FS_BRACKET_NONE;
440 fv->FileBracketPtr = 0;
441 }
442 }
443
444
445 if (fc!=0)
446 {
447 c = FSOut1(fv);
448 fv->FileCRSend = (ts.TransBin == 0) && (fv->SendChar == 0x0D);
449 fv->FileRetrySend = (c == 0);
450 if (fv->FileRetrySend)
451 {
452 if (SendVar->ByteCount != BCOld) {
453 fv->SendDlg->RefreshNum(SendVar->StartTime, SendVar->FileSize, SendVar->ByteCount);
454 }
455 return;
456 }
457 if (ts.LocalEcho>0)
458 {
459 c = FSEcho1(fv);
460 fv->FileRetryEcho = (c==0);
461 if (fv->FileRetryEcho)
462 return;
463 }
464 }
465 if ((fc==0) || ((SendVar->ByteCount % 100 == 0) && (fv->FileBracketPtr == 0))) {
466 fv->SendDlg->RefreshNum(SendVar->StartTime, SendVar->FileSize, SendVar->ByteCount);
467 BCOld = SendVar->ByteCount;
468 if (fc!=0)
469 return;
470 }
471 } while (fc!=0);
472
473 FileSendEnd();
474 }
475
476 BOOL IsSendVarNULL()
477 {
478 return SendVar == NULL;
479 }
480
481 /**
482 * TODO: IsSendVarNULL() ����������?
483 */
484 BOOL FileSnedIsSending(void)
485 {
486 return FSend;
487 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26