Refactor CBotString::LoadString

dev-time-step
krzys-h 2015-12-20 15:06:35 +01:00
parent 3eeab0f9b7
commit 87a34ba1ff
11 changed files with 208 additions and 226 deletions

View File

@ -0,0 +1,109 @@
#include "CBot/CBotKeywordStrings.h"
#include <map>
//! \brief Keeps the string corresponding to keyword ID
// Map is filled with id-string pars that are needed for CBot language parsing
static const std::map<EID, const char*> s_keywordString = {
{ID_IF, "if"},
{ID_ELSE, "else"},
{ID_WHILE, "while"},
{ID_DO, "do"},
{ID_FOR, "for"},
{ID_BREAK, "break"},
{ID_CONTINUE, "continue"},
{ID_SWITCH, "switch"},
{ID_CASE, "case"},
{ID_DEFAULT, "default"},
{ID_TRY, "try"},
{ID_THROW, "throw"},
{ID_CATCH, "catch"},
{ID_FINALLY, "finally"},
{ID_TXT_AND, "and"},
{ID_TXT_OR, "or"},
{ID_TXT_NOT, "not"},
{ID_RETURN, "return"},
{ID_CLASS, "class"},
{ID_EXTENDS, "extends"},
{ID_SYNCHO, "synchronized"},
{ID_NEW, "new"},
{ID_PUBLIC, "public"},
{ID_EXTERN, "extern"},
{ID_STATIC, "static"},
{ID_PROTECTED, "protected"},
{ID_PRIVATE, "private"},
{ID_INT, "int"},
{ID_FLOAT, "float"},
{ID_BOOLEAN, "boolean"},
{ID_STRING, "string"},
{ID_VOID, "void"},
{ID_BOOL, "bool"},
{ID_TRUE, "true"},
{ID_FALSE, "false"},
{ID_NULL, "null"},
{ID_NAN, "nan"},
{ID_OPENPAR, "("},
{ID_CLOSEPAR, ")"},
{ID_OPBLK, "{"},
{ID_CLBLK, "}"},
{ID_SEP, ";"},
{ID_COMMA, ","},
{ID_DOTS, ":"},
{ID_DOT, "."},
{ID_OPBRK, "["},
{ID_CLBRK, "]"},
{ID_DBLDOTS, "::"},
{ID_LOGIC, "?"},
{ID_ADD, "+"},
{ID_SUB, "-"},
{ID_MUL, "*"},
{ID_DIV, "/"},
{ID_ASS, "="},
{ID_ASSADD, "+="},
{ID_ASSSUB, "-="},
{ID_ASSMUL, "*="},
{ID_ASSDIV, "/="},
{ID_ASSOR, "|="},
{ID_ASSAND, "&="},
{ID_ASSXOR, "^="},
{ID_ASSSL, "<<="},
{ID_ASSSR, ">>>="},
{ID_ASSASR, ">>="},
{ID_SL, "<<"},
{ID_SR, ">>"},
{ID_ASR, ">>"},
{ID_INC, "++"},
{ID_DEC, "--"},
{ID_LO, "<"},
{ID_HI, ">"},
{ID_LS, "<="},
{ID_HS, ">="},
{ID_EQ, "=="},
{ID_NE, "!="},
{ID_AND, "&"},
{ID_XOR, "^"},
{ID_OR, "|"},
{ID_LOG_AND, "&&"},
{ID_LOG_OR, "||"},
{ID_LOG_NOT, "!"},
{ID_NOT, "~"},
{ID_MODULO, "%"},
{ID_POWER, "**"},
{ID_ASSMODULO, "%="},
{TX_UNDEF, "undefined"},
{TX_NAN, "not a number"}
};
static const char emptyString[] = "";
const char* LoadString(EID id)
{
if (s_keywordString.find(id) != s_keywordString.end())
{
return s_keywordString.at(id);
}
else
{
return emptyString;
}
}

View File

@ -0,0 +1,10 @@
#pragma once
#include "CBotEnums.h"
/**
* \brief LoadString Maps given ID to its string equivalent.
* \param id Provided identifier.
* \return String if found, else NullString.
*/
const char* LoadString(EID id);

