Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
973 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 2000 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
Industry Leaders: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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…

927 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