ini逻辑 占位

This commit is contained in:
Jie 2025-03-06 14:03:16 +08:00
parent 78169021c3
commit 4c864d813b
5 changed files with 144 additions and 32 deletions

62
.vscode/settings.json vendored
View File

@ -1,6 +1,66 @@
{ {
"files.associations": { "files.associations": {
"*.cpp": "cpp", "*.cpp": "cpp",
"type_traits": "cpp" "type_traits": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"expected": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"ranges": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"format": "cpp",
"codecvt": "cpp"
} }
} }

View File

@ -0,0 +1,50 @@
#ifndef INI_H
#define INI_H
#include <string>
#include <fstream>
#include <util.h>
#include <filesystem>
#include <ios>
namespace fs = std::filesystem;
namespace ini
{
class IniHelper
{
private:
std::fstream handle;
public:
IniHelper()
{
auto path = (fs::current_path() / "ini.ini").string();
std::cout << path <<std::endl;
handle = std::fstream(path, std::ios::ate);
}
IniHelper(const std::string& file_path)
{
handle = std::fstream(file_path);
}
~IniHelper() noexcept = default;
template <typename T = std::string>
inline T GetValue(const std::string &section, const std::string &key, T &&default_value)
{
throw std::exception("Not Implemented");
}
template <typename T = std::string>
inline bool SaveValue(const std::string &section, const std::string &key, T &&default_value)
{
throw std::exception("Not Implemented");
}
};
}
#endif // !INI_H

View File

@ -12,7 +12,8 @@ namespace ranges = std::ranges;
namespace views = std::ranges::views; namespace views = std::ranges::views;
constexpr size_t buffer_size = 32; constexpr size_t buffer_size = 32;
namespace string{ namespace string
{
template <typename T> template <typename T>
requires std::is_same_v<std::string, T> || std::is_same_v<const char *, T> requires std::is_same_v<std::string, T> || std::is_same_v<const char *, T>
inline std::string replace_string(const std::string &str, T d, T e) inline std::string replace_string(const std::string &str, T d, T e)
@ -51,15 +52,17 @@ namespace string{
{ return T(word.begin(), word.end()); }); { return T(word.begin(), word.end()); });
return std::vector<T>(v.begin(), v.end()); return std::vector<T>(v.begin(), v.end());
} }
template<typename T> template <typename T>
requires std::is_same_v<T, double> || std::is_same_v<T, float> requires std::is_same_v<T, double> || std::is_same_v<T, float>
double round(T value, int c){ double round(T value, int c)
{
auto temp = 1; auto temp = 1;
for(int i=0;i<c;i++){ for (int i = 0; i < c; i++)
temp=temp*10; {
temp = temp * 10;
} }
return std::round(value*temp)/temp; return std::round(value * temp) / temp;
} }
} }

View File

@ -3,5 +3,5 @@
#include "util.h" #include "util.h"
#include "stringconv.h" #include "stringconv.h"
#include "ini.h"
#endif #endif

View File

@ -5,41 +5,43 @@
#include <expected> #include <expected>
#include <chrono> #include <chrono>
namespace util{ namespace util
template <typename T>
inline std::expected<std::string, std::string> to_string(T value)
{ {
template <typename T>
inline std::expected<std::string, std::string> to_string(T value)
{
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, double>) if constexpr (std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, double>)
{ {
char buffer[buffer_size]; char buffer[buffer_size];
auto res = std::to_chars(buffer, buffer + buffer_size, value); auto res = std::to_chars(buffer, buffer + buffer_size, value);
if (res.ec != std::errc()) if (res.ec != std::errc())
{ {
return std::unexpected(std::make_error_code(res.ec).message()); return std::unexpected(std::make_error_code(res.ec).message());
} }
return std::string(buffer, res.ptr - buffer); return std::string(buffer, res.ptr - buffer);
} }
else if constexpr (std::is_same_v<typename std::remove_const<T>::type, else if constexpr (std::is_same_v<typename std::remove_const<T>::type,
char *>) char *>)
{ {
return std::to_string(value); return std::to_string(value);
} }
} }
template <typename T = double> // requires std::is_same_v<T, std::string> template <typename T = double> // requires std::is_same_v<T, std::string>
inline std::expected<T, std::string> stoi(const std::string &str) inline std::expected<T, std::string> stoi(const std::string &str)
{ {
T value; T value;
auto res = std::from_chars(str.c_str(), str.c_str() + str.size(), value); auto res = std::from_chars(str.c_str(), str.c_str() + str.size(), value);
if (res.ec != std::errc()) if (res.ec != std::errc())
{ {
return std::unexpected(std::make_error_code(res.ec).message()); return std::unexpected(std::make_error_code(res.ec).message());
} }
return value; return value;
} }
inline std::string get_time_now(){ inline std::string get_time_now()
{
auto time_now = std::chrono::system_clock::now(); auto time_now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(time_now); auto now_c = std::chrono::system_clock::to_time_t(time_now);
char buffer[32]; char buffer[32];
@ -52,9 +54,6 @@ inline std::expected<T, std::string> stoi(const std::string &str)
result = string::replace_string(result, "\n", ""); result = string::replace_string(result, "\n", "");
return std::move(result); return std::move(result);
} }
} }
#endif
#endif