-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileFunc.cpp
More file actions
222 lines (171 loc) · 4.38 KB
/
FileFunc.cpp
File metadata and controls
222 lines (171 loc) · 4.38 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "stdafx.h"
#include <Windows.h>
#include <commdlg.h>
#define ANSI 0
static OPENFILENAME ofn;
void FileInitialize(HWND hwnd)
{
static TCHAR szFilter[] = L"Java源文件 (*.Java)\0*.java\0"
L"文本文档 (*.txt)\0*.txt\0"
L"All Files(*.*)\0*.*\0\0";
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = NULL;
ofn.lpstrFilter = szFilter;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 0;
ofn.lpstrFile = NULL;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = MAX_PATH;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = 0;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = TEXT("java");
ofn.lCustData = 0L;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = NULL;
}
BOOL FileOpen(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
ofn.hwndOwner = hwnd;
ofn.lpstrFile = pstrFileName;
ofn.lpstrFileTitle = pstrTitleName;
ofn.lpstrTitle = L"打开文件";
ofn.Flags = OFN_HIDEREADONLY;
return GetOpenFileName(&ofn);
}
BOOL FileSave(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
ofn.hwndOwner = hwnd;
ofn.lpstrFile = pstrFileName;
ofn.lpstrFileTitle = pstrTitleName;
ofn.lpstrTitle = L"另存为";
ofn.Flags = OFN_OVERWRITEPROMPT;
return GetSaveFileName(&ofn);
}
BOOL FileRead(HWND hwndEdit, PTSTR pstrFileName)
{
BYTE bySwap;
DWORD dwBytesRead;
HANDLE hFile;
int i, iFileLength, iUniTest;
PBYTE pBuffer, pText, pConv;
// 打开文件
if (INVALID_HANDLE_VALUE == (hFile = CreateFile(pstrFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)) )
return FALSE;
// 获取文件大小
// 加EOF
iFileLength = GetFileSize(hFile, NULL);
pBuffer = new BYTE[iFileLength + 2];
// 读文件并且添加EOF
ReadFile(hFile, pBuffer, iFileLength, &dwBytesRead, NULL);
CloseHandle(hFile);
pBuffer[iFileLength] = 0;
pBuffer[iFileLength + 1] = 0;
// 检查是否为UNICODE
iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE;
if (IsTextUnicode(pBuffer, iFileLength, &iUniTest))
{
pText = pBuffer + 2;
iFileLength -= 2;
if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
{
for (i = 0; i < iFileLength / 2; i++)
{
bySwap = ((BYTE *)pText)[2 * i];
((BYTE *)pText)[2 * i] = ((BYTE *)pText)[2 * i + 1];
((BYTE *)pText)[2 * i + 1] = bySwap;
}
}
// 分配内存
pConv = new BYTE[iFileLength + 2];
// 转换UNICODE到非UNICODE
#ifndef UNICODE
WideCharToMultiByte(CP_ACP, 0, (PWSTR)pText, -1, pConv,
iFileLength + 2, NULL, NULL);
// If the edit control is Unicode, just copy the string
#else
lstrcpy((PTSTR)pConv, (PTSTR)pText);
#endif
}
else // 文件非UNICODE
{
pText = pBuffer;
// 为字符分配可用内存空间
pConv = new BYTE[iFileLength * 2 + 2];
// 转换UNICODE 到 ANSI
#ifdef UNICODE
MultiByteToWideChar(CP_ACP, 0, (LPCCH)pText, -1, (PTSTR)pConv, iFileLength + 1);
// 复制缓冲区
#else
lstrcpy((PTSTR)pConv, (PTSTR)pText);
#endif
}
SetWindowText(hwndEdit, (PTSTR)pConv);
delete[] pBuffer;
delete[] pConv;
return TRUE;
}
BOOL FileWrite(HWND hwndEdit, PTSTR pstrFileName, bool Format = UNICODE)
{
DWORD dwBytesWritten;
HANDLE hFile;
int iLength;
PTSTR pstrBuffer;
PCHAR Temp;
WORD wByteOrderMark = 0xFEFF;
//打开或新建文件
if (INVALID_HANDLE_VALUE ==
(hFile = CreateFile(pstrFileName, GENERIC_WRITE, 0,
NULL, CREATE_ALWAYS, 0, NULL)))
return FALSE;
// 分配内存
// 获取字符数量
iLength = GetWindowTextLength(hwndEdit);
pstrBuffer = new TCHAR[iLength + 1];
if (!pstrBuffer)
{
CloseHandle(hFile);
return FALSE;
}
if (Format == UNICODE)
{
// 写入UNICODE标识符
// 写入文件
WriteFile(hFile, &wByteOrderMark, 2, &dwBytesWritten, NULL);
// 获取缓冲区数据,写入文件
GetWindowText(hwndEdit, pstrBuffer, iLength + 1);
WriteFile(hFile, pstrBuffer, iLength * sizeof(TCHAR),
&dwBytesWritten, NULL);
if ((iLength * sizeof(TCHAR)) != (int)dwBytesWritten)
{
CloseHandle(hFile);
delete[] pstrBuffer;
return FALSE;
}
CloseHandle(hFile);
delete[] pstrBuffer;
return TRUE;
}
else
{
//写入ANSI格式
iLength = GetWindowTextLengthA(hwndEdit);
Temp = new char[iLength + 1]{'\0'};
GetWindowTextA(hwndEdit, Temp, iLength + 1);
WriteFile(hFile, Temp, iLength, &dwBytesWritten, NULL);
if (iLength != (int)dwBytesWritten)
{
CloseHandle(hFile);
delete[] Temp;
return FALSE;
}
CloseHandle(hFile);
delete[] Temp;
return TRUE;
}
}