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
941 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 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

12 Experts available now in Live!

Get 1:1 Help Now