通用头文件

#define _CRT_SECURE_NO_WARNINGS
#pragma once

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<sstream>
#include<io.h>

//from space_0 to space_1
float normalize(float min_0, float max_0, float value, float min_1, float max_1){
return min_1 + ( max_1 - min_1 )*( value - min_0 ) / ( max_0 - min_0 );
}

int C2I(const char* s){
return atoi(s);
}

char* I2C(int i){
char buff[1024] = { 0 };
sprintf_s(buff, 1024, "%d", i);
char*res = (char*)calloc(strlen(buff), sizeof(char));
strcpy_s(res, strlen(res), buff);
return res;
}

char* I2C(int i, size_t alignment_width){
char* cmd = I2C(alignment_width);
printf("cmd=%s\n", cmd);
char buff[1024] = { 0 };
sprintf_s(buff, 1024, "%%0%sd", cmd);
printf("buff=%s\n", buff);
sprintf_s(buff, 1024, cmd, i);
printf("buff=%s\n", buff);
char*res = (char*)calloc(strlen(buff), sizeof(char));
strcpy_s(res, strlen(res), buff);
return res;
}

int S2I(const std::string& s){
std::stringstream ss(s);
int i;
ss >> i;
return i;
}

std::string I2S(int i, size_t alignment_width = 8){
std::string right = std::to_string(i);
if (right.size() < alignment_width){
std::string left;
left.append(alignment_width - right.size(), '0');
left.append(right);
return left;
}
return right;
}

std::string F2S(float d){
return std::to_string(double(d));
}

float S2F(const std::string& str){
std::istringstream iss(str);
float a;
iss >> a;
return a;
}

std::string D2S(double d){
return std::to_string(d);
}

double D2F(const std::string& str){
std::istringstream iss(str);
double a;
iss >> a;
return a;
}

void stringreplace(std::string &str, const std::string& old_value, const std::string& new_value){
size_t pos(0);
while (( pos = str.find(old_value) ) != std::string::npos){
str.replace(pos, old_value.length(), new_value);
}
}

std::string trim(const std::string& str){
std::string res = str;
//str.find_first_not_of(" \t\r\n"),在字符串str中从索引0开始,返回首次不匹配"\t\r\n"的位置
res.erase(0, res.find_first_not_of(" \t\r\n"));
res.erase(res.find_last_not_of(" \t\r\n") + 1);
return res;
}

std::vector<std::string> split(const std::string& str, const std::string& delim){
std::string work = str;
std::vector<std::string> res;
if (str.size() < 1) return res;
size_t pos = std::string::npos;
do{
pos = work.find_first_of(delim);
if (pos == std::string::npos || pos < 0)return res;
std::string word = trim(work.substr(0, pos - 1));
if (word != delim)res.push_back(word);
work = work.substr(pos + 1, work.size() - pos - 1);
} while (pos >= 0);
return res;
}

std::vector<std::string> split(const std::string& str, char delim){
std::vector<std::string> res;
if (str.length() < 1) return res;
std::istringstream ss(str);
std::string word;
while (std::getline(ss, word, delim)){
res.push_back(trim(word));
}
return res;
}

template<class T> bool vectorExist(const std::vector<T>&vec, const T& elem){
for (size_t i = 0; i < vec.size(); ++i){
if (vec[i] == elem)return true;
}
return false;
}

template<class T> size_t vectorPos(const std::vector<T>&vec, const T& elem){
for (size_t i = 0; i < vec.size(); ++i){
if (vec[i] == elem)return i;
}
return std::string::npos;
}

bool isNum(std::string str){
std::stringstream sin(str);
double d;
char c;
if (!( sin >> d )){
return false;
}
if (sin >> c){
return false;
}
return true;
}

//获取目录和子文件夹下所有文件路径
void getFolderFiles(std::string path, std::vector<std::string>& files) {
files.clear();
intptr_t hFile = 0;
//文件信息
struct _finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append("/*").c_str(), &fileinfo)) != -1) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFolderFiles(p.assign(path).append("/").append(fileinfo.name), files);
} else {
files.push_back(p.assign(path).append("/").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}

//获取当前目录下所有文件夹
void getFolderDir(std::string path, std::vector<std::string>& files) {
files.clear();
//文件句柄
intptr_t hFile = 0;
//文件信息
struct _finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append("/*").c_str(), &fileinfo)) != -1) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
files.push_back(p.assign(path).append("/").append(fileinfo.name) );
}

}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}

//获取当前目录下所有文件夹名
void getFolderDirName(std::string path, std::vector<std::string>& files) {
files.clear();
//文件句柄
intptr_t hFile = 0;
//文件信息
struct _finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
files.push_back(fileinfo.name);
}

}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}

//获取当前目录下所有文件
void getFolderFile(std::string path, std::vector<std::string>& files) {
files.clear();
//文件句柄
intptr_t hFile = 0;
//文件信息
struct _finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append("/*").c_str(), &fileinfo)) != -1) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) {
;
} else {
files.push_back(p.assign(path).append("/").append(fileinfo.name) );
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}

//获取当前目录下所有文件名
void getFolderFileName(std::string path, std::vector<std::string>& files) {
files.clear();
//文件句柄
intptr_t hFile = 0;
//文件信息
struct _finddata_t fileinfo;
std::string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) {
;
} else {
files.push_back(fileinfo.name);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}

size_t max(size_t a, size_t b) {
return a > b ? a : b;
}

size_t min(size_t a, size_t b){
return a < b ? a : b;
}

//include ext
std::string getFileName(const std::string& filepath) {
size_t found = filepath.find_last_of('/');
if (found < 0)found = filepath.find_last_of('\\');
//if (found < 0 || found>filepath.size())return std::string();
return filepath.substr(found + 1, filepath.length() - found - 1);
}

//start with '.'
std::string getFileExt(const std::string& filepath) {
size_t found = filepath.find_last_of('.');
if (found < 0||found>filepath.size())return std::string();
return filepath.substr(found, filepath.length() - found);
}

bool copyFile(const std::string& src,const std::string& dst) {
remove(dst.c_str());
std::ifstream in(src, std::ios::binary);
std::ofstream out(dst, std::ios::binary);
if (!in.is_open()) {
std::cerr << "error open file " << src << std::endl;
return false;
}
if (!out.is_open()) {
std::cerr << "error open file " << dst << std::endl;
return false;
}
if (src == dst) {
std::cerr << "the src file can't be same with dst file" << std::endl;
return false;
}

char buf[2048];
long long totalBytes = 0;
while (in) {
//read从in流中读取2048字节,放入buf数组中,同时文件指针向后移动2048字节
//若不足2048字节遇到文件结尾,则以实际提取字节读取。
in.read(buf, 2048);
//gcount()用来提取读取的字节数,write将buf中的内容写入out流。
out.write(buf, in.gcount());
totalBytes += in.gcount();
}
in.close();
out.close();
return true;
}

posted @ 2018-09-14 10:38  aimhabo  阅读(381)  评论(0编辑  收藏  举报