diff options
Diffstat (limited to 'src/CBot/StringFunctions.cpp')
-rw-r--r-- | src/CBot/StringFunctions.cpp | 870 |
1 files changed, 436 insertions, 434 deletions
diff --git a/src/CBot/StringFunctions.cpp b/src/CBot/StringFunctions.cpp index 27332db..213b956 100644 --- a/src/CBot/StringFunctions.cpp +++ b/src/CBot/StringFunctions.cpp @@ -1,434 +1,436 @@ -// * This file is part of the COLOBOT source code
-// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
-// *
-// * 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://www.gnu.org/licenses/.// définition des fonctions sur les chaînes
-
-
-// donne la longueur d'une chaîne
-// exécution
-
-bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
-
- // pas de second paramètre
- if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; }
-
- // recupére le contenu de la string
- CBotString s = pVar->GivValString();
-
- // met la longueur sur la pile
- pResult->SetValInt( s.GivLength() );
- return true;
-}
-
-// int xxx ( string )
-// compilation
-
-CBotTypResult cIntStr( CBotVar* &pVar, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString )
- return CBotTypResult( TX_BADPARAM );
-
- // pas de second paramètre
- if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
-
- // le résultat final est un nombre entier
- return CBotTypResult( CBotTypInt );
-}
-
-
-// donne la partie gauche d'une chaîne
-// exécution
-
-bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
-
- // recupére le contenu de la string
- CBotString s = pVar->GivValString();
-
- // il faut un second paramètre
- pVar = pVar->GivNext();
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être un nombre
- if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; }
-
- // récupère ce nombre
- int n = pVar->GivValInt();
-
- // pas de 3e paramètre
- if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; }
-
- // prend la partie intéressante
- s = s.Left( n );
-
- // la met sur la pile
- pResult->SetValString( s );
- return true;
-}
-
-// string xxx ( string, int )
-// compilation
-
-CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString )
- return CBotTypResult( TX_BADSTRING );
-
- // il faut un second paramètre
- pVar = pVar->GivNext();
- if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
-
- // qui doit être un nombre
- if ( pVar->GivType() > CBotTypDouble )
- return CBotTypResult( TX_BADNUM );
-
- // pas de 3e paramètre
- if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
-
- // le résultat final est une string
- return CBotTypResult( CBotTypString );
-}
-
-// donne la partie droite d'une chaîne
-// exécution
-
-bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
-
- // recupére le contenu de la string
- CBotString s = pVar->GivValString();
-
- // il faut un second paramètre
- pVar = pVar->GivNext();
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être un nombre
- if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; }
-
- // récupère ce nombre
- int n = pVar->GivValInt();
-
- // pas de 3e paramètre
- if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; }
-
- // prend la partie intéressante
- s = s.Right( n );
-
- // la met sur la pile
- pResult->SetValString( s );
- return true;
-}
-
-// donne la partie centrale d'une chaîne
-// exécution
-
-bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
-
- // recupére le contenu de la string
- CBotString s = pVar->GivValString();
-
- // il faut un second paramètre
- pVar = pVar->GivNext();
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être un nombre
- if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; }
-
- // récupère ce nombre
- int n = pVar->GivValInt();
-
- // 3e paramètre optionnel
- if ( pVar->GivNext() != NULL )
- {
- pVar = pVar->GivNext();
-
- // qui doit être un nombre
- if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; }
-
- // récupère ce nombre
- int l = pVar->GivValInt();
-
- // mais pas de 4e paramètre
- if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; }
-
- // prend la partie intéressante
- s = s.Mid( n, l );
- }
- else
- {
- // prend la partie intéressante
- s = s.Mid( n );
- }
-
- // la met sur la pile
- pResult->SetValString( s );
- return true;
-}
-
-// donne la partie centrale d'une chaîne
-// compilation
-
-CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString )
- return CBotTypResult( TX_BADSTRING );
-
- // il faut un second paramètre
- pVar = pVar->GivNext();
- if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
-
- // qui doit être un nombre
- if ( pVar->GivType() > CBotTypDouble )
- return CBotTypResult( TX_BADNUM );
-
- // 3e paramètre optionnel
- if ( pVar->GivNext() != NULL )
- {
-
- pVar = pVar->GivNext();
- // qui doit être un nombre
- if ( pVar->GivType() > CBotTypDouble )
- return CBotTypResult( TX_BADNUM );
-
- // pas de 4e paramètre
- if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
- }
-
- // le résultat final est une string
- return CBotTypResult( CBotTypString );
-}
-
-
-// donne le nombre contenu dans une chaîne
-// exécution
-
-bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
-
- // recupére le contenu de la string
- CBotString s = pVar->GivValString();
-
- // mais pas de 2e paramètre
- if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; }
-
- float val = GivNumFloat(s);
-
- // la met la valeur sur la pile
- pResult->SetValFloat( val );
- return true;
-}
-
-// float xxx ( string )
-// compilation
-
-CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString )
- return CBotTypResult( TX_BADSTRING );
-
- // pas de 2e paramètre
- if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
-
- // le résultat final est un nombre
- return CBotTypResult( CBotTypFloat );
-}
-
-
-// trouve une chaine dans une autre
-// exécution
-
-bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
-
- // recupére le contenu de la string
- CBotString s = pVar->GivValString();
-
- // il faut un second paramètre
- pVar = pVar->GivNext();
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
-
- // récupère ce nombre
- CBotString s2 = pVar->GivValString();
-
- // pas de 3e paramètre
- if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; }
-
- // met le résultat sur la pile
- int res = s.Find(s2);
- pResult->SetValInt( res );
- if ( res < 0 ) pResult->SetInit( IS_NAN );
- return true;
-}
-
-// int xxx ( string, string )
-// compilation
-
-CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString )
- return CBotTypResult( TX_BADSTRING );
-
- // il faut un second paramètre
- pVar = pVar->GivNext();
- if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString )
- return CBotTypResult( TX_BADSTRING );
-
- // pas de 3e paramètre
- if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
-
- // le résultat final est un nombre
- return CBotTypResult( CBotTypInt );
-}
-
-// donne une chaine en majuscule
-// exécution
-
-bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
-
- // recupére le contenu de la string
- CBotString s = pVar->GivValString();
-
- // mais pas de 2e paramètre
- if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; }
-
-
- s.MakeUpper();
-
- // la met la valeur sur la pile
- pResult->SetValString( s );
- return true;
-}
-
-// donne une chaine en minuscules
-// exécution
-
-bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; }
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; }
-
- // recupére le contenu de la string
- CBotString s = pVar->GivValString();
-
- // mais pas de 2e paramètre
- if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; }
-
-
- s.MakeLower();
-
- // la met la valeur sur la pile
- pResult->SetValString( s );
- return true;
-}
-
-// string xxx ( string )
-// compilation
-
-CBotTypResult cStrStr( CBotVar* &pVar, void* pUser )
-{
- // il faut un paramètre
- if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM );
-
- // qui doit être une string
- if ( pVar->GivType() != CBotTypString )
- return CBotTypResult( TX_BADSTRING );
-
- // pas de 2e paramètre
- if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM );
-
- // le résultat final est une string
- return CBotTypResult( CBotTypString );
-}
-
-
-void InitStringFunctions()
-{
- CBotProgram::AddFunction("strlen", rStrLen, cIntStr );
- CBotProgram::AddFunction("strleft", rStrLeft, cStrStrInt );
- CBotProgram::AddFunction("strright", rStrRight, cStrStrInt );
- CBotProgram::AddFunction("strmid", rStrMid, cStrStrIntInt );
-
- CBotProgram::AddFunction("strval", rStrVal, cFloatStr );
- CBotProgram::AddFunction("strfind", rStrFind, cIntStrStr );
-
- CBotProgram::AddFunction("strupper", rStrUpper, cStrStr );
- CBotProgram::AddFunction("strlower", rStrLower, cStrStr );
-}
+// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * 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://www.gnu.org/licenses/. + +// definition of string functions + + +// gives the length of a chain +// execution + +bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // no second parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // puts the length of the stack + pResult->SetValInt( s.GivLength() ); + return true; +} + +// int xxx ( string ) +// compilation + +CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADPARAM ); + + // no second parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // the end result is an integer + return CBotTypResult( CBotTypInt ); +} + + +// gives the left side of a chain +// execution + +bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + + // retrieves this number + int n = pVar->GivValInt(); + + // no third parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + + // takes the interesting part + s = s.Left( n ); + + // puts on the stack + pResult->SetValString( s ); + return true; +} + +// string xxx ( string, int ) +// compilation + +CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) + return CBotTypResult( TX_BADNUM ); + + // no third parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // the end result is a string + return CBotTypResult( CBotTypString ); +} + +// gives the right of a string +// execution + +bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + + // retrieves this number + int n = pVar->GivValInt(); + + // no third parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + + // takes the interesting part + s = s.Right( n ); + + // puts on the stack + pResult->SetValString( s ); + return true; +} + +// gives the central part of a chain +// execution + +bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + + // retrieves this number + int n = pVar->GivValInt(); + + // third parameter optional + if ( pVar->GivNext() != NULL ) + { + pVar = pVar->GivNext(); + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + + // retrieves this number + int l = pVar->GivValInt(); + + // but no fourth parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + + // 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; +} + +// gives the central part of a chain +// compilation + +CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) + return CBotTypResult( TX_BADNUM ); + + // third parameter optional + if ( pVar->GivNext() != NULL ) + { + + pVar = pVar->GivNext(); + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) + return CBotTypResult( TX_BADNUM ); + + // no fourth parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + } + + // the end result is a string + return CBotTypResult( CBotTypString ); +} + + +// gives the number stored in a string +// execution + +bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // but no second parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + + float val = GivNumFloat(s); + + // puts the value on the stack + pResult->SetValFloat( val ); + return true; +} + +// float xxx ( string ) +// compilation + +CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // no second parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // the end result is a number + return CBotTypResult( CBotTypFloat ); +} + + +// find string in other +// exécution + +bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // retrieves this number + CBotString s2 = pVar->GivValString(); + + // no third parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + + // puts the result on the stack + int res = s.Find(s2); + pResult->SetValInt( res ); + if ( res < 0 ) pResult->SetInit( IS_NAN ); + return true; +} + +// int xxx ( string, string ) +// compilation + +CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // no third parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // the end result is a number + return CBotTypResult( CBotTypInt ); +} + +// gives a string to uppercase +// exécution + +bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // but no second parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + + + s.MakeUpper(); + + // puts the value on the stack + pResult->SetValString( s ); + return true; +} + +// gives a string to lowercase +// exécution + +bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // but no second parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + + + s.MakeLower(); + + // puts the value on the stack + pResult->SetValString( s ); + return true; +} + +// string xxx ( string ) +// compilation + +CBotTypResult cStrStr( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // no second parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // the end result is a string + return CBotTypResult( CBotTypString ); +} + + +void InitStringFunctions() +{ + CBotProgram::AddFunction("strlen", rStrLen, cIntStr ); + CBotProgram::AddFunction("strleft", rStrLeft, cStrStrInt ); + CBotProgram::AddFunction("strright", rStrRight, cStrStrInt ); + CBotProgram::AddFunction("strmid", rStrMid, cStrStrIntInt ); + + CBotProgram::AddFunction("strval", rStrVal, cFloatStr ); + CBotProgram::AddFunction("strfind", rStrFind, cIntStrStr ); + + CBotProgram::AddFunction("strupper", rStrUpper, cStrStr ); + CBotProgram::AddFunction("strlower", rStrLower, cStrStr ); +} |