plugin_info_from_reg.cpp

来自「ncbi源码」· C++ 代码 · 共 304 行

CPP
304
字号
/* * =========================================================================== * PRODUCTION $Log: plugin_info_from_reg.cpp,v $ * PRODUCTION Revision 1000.3  2004/06/01 20:56:11  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.7 * PRODUCTION * =========================================================================== *//*  $Id: plugin_info_from_reg.cpp,v 1000.3 2004/06/01 20:56:11 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software/database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software/database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors:  Josh Cherry * * File Description:  Create plugin info objects from registry * */#include <ncbi_pch.hpp>#include "plugin_info_from_reg.hpp"#include <gui/core/plugin_utils.hpp>#include <gui/plugin/PluginValueConstraint.hpp>#include <gui/plugin/PluginCommand.hpp>#include <gui/plugin/PluginCommandSet.hpp>#include <gui/core/selection_buffer.hpp>#include <gui/core/version.hpp>#include <corelib/ncbireg.hpp>#include <util/regexp.hpp>#include <algorithm>BEGIN_NCBI_SCOPEUSING_SCOPE(objects);// compare strings s.t. all foo.bar come right after foo// and compare equal to each otherstruct SCompare{public:    bool operator()(const string& lhs, const string& rhs) const    {        for (unsigned int i = 0;  ; i++) {            if (i == lhs.size()) {                return i != rhs.size();            }            if (i == rhs.size()) {                return false;            }            if (rhs[i] == '.') {                // even if lhs[i] == '.' too                return false;            }            if (lhs[i] == rhs[i]) {                continue;            }            if (lhs[i] == '.') {                return true;            }            return lhs[i] < rhs[i];        }    }};void PluginInfoFromRegistry(const CNcbiRegistry& reg,                            vector<CRef<CPluginInfo> >& plugins,                            const vector<string>& sect_order){    // if sect_order is empty, use the order returned by    // EnumerateSections    vector<string> sections;    if (sect_order.empty()) {        list<string> sec_tmp;        reg.EnumerateSections(&sec_tmp);        sections.reserve(sec_tmp.size());        ITERATE (list<string>, iter, sec_tmp) {            sections.push_back(*iter);        }    } else {        sections = sect_order;    }    std::stable_sort(sections.begin(), sections.end(), SCompare());    vector<string>::iterator section = sections.begin();    while (section != sections.end()) {        CRef<CPluginInfo> info(new CPluginInfo);        // the name used in section headings; may have no        // relationship to the "class name"        string plugin_name = *section;        // basic info from this plugin        int ver_major = reg.GetInt(plugin_name, "ver-major", 0);        int ver_minor = reg.GetInt(plugin_name, "ver-minor", 0);        int ver_revision = reg.GetInt(plugin_name, "ver-revision", 0);        string ver_build_date = reg.GetString(plugin_name,                                              "ver-build-date", "unknown");        string class_name = reg.Get(plugin_name, "class-name");        string menu_item = reg.Get(plugin_name, "menu-item");        string tool_tip = reg.Get(plugin_name, "tool-tip");        string help_file = reg.Get(plugin_name, "help-file");            info->SetInfo(ver_major, ver_minor, ver_revision, ver_build_date,                      class_name, menu_item, tool_tip, help_file);        CPluginCommandSet& cmds = info->SetCommands();        CPluginCommand&    args = cmds.AddAlgoCommand(eAlgoCommand_run);        // each section of form *section + '.' + something defines        // one plugin argument        ++section;        while (1) {            if (section == sections.end() ||                !NStr::StartsWith(*section, plugin_name + '.')) {                break;            }            string name = reg.Get(*section, "name");            string desc = reg.Get(*section, "desc");            // a default value, if present            string value = reg.Get(*section, "value");            string type_str = reg.Get(*section, "type");            bool optional = reg.GetBool(*section, "optional", false);            // whether it's an array (otherwise single)            bool array = reg.GetBool(*section, "array", false);            bool hidden = reg.GetBool(*section, "hidden", false);            string constraint_set = reg.Get(*section, "constraint-set");            // now some consistency checks            if (array && !value.empty()) {                throw runtime_error("array parameter can't have "                                    "default value");            }            if (hidden && value.empty() && !optional) {                throw runtime_error("non-optional hidden argument "                                    "must have a default value");            }            // figure out the argument's type            CPluginArg::EType type;            if (type_str == "boolean") {                type = CPluginArg::eBoolean;            } else if (type_str == "double") {                type = CPluginArg::eDouble;            } else if (type_str == "integer") {                type = CPluginArg::eInteger;            } else if (type_str == "string") {                type = CPluginArg::eString;            } else if (type_str == "seq" || type_str == "naseq"                       || type_str == "aaseq") {                type = CPluginArg::eObject;            } else {                throw runtime_error(string("unrecognized type: ") + type_str);            }            if (type != CPluginArg::eObject) {                if (value.empty() && !optional) {                    args.AddArgument(name, desc, type,                                     array ? CPluginArg::TData::e_Array                                     : CPluginArg::TData::e_Single);                } else if (optional) {                    args.AddOptionalArgument(name, desc, type,                                             array ? CPluginArg::TData::e_Array                                             : CPluginArg::TData::e_Single);                } else {                    args.AddDefaultArgument(name, desc, type, value);                }                if (hidden) {                    args[name].SetHidden(1);                }                if (!constraint_set.empty()) {                    vector<string> elements;                    NStr::Tokenize(constraint_set, "\n", elements);                    CPluginValueConstraint *code_list =                        CPluginValueConstraint::CreateSet();                    ITERATE (vector<string>, element, elements) {                        code_list->SetSet().push_back(*element);                    }                    args.SetConstraint(name, *code_list);                }            } else {                // a Seq-loc                if (!value.empty()) {                    throw runtime_error("Seq-loc parameter can't have "                                        "default value");                }                if (hidden) {                    throw runtime_error("Seq-loc parameter can't be hidden");                }                args.AddArgument(name, desc,                                 CSeq_loc::GetTypeInfo(),                                 array ? CPluginArg::TData::e_Array                                 : CPluginArg::TData::e_Single);                if (type_str == "naseq") {                    args.SetConstraint                        (name,                         (*CPluginValueConstraint::CreateSeqMol(),                          CSeq_inst::eMol_na,                          CSeq_inst::eMol_dna,                          CSeq_inst::eMol_rna));                } else if (type_str == "aaseq") {                    args.SetConstraint                        (name,                         (*CPluginValueConstraint::CreateSeqMol(),                          CSeq_inst::eMol_aa));                }            }            ++section;        }        plugins.push_back(info);    }}void PluginInfoFromRegistry(CNcbiIstream& istr,                            vector<CRef<CPluginInfo> >& plugins){    CT_POS_TYPE pos = istr.tellg();        // get section names in order    vector<string> sect_order;    string line;    CRegexp re("^ *\\[ *([^ ]*) *\\] *$");    while(getline(istr, line)) {        if (!re.GetMatch(line.c_str(), 0).empty()) {            const int *name_range = re.GetResults(1);            string name = line.substr(name_range[0],                                      name_range[1] - name_range[0]);            sect_order.push_back(name);        }    }    // back up in stream and make a registry    istr.clear();  // because getline failed    istr.seekg(pos);    CNcbiRegistry reg(istr);        PluginInfoFromRegistry(reg, plugins, sect_order);}END_NCBI_SCOPE/* * =========================================================================== * $Log: plugin_info_from_reg.cpp,v $ * Revision 1000.3  2004/06/01 20:56:11  gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.7 * * Revision 1.7  2004/05/21 22:27:47  gorelenk * Added PCH ncbi_pch.hpp * * Revision 1.6  2004/01/27 18:45:24  dicuccio * Added missing header files * * Revision 1.5  2003/12/10 16:08:50  ucko * Change the pos_type substitute (needed on GCC 2.95) to CT_POS_TYPE. * * Revision 1.4  2003/12/10 12:31:22  dicuccio * Replace ios::pos_type with size_t to make Alpha compile happy * * Revision 1.3  2003/12/04 00:36:54  jcherry * Preserve order of plugin parameters specified in registry format * * Revision 1.2  2003/12/02 14:41:02  dicuccio * Fixed compilation errors on MSVC - don't sort std::list() * * Revision 1.1  2003/12/01 23:13:20  jcherry * Initial version * * =========================================================================== */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?