stratgame
annotate 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 |
rev | line source |
---|---|
nuclear@2 | 1 #include <string.h> |
nuclear@2 | 2 #include <string> |
nuclear@2 | 3 #include <vector> |
nuclear@2 | 4 #include <stack> |
nuclear@2 | 5 #ifdef _MSC_VER |
nuclear@2 | 6 #include <malloc.h> |
nuclear@2 | 7 #else |
nuclear@2 | 8 #include <alloca.h> |
nuclear@2 | 9 #endif |
nuclear@2 | 10 #include "datapath.h" |
nuclear@2 | 11 |
nuclear@2 | 12 using namespace std; |
nuclear@2 | 13 |
nuclear@2 | 14 static vector<string> first; |
nuclear@2 | 15 static stack<vector<string>> dpath_stack; |
nuclear@2 | 16 |
nuclear@2 | 17 static void init() |
nuclear@2 | 18 { |
nuclear@2 | 19 if(dpath_stack.empty()) { |
nuclear@2 | 20 dpath_stack.push(first); |
nuclear@2 | 21 } |
nuclear@2 | 22 } |
nuclear@2 | 23 |
nuclear@2 | 24 void reset_data_path() |
nuclear@2 | 25 { |
nuclear@2 | 26 init(); |
nuclear@2 | 27 |
nuclear@2 | 28 dpath_stack.top().clear(); |
nuclear@2 | 29 } |
nuclear@2 | 30 |
nuclear@2 | 31 void push_data_path() |
nuclear@2 | 32 { |
nuclear@2 | 33 init(); |
nuclear@2 | 34 |
nuclear@2 | 35 dpath_stack.push(dpath_stack.top()); |
nuclear@2 | 36 } |
nuclear@2 | 37 |
nuclear@2 | 38 void pop_data_path() |
nuclear@2 | 39 { |
nuclear@2 | 40 init(); |
nuclear@2 | 41 |
nuclear@2 | 42 dpath_stack.pop(); |
nuclear@2 | 43 } |
nuclear@2 | 44 |
nuclear@2 | 45 void add_data_path(const char *path) |
nuclear@2 | 46 { |
nuclear@2 | 47 init(); |
nuclear@2 | 48 |
nuclear@2 | 49 char *tmp = (char*)alloca(strlen(path) + 1); |
nuclear@2 | 50 strcpy(tmp, path); |
nuclear@2 | 51 |
nuclear@2 | 52 char *endp = strrchr(tmp, '/'); |
nuclear@2 | 53 if(endp) { |
nuclear@2 | 54 *endp = 0; |
nuclear@2 | 55 } |
nuclear@2 | 56 |
nuclear@2 | 57 dpath_stack.top().push_back(tmp); |
nuclear@2 | 58 } |
nuclear@2 | 59 |
nuclear@2 | 60 bool find_file(const char *fname, char *path) |
nuclear@2 | 61 { |
nuclear@2 | 62 FILE *fp; |
nuclear@2 | 63 |
nuclear@2 | 64 init(); |
nuclear@2 | 65 |
nuclear@2 | 66 for(size_t i=0; i<dpath_stack.top().size(); i++) { |
nuclear@2 | 67 sprintf(path, "%s/%s", dpath_stack.top()[i].c_str(), fname); |
nuclear@2 | 68 |
nuclear@2 | 69 if((fp = fopen(path, "r"))) { |
nuclear@2 | 70 fclose(fp); |
nuclear@2 | 71 return true; |
nuclear@2 | 72 } |
nuclear@2 | 73 } |
nuclear@2 | 74 |
nuclear@2 | 75 if((fp = fopen(fname, "r"))) { |
nuclear@2 | 76 fclose(fp); |
nuclear@2 | 77 strcpy(path, fname); |
nuclear@2 | 78 return true; |
nuclear@2 | 79 } |
nuclear@2 | 80 return false; |
nuclear@2 | 81 } |