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