settings.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:10k
- /*
- * ===========================================================================
- * PRODUCTION $Log: settings.cpp,v $
- * PRODUCTION Revision 1000.1 2004/06/01 20:43:22 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.5
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: settings.cpp,v 1000.1 2004/06/01 20:43:22 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: Mike DiCuccio, Robert Smith
- *
- * File Description:
- * CSettings -- Repository for application settings.
- */
- #include <ncbi_pch.hpp>
- #include <gui/config/settings.hpp>
- #include <gui/utils/system_path.hpp>
- #include <serial/serial.hpp>
- #include <serial/objostr.hpp>
- #include <serial/objistr.hpp>
- #include <corelib/ncbifile.hpp>
- BEGIN_NCBI_SCOPE
- USING_SCOPE(objects);
- namespace {
- const string kDefaultConfigFileName("plugin_config.asn");
- }
- CSettings::CSettings()
- : m_PCC(NULL),
- m_PluginConfigSaveFile(CSystemPath::ResolvePath("<home>", kDefaultConfigFileName))
- {
- }
- bool CSettings::IsSetPluginConfig(void) const
- {
- return m_PCC;
- }
- bool CSettings::CanGetPluginConfig(void) const
- {
- return IsSetPluginConfig();
- }
- const CPluginConfigCache& CSettings::GetPluginConfig(void) const
- {
- return *m_PCC;
- }
- CPluginConfigCache& CSettings::SetPluginConfig(void)
- {
- if (! IsSetPluginConfig() ) {
- m_PCC.Reset( new CPluginConfigCache() );
- }
- return *m_PCC;
- }
- void CSettings::SetPluginConfig(CPluginConfigCache& pcc)
- {
- m_PCC.Reset(&pcc);
- }
- void CSettings::ShutDown(void)
- {
- SavePluginConfig();
- }
- void CSettings::SavePluginConfig(const string& pcfile)
- {
- if (CanGetPluginConfig()) {
- WritePluginConfig(pcfile, GetPluginConfig());
- }
- }
- void CSettings::SavePluginConfig(void)
- {
- if (CanGetPluginConfig()) {
- WritePluginConfig(GetPluginConfigWriteFile(), GetPluginConfig());
- }
- }
- string CSettings::SetPluginConfigWriteFile(const string file_name)
- {
- string old_file(m_PluginConfigSaveFile);
- m_PluginConfigSaveFile = file_name;
- return old_file;
- }
- string CSettings::GetPluginConfigWriteFile() const
- {
- return m_PluginConfigSaveFile;
- }
- void CSettings::LoadPluginConfig(void)
- {
-
- LoadPluginConfigFromString(GetBuiltInPluginConfig());
- const string pcfile(kDefaultConfigFileName);
- string pcc_path;
-
- pcc_path = CDirEntry::MakePath(CSystemPath::ResolvePath("<std>", "etc"), pcfile);
- LoadPluginConfig(pcc_path);
- pcc_path = CDirEntry::MakePath(CSystemPath::ResolvePath("<home>", ""), pcfile);
- LoadPluginConfig(pcc_path);
- }
- void CSettings::LoadPluginConfigs(const string& pcfiles)
- {
- list<string> paths;
- NStr::Split(pcfiles, ",tnr", paths);
- ITERATE(list<string>, pcfile_it, paths) {
- if ( ! (pcfile_it->empty())) {
- LoadPluginConfig(*pcfile_it);
- }
- }
- return;
- }
- void CSettings::LoadPluginConfig(const string& pc_file)
- {
- string pcfile = CSystemPath::ResolvePath(pc_file);
- CRef<CPluginConfigCache> config_cache(new CPluginConfigCache);
-
- if (ReadPluginConfig(pc_file, *config_cache)) {
- x_LoadPluginConfig(*config_cache);
- }
- }
- void CSettings::x_LoadPluginConfig(CPluginConfigCache& pcc)
- {
- if ( IsSetPluginConfig()) {
- SetPluginConfig().Merge(pcc);
- } else {
- SetPluginConfig(pcc);
- }
- return;
- }
- void CSettings::WritePluginConfig(const string& pcfile, const CPluginConfigCache& pcc)
- {
- try {
- auto_ptr<CObjectOStream> os(CObjectOStream::Open(eSerial_AsnText, pcfile));
- *os << pcc;
- LOG_POST( Info << "CSettings::SavePluginConfig(): "
- "plugin config cache written to: " << pcfile);
- } catch (const CException& e) {
- LOG_POST( Error << "CSettings::SavePluginConfig(): "
- "Write of plugin config cache to " << pcfile << " failed.");
- LOG_POST( Error << e.what());
- }
-
- return;
- }
- bool CSettings::ReadPluginConfig(const string& pcfile, objects::CPluginConfigCache& config_cache)
- {
- {{
- // first check if the file exists.
- CFile file(pcfile);
- if ( ! file.Exists() ) {
- LOG_POST( Info << "CSettings::ReadPluginConfig(): "
- "no plugin config cache found at " << pcfile);
- return false;
- }
- }}
-
- // try reading in ASN.1 test format
- try {
- auto_ptr<CObjectIStream> is(CObjectIStream::Open(eSerial_AsnText, pcfile));
-
- *is >> config_cache;
- LOG_POST( Info << "CSettings::ReadPluginConfig(): "
- "text plugin config cache read from: " << pcfile);
- return true;
- } catch (...) {
- _TRACE("read of ASN.1 text plugin config cache in " << pcfile << " failed");
- }
-
- // try reading in ASN.1 binary format
- try {
- auto_ptr<CObjectIStream> is(CObjectIStream::Open(eSerial_AsnBinary, pcfile));
-
- *is >> config_cache;
- LOG_POST( Info << "CSettings::ReadPluginConfig(): "
- "binary plugin config cache read from: " << pcfile);
- return true;
- } catch (...) {
- _TRACE("read of ASN.1 binary plugin config cache in " << pcfile << " failed");
- }
- // if not, try reading in XML
- try {
- auto_ptr<CObjectIStream> is(CObjectIStream::Open(eSerial_Xml, pcfile));
-
- *is >> config_cache;
- LOG_POST( Info << "CSettings::ReadPluginConfig(): "
- "XML plugin config cache read from: " << pcfile);
- return true;
- } catch (...) {
- _TRACE("read of XML plugin config cache in " << pcfile << " failed");
- }
-
- // Log an error and continue.
- LOG_POST( Error << "CSettings::ReadPluginConfig(): "
- "File does not contain a plugin config cache: " << pcfile);
-
- return false;
- }
- /// method to allocate and create a PCC from a string with the asn1 source for it.
- void CSettings::LoadPluginConfigFromString(const string& asn1_str)
- {
- CRef<CPluginConfigCache> config_cache(new CPluginConfigCache);
-
- if (ReadPluginConfigFromString(asn1_str, *config_cache)) {
- x_LoadPluginConfig(*config_cache);
- }
- }
- bool CSettings::ReadPluginConfigFromString(const string& asn1_str,
- objects::CPluginConfigCache& config_cache)
- {
- try {
- CNcbiIstrstream is(asn1_str.c_str(), strlen(asn1_str.c_str()));
- auto_ptr<CObjectIStream>
- pcc_in(CObjectIStream::Open(eSerial_AsnText, is));
- *pcc_in >> config_cache;
- LOG_POST( Info << "CSettings::LoadPluginConfigFromString(): "
- "text plugin config cache read from string: "" << asn1_str.substr(0,20) << " ... "" );
- } catch (...) {
- _TRACE("read of ASN.1 text plugin config cache in string failed. String's first 100 chars are: "
- << asn1_str.substr(0, 100));
- return false;
- }
- return true;
- }
- // defines sc_BuiltInASN.
- #include "settings_builtin.cpp"
- /// Get the string containing our built-in config settings in ASN format.
- string CSettings::GetBuiltInPluginConfig()
- {
- return BuildString(sc_BuiltInASN);
- }
- /// Concatenate a bunch of cstrings into one string.
- /// useful since on some platforms string literals must be of limited size.
- // MSVC++ compiler < 2096.
- string CSettings::BuildString(const char* cstrings[])
- {
- string str;
- for (size_t i = 0; cstrings[i]; i++) {
- str += cstrings[i];
- }
- return str;
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: settings.cpp,v $
- * Revision 1000.1 2004/06/01 20:43:22 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.5
- *
- * Revision 1.5 2004/05/21 22:27:40 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.4 2004/04/02 18:43:19 rsmith
- * comment added for builtin settings.
- *
- * Revision 1.3 2004/02/20 16:11:50 rsmith
- * somewhat better diagnostics on failure to write out settings.
- *
- * Revision 1.2 2004/02/18 17:16:24 dicuccio
- * Use static non-member string array for defaults
- *
- * Revision 1.1 2004/02/17 20:35:19 rsmith
- * moved core/settings.[ch]pp and core/system_path.[ch]pp to config and utils, respectively.
- *
- * Revision 1.8 2004/01/16 21:43:26 rsmith
- * Support for built-in configuration settings.
- *
- * Revision 1.7 2004/01/06 19:53:15 rsmith
- * Add method to read PCC ASN.1 values from strings.
- * Reorganize methods with static methods so that PCC reading and writing could be done
- * without linking in all the gui framework stuff.
- *
- * Revision 1.6 2003/11/06 20:05:09 dicuccio
- * Un-inlined code
- *
- * Revision 1.5 2003/08/25 18:54:01 rsmith
- * took out all the color stuff and made this the manager of the Plugin Config Cache, including reading writing it to disk.
- *
- * Revision 1.4 2003/08/22 15:46:10 dicuccio
- * Removed config file from CSettings - accessible through CNcbiApplication
- *
- * Revision 1.3 2003/05/30 12:56:50 dicuccio
- * Converted code to use MessageBox() instead of fl_ask()/fl_message()/fl_alert()
- *
- * Revision 1.2 2003/02/25 14:47:11 dicuccio
- * Added registry file to CSettings
- *
- * Revision 1.1 2003/01/15 21:06:34 dicuccio
- * Initial revision
- *
- * ===========================================================================
- */