cvar.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. Copyright (C) 2009-2011 id Software LLC, a ZeniMax Media company.
  3. Copyright (C) 2004 Michael Liebscher
  4. Copyright (C) 1997-2001 Id Software, Inc.
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General License for more details.
  13. You should have received a copy of the GNU General License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include "../doomiphone.h"
  18. cvar_t *cvar_vars;
  19. /*
  20. -----------------------------------------------------------------------------
  21. Function: Cvar_FindVar -Return cvar;
  22. Parameters: var_name -[in] Name of cvar to lookup.
  23. Returns: NULL if cvar not found, otherwise returns the cvar.
  24. Notes:
  25. -----------------------------------------------------------------------------
  26. */
  27. cvar_t *Cvar_FindVar( const char *var_name )
  28. {
  29. cvar_t *var;
  30. int hashid;
  31. hashid = HashString( var_name );
  32. for( var = cvar_vars ; var ; var = var->next )
  33. {
  34. if( hashid == var->hashid && !strcasecmp( var_name, var->name ) )
  35. {
  36. return var;
  37. }
  38. }
  39. return NULL;
  40. }
  41. /*
  42. -----------------------------------------------------------------------------
  43. Function: Cvar_VariableValue -Get value of cvar.
  44. Parameters: var_name -[in] Name of cvar to get value.
  45. Returns: 0 if not found, other the value of the cvar.
  46. Notes:
  47. -----------------------------------------------------------------------------
  48. */
  49. float Cvar_VariableValue( const char *var_name )
  50. {
  51. cvar_t *var;
  52. var = Cvar_FindVar( var_name );
  53. if( ! var )
  54. {
  55. return 0;
  56. }
  57. return (float)atof( var->string );
  58. }
  59. /*
  60. -----------------------------------------------------------------------------
  61. Function: Cvar_VariableString -Get cvar variable as string.
  62. Parameters: var_name -[in] Name of cvar to get value.
  63. Returns: Blank string on error, otherwise value string.
  64. Notes:
  65. -----------------------------------------------------------------------------
  66. */
  67. char *Cvar_VariableString( const char *var_name )
  68. {
  69. cvar_t *var;
  70. var = Cvar_FindVar( var_name );
  71. if( ! var )
  72. {
  73. return "";
  74. }
  75. return var->string;
  76. }
  77. /*
  78. -----------------------------------------------------------------------------
  79. Function: Cvar_CompleteVariable -Complete cvar string name.
  80. Parameters: partial -[in] Partial name of string to look up.
  81. Returns: NULL if partial string not found, otherwise the complete
  82. string name.
  83. Notes:
  84. -----------------------------------------------------------------------------
  85. */
  86. char *Cvar_CompleteVariable( const char *partial )
  87. {
  88. cvar_t *cvar;
  89. size_t len;
  90. len = strlen( partial );
  91. if( ! len )
  92. {
  93. return NULL;
  94. }
  95. //
  96. // Check partial match.
  97. //
  98. for( cvar = cvar_vars ; cvar ; cvar = cvar->next )
  99. {
  100. if( ! strncmp( partial, cvar->name, len ) )
  101. {
  102. return cvar->name;
  103. }
  104. }
  105. return NULL;
  106. }
  107. /*
  108. -----------------------------------------------------------------------------
  109. Function: Cvar_Get -Get cvar structure.
  110. Parameters:
  111. var_name -[in] the name of the cvar variable.
  112. var_value -[in] string value of the cvar variable.
  113. flags -[in] see CVARFlags for more information.
  114. Returns: NULL on error, otherwise valid pointer to cvar_t structure.
  115. Notes:
  116. If the variable already exists, the value will not be set and
  117. the flags will be or'ed.
  118. -----------------------------------------------------------------------------
  119. */
  120. cvar_t *Cvar_Get( const char *var_name, const char *var_value, CVARFlags flags ) {
  121. cvar_t *var;
  122. var = Cvar_FindVar( var_name );
  123. if( var ) {
  124. var->flags |= flags;
  125. return var;
  126. }
  127. if( ! var_value ) {
  128. return NULL;
  129. }
  130. var = malloc( sizeof( *var ) );
  131. var->name = strdup( var_name );
  132. var->string = strdup( var_value );
  133. var->defaultString = strdup( var_value );
  134. var->hashid = HashString( var_name );
  135. var->modified = true;
  136. var->value = (float)atof( var->string );
  137. // link the variable in
  138. var->next = cvar_vars;
  139. cvar_vars = var;
  140. var->flags = flags;
  141. return var;
  142. }
  143. /*
  144. -----------------------------------------------------------------------------
  145. Function:
  146. Parameters:
  147. Returns:
  148. Notes:
  149. -----------------------------------------------------------------------------
  150. */
  151. void Cvar_Set( const char *var_name, const char *value ) {
  152. cvar_t *var;
  153. var = Cvar_FindVar( var_name );
  154. if( ! var ) {
  155. Com_Printf( "Cvar '%s' doesn't exist\n", var_name );
  156. return;
  157. }
  158. if( var->flags & CVAR_NOSET ) {
  159. Com_Printf( "%s is write protected.\n", var_name );
  160. return;
  161. }
  162. if( ! strcmp( value, var->string ) ) {
  163. return; // not changed
  164. }
  165. var->modified = true;
  166. free( var->string ); // free the old value string
  167. var->string = strdup( value );
  168. var->value = (float)atof( var->string );
  169. }
  170. /*
  171. -----------------------------------------------------------------------------
  172. Function:
  173. Parameters:
  174. Returns:
  175. Notes:
  176. -----------------------------------------------------------------------------
  177. */
  178. void Cvar_SetValue( const char *var_name, float value )
  179. {
  180. char val[ 32 ];
  181. if( value == (int)value )
  182. {
  183. snprintf( val, sizeof( val ), "%i", (int)value );
  184. }
  185. else
  186. {
  187. snprintf( val, sizeof( val ), "%f", value );
  188. }
  189. Cvar_Set( var_name, val );
  190. }
  191. /*
  192. -----------------------------------------------------------------------------
  193. Function: Cvar_Command -Handles variable inspection and changing from
  194. the console.
  195. Parameters: Nothing.
  196. Returns: false if variable not found, otherwise true.
  197. Notes:
  198. -----------------------------------------------------------------------------
  199. */
  200. boolean Cvar_Command( void )
  201. {
  202. cvar_t *v;
  203. // check variables
  204. v = Cvar_FindVar( Cmd_Argv( 0 ) );
  205. if( ! v )
  206. {
  207. return false;
  208. }
  209. // perform a variable print or set
  210. if( Cmd_Argc() == 1 )
  211. {
  212. Com_Printf( "\"%s\" is \"%s\"\n", v->name, v->string );
  213. return true;
  214. }
  215. Cvar_Set( v->name, Cmd_Argv( 1 ) );
  216. return true;
  217. }
  218. /*
  219. -----------------------------------------------------------------------------
  220. Function: Cvar_WriteVariables -Appends lines containing "set variable value"
  221. for all variables with the archive flag set
  222. to true.
  223. Parameters:
  224. Returns: Nothing.
  225. Notes:
  226. -----------------------------------------------------------------------------
  227. */
  228. void Cvar_WriteVariables( const char *path )
  229. {
  230. cvar_t *var;
  231. char buffer[1024];
  232. FILE *f;
  233. f = fopen( path, "a" );
  234. for( var = cvar_vars ; var ; var = var->next )
  235. {
  236. if( var->flags & CVAR_ARCHIVE )
  237. {
  238. snprintf( buffer, sizeof( buffer ), "set %s %s\n", var->name, var->string );
  239. fprintf( f, "%s", buffer );
  240. }
  241. }
  242. fclose( f );
  243. }
  244. /*
  245. -----------------------------------------------------------------------------
  246. Function: Cvar_List_f -Print all cvars to the console.
  247. Parameters: Nothing.
  248. Returns: Nothing.
  249. Notes:
  250. -----------------------------------------------------------------------------
  251. */
  252. void Cvar_List_f( void )
  253. {
  254. cvar_t *var;
  255. int i;
  256. i = 0;
  257. for( var = cvar_vars ; var ; var = var->next, ++i )
  258. {
  259. if( var->flags & CVAR_ARCHIVE )
  260. {
  261. Com_Printf ("*");
  262. }
  263. else
  264. {
  265. Com_Printf (" ");
  266. }
  267. Com_Printf (" %s \"%s\"\n", var->name, var->string);
  268. }
  269. Com_Printf ("%i cvars\n", i);
  270. }
  271. void Cvar_Reset_f( void ) {
  272. for( cvar_t *var = cvar_vars ; var ; var = var->next ) {
  273. Cvar_Set( var->name, var->defaultString );
  274. }
  275. }