首页 > 编程语言 >c++相关的 常用帮助函数集

c++相关的 常用帮助函数集

时间:2022-10-25 23:57:25浏览次数:57  
标签:常用 return 函数 const c++ static str path string

//
// Created by DangXS on 2022/4/27.
//

#ifndef CPLUSPLUS_PROJECT1_HELPER_H
#define CPLUSPLUS_PROJECT1_HELPER_H

#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "dirent.h"
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
#define MICRO_IN_SECOND 1000000
#define NANOS_IN_SECOND 1000000000


// 字符串分割到 vector
static vector<string> string_split(const string &str, const string &delim) {
    vector<string> res;
    if ("" == str) return res;
    //先将要切割的字符串从string类型转换为char*类型
    char *strs = new char[str.length() + 1]; //不要忘了
    strcpy(strs, str.c_str());

    char *d = new char[delim.length() + 1];
    strcpy(d, delim.c_str());

    char *p = strtok(strs, d);
    while (p) {
        string s = p; //分割得到的字符串转换为string类型
        res.push_back(s); //存入结果数组
        p = strtok(NULL, d);
    }
    return res;
}

// 获取文件后缀名
static string getExtension(const string &filename) {
    return filename.substr(filename.find_last_of('.'));
}

// 判断是不是图片
static bool isImage(const string &filename) {
    string ext = getExtension(filename);
    vector<string> formats = {".jpg", ".jpeg", ".png", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", ".webp"};
    auto it = find(formats.begin(), formats.end(), ext);
    return it != formats.end();
}

// 获取文件名,不包含目录
static string getFilename(const string &file_fullpath) {
    return file_fullpath.substr(file_fullpath.find_last_of('/') + 1);
}

//获取文件名,不包含目录和后缀名
static string getFilenameNoExt(const string &filename) {
    string _filename = getFilename(filename);
    return _filename.substr(0, _filename.find_last_of('.'));
}

static string getParentPath(const string &file_fullpath) {
    int idx = file_fullpath.find_last_of('/');
    return file_fullpath.substr(0, idx);
}

// 获取文件绝对路径的父目录名
static string getParentDirname(string file_fullpath) {
    vector<string> groups = string_split(file_fullpath, "/");
    return groups[groups.size() - 2];
}

static bool pathIsExisted(const string &path) {
    return access(path.c_str(), 0) == F_OK;
}

static bool isFolder(const string &path) {
    if (!pathIsExisted(path)) {
        printf_s("%s:%d not existed.\n", __FILE__, __LINE__, path.c_str());
        return false;
    }
    struct stat buffer{};
    return (stat(path.c_str(), &buffer)) == 0 && S_ISDIR(buffer.st_mode);

}


static bool isFile(const string &path) {
    if (!pathIsExisted(path)) {
        printf_s("%s:%d not existed.\n", __FILE__, __LINE__, path.c_str());
        return false;
    }

    struct stat buffer{};
    return (stat(path.c_str(), &buffer)) == 0 && S_ISREG(buffer.st_mode);

}


// 递归创建文件夹
static int mkDirsRecurive(string filename) {
    filename += "/";
    char *fileName = (char *) filename.c_str();
    char *tag;
    int a = 0;
    for (tag = fileName; *tag; tag++, a++) {
        if (*tag == '/') {
            string new_path = filename.substr(0, a + 1);

            if (access(new_path.c_str(), 0) == -1) //如果文件夹不存在
//                mkdir(new_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //则创建
                mkdir(new_path.c_str());
        }
    }

    return 0;
}

//读取某给定路径下所有文件夹与文件名称,并带完整路径
static void getAllFilesInDirs(const string &path, vector<string> &fileFullpath) {

    DIR *pDir;
    struct dirent *ptr;
    if (!(pDir = opendir(path.c_str())))
        return;
    struct stat buffer{};
    while ((ptr = readdir(pDir)) != 0) {
        string new_path = path + "/" + ptr->d_name;
        stat(new_path.c_str(), &buffer);
        if (S_ISDIR(buffer.st_mode)) {
            if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
                getAllFilesInDirs(new_path, fileFullpath);
            }
        } else {
            fileFullpath.push_back(path + "/" + ptr->d_name);
        }
    }
    closedir(pDir);
}

//读取某给定路径下后缀名为format得文件名称,并带完整路径
static void getAllFilesByformat(const string &path, vector<string> &fileFullpath, const string &format) {
    DIR *pDir;
    struct dirent *ptr;
    if (!(pDir = opendir(path.c_str())))
        return;
    while ((ptr = readdir(pDir)) != 0) {
        string new_path = path + "/" + ptr->d_name;
        if (isFolder(new_path)) {
            if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
                getAllFilesByformat(new_path, fileFullpath, format);
            }
        } else if (isFile(new_path) && getExtension(ptr->d_name) == format) {
            string f = path + string("/") + string(ptr->d_name);
//            std::cout << f << std::endl;
            fileFullpath.push_back(f);
        }
    }
    closedir(pDir);
}

