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
955 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

738 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