Prev: const is an overrated concept that is a source of extra typing and maintenance
Next: const is an overrated concept that is a source of extra typing and maintenance
From: Superfreak3 on 25 Mar 2010 02:35 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 //.. 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!! -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: AnonMail2005 on 25 Mar 2010 05:20
On Mar 25, 1:35 pm, Superfreak3 <mawa...(a)rcn.com> wrote: > 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 //.. > > 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!! > I would guess that you can use a single buffer of 2048 (if 1024 is the maximum for each call) and place the characters in that buffer. Something like this: // first call places characters starting at Buf[0] NumOfBytes = GetPrivateProfileSection("OurApp Update PlugIns", Buf, 1024, m_IniPath); // // do error checking - make sure NumOfBytes is positve, etc. Also what happens if result is >= 1024 in length? Does the trailing null character get omitted or does the result get truncated? // // second call places characters staring at Buf+NumOfBytes+1 offset. NumOfBytes2 = GetPrivateProfileSection("OurApp Update PlugIns", Buf +NumOfBytes+1, 1024, m_OldIniPath); // // do similar error checking again // I'm not sure if the offset should be Buf+NumOfBytes or Buf+NumOfBytes +1. Please double check. Other than that the rest of your code should work unchanged. But you can improve the code. For instance, the str.Find("=") is done twice. You can just save it's result and reuse it. HTH -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |