2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-11-28 15:47:07 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
|
|
|
|
|
|
|
|
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
#pragma hdrstop
|
|
|
|
#include "../idlib/precompiled.h"
|
|
|
|
#include "ConsoleHistory.h"
|
|
|
|
|
|
|
|
idConsoleHistory consoleHistory;
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* HISTORY_FILE_NAME = "consoleHistory.txt";
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idConsoleHistory::AddToHistory
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idConsoleHistory::AddToHistory( const char* line, bool writeHistoryFile )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// empty lines never modify history
|
2012-11-28 15:47:07 +00:00
|
|
|
if( line == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* s;
|
|
|
|
for( s = line; *s != '\0'; s++ )
|
|
|
|
{
|
|
|
|
if( *s > ' ' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *s == '\0' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// repeating the last command doesn't add to the list
|
2012-11-28 15:47:07 +00:00
|
|
|
if( historyLines[( numHistory - 1 ) & ( COMMAND_HISTORY - 1 )].Icmp( line ) == 0 )
|
|
|
|
{
|
|
|
|
if( historyLines[returnLine & ( COMMAND_HISTORY - 1 )].Icmp( line ) == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// the command was retrieved from the history, so
|
|
|
|
// move the up point down so that up arrow will retrieve the same
|
|
|
|
// command again.
|
|
|
|
upPoint = returnLine;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// the command was typed again, so leave the up/down points alone
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If we had previously retrieved older history commands with
|
|
|
|
// up arrow, the up/down point will be reset to the end where
|
|
|
|
// this command is added.
|
|
|
|
upPoint = numHistory;
|
|
|
|
returnLine = numHistory;
|
|
|
|
downPoint = numHistory + 1;
|
|
|
|
// //mem.PushHeap(); // go to the system heap, not the current map heap
|
|
|
|
historyLines[numHistory & ( COMMAND_HISTORY - 1 )] = line;
|
|
|
|
// //mem.PopHeap();
|
|
|
|
numHistory++;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// write the history file to disk
|
2012-11-28 15:47:07 +00:00
|
|
|
if( writeHistoryFile )
|
|
|
|
{
|
|
|
|
idFile* f = fileSystem->OpenFileWrite( HISTORY_FILE_NAME );
|
|
|
|
if( f != NULL )
|
|
|
|
{
|
|
|
|
for( int i = numHistory - COMMAND_HISTORY; i < numHistory; i++ )
|
|
|
|
{
|
|
|
|
if( i < 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
f->Printf( "%s\n", historyLines[i & ( COMMAND_HISTORY - 1 )].c_str() );
|
|
|
|
}
|
|
|
|
delete f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idConsoleHistory::RetrieveFromHistory
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idStr idConsoleHistory::RetrieveFromHistory( bool backward )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// if there are no commands in the history
|
2012-11-28 15:47:07 +00:00
|
|
|
if( numHistory == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return idStr( "" );
|
|
|
|
}
|
|
|
|
// move the history point
|
2012-11-28 15:47:07 +00:00
|
|
|
if( backward )
|
|
|
|
{
|
|
|
|
if( upPoint < numHistory - COMMAND_HISTORY || upPoint < 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return idStr( "" );
|
|
|
|
}
|
|
|
|
returnLine = upPoint;
|
|
|
|
downPoint = upPoint + 1;
|
|
|
|
upPoint--;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( downPoint >= numHistory )
|
|
|
|
{
|
2012-12-21 03:52:56 +00:00
|
|
|
// DG: without this you'll get the last-but-one command when pressing UP
|
|
|
|
upPoint = downPoint - 1;
|
|
|
|
// DG end
|
2012-11-26 18:58:24 +00:00
|
|
|
return idStr( "" );
|
|
|
|
}
|
|
|
|
returnLine = downPoint;
|
|
|
|
upPoint = downPoint - 1;
|
|
|
|
downPoint++;
|
|
|
|
}
|
|
|
|
return historyLines[returnLine & ( COMMAND_HISTORY - 1 )];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idConsoleHistory::LoadHistoryFile
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idConsoleHistory::LoadHistoryFile()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer lex;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( lex.LoadFile( HISTORY_FILE_NAME, false ) )
|
|
|
|
{
|
|
|
|
while( 1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr line;
|
|
|
|
lex.ParseCompleteLine( line );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( line.IsEmpty() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
line.StripTrailingWhitespace(); // remove the \n
|
|
|
|
AddToHistory( line, false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idConsoleHistory::PrintHistory
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idConsoleHistory::PrintHistory()
|
|
|
|
{
|
|
|
|
for( int i = numHistory - COMMAND_HISTORY; i < numHistory; i++ )
|
|
|
|
{
|
|
|
|
if( i < 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
idLib::Printf( "%c%c%c%4i: %s\n",
|
|
|
|
i == upPoint ? 'U' : ' ',
|
|
|
|
i == downPoint ? 'D' : ' ',
|
|
|
|
i == returnLine ? 'R' : ' ',
|
|
|
|
i, historyLines[i & ( COMMAND_HISTORY - 1 )].c_str() );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idConsoleHistory::ClearHistory
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idConsoleHistory::ClearHistory()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
upPoint = 0;
|
|
|
|
downPoint = 0;
|
|
|
|
returnLine = 0;
|
|
|
|
numHistory = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
history
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
CONSOLE_COMMAND_SHIP( history, "Displays the console command history", 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
consoleHistory.PrintHistory();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
clearHistory
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
CONSOLE_COMMAND_SHIP( clearHistory, "Clears the console history", 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
consoleHistory.ClearHistory();
|
|
|
|
}
|