/
weddoc.cpp
678 lines (531 loc) · 15.4 KB
/
weddoc.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/* =====[ weddoc.cpp ]==========================================
Description: The wed project, implementation of the weddoc.cpp
Defines the behavior for the application.
Compiled: MS-VC 6.00
Notes: <Empty Notes>
Revisions:
REV DATE BY DESCRIPTION
---- -------- ----------- ----------------------------
0.00 1/6/2009 Peter Glen Initial version.
======================================================================= */
/////////////////////////////////////////////////////////////////////////////
// wedDoc.cpp : implementation of the CWedDoc class
//
#include "stdafx.h"
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include "wed.h"
#include "StrList.h"
#include "wedDoc.h"
#include "wedView.h"
#include "mxpad.h"
#include "editor.h"
#include "io.h"
#include "misc.h"
/////////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
#define DETECTSIZE 1024
#define MAX_AUTO_LINES 10000
/////////////////////////////////////////////////////////////////////////////
// Get single line comment delimiter and function regex
const char *comms[] =
{
".PHP", "//", "^function",
".SH", "#", "^function",
".PL", "#", "^sub",
".CGI", "#", "^sub",
".PY", "#", "^def",
".ASM", ";", "^function",
".BAT", "rem", "^function",
".C", "//", "^([a-z]|[A-Z]).*\\(.*\\)",
".CPP", "//", "^([a-z]|[A-Z]).*\\(.*\\)",
".JAVA", "//", "^([a-z]|[A-Z]).*\\(.*\\)",
".SRC", ";", "^([a-z]|[A-Z]).*\\(.*\\)",
NULL, NULL, NULL
} ;
/////////////////////////////////////////////////////////////////////////////
// CWedDoc
IMPLEMENT_DYNCREATE(CWedDoc, CDocument)
BEGIN_MESSAGE_MAP(CWedDoc, CDocument)
//{{AFX_MSG_MAP(CWedDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
END_MESSAGE_MAP()
// Declare the variables needed
#ifdef _DEBUG
static CMemoryState oldMemState, newMemState, diffMemState;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWedDoc construction
CWedDoc::CWedDoc()
{
#ifdef _DEBUG
oldMemState.Checkpoint();
#endif
inited = notfullundo = 0;
unix = readonly = 0;
origlines = undopos = undoorig = 0;
spaceify = 0;
// Historical default
maxcol = 80;
comment = "//";
memset(&docstat, 0 , sizeof(docstat));
memset(&docstat2, 0 , sizeof(docstat2));
}
/////////////////////////////////////////////////////////////////////////////
// CWedDoc destruction
CWedDoc::~CWedDoc()
{
#ifdef _DEBUG
//oldMemState.DumpAllObjectsSince();
//newMemState.Checkpoint();
if( diffMemState.Difference( oldMemState, newMemState ) )
{
//P2N("WedDoc Memory leaked!\r\n");
//TRACE( "Memory leaked!\n" );
}
#endif
}
/////////////////////////////////////////////////////////////////////////////
// OnNewDocument
BOOL CWedDoc::OnNewDocument()
{
P2N("CWedDoc::OnNewDocument()\r\n");
if(GetNumOfDocs() > MAX_DOC_LIMIT)
{
AfxMessageBox("Too many open documents");
return FALSE;
}
// Go get one
if (!CDocument::OnNewDocument())
return FALSE;
CString num;
// Find temporary name that is not an existing file
while(TRUE)
{
num.Format("untitled%02d.txt", newcount++);
if(access(num, 0))
break;
// Safety net
if(newcount > 100)
break;
}
SetPathName(num);
SetModifiedFlag();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// OnOpenDocument
BOOL CWedDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
BOOL retval = FALSE;
if(GetNumOfDocs() > MAX_DOC_LIMIT)
{
AfxMessageBox("Too many open documents");
return FALSE;
}
// For originial document properties
_stat(lpszPathName, &docstat); docstat2 = docstat;
// Load as usual
retval = CDocument::OnOpenDocument(lpszPathName);
//P2N("Opened document %s\r\n", lpszPathName);
// Get the undo/redo
LoadUndo(lpszPathName); LoadRedo(lpszPathName);
// Used by IsDocModified()
undoorig = undo.GetCount();
//P2N("Undo orig = %d\r\n", undoorig);
SaveBackup(lpszPathName);
// Assign comment file
GetComment(lpszPathName);
return(retval);
}
/////////////////////////////////////////////////////////////////////////////
// Override for displaying correct message
//
void CWedDoc::Serialize(CArchive& ar, const char *docname)
{
CString num, str;
CString filename = docname;
PathToFname(filename);
if (ar.IsStoring())
{
// Writing
CWaitCursor cursor;
POSITION pos;
pos = strlist.GetHeadPosition();
origlines = 0;
while(TRUE)
{
CString line;
if(!(origlines % 100))
{
num.Format("Writing %s line %d", filename, origlines);
message(num);
}
origlines++;
if (!pos)
break;
line = strlist.GetNext(pos);
// Obey tab save option
if(Tab2Space || spaceify)
ExpandTabs(line);
// Dispose space from EOL
line.TrimRight();
// Write out to file
ar.WriteString(line);
// Do end of line according to settings
if(unix)
ar.WriteString("\n");
else
ar.WriteString("\r\n");
}
num.Format("Wrote %s", filename); message(num);
// If user decides to save, save undo/redo info as well
SaveUndo(); SaveRedo();
}
else
{
// Reading
CWaitCursor cursor;
char buff[DETECTSIZE];
strlist.RemoveAll();
// Determine file type
CFile* fp = ar.GetFile();
int rlen = fp->Read(buff, DETECTSIZE);
// File without a newline ---> default to DOS
if(!strstr(buff, "\n"))
unix = FALSE;
else if (strstr(buff, "\r\n"))
unix = FALSE;
else
unix = TRUE;
// Take a wild guess for tabs
if(!strstr(buff, "\t") && rlen > DETECTSIZE/2 )
{
spaceify = TRUE;
}
// Start over on file
fp->Seek(0,CFile::begin);
origlines = 0;
// Load file
while(TRUE)
{
str = "";
if(!(origlines % 100))
{
CString num;
num.Format("Reading %s line %d", filename, origlines);
message(num);
}
if(YieldToWinEx())
{
readonly = TRUE;
AfxMessageBox("Aborted load, partial buffer is readonly.");
break;
}
TRY
{
if(!ar.ReadString(str))
break;
}
CATCH_ALL(ee); END_CATCH_ALL
origlines++;
TRY
{
strlist.AddTail(str);
}
CATCH(CMemoryException, ee)
{
//P2N("Memory exception\r\n");
break;
}
END_CATCH
}
// So unix can see an unlocked file
// Let others see it
// ReleaseFile(fp, 1);
// Patch things up
if( strlist.IsEmpty())
{
//P2N("Empty file\r\n");
strlist.AddTail("");
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CWedDoc serialization
void CWedDoc::Serialize(CArchive& ar)
{
CString docname = GetPathName();
// Call overridden version
Serialize(ar, docname);
}
/////////////////////////////////////////////////////////////////////////////
// CWedDoc diagnostics
#ifdef _DEBUG
void CWedDoc::AssertValid() const
{
CDocument::AssertValid();
}
/////////////////////////////////////////////////////////////////////////////
void CWedDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// Serialize undo
void CWedDoc::SaveUndo()
{
CString droot; int hhh = HashString(GetPathName());
droot.Format("%sdata\\%x.udo", dataroot, hhh);
//P2N("Save Undo To: %s\r\n", droot);
CFile cf;
if( cf.Open(droot, CFile::modeCreate | CFile::modeWrite ))
{
CArchive ar( &cf, CArchive::store);
undo.Serialize(ar);;
}
else
{
//P2N( "Cannot create undo file: %s\r\n", droot);
}
}
/////////////////////////////////////////////////////////////////////////////
// Serialize redo
void CWedDoc::SaveRedo()
{
CString droot; int hhh = HashString(GetPathName());
droot.Format("%sdata\\%x.rdo", dataroot, hhh);
//P2N("Save Redo To: %s\r\n", droot);
CFile cf;
if( cf.Open(droot, CFile::modeCreate | CFile::modeWrite ))
{
CArchive ar( &cf, CArchive::store);
redo.Serialize(ar);
}
else
{
//P2N("Cannot create redo file: %s\r\n", droot);
}
}
/////////////////////////////////////////////////////////////////////////////
// Load Undo
void CWedDoc::LoadUndo(LPCTSTR lpszPathName)
{
CString droot; int hhh = HashString(lpszPathName);
droot.Format("%sdata\\%x.udo", dataroot, hhh);
//P2N("Load Undo from: %s\r\n", droot);
CFile cf;
if( cf.Open(droot, CFile::modeRead ))
{
CArchive ar( &cf, CArchive::load);
undo.Serialize(ar);
}
else
{
//P2N("Cannot open undo file: %s\r\n", droot);
}
}
/////////////////////////////////////////////////////////////////////////////
// Load redo
void CWedDoc::LoadRedo(LPCTSTR lpszPathName)
{
CString droot; int hhh = HashString(lpszPathName);
droot.Format("%sdata\\%x.rdo", dataroot, hhh);
//P2N("Load Redo from: %s\r\n", droot);
CFile cf;
if( cf.Open(droot, CFile::modeRead ))
{
CArchive ar( &cf, CArchive::load);
redo.Serialize(ar);
}
else
{
//P2N("Cannot open redo file: %s\r\n", droot);
}
}
/////////////////////////////////////////////////////////////////////////////
// Save Document
BOOL CWedDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
if(readonly)
{
AfxMessageBox("Read only buffer, cannot save");
return 1;
}
//P2N("SaveDocument '%s'\r\n", lpszPathName);
int ret_val = CDocument::OnSaveDocument(lpszPathName);
// Update current stat info
_stat(lpszPathName, &docstat2);
// Get comment status
GetComment(lpszPathName);
return(ret_val);
}
/////////////////////////////////////////////////////////////////////////////
// Autosave
void CWedDoc::AutoSave()
{
CString num;
CString docname = GetPathName();
if(docname == "")
{
message ("Empty file name on AutoSave");
return;
}
CString filename = GetPathName();
PathToFname(filename);
if(strlist.GetCount() > MAX_AUTO_LINES)
{
num.Format("File %s is too large for autosave ", filename);
message(num);
return;
}
CString droot; int hhh = HashString(docname);
droot.Format("%sdata\\%x.sav", dataroot, hhh);
num.Format("Autosaving %s", filename); message(num);
//P2N("AutoSave %s %s\r\n", filename, droot);
Sleep(300);
CFile cf;
if(cf.Open(droot, CFile::modeCreate | CFile::modeWrite ))
{
CArchive ar(&cf, CArchive::store);
Serialize(ar, droot);
SetModifiedFlag(FALSE);
}
else
{
num.Format("Could not autosave %s ", filename);
message(num);
//P2N("Cannot create autosave file: %s\r\n", droot);
}
}
/////////////////////////////////////////////////////////////////////////////
// Save Backup copy of the file
void CWedDoc::SaveBackup(const char *docname)
{
CString num, filename = docname;
PathToFname(filename);
if(access(docname, 0) < 0)
{
num.Format("File %s does not exist, did make auto backup ", filename);
message(num);
//AfxMessageBox(num);
return;
}
// Save only if smaller then 2 Meg
if(docstat.st_size > MAX_BACKUP)
{
num.Format("File is too large for auto backup ", filename);
message(num);
return;
}
// Truncate up tp the first backslash
int idx; CString tfn(docname);
idx = tfn.Find("\\\\");
if(idx >= 0)
{
tfn = tfn.Right(tfn.GetLength() - (idx + 1) );
}
else
{
idx = tfn.Find('\\');
if(idx >= 0)
{
tfn = tfn.Right(tfn.GetLength() - (idx) );
}
}
CString fbackup;
//int hhh = HashString(docname);
fbackup.Format("%sbackup%s", dataroot, tfn);
create_full_dir(fbackup);
CString fbdir(fbackup);
PathToDir(fbdir);
fbdir += "history\\"; fbdir += filename;
//P2N("Sent backup copy to: %s\r\n", fbackup);
// See if backup file exists already
struct _stat docstat_o, docstat_b;
_stat(docname, &docstat_o);
CTime ct(docstat_o.st_mtime);
CString datestr = ct.Format(".%a_%b_%d_%Y--%H_%M");
fbdir += datestr;
//P2N("Sent backup history to: %s\r\n", fbdir);
// If backup is less than ....
if(_stat(fbackup, &docstat_b) >=0)
{
//P2N("Document %s m_time=%d BackupFile m_time=%d \r\n",
// docname, docstat_o.st_mtime, docstat_b.st_mtime );
//if((docstat_o.st_mtime - docstat_b.st_mtime) > 10)
{
//P2N("***Backing up: %s\r\n", fbackup);
create_full_dir(fbdir);
rename(fbackup, fbdir);
}
}
CFile cf;
if(cf.Open(fbackup, CFile::modeCreate | CFile::modeWrite ))
{
num.Format("Saving backup copy %s ", filename);
message(num);
CArchive ar(&cf, CArchive::store);
Serialize(ar, fbackup);
}
else
{
num.Format("Could not create backup copy of %s ", filename);
message(num);
//P2N("Cannot create backup copy of: %s\r\n", droot);
}
}
/////////////////////////////////////////////////////////////////////////////
// Count the number of documents
int CWedDoc::GetNumOfDocs()
{
int cnt = 0;
POSITION Pos =
((CWedApp*)AfxGetApp())->pDocTemplate->GetFirstDocPosition();
for(;;)
{
if(!Pos)
break;
CWedDoc* doc = (CWedDoc*)
((CWedApp*)AfxGetApp())->pDocTemplate->GetNextDoc(Pos);
cnt++;
}
return (cnt);
}
/////////////////////////////////////////////////////////////////////////////
int CWedDoc::GetComment(const char *fname)
{
int loop = 0;
CString name(fname), drive, dir, filename, ext;
SplitPath(name, drive, dir, filename, ext);
ext.MakeUpper();
comment = "//";
while(TRUE)
{
if(!comms[loop])
break;
if(ext == comms[loop])
{
comment = comms[loop+1];
funcregex = comms[loop+2];
P2N("Got: '%s' '%s' '%s'\r\n", comms[loop], comms[loop+1], comms[loop+2]);
break;
}
//P2N("Searching extension %s %s\r\n", str, comms[loop]);
loop += 3;
}
return(0);
}
// EOF