From bd9184bd92d60986eabe298f2b4479a52f2d2219 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Fri, 8 Apr 2016 22:27:42 +0200 Subject: [PATCH] Refactored part of CScript to std::string --- src/script/script.cpp | 62 +++++++++++++------------------------ src/script/script.h | 12 +++---- src/ui/object_interface.cpp | 14 ++++----- 3 files changed, 33 insertions(+), 55 deletions(-) diff --git a/src/script/script.cpp b/src/script/script.cpp index 924081da..4f7bf323 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -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(error); - strcpy(m_title, ""); - m_mainFunction[0] = 0; + m_title = ""; + m_mainFunction.clear(); return false; } } @@ -220,14 +217,13 @@ bool CScript::CheckToken() bool CScript::Compile() { std::vector 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, ""); - m_mainFunction[0] = 0; + m_title = ""; + 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, ""); - m_mainFunction[0] = 0; + m_title = ""; + 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; } diff --git a/src/script/script.h b/src/script/script.h index a1141714..deed0c6b 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -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) diff --git a/src/ui/object_interface.cpp b/src/ui/object_interface.cpp index c2ed098f..aa52678a 100644 --- a/src/ui/object_interface.cpp +++ b/src/ui/object_interface.cpp @@ -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()); } }