Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. StreamOut with large content

StreamOut with large content

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    blongtq
    wrote on last edited by
    #1

    Hello all, When I use StreamOut feature of RichEditCtrl, I have a problem with large content. My StreamOut function always gets 2050 characters although my RichTextCtrl contains up to 300000 characters. Please take a look on my code below and tell me why. DWORD __stdcall MyStreamOutCallback(DWORD dwCookie,LPBYTE pbBuff,LONG cb,LONG *pcb) { USES_CONVERSION; LPWSTR lpUniText = T2W((LPTSTR)pbBuff); CString * psBuffer = (CString *)dwCookie; *psBuffer = CString(lpUniText); *pcb = wcslen(lpUniText); return 0; } CString GetSelText(CRichEditCtrl &rtf) { CString strOut; EDITSTREAM es; es.dwCookie = (DWORD)&strOut; es.pfnCallback = MyStreamOutCallback; rtf.StreamOut(SF_TEXT|SF_UNICODE,es); CHARRANGE cr; rtf.GetSel(cr); strOut = strOut.Mid(cr.cpMin,cr.cpMax-cr.cpMin-1); TRACE(_T("\ncpMax-cpMin-1 = %ld"),cr.cpMax-cr.cpMin-1); return strOut; } void CTestRTFView::OnButton1Click() { CRichEditCtrl &rtf = (CRichEditCtrl &)GetRichEditCtrl(); CHARRANGE cr; rtf.SetSel(0,-1); CString str = GetSelText(rtf); //str always contains 2050 characters. } Is my StreamOut function correct? Please give me a way to use StreamOut with a large content. Thank you very much for your help.

    M 1 Reply Last reply
    0
    • B blongtq

      Hello all, When I use StreamOut feature of RichEditCtrl, I have a problem with large content. My StreamOut function always gets 2050 characters although my RichTextCtrl contains up to 300000 characters. Please take a look on my code below and tell me why. DWORD __stdcall MyStreamOutCallback(DWORD dwCookie,LPBYTE pbBuff,LONG cb,LONG *pcb) { USES_CONVERSION; LPWSTR lpUniText = T2W((LPTSTR)pbBuff); CString * psBuffer = (CString *)dwCookie; *psBuffer = CString(lpUniText); *pcb = wcslen(lpUniText); return 0; } CString GetSelText(CRichEditCtrl &rtf) { CString strOut; EDITSTREAM es; es.dwCookie = (DWORD)&strOut; es.pfnCallback = MyStreamOutCallback; rtf.StreamOut(SF_TEXT|SF_UNICODE,es); CHARRANGE cr; rtf.GetSel(cr); strOut = strOut.Mid(cr.cpMin,cr.cpMax-cr.cpMin-1); TRACE(_T("\ncpMax-cpMin-1 = %ld"),cr.cpMax-cr.cpMin-1); return strOut; } void CTestRTFView::OnButton1Click() { CRichEditCtrl &rtf = (CRichEditCtrl &)GetRichEditCtrl(); CHARRANGE cr; rtf.SetSel(0,-1); CString str = GetSelText(rtf); //str always contains 2050 characters. } Is my StreamOut function correct? Please give me a way to use StreamOut with a large content. Thank you very much for your help.

      M Offline
      M Offline
      Martyn Pearson
      wrote on last edited by
      #2

      Here's the methods that I use, which happily work for thousands of characters!CString CRichEditViewEx::GetContent() { CString strContent = ""; EDITSTREAM es; CMemFile memFile; es.dwCookie = (DWORD) &memFile; es.pfnCallback = StreamOutCallback; GetRichEditCtrl().StreamOut(SF_RTF, es); memFile.SeekToBegin(); char szBuff[GETCONTENT_BUFFER + 1]; memset(szBuff, 0, GETCONTENT_BUFFER + 1); while(memFile.Read(szBuff, GETCONTENT_BUFFER) > 0) { strContent += CString(szBuff); memset(szBuff, 0, GETCONTENT_BUFFER + 1); } return strContent; } DWORD CRichEditViewEx::StreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) { CMemFile *memFile=(CMemFile *)dwCookie; memFile->WriteHuge(pbBuff,cb); *pcb = cb; return 0; }
      Note that here I am getting the RTF - to just get the text, you need to change the flag SF_RTF to SF_TEXT, like you have in your code - and of course add SF_UNICODE if required. If you want to get just the selected text, which is what the line strOut=strOut.Mid(....) seems to imply, then instead of doing CString::Mid(), you can use the flag SFF_SELECTION along with SF_TEXT et al.

      B 1 Reply Last reply
      0
      • M Martyn Pearson

        Here's the methods that I use, which happily work for thousands of characters!CString CRichEditViewEx::GetContent() { CString strContent = ""; EDITSTREAM es; CMemFile memFile; es.dwCookie = (DWORD) &memFile; es.pfnCallback = StreamOutCallback; GetRichEditCtrl().StreamOut(SF_RTF, es); memFile.SeekToBegin(); char szBuff[GETCONTENT_BUFFER + 1]; memset(szBuff, 0, GETCONTENT_BUFFER + 1); while(memFile.Read(szBuff, GETCONTENT_BUFFER) > 0) { strContent += CString(szBuff); memset(szBuff, 0, GETCONTENT_BUFFER + 1); } return strContent; } DWORD CRichEditViewEx::StreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) { CMemFile *memFile=(CMemFile *)dwCookie; memFile->WriteHuge(pbBuff,cb); *pcb = cb; return 0; }
        Note that here I am getting the RTF - to just get the text, you need to change the flag SF_RTF to SF_TEXT, like you have in your code - and of course add SF_UNICODE if required. If you want to get just the selected text, which is what the line strOut=strOut.Mid(....) seems to imply, then instead of doing CString::Mid(), you can use the flag SFF_SELECTION along with SF_TEXT et al.

        B Offline
        B Offline
        blongtq
        wrote on last edited by
        #3

        Hello, I have followed your code, but I have a Debug Assertion Failed at line 102 of file filemem.cpp when it executes to line: while(memFile.Read(szBuff, GETCONTENT_BUFFER) > 0) Can you tell me what happens. Thank you very much for your help.

        M 1 Reply Last reply
        0
        • B blongtq

          Hello, I have followed your code, but I have a Debug Assertion Failed at line 102 of file filemem.cpp when it executes to line: while(memFile.Read(szBuff, GETCONTENT_BUFFER) > 0) Can you tell me what happens. Thank you very much for your help.

          M Offline
          M Offline
          Martyn Pearson
          wrote on last edited by
          #4

          I'm confused. Delving into the CMemFile code, it looks like the internal CMemFile buffer is NULL, which means that (memory shortage problems aside) the rich edit control has never called the callback to write to the file. It might be worth putting a breakpoint before the offending line and taking a look at the memFile member variables to see if there is indeed a valid looking buffer in there! If there is no buffer, it could be down to where you are calling the function from - could it be at a point where the rich edit control might have already cleared down the content and started destroying itself? Just out of interest, what did you set GETCONTENT_BUFFER to? I set it to 4096. Sorry I can't be any more help!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups