存储单位转换是不是很麻烦
我查看了很多文章,没有一个完美的解决代码,那就干脆自己手搓
kitUnit.h
/**
******************************************************************************
* @file : kitUnit.h
* @author : CircleDBA
* @mail : weiyuanquan@kingbase.com.cn
* @blog : circle-dba.blog.csdn.net
* @date : 24-5-23
******************************************************************************
*/
#ifndef KINGBASEMANAGERTOOLS_KITUNIT_H
#define KINGBASEMANAGERTOOLS_KITUNIT_H
#include <iostream>
#include <sstream>
#include <string>
#include <any>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <functional>
#include "kitStr.h"
class kitUnit {
protected:
std::string retainNumbers(const std::string& input) {
std::string result;
for (char ch : input) {
if (std::isdigit(static_cast<unsigned char>(ch))) {
result.push_back(ch);
}
}
return result;
};
std::map<std::string, int> UnitToInt = {
{"BYTE", 0},
{"B", 1},
{"KB", 1},
{"K", 1},
{"MB", 2},
{"M", 2},
{"GB", 3},
{"G", 3},
{"TB", 4},
{"T", 4},
{"EB", 5},
{"E", 5},
{"ZB", 6},
{"Z", 6},
{"YB", 7},
{"Y", 7},
{"BB", 8},
{"NB", 9},
{"N", 9},
{"DB", 10},
{"D", 10},
};
std::pair<double,std::string> UnitStrSplit(std::string size){
std::istringstream ss(size);
double number = 0;
ss >> number;
std::string remaining;
std::getline(ss, remaining);
return std::make_pair(number, remaining);
};
public:
//存储单位转换-Byte转各种单位
boost::any ByteToUnit(long long size, std::string unit){
return ByteToUnit(size,unit,false);
};
boost::any ByteToUnit(long long size, std::string unit , bool showUnit = false)
{
long long result = size;
std::string unit_str = kitStr::toUpper(unit);
result = size / pow(1024,UnitToInt[unit_str]);
if(showUnit){
std::string number_str = std::to_string(result);
std::string numberUnitStr = number_str + unit_str;
return numberUnitStr;
};
return result;
};
//存储单位转换-带单位转换Byte
boost::any UnitToByte(std::string size){
return UnitToByte(size, false);
};
boost::any UnitToByte(std::string size,bool showUnit){
double number;
long long result;
std::string number_str,unit_str;
std::string defaultUnit = "Byte";
std::ostringstream ss;
if(kitStr::containsEnglish(size)){
std::pair<double,std::string> split_str = UnitStrSplit(size);
number = split_str.first;
unit_str = split_str.second;
}else{
number = std::stod(size);
unit_str = defaultUnit;
}
unit_str = kitStr::toUpper(unit_str);
result = number * pow(1024,UnitToInt[unit_str]);
if(showUnit){
std::string _number_str = std::to_string(result);
std::string numberUnitStr = _number_str + kitStr::toUpper(defaultUnit);;
return numberUnitStr;
};
return result;
};
};
#endif //KINGBASEMANAGERTOOLS_KITUNIT_H
kitStr.h
/**
******************************************************************************
* @file : kitStr.h
* @author : CircleDBA
* @mail : weiyuanquan@kingbase.com.cn
* @blog : circle-dba.blog.csdn.net
* @date : 24-5-23
******************************************************************************
*/
#ifndef KINGBASEMANAGERTOOLS_KITSTR_H
#define KINGBASEMANAGERTOOLS_KITSTR_H
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
class kitStr {
protected:
public:
static std::vector<std::string> split(const std::string &str, const char pattern) {
std::vector<std::string> res;
std::stringstream input(str); //读取str到字符串流中
std::string temp;
//使用getline函数从字符串流中读取,遇到分隔符时停止,和从cin中读取类似
//注意,getline默认是可以读取空格的
while (getline(input, temp, pattern)) {
res.push_back(temp);
}
return res;
};
static bool containsEnglish(const std::string& str) {
for (char c : str) {
if (std::isalpha(static_cast<unsigned char>(c))) { // 确保处理时是无符号字符
return true;
}
}
return false;
}
static std::string toUpper(std::string str){
transform(str.begin(),str.end(),str.begin(),::toupper);
return str;
}
static std::string toLower(std::string str){
transform(str.begin(),str.end(),str.begin(),::tolower);
return str;
}
};
#endif //KINGBASEMANAGERTOOLS_KITSTR_H
示例代码:
showUnit 字段为BOOL类型 用于控制数据返回类型
本案例返回为boost::any 提取时注意处理一下
long long a = boost::any_cast<long long>(kitUnit::UnitToByte("1TB"));
std::string = boost::any_cast<string>(kitUnit::UnitToByte("1TB", true));
double c = boost::any_cast<double>(kitUnit::ByteToUnit(pow(1024,8),"TB"));
std::string d = boost::any_cast<string>(kitUnit::UnitToByte((pow(1024,5),"TB",true));