⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stroperation.cpp

📁 跨操作系统的微型中间件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "StrOperation.h"
#include <stdlib.h>
#include <ctype.h>
#include "OSHeaders.h"
#include "MyAssert.h"

#ifndef min
#define min(a,b)            (((a) < (b)) ? (a) : (b))
#endif


// added by samsmith
// convert unix format to dos format  
Bool CStrOperate::ModifyPath(std::string& path)
{
  std::string where,val;
#ifdef WIN32
  where = "/"; val = "\\";
  Replace(path,where,val);
  where = "\\\\"; val = "\\";
  Replace(path,where,val);
#else
  where = "\\"; val = "/";
  Replace(path,where,val);
  where = "//",val ="/";
  Replace(path,where,val);
#endif
  return TRUE;
}
Bool CStrOperate::Replace(std::string& str,std::string& where,std::string& val,Bool cycle,Bool rfind)
{
	int npos = -1;
	if( cycle == FALSE )
	  {
	    if( rfind )
	      {
		npos = str.rfind(where);
		if( npos == -1 )
		  return FALSE;
		str.replace( npos,where.length(),val);
	      }
	    else
	      {
		npos = str.find( where );
		if( npos == -1 )
		  return FALSE;
		str.replace( npos,where.length(),val );
	      }
	  }
	else
	  {
	    while( (npos = str.find(where)) != -1 )
	      {
		str.replace(npos,where.length(),val);
	      }
	  }

	return TRUE;
}
void CStrOperate::ConvertUnixToDos(std::string& strConvert)
{
	int j=0,len = strConvert.length();
	Assert( len>0 );
	std::string strOrigin = strConvert;
	strConvert.erase();
	
	for(int i=0;i<len;i++)
	{
		if( strOrigin.at(i)==0x0A ) // 找到0A
		{
			if( i == 0 ) // 字符缓冲中,第一个是0X0A。
			{
				strConvert += 0x0D;
				strConvert += 0x0A;
			}
			else
			{
				if( strOrigin.at(i-1)!= 0x0D ) // 0x0A,前面不是0X0D
				{
					strConvert +=0x0D;
					strConvert +=0x0A;
				}
				else
				{
					strConvert +=0x0A;
				}
			}
		}
		else
		{
			strConvert +=strOrigin.at(i);
		}
	}
}
// end;

string CStrOperate::GetKeyValue(const string& strSrc, string strKeyItem, string strEndKey)
{
	int nPos = 0;
	return GetKeyValue(strSrc, strKeyItem, strEndKey, nPos);
}

string CStrOperate::GetKeyValue(const string& strSrc, string strKeyItem, string strEndKey, int& nStartPos/*=0*/)
{
	//为了使对大小写不敏感
	string strStd = strSrc;
	int i;
	for(i=0; i<strSrc.length(); i++)
	{
		strStd.at(i) = tolower(strStd.at(i));  
	}
	
	for(i=0; i<strKeyItem.length(); i++)
	{
		strKeyItem.at(i) = tolower(strKeyItem.at(i));
	}
	
	for(i=0; i<strEndKey.length(); i++)
	{
		strEndKey.at(i) = tolower(strEndKey.at(i));
	}
	///////////
	
	string strItems[10];
	int pos1 = 0;
	int pos2 = strKeyItem.find("\\", pos1);
	int n;
	for(n=0; pos2 != -1; n++)
	{
		strItems[n] = strKeyItem.substr(pos1, pos2-pos1);
		pos1 = pos2 + 1;
		pos2 = strKeyItem.find("\\", pos1);
	}
	strItems[n] = strKeyItem.substr(pos1, strKeyItem.length() - pos1);
	
	int iBegin = 0;
	
	for(int j=0; j<=n; j++)
	{
		iBegin = strStd.find(strItems[j], nStartPos+iBegin) + strItems[j].length();
		if(iBegin == -1 + strItems[j].length())
		{
			return "";
		}
	}
	
	//////////
	//如果在行结束"\r\n"之前没有找到strEndKey, 则以"\r\n"结束
	int iEndPos1 = strStd.find(strEndKey, iBegin);
	
	int iEndPos2 = strStd.find("\r\n", iBegin);
	
	int iEndPos;
	
	if(iEndPos1 != -1 && iEndPos2 != -1)
	{
		iEndPos = min(iEndPos1, iEndPos2);
	}
	else if(iEndPos1 == -1 && iEndPos2 == -1)
	{
		iEndPos = strStd.length();
	}
	else if(iEndPos1 == -1)
	{
		iEndPos = iEndPos2;
	}
	else if(iEndPos2 == -1)
	{
		iEndPos = iEndPos1;
	}
	else
	{
		return "";
	}
	
	//////////
	nStartPos = iEndPos;
	return strSrc.substr(iBegin, iEndPos - iBegin);
}

string CStrOperate::GetKeyValue(const string& strSrc, string strKeyItem, int nLength)
{
	//为了使对大小写不敏感
	string strStd = strSrc;
	int i;
	for(i=0; i<strSrc.length(); i++)
	{
		strStd.at(i) = tolower(strStd.at(i));  
	}
	
	for(i=0; i<strKeyItem.length(); i++)
	{
		strKeyItem.at(i) = tolower(strKeyItem.at(i));
	}
	
	///////////
	
	string strItems[10];
	int pos1 = 0;
	int pos2 = strKeyItem.find("\\", pos1);
	int n;
	for(n=0; pos2 != -1; n++)
	{
		strItems[n] = strKeyItem.substr(pos1, pos2-pos1);
		pos1 = pos2 + 1;
		pos2 = strKeyItem.find("\\", pos1);
	}
	strItems[n] = strKeyItem.substr(pos1, strKeyItem.length() - pos1);
	
	int iBegin = 0;
	
	for(int j=0; j<=n; j++)
	{
		iBegin = strStd.find(strItems[j], iBegin) + strItems[j].length();
		if(iBegin == -1 + strItems[j].length())
		{
			return "";
		}
	}
	
	//////////
	int iEndPos = iBegin + nLength;
	
	return strSrc.substr(iBegin, iEndPos - iBegin);
}

int CStrOperate::ReplaceItemValue(string &strSrc, string strKeyItem, string strEndKey, const string strNewValue, int nStartPos)
{
	//////////
	string strStd = strSrc;
	int i;
	for(i=0; i<strSrc.length(); i++)
	{
		strStd.at(i) = tolower(strStd.at(i));  //为了使对大小写不敏感
	}
	
	for(i=0; i<strKeyItem.length(); i++)
	{
		strKeyItem.at(i) = tolower(strKeyItem.at(i));
	}
	
	for(i=0; i<strEndKey.length(); i++)
	{
		strEndKey.at(i) = tolower(strEndKey.at(i));
	}
	///////////

	string strItems[10];
	int pos1 = 0;
	int pos2 = strKeyItem.find("\\", pos1);
	int n;
	for(n=0; pos2 != -1; n++)
	{
		strItems[n] = strKeyItem.substr(pos1, pos2-pos1);
		pos1 = pos2 + 1;
		pos2 = strKeyItem.find("\\", pos1);
	}
	strItems[n] = strKeyItem.substr(pos1, strKeyItem.length() - pos1);

	int iBegin = 0;

	for(int j=0; j<=n; j++)
	{
		iBegin = strStd.find(strItems[j], nStartPos+iBegin) + strItems[j].length();
		if(iBegin == -1 + strItems[j].length())
		{
			return -1;
		}
	}
	//////////
	//如果在行结束"\r\n"之前没有找到strEndKey, 则以"\r\n"结束
	int iEndPos1 = strStd.find(strEndKey, iBegin);
	
	int iEndPos2 = strStd.find("\r\n", iBegin);
	
	int iEndPos;
	
	if(iEndPos1 != -1 && iEndPos2 != -1)
	{
		iEndPos = min(iEndPos1, iEndPos2);
	}
	else if(iEndPos1 == -1)
	{
		iEndPos = iEndPos2;
	}
	else if(iEndPos2 == -1)
	{
		iEndPos = iEndPos1;
	}
	else
	{
		return -1;
	}
	//////////

	strSrc.replace(iBegin, iEndPos - iBegin, strNewValue);

	return 0;
}

//移除操作. (关键字不分大小写)
int CStrOperate::RemoveSubString(string &strSrc, string strKeyItem, string strEndKey)
{
	//////////
	string strStd = strSrc;
	int i;
	for(i=0; i<strSrc.length(); i++)
	{
		strStd.at(i) = tolower(strStd.at(i));  //为了使对大小写不敏感
	}
	
	for(i=0; i<strKeyItem.length(); i++)
	{
		strKeyItem.at(i) = tolower(strKeyItem.at(i));
	}
	
	for(i=0; i<strEndKey.length(); i++)
	{
		strEndKey.at(i) = tolower(strEndKey.at(i));
	}

	///////////
	
	string strItems[10];
	int pos1 = 0;
	int pos2 = strKeyItem.find("\\", pos1);
	int n;
	for(n=0; pos2 != -1; n++)
	{
		strItems[n] = strKeyItem.substr(pos1, pos2-pos1);
		pos1 = pos2 + 1;
		pos2 = strKeyItem.find("\\", pos1);
	}
	strItems[n] = strKeyItem.substr(pos1, strKeyItem.length() - pos1);
	
	int iBegin = 0;
	
	for(int j=0; j<=n; j++)
	{
		iBegin = strStd.find(strItems[j], iBegin);
		if(iBegin == -1)
		{
			return -1;
		}
	}
	//////////
	int iEndPos = strStd.find(strEndKey, iBegin);
	if(iEndPos == -1)
		return -1;
	//////////
	
	strSrc.erase(iBegin, iEndPos - iBegin);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -