From 2ba6b0bcf7331046ac80ca7892320159350e0db2 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Thu, 1 Oct 2015 04:44:26 +0200 Subject: [PATCH] Some code improvements --- src/fmenuitem.cpp | 44 +++++++++++++++++++++++++++++--------- src/foptimove.cpp | 10 ++++++--- src/fterm.cpp | 4 +--- test/string-operations.cpp | 12 +++++++++-- 4 files changed, 52 insertions(+), 18 deletions(-) diff --git a/src/fmenuitem.cpp b/src/fmenuitem.cpp index 7400e8fa..f78dbd5f 100644 --- a/src/fmenuitem.cpp +++ b/src/fmenuitem.cpp @@ -96,10 +96,12 @@ void FMenuItem::init (FWidget* parent) if ( parent ) { + FMenuList* super_menu_list = dynamic_cast(parent); + setSuperMenu(super_menu_list); + super_menu_list->insert(this); + if ( isMenuBar(parent) ) // Parent is menubar { - setSuperMenu( dynamic_cast(parent) ); - superMenu()->insert(this); FMenuBar* menubar_ptr = dynamic_cast(parent); if ( menubar_ptr ) menubar_ptr->menu_dimension(); @@ -119,8 +121,6 @@ void FMenuItem::init (FWidget* parent) } else if ( isMenu(parent) ) // Parent is menu { - setSuperMenu( dynamic_cast(parent) ); - superMenu()->insert(this); FMenu* super_menu_ptr = dynamic_cast(parent); if ( super_menu_ptr ) super_menu_ptr->menu_dimension(); @@ -259,10 +259,18 @@ void FMenuItem::onMouseDown (FMouseEvent* ev) if ( super_menu ) { if ( isMenu(super_menu) ) - dynamic_cast(super_menu)->onMouseDown(ev); + { + FMenu* sm = dynamic_cast(super_menu); + if ( sm ) + sm->onMouseDown(ev); + } if ( isMenuBar(super_menu) ) - dynamic_cast(super_menu)->onMouseDown(ev); + { + FMenuBar* mb = dynamic_cast(super_menu); + if ( mb ) + mb->onMouseDown(ev); + } } delete ev; @@ -281,10 +289,18 @@ void FMenuItem::onMouseUp (FMouseEvent* ev) if ( super_menu ) { if ( isMenu(super_menu) ) - dynamic_cast(super_menu)->onMouseUp(ev); + { + FMenu* sm = dynamic_cast(super_menu); + if ( sm ) + sm->onMouseUp(ev); + } if ( isMenuBar(super_menu) ) - dynamic_cast(super_menu)->onMouseUp(ev); + { + FMenuBar* mb = dynamic_cast(super_menu); + if ( mb ) + mb->onMouseUp(ev); + } } delete ev; @@ -303,10 +319,18 @@ void FMenuItem::onMouseMove (FMouseEvent* ev) if ( super_menu ) { if ( isMenu(super_menu) ) - dynamic_cast(super_menu)->onMouseMove(ev); + { + FMenu* sm = dynamic_cast(super_menu); + if ( sm ) + sm->onMouseMove(ev); + } if ( isMenuBar(super_menu) ) - dynamic_cast(super_menu)->onMouseMove(ev); + { + FMenuBar* mb = dynamic_cast(super_menu); + if ( mb ) + mb->onMouseMove(ev); + } } delete ev; diff --git a/src/foptimove.cpp b/src/foptimove.cpp index 546c25e1..0f3af13f 100644 --- a/src/foptimove.cpp +++ b/src/foptimove.cpp @@ -422,7 +422,9 @@ int FOptiMove::relative_move ( char*& move if ( F_parm_right_cursor.cap && F_parm_right_cursor.duration < htime ) { - strcat (hmove, tparm(F_parm_right_cursor.cap, num)); + strncat ( hmove + , tparm(F_parm_right_cursor.cap, num) + , sizeof(hmove) - strlen(hmove) - 1 ); htime = F_parm_right_cursor.duration; } @@ -647,7 +649,7 @@ char* FOptiMove::cursor_move (int xold, int yold, int xnew, int ynew) break; case 4: - strncpy (move_ptr, F_cursor_to_ll.cap, sizeof(move_buf)); + strncpy (move_ptr, F_cursor_to_ll.cap, sizeof(move_buf) - 1); move_ptr += F_cursor_to_ll.length; relative_move (move_ptr, 0, screen_height-1, xnew, ynew); break; @@ -656,7 +658,9 @@ char* FOptiMove::cursor_move (int xold, int yold, int xnew, int ynew) move_buf[0] = '\0'; if ( xold >= 0 ) strcat (move_ptr, F_carriage_return.cap); - strcat (move_ptr, F_cursor_left.cap); + strncat ( move_ptr + , F_cursor_left.cap + , sizeof(move_buf) - strlen(move_ptr) - 1 ); move_ptr += strlen(move_buf); relative_move (move_ptr, screen_width-1, yold-1, xnew, ynew); break; diff --git a/src/fterm.cpp b/src/fterm.cpp index 4174e9a3..580e1a60 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -220,11 +220,9 @@ int FTerm::getFramebuffer_bpp () else { ::close(fd); - return -1; } } - else - return -1; + return -1; } //---------------------------------------------------------------------- diff --git a/test/string-operations.cpp b/test/string-operations.cpp index 1ef290ac..bc4cc606 100644 --- a/test/string-operations.cpp +++ b/test/string-operations.cpp @@ -206,8 +206,15 @@ int main (int, char**) << alphabet.right(11) << "\"" << std::endl; FString insert_str = "I am a string"; - std::cout << " insert: " - << insert_str.insert("changed ", 7) << std::endl; + try + { + std::cout << " insert: " + << insert_str.insert("changed ", 7) << std::endl; + } + catch (const std::out_of_range& ex) + { + std::cerr << "Out of Range error: " << ex.what() << std::endl; + } FString index(5); // a string with five characters index = "index"; @@ -222,6 +229,7 @@ int main (int, char**) { std::cerr << "Out of Range error: " << ex.what() << std::endl; } + FString stringIterator = "iterator"; FString::iterator iter; iter = stringIterator.begin();