ConsoleHistory.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "ConsoleHistory.h"
  23. idConsoleHistory consoleHistory;
  24. const char * HISTORY_FILE_NAME = "consoleHistory.txt";
  25. /*
  26. ========================
  27. idConsoleHistory::AddToHistory
  28. ========================
  29. */
  30. void idConsoleHistory::AddToHistory( const char *line, bool writeHistoryFile ) {
  31. // empty lines never modify history
  32. if ( line == NULL ) {
  33. return;
  34. }
  35. const char *s;
  36. for ( s = line; *s != '\0'; s++ ) {
  37. if ( *s > ' ' ) {
  38. break;
  39. }
  40. }
  41. if ( *s == '\0' ) {
  42. return;
  43. }
  44. // repeating the last command doesn't add to the list
  45. if ( historyLines[( numHistory - 1 ) & ( COMMAND_HISTORY - 1 )].Icmp( line ) == 0 ) {
  46. if ( historyLines[returnLine & ( COMMAND_HISTORY - 1 )].Icmp( line ) == 0 ) {
  47. // the command was retrieved from the history, so
  48. // move the up point down so that up arrow will retrieve the same
  49. // command again.
  50. upPoint = returnLine;
  51. } else {
  52. // the command was typed again, so leave the up/down points alone
  53. }
  54. return;
  55. }
  56. // If we had previously retrieved older history commands with
  57. // up arrow, the up/down point will be reset to the end where
  58. // this command is added.
  59. upPoint = numHistory;
  60. returnLine = numHistory;
  61. downPoint = numHistory + 1;
  62. // //mem.PushHeap(); // go to the system heap, not the current map heap
  63. historyLines[numHistory & ( COMMAND_HISTORY - 1 )] = line;
  64. // //mem.PopHeap();
  65. numHistory++;
  66. // write the history file to disk
  67. if ( writeHistoryFile ) {
  68. idFile *f = fileSystem->OpenFileWrite( HISTORY_FILE_NAME );
  69. if ( f != NULL ) {
  70. for ( int i = numHistory - COMMAND_HISTORY; i < numHistory; i++ ) {
  71. if ( i < 0 ) {
  72. continue;
  73. }
  74. f->Printf( "%s\n", historyLines[i & ( COMMAND_HISTORY - 1 )].c_str() );
  75. }
  76. delete f;
  77. }
  78. }
  79. }
  80. /*
  81. ========================
  82. idConsoleHistory::RetrieveFromHistory
  83. ========================
  84. */
  85. idStr idConsoleHistory::RetrieveFromHistory( bool backward ) {
  86. // if there are no commands in the history
  87. if ( numHistory == 0 ) {
  88. return idStr( "" );
  89. }
  90. // move the history point
  91. if ( backward ) {
  92. if ( upPoint < numHistory - COMMAND_HISTORY || upPoint < 0 ) {
  93. return idStr( "" );
  94. }
  95. returnLine = upPoint;
  96. downPoint = upPoint + 1;
  97. upPoint--;
  98. } else {
  99. if ( downPoint >= numHistory ) {
  100. return idStr( "" );
  101. }
  102. returnLine = downPoint;
  103. upPoint = downPoint - 1;
  104. downPoint++;
  105. }
  106. return historyLines[returnLine & ( COMMAND_HISTORY - 1 )];
  107. }
  108. /*
  109. ========================
  110. idConsoleHistory::LoadHistoryFile
  111. ========================
  112. */
  113. void idConsoleHistory::LoadHistoryFile() {
  114. idLexer lex;
  115. if ( lex.LoadFile( HISTORY_FILE_NAME, false ) ) {
  116. while( 1 ) {
  117. idStr line;
  118. lex.ParseCompleteLine( line );
  119. if ( line.IsEmpty() ) {
  120. break;
  121. }
  122. line.StripTrailingWhitespace(); // remove the \n
  123. AddToHistory( line, false );
  124. }
  125. }
  126. }
  127. /*
  128. ========================
  129. idConsoleHistory::PrintHistory
  130. ========================
  131. */
  132. void idConsoleHistory::PrintHistory() {
  133. for ( int i = numHistory - COMMAND_HISTORY; i < numHistory; i++ ) {
  134. if ( i < 0 ) {
  135. continue;
  136. }
  137. idLib::Printf( "%c%c%c%4i: %s\n",
  138. i == upPoint ? 'U' : ' ',
  139. i == downPoint ? 'D' : ' ',
  140. i == returnLine ? 'R' : ' ',
  141. i, historyLines[i & ( COMMAND_HISTORY - 1 )].c_str() );
  142. }
  143. }
  144. /*
  145. ========================
  146. idConsoleHistory::ClearHistory
  147. ========================
  148. */
  149. void idConsoleHistory::ClearHistory() {
  150. upPoint = 0;
  151. downPoint = 0;
  152. returnLine = 0;
  153. numHistory = 0;
  154. }
  155. /*
  156. ========================
  157. history
  158. ========================
  159. */
  160. CONSOLE_COMMAND_SHIP( history, "Displays the console command history", 0 ) {
  161. consoleHistory.PrintHistory();
  162. }
  163. /*
  164. ========================
  165. clearHistory
  166. ========================
  167. */
  168. CONSOLE_COMMAND_SHIP( clearHistory, "Clears the console history", 0 ) {
  169. consoleHistory.ClearHistory();
  170. }