stratgame

view common/src/datapath.cc @ 5:2e38715de41b

terrain
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 27 May 2012 07:00:48 +0300
parents
children
line source
1 #include <string.h>
2 #include <string>
3 #include <vector>
4 #include <stack>
5 #ifdef _MSC_VER
6 #include <malloc.h>
7 #else
8 #include <alloca.h>
9 #endif
10 #include "datapath.h"
12 using namespace std;
14 static vector<string> first;
15 static stack<vector<string>> dpath_stack;
17 static void init()
18 {
19 if(dpath_stack.empty()) {
20 dpath_stack.push(first);
21 }
22 }
24 void reset_data_path()
25 {
26 init();
28 dpath_stack.top().clear();
29 }
31 void push_data_path()
32 {
33 init();
35 dpath_stack.push(dpath_stack.top());
36 }
38 void pop_data_path()
39 {
40 init();
42 dpath_stack.pop();
43 }
45 void add_data_path(const char *path)
46 {
47 init();
49 char *tmp = (char*)alloca(strlen(path) + 1);
50 strcpy(tmp, path);
52 char *endp = strrchr(tmp, '/');
53 if(endp) {
54 *endp = 0;
55 }
57 dpath_stack.top().push_back(tmp);
58 }
60 bool find_file(const char *fname, char *path)
61 {
62 FILE *fp;
64 init();
66 for(size_t i=0; i<dpath_stack.top().size(); i++) {
67 sprintf(path, "%s/%s", dpath_stack.top()[i].c_str(), fname);
69 if((fp = fopen(path, "r"))) {
70 fclose(fp);
71 return true;
72 }
73 }
75 if((fp = fopen(fname, "r"))) {
76 fclose(fp);
77 strcpy(path, fname);
78 return true;
79 }
80 return false;
81 }