C/C++常用工具库
Published on 2022-05-30 10:51 in 暂未分类 with 萧海~

C/C++常用工具库

    // utils.h : Include file for standard system include files,
    // or project specific include files.
    #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);
    }
    };
    posted @   萧海~  阅读(341)  评论(0编辑  收藏  举报
    相关博文:
    阅读排行:
    · winform 绘制太阳,地球,月球 运作规律
    · AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
    · 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
    · 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
    · 上周热点回顾(3.3-3.9)
    点击右上角即可分享
    微信分享提示
    电磁波切换