<commit_before>/******************************
** Tsunagari Tile Engine     **
** resourcer.h               **
** Copyright 2011 OmegaSDG   **
******************************/

#ifndef RESOURCER_H
#define RESOURCER_H

#include <string>

#include <Gosu/Gosu.hpp>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <zip.h>

class GameWindow;

//! Resourcer Class
/*!
	This class provides the engine's global resource handling and caching.
*/
class Resourcer
{
public:
	//! Resourcer Constructor
	Resourcer(GameWindow* window, const std::string& filename);

	//! Resourcer Destructor
	~Resourcer();

	//! Resourcer Initializer
	bool init();

	//! Returns an image resource from disk or cache.
	Gosu::Image* getImage(const std::string& name);

	//! Returns a string resource from disk or cache.
	std::string getString(const std::string& name);

	//! Returns an XML resource from disk or cache.
	xmlDoc* getXMLDoc(const std::string& name);

private:
	//! Read a resource from disk into memory. Returns NULL on error.
	Gosu::Buffer* read(const std::string& name);

	//! Helper function 
	std::string path(const std::string& entry_name) const;

	GameWindow* window;
	zip* z;
	const std::string zip_filename;
};

#endif
<commit_msg>Start of resource cache implementation.<commit_after>/******************************
** Tsunagari Tile Engine     **
** resourcer.h               **
** Copyright 2011 OmegaSDG   **
******************************/

#ifndef RESOURCER_H
#define RESOURCER_H

#include <map>
#include <string>
#include <utility>

#include <Gosu/Gosu.hpp>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <zip.h>

#define CACHE_TTL "300" // Cache drop time in seconds. Defaults to 5 minutes.

class GameWindow;

//! Resourcer Class
/*!
	This class provides the engine's global resource handling and caching.
*/
class Resourcer
{
public:
	//! Resourcer Constructor
	Resourcer(GameWindow* window, const std::string& filename);

	//! Resourcer Destructor
	~Resourcer();

	//! Resourcer Initializer
	bool init();

	//! Requests an image resource from cache.
	Gosu::Image* getImage(const std::string& name);

	//! Requests a string resource from cache.
	std::string getString(const std::string& name);

	//! Requests an XML resource from cache.
	xmlDoc* getXMLDoc(const std::string& name);
	
	//! Close resource request and drop from cache. IMPORTANT!!!
	void drop(const std::string& name);

private:
	//! Read a resource from disk into memory. Returns NULL on error.
	Gosu::Buffer* read(const std::string& name);

	//! Helper function 
	std::string path(const std::string& entry_name) const;

	GameWindow* window;
	zip* z;
	const std::string zip_filename;
	
	//! Resource Cache
	/*!
		The key is the cached file's name, and the value is a pair of a
		"tally" and a pointer to the data. The "tally" is increased each
		time something requests the resource, and decreased each time
		something is finished with that resource. When the tally reaches
		zero, nothing is using the resource, and it is dropped after a
		few minutes. The cache drop timer is in a thread.
	*/
	std::map<std::string, std::pair<uint32_t, void*> > cache;
	
	//! Modify the cache tally. Logs a Developer warning on tally underrun.
	void cacheTally(const std::string key, const int32_t mod);
};

#endif

{'fraction_non_alphanumeric': 0.1504102096627165, 'fraction_numerical': 0.004861744150714068, 'mean_word_length': 4.184251968503937, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5648044', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1007, 'n_words': 358}