PluginConfigValues.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:10k
- /*
- * ===========================================================================
- * PRODUCTION $Log: PluginConfigValues.cpp,v $
- * PRODUCTION Revision 1000.2 2004/06/01 20:43:00 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.4
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: PluginConfigValues.cpp,v 1000.2 2004/06/01 20:43:00 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.
- *
- * ===========================================================================
- *
- * Author: Robert G. Smith
- *
- * File Description:
- * CPluginConfigValues represents one named set of key/value pairs
- * used to configure a plugin. It is identified by its 'item' and 'style' strings.
- * It is intended that 'item' would refer to the type of the data and 'style'
- * to particular saved data values. i.e. all those CPluginConfigValues that have
- * the same 'item' would have the same 'key's and structure in their 'keyvals'.
- *
- * The values and in the key/value pairs can be strings or recursive lists of
- * key/value pairs. Values at nested levels can be accessed and modified by
- * using compound keys (e.g. "algo.blast.database").
- *
- * Remark:
- * This code was originally generated by application DATATOOL
- * using specifications from the data definition file
- * 'plugin.asn'.
- */
- // standard includes
- // generated includes
- #include <ncbi_pch.hpp>
- #include <gui/config/PluginConfigValues.hpp>
- #include <gui/config/KeyValue.hpp>
- #include <gui/config/PluginConfigID.hpp>
- // generated classes
- BEGIN_NCBI_SCOPE
- BEGIN_objects_SCOPE // namespace ncbi::objects::
- //
- // simple functor to check if a type matches a given value
- //
- struct SConfigIdTypeMatches
- {
- SConfigIdTypeMatches(const string& val) : m_Key(val) {}
- bool operator() (const CRef<CPluginConfigID>& cid) const
- {
- return cid->TypeMatches(m_Key);
- }
- private:
- const string& m_Key;
- };
- // destructor
- CPluginConfigValues::~CPluginConfigValues(void)
- {
- }
- // merge the other_pcv with this one, over writing values in this one
- // when the key is the same.
- CPluginConfigValues& CPluginConfigValues::Merge(const CPluginConfigValues& other_pcv)
- {
- // we do not over write the key fields, the type and style.
- // usually this will be called with the PCVs with the same key fields.
- if (this == &other_pcv)
- return *this;
-
- /* // TODO? merge refer-to based on a paramter.
- // make optional refer-to-style the same as in our argument.
- if (other_pcv.CanGetRefer_to_style()) {
- SetRefer_to_style(other_pcv.GetRefer_to_style());
- } else if (! other_pcv.IsSetRefer_to_style()) {
- ResetRefer_to_style();
- }
- */
- if (other_pcv.CanGetByKey()) {
- const TData::TKeyvals& other_data = other_pcv.GetData().GetKeyvals();
- ITERATE(TData::TKeyvals, other_kv_it, other_data) {
- const CKeyValue& other_kv = **other_kv_it;
- if (other_kv.CanGetKey()) {
- CKeyValue& my_kv = AddKey(other_kv.GetKey());
- my_kv.Merge(other_kv);
- }
- }
- } else if (other_pcv.CanGetByType()) {
- const TData::TInclude& other_data = other_pcv.GetData().GetInclude();
- ITERATE(TData::TInclude, other_id_it, other_data) {
- AddPCId(**other_id_it);
- }
- }
- return *this;
- }
-
- // Given a key of type: "label.label2.label3"
- // search our nested hierarchy of key-value pairs and return the pair that corresponds.
- const CKeyValue& CPluginConfigValues::GetKeyvalueByKey(const string & key, const string& delim) const
- {
- // Split the key into the part before the first delimiter and the part after.
- string key1, key2;
- NStr::SplitInTwo(key, delim, key1, key2);
- _ASSERT( ! key1.empty()); // no empty key parts.
-
- // using the first part of the key, find the key/value pair that corresponds.
- const CKeyValue::C_Val::TKeyvals& keyvals = GetData().GetKeyvals();
- CKeyValue::C_Val::TKeyvals::const_iterator kv_it =
- CKeyValue::s_FindKeyvalueByKey(key1, keyvals);
- if (kv_it == keyvals.end())
- ThrowUnassigned(1);
- const CKeyValue& keyvalue = **kv_it;
-
- // more key? Tell the key/value to look deeper within.
- if ( ! key2.empty()) {
- return keyvalue.GetKeyvalueByKey(key2, delim);
- }
- return keyvalue;
- }
- const CPluginConfigID& CPluginConfigValues::GetPCIdByType(const string& type) const
- {
- // _ASSERT(CanGetByType());
-
- const TData::TInclude& pcIds = GetData().GetInclude();
-
- TData::TInclude::const_iterator pcId_it =
- find_if(pcIds.begin(), pcIds.end(),
- SConfigIdTypeMatches(type));
- if (pcId_it == pcIds.end()) {
- ThrowUnassigned(1);
- }
- return **pcId_it;
-
- }
- /// Return a list of all the keys or types in this PCC.
- /// use delim to separate parts of a hierachical key
- void CPluginConfigValues::GetAllKeys(CKeyValue::TKeyList& key_list, const string& delim ) const
- {
- if (CanGetData()) {
- if (GetData().IsKeyvals()) {
- ITERATE(TData::TKeyvals, kv_it, GetData().GetKeyvals()) {
- (*kv_it)->GetAllKeys(key_list, kEmptyStr, delim);
- }
- } else if (GetData().IsInclude()) {
- ITERATE(TData::TInclude, incl_it, GetData().GetInclude()) {
- const CPluginConfigID &pcid = **incl_it;
- if (pcid.CanGetType()) {
- key_list.push_back(pcid.GetType());
- }
- }
- }
- }
- }
- CKeyValue& CPluginConfigValues::AddKey(const string& key, const string& delim)
- {
- // Split the key into the part before the first delimiter and the part after.
- string key1, key2;
- NStr::SplitInTwo(key, delim, key1, key2);
- _ASSERT( ! key1.empty()); // no empty key parts.
-
- // add the first key to my list of key-values.
- CKeyValue& keyvalue = CKeyValue::s_AddKey(key1, SetData().SetKeyvals());
- if ( ! key2.empty()) {
- // add the rest of the key to the key-value just added (or found).
- return keyvalue.AddKey(key2);
- }
- return keyvalue;
- }
- CPluginConfigID& CPluginConfigValues::AddPCId(const string& type, const string& style)
- {
- TData::TInclude& pcIds = SetData().SetInclude();
-
- TData::TInclude::iterator pcId_it =
- find_if(pcIds.begin(), pcIds.end(),
- SConfigIdTypeMatches(type));
- if (pcId_it == pcIds.end()) {
- CRef<CPluginConfigID> pcId_new(new CPluginConfigID(type, style));
- pcIds.push_back(pcId_new);
- return *pcId_new;
- }
- (*pcId_it)->SetStyle(style);
- return **pcId_it;
- }
- // delete key value pairs.
- bool CPluginConfigValues::DelKeyval(const string & key, const string& delim)
- {
- if ( ! SetData().IsKeyvals()) {
- // recursion following include's requires PluginConfigCache.
- return false;
- }
-
- // Split the key into the part before the first delimiter and the part after.
- string key1, key2;
- NStr::SplitInTwo(key, delim, key1, key2);
- _ASSERT( ! key1.empty()); // no empty key parts.
-
- // using the first part of the key, find the key/value pair that corresponds.
- TData::TKeyvals& keyvals = SetData().SetKeyvals();
- TData::TKeyvals::iterator kv_it =
- CKeyValue::s_FindKeyvalueByKey(key1, keyvals);
- if (kv_it == keyvals.end()) {
- return false;
- }
-
- // found what we were looking for. Delete it.
- if ( key2.empty()) {
- keyvals.erase(kv_it);
- return true;
- }
- // Tell the KeyValue to delete the rest of the key.
- if ((*kv_it)->DelKeyval(key2, delim)) {
- // found and deleted. Now, is *kv_it itself empty?
- if ((*kv_it)->GetVal().GetKeyvals().empty()) {
- keyvals.erase(kv_it);
- }
- return true;
- }
- return false;
- }
- END_objects_SCOPE // namespace ncbi::objects::
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- *
- * $Log: PluginConfigValues.cpp,v $
- * Revision 1000.2 2004/06/01 20:43:00 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.4
- *
- * Revision 1.4 2004/05/21 22:27:39 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.3 2004/04/20 14:06:39 rsmith
- * add GetAllKeys method.
- *
- * Revision 1.2 2003/11/21 12:49:12 rsmith
- * Add ability to delete values by key.
- *
- * Revision 1.1 2003/10/10 17:43:04 rsmith
- * moved from gui/plugin to gui/config
- *
- * Revision 1.7 2003/08/19 18:36:08 rsmith
- * use SplitInTwo instead of duplicating code.
- *
- * Revision 1.6 2003/08/19 18:12:05 rsmith
- * can always add anything. CanAddKeys() and CanAddPCId() only advisory.
- *
- * Revision 1.5 2003/08/13 20:36:47 rsmith
- * get rid of include of find_if.hpp
- *
- * Revision 1.4 2003/08/13 20:24:20 rsmith
- * Merge method and get rid of FindIf
- *
- * Revision 1.3 2003/08/06 13:10:31 dicuccio
- * Replaced std::find_if() with FindIf() - work-around for MSVC's broken STL
- * implementation (no const_mem_fun1_t<>)
- *
- * Revision 1.2 2003/08/05 19:01:36 rsmith
- * change members used in mem_fun to satisfy certain compilers.
- *
- * Revision 1.1 2003/08/05 17:35:59 rsmith
- * Classes to allow saving of plugin's configuration values as ASN.1.
- *
- *
- * ===========================================================================
- */
- /* Original file checksum: lines: 64, chars: 1904, CRC32: 7925bcd */