Split file StringFunctions.cpp into two files StringFunctions.h and StringFunctions.cpp.

dev-time-step
Grunaka 2015-11-22 17:25:46 +01:00
parent 1a6b5ded64
commit 889c0fbe8e
7 changed files with 273 additions and 146 deletions

View File

@ -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<float>(num);
}
////////////////////////////////////////////////////////////////////////////////
CBotExprNum::CBotExprNum()
{

View File

@ -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

View File

@ -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<float>(num);
}

View File

@ -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);

View File

@ -13,6 +13,7 @@ set(SOURCES
CBotCallMethode.cpp
CBotStringArray.cpp
CBotTypResult.cpp
StringFunctions.cpp
CBotInstr/CBotInstr.cpp
CBotInstr/CBotInstrUtils.cpp
CBotInstr/CBotWhile.cpp

View File

@ -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 );

137
src/CBot/StringFunctions.h Normal file
View File

@ -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();