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