colobot/src/CBot/StringFunctions.cpp

441 lines
12 KiB
C++
Raw Normal View History

/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
2012-08-08 00:01:06 +00:00
// definition of string functions
// gives the length of a chain
// execution
bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// no second parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ) { ex = TX_OVERPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// get the contents of the string
2012-08-11 18:59:35 +00:00
CBotString s = pVar->GetValString();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// puts the length of the stack
2012-08-11 18:59:35 +00:00
pResult->SetValInt( s.GetLength() );
2012-08-08 20:35:17 +00:00
return true;
2012-08-08 00:01:06 +00:00
}
// int xxx ( string )
// compilation
CBotTypResult cIntStr( CBotVar* &pVar, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString )
2012-08-08 20:35:17 +00:00
return CBotTypResult( TX_BADPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// no second parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// the end result is an integer
return CBotTypResult( CBotTypInt );
2012-08-08 00:01:06 +00:00
}
// gives the left side of a chain
// execution
bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// get the contents of the string
2012-08-11 18:59:35 +00:00
CBotString s = pVar->GetValString();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// it takes a second parameter
2012-08-11 18:59:35 +00:00
pVar = pVar->GetNext();
2012-08-08 20:35:17 +00:00
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// which must be a number
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// retrieves this number
2012-08-11 18:59:35 +00:00
int n = pVar->GetValInt();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// no third parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ) { ex = TX_OVERPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// takes the interesting part
s = s.Left( n );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// puts on the stack
pResult->SetValString( s );
return true;
2012-08-08 00:01:06 +00:00
}
// string xxx ( string, int )
// compilation
CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString )
2012-08-08 20:35:17 +00:00
return CBotTypResult( TX_BADSTRING );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// it takes a second parameter
2012-08-11 18:59:35 +00:00
pVar = pVar->GetNext();
2012-08-08 20:35:17 +00:00
if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// which must be a number
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() > CBotTypDouble )
2012-08-08 20:35:17 +00:00
return CBotTypResult( TX_BADNUM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// no third parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// the end result is a string
return CBotTypResult( CBotTypString );
2012-08-08 00:01:06 +00:00
}
// gives the right of a string
// execution
bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// get the contents of the string
2012-08-11 18:59:35 +00:00
CBotString s = pVar->GetValString();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// it takes a second parameter
2012-08-11 18:59:35 +00:00
pVar = pVar->GetNext();
2012-08-08 20:35:17 +00:00
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// which must be a number
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// retrieves this number
2012-08-11 18:59:35 +00:00
int n = pVar->GetValInt();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// no third parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ) { ex = TX_OVERPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// takes the interesting part
s = s.Right( n );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// puts on the stack
pResult->SetValString( s );
return true;
2012-08-08 00:01:06 +00:00
}
// gives the central part of a chain
// execution
bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
2012-08-08 20:35:17 +00:00
// get the contents of the string
2012-08-11 18:59:35 +00:00
CBotString s = pVar->GetValString();
2012-08-08 20:35:17 +00:00
// it takes a second parameter
2012-08-11 18:59:35 +00:00
pVar = pVar->GetNext();
2012-08-08 20:35:17 +00:00
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
// which must be a number
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; }
2012-08-08 20:35:17 +00:00
// retrieves this number
2012-08-11 18:59:35 +00:00
int n = pVar->GetValInt();
2012-08-08 20:35:17 +00:00
// third parameter optional
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL )
2012-08-08 20:35:17 +00:00
{
2012-08-11 18:59:35 +00:00
pVar = pVar->GetNext();
2012-08-08 20:35:17 +00:00
// which must be a number
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; }
2012-08-08 20:35:17 +00:00
// retrieves this number
2012-08-11 18:59:35 +00:00
int l = pVar->GetValInt();
2012-08-08 20:35:17 +00:00
// but no fourth parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ){ ex = TX_OVERPARAM ; return true; }
2012-08-08 20:35:17 +00:00
// takes the interesting part
s = s.Mid( n, l );
}
else
{
// takes the interesting part
s = s.Mid( n );
}
// puts on the stack
pResult->SetValString( s );
return true;
2012-08-08 00:01:06 +00:00
}
// gives the central part of a chain
// compilation
CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString )
2012-08-08 20:35:17 +00:00
return CBotTypResult( TX_BADSTRING );
// it takes a second parameter
2012-08-11 18:59:35 +00:00
pVar = pVar->GetNext();
2012-08-08 20:35:17 +00:00
if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
// which must be a number
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() > CBotTypDouble )
2012-08-08 20:35:17 +00:00
return CBotTypResult( TX_BADNUM );
// third parameter optional
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL )
2012-08-08 20:35:17 +00:00
{
2012-08-11 18:59:35 +00:00
pVar = pVar->GetNext();
2012-08-08 20:35:17 +00:00
// which must be a number
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() > CBotTypDouble )
2012-08-08 20:35:17 +00:00
return CBotTypResult( TX_BADNUM );
// no fourth parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
2012-08-08 20:35:17 +00:00
}
// the end result is a string
return CBotTypResult( CBotTypString );
2012-08-08 00:01:06 +00:00
}
// gives the number stored in a string
// execution
bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// get the contents of the string
2012-08-11 18:59:35 +00:00
CBotString s = pVar->GetValString();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// but no second parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ){ ex = TX_OVERPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-11 18:59:35 +00:00
float val = GetNumFloat(s);
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// puts the value on the stack
pResult->SetValFloat( val );
return true;
2012-08-08 00:01:06 +00:00
}
// float xxx ( string )
// compilation
CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString )
2012-08-08 20:35:17 +00:00
return CBotTypResult( TX_BADSTRING );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// no second parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// the end result is a number
return CBotTypResult( CBotTypFloat );
2012-08-08 00:01:06 +00:00
}
// find string in other
// exécution
bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// get the contents of the string
2012-08-11 18:59:35 +00:00
CBotString s = pVar->GetValString();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// it takes a second parameter
2012-08-11 18:59:35 +00:00
pVar = pVar->GetNext();
2012-08-08 20:35:17 +00:00
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// retrieves this number
2012-08-11 18:59:35 +00:00
CBotString s2 = pVar->GetValString();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// no third parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ) { ex = TX_OVERPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// puts the result on the stack
int res = s.Find(s2);
pResult->SetValInt( res );
if ( res < 0 ) pResult->SetInit( CBotVar::InitType::IS_NAN );
2012-08-08 20:35:17 +00:00
return true;
2012-08-08 00:01:06 +00:00
}
// int xxx ( string, string )
// compilation
CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString )
2012-08-08 20:35:17 +00:00
return CBotTypResult( TX_BADSTRING );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// it takes a second parameter
2012-08-11 18:59:35 +00:00
pVar = pVar->GetNext();
2012-08-08 20:35:17 +00:00
if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString )
2012-08-08 20:35:17 +00:00
return CBotTypResult( TX_BADSTRING );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// no third parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// the end result is a number
return CBotTypResult( CBotTypInt );
2012-08-08 00:01:06 +00:00
}
// gives a string to uppercase
// exécution
bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// get the contents of the string
2012-08-11 18:59:35 +00:00
CBotString s = pVar->GetValString();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// but no second parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ){ ex = TX_OVERPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
s.MakeUpper();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// puts the value on the stack
pResult->SetValString( s );
return true;
2012-08-08 00:01:06 +00:00
}
// gives a string to lowercase
// exécution
bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// get the contents of the string
2012-08-11 18:59:35 +00:00
CBotString s = pVar->GetValString();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// but no second parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ){ ex = TX_OVERPARAM ; return true; }
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
s.MakeLower();
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// puts the value on the stack
pResult->SetValString( s );
return true;
2012-08-08 00:01:06 +00:00
}
// string xxx ( string )
// compilation
CBotTypResult cStrStr( CBotVar* &pVar, void* pUser )
{
2012-08-08 20:35:17 +00:00
// it takes a parameter
if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// to be a string
2012-08-11 18:59:35 +00:00
if ( pVar->GetType() != CBotTypString )
2012-08-08 20:35:17 +00:00
return CBotTypResult( TX_BADSTRING );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// no second parameter
2012-08-11 18:59:35 +00:00
if ( pVar->GetNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
// the end result is a string
return CBotTypResult( CBotTypString );
2012-08-08 00:01:06 +00:00
}
void InitStringFunctions()
{
2012-08-08 20:35:17 +00:00
CBotProgram::AddFunction("strlen", rStrLen, cIntStr );
CBotProgram::AddFunction("strleft", rStrLeft, cStrStrInt );
CBotProgram::AddFunction("strright", rStrRight, cStrStrInt );
CBotProgram::AddFunction("strmid", rStrMid, cStrStrIntInt );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
CBotProgram::AddFunction("strval", rStrVal, cFloatStr );
CBotProgram::AddFunction("strfind", rStrFind, cIntStrStr );
2012-08-08 00:01:06 +00:00
2012-08-08 20:35:17 +00:00
CBotProgram::AddFunction("strupper", rStrUpper, cStrStr );
CBotProgram::AddFunction("strlower", rStrLower, cStrStr );
2012-08-08 00:01:06 +00:00
}