Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5df3bea

Browse files
committed
feat: added file utils and improved logger utils
1 parent ca67962 commit 5df3bea

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

include/file.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <vector>
4+
#include <fstream>
5+
#include <filesystem>
6+
#include <optional>
7+
#include "xor.hpp"
8+
9+
namespace commons::file
10+
{
11+
inline std::optional<std::vector<char>> ReadFile(const std::filesystem::path& fullFilePath)
12+
{
13+
if (!exists(fullFilePath))
14+
{
15+
std::wcerr << XORW(L"Cannot open file: ") << fullFilePath << '\n';
16+
return std::nullopt;
17+
}
18+
19+
std::ifstream file(fullFilePath, std::ios::binary | std::ios::ate);
20+
21+
if (!file)
22+
{
23+
std::wcerr << XORW(L"Failed to open file: ") << fullFilePath << '\n';
24+
return std::nullopt;
25+
}
26+
27+
const std::streampos size = file.tellg();
28+
std::vector<char> buffer(static_cast<size_t>(size));
29+
file.seekg(0, std::ios::beg);
30+
file.read(buffer.data(), size);
31+
32+
return buffer;
33+
}
34+
}

include/logger.hpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
#pragma once
22
#include <string>
33

4-
#ifndef NDEBUG
5-
template <typename... Args>
6-
void LOG(const std::wstring& fmt, Args&&... args)
4+
namespace commons::logger
75
{
8-
wprintf(fmt.c_str(), std::forward<Args>(args)...);
9-
}
6+
#ifndef NDEBUG
7+
template <typename... Args>
8+
void LOG(const std::wstring& fmt, Args&&... args)
9+
{
10+
wprintf(fmt.c_str(), std::forward<Args>(args)...);
11+
}
1012

11-
template <typename... Args>
12-
void LOG(const std::string& fmt, Args&&... args)
13-
{
14-
printf(fmt.c_str(), std::forward<Args>(args)...);
15-
}
13+
template <typename... Args>
14+
void LOG(const std::string& fmt, Args&&... args)
15+
{
16+
printf(fmt.c_str(), std::forward<Args>(args)...);
17+
}
1618
#else
17-
#define LOG(...)
19+
template <typename... Args>
20+
void LOG(const std::wstring& fmt, Args&&... args)
21+
{
22+
}
1823
#endif
24+
}

reversing-commons.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
</ItemGroup>
148148
<ItemGroup>
149149
<ClInclude Include="include\console.hpp" />
150+
<ClInclude Include="include\file.hpp" />
150151
<ClInclude Include="include\keyboard.hpp" />
151152
<ClInclude Include="include\memory.hpp" />
152153
<ClInclude Include="include\process.hpp" />

reversing-commons.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,8 @@
4141
<ClInclude Include="include\logger.hpp">
4242
<Filter>Header Files</Filter>
4343
</ClInclude>
44+
<ClInclude Include="include\file.hpp">
45+
<Filter>Header Files</Filter>
46+
</ClInclude>
4447
</ItemGroup>
4548
</Project>

0 commit comments

Comments
 (0)