📄 pub.cpp
字号:
#include "stdafx.h"
#include "pub.h"
#include <direct.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
void doSomething()
{
}
char szCommonBuffer[MAX_PATH];
char *GetAppDataPathfile(LPCTSTR scszDataPath, LPCTSTR scszDataFile, LPTSTR szTheBuffer)
{
CFileStatus rStatus;
if(szTheBuffer==NULL)
szTheBuffer=szCommonBuffer;
if(scszDataPath==NULL && scszDataFile==NULL) return NULL;
int i=GetModuleFileName(NULL, szTheBuffer,MAX_PATH);
while(szTheBuffer[i]!='\\' && i>0)i--;
if(i==0)
return NULL;
if(scszDataPath!=NULL){
strcpy(szTheBuffer+i+1,scszDataPath);
// Ensure the path exists;
if(CFile::GetStatus(szTheBuffer,rStatus)){
if(rStatus.m_attribute & CFile::directory ==0){
CFile::Remove(szTheBuffer);
if(_mkdir(szTheBuffer)!=0 )
return NULL;
}
}
else{
if(_mkdir(szTheBuffer)!=0 )
return NULL;
}
}
if(scszDataFile){
if(scszDataFile[0]=='.'){//newNNNN.abc case;
ASSERT(scszDataPath!=NULL);
char bufferTemp[256];
int iIndex=1;
do{
VERIFY(iIndex<10000);
sprintf(bufferTemp, "%s\\new%04i%s", szTheBuffer, iIndex++, scszDataFile);
}while(CFile::GetStatus(bufferTemp, rStatus));
strcpy(szTheBuffer, bufferTemp);
}
else{//Normal case;
if(scszDataPath!=NULL){
strcat(szTheBuffer, "\\");
strcat(szTheBuffer, scszDataFile);
}
else{
strcpy(szTheBuffer+i+1,scszDataFile);
}
}
}
return szTheBuffer;
}
//16进制转化为10近制
int GetIntvalueByString(LPTSTR pstr)
{
CString strtemp;
strtemp = pstr;
return GetIntvalueByString(strtemp);
}
int GetIntvalueByString(CString & str)
{
str.TrimLeft();
str.TrimRight();
CString strTemp;
if(str.Left(2).CompareNoCase("0x")==0)
strTemp=str.Right(str.GetLength()-2);
else
strTemp=str;
int nValue=0;
int nDigit;
int nLen=strTemp.GetLength();
for(int i=0;i<strTemp.GetLength();i++)
{
if(strTemp.GetAt(i)>='a' && strTemp.GetAt(i)<='f')
{
nDigit = (int)(strTemp.GetAt(i)-'a'+10);
if(i<nLen-1)
nValue+=nDigit*(2<<(4*(nLen-i-1)-1));
else
nValue+=nDigit;
}
else if(strTemp.GetAt(i)>='A' && strTemp.GetAt(i)<='F')
{
nDigit = (int)(strTemp.GetAt(i)-'A'+10);
if(i<nLen-1)
nValue+=nDigit*(2<<(4*(nLen-i-1)-1));
else
nValue+=nDigit;
}
else if(strTemp.GetAt(i)>='0' && strTemp.GetAt(i)<='9')
{
nDigit = (int)(strTemp.GetAt(i)-'0');
if(i<nLen-1)
nValue+=nDigit*(2<<(4*(nLen-i-1)-1));
else
nValue+=nDigit;
}
}
return nValue;
}
void TrimFileExt(CString &strFileName)
{
char *p, *pFileName;
pFileName = strFileName.GetBuffer(strFileName.GetLength() + 1);
p = strrchr(pFileName, '.');
if(p){
*p = 0;
strFileName.ReleaseBuffer ();
}
}
BOOL IsFileTyped(const char *FileName, const char *ext)
{
char *p = strrchr(FileName, '.');
if(p)
{
if(*ext != '.'){
p++;
if(*p==0){
return FALSE;
}
}
if(stricmp(p, ext)==0){
return TRUE;
}
}
return FALSE;
}
char * trim(char *str)
{//要求str一定是一个用NULL结尾的字符串
//ASSERT(str);
char *p;
int i,len;
len = strlen(str);
if(len>0)
{
for(p=str, i=len-1;
(i>=0) && (p[i]==32 || p[i]==9 || p[i]=='\r' || p[i]=='\n');
i--) len--;
str[i+1] = 0;
for(; (*p==32 || *p==9 || *p=='\r' || *p=='\n'); p++) len--;
memcpy(str,p,len+1);
}
return (char *)str;
}
char *Path_PickupFileName(const char *path, char *buf, int size)
{
char *p = strrchr(path, '\\');
if(p){
++p;
strncpy(buf, p, size);
}
else{
strncpy(buf, path, size);
}
buf[size-1] = 0;
trim(buf);
return buf;
}
char *Path_PickupFileName(const char *path, CString &strFileTitle)
{
char szFileTitle[MAX_PATH];
Path_PickupFileName(path, szFileTitle, sizeof(szFileTitle));
strFileTitle = szFileTitle;
return (char *)(LPCTSTR)strFileTitle;
}
void AFXAPI DDX_My_Int(CDataExchange *pDX, UINT id, int &iValue)
{
HWND hWndCtrl = pDX->PrepareEditCtrl(id);
char buf[32];
if (pDX->m_bSaveAndValidate)
{
CString str;
//char buf[32];
::GetWindowText(hWndCtrl,buf,5);
str = buf;
if(str.GetLength() >0 && str.GetAt(0) == '-')
iValue = 0-GetIntvalueByString(str.Right(str.GetLength()-1));
else
iValue = GetIntvalueByString(str);
//DDX_Text(pDX, id, iValue);
}
else{
if(iValue <0)
sprintf(buf,"-%x",-iValue);
else
sprintf(buf,"%x",iValue);
::SetWindowText(hWndCtrl, buf);
}
}
void AFXAPI DDX_My_Double(CDataExchange * pDX,UINT id, double& dValue)
{
HWND hWndCtrl = pDX->PrepareEditCtrl(id);
char buf[32];
if (pDX->m_bSaveAndValidate)
{
CString str;
//char buf[32];
::GetWindowText(hWndCtrl,buf,5);
str = buf;
dValue = atof((LPCTSTR)str);
//DDX_Text(pDX, id, iValue);
}
else{
sprintf(buf,"%2.2f",dValue);
::SetWindowText(hWndCtrl, buf);
}
}
void AFXAPI DDV_MyMinMaxInt(CDataExchange* pDX, int value, int minVal, int maxVal)
{
ASSERT(minVal <= maxVal);
if (value < minVal || value > maxVal)
_MyFailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal);
}
AFX_STATIC void AFXAPI _MyFailMinMaxWithFormat(CDataExchange* pDX,
long minVal, long maxVal)
{
//ASSERT(lpszFormat != NULL);
if (!pDX->m_bSaveAndValidate)
{
TRACE0("Warning: initial dialog data is out of range.\n");
return; // don't stop now
}
TCHAR szMin[32];
TCHAR szMax[32];
if(minVal < 0)
wsprintf(szMin, "-%x", -minVal);
else
wsprintf(szMin, "%x", minVal);
wsprintf(szMax, "%x", maxVal);
CString prompt;
//AfxFormatString2(prompt, nIDPrompt, szMin, szMax);
prompt.Format("you must put a value between %s and %s",szMin,szMax);
AfxMessageBox(prompt, MB_ICONEXCLAMATION);
prompt.Empty(); // exception prep
pDX->Fail();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -