当前位置:网站首页>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]);
}
边栏推荐
- Develop synergy and efficiently manage | community essay solicitation
- NLog details
- How to achieve the turning effect of wechat video recording?
- 7、STM32——LCD
- 电子元器件行业B2B交易管理系统:提升数据化驱动能力,促进企业销售业绩增长
- 2022年信息安全工程师考试知识点:访问控制
- 2022 point de connaissance de l'examen des ingénieurs en sécurité de l'information: contrôle d'accès
- The lower left corner of vs QT VTK displays the synchronized minor coordinate axis
- How can wechat video numbers be broadcast live on a PC?
- 企业网站的制作流程是什么?设计和制作一个网站需要多长时间?
猜你喜欢

MySQL索引底层为什么用B+树?看完这篇文章,轻松应对面试。

MySQL导致索引失效的情况详解

STM32-------定时器

Idea automatically generates unit tests, doubling efficiency!

The lower left corner of vs QT VTK displays the synchronized minor coordinate axis

再见,2020,这碗毒鸡汤,我先干了
![入参参数为Object,但传递过去却成了[object object] 是因为需要转为JSON格式](/img/8c/b1535e03900d71b075f73f80030064.png)
入参参数为Object,但传递过去却成了[object object] 是因为需要转为JSON格式

7、STM32——LCD

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

高仿书旗小说 Flutter 版,学起来
随机推荐
Go language core 36 lectures (go language practice and application 23) -- learning notes
Solve the problem that slf4j logs are not printed
VS QT VTK 左下角显示同步小坐标轴
2022 Shandong Health Expo, Jinan International Health Industry Expo, China Nutrition and Health Exhibition
3D打印和激光切割流程的初步了解
日化用品行业集团采购管理系统改变传统采购模式,降低采购成本
Several cases of index invalidation caused by MySQL
"Shanda Diwei Cup" the 12th Shandong ICPC undergraduate program design competition
Bitmap load memory analysis
泰勒公式及常用展开
Kotlin set list, set, map operation summary
Kotlin coroutine asynchronous flow
完整开源项目之诗词吧 APP
Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad
Construction of cache stack FIFO in different application scenarios for PLC data operation series (detailed algorithm explanation)
一个人竟然撸了一个网易云音乐云村
The lower left corner of vs QT VTK displays the synchronized minor coordinate axis
E: Unable to acquire lock /var/lib/dpkg/lock
NLog details
Which securities dealers recommend? Is online account opening safe?