📄 xcommon.cpp
字号:
#include "xcommon.h"
#include <string.h>
#include <mbstring.h>
#include <ctype.h>
#include <time.h>
#include <stdarg.h>
using namespace std;
int asctohex (char c)
{
return c > '9' ? tolower (c) - 'a' + 10 : tolower (c) - '0';
}
int Hex(char *buf)
{
char *str1=buf;
while(*str1==' '||*str1=='\t')
str1++;
int ret=0;
strlwr(str1);
sscanf(str1,"%x",&ret);
return ret;
}
int HexStringToInt(char* InLine)
{
char *str1=InLine,buf[20]="";
char *pbuf=buf;
int len=0,val=0,i=0,j=1;
while(*str1==' ')
str1++;
while((*str1!=' ')&&(*str1!=0))
{
*(pbuf++)=*(str1++);
}
*pbuf=0;
len=strlen(buf);//2DEB6
//printf("\n%d\n",len);
j=0;
while(len--)
{
i=asctohex(buf[len]);
val=val|(i<<j);
j+=4;
}
return val;
}
int CleanHeadTailSpace(char *InLine)
{
char *str1=InLine;
while(1)
{
if(*str1 == ' ')
str1++;
else if(!strncmp(str1,"丂",2))
str1+=2;
else
break;
}
if(InLine != str1)
strcpy(InLine,str1);
str1=InLine + strlen(InLine);
while(1)
{
if(*(str1 - 1) == ' ')
str1--;
else if( !strncmp(str1 - 2,"丂",2))
str1-=2;
else
break;
}
*str1 = 0;
return 1;
}
int CleanTailSpace(char *InLine)
{
char *str1=InLine;
str1=InLine + strlen(InLine);
while(1)
{
if(*(str1 - 1) == ' ')
str1--;
else if( !strncmp(str1 - 2,"丂",2))
str1-=2;
else
break;
}
*str1 = 0;
return 1;
}
int SortWordCompare(const void * a,const void *b)
{
return strcmp(*(char**)a,*(char**)b);
}
int NormalCompare(const void * a,const void *b)
{
return strcmp((char*)a,(char*)b);
}
int SortWord(char *InLine,char* indelimiter,char* outdelimiter,WORKMODE mode,short RemoveDup)
{
char *ppBase[1000];
unsigned int nCount = 0;
char *str1;
str1=InLine;
str1 = strtok(InLine,indelimiter);
while(str1)
{
ppBase[nCount]=new char[strlen(str1)+2];
strcpy(ppBase[nCount],str1);
nCount++;
str1 = strtok(NULL,indelimiter);
}
if(!nCount)
return nCount;
if(mode == E_MODE)
{
qsort(ppBase,nCount,sizeof(char*),SortWordCompare);
if(RemoveDup)
remdup(ppBase,nCount,SortWordCompare);
}
else
{
qsort(ppBase,nCount,sizeof(char*),SortWordCompare/*JapanCompare*/);
if(RemoveDup)
remdup(ppBase,nCount,SortWordCompare/*JapanCompare*/);
}
InLine[0]=0;
strcat(InLine,ppBase[0]);
for(unsigned int i=1;i<nCount;i++)
{
strcat(InLine,outdelimiter);
strcat(InLine,ppBase[i]);
delete []ppBase[i];
}
return nCount;
}
void __cdecl remdup(void * base,uint &cnt,int(__cdecl*comp)(const void* ,const void* ))
{
if(cnt==0)
return;
char **pS;
pS=(char**)base;
int j=1;
for(unsigned int i=1;i<cnt;i++)
{
if( comp( pS+j-1 , pS+i ) )
{
*(pS+j)=*(pS+i);
j++;
}
else
delete [](*(pS+i));
}
cnt=j;
}
char* ExtractColumn(const char *InLine,int col,char *OutLine,const char *delimiter)
{
int len,i;
char *buf,*str1;
if(col<0||(!InLine[0]))
{
OutLine[0]=0;
return NULL;
}
len= strlen(InLine);
buf = new char[len+2];
strcpy(buf,InLine);
str1=strtok(buf,delimiter);
for(i=0;i<col;i++)
{
str1 = strtok(NULL,delimiter);
if(!str1)
{
OutLine[0]=0;
return NULL;
}
}
if(str1)
strcpy(OutLine,str1);
delete []buf;
return OutLine;
}
char* MoveToColumn(char *InLine,int col,const char *delimiter )
{
char *str1=InLine;
for(int i=0;i<col;i++)
{
while(*str1&&!strchr(delimiter,*str1))
str1++;
while(*str1&&strchr(delimiter,*str1))
str1++;
}
if(*str1)
return str1;
else
return NULL;
}
char* ChopEnd(char *InLine)
{
int len;
len=strlen(InLine);
if(InLine[len-1]=='\n')
InLine[len-1]=0;
if(InLine[len-2]==0x0d)
InLine[len-2]=0;
return InLine;
}
int ForceAlign(char *lpszBuf,int cnt)
{
char buf[100];
int nLen = strlen(lpszBuf);
if(nLen < cnt)
{
memset(buf,'0',cnt-nLen);
*(buf+(cnt-nLen))=0;
strcat(buf,lpszBuf);
strcpy(lpszBuf, buf);
return 1;
}
else if(nLen == cnt)
return 1;
else
{
lpszBuf[cnt] = 0;
return 0;
}
}
int Align(const char *InLine,char *OutLine,int cnt)
{
char *str1;
int len = strlen(InLine);
memcpy(OutLine,InLine,len);
str1 = OutLine+len;
if(len>cnt)
{
*str1 = 0;
return 0;
}
else
{
memset(str1,' ',cnt - len);
str1 +=cnt - len;
*str1=0;
return 1;
}
}
int OpenAll(CWorkFile *wk,...)
{
va_list pWk;
CWorkFile *pR;
if(wk->open()==0)
{
cerr<<"Cann't open "<<wk->GetName();
return 0;
}
va_start(pWk,wk);
while( (pR = va_arg(pWk,CWorkFile*))!=0)
{
if( pR->open()==0)
{
cerr<<"Cann't open "<<pR->GetName();
return 0;
}
}
va_end(pWk);
return 1;
}
int CloseAll(CWorkFile *wk...)
{
va_list pWk;
CWorkFile *pR;
wk->close();
va_start(pWk,wk);
while( (pR = va_arg(pWk,CWorkFile*))!=0)
{
pR->close();
}
va_end(pWk);
return 1;
}
// 赏屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯突
// 韧屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯图
const char copyright[] =
"Copyright 1996-2001 (c) Shenzhen Namtek CO., LTD (D.P. team)\n"
" All rights reserved.";
__int32 CCompare::CompareStr(const char *pStr1, const char *pStr2, DWORD match_flag) const
{
return strcmp(pStr1, pStr2);
}
int CWorkFile::ReadUnit(char *buf,int max_line,int max_col)
{
int i=0;
if(!feof(fp))
{
do//read a unit
{
GetLine(buf+max_col*(i++));
}while(*(buf+max_col*(i-1))!=0);
return i;
}
return 0;
}
void CWorkFile::SetMap(SimFileMap *Map1)
{
m_pMap = Map1;
}
void CWorkFile::SetSepChar(char *buf)
{
strcpy(m_pSepChar,buf);
}
char * CWorkFile::GetFieldStringByLineNo(__int32 iLineNo, __int32 iFieldNo, char * szBuf)
{
m_pMap->GetLine(szBuf,iLineNo);
char * pStr = strtok(szBuf, m_pSepChar);
for (__int32 iFN = 0; pStr && iFN < iFieldNo; iFN ++)
pStr = strtok(NULL, m_pSepChar);
if (pStr)
strcpy(szBuf, pStr);
else
*szBuf = 0;
return szBuf;
}
void CWorkFile::rewind ()
{
::rewind (fp);
}
char* CWorkFile::GetName(char *InLine)
{
if(strcpy(InLine,name))
return InLine;
else
return 0;
}
char* CWorkFile::GetName() const
{
return (char*)name;
}
CWorkFile::CWorkFile()
{
strcpy(name,"");
strcpy(extension,"");
strcpy(attrib,"");
strcpy(m_pSepChar,"\t ,;\r\n");
fp=NULL;
}
CWorkFile::CWorkFile(const char* Name,const char* Attrib)
{
strcpy(name,Name);
strcpy(attrib,Attrib);
bIsFirstOpen = true;
msgLineCount = 0;
fp = NULL;
}
CWorkFile& CWorkFile::operator=(CWorkFile& WK)
{
char InLine[12000];
WK.rewind();
while(WK.GetLine(InLine))
{
fprintf(fp,"%s\n",InLine);
}
return WK;
}
CWorkFile& CWorkFile::operator <<(char* InLine)
{
fprintf(fp,"%s",InLine);
return *this;
}
CWorkFile& CWorkFile::operator<<(int i)
{
fprintf(fp,"%d",i);
return *this;
}
int CWorkFile::open()
{
if(*name!=0)
{
fp = fopen (name, attrib);
if ( !fp )
{
return 0;
}
return 1;
}
else
{
return 0;
}
}
char* CWorkFile::GetLine(char* InLine)
{
char *ReturnValue;
int l;
if ( !fp )
{
cerr<<"File hasn't been opened"<<endl;
return NULL;
}
ReturnValue = fgets (InLine, MAX_LINE, fp);
if (ReturnValue)
{
l = strlen (InLine) - 1;
while(l>=0)
{
if (InLine[l] == '\n' || InLine[l] == (char)0x0d)
InLine[l] = 0;
else
break;
l--;
}
}
else
InLine[0] = 0;
return ReturnValue;
}
long cdecl CWorkFile::file_line_cnt(int mode)
{
long cnt;
char line[MAX_LINE];
cnt = 0;
while (fgets (line, MAX_LINE, fp))
{
switch (mode)
{
case 0:
++cnt;
break;
case 1:
if (line[0] && line[0] != ':' && line[0] != '\n')
++cnt;
break;
}
}
::rewind (fp);
return (cnt);
}
void CWorkFile::running_msg(const char *InLine,int timer)
{
if (!(++msgLineCount % timer))
{
char line[81],msg[81],*sp,*dp;
int i;
strncpy (line, InLine, 79);
if (line[strlen (line) - 1] == '\n')
line[strlen (line) - 1] = 0; // strip CR
switch ((msgLineCount / timer ) % 4)
{
case 0:sprintf (msg, "\r[腯 %s (%ld) : ", name, msgLineCount); break;
case 1:sprintf (msg, "\r[\\] %s (%ld) : ", name, msgLineCount); break;
case 2:sprintf (msg, "\r[|] %s (%ld) : ", name, msgLineCount); break;
case 3:sprintf (msg, "\r[/] %s (%ld) : ", name, msgLineCount); break;
}
line[79 - strlen (msg)] = 0;
sp = line;
dp = strchr (msg, 0);
i = strlen (msg);
for (i = strlen (msg), sp = line, dp = strchr (msg, 0);
i < 80 && *sp; ++i, ++dp, ++sp)
{
if (*sp == '\t')
{
if (!(i % 8))
{
*dp++ = ' ';
++i;
}
for (; (i % 8) && (i < 80) ;++i, ++dp)
*dp = ' ';
--dp;
}
else
*dp = *sp;
}
for (; i < 80; ++i)
*dp++ = ' ';
*dp = 0;
cerr << msg << "\r";
}
}
SimFileMap::SimFileMap()
{
m_pHead=0;
m_pOffsetArray=NULL;
m_pSearchArray=0;
m_iCount=0;
m_iCurrentPos=0;
m_iMaxCount = 0;
}
SimFileMap::SimFileMap(int iInitLineCount)
{
m_iMaxCount = iInitLineCount;
m_pHead = new char*[m_iMaxCount];
m_pSearchArray =new char**[m_iMaxCount];
m_pOffsetArray = NULL;
m_iCount = 0;
m_iCurrentPos = 0;
}
bool SimFileMap::IsValidLineNo(int iLineNo)
{
if(m_iCount == 0)
return false;
if(iLineNo >=0 && iLineNo < m_iCount)
return true;
else
return false;
}
int SimFileMap::Print_Data(FILE* fp)
{
int i;
for(i=0;i<m_iCount;i++)
fprintf(fp,"%s\n",m_pHead[i]);
return i;
}
int SimFileMap::Print_Data(LPCSTR lpFilename)
{
FILE *fp = fopen(lpFilename,"w");
if(!fp)
return 0;
else
return Print_Data(fp);
}
int SimFileMap::PrintSortData(FILE *fp,bool flag)
{
int i;
for(i=0;i<m_iCount;i++)
{
if(flag)
fprintf(fp,"%s\n",*(m_pSearchArray[i]));
else
fprintf(fp,"%s\n",*(m_pSearchArray[m_iCount-i-1]));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -