/*********************************************************************
Copyright 2001 Alexander Berthold, alexander-berthold@web.de.
-- This file is part of ctkCommon --
"ctkCommon" is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or any later version.
"ctkCommon" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with "ctkCommon"; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
---------------------------------------------------------------
If you find any bugs or if you make other corrections/
enhancements, i'd appreciate if you'd let me know about
that. My email is
alexander-berthold@web.de
If you share this code, do not remove this text.
---------------------------------------------------------------
Class: ctkMisc
Author: Alexander Berthold
Copyright: Alexander Berthold
Date: 2001/06/05
Version: 0.1.01
Purpose: Provides some helper methods used by cxTokenizer
and cxAnalyzer.
Version history:
- 2001/06/05
Current source labeled version 0.1.01
*********************************************************************/
// ctkMisc.h: interface for the ctkMisc class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CTKMISC_H__02DB3E9E_00AB_4FC7_9F58_E01EECC31A91__INCLUDED_)
#define AFX_CTKMISC_H__02DB3E9E_00AB_4FC7_9F58_E01EECC31A91__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "tkCommon.h"
class ctkMisc
{
// Purely static helper class
public:
static bool fStripOffBraces(std::tstring& strTarget)
{
int nLen = strTarget.length();
if(nLen<2)
return false;
if(nLen==2)
return false;
if(strTarget[0]!='{' || strTarget[nLen-1]!='}')
return false;
strTarget =strTarget.substr(1,strTarget.length()-2);
return true;
}
static std::tstring strParseEscapeCharacters(const std::tstring& strSource)
{
std::tstring strDest;
int nLen = strSource.length();
int i;
bool fEscapeNext = false;
for(i=0;i<nLen;i++)
{
TCHAR tcTemp = strSource[i];
if(fEscapeNext)
{
bool fOk = false;
if(tcTemp==_T('n'))
fOk = true, strDest+="\n";
if(tcTemp==_T('r'))
fOk = true, strDest+="\r";
if(tcTemp==_T('\\'))
fOk = true, strDest+="\\";
if(tcTemp==_T('x') && ((i+2)<nLen))
{
TCHAR szHex[3];
szHex[0] =strSource[i+1];
szHex[1] =strSource[i+2];
szHex[2] ='\0';
strDest+=(TCHAR)(_tcstoul(szHex,NULL,16));
fOk = true;
}
if(!fOk)
strDest+=tcTemp;
fEscapeNext = false;
continue;
}
if(tcTemp=='\\')
{
fEscapeNext = true;
continue;
}
strDest+=tcTemp;
}
return strDest;
}
static std::tstring strEscapeBraces(const std::tstring& strSource)
{
std::tstring strDest;
int nLen = strSource.length();
int i;
TCHAR tcBefore,tcTemp;
bool fSkip;
if(nLen==0)
return strSource;
tcBefore =_T('\0');
for(i=0;i<nLen;i++)
{
fSkip =false;
tcTemp =strSource[i];
if(tcBefore!=_T('\\') && tcTemp==_T('{'))
strDest+=_T("{{"), fSkip=true;
if(tcBefore!=_T('\\') && tcTemp==_T('}'))
strDest+=_T("}}"), fSkip=true;
if(!fSkip)
strDest+=tcTemp;
tcBefore =tcTemp;
}
return strDest;
}
static std::tstring strUnescapeBraces(const std::tstring& strSource)
{
std::tstring strDest;
int nLen = strSource.length();
int i;
TCHAR tcTemp;
bool fSkip;
if(nLen==0)
return strSource;
for(i=0;i<nLen-1;i++)
{
fSkip =false;
tcTemp =strSource[i];
if(tcTemp=='\\' && strSource[i+1]=='{')
{
strDest +=_T("{");
fSkip=true;
i++;
}
if(tcTemp=='\\' && strSource[i+1]=='}')
{
strDest +=_T("}");
fSkip=true;
i++;
}
if(!fSkip)
strDest +=tcTemp;
}
if(i<nLen)
strDest+=strSource[i];
return strDest;
}
};
#endif // !defined(AFX_CTKMISC_H__02DB3E9E_00AB_4FC7_9F58_E01EECC31A91__INCLUDED_)