Refactored part of CScript to std::string

dev-time-step
krzys-h 2016-04-08 22:27:42 +02:00
parent 7f38aca766
commit bd9184bd92
3 changed files with 33 additions and 55 deletions

View File

@ -70,11 +70,8 @@ CScript::CScript(COldObject* object)
m_bRun = false;
m_bStepMode = false;
m_bCompile = false;
m_title[0] = 0;
m_mainFunction[0] = 0;
m_cursor1 = 0;
m_cursor2 = 0;
m_filename[0] = 0;
}
// Object's destructor.
@ -159,8 +156,8 @@ bool CScript::CheckToken()
if ( !m_object->GetCheckToken() ) return true;
m_error = CBot::CBotNoErr;
m_title[0] = 0;
m_mainFunction[0] = 0;
m_title.clear();
m_mainFunction.clear();
m_token.clear();
m_bCompile = false;
@ -206,8 +203,8 @@ bool CScript::CheckToken()
m_tokenUsed = used[it.first];
m_tokenAllowed = allowed;
m_error = static_cast<CBot::CBotError>(error);
strcpy(m_title, "<incorrect instructions>");
m_mainFunction[0] = 0;
m_title = "<incorrect instructions>";
m_mainFunction.clear();
return false;
}
}
@ -220,14 +217,13 @@ bool CScript::CheckToken()
bool CScript::Compile()
{
std::vector<std::string> functionList;
int i;
std::string p;
m_error = CBot::CBotNoErr;
m_cursor1 = 0;
m_cursor2 = 0;
m_title[0] = 0;
m_mainFunction[0] = 0;
m_title.clear();
m_mainFunction.clear();
m_bCompile = false;
if ( IsEmpty() ) // program exist?
@ -245,33 +241,17 @@ bool CScript::Compile()
{
if (functionList.empty())
{
strcpy(m_title, "<extern missing>");
m_mainFunction[0] = 0;
m_title = "<extern missing>";
m_mainFunction.clear();
}
else
{
p = functionList[0];
i = 0;
bool titleDone = false;
while ( true )
m_mainFunction = functionList[0];
m_title = m_mainFunction;
if (m_title.length() >= 20)
{
if ( p[i] == 0 || p[i] == '(' ) break;
if ( i >= 20 && !titleDone )
{
m_title[i+0] = '.';
m_title[i+1] = '.';
m_title[i+2] = '.';
m_title[i+3] = 0;
titleDone = true;
}
if(!titleDone)
m_title[i] = p[i];
m_mainFunction[i] = p[i];
i ++;
m_title = m_title.substr(0, 20)+"...";
}
if(!titleDone)
m_title[i] = 0;
m_mainFunction[i] = p[i];
}
m_bCompile = true;
return true;
@ -289,8 +269,8 @@ bool CScript::Compile()
{
m_cursor1 = m_cursor2 = 0;
}
strcpy(m_title, "<error>");
m_mainFunction[0] = 0;
m_title = "<error>";
m_mainFunction.clear();
return false;
}
}
@ -298,9 +278,9 @@ bool CScript::Compile()
// Returns the title of the script.
void CScript::GetTitle(char* buffer)
const std::string& CScript::GetTitle()
{
strcpy(buffer, m_title);
return m_title;
}
@ -323,9 +303,9 @@ bool CScript::Run()
{
if (m_botProg == nullptr) return false;
if ( m_script == nullptr || m_len == 0 ) return false;
if ( m_mainFunction[0] == 0 ) return false;
if ( m_mainFunction.empty() ) return false;
if ( !m_botProg->Start(m_mainFunction) ) return false;
if ( !m_botProg->Start(m_mainFunction.c_str()) ) return false;
m_bRun = true;
m_bContinue = false;
@ -1024,12 +1004,12 @@ bool CScript::Compare(CScript* other)
// Management of the file name when the script is saved.
void CScript::SetFilename(char *filename)
void CScript::SetFilename(const std::string& filename)
{
strcpy(m_filename, filename);
m_filename = filename;
}
char* CScript::GetFilename()
const std::string& CScript::GetFilename()
{
return m_filename;
}

View File

@ -66,7 +66,7 @@ public:
bool GetScript(Ui::CEdit* edit);
bool GetCompile();
void GetTitle(char* buffer);
const std::string& GetTitle();
void SetStepMode(bool bStep);
bool GetStepMode();
@ -92,8 +92,8 @@ public:
bool WriteStack(FILE *file);
bool Compare(CScript* other);
void SetFilename(char *filename);
char* GetFilename();
void SetFilename(const std::string &filename);
const std::string& GetFilename();
protected:
bool IsEmpty();
@ -119,9 +119,9 @@ protected:
bool m_bStepMode = false; // step by step
bool m_bContinue = false; // external function to continue
bool m_bCompile = false; // compilation ok?
char m_title[50] = {}; // script title
char m_mainFunction[50] = {};
char m_filename[50] = {}; // file name
std::string m_title = ""; // script title
std::string m_mainFunction = "";
std::string m_filename = ""; // file name
std::string m_token = ""; // missing instruction
int m_tokenUsed = 0, m_tokenAllowed = 0;
CBot::CBotError m_error = CBot::CBotNoErr; // error (0=ok)

View File

@ -1623,7 +1623,6 @@ void CObjectInterface::UpdateInterface()
CSlider* ps;
CColor* pc;
bool bFly, bRun;
char title[100];
if ( !m_object->GetSelect() ) return;
@ -1793,8 +1792,8 @@ void CObjectInterface::UpdateInterface()
{
if (m_programStorage->GetProgram(m_selScript)->runnable)
{
m_programStorage->GetProgram(m_selScript)->script->GetTitle(title);
if ( title[0] != 0 )
std::string title = m_programStorage->GetProgram(m_selScript)->script->GetTitle();
if ( !title.empty() )
{
bRun = true;
}
@ -1943,7 +1942,6 @@ void CObjectInterface::UpdateScript(CWindow *pw)
{
CList* pl;
char name[100];
char title[100];
pl = static_cast< CList* >(pw->SearchControl(EVENT_OBJECT_PROGLIST));
if ( pl == nullptr ) return;
@ -1953,16 +1951,16 @@ void CObjectInterface::UpdateScript(CWindow *pw)
{
sprintf(name, "%d", i+1);
m_programStorage->GetProgram(i)->script->GetTitle(title);
if ( title[0] != 0 )
std::string title = m_programStorage->GetProgram(i)->script->GetTitle();
if ( !title.empty() )
{
if(!m_programStorage->GetProgram(i)->readOnly)
{
sprintf(name, "%d: %s", i+1, title);
sprintf(name, "%d: %s", i+1, title.c_str());
}
else
{
sprintf(name, "*%d: %s", i+1, title);
sprintf(name, "*%d: %s", i+1, title.c_str());
}
}