Thread: Combine Two Buffers - How To?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    buf[strlen(newPlugins)-1] = '\0';
    What exactly are you trying to do here?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Ah, you're quick. Proof-read after post. Whoops.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    I'm not quite sure what you mean by these posts?


  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Why not to use std::string class?
    Something like
    Code:
    std::string str (buf);
    std::string second(buf2);
    
    str += second;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    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.....

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    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.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    NumOfBytes = GetPrivateProfileSection("OurApp Update PlugIns", Buf, 2048, m_OldIniPath);
    NumOfBytes2 = GetPrivateProfileSection("OurApp Update PlugIns", Buf+NumOfBytes, 2048, m_IniPath);
    That is good, because one buffer here is better than two.
    But you now have to write
    Code:
    NumOfBytes = GetPrivateProfileSection("OurApp Update PlugIns", Buf, 2048, m_OldIniPath);
    NumOfBytes += GetPrivateProfileSection("OurApp Update PlugIns", Buf + NumOfBytes, 2048 - NumOfBytes, m_IniPath);
    to make the code safe, otherwise it is potential buffer overflow; and it is possible to use just one variable instead of two to ease the comprehension of the code.
    Also, keep in mind that GetPrivateProfileSection() may return "2048 - 1" here which means that not all data has been retrieved from INI file. So it would be great if you add some checkings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-13-2010, 05:10 AM
  2. Using OR operator to combine two chunks of bits?
    By yougene in forum C Programming
    Replies: 5
    Last Post: 12-22-2008, 09:54 PM
  3. Reading in 16 and 24-bit audio data into (32-bit) integer buffers
    By theblindwatchma in forum C Programming
    Replies: 2
    Last Post: 04-13-2008, 11:12 PM
  4. winsock internal buffers
    By X PaYnE X in forum Networking/Device Communication
    Replies: 7
    Last Post: 05-16-2005, 04:25 AM
  5. Copy to Buffers
    By egomaster69 in forum C Programming
    Replies: 3
    Last Post: 12-05-2004, 06:50 PM