C/C++常用工具库(utils.h)

xingyun86 2021-6-11 912

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:
    static std::string format_string(const char* format, ...)
    {
        va_list args = nullptr;
        va_start(args, format);
        std::vector<wchar_t> buffer(BUFSIZ);
        int length = vsnprintf((char* const)buffer.data(), buffer.size(), format, args);
        if (length > buffer.size())
        {
            buffer.resize(length);
            vsnprintf((char* const)buffer.data(), length, format, args);
        }
        va_end(args);
        return ((char* const)buffer.data());
    }
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);
    }
};


上传的附件:
×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回