From dc415c3d2a77f7f447d87118e49f1f53d9a4595a Mon Sep 17 00:00:00 2001 From: MatiRg Date: Sun, 25 Sep 2016 19:13:04 +0200 Subject: [PATCH] Remove character limit in CEdit (#836) --- src/level/robotmain.cpp | 4 +- .../implementation/program_storage_impl.cpp | 1 - src/script/script.cpp | 15 +- src/ui/controls/edit.cpp | 240 ++++++++---------- src/ui/controls/edit.h | 30 +-- src/ui/controls/editvalue.cpp | 6 +- src/ui/screen/screen_io.cpp | 9 +- src/ui/screen/screen_player_select.cpp | 22 +- src/ui/studio.cpp | 75 +++--- 9 files changed, 180 insertions(+), 222 deletions(-) diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 23c9c9db..cfb98b4e 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -780,10 +780,10 @@ bool CRobotMain::ProcessEvent(Event &event) if (event.type == EVENT_KEY_DOWN && event.GetData()->key == KEY(RETURN) && m_cmdEdit) { - char cmd[50]; + std::string cmd; Ui::CEdit* pe = static_cast(m_interface->SearchControl(EVENT_CMD)); if (pe == nullptr) return false; - pe->GetText(cmd, 50); + cmd = pe->GetText(50); pe->SetText(""); pe->ClearState(Ui::STATE_VISIBLE); m_interface->SetFocus(nullptr); diff --git a/src/object/implementation/program_storage_impl.cpp b/src/object/implementation/program_storage_impl.cpp index c0fad70b..dc7e71d2 100644 --- a/src/object/implementation/program_storage_impl.cpp +++ b/src/object/implementation/program_storage_impl.cpp @@ -131,7 +131,6 @@ Program* CProgramStorageObjectImpl::CloneProgram(Program* program) // TODO: Is there any reason CScript doesn't have a function to get the program code directly? auto edit = MakeUnique(); - edit->SetMaxChar(Ui::EDITSTUDIOMAX); program->script->PutScript(edit.get(), ""); newprog->script->GetScript(edit.get()); diff --git a/src/script/script.cpp b/src/script/script.cpp index ff4c04d2..8253caea 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -105,9 +105,11 @@ void CScript::PutScript(Ui::CEdit* edit, const char* name) bool CScript::GetScript(Ui::CEdit* edit) { int len = edit->GetTextLength(); - m_script = MakeUniqueArray(len+1); + m_script = MakeUniqueArray(len+2); + + std::string tmp = edit->GetText(len+1); + strncpy(m_script.get(), tmp.c_str(), len+1); - edit->GetText(m_script.get(), len+1); edit->GetCursor(m_cursor2, m_cursor1); m_len = strlen(m_script.get()); @@ -592,13 +594,13 @@ void CScript::UpdateList(Ui::CList* list) void CScript::ColorizeScript(Ui::CEdit* edit, int rangeStart, int rangeEnd) { - if (rangeEnd > edit->GetMaxChar()) - rangeEnd = edit->GetMaxChar(); + if (rangeEnd > edit->GetTextLength()) + rangeEnd = edit->GetTextLength(); edit->SetFormat(rangeStart, rangeEnd, Gfx::FONT_HIGHLIGHT_COMMENT); // anything not processed is a comment // NOTE: Images are registered as index in some array, and that can be 0 which normally ends the string! - std::string text = std::string(edit->GetText(), edit->GetMaxChar()); + std::string text = edit->GetText(); text = text.substr(rangeStart, rangeEnd-rangeStart); auto tokens = CBot::CBotToken::CompileTokens(text.c_str()); @@ -927,7 +929,6 @@ bool CScript::SendScript(const char* text) if ( !Compile() ) return false;*/ Ui::CEdit* edit = m_interface->CreateEdit(Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f), 0, EVENT_EDIT9); - edit->SetMaxChar(Ui::EDITSTUDIOMAX); edit->SetAutoIndent(m_engine->GetEditIndentMode()); edit->SetText(text, true); GetScript(edit); @@ -947,7 +948,6 @@ bool CScript::ReadScript(const char* filename) m_script.reset(); edit = m_interface->CreateEdit(Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f), 0, EVENT_EDIT9); - edit->SetMaxChar(Ui::EDITSTUDIOMAX); edit->SetAutoIndent(m_engine->GetEditIndentMode()); edit->ReadText(filename); GetScript(edit); @@ -966,7 +966,6 @@ bool CScript::WriteScript(const char* filename) } Ui::CEdit* edit = m_interface->CreateEdit(Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f), 0, EVENT_EDIT9); - edit->SetMaxChar(Ui::EDITSTUDIOMAX); edit->SetAutoIndent(m_engine->GetEditIndentMode()); edit->SetText(m_script.get()); edit->WriteText(filename); diff --git a/src/ui/controls/edit.cpp b/src/ui/controls/edit.cpp index c50f9796..f453c8fd 100644 --- a/src/ui/controls/edit.cpp +++ b/src/ui/controls/edit.cpp @@ -60,7 +60,6 @@ const float BIG_FONT = 1.6f; - //! Indicates whether a character is a space. bool IsSpace(int character) @@ -90,11 +89,11 @@ bool IsSep(int character) //! Object's constructor. CEdit::CEdit() : CControl(), + m_maxChar( std::numeric_limits::max() ), + m_text(), m_lineOffset(), m_lineIndent() { - m_maxChar = 100; - m_text = std::vector(m_maxChar+1, '\0'); m_len = 0; m_fontType = Gfx::FONT_COURIER; @@ -558,7 +557,7 @@ bool CEdit::IsLinkPos(Math::Point pos) { int i; - if ( m_format.size() == 0 ) return false; + if ( m_format.empty() ) return false; i = MouseDetect(pos); if ( i == -1 ) return false; @@ -721,7 +720,7 @@ int CEdit::MouseDetect(Math::Point mouse) { len = m_lineOffset[i+1] - m_lineOffset[i]; - if ( m_format.size() == 0 ) + if ( m_format.empty() ) { // c = m_engine->GetText()->Detect(m_text.data()+m_lineOffset[i], // len, offset, m_fontSize, @@ -1018,7 +1017,7 @@ void CEdit::Draw() o1 = c1; if ( o1 < beg ) o1 = beg; o2 = c2; if ( o2 > beg+len ) o2 = beg+len; - if ( m_format.size() == 0 ) + if ( m_format.empty() ) { start.x = ppos.x+m_engine->GetText()->GetStringWidth(std::string(m_text.data()+beg).substr(0, o1-beg), m_fontType, size); end.x = m_engine->GetText()->GetStringWidth(std::string(m_text.data()+o1).substr(0, o2-o1), m_fontType, size); @@ -1052,7 +1051,7 @@ void CEdit::Draw() eol = 2; // square (eot) } if ( !m_bMulti || !m_bDisplaySpec ) eol = 0; - if ( m_format.size() == 0 ) + if ( m_format.empty() ) { m_engine->GetText()->DrawText(std::string(m_text.data()+beg).substr(0, len), m_fontType, size, ppos, m_dim.x, Gfx::TEXT_ALIGN_LEFT, eol); } @@ -1093,7 +1092,7 @@ void CEdit::Draw() len = m_cursor1 - m_lineOffset[i]; - if ( m_format.size() == 0 ) + if ( m_format.empty() ) { m_engine->GetText()->SizeText(std::string(m_text.data()+m_lineOffset[i]).substr(0, len), m_fontType, size, pos, Gfx::TEXT_ALIGN_LEFT, @@ -1255,106 +1254,82 @@ void CEdit::DrawColor(Math::Point pos, Math::Point dim, Gfx::Color color) // Give the text to edit. -void CEdit::SetText(const char *text, bool bNew) +void CEdit::SetText(const std::string& text, bool bNew) { int i, j, font; bool bBOL; if ( !bNew ) UndoMemorize(OPERUNDO_SPEC); - m_len = strlen(text); - if ( m_len > m_maxChar ) m_len = m_maxChar; + m_len = text.size(); - if ( m_format.size() == 0 ) + if( m_len >= GetMaxChar() ) m_len = GetMaxChar(); + + m_text.resize( m_len + 1, '\0' ); + m_format.resize( m_len + 1, m_fontType ); + + font = m_fontType; + j = 0; + bBOL = true; + for ( i=0 ; i max ) max = max-1; - strncpy(buffer, m_text.data(), max); - buffer[max] = 0; + return std::string( m_text, 0, max ); } // Returns the length of the text. @@ -1437,15 +1410,15 @@ void CEdit::FreeImage() // Read from a text file. -bool CEdit::ReadText(std::string filename, int addSize) +bool CEdit::ReadText(std::string filename) { - int len, i, j, n, font, iLines, iCount; + int len, len2, i, j, n, font, iLines, iCount; char iName[50]; float iWidth; InputSlot slot; bool bInSoluce, bBOL; - if ( filename == "" ) return false; + if ( filename.empty() ) return false; CInputStream stream; stream.open(filename); @@ -1457,26 +1430,22 @@ bool CEdit::ReadText(std::string filename, int addSize) } len = stream.size(); + len2 = len + 1; - m_maxChar = len+addSize+100; m_len = len; m_cursor1 = 0; m_cursor2 = 0; FreeImage(); - m_text = std::vector(m_maxChar+1, '\0'); + m_text = std::string(len2+1, '\0'); - std::vector buffer(m_maxChar+1, '\0'); + std::vector buffer(len2+1, '\0'); stream.read(buffer.data(), len); m_format.clear(); - m_format.reserve(m_maxChar+1); - for (i = 0; i <= m_maxChar+1; i++) - { - m_format.push_back(m_fontType); - } + m_format.resize(len2+1, m_fontType); stream.close(); @@ -1922,14 +1891,10 @@ void CEdit::SetMaxChar(int max) m_maxChar = max; - m_text = std::vector(m_maxChar+1, '\0'); + m_text.resize( m_maxChar + 1, '\0' ); m_format.clear(); - m_format.reserve(m_maxChar+1); - for (int i = 0; i <= m_maxChar+1; i++) - { - m_format.push_back(m_fontType); - } + m_format.resize(m_maxChar + 1, m_fontType); m_len = 0; m_cursor1 = 0; @@ -2121,11 +2086,7 @@ void CEdit::SetMultiFont(bool bMulti) if (bMulti) { - m_format.reserve(m_maxChar+1); - for (int i = 0; i <= m_maxChar+1; i++) - { - m_format.push_back(m_fontType); - } + m_format.resize( m_text.size() + 1, m_fontType ); } } @@ -2419,7 +2380,7 @@ void CEdit::MoveLine(int move, bool bWord, bool bSelect) column -= indentLength*m_lineIndent[line]; } - if ( m_format.size() == 0 ) + if ( m_format.empty() ) { c = m_engine->GetText()->Detect(std::string(m_text.data()+m_lineOffset[line]), m_fontType, m_fontSize, @@ -2450,7 +2411,7 @@ void CEdit::ColumnFix() line = GetCursorLine(m_cursor1); - if ( m_format.size() == 0 ) + if ( m_format.empty() ) { m_column = m_engine->GetText()->GetStringWidth( std::string(m_text.data()+m_lineOffset[line]), @@ -2706,7 +2667,10 @@ void CEdit::InsertOne(char character) DeleteOne(0); // deletes the selected characters } - if ( m_len >= m_maxChar ) return; + if ( m_len >= GetMaxChar() ) return; + + m_text.resize( m_text.size() + 1, '\0' ); + m_format.resize( m_format.size() + 1, m_fontType ); for ( i=m_len ; i>m_cursor1 ; i-- ) { @@ -2938,13 +2902,16 @@ bool CEdit::MinMaj(bool bMaj) void CEdit::Justif() { float width, size, indentLength = 0.0f; - int i, j, line, indent; + int i, j, k, line, indent; bool bDual, bString, bRem; + m_lineOffset.clear(); + m_lineIndent.clear(); + indent = 0; m_lineTotal = 0; - m_lineOffset[m_lineTotal] = 0; - m_lineIndent[m_lineTotal] = indent; + m_lineOffset.push_back( 0 ); + m_lineIndent.push_back( indent ); m_lineTotal ++; if ( m_bAutoIndent ) @@ -2954,7 +2921,7 @@ void CEdit::Justif() } bString = bRem = false; - i = 0; + i = k = 0; while ( true ) { bDual = false; @@ -2965,7 +2932,7 @@ void CEdit::Justif() width -= indentLength*m_lineIndent[m_lineTotal-1]; } - if ( m_format.size() == 0 ) + if ( m_format.empty() ) { // TODO check if good @@ -3014,26 +2981,27 @@ void CEdit::Justif() if ( indent < 0 ) indent = 0; } - m_lineOffset[m_lineTotal] = i; - m_lineIndent[m_lineTotal] = indent; + m_lineOffset.push_back( i ); + m_lineIndent.push_back( indent ); m_lineTotal ++; if ( bDual ) { - m_lineOffset[m_lineTotal] = i; - m_lineIndent[m_lineTotal] = indent; + m_lineOffset.push_back( i ); + m_lineIndent.push_back( indent ); m_lineTotal ++; } - if ( m_lineTotal >= EDITLINEMAX-2 ) break; + if ( k == i ) break; + k = i; } if ( m_len > 0 && m_text[m_len-1] == '\n' ) { - m_lineOffset[m_lineTotal] = m_len; - m_lineIndent[m_lineTotal] = 0; + m_lineOffset.push_back( m_len ); + m_lineIndent.push_back( 0 ); m_lineTotal ++; } - m_lineOffset[m_lineTotal] = m_len; - m_lineIndent[m_lineTotal] = 0; + m_lineOffset.push_back( m_len ); + m_lineIndent.push_back( 0 ); if ( m_bAutoIndent ) { @@ -3168,7 +3136,7 @@ bool CEdit::UndoRecall() bool CEdit::ClearFormat() { - if ( m_format.size() == 0 ) + if ( m_format.empty() ) { SetMultiFont(true); } diff --git a/src/ui/controls/edit.h b/src/ui/controls/edit.h index bb7b5e3c..4192a133 100644 --- a/src/ui/controls/edit.h +++ b/src/ui/controls/edit.h @@ -34,21 +34,15 @@ namespace Ui class CScroll; - -//! maximum number of characters in CBOT edit -const int EDITSTUDIOMAX = 20000; -//! maximum total number of lines -const int EDITLINEMAX = 1000; //! max number of levels preserves const int EDITHISTORYMAX = 50; - //! max number of successive undo const int EDITUNDOMAX = 20; struct EditUndo { //! original text - std::vector text; + std::string text; //! length of the text int len = 0; //! offset cursor @@ -124,12 +118,12 @@ public: bool EventProcess(const Event &event) override; void Draw() override; - void SetText(const char *text, bool bNew=true); - void GetText(char *buffer, int max); - char* GetText(); - int GetTextLength(); + void SetText(const std::string& text, bool bNew=true); + std::string GetText(int max); + const std::string& GetText(); + int GetTextLength(); - bool ReadText(std::string filename, int addSize=0); + bool ReadText(std::string filename); bool WriteText(std::string filename); void SetMaxChar(int max); @@ -234,8 +228,8 @@ protected: protected: std::unique_ptr m_scroll; // vertical scrollbar on the right - int m_maxChar; // max length of the buffer m_text - std::vector m_text; // text (without zero terminator) + int m_maxChar; + std::string m_text; // text (without zero terminator) std::vector m_format; // format characters int m_len; // length used in m_text int m_cursor1; // offset cursor @@ -256,14 +250,14 @@ protected: int m_lineVisible; // total number of viewable lines int m_lineFirst; // the first line displayed int m_lineTotal; // number lines used (in m_lineOffset) - int m_lineOffset[EDITLINEMAX]; - char m_lineIndent[EDITLINEMAX]; + std::vector m_lineOffset; + std::vector m_lineIndent; std::vector m_image; std::vector m_link; std::vector m_marker; int m_historyTotal; int m_historyCurrent; - HyperHistory m_history[EDITHISTORYMAX]; + std::array m_history; float m_time; // absolute time float m_timeBlink; float m_timeLastClick; @@ -276,7 +270,7 @@ protected: bool m_bUndoForce; OperUndo m_undoOper; - EditUndo m_undo[EDITUNDOMAX]; + std::array m_undo; }; diff --git a/src/ui/controls/editvalue.cpp b/src/ui/controls/editvalue.cpp index 21bb3d46..277f18b0 100644 --- a/src/ui/controls/editvalue.cpp +++ b/src/ui/controls/editvalue.cpp @@ -298,13 +298,13 @@ void CEditValue::SetValue(float value, bool bSendMessage) float CEditValue::GetValue() { - char text[100]; + std::string text; float value = 0.0f; if ( m_edit != nullptr ) { - m_edit->GetText(text, 100); - sscanf(text, "%f", &value); + text = m_edit->GetText(100); + sscanf(text.c_str(), "%f", &value); if ( m_type == EVT_100 ) { diff --git a/src/ui/screen/screen_io.cpp b/src/ui/screen/screen_io.cpp index 82db8d12..6cf1b4a9 100644 --- a/src/ui/screen/screen_io.cpp +++ b/src/ui/screen/screen_io.cpp @@ -193,11 +193,10 @@ void CScreenIO::IODeleteScene() } // clears filename only to leave letter or numbers -std::string clearName(char *name) +std::string clearName(std::string name) { std::string ret; - int len = strlen(name); - for (int i = 0; i < len; i++) + for (int i = 0; i < static_cast(name.size()); i++) { if (isalnum(name[i])) { @@ -214,7 +213,7 @@ void CScreenIO::IOWriteScene() CWindow* pw; CList* pl; CEdit* pe; - char info[100]; + std::string info; pw = static_cast(m_interface->SearchControl(EVENT_WINDOW5)); if ( pw == nullptr ) return; @@ -226,7 +225,7 @@ void CScreenIO::IOWriteScene() int sel = pl->GetSelect(); if ( sel == -1 ) return; - pe->GetText(info, 100); + info = pe->GetText(100); m_interface->DeleteControl(EVENT_WINDOW5); diff --git a/src/ui/screen/screen_player_select.cpp b/src/ui/screen/screen_player_select.cpp index 678f8286..4bcd139b 100644 --- a/src/ui/screen/screen_player_select.cpp +++ b/src/ui/screen/screen_player_select.cpp @@ -232,7 +232,7 @@ void CScreenPlayerSelect::UpdateNameControl() CList* pl; CButton* pb; CEdit* pe; - char name[100]; + std::string name; int total, sel; pw = static_cast(m_interface->SearchControl(EVENT_WINDOW5)); @@ -244,7 +244,7 @@ void CScreenPlayerSelect::UpdateNameControl() total = pl->GetTotal(); sel = pl->GetSelect(); - pe->GetText(name, 100); + name = pe->GetText(100); pb = static_cast(pw->SearchControl(EVENT_INTERFACE_NDELETE)); if ( pb != nullptr ) @@ -255,13 +255,13 @@ void CScreenPlayerSelect::UpdateNameControl() pb = static_cast(pw->SearchControl(EVENT_INTERFACE_NOK)); if ( pb != nullptr ) { - pb->SetState(STATE_ENABLE, name[0]!=0 || sel!=-1); + pb->SetState(STATE_ENABLE, !name.empty() || sel!=-1); } pb = static_cast(pw->SearchControl(EVENT_INTERFACE_PERSO)); if ( pb != nullptr ) { - pb->SetState(STATE_ENABLE, name[0]!=0 || sel!=-1); + pb->SetState(STATE_ENABLE, !name.empty() || sel!=-1); } } @@ -272,7 +272,7 @@ void CScreenPlayerSelect::UpdateNameList() CWindow* pw; CList* pl; CEdit* pe; - char name[100]; + std::string name; int total, i; pw = static_cast(m_interface->SearchControl(EVENT_WINDOW5)); @@ -282,7 +282,7 @@ void CScreenPlayerSelect::UpdateNameList() pe = static_cast(pw->SearchControl(EVENT_INTERFACE_NEDIT)); if ( pe == nullptr ) return; - pe->GetText(name, 100); + name = pe->GetText(100); total = pl->GetTotal(); for ( i=0 ; i(m_interface->SearchControl(EVENT_WINDOW5)); @@ -349,7 +349,7 @@ void CScreenPlayerSelect::NameSelect() pe = static_cast(pw->SearchControl(EVENT_INTERFACE_NEDIT)); if ( pe == nullptr ) return; - pe->GetText(name, 100); + name = pe->GetText(100); sel = pl->GetSelect(); if ( sel == -1 ) @@ -377,9 +377,9 @@ bool CScreenPlayerSelect::NameCreate() pe = static_cast(pw->SearchControl(EVENT_INTERFACE_NEDIT)); if ( pe == nullptr ) return false; - char name[100]; - pe->GetText(name, 100); - if ( name[0] == 0 ) + std::string name; + name = pe->GetText(100); + if ( name.empty() ) { m_sound->Play(SOUND_TZOING); return false; diff --git a/src/ui/studio.cpp b/src/ui/studio.cpp index 22268ecb..4f6fa206 100644 --- a/src/ui/studio.cpp +++ b/src/ui/studio.cpp @@ -436,8 +436,8 @@ void CStudio::SearchToken(CEdit* edit) { ObjectType type; int len, cursor1, cursor2, i, character, level; - char* text; - char token[100]; + std::string text; + std::string token( 100, '\0'); text = edit->GetText(); len = edit->GetTextLength(); @@ -503,7 +503,7 @@ void CStudio::SearchToken(CEdit* edit) } token[i] = 0; - m_helpFilename = GetHelpFilename(token); + m_helpFilename = GetHelpFilename(token.c_str()); if ( m_helpFilename.length() == 0 ) { for ( i=0 ; i(GetObjectName(type)); if ( text[0] != 0 ) { - if ( strcmp(token, text) == 0 ) + if ( token == text ) { m_helpFilename = GetHelpFilename(type); - SetInfoText(std::string(token), true); + SetInfoText(token, true); return; } } text = const_cast(GetObjectAlias(type)); if ( text[0] != 0 ) { - if ( strcmp(token, text) == 0 ) + if ( token == text ) { m_helpFilename = GetHelpFilename(type); - SetInfoText(std::string(token), true); + SetInfoText(token, true); return; } } } } - text = const_cast(GetHelpText(token)); + text = const_cast(GetHelpText(token.c_str())); if ( text[0] == 0 && m_helpFilename.length() > 0 ) { - SetInfoText(std::string(token), true); + SetInfoText(token, true); } else { @@ -606,7 +606,6 @@ void CStudio::StartEditScript(CScript *script, std::string name, Program* progra edit->SetState(STATE_SHADOW); edit->SetInsideScroll(false); //? if ( m_bRunning ) edit->SetEdit(false); - edit->SetMaxChar(EDITSTUDIOMAX); edit->SetFontType(Gfx::FONT_COURIER); edit->SetFontStretch(1.0f); edit->SetDisplaySpec(true); @@ -1245,7 +1244,7 @@ void CStudio::AdjustDialog() CEdit* pe; Math::Point wpos, wdim, ppos, ddim; int nli, nch; - char name[100]; + std::string name; pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW9)); if ( pw == nullptr ) return; @@ -1302,7 +1301,7 @@ void CStudio::AdjustDialog() pe->SetDim(ddim); nch = static_cast< int >((ddim.x*640.0f-22.0f)/8.0f); - pe->GetText(name, 100); + name = pe->GetText(100); pe->SetMaxChar(nch); name[nch] = 0; // truncates the text according to max pe->SetText(name); @@ -1467,7 +1466,7 @@ void CStudio::SetFilenameField(CEdit* edit, const std::string& filename) name = name.substr(0, edit->GetMaxChar()); // truncates according to max length } } - edit->SetText(name.c_str()); + edit->SetText(name); } // Updates the list after a change in name. @@ -1494,7 +1493,7 @@ void CStudio::UpdateDialogAction() CWindow* pw; CEdit* pe; CButton* pb; - char name[100]; + std::string name; int len, i; bool bError; @@ -1505,8 +1504,8 @@ void CStudio::UpdateDialogAction() pb = static_cast< CButton* >(pw->SearchControl(EVENT_DIALOG_OK)); if ( pb == nullptr ) return; - pe->GetText(name, 100); - len = strlen(name); + name = pe->GetText(100); + len = name.size(); if ( len == 0 ) { bError = true; @@ -1558,7 +1557,7 @@ void CStudio::UpdateDialogPublic() if ( pl != nullptr ) { // GetResource(RES_TEXT, RT_IO_LIST, name); // TODO: unused? - pl->SetName(SearchDirectory(false).c_str(), false); + pl->SetName(SearchDirectory(false), false); } } @@ -1619,25 +1618,25 @@ bool CStudio::ReadProgram() { CWindow* pw; CEdit* pe; - char filename[100]; - char dir[100]; - char* p; + std::string filename; + std::string dir; + size_t p; pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW9)); if ( pw == nullptr ) return false; pe = static_cast< CEdit* >(pw->SearchControl(EVENT_DIALOG_EDIT)); if ( pe == nullptr ) return false; - pe->GetText(filename, 100); - if ( filename[0] == 0 ) return false; + filename = pe->GetText(100); + if ( filename.empty() ) return false; - p = strstr(filename, ".txt"); - if ( p == nullptr || p != filename+strlen(filename)-4 ) + p = filename.find(".txt"); + if ( p == std::string::npos ) { - strcat(filename, ".txt"); + filename += ".txt"; } - strcpy(dir, SearchDirectory(true).c_str()); - strcat(dir, filename); + dir = SearchDirectory(true); + dir += filename; pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW3)); if ( pw == nullptr ) return false; @@ -1657,32 +1656,32 @@ bool CStudio::WriteProgram() { CWindow* pw; CEdit* pe; - char filename[100]; - char dir[100]; - char* p; + std::string filename; + std::string dir; + size_t p; pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW9)); if ( pw == nullptr ) return false; pe = static_cast< CEdit* >(pw->SearchControl(EVENT_DIALOG_EDIT)); if ( pe == nullptr ) return false; - pe->GetText(filename, 100); - if ( filename[0] == 0 ) return false; + filename = pe->GetText(100); + if ( filename.empty() ) return false; - p = strstr(filename, ".txt"); - if ( p == nullptr || p != filename+strlen(filename)-4 ) + p = filename.find(".txt"); + if ( p == std::string::npos ) { - strcat(filename, ".txt"); + filename += ".txt"; } - strcpy(dir, SearchDirectory(true).c_str()); - strcat(dir, filename); + dir = SearchDirectory(true); + dir += filename; pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW3)); if ( pw == nullptr ) return false; pe = static_cast< CEdit* >(pw->SearchControl(EVENT_STUDIO_EDIT)); if ( pe == nullptr ) return false; - if ( !pe->WriteText(std::string(dir)) ) return false; + if ( !pe->WriteText(dir) ) return false; m_script->SetFilename(filename); return true;