plugins/include/keyvalues.inc
author Erik Minekus <erik@interwavestudios.com>
Wed Jun 23 23:41:47 2010 -0400 (2010-06-23)
changeset 3052 a102c645bf4b
parent 2289 44045ead61cd
child 3323 21915bed1eed
permissions -rw-r--r--
Fixed lack of error checking on sm_motd. (bug 4460, r=dvander)
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32  
    33 #if defined _keyvalues_included
    34  #endinput
    35 #endif
    36 #define _keyvalues_included
    37 
    38 /**
    39  * KeyValue data value types
    40  */
    41 enum KvDataTypes
    42 {
    43 	KvData_None = 0,	/**< Type could not be identified, or no type */
    44 	KvData_String,		/**< String value */
    45 	KvData_Int,			/**< Integer value */
    46 	KvData_Float,		/**< Floating point value */
    47 	KvData_Ptr,			/**< Pointer value (sometimes called "long") */
    48 	KvData_WString,		/**< Wide string value */
    49 	KvData_Color,		/**< Color value */
    50 	KvData_UInt64,		/**< Large integer value */
    51 	/* --- */
    52 	KvData_NUMTYPES, 
    53 };
    54 
    55 /**
    56  * Creates a new KeyValues structure.  The Handle must always be closed.
    57  *
    58  * @param name			Name of the root section.
    59  * @param firstKey		If non-empty, specifies the first key value.
    60  * @param firstValue	If firstKey is non-empty, specifies the first key's value.
    61  * @return				A Handle to a new KeyValues structure.
    62  */
    63 native Handle:CreateKeyValues(const String:name[], const String:firstkey[]="", const String:firstValue[]="");
    64 
    65 /** 
    66  * Sets a string value of a KeyValues key.
    67  *
    68  * @param kv			KeyValues Handle.
    69  * @param key			Name of the key, or NULL_STRING.
    70  * @param value			String value.
    71  * @noreturn
    72  * @error				Invalid Handle.
    73  */
    74 native KvSetString(Handle:kv, const String:key[], const String:value[]);
    75 
    76 /** 
    77  * Sets an integer value of a KeyValues key.
    78  *
    79  * @param kv			KeyValues Handle.
    80  * @param key			Name of the key, or NULL_STRING.
    81  * @param value			Value number.
    82  * @noreturn
    83  * @error				Invalid Handle.
    84  */
    85 native KvSetNum(Handle:kv, const String:key[], value);
    86 
    87 /** 
    88  * Sets a large integer value of a KeyValues key.
    89  *
    90  * @param kv			KeyValues Handle.
    91  * @param key			Name of the key, or NULL_STRING.
    92  * @param value			Large integer value (0=High bits, 1=Low bits)
    93  * @noreturn
    94  * @error				Invalid Handle.
    95  */
    96 native KvSetUInt64(Handle:kv, const String:key[], const value[2]);
    97 
    98 /** 
    99  * Sets a floating point value of a KeyValues key.
   100  *
   101  * @param kv			KeyValues Handle.
   102  * @param key			Name of the key, or NULL_STRING.
   103  * @param value			Floating point value.
   104  * @noreturn
   105  * @error				Invalid Handle.
   106  */
   107 native KvSetFloat(Handle:kv, const String:key[], Float:value);
   108 
   109 /** 
   110  * Sets a set of color values of a KeyValues key.
   111  *
   112  * @param kv			KeyValues Handle.
   113  * @param key			Name of the key, or NULL_STRING.
   114  * @param r				Red value.
   115  * @param g				Green value.
   116  * @param b				Blue value.
   117  * @param a				Alpha value.
   118  * @noreturn
   119  * @error				Invalid Handle.
   120  */
   121 native KvSetColor(Handle:kv, const String:key[], r, g, b, a=0);
   122 
   123 /** 
   124  * Sets a vector value of a KeyValues key.
   125  *
   126  * @param kv			KeyValues Handle.
   127  * @param key			Name of the key, or NULL_STRING.
   128  * @param vec			Vector value.
   129  * @noreturn
   130  * @error				Invalid Handle.
   131  */
   132 native KvSetVector(Handle:kv, const String:key[], const Float:vec[3]);
   133 
   134 /** 
   135  * Retrieves a string value from a KeyValues key.
   136  *
   137  * @param kv			KeyValues Handle.
   138  * @param key			Name of the key, or NULL_STRING.
   139  * @param value			Buffer to store key value in.
   140  * @param maxlength		Maximum length of the value buffer.
   141  * @param defvalue		Optional default value to use if the key is not found.
   142  * @noreturn
   143  * @error				Invalid Handle.
   144  */
   145 native KvGetString(Handle:kv, const String:key[], String:value[], maxlength, const String:defvalue[]="");
   146 
   147 /** 
   148  * Retrieves an integer value from a KeyValues key.
   149  *
   150  * @param kv			KeyValues Handle.
   151  * @param key			Name of the key, or NULL_STRING.
   152  * @param defvalue		Optional default value to use if the key is not found.
   153  * @return				Integer value of the key.
   154  * @error				Invalid Handle.
   155  */
   156 native KvGetNum(Handle:kv, const String:key[], defvalue=0);
   157 
   158 /** 
   159  * Retrieves a floating point value from a KeyValues key.
   160  *
   161  * @param kv			KeyValues Handle.
   162  * @param key			Name of the key, or NULL_STRING.
   163  * @param defvalue		Optional default value to use if the key is not found.
   164  * @return				Floating point value of the key.
   165  * @error				Invalid Handle.
   166  */
   167 native Float:KvGetFloat(Handle:kv, const String:key[], Float:defvalue=0.0);
   168 
   169 /** 
   170  * Retrieves a set of color values from a KeyValues key.
   171  *
   172  * @param kv			KeyValues Handle.
   173  * @param key			Name of the key, or NULL_STRING.
   174  * @param r				Red value, set by reference.
   175  * @param g				Green value, set by reference. 
   176  * @param b				Blue value, set by reference. 
   177  * @param a				Alpha value, set by reference. 
   178  * @noreturn
   179  * @error				Invalid Handle.
   180  */
   181 native KvGetColor(Handle:kv, const String:key[], &r, &g, &b, &a);
   182 
   183 /** 
   184  * Retrieves a large integer value from a KeyValues key.
   185  *
   186  * @param kv			KeyValues Handle.
   187  * @param key			Name of the key, or NULL_STRING.
   188  * @param value			Array to represent the large integer.
   189  * @param defvalue		Optional default value to use if the key is not found.
   190  * @noreturn
   191  * @error				Invalid Handle.
   192  */
   193 native KvGetUInt64(Handle:kv, const String:key[], value[2], defvalue[2]={0,0});
   194 
   195 /** 
   196  * Retrieves a vector value from a KeyValues key.
   197  *
   198  * @param kv			KeyValues Handle.
   199  * @param key			Name of the key, or NULL_STRING.
   200  * @param vec			Destination vector to store the value in.
   201  * @param defvalue		Optional default value to use if the key is not found.
   202  * @noreturn
   203  * @error				Invalid Handle.
   204  */
   205 native KvGetVector(Handle:kv, const String:key[], Float:vec[3], const Float:defvalue[3]={0.0, 0.0, 0.0});
   206 
   207 /**
   208  * Sets the current position in the KeyValues tree to the given key.
   209  *
   210  * @param kv			KeyValues Handle.
   211  * @param key			Name of the key.
   212  * @param create		If true, and the key does not exist, it will be created.
   213  * @return				True if the key exists, false if it does not and was not created.
   214  */
   215 native bool:KvJumpToKey(Handle:kv, const String:key[], bool:create=false);
   216 
   217 /**
   218  * Sets the current position in the KeyValues tree to the given key.
   219  *
   220  * @param kv			KeyValues Handle.
   221  * @param id			KeyValues id.
   222  * @return				True if the key exists, false if it does not.
   223  */
   224 native bool:KvJumpToKeySymbol(Handle:kv, id);
   225 
   226 /**
   227  * Sets the current position in the KeyValues tree to the first sub key.
   228  * This native adds to the internal traversal stack.
   229  *
   230  * @param kv			KeyValues Handle.
   231  * @param keyOnly		If false, non-keys will be traversed (values).
   232  * @return				True on success, false if there was no first sub key.
   233  * @error				Invalid Handle.
   234  */
   235 native bool:KvGotoFirstSubKey(Handle:kv, bool:keyOnly=true);
   236 
   237 /**
   238  * Sets the current position in the KeyValues tree to the next sub key.
   239  * This native does NOT add to the internal traversal stack, and thus
   240  * KvGoBack() is not needed for each successive call to this function.
   241  *
   242  * @param kv			KeyValues Handle.
   243  * @param keyOnly		If false, non-keys will be traversed (values).
   244  * @return				True on success, false if there was no next sub key.
   245  * @error				Invalid Handle.
   246  */
   247 native bool:KvGotoNextKey(Handle:kv, bool:keyOnly=true);
   248 
   249 /**
   250  * Saves the current position in the traversal stack onto the traversal
   251  * stack.  This can be useful if you wish to use KvGotoNextKey() and 
   252  * have the previous key saved for backwards traversal.
   253  *
   254  * @param kv			KeyValues Handle.
   255  * @noreturn
   256  * @error				Invalid Handle.
   257  */
   258 native KvSavePosition(Handle:kv);
   259 
   260 /**
   261  * Removes the given key from the current position.
   262  *
   263  * @param kv			KeyValues Handle.
   264  * @param key			Name of the key.
   265  * @return				True on success, false if key did not exist.
   266  * @error				Invalid Handle.
   267  */
   268 native bool:KvDeleteKey(Handle:kv, const String:key[]);
   269 
   270 /**
   271  * Removes the current sub-key and attempts to set the position
   272  * to the sub-key after the removed one.  If no such sub-key exists,
   273  * the position will be the parent key in the traversal stack.
   274  * Given the sub-key having position "N" in the traversal stack, the 
   275  * removal will always take place from position "N-1."
   276  *
   277  * @param kv			KeyValues Handle.
   278  * @return				1 if removal succeeded and there was another key.
   279  *						0 if the current node was not contained in the
   280  *						  previous node, or no previous node exists.
   281  *						-1 if removal succeeded and there were no more keys,
   282  *						  thus the state is as if KvGoBack() was called.
   283  * @error				Invalid Handle.
   284  */
   285 native KvDeleteThis(Handle:kv);
   286 
   287 /**
   288  * Jumps back to the previous position.  Returns false if there are no
   289  * previous positions (i.e., at the root node).  This should be called
   290  * once for each successful Jump call, in order to return to the top node.
   291  * This function pops one node off the internal traversal stack.
   292  *
   293  * @param kv			KeyValues Handle.
   294  * @return				True on success, false if there is no higher node.
   295  * @error				Invalid Handle.
   296  */
   297 native bool:KvGoBack(Handle:kv);
   298 
   299 /**
   300  * Sets the position back to the top node, emptying the entire node 
   301  * traversal history.  This can be used instead of looping KvGoBack()
   302  * if recursive iteration is not important.
   303  *
   304  * @param kv			KeyValues Handle.
   305  * @noreturn
   306  * @error				Invalid Handle.
   307  */
   308 native KvRewind(Handle:kv);
   309 
   310 /**
   311  * Retrieves the current section name.
   312  * 
   313  * @param kv			KeyValues Handle.
   314  * @param section		Buffer to store the section name.
   315  * @param maxlength		Maximum length of the name buffer.
   316  * @return				True on success, false on failure.
   317  * @error				Invalid Handle.
   318  */
   319 native bool:KvGetSectionName(Handle:kv, String:section[], maxlength);
   320 
   321 /**
   322  * Sets the current section name.
   323  * 
   324  * @param kv			KeyValues Handle.
   325  * @param section		Section name.
   326  * @noreturn
   327  * @error				Invalid Handle.
   328  */
   329 native KvSetSectionName(Handle:kv, const String:section[]);
   330 
   331 /**
   332  * Returns the data type at a key.
   333  *
   334  * @param kv			KeyValues Handle.
   335  * @param key			Key name.
   336  * @return				KvDataType value of the key.
   337  * @error				Invalid Handle.
   338  */
   339 native KvDataTypes:KvGetDataType(Handle:kv, const String:key[]);
   340 
   341 /**
   342  * Converts a KeyValues tree to a file.  The tree is dumped
   343  * from the current position.
   344  *
   345  * @param kv			KeyValues Handle.
   346  * @param file			File to dump write to.
   347  * @return				True on success, false otherwise.
   348  * @error				Invalid Handle.
   349  */
   350 native bool:KeyValuesToFile(Handle:kv, const String:file[]);
   351 
   352 /**
   353  * Converts a file to a KeyValues tree.  The file is read into
   354  * the current position of the tree.
   355  *
   356  * @param kv			KeyValues Handle.
   357  * @param file			File to read from.
   358  * @return				True on success, false otherwise.
   359  * @error				Invalid Handle.
   360  */
   361 native bool:FileToKeyValues(Handle:kv, const String:file[]);
   362 
   363 /**
   364  * Sets whether or not the KeyValues parser will read escape sequences.
   365  * For example, \n would be read as a literal newline.  This defaults
   366  * to false for new KeyValues structures.
   367  *
   368  * @param kv			KeyValues Handle.
   369  * @param useEscapes	Whether or not to read escape sequences.
   370  * @noreturn
   371  * @error				Invalid Handle.
   372  */
   373 native KvSetEscapeSequences(Handle:kv, bool:useEscapes);
   374 
   375 /**
   376  * Returns the position in the jump stack; I.e. the number of calls
   377  * required for KvGoBack to return to the root node.  If at the root node,
   378  * 0 is returned.
   379  *
   380  * @param kv			KeyValues Handle.
   381  * @return				Number of non-root nodes in the jump stack.
   382  * @error				Invalid Handle.
   383  */
   384 native KvNodesInStack(Handle:kv);
   385 
   386 /**
   387  * Makes a new copy of all subkeys in the origin KeyValues to
   388  * the destination KeyValues.
   389  * NOTE: All KeyValues are processed from the current location not the root one.
   390  *
   391  * @param origin		Origin KeyValues Handle.
   392  * @param dest			Destination KeyValues Handlee.
   393  * @noreturn
   394  * @error				Invalid Handle.
   395  */
   396 native KvCopySubkeys(Handle:origin, Handle:dest);
   397 
   398 /** 
   399  * Finds a KeyValues name by id.
   400  *
   401  * @param kv			KeyValues Handle.
   402  * @param id			KeyValues id.
   403  * @param name			Buffer to store the name.
   404  * @param maxlength		Maximum length of the value buffer.
   405  * @return			True on success, false if id not found.
   406  * @error				Invalid Handle.
   407  */
   408 native bool:KvFindKeyById(Handle:kv, id, String:name[], maxlength);
   409 
   410 /** 
   411  * Finds a KeyValues id inside a KeyValues tree.
   412  *
   413  * @param kv			KeyValues Handle.
   414  * @param key			Key name.
   415  * @param id			Id of the found KeyValue.
   416  * @return			True on success, false if key not found.
   417  * @error				Invalid Handle.
   418  */
   419 native bool:KvGetNameSymbol(Handle:kv, const String:key[], &id);
   420 
   421 /**
   422  * Retrieves the current section id.
   423  * 
   424  * @param kv			KeyValues Handle.
   425  * @param id			Id of the current section.
   426  * @return				True on success, false on failure.
   427  * @error				Invalid Handle.
   428  */
   429 native bool:KvGetSectionSymbol(Handle:kv, &id);