-
Notifications
You must be signed in to change notification settings - Fork 0
/
getenv.cxx
42 lines (37 loc) · 1.16 KB
/
getenv.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module;
#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <map>
export module modenv;
[[nodiscard]] auto trim(std::string &line) noexcept {
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
std::array<int, 4> locate;
size_t count = 0;
for (size_t i = 0; i < line.size(); i++) {
if (line[i] == '\'' || line[i] == '"') {
locate[count] = i + 1;
if (count & 1)
locate[count]--;
count++;
}
}
std::string key, value;
key = line.substr(locate[0], locate[1] - locate[0]);
value = line.substr(locate[2], locate[3] - locate[2]);
return std::array<std::string, 2>{key, value};
}
export namespace env {
[[nodiscard]] auto getenv(std::string path = ".env") noexcept {
std::ifstream file(path);
// we store the key and value in dictionary called baniya
std::map<std::string, std::string> baniya;
std::string line;
while (getline(file, line)) {
std::array<std::string, 2> temp{trim(line)};
baniya.emplace(temp[0], temp[1]);
}
return baniya;
}
}; //