/**
 * vim: set ts=4 :
 * ===============================================================
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
 * ===============================================================
 *
 *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
 * or modified under the Terms and Conditions of its License Agreement, which is found 
 * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
 * may change at any time.  To view the latest information, see:
 *   http://www.sourcemod.net/license.php
 *
 * Version: $Id$
 */

#if defined _sdktools_stringtables_included
  #endinput
#endif
#define _sdktools_stringtables_included

#define INVALID_STRING_TABLE -1		/**< An invalid string table index */
#define INVALID_STRING_INDEX -1		/**< An invalid string index in a table */

/**
 * Searches for a string table.
 *
 * @param name			Name of string table to find.
 * @return				A string table index number if found, INVALID_STRING_TABLE otherwise.
 */
native FindStringTable(const String:name[]);

/**
 * Returns the number of string tables that currently exist.
 *
 * @return				Number of string tables that currently exist.
 */
native GetNumStringTables();

/**
 * Returns the number of strings that currently exist in a given string table.
 *
 * @param tableidx		A string table index.
 * @return				Number of strings that currently exist.
 * @error				Invalid string table index.
 */
native GetStringTableNumStrings(tableidx);

/**
 * Returns the maximum number of strings that are allowed in a given string table.
 *
 * @param tableidx		A string table index.
 * @return				Maximum number of strings allowed.
 * @error				Invalid string table index.
 */
native GetStringTableMaxStrings(tableidx);

/**
 * Retrieves the name of a string table.
 *
 * @param tableidx		A string table index.
 * @param name			Buffer to store the name of the string table.
 * @param maxlength		Maximum length of string buffer.
 * @return				Number of bytes written to the buffer (UTF-8 safe).
 * @error				Invalid string table index.
 */
native GetStringTableName(tableidx, String:name[], maxlength);

/**
 * Searches for the index of a given string in a string table.
 *
 * @param tableidx		A string table index.
 * @param string		String to find.
 * @return				String index if found, INVALID_STRING_INDEX otherwise.
 * @error				Invalid string table index.
 */
native FindStringIndex(tableidx, const String:str[]);

/**
 * Retrieves the string at a given index of a string table.
 *
 * @param tableidx		A string table index.
 * @param stringidx		A string index.
 * @param name			Buffer to store the string value.
 * @param maxlength		Maximum length of string buffer.
 * @return				Number of bytes written to the buffer (UTF-8 safe).
 * @error				Invalid string table index or string index.
 */
native ReadStringTable(tableidx, stringIdx, String:str[], maxlength);

/**
 * Returns the length of the user data associated with a given string index.
 *
 * @param tableidx		A string table index.
 * @param stringidx		A string index.
 * @return				Length of user data. This will be 0 if there is no user data.
 * @error				Invalid string table index or string index.
 */
native GetStringTableDataLength(tableidx, stringidx);

/**
 * Retrieves the user data associated with a given string index.
 *
 * @param tableidx		A string table index.
 * @param stringidx		A string index.
 * @param userdata		Buffer to store the user data. This will be set to "" if there is no user data.
 * @param maxlength		Maximum length of string buffer.
 * @return				Number of bytes written to the buffer (UTF-8 safe).
 * @error				Invalid string table index or string index.
 */
native GetStringTableData(tableidx, stringIdx, String:userdata[], maxlength);

/**
 * Sets the user data associated with a given string index.
 *
 * @param tableidx		A string table index.
 * @param stringidx		A string index.
 * @param userdata		User data string that will be set.
 * @param length		Length of user data string. This should include the null terminator.
 * @return				Number of bytes written to the buffer (UTF-8 safe).
 * @error				Invalid string table index or string index.
 */
native SetStringTableData(tableidx, stringIdx, const String:userdata[], length);

/**
 * Adds a string to a given string table.
 *
 * @param tableidx		A string table index.
 * @param string		String to add.
 * @param userdata		An optional user data string.
 * @param length		Length of user data string. This should include the null terminator.
 *						If set to -1, then user data will be not be altered if the specified string
 *						already exists in the string table.
 */
native AddToStringTable(tableidx, const String:str[], const String:userdata[]="", length=-1);

/**
 * Locks or unlocks the network string tables.
 *
 * @param lock			Determines whether network string tables should be locked.
 *						True means the tables should be locked for writing; false means unlocked.
 * @return				Previous lock state.
 */
native bool:LockStringTables(bool:lock);

/**
 * Adds a file to the downloadables network string table.
 * This forces a client to download the file if they do not already have it.
 *
 * @param filename		File that will be added to downloadables table.
 */
stock AddFileToDownloadsTable(const String:filename[])
{
	static table = INVALID_STRING_TABLE;
	
	if (table == INVALID_STRING_TABLE)
	{
		table = FindStringTable("downloadables");
	}
	
	new bool:save = LockStringTables(false);
	AddToStringTable(table, filename);
	LockStringTables(save);
}