📄 ncbireg.cpp
字号:
const string& name, const string& default_value, TFlags flags) const{ const string& value = Get(section, name, flags); return value.empty() ? default_value : value;}int CNcbiRegistry::GetInt(const string& section, const string& name, int default_value, TFlags flags, EErrAction err_action) const{ const string& value = Get(section, name, flags); if ( value.empty() ) return default_value; try { return NStr::StringToInt(value); } catch (CStringException& ex) { if (err_action == eReturn) return default_value; string msg = "CNcbiRegistry::GetInt()"; msg += " Reg entry:" + section + ":" + name; if (err_action == eThrow) NCBI_RETHROW_SAME(ex, msg); if (err_action == eErrPost) ERR_POST(ex.what() << msg); return default_value; }}bool CNcbiRegistry::GetBool(const string& section, const string& name, bool default_value, TFlags flags, EErrAction err_action) const{ const string& value = Get(section, name, flags); if ( value.empty() ) return default_value; try { return NStr::StringToBool(value); } catch (CStringException& ex) { if (err_action == eReturn) return default_value; string msg = "CNcbiRegistry::GetBool()"; msg += " Reg entry:" + section + ":" + name; if (err_action == eThrow) NCBI_RETHROW_SAME(ex, msg); if (err_action == eErrPost) ERR_POST(ex.what() << msg); return default_value; }}double CNcbiRegistry::GetDouble(const string& section, const string& name, double default_value, TFlags flags, EErrAction err_action) const{ const string& value = Get(section, name, flags); if ( value.empty() ) return default_value; try { return NStr::StringToDouble(value); } catch (CStringException& ex) { if (err_action == eReturn) return default_value; string msg = "CNcbiRegistry::GetDouble()"; msg += " Reg entry:" + section + ":" + name; if (err_action == eThrow) NCBI_RETHROW_SAME(ex, msg); if (err_action == eErrPost) ERR_POST(ex.what() << msg); return default_value; }}bool CNcbiRegistry::Set(const string& section, const string& name, const string& value, TFlags flags, const string& comment){ CHECK_FLAGS("Set", flags, ePersistent | eNoOverride | eTruncate); // Truncate marginal spaces of "section" and "name" // Make sure they aren't empty and consist of alpanum and '_' only string x_section = NStr::TruncateSpaces(section); if ( !s_IsNameSection(x_section) ) { _TRACE("CNcbiRegistry::Set(): bad or empty section name: " + section); return false; } string x_name = NStr::TruncateSpaces(name); // is the entry name valid ? if ( !s_IsNameSection(x_name) ) { _TRACE("CNcbiRegistry::Set(): bad or empty entry name: " + name); return false; } // MT-protect everything down the function code CFastMutexGuard LOCK(s_RegMutex); // find section TRegistry::iterator find_section = m_Registry.find(x_section); if (find_section == m_Registry.end()) { if ( value.empty() ) // the "unset" case return false; // new section, new entry x_SetValue(m_Registry[x_section][x_name], value, flags, comment); return true; } // find entry within the found section TRegSection& reg_section = find_section->second; _ASSERT( !reg_section.empty() ); TRegSection::iterator find_entry = reg_section.find(x_name); if (find_entry == reg_section.end()) { if ( value.empty() ) // the "unset" case return false; // new entry x_SetValue(reg_section[x_name], value, flags, comment); return true; } // modifying an existing entry... if (flags & eNoOverride) return false; // cannot override TRegEntry& entry = find_entry->second; // check if it tries to unset an already unset value bool transient = (flags & ePersistent) == 0; if (value.empty() && (( transient && entry.transient.empty()) || (!transient && entry.persistent.empty()))) return false; // modify an existing entry x_SetValue(entry, value, flags, comment); // unset(remove) the entry, if empty if (entry.persistent.empty() && entry.transient.empty()) { reg_section.erase(find_entry); // remove the section, if empty if (reg_section.empty() ) { m_Registry.erase(find_section); } } return true;}bool CNcbiRegistry::SetComment(const string& comment, const string& section, const string& name){ CFastMutexGuard LOCK(s_RegMutex); // If "section" is empty string, then set as the registry comment string x_section = NStr::TruncateSpaces(section); if (x_section == kEmptyStr) { m_Comment = s_ConvertComment(comment, true); m_Modified = true; return true; } // Find section TRegistry::iterator find_section = m_Registry.find(x_section); if (find_section == m_Registry.end()) { return false; } TRegSection& reg_section = find_section->second; string x_name = NStr::TruncateSpaces(name); string x_comment = s_ConvertComment(comment); // If "name" is empty string, then set as the "section" comment if (name == kEmptyStr) { TRegSection::iterator comm_entry = reg_section.find(kEmptyStr); if (comm_entry != reg_section.end()) { // replace old comment comm_entry->second.comment = x_comment; m_Modified = true; } else { // new comment x_SetValue(m_Registry[x_section][kEmptyStr], kEmptyStr, ePersistent, x_comment); } return true; } // This is an entry comment _ASSERT( !reg_section.empty() ); TRegSection::iterator find_entry = reg_section.find(x_name); if (find_entry == reg_section.end()) return false; // (Re)set entry comment find_entry->second.comment = x_comment; m_Modified = true; return true;}const string& CNcbiRegistry::GetComment(const string& section, const string& name) const{ CFastMutexGuard LOCK(s_RegMutex); // If "section" is empty string, then get the registry's comment. string x_section = NStr::TruncateSpaces(section); if (x_section == kEmptyStr) { return m_Comment; } // Find section TRegistry::const_iterator find_section = m_Registry.find(x_section); if (find_section == m_Registry.end()) { return kEmptyStr; } const TRegSection& reg_section = find_section->second; // If "name" is empty string, then get "section"'s comment. string x_name = NStr::TruncateSpaces(name); if (x_name == kEmptyStr) { TRegSection::const_iterator comm_entry = reg_section.find(kEmptyStr); if (comm_entry == reg_section.end()) { return kEmptyStr; } return comm_entry->second.comment; } // Get "section:entry"'s comment _ASSERT( !reg_section.empty() ); TRegSection::const_iterator find_entry = reg_section.find(x_name); if (find_entry == reg_section.end()) { return kEmptyStr; } return find_entry->second.comment;}void CNcbiRegistry::EnumerateSections(list<string>* sections) const{ CFastMutexGuard LOCK(s_RegMutex); sections->clear(); ITERATE (TRegistry, section, m_Registry) { sections->push_back(section->first); }}void CNcbiRegistry::EnumerateEntries(const string& section, list<string>* entries) const{ CFastMutexGuard LOCK(s_RegMutex); entries->clear(); // find section TRegistry::const_iterator find_section = m_Registry.find(section); if (find_section == m_Registry.end()) return; const TRegSection& reg_section = find_section->second; _ASSERT( !reg_section.empty() ); // enumerate through the entries in the found section ITERATE (TRegSection, entry, reg_section) { if ( entry->first.empty() ) continue; // skip section comment entries->push_back(entry->first); }}bool CNcbiRegistry::x_IsAllTransient(void) const{ if ( !m_Comment.empty() ) { return false; } ITERATE (TRegistry, section, m_Registry) { ITERATE (TRegSection, entry, section->second) { if (section->first.empty() || !entry->second.persistent.empty()) { return false; // section comment or non-transient entry } } } return true;}void CNcbiRegistry::x_SetValue(TRegEntry& entry, const string& value, TFlags flags, const string& comment){ bool persistent = (flags & ePersistent) != 0; if ( persistent ) { m_Modified = true; } if ( !comment.empty() ) { entry.comment = s_ConvertComment(comment); } string* to = persistent ? &entry.persistent : &entry.transient; if ((flags & eTruncate) == 0 || value.empty()) { *to = value; return; } SIZE_TYPE beg; for (beg = 0; beg < value.length() && isspace(value[beg]) && value[beg] != '\n'; beg++) continue; if (beg == value.length()) { to->erase(); return; } SIZE_TYPE end; for (end = value.length() - 1; isspace(value[end]) && value[end] != '\n'; end--) continue; _ASSERT(beg <= end); *to = value.substr(beg, end - beg + 1);}END_NCBI_SCOPE/* * =========================================================================== * $Log: ncbireg.cpp,v $ * Revision 1000.1 2004/06/01 19:09:18 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.39 * * Revision 1.39 2004/05/14 13:59:27 gorelenk * Added include of ncbi_pch.hpp * * Revision 1.38 2003/10/20 21:55:12 vakatov * CNcbiRegistry::GetComment() -- make it "const" * * Revision 1.37 2003/06/26 18:54:48 rsmith * fix bug where comments multiplied blank lines after them. * * Revision 1.36 2003/06/25 20:05:57 rsmith * Fix small bugs setting comments * * Revision 1.35 2003/05/08 13:47:16 ivanov * Fixed Read() for right handle entry names with leading spaces * * Revision 1.34 2003/04/07 19:40:36 ivanov * Rollback to R1.32 * * Revision 1.33 2003/04/07 16:08:29 ivanov * Added more thread-safety to CNcbiRegistry:: methods -- mutex protection. * Get() and GetComment() returns "string", not "string&". * * Revision 1.32 2003/03/10 18:57:08 kuznets * iterate->ITERATE * * Revision 1.31 2003/02/28 19:24:51 vakatov * Get rid of redundant "const" in the return type of GetInt/Bool/Double() * * Revision 1.30 2003/02/24 19:56:05 gouriano * use template-based exceptions instead of errno and parse exceptions * * Revision 1.29 2003/01/17 20:45:39 kuznets * Minor code cleanup~ * * Revision 1.28 2003/01/17 20:27:30 kuznets * CNcbiRegistry added ErrPost error action * * Revision 1.27 2003/01/17 17:31:07 vakatov * CNcbiRegistry::GetString() to return "string", not "string&" -- for safety * * Revision 1.26 2002/12/30 23:23:07 vakatov * + GetString(), GetInt(), GetBool(), GetDouble() -- with defaults, * conversions and error handling control (to extend Get()'s functionality). * * Revision 1.25 2002/09/19 20:05:43 vasilche * Safe initialization of static mutexes * * Revision 1.24 2002/08/01 18:43:57 ivanov * Using NcbiGetlineEOL() instead s_NcbiGetline() * * Revision 1.23 2002/07/11 14:18:27 gouriano * exceptions replaced by CNcbiException-type ones * * Revision 1.22 2002/04/11 21:08:03 ivanov * CVS log moved to end of the file * * Revision 1.21 2001/10/29 18:55:08 ivanov * Fixed adding blank lines at write registry file * * Revision 1.20 2001/09/11 00:52:51 vakatov * Fixes to R1.17 - R1.19: * Renamed HasChanged() to Modified(), get rid of bugs, refined and * extended its functionality. * Made Write() be "const" again. * Prevent section comment from inadvertently concatenating with the * section name (without a new-line between them). * Other fixes and refinements. * * Revision 1.19 2001/09/10 16:34:35 ivanov * Added method HasChanged() * * Revision 1.18 2001/06/26 15:20:22 ivanov * Fixed small bug in s_ConvertComment(). * Changed method of check and create new section in Read(). * * Revision 1.17 2001/06/22 21:49:47 ivanov * Added (with Denis Vakatov) ability for read/write the registry file * with comments. Also added functions GetComment() and SetComment(). * * Revision 1.16 2001/05/17 15:04:59 lavr * Typos corrected * * Revision 1.15 2001/04/09 17:39:45 grichenk * CNcbiRegistry::Get() return type reverted to "const string&" * * Revision 1.14 2001/04/06 15:46:30 grichenk * Added thread-safety to CNcbiRegistry:: methods * * Revision 1.13 2001/01/30 22:13:28 vakatov * Write() -- use "s_Endl" instead of "NcbiEndl" * * Revision 1.12 2001/01/30 00:41:58 vakatov * Read/Write -- serialize '\r' as "\\r" * * Revision 1.11 2000/03/30 21:06:35 kans * bad end of line detected on Mac * * Revision 1.10 1999/11/18 20:14:15 vakatov * Get rid of some CodeWarrior(MAC) C++ compilation warnings * * Revision 1.9 1999/09/02 21:52:14 vakatov * CNcbiRegistry::Read() -- fixed to accept if underscore is the 1st * symbol in the section/entry name; * Allow '-' and '.' in the section/entry name * * Revision 1.8 1999/08/30 16:00:41 vakatov * CNcbiRegistry:: Get()/Set() -- force the "name" and "section" to * consist of alphanumeric and '_' only; ignore leading and trailing * spaces * * Revision 1.7 1999/07/06 15:26:35 vakatov * CNcbiRegistry:: * - allow multi-line values * - allow values starting and ending with space symbols * - introduced EFlags/TFlags for optional parameters in the class * member functions -- rather than former numerous boolean parameters * * Revision 1.6 1998/12/28 17:56:37 vakatov * New CVS and development tree structure for the NCBI C++ projects * * Revision 1.5 1998/12/10 22:59:47 vakatov * CNcbiRegistry:: API is ready(and by-and-large tested) * =========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -