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
935 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now