conworlds

diff src/dataset.inl @ 13:283cdfa7dda2

added a crapload of code from goat3dgfx
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 24 Aug 2014 09:41:24 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/dataset.inl	Sun Aug 24 09:41:24 2014 +0300
     1.3 @@ -0,0 +1,55 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6 +
     1.7 +template <typename T>
     1.8 +DataSet<T>::DataSet(T (*load_func)(const char*), void (*destr_func)(T))
     1.9 +{
    1.10 +	load = load_func;
    1.11 +	destroy = destr_func;
    1.12 +}
    1.13 +
    1.14 +template <typename T>
    1.15 +DataSet<T>::~DataSet()
    1.16 +{
    1.17 +	clear();
    1.18 +}
    1.19 +
    1.20 +template <typename T>
    1.21 +void DataSet<T>::clear()
    1.22 +{
    1.23 +	if(destroy) {
    1.24 +		typename std::map<std::string, T>::iterator it = data.begin();
    1.25 +		while(it != data.end()) {
    1.26 +			destroy(it++->second);
    1.27 +		}
    1.28 +	}
    1.29 +	data.clear();
    1.30 +}
    1.31 +
    1.32 +template <typename T>
    1.33 +T DataSet<T>::get(const char *name) const
    1.34 +{
    1.35 +	typename std::map<std::string, T>::const_iterator iter = data.find(name);
    1.36 +	if(iter != data.end()) {
    1.37 +		return iter->second;
    1.38 +	}
    1.39 +
    1.40 +	const char *fname, *slash;
    1.41 +	if((slash = strrchr(name, '/'))) {
    1.42 +		fname = slash + 1;
    1.43 +	} else {
    1.44 +		fname = name;
    1.45 +	}
    1.46 +
    1.47 +	std::string path = fname;
    1.48 +	if(path.empty()) {
    1.49 +		fprintf(stderr, "can't find data file: %s\n", name);
    1.50 +		return 0;
    1.51 +	}
    1.52 +
    1.53 +	T res = load(path.c_str());
    1.54 +	if(res) {
    1.55 +		data[name] = res;
    1.56 +	}
    1.57 +	return res;
    1.58 +}