C++工具函数

2019-07-17  本文已影响0人  this_is_for_u

C++ 工具函数

读取二进制文件

#include<fstream>
std::vector<char> GetDataFromBinaryFile(const std::string& file_name) {
    std::ifstream in(file_name.c_str(), std::ios::binary);
    if (in.fail()) {
        return std::vector<char>();
    }
    std::vector<char> contents(std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>()));
    in.close();
    return contents;
}

字符串split

template <typename Out>
void string_split(const std::string& s, char delim, Out result) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        *(result++) = item;
    }
}

std::vector<std::string> string_split(const std::string& s, char delim) {
    std::vector<std::string> elems;
    string_split(s, delim, std::back_inserter(elems));
    return elems;
}

删除文件

#include <dirent.h>
int DeleteVideoFile(const std::string& file_name) {
    return remove(file_name.c_str());
}

定义枚举类,并将枚举常量转成int

enum class VideoEnum { kA = 0, kB = 1 };

template <typename E>
constexpr auto toUtype(E enumerator) noexcept {
    return static_cast<std::underlying_type_t<E>>(enumerator);
}

获取当前目录剩余存储空间byte

#include <sys/statvfs.h>
long long GetAvailableSpace(const std::string& path) {
    struct statvfs stat;
    if (statvfs(path.c_str(), &stat) != 0) {
        return -1;
    }
    return stat.f_bsize * stat.f_bavail;
}

按顺序获取某一目录文件名

int FileNameFilter(const struct dirent* cur) {
    std::string str(cur->d_name);
    if (str.find("xxx") != std::string::npos) {
        return 1;
    }
    return 0;
}

std::vector<std::string> GetDirXXXSortedPath(const std::string& dir_path) {
    struct dirent** namelist;
    std::vector<std::string> ret;
    int n = scandir(dir_path.c_str(), &namelist, FileNameFilter, alphasort);
    if (n < 0) {
        return ret;
    }
    for (int i = 0; i < n; ++i) {
        std::string path = dir_path + "/" + namelist[i]->d_name;
        ret.emplace_back(path);
    }
    free(namelist);
    return ret;
}

判断文件是否存在

bool FileExists(const std::string& filename) {
    std::ifstream ifile(filename.c_str());
    return (bool)ifile;
}

打印耗时的helper

log

#define KNRM  "\x1B[0m"
#define KRED  "\x1B[31m"
#define KGRN  "\x1B[32m"
#define KYEL  "\x1B[33m"
#define KBLU  "\x1B[34m"
#define KMAG  "\x1B[35m"
#define KCYN  "\x1B[36m"
#define KWHT  "\x1B[37m"
//log添加颜色
#define LOG_RELEASE 1
#define LOG_DEBUG 3

#ifdef DEBUG
#define LOG_LEVEL 4 // DEBUG
#else
#define LOG_LEVEL 2 //RELEASE
#endif


int log(int priority, const char *format, ...) {
  if (priority > LOG_LEVEL) {
    return 1;
  }
  printf("%s", KBLU);
  va_list args;
  va_start(args, format);
  vprintf(format, args);
  va_end(args);
  printf("%s", KNRM);
  return 0;
}

timer

#include <iostream>
#include <chrono>
#include <string>
#include <ctime>
#include <sys/time.h>
#include <fstream>

using namespace std::chrono;
using llong = long long;

class TimerLog {
public:
  TimerLog(const std::string tag) {
    m_begin = high_resolution_clock::now();
    m_tag = tag;
  }

  void reset() {
    m_begin = high_resolution_clock::now();
  }

  llong elapsed() {
    return (llong) duration_cast<std::chrono::milliseconds>(high_resolution_clock::now() - m_begin).count();
  }

  static time_point<high_resolution_clock> now() {
    return high_resolution_clock::now();
  }

  static llong diffUs(time_point<high_resolution_clock> before, time_point<high_resolution_clock> after) {
    return (llong) duration_cast<std::chrono::microseconds>(after - before).count();
  }

  static llong diffMs(time_point<high_resolution_clock> before, time_point<high_resolution_clock> after) {
    return (llong) duration_cast<std::chrono::milliseconds>(after - before).count();
  }

  TimerLog() {
    auto time = duration_cast<std::chrono::milliseconds>(high_resolution_clock::now() - m_begin).count();
    log(LOG_DEBUG, "time { %s } %f ms\n", m_tag.c_str(), (double)time);
  }

  static llong getCurrentMs() {
    std::chrono::milliseconds ms =
        std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
    return static_cast<llong>(ms.count());
  }

  static void showCurTime() {
    time_t now = time(0);
    char* dt = ctime(&now);
    log(LOG_DEBUG, "cur time is %s\n", dt);
    log(LOG_DEBUG, "cur ms %lld\n", getCurrentMs());
  }

  static struct timeval getCurrentTimeofDay() {
    struct timeval time;
    gettimeofday(&time, NULL);
    return time;
  }

private:
  time_point<high_resolution_clock> m_begin;
  std::string m_tag;
};

上一篇 下一篇

猜你喜欢

热点阅读