Added request am29544 (nvault_touch)
This commit is contained in:
		| @@ -15,6 +15,7 @@ public: | |||||||
| 	virtual void Clear() =0; | 	virtual void Clear() =0; | ||||||
| 	virtual void Remove(const char *key) =0; | 	virtual void Remove(const char *key) =0; | ||||||
| 	virtual size_t Items() =0; | 	virtual size_t Items() =0; | ||||||
|  | 	virtual void Touch(const char *key, time_t stamp) =0; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| class IVaultMngr | class IVaultMngr | ||||||
|   | |||||||
| @@ -8,6 +8,12 @@ | |||||||
| #define _snprintf snprintf | #define _snprintf snprintf | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | /**  | ||||||
|  |  * :TODO: This beast calls strcpy()/new() way too much by creating new strings on the stack. | ||||||
|  |  *		  That's easily remedied and it should be fixed? | ||||||
|  |  *         ---bail | ||||||
|  |  */ | ||||||
|  |  | ||||||
| template <> | template <> | ||||||
| int HashFunction<String>(const String & k) | int HashFunction<String>(const String & k) | ||||||
| { | { | ||||||
| @@ -284,6 +290,18 @@ size_t NVault::Prune(time_t start, time_t end) | |||||||
| 	return m_Hash.Prune(start, end); | 	return m_Hash.Prune(start, end); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void NVault::Touch(const char *key, time_t stamp) | ||||||
|  | { | ||||||
|  | 	String sKey(key); | ||||||
|  |  | ||||||
|  | 	if (!m_Hash.Exists(sKey)) | ||||||
|  | 	{ | ||||||
|  | 		SetValue(key, "", time(NULL)); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	m_Hash.Touch(key, stamp); | ||||||
|  | } | ||||||
|  |  | ||||||
| bool NVault::GetValue(const char *key, time_t &stamp, char buffer[], size_t len) | bool NVault::GetValue(const char *key, time_t &stamp, char buffer[], size_t len) | ||||||
| { | { | ||||||
| 	String sKey(key); | 	String sKey(key); | ||||||
|   | |||||||
| @@ -41,6 +41,7 @@ public: | |||||||
| 	const char *GetValue(const char *key); | 	const char *GetValue(const char *key); | ||||||
| 	void SetValue(const char *key, const char *val); | 	void SetValue(const char *key, const char *val); | ||||||
| 	void SetValue(const char *key, const char *val, time_t stamp); | 	void SetValue(const char *key, const char *val, time_t stamp); | ||||||
|  | 	void Touch(const char *key, time_t stamp); | ||||||
| 	size_t Prune(time_t start, time_t end); | 	size_t Prune(time_t start, time_t end); | ||||||
| 	void Clear(); | 	void Clear(); | ||||||
| 	void Remove(const char *key); | 	void Remove(const char *key); | ||||||
|   | |||||||
| @@ -58,6 +58,28 @@ static cell nvault_open(AMX *amx, cell *params) | |||||||
| 	return id; | 	return id; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | static cell nvault_touch(AMX *amx, cell *params) | ||||||
|  | { | ||||||
|  | 	unsigned int id = params[1]; | ||||||
|  | 	if (id >= g_Vaults.size() || !g_Vaults.at(id)) | ||||||
|  | 	{ | ||||||
|  | 		MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id); | ||||||
|  | 		return 0; | ||||||
|  | 	} | ||||||
|  | 	NVault *pVault = g_Vaults.at(id); | ||||||
|  | 	int len; | ||||||
|  | 	char *key = MF_GetAmxString(amx, params[2], 0, &len); | ||||||
|  |  | ||||||
|  | 	if (params[3] == -1) | ||||||
|  | 	{ | ||||||
|  | 		pVault->Touch(key, time(NULL)); | ||||||
|  | 	} else { | ||||||
|  | 		pVault->Touch(key, static_cast<time_t>(params[3])); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return 1; | ||||||
|  | } | ||||||
|  |  | ||||||
| static cell nvault_get(AMX *amx, cell *params) | static cell nvault_get(AMX *amx, cell *params) | ||||||
| { | { | ||||||
| 	unsigned int id = params[1]; | 	unsigned int id = params[1]; | ||||||
| @@ -239,5 +261,6 @@ AMX_NATIVE_INFO nVault_natives[] = { | |||||||
| 	{"nvault_close",			nvault_close}, | 	{"nvault_close",			nvault_close}, | ||||||
| 	{"nvault_prune",			nvault_prune}, | 	{"nvault_prune",			nvault_prune}, | ||||||
| 	{"nvault_remove",			nvault_remove}, | 	{"nvault_remove",			nvault_remove}, | ||||||
|  | 	{"nvault_touch",			nvault_touch}, | ||||||
| 	{NULL,				NULL}, | 	{NULL,				NULL}, | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -109,6 +109,11 @@ public: | |||||||
| 		stamp = pNode->stamp; | 		stamp = pNode->stamp; | ||||||
| 		return pNode->val; | 		return pNode->val; | ||||||
| 	} | 	} | ||||||
|  | 	void Touch(const K & k, time_t stamp) | ||||||
|  | 	{ | ||||||
|  | 		THashNode *pNode = _FindOrInsert(k); | ||||||
|  | 		pNode->stamp = stamp; | ||||||
|  | 	} | ||||||
| 	V & Retrieve(const K & k) | 	V & Retrieve(const K & k) | ||||||
| 	{ | 	{ | ||||||
| 		time_t stamp; | 		time_t stamp; | ||||||
|   | |||||||
| @@ -19,32 +19,43 @@ | |||||||
|  #pragma library nvault |  #pragma library nvault | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| //Opens a vault by name (such as "myvault") | /* All timestamps are in UNIX epoch form. */ | ||||||
| //Returns a vault id, INVALID_HANDLE otherwise (-1) |  | ||||||
|  | /* Opens a vault by name (such as "myvault") | ||||||
|  |  * Returns a vault id, INVALID_HANDLE otherwise (-1) | ||||||
|  |  */ | ||||||
| native nvault_open(const name[]); | native nvault_open(const name[]); | ||||||
|  |  | ||||||
| //Gets a vault value by returning an int | /* Gets a vault value by returning an int | ||||||
| // setting a byref float |  * setting a byref float or setting a string + maxlength | ||||||
| // or setting a string + maxlength |  */ | ||||||
| native nvault_get(vault, const key[], ...); | native nvault_get(vault, const key[], ...); | ||||||
|  |  | ||||||
| //Looks up a vault value for full information | /* Looks up a vault value for full information | ||||||
| //Returns 0 if the entry is not found |  * Returns 0 if the entry is not found | ||||||
|  |  */ | ||||||
| native nvault_lookup(vault, const key[], value[], maxlen, ×tamp) | native nvault_lookup(vault, const key[], value[], maxlen, ×tamp) | ||||||
|  |  | ||||||
| //Sets a vault value (with current timestamp) | /* Sets a vault value (with current timestamp) */ | ||||||
| native nvault_set(vault, const key[], const value[]); | native nvault_set(vault, const key[], const value[]); | ||||||
|  |  | ||||||
| //Sets a permanent vault value (no timestamp) | /* Sets a permanent vault value (no timestamp) */ | ||||||
| native nvault_pset(vault, const key[], const value[]); | native nvault_pset(vault, const key[], const value[]); | ||||||
|  |  | ||||||
| //Prunes the vault for entries that are within the given timestamps. | /* Prunes the vault for entries that are within the given timestamps. | ||||||
| //This will not erase values set with pset |  * This will not erase values set with pset | ||||||
|  |  */ | ||||||
| native nvault_prune(vault, start, end); | native nvault_prune(vault, start, end); | ||||||
|  |  | ||||||
| //Closes a vault | /* Closes a vault */ | ||||||
| native nvault_close(vault); | native nvault_close(vault); | ||||||
|  |  | ||||||
| //Removes a key from the vault | /* Removes a key from the vault */ | ||||||
| native nvault_remove(vault, const key[]); | native nvault_remove(vault, const key[]); | ||||||
|  |  | ||||||
|  | /* "Touches" a key to update its timestamp value.   | ||||||
|  |  * If stamp is -1 (default), it will use the current time. | ||||||
|  |  * Like the unix command "touch," it will create an empty key | ||||||
|  |  *  if the value does not exist. | ||||||
|  |  */ | ||||||
|  | native nvault_touch(vault, const key[], timestamp=-1); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user