-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileIO.cpp
406 lines (389 loc) · 10.2 KB
/
fileIO.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include <windows.h>
#include <tchar.h>
#include <commctrl.h>
#include <richedit.h>
#include <iostream>
#include <string>
#include <atlstr.h>
#include <cassert>
#include "appconst.h"
#include "toolbarControl.h"
#include "statusControl.h"
#include "mainWindowProc.h"
#include "menuids.h"
#include "mainPrefs.h"
#include "fileIO.h"
#include "globalVars.h"
#include "recentFiles.h"
#include "translations.h"
#include "service.h"
//converts menuindex to vector index and gets the filename from the string vector
WPARAM convertMenu2VecIndex(WPARAM menuIndex) {
//use throw catch here if menuIndex<22
WPARAM vecIndex = recentDocsList.size()-(menuIndex-IDM_REC_CLEAR);
//WPARAM vecIndex = (menuIndex - IDM_REC_CLEAR - 1);
return vecIndex;
}
std::string getRecentlyOpenedFileName(WPARAM index) {
return recentDocsList.at(index);
}
int detectFileType(std::string strPath) {
int startingPos = strPath.length()-4-1;
return strPath.find("rtf",startingPos)==-1?1:2;
}
int getActivatedFileMode(void) {
return OPENMODE; //1 - txt, 2 - rtf.
}
LPCTSTR getCurrentlyOpenedFileName(void) {
return (LPCTSTR)currentlyOpenedFile;
}
DWORD CALLBACK EditStreamCallbackIn(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, PLONG pcb) {
HANDLE hFile = (HANDLE)dwCookie;
if (ReadFile(hFile, lpBuff, cb, (DWORD *)pcb, NULL)) {
return 0;
}
return -1;
}
DWORD CALLBACK EditStreamCallbackOut(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, PLONG pcb) {
HANDLE hFile = (HANDLE)dwCookie;
if (WriteFile(hFile, lpBuff, cb, (DWORD *)pcb, NULL)) {
return 0;
}
return -1;
}
void openFileDiag(HWND mainWindow, int mode) { //0 - open, 1 - save as {
int fileTypeSaveOpen = 0;
OPENFILENAME fileName;
TCHAR szFile[MAX_PATH];
ZeroMemory(&fileName, sizeof(fileName));
fileName.lStructSize = sizeof(fileName);
fileName.lpstrFile = szFile;
fileName.lpstrFile[0] = '\0';
fileName.hwndOwner = mainWindow;
fileName.nMaxFile = sizeof(szFile);
fileName.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0RTF files (*.rtf)\0*.rtf*\0");
//_T("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
fileName.nFilterIndex = OPENMODE; //1 - .txt, 2 - .rtf
if (mode) {
fileName.Flags =
OFN_PATHMUSTEXIST
| OFN_OVERWRITEPROMPT
| OFN_EXPLORER
| OFN_HIDEREADONLY;
}
else {
fileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
}
if (mode) {
if (GetSaveFileName(&fileName)) {
fileTypeSaveOpen = fileName.nFilterIndex;
loadSaveFile(fileName.lpstrFile,mode,fileTypeSaveOpen);
_tcscpy(currentlyOpenedFile, szFile);
SetWindowText(hwnd, getCurrentlyOpenedFileName());
changeStatusControlMessage(2);
fileChanged = false;
fileLoaded = true;
disableFastSaveIcon();
}
//usr canceled out
else {
fileChanged = true;
}
}
else {
if (GetOpenFileName(&fileName)) {
fileTypeSaveOpen = fileName.nFilterIndex;
loadSaveFile(fileName.lpstrFile,mode,fileTypeSaveOpen);
_tcscpy(currentlyOpenedFile,szFile);
SetWindowText(hwnd, getCurrentlyOpenedFileName());
changeStatusControlMessage(1);
fileLoaded = true;
fileChanged = false;
//append
if (!recentDocsList.size()) {
recentDocsList_push(getCurrentlyOpenedFileName(), 0);
}
//insert before currAddRecent-1
else {
recentDocsList_push(getCurrentlyOpenedFileName(), 1);
}
}
}
}
//stream mode: 1 - write to file, 0 - write to editor
void loadSaveFile(LPTSTR filePath, int streamMode, int fileType) {
streamMode?FillFileFromRichEdit((LPCTSTR(filePath)),fileType):FillRichEditFromFile((LPCTSTR)filePath,fileType);
}
//1 - txt, 2 - rtf
void FillRichEditFromFile(LPCTSTR pszFile, int fileType) {
//settings.recentDocsList.push_back(pszFile);
WPARAM OPENFILETYPE;
assert(fileType>0);
if (fileType == 1) {
OPENMODE = 1;
OPENFILETYPE = SF_TEXT;
}
else if (fileType == 2) {
OPENMODE = 2;
OPENFILETYPE = SF_RTF;
}
BOOL fSuccess = FALSE;
HANDLE hFile = CreateFile(pszFile, GENERIC_READ,
FILE_SHARE_READ, 0, OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
//file exists
if (hFile != INVALID_HANDLE_VALUE) {
EDITSTREAM es = {0}; //main stream structure
es.pfnCallback = EditStreamCallbackIn;
es.dwCookie = (DWORD_PTR)hFile;
if (SendMessage(richEditControl, EM_STREAMIN, OPENFILETYPE, (LPARAM)&es) && es.dwError == 0) {
fSuccess = TRUE;
}
CloseHandle(hFile);
//TODO change to string CA2T/CT2A
std::wstring str = CW2W(pszFile);
//incrementRecentDocs(str); non-functional
//setting titlebar text to file path
//TODO resolve for settings file
if (settings.bShowCompleteFilePathInTitle) {
setWindowTitleToFile(pszFile, 1);
}
else {
setWindowTitleToFile(pszFile, 0);
}
//cutoff
//txt clause block toolbar formatting
if (!OPENMODE) {
disableFormattingOptions();
}
else {
enableFormattingOptions();
}
//save pszFile to global tracker
}
else {
//if file isn't found
MessageBox(hwnd, commDlgMsgError_EN[1], commDlgMsgErrorCaptions_EN[0], MB_ICONERROR | MB_OK);
if (askForDeletion())
{
recentDocsList_delete(pszFile);
}
//TODO: Prompt to delete from the recent list of files
}
}
void saveFile(void)
{
//stub
}
void FillFileFromRichEdit(LPCTSTR pszFile, int fileType) {
WPARAM SAVEFILETYPE;
assert(fileType);
if (fileType == 1) {
_tcscat((wchar_t*)pszFile,txtExt); //appending extension
SAVEFILETYPE = SF_TEXT;
}
else if (fileType == 2) {
_tcscat((wchar_t*)pszFile, rtfExt);
SAVEFILETYPE = SF_RTF;
}
BOOL fSuccess = FALSE;
HANDLE hFile = CreateFile(pszFile, GENERIC_WRITE,
0, 0, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
EDITSTREAM es = { 0 }; //main stream structure
es.pfnCallback = EditStreamCallbackOut;
es.dwCookie = (DWORD_PTR)hFile;
if (SendMessage(richEditControl, EM_STREAMOUT, (CP_UTF8 << 16) | SF_USECODEPAGE | SF_TEXT, (LPARAM)&es) && es.dwError == 0)
{
fSuccess = TRUE;
}
//cursor goes back to normal here
CloseHandle(hFile);
_tcscpy(currentlyOpenedFile, pszFile);
}
else {
MessageBox(hwnd, commDlgMsgError_EN[0], commDlgMsgErrorCaptions_EN[0], MB_OK | MB_ICONERROR);
}
}
void silentSaving(void) {
//silentSaving();
//initWaitCursor();
FillFileFromRichEdit(getCurrentlyOpenedFileName(), getActivatedFileMode()); //EXPLICIT CALL BE CAREFUL HERE
//resetCursor();
//MessageBox(hwnd, _T("Silently saved"), _T("Notification"), MB_ICONINFORMATION | MB_OK);
changeStatusControlMessage(3);
SetWindowText(hwnd, getCurrentlyOpenedFileName());
fileChanged = false;
fileSaved = true;
disableFastSaveIcon();
}
void setWindowTitleToFile(LPCTSTR filePath, int mode)
{
TCHAR titleAppend[126];
LPCTSTR *file;
//complete
if (mode) {
_tcscpy(titleAppend, filePath);
_tcscat(titleAppend, _T(" - TexEdit"));
}
else {
file = cutoffFileName(filePath);
_tcscpy(titleAppend,(const wchar_t*)file);
}
SetWindowText(hwnd, titleAppend);
}
LPCTSTR *cutoffFileName(LPCTSTR fullPath) {
wchar_t fileNameExt[CHAR_MAX];
wchar_t drive[CHAR_MAX];
wchar_t dir[CHAR_MAX];
wchar_t fileName[CHAR_MAX];
wchar_t ext[CHAR_MAX];
_wsplitpath(fullPath,drive,dir,fileName,ext);
//LPCTSTR *res = (LPCTSTR*)_tcsrchr((const wchar_t*)fullPath,(wchar_t)'\\');
//assert(res);
//res = (LPCTSTR*)_tcstok((wchar_t*)res,(const wchar_t*)'\\');
_tcscpy(fileNameExt,fileName);
_tcscat(fileNameExt,ext);
return (LPCTSTR*)fileNameExt;
}
//save as
void initSavingAsSequence(void) {
openFileDiag(hwnd,1);
}
void initSilentSavingSequence(void) {
if (fileLoaded) {
if (fileChanged) {
silentSaving();
}
}
else {
if (fileChanged) {
openFileDiag(hwnd, 1);
}
else {
MessageBox(hwnd, _T("Save blanks prompt here"), _T("Notification"), MB_ICONINFORMATION | MB_OK);
}
}
}
//1 - txt, 2 - rtf
void fromRecent::initOpeningSequence(LPCTSTR filePath,int mode) {
if (fileLoaded) {
if (fileChanged) {
if (MessageBox(hwnd, _T("You have unsaved changes\nSave them now?"), _T("Warning"), MB_ICONWARNING | MB_YESNOCANCEL) == IDYES) {
silentSaving();
}
else{
FillRichEditFromFile(filePath, mode);
}
}
else {
FillRichEditFromFile(filePath, mode);
}
}
else {
if (fileChanged) {
switch (MessageBox(hwnd, _T("You have unsaved changes\nSave them now?"), _T("Warning"), MB_ICONWARNING | MB_YESNOCANCEL)) {
case IDYES: {
openFileDiag(hwnd, 1);
break;
}
case IDNO: {
FillRichEditFromFile(filePath, mode);
break;
}
case IDCANCEL: {
break;
}
}
}
else {
FillRichEditFromFile(filePath, mode);
//MessageBox(hwnd, _T("Save blanks prompt here"), _T("Notification"), MB_ICONINFORMATION | MB_OK);
}
}
}
void normal::initOpeningSequence(void) {
if (fileLoaded) {
if (fileChanged) {
if (MessageBox(hwnd, _T("You have unsaved changes\nSave them now?"), _T("Warning"), MB_ICONWARNING | MB_YESNOCANCEL) == IDYES) {
silentSaving();
}
else {
openFileDiag(hwnd,0);
}
}
else {
openFileDiag(hwnd,0);
}
}
else {
if (fileChanged) {
switch (MessageBox(hwnd, _T("You have unsaved changes\nSave them now?"), _T("Warning"), MB_ICONWARNING | MB_YESNOCANCEL)) {
case IDYES: {
openFileDiag(hwnd, 1);
break;
}
case IDNO: {
openFileDiag(hwnd, 0);
break;
}
case IDCANCEL: {
break;
}
}
}
else {
openFileDiag(hwnd,0);
//MessageBox(hwnd, _T("Save blanks prompt here"), _T("Notification"), MB_ICONINFORMATION | MB_OK);
}
}
}
//1 - txt, 2 - rtf
void initNewSequence(int mode) {
if (fileLoaded) {
if (fileChanged) {
if (MessageBox(hwnd, _T("You have unsaved changes\nSave them now?"), _T("Warning"), MB_ICONWARNING | MB_YESNOCANCEL) == IDYES) {
silentSaving();
}
else {
activateRespMode(mode);
}
}
else {
activateRespMode(mode);
}
}
else {
//bug with multiple new instances
if (fileChanged) {
switch (MessageBox(hwnd, _T("You have unsaved changes\nSave them now?"), _T("Warning"), MB_ICONWARNING | MB_YESNOCANCEL)) {
case IDYES: {
openFileDiag(hwnd, 1);
break;
}
case IDNO: {
activateRespMode(mode);
break;
}
case IDCANCEL: {
break;
}
}
}
else {
//MessageBox(hwnd, _T("Save blanks prompt here"), _T("Notification"), MB_ICONINFORMATION | MB_OK);
activateRespMode(mode);
}
}
}
void activateRespMode(int mode) {
if (mode == 2) {
activateNewRTFMode();
}
else if (mode == 1) {
activateNewTXTMode();
}
fileLoaded = false;
fileChanged = false;
}