certificate_manager.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "certificate_manager.h"
bool CertificateManager::Read(const std::string& path)
{
std::ifstream certificateFile;
certificateFile.open(path);
if (!certificateFile.is_open())
{
return false;
}
std::getline(certificateFile, m_certificate);
certificateFile.close();
return true;
}
certificate_manager.h
/* © 2021 AO Kaspersky Lab */
#ifndef SEPARATE_STORAGE_CERTIFICATE_MANAGER_H
#define SEPARATE_STORAGE_CERTIFICATE_MANAGER_H
#include <string>
class CertificateManager
{
public:
// @brief Reads a string from the certificate file and stores it in the m_certificate.
// This function is used only to demonstrate the capability to read from a file
// and does not carry another payload.
// @param[in] path specification of the file we want to read
// @return true if successful, otherwise false
bool Read(const std::string& path);
// @brief Gets the contents of the m_certificate variable.
// @return constant reference to the m_certificate
std::string Get()
{
return m_certificate;
}
private:
std::string m_certificate;
};
#endif
main.cpp
#include "certificate_manager.h"
#include "file_names.h"
#include <iostream>
#include <fstream>
const std::string Tag = "[CertificateManager] ";
const std::string ErrorTag = "[Error] ";
void OpenUnavailableFile(const std::string& fileName)
{
std::cout << Tag << "Try to read unavailable file '" << fileName << "'" << std::endl;
std::ifstream inpFile;
inpFile.open(fileName);
if (!inpFile.is_open())
{
std::cout << Tag << "Unable to open the '" << fileName << "' file. It is correct behaviour." << std::endl;
}
else
{
std::cout << Tag << ErrorTag << “The ‘“ << fileName << “‘ file is open. It is not correct behaviour." << std::endl;
}
}
int main (int argc, char* argv[])
{
try
{
CertificateManager certificate;
std::cout << Tag << "Try to read '" << CertificateFileName << "' file" << std::endl;
if (!certificate.Read(CertificateFileName))
{
std::cout << Tag << ErrorTag << "Unable to read file: '" << UserListFileName << "'" << std::endl;
}
else
{
std::cout << Tag << "Success" << std::endl
<< Tag << "Certificate data: '" << certificate.Get() << "'" << std::endl;
}
// Attempts to read a file that we cannot access.
OpenUnavailableFile(UserListFileName);
}
catch(std::exception& ex)
{
std::cout << Tag << ErrorTag << ex.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Page top