diff --git a/aql2/src/prompt/ReadlineLineInterface.cpp b/aql2/src/prompt/ReadlineLineInterface.cpp index e1013ec93f04bc55a820bfd134c3c17e22df91c1..29dff4754f81b14ae20156403c7147445a7604bf 100644 --- a/aql2/src/prompt/ReadlineLineInterface.cpp +++ b/aql2/src/prompt/ReadlineLineInterface.cpp @@ -8,6 +8,9 @@ #include "ReadlineLineInterface.h" #include <cctype> +#include <iostream> + +#include <alib/string> #include <readline/readline.h> diff --git a/aql2/src/prompt/ReadlinePromptHistory.cpp b/aql2/src/prompt/ReadlinePromptHistory.cpp index 69de4c4ebba7193bb0a131181c92b963db030bbb..9181a68d6f6959788edaadbae97fca7eb634d05e 100644 --- a/aql2/src/prompt/ReadlinePromptHistory.cpp +++ b/aql2/src/prompt/ReadlinePromptHistory.cpp @@ -7,7 +7,90 @@ #include "ReadlinePromptHistory.h" +#include <readline/history.h> + +#include <alib/string> +#include <cstring> + char ReadlinePromptHistory::esc_char [] = { '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\' }; char ReadlinePromptHistory::essc_str [] = { 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\' }; size_t ReadlinePromptHistory::esc_char_size = sizeof ( ReadlinePromptHistory::esc_char ); +char * ReadlinePromptHistory::descape ( const char * buffer ) { + size_t l = strlen ( buffer ); + + char * dest = ( char * ) malloc ( ( l + 1 ) * sizeof ( char ) ); + char * ptr = dest; + for ( size_t i = 0; i < l; ++ i ) { + if ( buffer [ i ] == '\\' ) { + ++ i; + size_t j = std::find ( essc_str, essc_str + esc_char_size, buffer [ i ] ) - essc_str; + if ( j == esc_char_size ) { + free ( dest ); + return strdup ( buffer ); + } + * ptr ++ = esc_char [ j ]; + } else { + * ptr ++ = buffer [ i ]; + } + } + * ptr = '\0'; + + return dest; +} + +char * ReadlinePromptHistory::escape ( const char * buffer){ + size_t l = strlen ( buffer ); + + char * dest = ( char * ) malloc ( ( l * 2 + 1 ) * sizeof ( char ) ); + char * ptr = dest; + for ( size_t i = 0; i < l; ++ i ) { + size_t j = std::find ( esc_char, esc_char + esc_char_size, buffer [ i ] ) - esc_char; + if ( j == esc_char_size ) { + * ptr ++ = buffer [ i ]; + } else { + * ptr ++ = '\\'; + * ptr ++ = essc_str [ j ]; + } + } + * ptr = '\0'; + + return dest; +} + +template < class Callable > +void ReadlinePromptHistory::history_transform ( Callable callable ) { + HIST_ENTRY ** history = history_list ( ); + if ( history ) { + int i = 0; + while ( * history ) { + char * tmp = callable ( ( * history )->line ); + replace_history_entry ( i, tmp, ( * history )->data ); + free ( tmp ); + ++ history; + ++ i; + } + } +} + +ReadlinePromptHistory::ReadlinePromptHistory ( std::string history_file ) : m_history_file ( std::move ( history_file ) ) { + ReadlinePromptHistory::readHistory ( m_history_file ); +} + +ReadlinePromptHistory::~ ReadlinePromptHistory ( ) { + ReadlinePromptHistory::writeHistory ( m_history_file ); +} + +void ReadlinePromptHistory::readHistory ( const std::string & history_file ) { + read_history ( history_file.c_str ( ) ); + ReadlinePromptHistory::history_transform ( ReadlinePromptHistory::descape ); +} + +void ReadlinePromptHistory::writeHistory ( const std::string & history_file ) { + ReadlinePromptHistory::history_transform ( ReadlinePromptHistory::escape ); + write_history ( history_file.c_str ( ) ); +} + +void ReadlinePromptHistory::addHistory ( const std::string & line ) { + add_history ( line.c_str ( ) ); +} diff --git a/aql2/src/prompt/ReadlinePromptHistory.h b/aql2/src/prompt/ReadlinePromptHistory.h index e0a1db9fa7d8db6b6a998a0ee19e469758dfc948..9f38871fda0175f87eb8f36948cc24e5db49720b 100644 --- a/aql2/src/prompt/ReadlinePromptHistory.h +++ b/aql2/src/prompt/ReadlinePromptHistory.h @@ -1,5 +1,5 @@ /* - * ReadlinePromptCompletion.h + * ReadlinePromptHistory.h * * Created on: 26. 1. 2019 * Author: Jan Travnicek @@ -8,79 +8,24 @@ #ifndef _READLINE_PROMPT_HISTORY_H #define _READLINE_PROMPT_HISTORY_H -#include <cstring> -#include <alib/string> - -#include <readline/history.h> +#include <string> class ReadlinePromptHistory { static char esc_char []; static char essc_str []; static size_t esc_char_size; - static char * descape ( const char * buffer ) { - size_t l = strlen ( buffer ); - - char * dest = ( char * ) malloc ( ( l + 1 ) * sizeof ( char ) ); - char * ptr = dest; - for ( size_t i = 0; i < l; ++ i ) { - if ( buffer [ i ] == '\\' ) { - ++ i; - size_t j = std::find ( essc_str, essc_str + esc_char_size, buffer [ i ] ) - essc_str; - if ( j == esc_char_size ) { - free ( dest ); - return strdup ( buffer ); - } - * ptr ++ = esc_char [ j ]; - } else { - * ptr ++ = buffer [ i ]; - } - } - * ptr = '\0'; - - return dest; - } - - static char * escape ( const char * buffer){ - size_t l = strlen ( buffer ); - - char * dest = ( char * ) malloc ( ( l * 2 + 1 ) * sizeof ( char ) ); - char * ptr = dest; - for ( size_t i = 0; i < l; ++ i ) { - size_t j = std::find ( esc_char, esc_char + esc_char_size, buffer [ i ] ) - esc_char; - if ( j == esc_char_size ) { - * ptr ++ = buffer [ i ]; - } else { - * ptr ++ = '\\'; - * ptr ++ = essc_str [ j ]; - } - } - * ptr = '\0'; - - return dest; - } + static char * descape ( const char * buffer ); + + static char * escape ( const char * buffer); template < class Callable > - static void history_transform ( Callable callable ) { - HIST_ENTRY ** history = history_list ( ); - if ( history ) { - int i = 0; - while ( * history ) { - char * tmp = callable ( ( * history )->line ); - replace_history_entry ( i, tmp, ( * history )->data ); - free ( tmp ); - ++ history; - ++ i; - } - } - } + static void history_transform ( Callable callable ); std::string m_history_file; public: - ReadlinePromptHistory ( std::string history_file ) : m_history_file ( std::move ( history_file ) ) { - ReadlinePromptHistory::readHistory ( m_history_file ); - } + ReadlinePromptHistory ( std::string history_file ); ReadlinePromptHistory ( const ReadlinePromptHistory & ) = delete; @@ -90,23 +35,13 @@ public: ReadlinePromptHistory & operator = ( ReadlinePromptHistory && ) = delete; - ~ ReadlinePromptHistory ( ) { - ReadlinePromptHistory::writeHistory ( m_history_file ); - } + ~ ReadlinePromptHistory ( ); - static void readHistory ( const std::string & history_file ) { - read_history ( history_file.c_str ( ) ); - ReadlinePromptHistory::history_transform ( ReadlinePromptHistory::descape ); - } + static void readHistory ( const std::string & history_file ); - static void writeHistory ( const std::string & history_file ) { - ReadlinePromptHistory::history_transform ( ReadlinePromptHistory::escape ); - write_history ( history_file.c_str ( ) ); - } + static void writeHistory ( const std::string & history_file ); - static void addHistory ( const std::string & line ) { - add_history ( line.c_str ( ) ); - } + static void addHistory ( const std::string & line ); };