r522 - in trunk/code: client qcommon

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Mon Jan 23 23:35:19 EST 2006


Author: tma
Date: 2006-01-23 23:35:19 -0500 (Mon, 23 Jan 2006)
New Revision: 522

Modified:
   trunk/code/client/cl_console.c
   trunk/code/client/cl_keys.c
   trunk/code/client/client.h
   trunk/code/qcommon/common.c
   trunk/code/qcommon/cvar.c
   trunk/code/qcommon/q_shared.c
   trunk/code/qcommon/q_shared.h
Log:
* Persistent console history


Modified: trunk/code/client/cl_console.c
===================================================================
--- trunk/code/client/cl_console.c	2006-01-23 05:14:59 UTC (rev 521)
+++ trunk/code/client/cl_console.c	2006-01-24 04:35:19 UTC (rev 522)
@@ -321,6 +321,7 @@
 		Field_Clear( &historyEditLines[i] );
 		historyEditLines[i].widthInChars = g_console_field_width;
 	}
+	CL_LoadConsoleHistory( );
 
 	Cmd_AddCommand ("toggleconsole", Con_ToggleConsole_f);
 	Cmd_AddCommand ("messagemode", Con_MessageMode_f);

Modified: trunk/code/client/cl_keys.c
===================================================================
--- trunk/code/client/cl_keys.c	2006-01-23 05:14:59 UTC (rev 521)
+++ trunk/code/client/cl_keys.c	2006-01-24 04:35:19 UTC (rev 522)
@@ -486,7 +486,7 @@
 		// if not in the game explicitly prepend a slash if needed
 		if ( cls.state != CA_ACTIVE && g_consoleField.buffer[0] != '\\' 
 			&& g_consoleField.buffer[0] != '/' ) {
-			char	temp[MAX_STRING_CHARS];
+			char	temp[MAX_EDIT_LINE-1];
 
 			Q_strncpyz( temp, g_consoleField.buffer, sizeof( temp ) );
 			Com_sprintf( g_consoleField.buffer, sizeof( g_consoleField.buffer ), "\\%s", temp );
@@ -519,6 +519,8 @@
 
 		g_consoleField.widthInChars = g_console_field_width;
 
+		CL_SaveConsoleHistory( );
+
 		if ( cls.state == CA_DISCONNECTED ) {
 			SCR_UpdateScreen ();	// force an update, because the command
 		}							// may take some time
@@ -1256,3 +1258,107 @@
 	}
 }
 
+// This must not exceed MAX_CMD_LINE
+#define MAX_CONSOLE_SAVE_BUFFER 1024
+static char consoleSaveBuffer[ MAX_CONSOLE_SAVE_BUFFER ];
+
+/*
+================
+CL_LoadConsoleHistory
+
+Load the console history from cl_consoleHistory
+================
+*/
+void CL_LoadConsoleHistory( void )
+{
+	char		*token, *text_p;
+	int			i, numChars, numLines = 0;
+	cvar_t	*cv;
+
+	cv = Cvar_Get( "cl_consoleHistory", "", CVAR_ARCHIVE|CVAR_ROM );
+	Q_strncpyz( consoleSaveBuffer, cv->string, MAX_CONSOLE_SAVE_BUFFER );
+
+	text_p = consoleSaveBuffer;
+
+	for( i = COMMAND_HISTORY - 1; i >= 0; i-- )
+	{
+		if( !*( token = COM_Parse( &text_p ) ) )
+			break;
+
+		historyEditLines[ i ].cursor = atoi( token );
+
+		if( !*( token = COM_Parse( &text_p ) ) )
+			break;
+
+		historyEditLines[ i ].scroll = atoi( token );
+
+		if( !*( token = COM_Parse( &text_p ) ) )
+			break;
+
+		numChars = atoi( token );
+		text_p++;
+		if( numChars > ( strlen( consoleSaveBuffer ) -  ( text_p - consoleSaveBuffer ) ) )
+		{
+			Com_DPrintf( S_COLOR_YELLOW "WARNING: probable corrupt history\n" );
+			break;
+		}
+		Com_Memcpy( historyEditLines[ i ].buffer,
+				text_p, numChars );
+		historyEditLines[ i ].buffer[ numChars ] = '\0';
+		text_p += numChars;
+
+		numLines++;
+	}
+
+	memmove( &historyEditLines[ 0 ], &historyEditLines[ i + 1 ],
+			numLines * sizeof( field_t ) );
+	for( i = numLines; i < COMMAND_HISTORY; i++ )
+		Field_Clear( &historyEditLines[ i ] );
+
+	historyLine = nextHistoryLine = numLines;
+}
+
+/*
+================
+CL_SaveConsoleHistory
+
+Save the console history into the cvar cl_consoleHistory
+so that it persists across invocations of q3
+================
+*/
+void CL_SaveConsoleHistory( void )
+{
+	int i;
+	int	lineLength, saveBufferLength, additionalLength;
+
+	consoleSaveBuffer[ 0 ] = '\0';
+
+	i = ( nextHistoryLine - 1 ) % COMMAND_HISTORY;
+	do
+	{
+		if( historyEditLines[ i ].buffer[ 0 ] )
+		{
+			lineLength = strlen( historyEditLines[ i ].buffer );
+			saveBufferLength = strlen( consoleSaveBuffer );
+
+			//ICK "seta cl_consoleHistory " + "%d %d %d  " = 23 + 13 = 36
+			additionalLength = lineLength + 36;
+
+			if( saveBufferLength + additionalLength < MAX_CONSOLE_SAVE_BUFFER )
+			{
+				Q_strcat( consoleSaveBuffer, MAX_CONSOLE_SAVE_BUFFER,
+						va( "%d %d %d %s ",
+						historyEditLines[ i ].cursor,
+						historyEditLines[ i ].scroll,
+						lineLength,
+						historyEditLines[ i ].buffer ) );
+			}
+			else
+				break;
+		}
+		i = ( i - 1 + COMMAND_HISTORY ) % COMMAND_HISTORY;
+	}
+	while( i != ( nextHistoryLine - 1 ) % COMMAND_HISTORY );
+
+	Cvar_Set( "cl_consoleHistory", consoleSaveBuffer );
+}

Modified: trunk/code/client/client.h
===================================================================
--- trunk/code/client/client.h	2006-01-23 05:14:59 UTC (rev 521)
+++ trunk/code/client/client.h	2006-01-24 04:35:19 UTC (rev 522)
@@ -452,6 +452,8 @@
 void Con_Bottom( void );
 void Con_Close( void );
 
+void CL_LoadConsoleHistory( void );
+void CL_SaveConsoleHistory( void );
 
 //
 // cl_scrn.c

Modified: trunk/code/qcommon/common.c
===================================================================
--- trunk/code/qcommon/common.c	2006-01-23 05:14:59 UTC (rev 521)
+++ trunk/code/qcommon/common.c	2006-01-24 04:35:19 UTC (rev 522)
@@ -2965,8 +2965,11 @@
 ===============
 */
 static void PrintCvarMatches( const char *s ) {
+	char value[ TRUNCATE_LENGTH ];
+
 	if ( !Q_stricmpn( s, shortestMatch, strlen( shortestMatch ) ) ) {
-		Com_Printf( "    %s = \"%s\"\n", s, Cvar_VariableString( s ) );
+		Com_TruncateLongString( value, Cvar_VariableString( s ) );
+		Com_Printf( "    %s = \"%s\"\n", s, value );
 	}
 }
 

Modified: trunk/code/qcommon/cvar.c
===================================================================
--- trunk/code/qcommon/cvar.c	2006-01-23 05:14:59 UTC (rev 521)
+++ trunk/code/qcommon/cvar.c	2006-01-24 04:35:19 UTC (rev 522)
@@ -469,7 +469,10 @@
 ============
 */
 qboolean Cvar_Command( void ) {
-	cvar_t			*v;
+	cvar_t	*v;
+	char		string[ TRUNCATE_LENGTH ];
+	char		resetString[ TRUNCATE_LENGTH ];
+	char		latchedString[ TRUNCATE_LENGTH ];
 
 	// check variables
 	v = Cvar_FindVar (Cmd_Argv(0));
@@ -479,9 +482,13 @@
 
 	// perform a variable print or set
 	if ( Cmd_Argc() == 1 ) {
-		Com_Printf ("\"%s\" is:\"%s" S_COLOR_WHITE "\" default:\"%s" S_COLOR_WHITE "\"\n", v->name, v->string, v->resetString );
+		Com_TruncateLongString( string, v->string );
+		Com_TruncateLongString( resetString, v->resetString );
+		Com_Printf ("\"%s\" is:\"%s" S_COLOR_WHITE "\" default:\"%s" S_COLOR_WHITE "\"\n",
+				v->name, string, resetString );
 		if ( v->latchedString ) {
-			Com_Printf( "latched: \"%s\"\n", v->latchedString );
+			Com_TruncateLongString( latchedString, v->latchedString );
+			Com_Printf( "latched: \"%s\"\n", latchedString );
 		}
 		return qtrue;
 	}
@@ -645,11 +652,21 @@
 		if( var->flags & CVAR_ARCHIVE ) {
 			// write the latched value, even if it hasn't taken effect yet
 			if ( var->latchedString ) {
+				if( strlen( var->name ) + strlen( var->latchedString ) + 10 > sizeof( buffer ) ) {
+					Com_Printf( S_COLOR_YELLOW "WARNING: value of variable "
+							"\"%s\" too long to write to file\n", var->name );
+					continue;
+				}
 				Com_sprintf (buffer, sizeof(buffer), "seta %s \"%s\"\n", var->name, var->latchedString);
 			} else {
+				if( strlen( var->name ) + strlen( var->string ) + 10 > sizeof( buffer ) ) {
+					Com_Printf( S_COLOR_YELLOW "WARNING: value of variable "
+							"\"%s\" too long to write to file\n", var->name );
+					continue;
+				}
 				Com_sprintf (buffer, sizeof(buffer), "seta %s \"%s\"\n", var->name, var->string);
 			}
-			FS_Printf (f, "%s", buffer);
+			FS_Write( buffer, strlen( buffer ), f );
 		}
 	}
 }

Modified: trunk/code/qcommon/q_shared.c
===================================================================
--- trunk/code/qcommon/q_shared.c	2006-01-23 05:14:59 UTC (rev 521)
+++ trunk/code/qcommon/q_shared.c	2006-01-24 04:35:19 UTC (rev 522)
@@ -918,7 +918,27 @@
 	return buf;
 }
 
+/*
+============
+Com_TruncateLongString
 
+Assumes buffer is atleast TRUNCATE_LENGTH big
+============
+*/
+void Com_TruncateLongString( char *buffer, const char *s )
+{
+	int length = strlen( s );
+
+	if( length <= TRUNCATE_LENGTH )
+		Q_strncpyz( buffer, s, TRUNCATE_LENGTH );
+	else
+	{
+		Q_strncpyz( buffer, s, ( TRUNCATE_LENGTH / 2 ) - 3 );
+		Q_strcat( buffer, TRUNCATE_LENGTH, " ... " );
+		Q_strcat( buffer, TRUNCATE_LENGTH, s + length - ( TRUNCATE_LENGTH / 2 ) + 3 );
+	}
+}
+
 /*
 =====================================================================
 

Modified: trunk/code/qcommon/q_shared.h
===================================================================
--- trunk/code/qcommon/q_shared.h	2006-01-23 05:14:59 UTC (rev 521)
+++ trunk/code/qcommon/q_shared.h	2006-01-24 04:35:19 UTC (rev 522)
@@ -682,6 +682,9 @@
 */
 char	* QDECL va(char *format, ...);
 
+#define TRUNCATE_LENGTH	64
+void Com_TruncateLongString( char *buffer, const char *s );
+
 //=============================================
 
 //




More information about the quake3-commits mailing list