| 1 | /* |
| 2 | This file is part of the GVars3 Library. |
| 3 | |
| 4 | Copyright (C) 2005 The Authors |
| 5 | |
| 6 | This library is free software, see LICENSE file for details |
| 7 | */ |
| 8 | |
| 9 | #include "gvars3/gvars3.h" |
| 10 | #include <vector> |
| 11 | #include <algorithm> |
| 12 | |
| 13 | #ifndef WIN32 |
| 14 | #include <fnmatch.h> |
| 15 | #else |
| 16 | // FIXME: empty dummy implementation for now |
| 17 | int fnmatch(const char *, const char *, int ){ |
| 18 | return -1; |
| 19 | } |
| 20 | #define FNM_CASEFOLD 0 |
| 21 | #endif |
| 22 | |
| 23 | using namespace std; |
| 24 | |
| 25 | namespace GVars3 |
| 26 | { |
| 27 | |
| 28 | std::map<std::string, std::string> GV3::unmatched_tags; |
| 29 | std::map<std::string, std::pair<BaseMap*,int> > GV3::registered_type_and_trait; |
| 30 | std::list<BaseMap*> GV3::maps; |
| 31 | |
| 32 | |
| 33 | void GV3::add_typemap(BaseMap* m) |
| 34 | { |
| 35 | maps.push_back(m); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | string GV3::get_var(string name) |
| 40 | { |
| 41 | if(registered_type_and_trait.count(name)) |
| 42 | return registered_type_and_trait[name].first->get_as_string(name, 0); |
| 43 | else if(unmatched_tags.count(name)) |
| 44 | return unmatched_tags[name]; |
| 45 | else |
| 46 | return "(Not present in GVar list.)"; |
| 47 | } |
| 48 | |
| 49 | bool GV3::set_var(string name, string val, bool silent) |
| 50 | { |
| 51 | if(registered_type_and_trait.count(name)) |
| 52 | { |
| 53 | int e = registered_type_and_trait[name].first->set_from_string(name, val); |
| 54 | if(!silent) |
| 55 | parse_warning(e, registered_type_and_trait[name].first->name(), name, val); |
| 56 | return e==0; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | unmatched_tags[name]=val; |
| 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void GV3::print_var_list(ostream& o, string pattern, bool show_all) |
| 66 | { |
| 67 | bool no_pattern = (pattern==""); |
| 68 | |
| 69 | if(show_all) |
| 70 | o << "//Registered GVars:" << endl; |
| 71 | |
| 72 | for(map<string, std::pair<BaseMap*,int> >::iterator i=registered_type_and_trait.begin(); i != registered_type_and_trait.end(); i++) |
| 73 | if(show_all || !(i->second.second & HIDDEN)) |
| 74 | if(no_pattern || !fnmatch(pattern.c_str(), i->first.c_str(), FNM_CASEFOLD)) |
| 75 | o << i->first << "=" << i->second.first->get_as_string(i->first, 1) << endl; |
| 76 | |
| 77 | if(show_all) |
| 78 | { |
| 79 | o << "//Unmatched tags:" << endl; |
| 80 | |
| 81 | for(map<string,string>::iterator i=unmatched_tags.begin(); i != unmatched_tags.end(); i++) |
| 82 | if(no_pattern || !fnmatch(pattern.c_str(), i->first.c_str(), FNM_CASEFOLD)) |
| 83 | o << i->first << "=" << i->second << endl; |
| 84 | |
| 85 | o << "// End of GVar list." << endl; |
| 86 | }; |
| 87 | |
| 88 | } |
| 89 | |
| 90 | vector<string> GV3::tag_list() |
| 91 | { |
| 92 | vector<string> v; |
| 93 | for(map<string, std::pair<BaseMap*, int> >::iterator i=registered_type_and_trait.begin(); i != registered_type_and_trait.end(); i++) |
| 94 | v.push_back(i->first); |
| 95 | |
| 96 | return v; |
| 97 | } |
| 98 | |
| 99 | void parse_warning(int e, string type, string name, string from) |
| 100 | { |
| 101 | if(e > 0) |
| 102 | std::cerr << "! GV3:Parse error setting " << type << " " << name << " from " << from << std::endl; |
| 103 | else if (e < 0) |
| 104 | std::cerr << "! GV3:Parse warning setting " << type << " " << name << " from " << from << ": " |
| 105 | << "junk is -->" << from.c_str()-e << "<--" << std::endl; |
| 106 | } |
| 107 | }; |