CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Mar 2010
    Posts
    15

    Combine Two Buffers - How To?

    Hi all,

    I'm really not at all experienced with C++, but I have some changes to make in code that used to be maintained by someone else. The code that exists may not be the best, but I'm not looking to totally re-write the widget because it works for us with no problems to date.

    Anyway, here is the current code... I'll put the psuedo code or what I would like to do in << >>. or commented //..

    Code:
    DWORD NumOfBytes = 0;
    <<DWORD NumOfBytes2 = 0;  // Don't know if this second NumOfBytes is needed or if first can be used twice>>
    char Buf[1024];
    <<char Buf2[1024];>>
    char *pNextSetting = NULL;
    CString str;
    CPlugIn *pPlugIn;
    
    
    NumOfBytes = GetPrivateProfileSection("OurApp Update PlugIns", Buf, 1024, m_IniPath);
    <<NumOfBytes2 = GetPrivateProfileSection("OurApp Update PlugIns", Buf2, 1024, m_OldIniPath);>>
    
    // Below is the current line of code, but I would really like this to become something like...
    // pNextSetting = Buf + Buf2
    
    pNextSetting = Buf;
    
    //So, how do I combine the two buffers?
    
    str = pNextSetting;
    if (NumOfBytes > 0) {
    	while (*pNextSetting != 0x00) {
    		pPlugIn = new CPlugIn;
    
    		pPlugIn->Id = str.Left(str.Find("="));
    		pPlugIn->Version = str.Right(str.GetLength() - str.Find("=") - 1);
    
    		m_LocalPlugIns.SetAt(pPlugIn->Id, pPlugIn);
    
    		pNextSetting = pNextSetting + strlen(pNextSetting) + 1;
    		str = pNextSetting;
    	}
    }
    Again, it may not be the best and its older code, but it works so I'm hoping there isn't much rework needed to combine what I'm pulling from .ini files.

    Any help is MORE THAN APPRECIATED!!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Combine Two Buffers - How To?

    I see two basic approaches you could take here.

    First, you could duplicate everything after "str = pNextSetting", except setting pNextSetting to Buf2 the second time. Actually, it might be better to move that block of code to a function and just call it with both Buf and Buf2, then....

    Second, instead of creating a second buffer to begin with, you could just double the size of Buf, and arrange things so that the pointer you pass to GetPrivateProfileSection() the second time is just past the end of the part of Buf that was used the first time. I don't know precisely what format of data Buf gets filled with, so I don't know how practical that is, but a parameter like Buf+NumOfBytes might do it (perhaps that's off by one). You'd then want to add the return value to NumOfBytes the second time, of course.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Combine Two Buffers - How To?

    Quote Originally Posted by Superfreak3 View Post
    Code:
    DWORD NumOfBytes = 0;
    <<DWORD NumOfBytes2 = 0;  // Don't know if this second NumOfBytes is needed or if first can be used twice>>
    char Buf[1024];
    <<char Buf2[1024];>>
    char *pNextSetting = NULL;
    CString str;
    ... 
    NumOfBytes = GetPrivateProfileSection("OurApp Update PlugIns", Buf, 1024, m_IniPath);
    <<NumOfBytes2 = GetPrivateProfileSection("OurApp Update PlugIns", Buf2, 1024, m_OldIniPath);>>
     
    // Below is the current line of code, but I would really like this to become something like...
    // pNextSetting = Buf + Buf2
     
    pNextSetting = Buf;
     
    //So, how do I combine the two buffers?...
    }
    Well, "copy" the first buffer to the CString and then add the second one!
    Code:
    str = Buf;
    str += Buf2;
    Victor Nijegorodov

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Combine Two Buffers - How To?

    Oh, I didn't notice str was a CString, thought it was just a pointer. Yeah, I guess that would be a fairly easy approach.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Combine Two Buffers - How To?

    Quote Originally Posted by Lindley View Post
    Oh, I didn't notice str was a CString, thought it was just a pointer. Yeah, I guess that would be a fairly easy approach.
    I didn't notice it at first either (because it is the C++ (Non Visual C++ Issues) Forum!)
    Victor Nijegorodov

  6. #6
    Join Date
    Mar 2010
    Posts
    15

    Re: Combine Two Buffers - How To?

    If I use the Buf2 approach, do I need

    Code:
    DWORD NumOfBytes2 = 0;
    ... or can I just reuse NumOfBytes like

    Code:
    NumOfBytes2 = GetPrivateProfileSection("OurApp Update PlugIns", Buf2, 1024, m_OldIniPath);
    ?

  7. #7
    Join Date
    Mar 2010
    Posts
    15

    Re: Combine Two Buffers - How To?

    ooops...

    meant...

    Code:
    NumOfBytes = GetPrivateProfileSection("OurApp Update PlugIns", Buf2, 1024, m_OldIniPath);

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Combine Two Buffers - How To?

    If you don't need the previous value of NumOfBytes anymore - then you can reuse it.
    Victor Nijegorodov

  9. #9
    Join Date
    Mar 2010
    Posts
    15

    Re: Combine Two Buffers - How To?

    Quote Originally Posted by VictorN View Post
    Well, "copy" the first buffer to the CString and then add the second one!
    Code:
    str = Buf;
    str += Buf2;
    Well if I do that, how would my code look. I don't really know what to do with pNextSetting based on the following snippet from the original code...

    Code:
    pNextSetting = Buf;
    
    str = pNextSetting;

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Combine Two Buffers - How To?

    Crazy idea: Why don't you declare buff1 & 2 like this?
    Code:
    char Buf[2048]; //Declare as 2048, use only 1024 for buf 1
    const char* Buf2 = Buf + 1024; //Use Buf's other 1024 chars.
    There, only one buffer, and the rest of the code should work (I didn't see any "sizeof", so there is probably nothing to worry about you array decaying into a pointer, that and buf and buf2 have the same scope).

    But at this point, I think you should really tell use what and why you are trying to achieve, rather than how ("use only 1 buffer" is an answer to a how question).

    So, what are you trying to achieve?

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Combine Two Buffers - How To?

    It's hard to know for sure what pNextSetting needs to be without knowing the form the data in the buffer will take. Given that code, I suspect it may have some embedded NULLs.
    Last edited by Lindley; March 25th, 2010 at 09:13 PM.

  12. #12
    Join Date
    Mar 2010
    Posts
    15

    Re: Combine Two Buffers - How To?

    Here is what I did and it appears to work...

    Code:
    DWORD NumOfBytes = 0;
    //Added new and increased Buf size...
    DWORD NumOfBytes2 = 0;
    char Buf[2048];
    char *pNextSetting = NULL;
    CString str;
    CPlugIn *pPlugIn;
    
    
    NumOfBytes = GetPrivateProfileSection("OurApp Update PlugIns", Buf, 2048, m_OldIniPath);
    NumOfBytes2 = GetPrivateProfileSection("OurApp Update PlugIns", Buf+NumOfBytes, 2048, m_IniPath);
    
    pNextSetting = Buf;
    	
    str = pNextSetting;
    Then I changed my condition to NumOfBytes2 > 0 from NumOfBytes > 0...

    Code:
    if (NumOfBytes2 > 0) {
    	while (*pNextSetting != 0x00) {
    		pPlugIn = new CPlugIn;  //etc, etc, etc.....

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Combine Two Buffers - How To?

    I'd suggest checking whether NumOfBytes+NumOfBytes2 > 0 instead. Otherwise, that looks okay.

  14. #14
    Join Date
    Mar 2010
    Posts
    15

    Re: Combine Two Buffers - How To?

    Yeah...

    I think I had to change the condition to (NumOfBytes > 0 || NumOfBytes2 > 0). Seems to be right in my testing so far. My fingers are crossed, however.

    I guess I could could also go the NOB + NOB2 > 0 route. 6 Of one, 1/2 dozen of the other, right?

  15. #15
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Combine Two Buffers - How To?

    Quote Originally Posted by Superfreak3 View Post
    Code:
    char Buf[2048];
    ...
    NumOfBytes = GetPrivateProfileSection("OurApp Update PlugIns", Buf, 2048, m_OldIniPath);
    NumOfBytes2 = GetPrivateProfileSection("OurApp Update PlugIns", Buf+NumOfBytes, 2048, m_IniPath);
    The last line can cause buffer overrun. The fix:
    Code:
    NumOfBytes2 = GetPrivateProfileSection("OurApp Update PlugIns", Buf+NumOfBytes, 2048-NumOfBytes, m_IniPath);
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured