00001 #include <malloc.h>
00002 #include "command_line.h"
00003 #ifdef HAVE_LIBREADLINE
00004 #include <readline/readline.h>
00005 #include <readline/history.h>
00006 #endif //READLINE
00007
00008 void CommandLine::Init(const char* _history_file)
00009 {
00010 history_file = NULL;
00011 #ifdef HAVE_READLINE_HISTORY
00012 if(_history_file)
00013 {
00014 history_file = strdup(_history_file);
00015 read_history(history_file);
00016 }
00017 #endif
00018 }
00019
00020 CommandLine::~CommandLine()
00021 {
00022 #ifdef HAVE_READLINE_HISTORY
00023 if(history_file)
00024 {
00025 write_history(history_file);
00026 free(history_file);
00027 }
00028 #endif
00029 }
00030
00031 std::string CommandLine::GetLine(std::string prompt)
00032 {
00033 char* line = NULL;
00034 #ifdef HAVE_LIBREADLINE
00035 while(true)
00036 {
00037 line = readline(prompt.c_str());
00038 if(line != NULL && *line == '\0')
00039 continue;
00040 break;
00041 }
00042 if(line)
00043 add_history(line);
00044 #else
00045 size_t len = 0;
00046 printf("%s", prompt.c_str());
00047
00048 if(getline(&line, &len, stdin) == -1)
00049 return std::string("");
00050
00051
00052 if(*line)
00053 line[strlen(line) - 1] = '\0';
00054 #endif
00055 if(line)
00056 {
00057 std::string str(line);
00058 free(line);
00059 return str;
00060 }
00061 return std::string("");
00062 }