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

View File

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

View File

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