最新消息:热烈庆祝IT小记上线!

[置顶] C++常用函数(续)

又写了一些函数,希望大家给指点一下。。。。小弟不胜感激。。。因为我是在利用ACE 在Linux下编写程序,看到ACE_DEBUG(( _ERROR ACE_TEXT( "  delAllBlank execaption\n" ) ));          等是为了调试程序的方便写的。用的时候可以丢掉的。
/*** wallwind 2011/8/31***/
 
///////十六进制转char。。。。
inthexToChar(string &value, char *buf, int &bufLen)
{  
//    ACE_DEBUG(( _INFO ACE_TEXT( "value:[%s]\n" ), value.c_str()));

    memset(buf, 0x00, bufLen);    
    if ((unsigned int )bufLen < (value.length()/2)) 
    {
        return -1;
    }

    if (value.length() == 0)
    {
        return -1;
    }
    if (value.length() % 2 != 0)
    {
        return -1;
    }

    int bufIndex = 0;
    for (unsigned int i = 0; i < value.length()/2; i++)
    {    
        string tmp = value.substr(i*2, 2);  
        ascToHex(tmp.c_str(), &buf[bufIndex], 2);
        bufIndex++;
    }

    bufLen = bufIndex;
    
    //ACE_HEX_DUMP((LM_INFO, (char *)buf, bufLen, "resp message:"));
    
    return 0;
}

////asc转十六进制
intascToHex(const char *Asc, char *Hex, int AscLen)
{
	char *HexPtr = Hex;
	short i;
	
	for(i = 0; i < AscLen; i++)
	{
		*HexPtr = Asc[i] << 4;
		if (!(Asc[i]>='0' && Asc[i]<='9'))
			*HexPtr += 0x90;
		i++;
		*HexPtr |= (Asc[i] & 0x0F);
		if (!(Asc[i]>='0' && Asc[i]<='9'))
			*HexPtr += 0x09;
		HexPtr++;
	}
	
	return 0;
}

//十六进制转asc
inthexToAsc(const char *Hex, int HexLen, char *Asc)
{
	int i;
	char *AscPtr;
	const char *HexPtr;
	char Temp;

	HexPtr=Hex;
	AscPtr=Asc;

	for(i = 0; i < HexLen; i++)
	{
		Temp = (*HexPtr & 0xf0) >> 4;
		if (Temp < 10)
			*AscPtr = 0x30 + Temp; // 数字0-9
		else
			*AscPtr = 0x37 + Temp; // 字母A-F
			
		AscPtr++;
		
		Temp = *HexPtr & 0x0f;
		if (Temp < 10)
			*AscPtr = 0x30 + Temp;
		else
			*AscPtr = 0x37 + Temp;
			
		AscPtr++;
		HexPtr++;
	}
	
	return 0;
}
//删除字符串中的空格。
voiddelAllBlank(string &line)
{
    if (line.length() <= 0)        
    {
        return;
    }

    string tmp;
    tmp.clear();
    try
    {
       for(unsigned int i = 0; i < line.length(); i++)
        {
            if (line[i] == ' ' ||  line[i] == '\t' 
             || line[i] == '\n' || line[i] == '\r')
            {
                continue;
            }
            else
            {
                tmp += line[i];  
            }
        }
        line.clear();
        line = tmp;
    }
    catch(...)
    {        
        ACE_DEBUG(( _ERROR ACE_TEXT( "  delAllBlank execaption\n" ) ));          
    }
    return;
}

///同上。
voiddelAllBlank(char *pstr)
{
    if (NULL == pstr)
    {
        return;
    }

    int strLen = strlen(pstr);

    string tmpStr;
    try
    {
       for(int i = 0; i < strLen; i++)
        {
            if (pstr[i] == ' ' ||  pstr[i] == '\t' 
             || pstr[i] == '\n' || pstr[i] == '\r')
            {
                continue;
            }
            else
            {
                tmpStr += pstr[i];  
            }
        }
    }
    catch(...)
    {        
        ACE_DEBUG(( _ERROR ACE_TEXT( "  delAllBlank execaption\n" ) ));          
    }

    memset(pstr, 0x00, strLen);
    memcpy(pstr, tmpStr.c_str(), tmpStr.length());
    
    return;
}

