📄 ydefine.cpp
字号:
#include "ydefine.h"
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QRegExp>
/***
* change string value
* 788990 =>908978
*
***/
QString getLittleEndian(const QString&str,int i)
{
QString res;;
yDebug(QString("getLittleEndian(const QString&str= (%1),int i=(%2)" ).arg(str).arg(i) );
switch(i)
{
case 1:
//byte
res="0x";
res.append(str);
yDebug(" getLittleEndian byte 1:"+res);
break;
case 2:
//word = 2byte
res="0x";
res.append(str.mid(2,2));
yDebug(" getLittleEndian word 1:"+res);
res.append(str.mid(0,2));
yDebug(" getLittleEndian word 2:"+res);
break;
case 4:
//dword =4byte
res="0x";
res.append(str.mid(6,2));
yDebug(" getLittleEndian dword 1:"+res);
res.append(str.mid(4,2));
yDebug(" getLittleEndian dword 2:"+res);
res.append(str.mid(2,2));
yDebug(" getLittleEndian dword 3:"+res);
res.append(str.mid(0,2));
yDebug(" getLittleEndian dword 4:"+res);
break;
default:
res=str;
yDebug(" getLittleEndian default:"+res);
break;
}
return res;
}
/***
*get only file name no directory
***/
QString trFileName(const QString &f)
{
QString str;
QFileInfo fi(f);
if(fi.exists () && (f !="NonName") )
{
str=fi.fileName ();
}
else
{
str=f;
}
yDebug(str);
return str;
}
/***
*get only dir no file name
***/
QString trDirName(const QString &f)
{
QString str;
//only for windows
QFileInfo fi(f);
if(fi.exists ()&& (f !="NonName"))
{
str= fi.absolutePath ();
}
else
{
str=f;
}
return str;
}
/***
*get current data time
***/
QString getCurrentTime()
{
QDate date = QDate::currentDate();
QTime time = QTime::currentTime();
QString s;
s=date.toString("yyyy-MM-dd");
s.append(" ");
s.append(time.toString("hh:mm:ss"));
return s;
}
/***
*get the path name from the string
* diffrent: trDirName get a QDir::currentDirPath if no dir
* trPathName get a "" if no dir
***/
QString trPathName(const QString &f)
{
QString str;
//only for windows
QFileInfo fi(f);
if(fi.exists () && (f !="NonName") )
{
str= fi.absolutePath ();
}
else
{
str=f;
}
return str;
}
/***
*remove the start the whitespace
***/
QString stripFirstWhiteSpaces(const QString &f)
{
QString str;
int position = 0;
while (position < f.length())
{
if (f.mid(position, 1) != " ")
break;
position++;
}
str = f.mid(position).trimmed();
return str;
}
/***
*remove the last the whitespace
***/
QString stripLastWhiteSpaces(const QString &f)
{
QString str;
str=f;
int position = f.length ();
while (position < f.length())
{
if (f.mid(position, 1) != " ")
break;
position--;
}
str = f.left(position).trimmed();
return str;
}
/***
*just for qstring::indexOf file, because this function need a const qstring to search
***/
int indexOfStr(const QString &str,const QString & s,Qt::CaseSensitivity cs)
{
return str.indexOf(s ,0,cs);
}
/***
* open a global debug file use fprintf(
***/
FILE *g_stream; //this a flobal value
bool openFileStream()
{
if( (g_stream = fopen( "debug.txt", "a+" )) == NULL ) // C4996
// Note: fopen is deprecated; consider using fopen_s instead
return false;
else
return true;
}
/***
* close a global debug file use fprintf(
***/
bool closeFileStream()
{
if( g_stream)
{
if ( fclose( g_stream ) )
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
/****
*first debug function
*****/
void debugInfos(const char *name, int value)
{
fprintf(g_stream,name, value);
}
void debugInfos(const char *name)
{
fprintf(g_stream,name);
}
/***
* convert string to hex display
***/
QString strToHex(const QString &str)
{
QByteArray bytes;
QString res=NULL;
bytes= str.toAscii();
QString tmp;
for (int i=0;i<bytes.length(); i++)
{
if( int (bytes[i])<16 )
{
tmp=QString::number(bytes[i],16);
tmp=tmp.prepend("0");
res.append(tmp);
res.append(0x20);
}
else
{
res.append(QString::number(bytes[i],16));
res.append(0x20);
}
}
return res;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -