-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrichtext_io.cpp
More file actions
112 lines (98 loc) · 2.63 KB
/
richtext_io.cpp
File metadata and controls
112 lines (98 loc) · 2.63 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
#include "stdafx.h"
#include "richtext_io.h"
shared_char_ptr GetRichText( CRichEditCtrl& re, int format ) //| SFF_SELECTION
{
CMemFile iFile( 1024 );
CArchive oar( &iFile, CArchive::store );
//need a throw exception here.
EDITSTREAM es= { NULL, 0, EditStreamCallBack };
_afxRichEditStreamCookie cookie( oar );
es.dwCookie= (DWORD_PTR)&cookie;
re.StreamOut( format, es );
return shared_char_ptr((char*)iFile.Detach(),std::default_delete<char>());
}
DWORD CALLBACK EditStreamCallBack(
DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb )
{
_afxRichEditStreamCookie* pCookie = (_afxRichEditStreamCookie*)dwCookie;
CArchive& ar = pCookie->m_ar;
DWORD dw = 0;
*pcb = cb;
TRY
{
if ( ar.IsStoring( ) )
ar.GetFile( )->Write( pbBuff, cb );
else
*pcb = ar.GetFile( )->Read( pbBuff, cb );
}
CATCH( CFileException, e )
{
*pcb = 0;
pCookie->m_dwError = (DWORD)e->m_cause;
dw = 1;
e->Delete( );
}
AND_CATCH_ALL( e )
{
*pcb = 0;
pCookie->m_dwError = -1;
dw = 1;
e->Delete( );
}
END_CATCH_ALL
return dw;
}
BOOL SetRichText( CRichEditCtrl& re, LPCTSTR buf, int format )//| SFF_SELECTION
{
CMemFile oFile( (BYTE*)buf, (UINT)_tcslen( buf ) );
CArchive oar( &oFile, CArchive::load );
EDITSTREAM es= {0, 0, EditStreamCallBack};
_afxRichEditStreamCookie cookie( oar );
es.dwCookie = (DWORD_PTR)&cookie;
oFile.SeekToBegin( );
re.StreamIn( format, es ); //| SFF_SELECTION
if( cookie.m_dwError != 0 )
{
AfxThrowFileException( cookie.m_dwError );
return FALSE;
}
oar.Close( );
oFile.Close( );
return TRUE;
}
BOOL SetRichText( CRichEditCtrl& re, CFile& file )//| SFF_SELECTION
{
//CMemFile oFile( (BYTE*)buf, (UINT)strlen( buf ) );
//CArchive oar( &file, CArchive::load, 4096 * 16 );
CArchive oar( &file, CArchive::load, 64000 );
EDITSTREAM es= {0, 0, EditStreamCallBack};
_afxRichEditStreamCookie cookie( oar );
es.dwCookie = (DWORD_PTR)&cookie;
//oFile.Write( buf, strlen( buf ) );
file.SeekToBegin( );
re.StreamIn( SF_RTF, es ); //| SFF_SELECTION
if( cookie.m_dwError != 0 )
{
AfxThrowFileException( cookie.m_dwError );
return FALSE;
}
return TRUE;
}
BOOL AppendRichText( CRichEditCtrl& rtf, LPCTSTR buf )
{
rtf.SetSel(-1, -1);
if( ! SetRichText(rtf,buf, SF_RTF | SFF_SELECTION))
return FALSE;
auto buffer = GetRichText(rtf);
char* che= buffer.get();
for(; *che; ++che);//to end
char* ch= che;
for(; *ch != ' '; --ch);//back to first space
for(; *ch != '\\'; ++ch);//then to first '\', assumes not \\,\},\{ for now
if( ch + 10 > che )
return FALSE;//but it should fit....
auto re = R"(\par\par})"; // the replacement
for( size_t i= 0; i < 10; ++i)
*ch++ = *re++;
return SetRichText(rtf,buffer.get());
}