How to optimize the code snippet which has several calls to find method using map?

  • Thread starter Thread starter bhavya internship
  • Start date Start date
B

bhavya internship

Guest
Hello All,

I wrote a functionality to read files and load them into the map (m_fileParams).

Then I am searching the specific keys in the map container using "find". Here using "find" I am searching different keys.

My code is working fine. But I wanted to know how best I can optimize the code?

In several places I have used "find" to search the key in the map. Is there any way I can write generic method?

"m_fileParams" is declared like as shown below in the .h file:

std::map<std::wstring, std::wstring> m_fileParams;

below is the .cpp file:

bool McEMRMgr::ReadFile()
{
IOperation* pFileReader = new McFileReader();
std::map<std::wstring, std::wstring> mapFileReaderResponse;
if(pFileReader)
{
std::wstring strVendor, wstrFileLocation, wstrRegLocation;
std::wstring FileType, regType;

auto manufacSearchValue = m_fileParams.find(VENDOR);
if (manufacSearchValue != m_fileParams.end())
{
strVendor.assign(manufacSearchValue->second);
transform(
strVendor.begin(), strVendor.end(),
strVendor.begin(),
towlower);

}
else
{
strVendor.assign(L"default");
FileType.assign(L".File");
regType.assign(L".registry");
}
auto emailSearchValue = m_fileParams.find(L"email_locations." + strVendor + L".type" + FileType);
if (emailSearchValue != m_fileParams.end())
{
if (emailSearchValue->second.compare(L"File") == 0)
{
auto FileSearchPath = m_fileParams.find(L"email_locations." + strVendor + L".paths" + FileType);
wstrFileLocation = FileSearchPath->second;
}
}

{
emailSearchValue = m_fileParams.find(L"email_locations." + strVendor + L".type" + regType);
if (emailSearchValue->second.compare(L"registry") == 0)
{
auto regSearchPath = m_fileParams.find(L"email_locations." + strVendor + L".paths" + regType);
wstrRegLocation = regSearchPath->second;
}
}
if (wstrFileLocation.length() != 0)
{
std::wstring oemFolderPath;
auto oemSearchType = m_fileParams.find(L"oem_info_folder");
if (oemSearchType != m_fileParams.end())
{
oemFolderPath.assign(oemSearchType->second);
}
else
{
oemFolderPath.assign(OEM_FOLDER_DEFAULT_PATH);
}
std::wstring oemPathPublicKey(oemFolderPath), oemPathSessionKey(oemFolderPath), oemPathUserChoices(oemFolderPath);
oemPathPublicKey.append(PUBLIC_KEY_File);
oemPathSessionKey.append(SESSION_KEY_File);
oemPathUserChoices.append(USERCHOICES_File);
pFileReader->SetParams((wchar_t*)oemPathPublicKey.c_str(), L"File");
pFileReader->Execute();
pFileReader->SetParams((wchar_t*)oemPathSessionKey.c_str(), L"File");
pFileReader->Execute();
pFileReader->SetParams((wchar_t*)oemPathUserChoices.c_str(), L"File");
pFileReader->Execute();

wchar_t* ptrWStr;
wchar_t* buffer;
ptrWStr = wcstok_s((wchar_t*)wstrFileLocation.c_str(), L" ", &buffer);
while (ptrWStr != NULL)
{
pFileReader->SetParams(ptrWStr, L"File");
pFileReader->Execute();
mapFileReaderResponse = pFileReader->GetResponse();
for (std::map<std::wstring, std::wstring>::iterator it = mapFileReaderResponse.begin(); it != mapFileReaderResponse.end(); ++it)
{
m_fileParams.insert({ it->first, it->second });
}
ptrWStr = wcstok_s(nullptr, L" ", &buffer);
}

if (mapFileReaderResponse.size() != 0)
{
m_cJobState.eFileStatus = OperationState::Success;
}
else
{
m_cJobState.eFileStatus = OperationState::Failed;
}
}

if (wstrRegLocation.length() != 0)
{
pFileReader->SetParams((wchar_t*)wstrRegLocation.c_str(), L"registry");
pFileReader->Execute();
mapFileReaderResponse = pFileReader->GetResponse();
for (std::map<std::wstring, std::wstring>::iterator it = mapFileReaderResponse.begin(); it != mapFileReaderResponse.end(); ++it)
{
m_fileParams.insert({ it->first, it->second });
}
}
auto emailSearchType = m_fileParams.find(L"email");
std::wstring emailValue = emailSearchType->second;
if (emailValue.length() > 0)
{
MakeCopyofEmailToHKLM(emailValue);
}
else
{
}

delete pFileReader;
}

return true;
}

Someone please help me by providing your review comments.

Thanks in advance.

Continue reading...
 
Back
Top