当前位置:网站首页>Quelques fonctions d'outils couramment utilisées au travail
Quelques fonctions d'outils couramment utilisées au travail
2022-06-23 23:45:00 【Aloe ennuyeuse】
Du code source du client de la machine de chiffrement qu'il a écrit,Inclure la vérification du format heure - date,IPVérification,Conversion de taille,Recherche de fichiers, etc
#include"util.h"
#include<string>
#include"Language.h"
//Appuyez sur Entrée pour quitter
void GETCHAR()
{
cout<< Language::Instance()->GetContent(OutMessage::PressEnter) <<endl;
cin.ignore();//Parce qu'une fonction de rappel renvoie,Le contenu du tampon doit être ignoré
getchar();
}
//Non, non.getch()Implémenter l'entrée du mot de passe,Echo as*,Maximum inputable16Nombre de chiffres, Laissez le mot de passe*No de sortie
#define BACKSPACE 0x08 //Supprimer la cléasccllValeur du Code
char* InputCode(char *pass)
{
int i = 0;
system("stty -icanon"); //Mise en place d'une opération de lecture unique,C'est - à - dire:getchar()Vous pouvez obtenir des caractères sans rentrer
system("stty -echo"); //Désactiver Echo,C'est - à - dire qu'aucun caractère entré n'est affiché
while(i < 16)
{
pass[i]=getchar(); //Obtient la valeur du clavier dans le tableau
if(i == 0 && pass[i] == BACKSPACE)
{
i=0; // S'il n'y a pas de valeur au début ,Entrée Supprimer,Et, Pas de valeur
pass[i]='\0';
continue;
}
else if(pass[i] == BACKSPACE)
{
printf("\b \b"); //Si supprimé, Le curseur avance , Écraser l'espace d'entrée , Puis déplacez le curseur vers l'avant
pass[i]='\0';
i=i-1; // Retour à la valeur précédente continuer l'entrée
continue; //Fin du cycle actuel
}
else if(pass[i] == '\n') // Si vous appuyez sur Entrée ,Fin de l'entrée
{
pass[i]='\0';
break;
}
else
{
printf("*");
}
i++;
}
system("stty echo"); //Activer Echo
system("stty icanon"); // Fermer l'opération de lecture unique ,C'est - à - dire:getchar() Vous devez retourner pour obtenir des caractères
cout<<endl;
return pass; // Retour au résultat final
}
// Grande extrémité à petite extrémité
UINT reversebytes_UINT(UINT value)
{
return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
(value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
}
//Entrée de jugementipEtmaskOui Non
bool IpValid(string str)
{
struct in_addr addr;
int ret;
ret = inet_pton(AF_INET, str.c_str(), &addr);
if( ret != 1 )
{
return false;
}
return true;
}
//Fonction de conversion du temps
long metis_strptime(char *str_time)
{
struct tm stm;
strptime(str_time, "%Y-%m-%d %H:%M:%S",&stm);
long t = mktime(&stm);// Convertir le temps en 1970Année1Mois1 Le nombre de secondes écoulées depuis ,Retour en cas d'erreur-1.
return t;
}
// Conversion au niveau du Journal
string LogLevelConvert(BYTE level)
{
string strloglevel;
switch ((int)level)
{
case 0://
strloglevel = "Debug";
break;
case 1://
strloglevel = "Info";
break;
case 2://
strloglevel = "Warn";
break;
case 3://
strloglevel = "Error";
break;
case 4://
strloglevel = "Fatal";
break;
default:
cout << "LogLevelErreur d'entrée!" << endl;
break;
}
return strloglevel;
}
// Trouver des fichiers de mise à jour
int FindUpGradeFile(char *filename)
{
DIR *dir;
struct dirent *ptr;
string path = "./upgrade";
char *dirpath = (char*)(path.c_str());
char *filenames = (char*)".xml";
char *curfilename = NULL;
int findflag = 0;
if ((dir = opendir(dirpath)) == NULL)
{
perror("Open dir error...");
return -1;
}
while ((ptr = readdir(dir)) != NULL)// Lire à la fin du fichier catalogue renvoie NULL
{
curfilename = rindex(ptr->d_name, '.');
if (curfilename != NULL&& strcmp(curfilename, filenames) == 0)
{
//printf("find file name :%s\n", ptr->d_name); //ptr->d_name Pour le nom de fichier trouvé
findflag = 1;
break;
}
}
if (0 == findflag)
{
cout<< Language::Instance()->GetContent(OutMessage::NoUpgradeFile) <<endl;
return -1;
}
else
{
strcpy(filename, ptr->d_name);
//printf("Find the upgrade file is: %s\n", filename);
cout<< Language::Instance()->GetContent(OutMessage::FindUpgradeFile) << ptr->d_name <<endl;
}
closedir(dir);
return 0;
}
/* Sous - fonction de jugement du format temporel */
std::string trim(const std::string& str)
{
std::string::size_type pos = str.find_first_not_of(' ');
if (pos == std::string::npos) {
return str;
}
std::string::size_type pos2 = str.find_last_not_of(' ');
if (pos2 != std::string::npos) {
return str.substr(pos, pos2 - pos + 1);
}
return str.substr(pos);
}
void split(const std::string& str, std::vector<std::string>* ret_, std::string sep = ",")
{
if (str.empty()) {
return;
}
std::string tmp;
std::string::size_type pos_begin = str.find_first_not_of(sep);
std::string::size_type comma_pos = 0;
while (pos_begin != std::string::npos) {
comma_pos = str.find(sep, pos_begin);
if (comma_pos != std::string::npos) {
tmp = str.substr(pos_begin, comma_pos - pos_begin);
pos_begin = comma_pos + sep.length();
}
else {
tmp = str.substr(pos_begin);
pos_begin = comma_pos;
}
if (!tmp.empty()) {
ret_->push_back(tmp);
tmp.clear();
}
}
}
bool DateVerify(int year, int month, int day)
{
// L'année est limitée à 1970-2100,Peut être enlevé
if (year < 1970 || year > 2100 || month < 1 || month > 12 || day < 1 || day > 31)
{
return false;
}
switch (month) {
case 4:
case 6:
case 9:
case 11:
if (day > 30) {
// 4.6.9.11 Le nombre de jours par mois ne peut être supérieur à 30
return false;
}
break;
case 2:
{
bool bLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if ((bLeapYear && day > 29) || (!bLeapYear && day > 28)) {
// Année bissextile2 Le mois ne peut pas être supérieur à 29Oh, mon Dieu.;Année civile2 Le mois ne peut pas être supérieur à 28Oh, mon Dieu.
return false;
}
}
break;
default:
break;
}
return true;
}
// Vérificationyyyy/mm/dd
bool CheckDateValid(const std::string& strDate)
{
std::string strPureDate = trim(strDate);
if (strPureDate.length() < 8 || strPureDate.length() > 10) {
return false;
}
std::vector<std::string> vecFields;
split(strPureDate, &vecFields, "-");
if (vecFields.size() != 3) {
return false;
}
// TODO: Il est préférable de déterminer si la conversion des caractères est réussie
int nYear = atoi(vecFields[0].c_str());
int nMonth = atoi(vecFields[1].c_str());
int nDay = atoi(vecFields[2].c_str());
return DateVerify(nYear, nMonth, nDay);
}
// VérificationHH:MM:SS
bool CheckTimeValid(const std::string& strTime)
{
std::string strPureTime = trim(strTime);
if (strPureTime.length() < 5 || strPureTime.length() > 8) {
return false;
}
std::vector<std::string> vecFields;
split(strPureTime, &vecFields, ":");
if (vecFields.size() != 3) {
return false;
}
int nHour = atoi(vecFields[0].c_str());
int nMinute = atoi(vecFields[1].c_str());
int nSecond = atoi(vecFields[2].c_str());
bool bValid = (nHour >= 0 && nHour <= 23);
bValid = bValid && (nMinute >= 0 && nMinute <= 59);
bValid = bValid && (nSecond >= 0 && nSecond <= 59);
return bValid;
}
// Le format de la date est: yyyy/mm/dd || yyyy/mm/dd HH:MM:SS yyyy-mm-dd HH:MM:SS
bool CheckDateTimeValid(const std::string& strDateTime)
{
std::string strPureDateTime = trim(strDateTime);
std::vector<std::string> vecFields;
split(strPureDateTime, &vecFields, " ");
if (vecFields.size() != 1 && vecFields.size() != 2) {
return false;
}
// Date seulement
if (vecFields.size() == 1) {
return CheckDateValid(vecFields[0]);
}
return CheckDateValid(vecFields[0]) && CheckTimeValid(vecFields[1]);
}
边栏推荐
猜你喜欢

Le roman du drapeau de l'imitation haute version flutter, apprenez - le
![入参参数为Object,但传递过去却成了[object object] 是因为需要转为JSON格式](/img/8c/b1535e03900d71b075f73f80030064.png)
入参参数为Object,但传递过去却成了[object object] 是因为需要转为JSON格式

STM32-------外部中断

【HackTheBox】 meow

接私活必备的 6 个开源项目

STM32-------定时器

数字物业管理成趋势,传统物业公司如何通过转型实现数字化蝶变?

Can the characteristics of different network structures be compared? Ant & meituan & NTU & Ali proposed a cross architecture self supervised video representation learning method CaCl, performance SOTA

产线工控安全有什么好的解决方案

A person even ran a Weibo app
随机推荐
[Xilinx ax7103 microbalze Learning Notes 6] MicroBlaze custom IP core packaging experiment
Activity的onSaveInstanceState回调时机
Construction of cache stack FIFO in different application scenarios for PLC data operation series (detailed algorithm explanation)
To ensure the safety of groups with special difficulties, Guangzhou Civil Affairs made every effort to do a good job in the three prevention work
smart-doc + Torna 兼容版本
完整开源项目之诗词吧 APP
VS QT VTK 左下角显示同步小坐标轴
解决Slf4j日志不打印问题
Androidkotlin comprehensive and detailed class usage grammar learning guide
Niuke.com: the double pointer problem of receiving rainwater
iNFTnews | 创造者经济的未来在Web3世界中该去向何处?
19 MySQL optimizations commonly used in projects
HDLBits-&gt; Circuits-&gt; Arithmetic Circuitd-&gt; 3-bit binary adder
嵌入式接口之TIM定时器与NVIC的STM32模板库函数的一些解释
牛客网:接雨水的双指针问题
Why can't the netherworld fortress machine be remotely connected to the server? What are the ways to solve such problems?
高仿書旗小說 Flutter 版,學起來
嵌入式接口复习资料
项目中常用到的 19 条 MySQL 优化
三维向量场中的通量