/*
Copyright (C) 1996-1997 Id Software, Inc.

This program 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 2
of the License, or (at your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

///////////////////////////////////////////////////////////////////////////
/// @file	cvar.c
/// @brief	dynamic variable tracking
/// @defgroup cvar_functions Cvar Functions
///////////////////////////////////////////////////////////////////////////
#include "quakedef.h"

cvar_t	*cvar_vars;			///< top of the @b linked @b cvar @b list
char	*cvar_null_string = "";

///////////////////////////////////////////////////////////////////////////
/// @ingroup cvar_functions
/// @brief	Search for cvar and return it. Search through the @b linked
///			@b cvar @b list for the specified variable.
/// @param	var_name The cvar to find
/// @return	The found cvar
/// @return	0 if no cvar is found
///////////////////////////////////////////////////////////////////////////
cvar_t *Cvar_FindVar (char *var_name)
{
	/// Create local cvar.
	cvar_t	*var;
	
	/// Loop through all cvars.
	for (var = cvar_vars ; var ; var = var->next)
	{
		/// If current cvar equals @b var_name, return the current cvar.
		if (!Q_strcmp (var_name, var->name))
		{
			return var;
		}
	}

	/// Return 0 if no cvar has been found
	return 0;
}

///////////////////////////////////////////////////////////////////////////
/// @ingroup cvar_functions
/// @brief	Search for @b var_name and return its value or
///			0 if the cvar is not defined or non numeric.
/// @param	var_name The cvar to search for.
/// @return	The found cvar's value.
/// @return @b NULL if none found
///////////////////////////////////////////////////////////////////////////
float Cvar_VariableValue (char *var_name)
{
	/// Create local cvar.
	cvar_t	*var;
	
	/// Search for the cvar.
	var = Cvar_FindVar (var_name);

	/// If: cvar @b not found? Return 0.
	if (!var)
		return 0;

	/// Else: Return the cvar's value.
	return Q_atof (var->string);
}


///////////////////////////////////////////////////////////////////////////
/// @ingroup cvar_functions
/// @brief	Search for @b var_name and return its value or
///			an empty string if not defined.
///			Returns an empty string if not defined.
/// @param var_name 
/// @return The cvar's string
///////////////////////////////////////////////////////////////////////////
char *Cvar_VariableString (char *var_name)
{
	cvar_t *var;

	var = Cvar_FindVar (var_name);

	if (!var)
		return cvar_null_string;

	return var->string;
}


///////////////////////////////////////////////////////////////////////////
/// @ingroup cvar_functions
/// @brief	Attempts to match a partial variable name for command line
///			completion. Returns NULL if nothing fits.
///	
///	Used for Command Completion in the Console.
///	@param partial The partial string to search through the cvars for.
///	@return The complete.
///////////////////////////////////////////////////////////////////////////
char *Cvar_CompleteVariable (char *partial)
{
	cvar_t		*cvar;
	int			len;
	
	len = Q_strlen (partial);
	
	if (!len)
		return NULL;
	
	/// Loop through the cvars and search for the partially typed cvar name.
	for (cvar = cvar_vars ; cvar ; cvar = cvar->next)
		if (!Q_strncmp (partial, cvar->name, len))
			return cvar->name;
	
	return NULL;
}


///////////////////////////////////////////////////////////////////////////
/// @ingroup cvar_functions
/// @brief	Set cvar @e var_name to @e value.
///			Only works on already existing cvars.
///			Equivalent to "<name> <variable>" typed at the console.
/// @param	var_name	
/// @param	value		
///////////////////////////////////////////////////////////////////////////
void Cvar_Set (char *var_name, char *value)
{
	cvar_t	*var;
	bool	changed;
	
	var = Cvar_FindVar (var_name);
	if (!var)
	{
		// there is an error in C code if this happens
		Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
		return;
	}
	
	changed = Q_strcmp (var->string, value);
	
	Z_Free (var->string);	// free the old value string
	
	var->string = (char *) Z_Malloc (Q_strlen (value) + 1);
	Q_strcpy (var->string, value);
	var->value = Q_atof (var->string);
	if (var->server && changed)
	{
		if (sv.active)
			SV_BroadcastPrintf ("\"%s\" changed to \"%s\"\n", var->name, var->string);
	}
}

///////////////////////////////////////////////////////////////////////////
/// @ingroup cvar_functions
/// @brief	Expands value to a string and calls Cvar_Set.
/// @param	var_name 
/// @param	value 
/// @return void
///////////////////////////////////////////////////////////////////////////
void Cvar_SetValue (char *var_name, float value)
{
	char	val[32];
	
	sprintf (val, "%f", value);
	Cvar_Set (var_name, val);
}


///////////////////////////////////////////////////////////////////////////
/// @ingroup cvar_functions
/// @brief	Registers a cvar.
/// 
/// Registers a cvar that already has the name, string, and optionally the
/// archive elements set. It does so by first checking if it doesn't
///	already exist, and if it isn't a command.
/// @param	variable The cvar to register
/// @return	void
///////////////////////////////////////////////////////////////////////////
void Cvar_RegisterVariable (cvar_t *variable)
{
	char *oldstr;
	
	if (Cvar_FindVar (variable->name))
	{
		Con_Printf ("Can't register variable %s, already defined!\n", variable->name);
		return;
	}
	
	if (Cmd_Exists (variable->name))
	{
		Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name);
		return;
	}
	
	// Copy the value off, because future sets will Z_Free it.
	oldstr = variable->string;
	variable->string = (char *) Z_Malloc (Q_strlen (variable->string) + 1);
	Q_strcpy (variable->string, oldstr);
	variable->value = Q_atof (variable->string);
	
	// link the variable in
	variable->next = cvar_vars;
	cvar_vars = variable;
}


///////////////////////////////////////////////////////////////////////////
/// @ingroup cvar_functions
/// @brief	Handles variable inspection and changing from the console
///			Called by Cmd_ExecuteString when Cmd_Argv(0) doesn't match a
///			known command.  Returns true if the command was a variable
///			reference that was handled. (print or change)
///////////////////////////////////////////////////////////////////////////
bool Cvar_Command (void)
{
	cvar_t	*v;
	
	// check variables
	v = Cvar_FindVar (Cmd_Argv (0));
	
	if (!v)
		return false;
		
	// perform a variable print or set
	if (Cmd_Argc() == 1)
	{
		Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
		return true;
	}
	
	Cvar_Set (v->name, Cmd_Argv (1));
	return true;
}


///////////////////////////////////////////////////////////////////////////
/// @ingroup cvar_functions
/// @brief	Writes lines containing "set variable value" for all variables
///			with the archive flag set to true.
///	@param	f	The file to write to.
///////////////////////////////////////////////////////////////////////////
void Cvar_WriteVariables (FILE *f)
{
	cvar_t	*var;
	
	for (var = cvar_vars ; var ; var = var->next)
		if (var->archive)
			fprintf (f, "%s \"%s\"\n", var->name, var->string);
}

