Console.cpp

00001 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
00002 /* 
00003  * Copyright (C) 2008 Micha Hersch, EPFL
00004  * RobotCub Consortium, European Commission FP6 Project IST-004370
00005  * email:   micha.hersch@robotcub.org
00006  * website: www.robotcub.org
00007  * Permission is granted to copy, distribute, and/or modify this program
00008  * under the terms of the GNU General Public License, version 2 or any
00009  * later version published by the Free Software Foundation.
00010  *
00011  * A copy of the license can be found at
00012  * http://www.robotcub.org/icub/license/gpl.txt
00013  *
00014  * This program is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00017  * Public License for more details
00018  */
00019 #include "Console.h"
00020 
00021 #include <iostream>
00022 using namespace std;
00023 
00024 Command::Command(string name){
00025   m_Name = name;
00026 }
00027 Command::~Command(){
00028 }
00029 int Command::Execute(string args){
00030   return 0;
00031 }
00032 
00033 streambuf * Console::m_Stdout = NULL;
00034 
00035 Console::Console(){
00036   if(m_Stdout == NULL)
00037     m_Stdout = cout.rdbuf();
00038 
00039   Free();
00040 }
00041 Console::~Console(){
00042   Free();
00043 
00044   if(cout.rdbuf() == GetStreamBuf())
00045     cout.rdbuf(m_Stdout);
00046 }
00047 
00048 void Console::Clear(){
00049   m_Lines.clear();
00050   m_History.clear();
00051   m_CurrY       = 0;
00052   m_CurrX       = 0;
00053   m_CurrCmd     = "";
00054   m_CurrHistory = 0;
00055   m_oStream.str("");
00056 }
00057 
00058 void Console::Free(){
00059   m_Commands.clear();
00060   m_MaxLines    = 20;
00061   m_MaxHistory  = 20;
00062   Clear();
00063 }
00064 
00065 void Console::SetMaxLines(int size){
00066   if(size<=0)
00067     return;
00068 
00069   if(size>m_MaxLines)
00070     m_MaxLines = size;
00071 
00072 
00073   while(size<m_MaxLines){
00074     if(m_Lines.size()>size)
00075       m_Lines.erase(m_Lines.begin());
00076     m_MaxLines--;
00077   }
00078 }
00079 
00080 void Console::SetMaxHistory(int size){
00081   if(size<=0)
00082     return;
00083   while(size<m_MaxHistory){
00084     m_History.erase(m_History.begin());
00085     m_CurrHistory--;
00086   }
00087 }
00088 
00089 void Console::AddLine(string line){
00090   if(m_Lines.size() >= m_MaxLines){
00091     m_Lines.erase(m_Lines.begin());
00092     m_Lines.push_back(line);
00093   }else{
00094     m_Lines.push_back(line);
00095   }
00096 }
00097 
00098 void Console::AddHistory(string line){
00099   if(m_History.size() >= m_MaxHistory){
00100     m_History.erase(m_History.begin());
00101     m_History.push_back(line);
00102   }else{
00103     m_History.push_back(line);
00104   }
00105   m_CurrHistory = m_History.size()-1;
00106 }
00107 
00108 void Console::AddChar(char c){
00109   string s(1,c);
00110   m_CurrCmd.append(s);
00111 }
00112 
00113 void Console::EraseChar(){
00114   if(m_CurrCmd.size()>0)
00115     m_CurrCmd = m_CurrCmd.substr(0,m_CurrCmd.size()-1);
00116 }
00117 
00118 void Console::Accept(){
00119   if(m_CurrCmd.size()>0)
00120     AddHistory(m_CurrCmd);  
00121 
00122   int cmdEnd = m_CurrCmd.find_first_of(" ");
00123   string cmd;
00124   string args;
00125 
00126   if(cmdEnd == string::npos){
00127     cmd   = m_CurrCmd;
00128     args  = "";
00129   }
00130   else{
00131     cmd   = m_CurrCmd.substr(0,cmdEnd);
00132     args  = m_CurrCmd.substr(cmdEnd+1);
00133   }
00134 
00135   pCommand pCmd = FindCommand(cmd);
00136   if(pCmd!=NULL){
00137     string s = "> ";
00138     s.append(m_CurrCmd);
00139     Print(s);
00140     pCmd->Execute(args);    
00141   }else{
00142     if(cmd.size()==0){
00143       Print(cmd);
00144     }else{
00145       string s = "\"";
00146       s.append(cmd);
00147       s = s.append("\": Command not found");
00148       Print(s);
00149     }
00150   }
00151 
00152   m_CurrCmd = "";
00153 }
00154 
00155 pCommand  Console::FindCommand(string name){
00156   int i;
00157   for(i=0;i<m_Commands.size();i++){
00158     if(m_Commands[i]->m_Name.compare(name)==0)
00159       return m_Commands[i];
00160   }
00161   return NULL;
00162 }
00163 
00164 int Console::AutoCompletion(){
00165   int i;
00166   int len = m_CurrCmd.size();
00167 
00168   m_AutoMatches.clear();
00169 
00170   for(i=0;i<m_Commands.size();i++){
00171     if(m_Commands[i]->m_Name.substr(0,len).compare(m_CurrCmd)==0)
00172       m_AutoMatches.push_back(i);
00173   }
00174 
00175   if(m_AutoMatches.size()==1){
00176     m_CurrCmd = m_Commands[m_AutoMatches[0]]->m_Name;
00177     m_CurrCmd.append(" ");
00178   }
00179 
00180   return m_AutoMatches.size();
00181 }
00182 
00183 void Console::AddCommand(pCommand cmd){
00184   if(cmd==NULL)
00185     return;
00186   m_Commands.push_back(cmd);
00187 }
00188 
00189 void Console::HistoryPrev(){
00190   if(m_History.size()>0){
00191     m_CurrCmd = m_History[m_CurrHistory];
00192     m_CurrHistory--;
00193     if(m_CurrHistory<0)
00194       m_CurrHistory=0;
00195   }
00196 }
00197 void Console::HistoryNext(){
00198   if(m_History.size()>0){
00199     m_CurrHistory++;
00200     if(m_CurrHistory<m_History.size()){
00201       m_CurrCmd = m_History[m_CurrHistory];
00202     }else{
00203       m_CurrCmd = "";
00204       m_CurrHistory--;
00205     }
00206   }
00207 }
00208 
00209 void Console::Print(string line){
00210   AddLine(line);  
00211 }
00212 
00213 void Console::Update(){
00214   string s = m_oStream.str();
00215   
00216   int cpos = 0;
00217   int opos = 0;
00218 
00219   if(s.size()>0){
00220     //while(cpos!=string::npos){
00221     for(int i=0;i<s.size();i++){
00222       opos = cpos;
00223       cpos = s.find("\n",opos);
00224       string ss = s.substr(opos,cpos-opos);
00225       if(ss.size()>0)
00226         Print(ss);        
00227       if(cpos==string::npos)
00228         break;
00229       cpos++;
00230     }
00231     m_oStream.str("");
00232   }
00233 }
00234 streambuf *Console::GetStreamBuf(){
00235   return m_oStream.rdbuf();
00236 }
00237 
00238 void Console::SetStdout(){
00239   cout.rdbuf(GetStreamBuf());  
00240 }
00241 /*
00242 ofstream outFile("some_file_name.dat");//create output file object
00243 streambuf * cout_backup=cout.rdbuf();//create backup of standard out
00244 cout.rdbuf(outFile.rdbuf());//assign cout stream to outFile stream
00245 cout<<"hello!"<<endl;//call any function that calls cout
00246 cout.rdbuf(cout_backup);//restore the standard stream
00247 outFile.close();//best to be neat and tidy about these things
00248 */
 All Data Structures Functions Variables

Generated on Wed Sep 22 16:51:25 2010 for Body_Schema_Learning by  doxygen 1.6.1