📄 keyvalue.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: KeyValue.cpp,v $ * PRODUCTION Revision 1000.2 2004/06/01 20:42:51 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.4 * PRODUCTION * =========================================================================== *//* $Id: KeyValue.cpp,v 1000.2 2004/06/01 20:42:51 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: * CKeyValue represents a key/value pair, where the key is a string * and the value can be a string or a recursive list of key/value pairs. * Values can be retrieved and stored with compound keys (e.g. "view.grapic.color.gene") * to get at deeper levels. * * * 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/KeyValue.hpp>// generated classesBEGIN_NCBI_SCOPEBEGIN_objects_SCOPE // namespace ncbi::objects::CKeyValue::CKeyValue(const string& key){ SetKey(key);}CKeyValue::CKeyValue(const string& key, const string& value){ SetKey(key); SetVal().SetStr(value);}// destructorCKeyValue::~CKeyValue(void){}// Merge an other key value object into this one.// the other one's values take precedence.// ignore the key value, it should be the same.CKeyValue& CKeyValue::Merge(const CKeyValue& other_kv){ if (this == &other_kv) return *this; if (other_kv.CanGetVal()) { const TVal& other_val = other_kv.GetVal(); if (other_val.IsStr()) { SetVal().SetStr(other_val.GetStr()); } else if (other_val.IsKeyvals()) { const TVal::TKeyvals& other_kvs = other_val.GetKeyvals(); ITERATE(TVal::TKeyvals, other_kv_it, other_kvs) { const CKeyValue& other_kv = **other_kv_it; if (other_kv.CanGetKey()) { CKeyValue& my_kv = AddKey(other_kv.GetKey()); my_kv.Merge(other_kv); } } } } return *this;} // non-trivial Accessor methods.// Assuming this KeyValue holds a list of KeyValues, not just a string// search for one with a matching key.const CKeyValue& CKeyValue::GetKeyvalueByKey1(const string & key) const{ const TVal::TKeyvals& keyvals = GetVal().GetKeyvals(); TVal::TKeyvals::const_iterator kv_it = s_FindKeyvalueByKey(key, keyvals); if (kv_it == keyvals.end()) ThrowUnassigned(1); return **kv_it;}// Given a key of type: "label.label2.label3"// search our nested hierarchy of key-value pairs and return the pair that corresponds.const CKeyValue& CKeyValue::GetKeyvalueByKey(const string & key, const string& delim) const{ _ASSERT(delim.size() == 1); // Split the key. list<string> keys; NStr::Split(key, delim, keys); _ASSERT (! keys.empty()); CConstRef<CKeyValue> kvp(this); ITERATE( list<string>, key_it, keys) { const TVal::TKeyvals& keyvals = kvp->GetVal().GetKeyvals(); TVal::TKeyvals::const_iterator kv_it = s_FindKeyvalueByKey(*key_it, keyvals); if (kv_it == keyvals.end()) ThrowUnassigned(1); kvp = *kv_it; } return *kvp;}void CKeyValue::GetAllKeys(TKeyList& key_list, const string& key_prefix, const string& delim ) const{ if (CanGetKey() && CanGetVal()) { string my_key = GetKey(); if ( ! key_prefix.empty()) { // don't put a delimiter in front by itself. my_key = key_prefix + delim + my_key; } if (GetVal().IsStr()) { key_list.push_back(my_key); } else if (GetVal().IsKeyvals()) { ITERATE(TVal::TKeyvals, kv_it, GetVal().GetKeyvals()) { (*kv_it)->GetAllKeys(key_list, my_key, delim); } } }}// Mutator (Set) methodsCKeyValue& CKeyValue::s_AddKey(const string & key, TVal::TKeyvals& keyvals){ _ASSERT(! key.empty()); TVal::TKeyvals::iterator kv_it = s_FindKeyvalueByKey(key, keyvals); if (kv_it == keyvals.end()) { // key not found, add it. CRef<CKeyValue> kv( new CKeyValue(key) ); keyvals.push_back(kv); return *kv; } // found. Just return it since no value to set. return **kv_it;}CKeyValue& CKeyValue::AddKey(const string& key, const string& delim){ _ASSERT(delim.size() == 1); // Split the key. list<string> keys; NStr::Split(key, delim, keys); _ASSERT (! keys.empty()); CRef<CKeyValue> kvp(this); ITERATE( list<string>, key_it, keys) { kvp = & s_AddKey(*key_it, kvp->SetVal().SetKeyvals()); } return *kvp;}// delete the keyval within this CKeyValue that has a particular key.// return true if key exists, false otherwise.bool CKeyValue::DelKeyval(const string& key, const string& delim){ // If there is no list of Keyvals here, there is nothing to delete. if ( ! GetVal().IsKeyvals()) { 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. TVal::TKeyvals& keyvals = SetVal().SetKeyvals(); TVal::TKeyvals::iterator kv_it = s_FindKeyvalueByKey(key1, keyvals); if (kv_it == keyvals.end()) { // key not found, nothing to delete. return false; } if (key2.empty()) { // here is what we were looking for. keyvals.erase(kv_it); return true; } // recurse with the rest of the key on the found CKeyValue 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: KeyValue.cpp,v $* Revision 1000.2 2004/06/01 20:42:51 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.3 2003/08/19 18:09:04 rsmith* add delim argument to AddKey and AddKeyString.** Revision 1.2 2003/08/13 20:18:53 rsmith* Merge two KeyValue objects method.** 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: 1874, CRC32: 5a5b30be */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -