| |
| |
| |
| #pragma once |
| |
| #include <stdio.h> |
| #include <string.h> |
| |
| #include <string> |
| #include <vector> |
| #include <fstream> |
| #include <functional> |
| #include <ppsyqm/json.hpp> |
| |
| #ifndef _MSC_VER |
| #include <unistd.h> |
| #include <limits.h> |
| #define MAX_PATH_LEN PATH_MAX |
| #else |
| #include <winsock2.h> |
| #define MAX_PATH_LEN MAX_PATH |
| #define popen _popen |
| #define pclose _pclose |
| #endif |
| |
| class Utils { |
| public: |
| static std::string RunCmd(const std::string& cmd) |
| { |
| std::string ret = (""); |
| FILE* f = popen((cmd+" 2>&1").c_str(), "r"); |
| if (f != nullptr) |
| { |
| size_t s = 0; |
| char d[MAX_PATH_LEN] = { 0 }; |
| while (!feof(f)) |
| { |
| s = fread(d, sizeof(char), sizeof(d) / sizeof(char), f); |
| if (s > 0) |
| { |
| ret.append(d, s); |
| } |
| } |
| pclose(f); |
| } |
| else |
| { |
| printf("popen %s error:(%d),%s.\n", cmd.c_str(), errno, strerror(errno)); |
| } |
| return ret; |
| } |
| |
| static std::string AppDir() { |
| std::string data(MAX_PATH_LEN, '\0'); |
| #ifndef _MSC_VER |
| char szTmp[64]; |
| sprintf(szTmp, "/proc/%d/exe", getpid()); |
| if (readlink(szTmp, (char*)data.data(), data.size()) <= 0) { |
| return (""); |
| } |
| #else |
| GetModuleFileNameA(NULL, (LPSTR)data.data(), (DWORD)data.size()); |
| for (auto& it : data) { if (it == '\\') { it = '/'; } }; |
| #endif |
| return data.substr(0, data.rfind('/')); |
| } |
| public: |
| template <typename ...Args> |
| static std::string format_string(const char* format, Args... args) { |
| char buffer[BUFSIZ] = {0}; |
| size_t newlen = snprintf(buffer, BUFSIZ, format, args...); |
| if (newlen > BUFSIZ) |
| { |
| std::vector<char> newbuffer(newlen + 1); |
| snprintf(newbuffer.data(), newlen, format, args...); |
| return std::string(newbuffer.data()); |
| } |
| return buffer; |
| } |
| public: |
| static int Load(const std::string& fname, std::function<int(ppsyqm::json&, void*)> cb, void* ptr) |
| { |
| try |
| { |
| std::string str((std::istreambuf_iterator<char>(std::ifstream(fname, std::ios::in | std::ios::binary).rdbuf())), std::istreambuf_iterator<char>()); |
| if (str.empty()) |
| { |
| str = "{}"; |
| } |
| auto json_obj = json::parse(str); |
| if (json_obj.is_object()) |
| { |
| return cb(json_obj, ptr); |
| } |
| } |
| catch (const std::exception&e) |
| { |
| std::cerr << e.what() << std::endl; |
| } |
| return (-1); |
| } |
| static int Save(const std::string& fname, std::function<int(ppsyqm::json&, void*) > cb, void* ptr) |
| { |
| try |
| { |
| std::ofstream of = {}; |
| std::string str((std::istreambuf_iterator<char>(std::ifstream(fname, std::ios::in | std::ios::binary).rdbuf())), std::istreambuf_iterator<char>()); |
| if (str.empty()) |
| { |
| str = "{}"; |
| } |
| auto json_obj = ppsyqm::json::parse(str); |
| if (json_obj.is_object()) |
| { |
| of.open(fname, std::ios::out); |
| if (of.is_open()) { |
| if (cb(json_obj, ptr) == 0) |
| { |
| std::string dump = json_obj.dump(2); |
| of.write(dump.data(), dump.size()); |
| return 0; |
| } |
| } |
| } |
| } |
| catch (const std::exception& e) |
| { |
| std::cerr << e.what() << std::endl; |
| } |
| return (-1); |
| } |
| }; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)