r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1#include <fstream>
2
3#ifdef _WIN32
4#include <windows.h>
5#endif
6
7#include "Utils/Utils.hpp"
8
9std::vector<char> utl::readFile(const std::string &filename)
10{
11 std::ifstream file(filename, std::ios::binary | std::ios::ate);
12 if (!file.is_open())
13 {
14 throw std::runtime_error("failed to open file " + filename);
15 }
16 const long int fileSize = file.tellg();
17 if (fileSize <= 0)
18 {
19 throw std::runtime_error("file " + filename + " is empty");
20 }
21 std::vector<char> buffer(static_cast<long unsigned int>(fileSize));
22 file.seekg(0, std::ios::beg);
23 if (!file.read(buffer.data(), fileSize))
24 {
25 throw std::runtime_error("failed to read file " + filename);
26 }
27 return buffer;
28}
29
30std::unordered_map<std::string, std::string> utl::getEnvMap(const char *const *env)
31{
32 std::unordered_map<std::string, std::string> cpyEnv;
33
34#ifdef _WIN32
35 LPCH envStrings = GetEnvironmentStringsA();
36 if (!envStrings)
37 {
38 return cpyEnv;
39 }
40
41 for (LPCH var = envStrings; *var; var += std::strlen(var) + 1)
42 {
43 std::string entry(var);
44 if (const auto pos = entry.find('='); pos != std::string::npos)
45 {
46 cpyEnv.emplace(entry.substr(0, pos), entry.substr(pos + 1));
47 }
48 }
49
50 FreeEnvironmentStringsA(envStrings);
51#else
52 for (const char *const *current = env; (current != nullptr) && (*current != nullptr); ++current)
53 {
54 std::string entry(*current);
55 if (const auto pos = entry.find('='); pos != std::string::npos)
56 {
57 cpyEnv.emplace(entry.substr(0, pos), entry.substr(pos + 1));
58 }
59 }
60#endif
61
62 return cpyEnv;
63}
This file contains utility functions.
std::vector< char > readFile(const std::string &filename)
Definition utils.cpp:9
std::unordered_map< std::string, std::string > getEnvMap(const char *const *env)
Definition utils.cpp:30