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!!
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.
Re: Combine Two Buffers - How To?
Quote:
Originally Posted by
Superfreak3
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;
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.
Re: Combine Two Buffers - How To?
Quote:
Originally Posted by
Lindley
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!)
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);
?
Re: Combine Two Buffers - How To?
ooops...
meant...
Code:
NumOfBytes = GetPrivateProfileSection("OurApp Update PlugIns", Buf2, 1024, m_OldIniPath);
Re: Combine Two Buffers - How To?
If you don't need the previous value of NumOfBytes anymore - then you can reuse it.
Re: Combine Two Buffers - How To?
Quote:
Originally Posted by
VictorN
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;
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?
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.
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.....
Re: Combine Two Buffers - How To?
I'd suggest checking whether NumOfBytes+NumOfBytes2 > 0 instead. Otherwise, that looks okay.
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?
Re: Combine Two Buffers - How To?
Quote:
Originally Posted by
Superfreak3
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);