// 文件重命名
static int file_rename(string oldname, string newname) {
    return rename(oldname.c_str(), newname.c_str());
}

// 文件移动
static int file_move(const string &src_filename, const string &dst_path) {
    string command = "mv ";
    command += src_filename;
    command += " ";
    command += dst_path; //mv /home/file1 /root/file2
    system((char *) command.c_str());
    return 0;
}

// 文件拷贝
static int file_copy(string src_filename, string dst_filename) {
    string command = "cp ";
    command += src_filename.c_str();
    command += " ";
    command += dst_filename.c_str(); //cp /home/file1 /root/file2
    // printf("%s\n", command.c_str());
    system((char *) command.c_str());
    return 0;
}

// 打乱vector
static vector<string> vector_shuffle(vector<string> v1) {
    vector<string> v2;
// 复制
    v2.assign(v1.begin(), v1.end());
//打乱
    random_shuffle(v2.begin(), v2.end());

    return v2;
}

///替换指定字符串
static string replace_all(string str, const string &old_value, const string &new_value) {
    string str1;
    while (true) {
        string::size_type pos(0);
        if ((pos = str.find(old_value)) != string::npos)
            str1 = str.replace(pos, old_value.length(), new_value);
        else break;
    }
    return str1;
}

static std::string GetTime() {
    time_t timep;
    time(&timep);
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&timep));
    return tmp;
}

// 获取当前毫秒数
// 用于测试程序耗时
// elapsed time = (t2-t1)
static double GetCurrentMiliSec() {
    struct timespec res{};
    double ret = 0;
    clock_gettime(CLOCK_MONOTONIC, &res);
    ret = (double) (res.tv_sec * NANOS_IN_SECOND + res.tv_nsec) / MICRO_IN_SECOND;

    return ret;
}

static bool cmp(const pair<int, float> &a, const pair<int, float> &b) {
    return a.second > b.second;
}

#endif //CPLUSPLUS_PROJECT1_HELPER_H

 

标签:常用,return,函数,const,c++,static,str,path,string
From: https://www.cnblogs.com/dxscode/p/16322009.html

相关文章

  • python获取当前运行函数名
     两种方式:#!/usr/bin/envpython3#coding:utf-8importsys,inspectdeftest_a():print('funcname:',sys._getframe().f_code.co_name)deftest_b():print......
  • C++ #pragma once
    转自:https://www.cnblogs.com/hokyhu/archive/2009/03/30/1425604.html 1.介绍避免同一个文件被include多次,C/C++中有两种方式,一种是#ifndef方式,一种是#pragmaonce方式......
  • VC++之MFC类库中文手册 完整chm版
    VC++之MFC类库中文手册用于查询MFC中各类的对应关系以及使用方法,拥有MFC类索引,参考阅读方便。类库概述概述部分描述了Microsoft基本类库(MFC)6.0版中的类并对其按种类分类。......
  • C++ override关键字
    转自:https://blog.csdn.net/fanyun_01/article/details/791221361.介绍 在派生类中,重写(override)继承自基类成员函数的实现(implementation)时,要满足如下条件:一......
  • 实验三 数组、指针与现代C++标准库
    实验五代码:info.hpp:#pragmaonce#include<iostream>usingnamespacestd;classinfo{public:info(stringni="",stringco="",stringci="",i......
  • C++ std::numeric_limits用法
    转自:https://blog.csdn.net/fengbingchun/article/details/779225581.介绍 std::numeric_limits为模板类,在库编译平台提供基础算术类型的极值等属性信息,需包含<limits>......
  • Dev-C++调试过程
    代码如下:#include<stdio.h>intmain(){inta=5;intb=6;intt;t=a;a=b;b=t;printf("a=%d,b=%d\n",a,b);return0;}步骤如下:1. 新建文件并输入上述代码保......
  • Dev-C++调试过程
    代码如下:#include<stdio.h>intmain(){inta=5;intb=6;intt;t=a;a=b;b=t;printf("a=%d,b=%d\n",a,b);return0;}步骤如下:1. 新建文件并输入上述代码保......
  • C++:运算符重载
    前言  重载我们又称之为多态,重载在编程中的用法非常之多,比如最熟悉的函数重载(基于C++开发的capl语言同样支持函数重载)。当然在C++中仍然支持将多态引用到运算符中,例如*......
  • SQL-五(常用函数)
    SQL常用函数①函数的概念:1.函数的格式:函数名(参数)              即:函数名(参数1,参数2,....) 三个注意事项:Ⅰ:英括号      ......