Solved

CRichEditCtrl's StreamIn function - How do you increase the block size for repeated calls to the Stream In callback function

Posted on 2003-12-03
7
944 Views
Last Modified: 2007-12-19
Example Function
DWORD CALLBACK CXRichEdit::CBStreamIn(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
      // We insert the rich text here.
      CString *pstr = (CString *) dwCookie;

      // cb = 4092;
      if (pstr->GetLength() < cb)
      {
            *pcb = pstr->GetLength();
            memcpy(pbBuff, (LPCSTR) *pstr, *pcb);
            pstr->Empty();
      }
      else
      {
            *pcb = cb;
            memcpy(pbBuff, (LPCSTR) *pstr, *pcb);
            *pstr = pstr->Right(pstr->GetLength() - cb);
      }

      return 0;
}

how can you increase this block size to allow faster stream in when loading large Arrays of data
0
Comment
Question by:Deon_Ball
7 Comments
 
LVL 6

Expert Comment

by:PlanetCpp
ID: 9870868
you never set it in the first place. the pass windows the address to where the data is and it returns how big of a chunk to take in. It seems to always take 4094 bytes at a time. ive never heard of being able to change this.
0
 

Author Comment

by:Deon_Ball
ID: 9871007

I realize that I didn't choose this block size, it's a default for the control  I asked how I can increase it!  There may be a way to do this by using the GetNewStorage() method or by creating my own global memory and pointing the rich edit control at that location.  

Word Pad can do this quickly.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 9878525
That code works quite quickly on my computer... just a few seconds to load a 1MB file.  It is inefficient in that the
     *pstr = pstr->Right(pstr->GetLength() - cb);
reallocates and moves huge blocks of data with each call, but even that is not a big deal on fast computers.   On *really* large files 10MB+, this can be a *BIG* concern.

That said, here is a sweet example that fills a RichEdit control very quickly.  It reads the enite file into a CString, then uses a class called CMemFile in the callback to process it efficiently in cb-sized chunks.

//--------------------------------------------------------- utility fn.  Save this in your toolkit!
BOOL TextFileToStr( LPCSTR sFilename, CString& s )
{
      CFile cf(sFilename, CFile::modeRead );  // TBD: add try/catch error handling
      DWORD nFileLen= cf.GetLength();
      char* p= s.GetBufferSetLength( nFileLen+1 );
      cf.Read( p, nFileLen );
      p[nFileLen]= 0;
      s.ReleaseBuffer();
      return(TRUE);
}

static DWORD CALLBACK
MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)  
{
      CMemFile* pFile= (CMemFile*)dwCookie;
      *pcb= pFile->Read(pbBuff, cb);
      return( 0 );
}

void CD19Dlg::OnButton1()
{
      CString sRTF;
      TextFileToStr( "c:\\temp\\bigtest.rtf", sRTF );

      CMemFile f( (LPBYTE)(LPCSTR)sRTF, sRTF.GetLength() );

      EDITSTREAM es;
      es.dwError = 0;
      es.pfnCallback = MyStreamInCallback;
      es.dwCookie = (DWORD) &f;

      m_ctlRE.LimitText( 2000000 );  // <<-- important! else RE truncates!
      m_ctlRE.StreamIn( SF_RTF, es );
}  // end-of-scope frees the string and destructs the CMemFile
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 49

Expert Comment

by:DanRollins
ID: 9884269
hi Deon_Ball,
Do you have any additional questions?  Do any comments need clarification?
-- Dan
0
 

Author Comment

by:Deon_Ball
ID: 9885377

Execellant reply, thanks to DanRollins

I used your example and it works great
Thanks

0
 

Expert Comment

by:cslick
ID: 12986534
DanRollins,

Is there an equally fast way of then writing out the contents of the RichEdit to a plain text file?  I have a 100 MB .rtf file I would like to convert to plain text, but don't know of a faster way to accomplish this than using the RichEdit control as an intermediate step.

Thanks,
Corey
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 12988588
cslick,
Welcome to EE.  It is best to ask your question as a new question.  In general, once a Q is closed, it is expected to remain static... the idea of "piggybacking" a new question onto an old one is definitely frowned upon.

That said, I'll go ahead with at least a partial answer...
If it is really an RTF file, incuding all of the formatting codes, then to get plain text out, I can think of no alternative to loading it as RTF and saving it as text (I have not tried that, though).  Maybe you could select it all and copy it to the clipboard and then request the TEXT representation of the clipboard data.  I expect thaat this would be a time- and resource-consuming operation.

-- Dan
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what is the purpose of protos  and PARAMS in the readline library 3 67
PDF library for Delphi 2 121
How can i compile this github project?? 2 84
Precision Problem in C++ 7 32
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question