stratgame

diff common/src/datapath.cc @ 2:369b51c9e4a8

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 23 May 2012 07:25:43 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/common/src/datapath.cc	Wed May 23 07:25:43 2012 +0300
     1.3 @@ -0,0 +1,81 @@
     1.4 +#include <string.h>
     1.5 +#include <string>
     1.6 +#include <vector>
     1.7 +#include <stack>
     1.8 +#ifdef _MSC_VER
     1.9 +#include <malloc.h>
    1.10 +#else
    1.11 +#include <alloca.h>
    1.12 +#endif
    1.13 +#include "datapath.h"
    1.14 +
    1.15 +using namespace std;
    1.16 +
    1.17 +static vector<string> first;
    1.18 +static stack<vector<string>> dpath_stack;
    1.19 +
    1.20 +static void init()
    1.21 +{
    1.22 +	if(dpath_stack.empty()) {
    1.23 +		dpath_stack.push(first);
    1.24 +	}
    1.25 +}
    1.26 +
    1.27 +void reset_data_path()
    1.28 +{
    1.29 +	init();
    1.30 +
    1.31 +	dpath_stack.top().clear();
    1.32 +}
    1.33 +
    1.34 +void push_data_path()
    1.35 +{
    1.36 +	init();
    1.37 +
    1.38 +	dpath_stack.push(dpath_stack.top());
    1.39 +}
    1.40 +
    1.41 +void pop_data_path()
    1.42 +{
    1.43 +	init();
    1.44 +
    1.45 +	dpath_stack.pop();
    1.46 +}
    1.47 +
    1.48 +void add_data_path(const char *path)
    1.49 +{
    1.50 +	init();
    1.51 +
    1.52 +	char *tmp = (char*)alloca(strlen(path) + 1);
    1.53 +	strcpy(tmp, path);
    1.54 +
    1.55 +	char *endp = strrchr(tmp, '/');
    1.56 +	if(endp) {
    1.57 +		*endp = 0;
    1.58 +	}
    1.59 +
    1.60 +	dpath_stack.top().push_back(tmp);
    1.61 +}
    1.62 +
    1.63 +bool find_file(const char *fname, char *path)
    1.64 +{
    1.65 +	FILE *fp;
    1.66 +
    1.67 +	init();
    1.68 +
    1.69 +	for(size_t i=0; i<dpath_stack.top().size(); i++) {
    1.70 +		sprintf(path, "%s/%s", dpath_stack.top()[i].c_str(), fname);
    1.71 +
    1.72 +		if((fp = fopen(path, "r"))) {
    1.73 +			fclose(fp);
    1.74 +			return true;
    1.75 +		}
    1.76 +	}
    1.77 +
    1.78 +	if((fp = fopen(fname, "r"))) {
    1.79 +		fclose(fp);
    1.80 +		strcpy(path, fname);
    1.81 +		return true;
    1.82 +	}
    1.83 +	return false;
    1.84 +}