/* Copyright (C) 2004 Stefan van der Walt Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "Index.h" #include #include #ifndef log #define log std::cout #endif std::ostream& operator<<(std::ostream& os, const Index& idx) { toolmap::const_iterator t_iter = idx.toolboxes.begin(); toolmap::const_iterator t_iter_end = idx.toolboxes.end(); while (t_iter != t_iter_end) { os << std::endl << "Toolbox: " << t_iter->first; os << " (" << t_iter->second << ")"; t_iter++; } catmap::const_iterator c_iter = idx.categories.begin(); catmap::const_iterator c_iter_end = idx.categories.end(); while (c_iter != c_iter_end) { os << std::endl << "Category: " << c_iter->first; os << " (" << (*c_iter).second << ")"; c_iter++; } funmap::const_iterator f_iter = idx.functions.begin(); funmap::const_iterator f_iter_end = idx.functions.end(); std::string fn; while (f_iter != f_iter_end) { fn = f_iter->first; os << std::endl << "Function: " << fn; os << " (" << (f_iter->second).second << ":" << (f_iter->second).first << ")"; std::string desc = ((Index&) idx).fn_descriptions[fn]; if (desc.length() != 0) { os << std::endl << " Desc: " << desc; } f_iter++; } varmap::const_iterator v_iter = idx.variables.begin(); varmap::const_iterator v_iter_end = idx.variables.end(); while (v_iter != v_iter_end) { os << std::endl << "Variable: " << (*v_iter).first; os << " (" << (*v_iter).second << ")"; v_iter++; } return os; } bool Index::load(const std::string& fn) { std::ifstream infile(fn.c_str()); if (!infile) { log << "Cannot open file: " << fn << std::endl; return false; } std::string ln; while ( std::getline(infile, ln) ) { ln = uncomment(ln); std::string ln_blank = lstrip(ln); if (ln_blank.length() == 0) continue; if (ln_blank[0] == '$') { parseline(ln_blank, variable); } else if ( ln[0] == ' ' ) { parseline(ln_blank, function); } else { if (ln_blank.find(">>") == std::string::npos) { parseline(ln_blank, category); } else { parseline(ln_blank, toolbox); } } } return true; } void Index::parseline(std::string ln, linetype lt) { static std::string cur_toolbox; static std::string cur_category; catmap::value_type cur_cat_tb = catmap::value_type(cur_category, cur_toolbox); if (lt == function) { std::string func, desc; getvar(ln, func, desc); // more than one function on one line? func = func + " "; std::string::size_type pos = 0, prev_pos = 0; std::string onefunc; while ( (pos = func.find_first_of(' ', pos)) != std::string::npos ) { onefunc = func.substr(prev_pos, pos - prev_pos); while (func[pos] == ' ') prev_pos = ++pos; functions.insert( funmap::value_type( onefunc, cur_cat_tb ) ); } if (desc.length() != 0) { fn_descriptions.insert( varmap::value_type( onefunc, desc ) ); } } else if (lt == variable) { std::string var, val; getvar(ln, var, val); var = var.substr(1); variables.insert( varmap::value_type( var, val ) ); } else if (lt == toolbox) { std::string::size_type pos = ln.find(">>"); std::string toolbox = rstrip( ln.substr(0, pos) ); std::string desc = strip( ln.substr(pos+2) ); toolboxes.insert( toolmap::value_type(toolbox, desc) ); cur_toolbox = toolbox; } else if (lt == category) { cur_category = ln; // check for duplicates std::pair< catmap::iterator, catmap::iterator > pos; pos = categories.equal_range( ln ); bool exists = false; while (pos.first != pos.second) { std::string cat = (*pos.first).first; std::string tb = (*pos.first).second; if ( (cat == cur_category) && (tb == cur_toolbox) ) { exists = true; break; } pos.first++; } if (!exists) { categories.insert( catmap::value_type(ln, cur_toolbox) ); } } } std::vector Index::toolbox_functions(const std::string& toolbox) { std::vector fns; funmap::iterator fi = functions.begin(); funmap::iterator fi_end = functions.end(); while ( fi != fi_end) { catmap::value_type cat_tb = fi->second; if (cat_tb.second == toolbox) { fns.push_back(fi->first); } fi++; } return fns; } std::string Index::function_toolbox(const std::string& function) { catmap::value_type vm = functions[function]; return vm.second; } void Index::getvar(std::string str, std::string& key, std::string& val) { std::string::size_type pos = str.find("="); if (pos == std::string::npos) { key = rstrip(str); val = std::string(); } else { key = rstrip(str.substr(0, pos)); val = str.substr(pos+1); } } /** * Merge two indeces. Use the larger of the two indeces as the first argument. */ Index Index::operator+(const Index &rhs) { Index c(*this); // toolmap toolmap::const_iterator t_iter = rhs.toolboxes.begin(); toolmap::const_iterator t_iter_end = rhs.toolboxes.end(); while (t_iter != t_iter_end) { c.toolboxes.insert(*t_iter); t_iter++; } // catmap -- careful, this is a multimap catmap::const_iterator c_iter = rhs.categories.begin(); catmap::const_iterator c_iter_end = rhs.categories.end(); while (c_iter != c_iter_end) { std::string category = c_iter->first; std::string toolbox = c_iter->second; bool exists = false; if (categories.count(category) != 0) { std::pair pos; pos = categories.equal_range(category); for (catmap::const_iterator p = pos.first; p != pos.second; p++) { if (toolbox == p->second) { exists = true; } } } if (!exists) { c.categories.insert(*c_iter); } c_iter++; } // funmap funmap::const_iterator f_iter = rhs.functions.begin(); funmap::const_iterator f_iter_end = rhs.functions.end(); while (f_iter != f_iter_end) { c.functions.insert(*f_iter); f_iter++; } // varmap varmap::const_iterator v_iter = rhs.variables.begin(); varmap::const_iterator v_iter_end = rhs.variables.end(); while (v_iter != v_iter_end) { c.variables.insert(*v_iter); v_iter++; } return c; }