/////删除最后一个空格
voiddelLastBlank(char *pstr)
{
    if (NULL == pstr)
    {
        return;
    }    
    
    string tmpStr = pstr;
    memset(pstr, 0x00, strlen(pstr));
    try
    {
        for (;;)
        {
            if (tmpStr.length() > 0)
            {
                if (tmpStr[tmpStr.length()-1] == ' ' ||  tmpStr[tmpStr.length()-1] == '\t' 
                 || tmpStr[tmpStr.length()-1] == '\n' || tmpStr[tmpStr.length()-1] == '\r')
                {
                    tmpStr.erase(tmpStr.length()-1);
                }
                else
                {
                    break;
                }
            }
            else
            {
                break;
            }            
        }
    }    
    catch(...)
    {        
        ACE_DEBUG(( _ERROR ACE_TEXT( "  delLastBlank execaption\n" ) ));          
    }
    memcpy(pstr, tmpStr.c_str(), tmpStr.length());
    
    return;
}

voiddelLastBlank(string &tmpStr )
{
    try
    {
        for (;;)
        {
            if (tmpStr.length() > 0)
            {        
                if (tmpStr[tmpStr.length()-1] == ' ' ||  tmpStr[tmpStr.length()-1] == '\t' 
                 || tmpStr[tmpStr.length()-1] == '\n' || tmpStr[tmpStr.length()-1] == '\r')
                {
                    tmpStr.erase(tmpStr.length()-1);
                }
                else
                {
                    break;
                }
            }
            else
            {
                break;
            }
        }
    }    
    catch(...)
    {        
        ACE_DEBUG(( _ERROR ACE_TEXT( "  delLastBlank execaption\n" ) ));          
    }    
    return;
}
////删除最前边的空格。
voiddelHeadBlank(string &tmpStr )
{
    try
    {
        for (;;)
        {
            if (tmpStr.length() > 0)
            {        
                if (tmpStr[0] == ' ' || tmpStr[0] == '\t' 
                 || tmpStr[0] == '\n' || tmpStr[0] == '\r')
                {
                    tmpStr.erase(0);
                }
                else
                {
                    break;
                }
            }
            else
            {
                break;
            }
        }
    }    
    catch(...)
    {        
        ACE_DEBUG(( _ERROR ACE_TEXT( "  delLastBlank execaption\n" ) ));          
    }    
    return;
}
//加载文件。
intloadFile(string fileName, vector<string> &vecLine)
{
    vecLine.clear();

    FILE *m_fp = fopen(fileName.c_str(), "rb");
    if (NULL == m_fp)
    {        
        ACE_DEBUG(( _ERROR ACE_TEXT( " getline execpaction:[%s]\n" ), fileName.c_str()));            
        return -1;
    }

    char fileLine[MAX_FILE_LINE_LEN];
    
    try
    {    
        for (;;)
        {
            memset(fileLine, 0x00, MAX_FILE_LINE_LEN);
            if (!feof(m_fp)) 
            {                         
                if (NULL == fgets(fileLine, MAX_FILE_LINE_LEN, m_fp))
                {
                    break;
                }
                vecLine.push_back(fileLine);                   
            }
            else
            {
                break;
            }        
        }
    }
    catch(...)
    {        
        fclose(m_fp);
        ACE_DEBUG(( _ERROR ACE_TEXT( " getline execpaction:[%s]\n" ), fileName.c_str()));            
        return -1;
    }
    fclose(m_fp);
    return 0;
}


////写到文件

void writeToFile(string fileName, char *format, ...)
{
    va_list ap;
    char date[10240];
    char msgformat[10240];

    memset(msgformat, 0x00, 10240);
    memset(date, 0x00, 10240);

    FILE *logfp = NULL;
    logfp = fopen(fileName.c_str(), "a+");

    if (NULL == logfp)
    {
        printf("open log file fail.\n");
        return ;
    }

    if (NULL == format)
    {
        printf("NULL == format.\n");
    }

    strcat(msgformat, format);

    int msglen = strlen(msgformat);
    if ('\n' != msgformat[msglen - 1])
    {
        strcat(msgformat, "\n");
    }

    va_start(ap, format);
    vfprintf (logfp, msgformat, ap);
    va_end(ap);

    fflush(logfp);

    fclose(logfp);

    return;
}

猜您喜欢

备案号:苏ICP备12016861号-4