View File

@ -30,6 +30,7 @@
#include "CBot/CBotInstr/CBotFunction.h"
#include "StringFunctions.h"
#include "CBotKeywordStrings.h"
// Local include
@ -357,9 +358,8 @@ bool CBotProgram::GetError(int& code, int& start, int& end, CBotProgram* &pProg)
////////////////////////////////////////////////////////////////////////////////
CBotString CBotProgram::GetErrorText(int code)
{
CBotString TextError;
CBotString TextError = LoadString(static_cast<EID>(code));
TextError.LoadString( code );
if (TextError.IsEmpty())
{
char buf[100];

View File

@ -21,104 +21,11 @@
#include "CBot/CBotString.h"
//strings management
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <boost/algorithm/string.hpp>
//Map is filled with id-string pars that are needed for CBot language parsing
const std::map<EID,const char *> CBotString::s_keywordString =
{
{ID_IF, "if"},
{ID_ELSE, "else"},
{ID_WHILE, "while"},
{ID_DO, "do"},
{ID_FOR, "for"},
{ID_BREAK, "break"},
{ID_CONTINUE, "continue"},
{ID_SWITCH, "switch"},
{ID_CASE, "case"},
{ID_DEFAULT, "default"},
{ID_TRY, "try"},
{ID_THROW, "throw"},
{ID_CATCH, "catch"},
{ID_FINALLY, "finally"},
{ID_TXT_AND, "and"},
{ID_TXT_OR, "or"},
{ID_TXT_NOT, "not"},
{ID_RETURN, "return"},
{ID_CLASS, "class"},
{ID_EXTENDS, "extends"},
{ID_SYNCHO, "synchronized"},
{ID_NEW, "new"},
{ID_PUBLIC, "public"},
{ID_EXTERN, "extern"},
{ID_STATIC, "static"},
{ID_PROTECTED, "protected"},
{ID_PRIVATE, "private"},
{ID_INT, "int"},
{ID_FLOAT, "float"},
{ID_BOOLEAN, "boolean"},
{ID_STRING, "string"},
{ID_VOID, "void"},
{ID_BOOL, "bool"},
{ID_TRUE, "true"},
{ID_FALSE, "false"},
{ID_NULL, "null"},
{ID_NAN, "nan"},
{ID_OPENPAR, "("},
{ID_CLOSEPAR, ")"},
{ID_OPBLK, "{"},
{ID_CLBLK, "}"},
{ID_SEP, ";"},
{ID_COMMA, ","},
{ID_DOTS, ":"},
{ID_DOT, "."},
{ID_OPBRK, "["},
{ID_CLBRK, "]"},
{ID_DBLDOTS, "::"},
{ID_LOGIC, "?"},
{ID_ADD, "+"},
{ID_SUB, "-"},
{ID_MUL, "*"},
{ID_DIV, "/"},
{ID_ASS, "="},
{ID_ASSADD, "+="},
{ID_ASSSUB, "-="},
{ID_ASSMUL, "*="},
{ID_ASSDIV, "/="},
{ID_ASSOR, "|="},
{ID_ASSAND, "&="},
{ID_ASSXOR, "^="},
{ID_ASSSL, "<<="},
{ID_ASSSR, ">>>="},
{ID_ASSASR, ">>="},
{ID_SL, "<<"},
{ID_SR, ">>"},
{ID_ASR, ">>"},
{ID_INC, "++"},
{ID_DEC, "--"},
{ID_LO, "<"},
{ID_HI, ">"},
{ID_LS, "<="},
{ID_HS, ">="},
{ID_EQ, "=="},
{ID_NE, "!="},
{ID_AND, "&"},
{ID_XOR, "^"},
{ID_OR, "|"},
{ID_LOG_AND, "&&"},
{ID_LOG_OR, "||"},
{ID_LOG_NOT, "!"},
{ID_NOT, "~"},
{ID_MODULO, "%"},
{ID_POWER, "**"},
{ID_ASSMODULO, "%="},
{TX_UNDEF, "undefined"},
{TX_NAN, "not a number"}
};
CBotString::CBotString()
{
m_str = "";
@ -202,12 +109,6 @@ void CBotString::MakeLower()
boost::to_lower(m_str);
}
bool CBotString::LoadString(unsigned int id)
{
m_str = MapIdToString(static_cast<EID>(id));
return !m_str.empty();
}
const CBotString& CBotString::operator=(const CBotString& stringSrc)
{
@ -333,15 +234,3 @@ const char* CBotString::CStr() const
if (this == nullptr) return emptyString; // TODO: can this be removed?
return m_str.c_str();
}
const char * CBotString::MapIdToString(EID id)
{
if (s_keywordString.find(id) != s_keywordString.end())
{
return s_keywordString.at(id);
}
else
{
return emptyString;
}
}

View File

@ -122,13 +122,6 @@ public:
*/
int ReverseFind(const char* lpsz);
/**
* \brief LoadString Load the string associate with the id.
* \param id The id to load.
* \return True if the id exist false otherwise.
*/
bool LoadString(unsigned int id);
/**
* \brief Mid Return a part of a string from a starting index and until
* the end of the string with a limited size.
@ -198,14 +191,4 @@ private:
//! \brief String
std::string m_str;
//! \brief Keeps the string corresponding to keyword ID
static const std::map<EID, const char *> s_keywordString;
/**
* \brief MapIdToString Maps given ID to its string equivalent.
* \param id Provided identifier.
* \return String if found, else NullString.
*/
static const char * MapIdToString(EID id);
};

View File

@ -19,6 +19,7 @@
// Modules inlcude
#include "CBot/CBotToken.h"
#include "CBotKeywordStrings.h"
// Local include
@ -480,14 +481,14 @@ void CBotToken::LoadKeyWords()
int i, n = 0;
i = TokenKeyWord; //start with keywords of the language
while (s.LoadString(i))
while (!(s = LoadString(static_cast<EID>(i))).IsEmpty())
{
m_ListKeyWords.push_back(s);
m_ListIdKeyWords[n++] = i++;
}
i = TokenKeyDeclare; //keywords of declarations
while (s.LoadString(i))
while (!(s = LoadString(static_cast<EID>(i))).IsEmpty())
{
m_ListKeyWords.push_back(s);
m_ListIdKeyWords[n++] = i++;
@ -495,14 +496,14 @@ void CBotToken::LoadKeyWords()
i = TokenKeyVal; //keywords of values
while (s.LoadString(i))
while (!(s = LoadString(static_cast<EID>(i))).IsEmpty())
{
m_ListKeyWords.push_back(s);
m_ListIdKeyWords[n++] = i++;
}
i = TokenKeyOp; //operators
while (s.LoadString(i))
while (!(s = LoadString(static_cast<EID>(i))).IsEmpty())
{
m_ListKeyWords.push_back(s);
m_ListIdKeyWords[n++] = i++;

View File

@ -21,8 +21,8 @@
#include "CBot/CBotVar/CBotVarBoolean.h"
#include "CBot/CBotEnums.h"
#include "CBot/CBotUtils.h"
#include "CBot/CBotKeywordStrings.h"
// Local include
@ -98,16 +98,16 @@ CBotString CBotVarBoolean::GetValString()
if ( m_binit == CBotVar::InitType::UNDEF )
{
res.LoadString(TX_UNDEF);
res = LoadString(TX_UNDEF);
return res;
}
if ( m_binit == CBotVar::InitType::IS_NAN )
{
res.LoadString(TX_NAN);
res = LoadString(TX_NAN);
return res;
}
ret.LoadString( m_val > 0 ? ID_TRUE : ID_FALSE );
ret = LoadString( m_val > 0 ? ID_TRUE : ID_FALSE );
return ret;
}

View File

@ -21,8 +21,8 @@
#include "CBot/CBotVar/CBotVarFloat.h"
#include "CBot/CBotEnums.h"
#include "CBot/CBotToken.h"
#include "CBot/CBotKeywordStrings.h"
#include "CBot/CBotUtils.h"
@ -99,13 +99,11 @@ CBotString CBotVarFloat::GetValString()
if ( m_binit == CBotVar::InitType::UNDEF )
{
res.LoadString(TX_UNDEF);
return res;
return LoadString(TX_UNDEF);
}
if ( m_binit == CBotVar::InitType::IS_NAN )
{
res.LoadString(TX_NAN);
return res;
return LoadString(TX_NAN);
}
char buffer[300];

View File

@ -21,10 +21,9 @@
#include "CBot/CBotVar/CBotVarInt.h"
#include "CBot/CBotEnums.h"
#include "CBot/CBotToken.h"
#include "CBot/CBotUtils.h"
#include "CBot/CBotKeywordStrings.h"
// Local include
@ -102,14 +101,12 @@ CBotString CBotVarInt::GetValString()
if ( m_binit == CBotVar::InitType::UNDEF )
{
res.LoadString(TX_UNDEF);
return res;
return LoadString(TX_UNDEF);
}
if ( m_binit == CBotVar::InitType::IS_NAN )
{
res.LoadString(TX_NAN);
return res;
return LoadString(TX_NAN);
}
char buffer[300];

View File

@ -21,10 +21,9 @@
#include "CBot/CBotVar/CBotVarString.h"
#include "CBot/CBotEnums.h"
#include "CBot/CBotToken.h"
#include "CBot/CBotUtils.h"
#include "CBot/CBotKeywordStrings.h"
// Local include
@ -77,15 +76,11 @@ CBotString CBotVarString::GetValString()
{
if ( m_binit == CBotVar::InitType::UNDEF )
{
CBotString res;
res.LoadString(TX_UNDEF);
return res;
return LoadString(TX_UNDEF);
}
if ( m_binit == CBotVar::InitType::IS_NAN )
{
CBotString res;
res.LoadString(TX_NAN);
return res;
return LoadString(TX_NAN);
}
return m_val;

View File

@ -1,73 +1,73 @@
set(SOURCES
CBotUtils.cpp
CBotFileUtils.cpp
CBotClass.cpp
CBotProgram.cpp
CBotStack.cpp
CBotCStack.cpp
CBotString.cpp
CBotToken.cpp
CBotCall.cpp
CBotDefParam.cpp
CBotCallMethode.cpp
CBotTypResult.cpp
StringFunctions.cpp
CBotInstr/CBotInstr.cpp
CBotInstr/CBotInstrUtils.cpp
CBotInstr/CBotWhile.cpp
CBotInstr/CBotDo.cpp
CBotInstr/CBotFor.cpp
CBotInstr/CBotListExpression.cpp
CBotInstr/CBotSwitch.cpp
CBotInstr/CBotCase.cpp
CBotInstr/CBotBreak.cpp
CBotInstr/CBotTry.cpp
CBotInstr/CBotCatch.cpp
CBotInstr/CBotThrow.cpp
CBotInstr/CBotExprAlpha.cpp
CBotInstr/CBotExprNum.cpp
CBotInstr/CBotNew.cpp
CBotInstr/CBotExprNan.cpp
CBotInstr/CBotExprNull.cpp
CBotInstr/CBotExprBool.cpp
CBotInstr/CBotLeftExprVar.cpp
CBotInstr/CBotPreIncExpr.cpp
CBotInstr/CBotPostIncExpr.cpp
CBotInstr/CBotExprVar.cpp
CBotInstr/CBotInstrMethode.cpp
CBotInstr/CBotInstrCall.cpp
CBotInstr/CBotListInstr.cpp
CBotInstr/CBotBlock.cpp
CBotInstr/CBotExprUnaire.cpp
CBotInstr/CBotParExpr.cpp
CBotInstr/CBotBoolExpr.cpp
CBotInstr/CBotLogicExpr.cpp
CBotInstr/CBotTwoOpExpr.cpp
CBotInstr/CBotExpression.cpp
CBotInstr/CBotIndexExpr.cpp
CBotInstr/CBotFieldExpr.cpp
CBotInstr/CBotLeftExpr.cpp
CBotInstr/CBotCondition.cpp
CBotInstr/CBotClassInst.cpp
CBotInstr/CBotIString.cpp
CBotInstr/CBotFloat.cpp
CBotInstr/CBotBoolean.cpp
CBotInstr/CBotEmpty.cpp
CBotInstr/CBotReturn.cpp
CBotInstr/CBotIf.cpp
CBotInstr/CBotListArray.cpp
CBotInstr/CBotInstArray.cpp
CBotInstr/CBotInt.cpp
CBotInstr/CBotFunction.cpp
CBotVar/CBotVarArray.cpp
CBotVar/CBotVarPointer.cpp
CBotVar/CBotVarClass.cpp
CBotVar/CBotVarBoolean.cpp
CBotVar/CBotVarString.cpp
CBotVar/CBotVarFloat.cpp
CBotVar/CBotVarInt.cpp
CBotVar/CBotVar.cpp
)
CBotUtils.cpp
CBotFileUtils.cpp
CBotClass.cpp
CBotProgram.cpp
CBotStack.cpp
CBotCStack.cpp
CBotString.cpp
CBotToken.cpp
CBotCall.cpp
CBotDefParam.cpp
CBotCallMethode.cpp
CBotTypResult.cpp
StringFunctions.cpp
CBotInstr/CBotInstr.cpp
CBotInstr/CBotInstrUtils.cpp
CBotInstr/CBotWhile.cpp
CBotInstr/CBotDo.cpp
CBotInstr/CBotFor.cpp
CBotInstr/CBotListExpression.cpp
CBotInstr/CBotSwitch.cpp
CBotInstr/CBotCase.cpp
CBotInstr/CBotBreak.cpp
CBotInstr/CBotTry.cpp
CBotInstr/CBotCatch.cpp
CBotInstr/CBotThrow.cpp
CBotInstr/CBotExprAlpha.cpp
CBotInstr/CBotExprNum.cpp
CBotInstr/CBotNew.cpp
CBotInstr/CBotExprNan.cpp
CBotInstr/CBotExprNull.cpp
CBotInstr/CBotExprBool.cpp
CBotInstr/CBotLeftExprVar.cpp
CBotInstr/CBotPreIncExpr.cpp
CBotInstr/CBotPostIncExpr.cpp
CBotInstr/CBotExprVar.cpp
CBotInstr/CBotInstrMethode.cpp
CBotInstr/CBotInstrCall.cpp
CBotInstr/CBotListInstr.cpp
CBotInstr/CBotBlock.cpp
CBotInstr/CBotExprUnaire.cpp
CBotInstr/CBotParExpr.cpp
CBotInstr/CBotBoolExpr.cpp
CBotInstr/CBotLogicExpr.cpp
CBotInstr/CBotTwoOpExpr.cpp
CBotInstr/CBotExpression.cpp
CBotInstr/CBotIndexExpr.cpp
CBotInstr/CBotFieldExpr.cpp
CBotInstr/CBotLeftExpr.cpp
CBotInstr/CBotCondition.cpp
CBotInstr/CBotClassInst.cpp
CBotInstr/CBotIString.cpp
CBotInstr/CBotFloat.cpp
CBotInstr/CBotBoolean.cpp
CBotInstr/CBotEmpty.cpp
CBotInstr/CBotReturn.cpp
CBotInstr/CBotIf.cpp
CBotInstr/CBotListArray.cpp
CBotInstr/CBotInstArray.cpp
CBotInstr/CBotInt.cpp
CBotInstr/CBotFunction.cpp
CBotVar/CBotVarArray.cpp
CBotVar/CBotVarPointer.cpp
CBotVar/CBotVarClass.cpp
CBotVar/CBotVarBoolean.cpp
CBotVar/CBotVarString.cpp
CBotVar/CBotVarFloat.cpp
CBotVar/CBotVarInt.cpp
CBotVar/CBotVar.cpp
CBotKeywordStrings.cpp CBotKeywordStrings.h)
# Includes
set(LOCAL_INCLUDES