-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
101 lines (90 loc) · 2.71 KB
/
Copy pathutils.cpp
File metadata and controls
101 lines (90 loc) · 2.71 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* utils.cpp - Utilities Implementation
*
* Implements helper functions for string conversion,
* file I/O, and logging.
*
* @version 1.0
* @date 2026-08-21
*/
#include "utils.h"
#include <fstream>
#include <chrono>
#include <iomanip>
#include <sstream>
#include <iostream>
namespace Utils {
std::wstring ToWideString(const std::string& str) {
/**
* Converts a UTF-8 string to a wide string (UTF-16).
*
* Uses MultiByteToWideChar for proper conversion.
*/
int size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
std::wstring wstr(size, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], size);
return wstr;
}
std::string ToString(const std::wstring& wstr) {
/**
* Converts a wide string (UTF-16) to a UTF-8 string.
*
* Uses WideCharToMultiByte for proper conversion.
*/
int size = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
std::string str(size, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], size, nullptr, nullptr);
return str;
}
void Log(const std::string& message) {
/**
* Log a message with a prefix.
*/
std::cout << "[LOG] " << message << std::endl;
}
void LogError(const std::string& message) {
/**
* Log an error message with a prefix.
*/
std::cerr << "[ERROR] " << message << std::endl;
}
std::string GetCurrentTime() {
/**
* Gets the current system time in a readable format.
*/
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S");
return ss.str();
}
bool FileExists(const std::string& path) {
/**
* Checks if a file exists using std::ifstream.
*/
std::ifstream file(path);
return file.good();
}
std::string ReadFile(const std::string& path) {
/**
* Reads an entire file into a string.
*/
std::ifstream file(path);
if (!file.is_open()) {
return "";
}
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
void WriteFile(const std::string& path, const std::string& content) {
/**
* Writes a string to a file, overwriting any existing content.
*/
std::ofstream file(path);
if (file.is_open()) {
file << content;
file.close();
}
}
} // namespace Utils