From 889c0fbe8e96c2a76ab8cae9884b90241e351bd2 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 17:25:46 +0100 Subject: [PATCH] Split file StringFunctions.cpp into two files StringFunctions.h and StringFunctions.cpp. --- src/CBot/CBotInstr/CBotExprNum.cpp | 98 --------------------- src/CBot/CBotProgram.cpp | 4 +- src/CBot/CBotUtils.cpp | 94 ++++++++++++++++++++ src/CBot/CBotUtils.h | 15 ++++ src/CBot/CMakeLists.txt | 1 + src/CBot/StringFunctions.cpp | 70 +++++---------- src/CBot/StringFunctions.h | 137 +++++++++++++++++++++++++++++ 7 files changed, 273 insertions(+), 146 deletions(-) create mode 100644 src/CBot/StringFunctions.h diff --git a/src/CBot/CBotInstr/CBotExprNum.cpp b/src/CBot/CBotInstr/CBotExprNum.cpp index 65dbd780..acf1e9a2 100644 --- a/src/CBot/CBotInstr/CBotExprNum.cpp +++ b/src/CBot/CBotInstr/CBotExprNum.cpp @@ -29,104 +29,6 @@ // Global include - -//////////////////////////////////////////////////////////////////////////////// -// converts a string into integer -// may be of the form 0xabc123 -long GetNumInt(const char* p) -{ - long num = 0; - while (*p >= '0' && *p <= '9') - { - num = num * 10 + *p - '0'; - p++; - } - if (*p == 'x' || *p == 'X') - { - while (*++p != 0) - { - if (*p >= '0' && *p <= '9') - { - num = num * 16 + *p - '0'; - continue; - } - if (*p >= 'A' && *p <= 'F') - { - num = num * 16 + *p - 'A' + 10; - continue; - } - if (*p >= 'a' && *p <= 'f') - { - num = num * 16 + *p - 'a' + 10; - continue; - } - break; - } - } - return num; -} - -//////////////////////////////////////////////////////////////////////////////// -// converts a string into a float number -extern float GetNumFloat(const char* p) -{ - double num = 0; - double div = 10; - bool bNeg = false; - - if (*p == '-') - { - bNeg = true; - p++; - } - while (*p >= '0' && *p <= '9') - { - num = num * 10. + (*p - '0'); - p++; - } - - if (*p == '.') - { - p++; - while (*p >= '0' && *p <= '9') - { - num = num + (*p - '0') / div; - div = div * 10; - p++; - } - } - - int exp = 0; - if (*p == 'e' || *p == 'E') - { - char neg = 0; - p++; - if (*p == '-' || *p == '+') neg = *p++; - - while (*p >= '0' && *p <= '9') - { - exp = exp * 10 + (*p - '0'); - p++; - } - if (neg == '-') exp = -exp; - } - - while (exp > 0) - { - num *= 10.0; - exp--; - } - - while (exp < 0) - { - num /= 10.0; - exp++; - } - - if (bNeg) num = -num; - return static_cast(num); -} - //////////////////////////////////////////////////////////////////////////////// CBotExprNum::CBotExprNum() { diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 4e3f2c49..4ee5bffb 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotVar/CBotVar.h" + #include "CBotCall.h" #include "CBotStack.h" #include "CBotCStack.h" @@ -28,7 +30,7 @@ #include "CBotInstr/CBotFunction.h" -#include "StringFunctions.cpp" +#include "StringFunctions.h" // Local include diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index c59f8745..5e38cb5f 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -178,3 +178,97 @@ void DestructElements(CBotString* pOldData, int nCount) pOldData++; } } + +//////////////////////////////////////////////////////////////////////////////// +long GetNumInt(const char* p) +{ + long num = 0; + while (*p >= '0' && *p <= '9') + { + num = num * 10 + *p - '0'; + p++; + } + if (*p == 'x' || *p == 'X') + { + while (*++p != 0) + { + if (*p >= '0' && *p <= '9') + { + num = num * 16 + *p - '0'; + continue; + } + if (*p >= 'A' && *p <= 'F') + { + num = num * 16 + *p - 'A' + 10; + continue; + } + if (*p >= 'a' && *p <= 'f') + { + num = num * 16 + *p - 'a' + 10; + continue; + } + break; + } + } + return num; +} + +//////////////////////////////////////////////////////////////////////////////// +float GetNumFloat(const char* p) +{ + double num = 0; + double div = 10; + bool bNeg = false; + + if (*p == '-') + { + bNeg = true; + p++; + } + while (*p >= '0' && *p <= '9') + { + num = num * 10. + (*p - '0'); + p++; + } + + if (*p == '.') + { + p++; + while (*p >= '0' && *p <= '9') + { + num = num + (*p - '0') / div; + div = div * 10; + p++; + } + } + + int exp = 0; + if (*p == 'e' || *p == 'E') + { + char neg = 0; + p++; + if (*p == '-' || *p == '+') neg = *p++; + + while (*p >= '0' && *p <= '9') + { + exp = exp * 10 + (*p - '0'); + p++; + } + if (neg == '-') exp = -exp; + } + + while (exp > 0) + { + num *= 10.0; + exp--; + } + + while (exp < 0) + { + num /= 10.0; + exp++; + } + + if (bNeg) num = -num; + return static_cast(num); +} diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index bf958bc8..0ce76414 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -116,3 +116,18 @@ void ConstructElements(CBotString* pNewData, int nCount); * \param nCount */ void DestructElements(CBotString* pOldData, int nCount); + +/*! + * \brief GetNumInt Converts a string into integer may be of the form 0xabc123. + * \param p + * \return + */ +long GetNumInt(const char* p); + +/*! + * \brief GetNumFloat Converts a string into a float number. + * \param p + * \return + */ +float GetNumFloat(const char* p); + diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 3bb1eb91..a2d530e1 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES CBotCallMethode.cpp CBotStringArray.cpp CBotTypResult.cpp + StringFunctions.cpp CBotInstr/CBotInstr.cpp CBotInstr/CBotInstrUtils.cpp CBotInstr/CBotWhile.cpp diff --git a/src/CBot/StringFunctions.cpp b/src/CBot/StringFunctions.cpp index 640efc19..c38af371 100644 --- a/src/CBot/StringFunctions.cpp +++ b/src/CBot/StringFunctions.cpp @@ -17,15 +17,20 @@ * along with this program. If not, see http://gnu.org/licenses */ +// Modules inlcude +#include "StringFunctions.h" + +#include "CBotProgram.h" +#include "CBotEnums.h" + #include "CBotVar/CBotVar.h" -// definition of string functions +// Local include -// gives the length of a chain -// execution - +// Global include +//////////////////////////////////////////////////////////////////////////////// bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -45,9 +50,7 @@ bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// int xxx ( string ) -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -64,10 +67,7 @@ CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ) 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 @@ -100,9 +100,7 @@ bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// string xxx ( string, int ) -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -127,9 +125,7 @@ CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ) return CBotTypResult( CBotTypString ); } -// gives the right of a string -// execution - +//////////////////////////////////////////////////////////////////////////////// bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -162,9 +158,7 @@ bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// gives the central part of a chain -// execution - +//////////////////////////////////////////////////////////////////////////////// bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -214,9 +208,7 @@ bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// gives the central part of a chain -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -251,10 +243,7 @@ CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser ) 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 @@ -276,9 +265,7 @@ bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// float xxx ( string ) -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -295,10 +282,7 @@ CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser ) return CBotTypResult( CBotTypFloat ); } - -// find string in other -// exécution - +//////////////////////////////////////////////////////////////////////////////// bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -330,9 +314,7 @@ bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// int xxx ( string, string ) -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -357,9 +339,7 @@ CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ) return CBotTypResult( CBotTypInt ); } -// gives a string to uppercase -// exécution - +//////////////////////////////////////////////////////////////////////////////// bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -382,9 +362,7 @@ bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// gives a string to lowercase -// exécution - +//////////////////////////////////////////////////////////////////////////////// bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -407,9 +385,7 @@ bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// string xxx ( string ) -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cStrStr( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -426,7 +402,7 @@ CBotTypResult cStrStr( CBotVar* &pVar, void* pUser ) return CBotTypResult( CBotTypString ); } - +//////////////////////////////////////////////////////////////////////////////// void InitStringFunctions() { CBotProgram::AddFunction("strlen", rStrLen, cIntStr ); diff --git a/src/CBot/StringFunctions.h b/src/CBot/StringFunctions.h new file mode 100644 index 00000000..da534aad --- /dev/null +++ b/src/CBot/StringFunctions.h @@ -0,0 +1,137 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.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 + */ + +#pragma once + +// Modules inlcude +#include "CBotTypResult.h" + +// Local include + +// Global include + +// Forward declaration +class CBotVar; + +/*! + * \brief rStrLen Gives the length of a chain execution + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief cIntStr int xxx ( string ) compilation + * \param pVar + * \param pUser + * \return + */ +CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ); + +/*! + * \brief rStrLeft Gives the left side of a chain execution + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief cStrStrInt string xxx ( string, int ) compilation + * \param pVar + * \param pUser + * \return + */ +CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ); + +/*! + * \brief rStrRight Gives the right of a string execution + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief rStrMid Gives the central part of a chain execution + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief rStrVal Gives the number stored in a string execution. + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief cIntStrStr int xxx ( string, string ) compilation + * \param pVar + * \param pUser + * \return + */ +CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ); + +/*! + * \brief rStrUpper Gives a string to uppercase exécution + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief rStrLower Gives a string to lowercase exécution. + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief cStrStr String xxx ( string ) compilation + * \param pVar + * \param pUser + * \return + */ +CBotTypResult cStrStr( CBotVar* &pVar, void* pUser ); + +/*! + * \brief InitStringFunctions + */ +void InitStringFunctions();