48 lines
No EOL
1.1 KiB
C++
48 lines
No EOL
1.1 KiB
C++
#include "dotenv.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
|
|
using namespace Etheryo;
|
|
|
|
DotEnv::DotEnv()
|
|
: _fileData({})
|
|
{
|
|
}
|
|
|
|
int DotEnv::readFile(const std::string& path)
|
|
{
|
|
std::ifstream file;
|
|
std::string line;
|
|
|
|
file.open(path);
|
|
|
|
if (!file)
|
|
return 1;
|
|
while (getline(file, line)) {
|
|
const size_t pos = line.find("=");
|
|
|
|
// Commentary detected, ignoring line
|
|
if (line.find("#") != std::string::npos)
|
|
continue;
|
|
// If could not find '=', skip the line
|
|
if (pos == std::string::npos)
|
|
continue;
|
|
|
|
// Gathering name
|
|
const std::string name = line.substr(0, pos);
|
|
// Gathering value and removing quotes
|
|
std::string value = line.substr(pos + 1, line.size());
|
|
auto deleteQuotes = std::remove(value.begin(), value.end(), '"');
|
|
value.erase(deleteQuotes, value.end());
|
|
// Adding the name and value to the map
|
|
_fileData.emplace(name, value);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
const std::string& DotEnv::operator[](const std::string& rvalue)
|
|
{
|
|
return _fileData[rvalue];
|
|
} |