From f19b4811aea0d87cafb9ed6ded94ae89578b067f Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Tue, 2 Oct 2018 01:03:44 +0200 Subject: [PATCH] Small optimizations --- src/fstring.cpp | 51 +++++++++-------- src/fterm.cpp | 101 +++++++++++++++++++--------------- src/include/final/fstring.h | 1 + src/include/final/fterm.h | 2 + src/include/final/ftermdata.h | 5 +- test/ftermcapquirks-test.cpp | 1 - 6 files changed, 92 insertions(+), 69 deletions(-) diff --git a/src/fstring.cpp b/src/fstring.cpp index 0d9a15cd..64fa3c05 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -2675,40 +2675,45 @@ inline void FString::_assign (const wchar_t s[]) length = new_length; } +//---------------------------------------------------------------------- +inline void FString::_insert (uInt len, const wchar_t s[]) +{ + if ( len == 0 ) // String s is a null or a empty string + return; + + if ( string ) + delete[](string); + + length = len; + bufsize = FWDBUFFER + length + 1; + + try + { + string = new wchar_t[bufsize](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << bad_alloc_str << " " << ex.what() << std::endl; + return; + } + + std::wcsncpy (string, s, bufsize); + string[bufsize - 1] = L'\0'; +} + //---------------------------------------------------------------------- inline void FString::_insert (uInt pos, uInt len, const wchar_t s[]) { if ( len == 0 ) // String s is a null or a empty string return; - if ( ! string ) + if ( ! string ) // string is null { - // string is null - - length = len; - bufsize = FWDBUFFER + length + 1; - - try - { - string = new wchar_t[bufsize](); - } - catch (const std::bad_alloc& ex) - { - std::cerr << bad_alloc_str << " " << ex.what() << std::endl; - return; - } - - std::wcsncpy (string, s, bufsize); - string[bufsize - 1] = L'\0'; - return; + _insert (len, s); } else { uInt x; - uInt insert_len = uInt(std::wcslen(s)); - - if ( len > insert_len ) - len = insert_len; if ( (length + len + 1) <= bufsize ) { diff --git a/src/fterm.cpp b/src/fterm.cpp index d57f75aa..09ada8ba 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -1259,6 +1259,46 @@ void FTerm::init_termcap_strings (char*& buffer) tcap[i].string = tgetstr(tcap[i].tname, &buffer); } +//---------------------------------------------------------------------- +void FTerm::init_termcap_keys_vt100 (char*& buffer) +{ + // Some terminals (e.g. PuTTY) send vt100 key codes for + // the arrow and function keys. + + char* key_up_string = tgetstr(C_STR("ku"), &buffer); + + if ( (key_up_string && (std::strcmp(key_up_string, CSI "A") == 0)) + || ( TCAP(fc::t_cursor_up) + && (std::strcmp(TCAP(fc::t_cursor_up), CSI "A") == 0) ) ) + { + for (int i = 0; fc::Fkey[i].tname[0] != 0; i++) + { + if ( std::strncmp(fc::Fkey[i].tname, "kux", 3) == 0 ) + fc::Fkey[i].string = C_STR(CSI "A"); // Key up + + if ( std::strncmp(fc::Fkey[i].tname, "kdx", 3) == 0 ) + fc::Fkey[i].string = C_STR(CSI "B"); // Key down + + if ( std::strncmp(fc::Fkey[i].tname, "krx", 3) == 0 ) + fc::Fkey[i].string = C_STR(CSI "C"); // Key right + + if ( std::strncmp(fc::Fkey[i].tname, "klx", 3) == 0 ) + fc::Fkey[i].string = C_STR(CSI "D"); // Key left + + if ( std::strncmp(fc::Fkey[i].tname, "k1X", 3) == 0 ) + fc::Fkey[i].string = C_STR(ESC "OP"); // PF1 + + if ( std::strncmp(fc::Fkey[i].tname, "k2X", 3) == 0 ) + fc::Fkey[i].string = C_STR(ESC "OQ"); // PF2 + + if ( std::strncmp(fc::Fkey[i].tname, "k3X", 3) == 0 ) + fc::Fkey[i].string = C_STR(ESC "OR"); // PF3 + + if ( std::strncmp(fc::Fkey[i].tname, "k4X", 3) == 0 ) + fc::Fkey[i].string = C_STR(ESC "OS"); // PF4 + } + } +} //---------------------------------------------------------------------- void FTerm::init_termcap_keys (char*& buffer) { @@ -1308,42 +1348,8 @@ void FTerm::init_termcap_keys (char*& buffer) fc::Fkey[i].string = C_STR(ESC "Ok"); // Keypad plus sign } - // Some terminals (e.g. PuTTY) send the wrong code for the arrow keys - // http://www.unix.com/shell-programming-scripting/.. - // ..110380-using-arrow-keys-shell-scripts.html - char* key_up_string = tgetstr(C_STR("ku"), &buffer); - - if ( (key_up_string && (std::strcmp(key_up_string, CSI "A") == 0)) - || ( TCAP(fc::t_cursor_up) - && (std::strcmp(TCAP(fc::t_cursor_up), CSI "A") == 0) ) ) - { - for (int i = 0; fc::Fkey[i].tname[0] != 0; i++) - { - if ( std::strncmp(fc::Fkey[i].tname, "kux", 3) == 0 ) - fc::Fkey[i].string = C_STR(CSI "A"); // Key up - - if ( std::strncmp(fc::Fkey[i].tname, "kdx", 3) == 0 ) - fc::Fkey[i].string = C_STR(CSI "B"); // Key down - - if ( std::strncmp(fc::Fkey[i].tname, "krx", 3) == 0 ) - fc::Fkey[i].string = C_STR(CSI "C"); // Key right - - if ( std::strncmp(fc::Fkey[i].tname, "klx", 3) == 0 ) - fc::Fkey[i].string = C_STR(CSI "D"); // Key left - - if ( std::strncmp(fc::Fkey[i].tname, "k1X", 3) == 0 ) - fc::Fkey[i].string = C_STR(ESC "OP"); // PF1 - - if ( std::strncmp(fc::Fkey[i].tname, "k2X", 3) == 0 ) - fc::Fkey[i].string = C_STR(ESC "OQ"); // PF2 - - if ( std::strncmp(fc::Fkey[i].tname, "k3X", 3) == 0 ) - fc::Fkey[i].string = C_STR(ESC "OR"); // PF3 - - if ( std::strncmp(fc::Fkey[i].tname, "k4X", 3) == 0 ) - fc::Fkey[i].string = C_STR(ESC "OS"); // PF4 - } - } + // VT100 key codes for the arrow and function keys + init_termcap_keys_vt100(buffer); } //---------------------------------------------------------------------- @@ -1982,14 +1988,8 @@ void FTerm::init (bool disable_alt_screen) // Save the used xterm font and window title init_captureFontAndTitle(); - if ( isKdeTerminal() ) - setKDECursor(fc::UnderlineCursor); - - if ( isCygwinTerminal() ) - init_cygwin_charmap(); - - if ( isTeraTerm() ) - init_teraterm_charmap(); + // KDE terminal cursor and cygwin + teraterm charmap correction + initTermspecifics(); // Redefine the color palette if ( init_values.color_change ) @@ -2055,6 +2055,19 @@ void FTerm::initOSspecifics() #endif } +//---------------------------------------------------------------------- +void FTerm::initTermspecifics() +{ + if ( isKdeTerminal() ) + setKDECursor(fc::UnderlineCursor); + + if ( isCygwinTerminal() ) + init_cygwin_charmap(); + + if ( isTeraTerm() ) + init_teraterm_charmap(); +} + //---------------------------------------------------------------------- void FTerm::finish() { diff --git a/src/include/final/fstring.h b/src/include/final/fstring.h index 4d6b8338..c3e873ef 100644 --- a/src/include/final/fstring.h +++ b/src/include/final/fstring.h @@ -382,6 +382,7 @@ class FString // Methods void initLength (uInt); void _assign (const wchar_t[]); + void _insert (uInt, const wchar_t[]); void _insert (uInt, uInt, const wchar_t[]); void _remove (uInt, uInt); char* wc_to_c_str (const wchar_t[]) const; diff --git a/src/include/final/fterm.h b/src/include/final/fterm.h index e7e2ef55..2d9d8eb2 100644 --- a/src/include/final/fterm.h +++ b/src/include/final/fterm.h @@ -387,6 +387,7 @@ class FTerm static void init_termcap_booleans(); static void init_termcap_numerics(); static void init_termcap_strings (char*&); + static void init_termcap_keys_vt100 (char*&); static void init_termcap_keys (char*&); static void init_OptiMove(); static void init_OptiAttr(); @@ -412,6 +413,7 @@ class FTerm void deallocationValues(); void init (bool); void initOSspecifics(); + void initTermspecifics(); void finish(); void finishOSspecifics1(); void finish_encoding(); diff --git a/src/include/final/ftermdata.h b/src/include/final/ftermdata.h index 201e11e8..47ef2bf6 100644 --- a/src/include/final/ftermdata.h +++ b/src/include/final/ftermdata.h @@ -181,6 +181,9 @@ inline FTermData::FTermData() , termfilename() , xterm_font() , xterm_title() +#if DEBUG + , framebuffer_bpp(-1) +#endif { // Initialize arrays with '\0' std::fill_n (termtype, sizeof(termtype), '\0'); @@ -370,7 +373,7 @@ inline void FTermData::setTermFileName (const char file_name[]) return; std::strncpy (termfilename, file_name, sizeof(termfilename)); - termtype[sizeof(termfilename) - 1] = '\0'; + termfilename[sizeof(termfilename) - 1] = '\0'; } //---------------------------------------------------------------------- diff --git a/test/ftermcapquirks-test.cpp b/test/ftermcapquirks-test.cpp index c3b184fa..48f3f655 100644 --- a/test/ftermcapquirks-test.cpp +++ b/test/ftermcapquirks-test.cpp @@ -287,7 +287,6 @@ void FTermcapQuirksTest::generalTest() , C_STR(CSI "29m") ); CPPUNIT_ASSERT_CSTRING ( printSequence(caps[finalcut::fc::t_enter_ca_mode].string).c_str() , C_STR("Esc 7 Esc [ ? 4 7 h ") ); - //delete[] caps; } //----------------------------------------------------------------------