NCBI C++ ToolKit
cuStdAnnotTypes.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: cuStdAnnotTypes.cpp 100549 2023-08-10 17:35:58Z lanczyck $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Chris Lanczycki
27 *
28 * File Description:
29 * API for defining and/or accessing pre-defined annotation types
30 * and descriptions.
31 *
32 */
33 
34 #include <ncbi_pch.hpp>
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbireg.hpp>
38 
40 BEGIN_SCOPE(cd_utils)
41 
42 const int CStdAnnotTypes::m_invalidType = -1;
44 
45 
46 bool CStdAnnotTypes::LoadTypes(const string& iniFile)
47 {
48  static const string ID("id");
49 
50  bool result = false;
51  int id;
54  string value;
55  list<string> categories, names;
56 
57  // Use CMemoryRegistry simply to leverage registry file format parser.
58  CMemoryRegistry dummyRegistry;
59  unique_ptr<CNcbiIfstream> iniIn(new CNcbiIfstream(iniFile.c_str(), IOS_BASE::in | IOS_BASE::binary));
60  if (*iniIn) {
61 
62  result = true;
63  ERR_POST(ncbi::Trace << "loading predefined annotation types " << iniFile);
64 
65  dummyRegistry.Read(*iniIn, readFlags);
66  dummyRegistry.EnumerateSections(&categories, accessFlags);
67 
68  // Loop over all type categories
69  ITERATE (list<string>, cit, categories) {
70  if (*cit == kEmptyStr) continue;
71 
72  // reject 'id' if it equals m_invalidType; otherwise allow negative values
73  id = dummyRegistry.GetInt(*cit, ID, m_invalidType, accessFlags, CMemoryRegistry::eReturn);
74  if (id != m_invalidType) {
75  TTypeNamesPair& typeNamesPair = m_stdAnnotTypeData[id];
76  TTypeNames& values = typeNamesPair.second;
77  typeNamesPair.first = *cit;
78 
79  // Get all named fields for this section.
80  dummyRegistry.EnumerateEntries(*cit, &names, accessFlags);
81 
82  // Add value for each non-id named field; sort alphabetically on completion.
83  ITERATE (list<string>, eit, names) {
84  if (*eit == ID || *eit == kEmptyStr) continue;
85  value = dummyRegistry.GetString(*cit, *eit, kEmptyStr, accessFlags);
87  if (value != kEmptyStr) {
88  values.push_back(value);
89  }
90  }
91 
92  sort(values.begin(), values.end());
93 
94  /*cout << "\nTesting sort for " << *cit << endl;
95  ITERATE (vector<string>, vit, values) {
96  cout << *vit << endl;
97  }*/
98 
99  }
100  }
101  }
102 
103  return result;
104 }
105 
106 
108 {
110  return ((type != m_invalidType) && (cit != m_stdAnnotTypeData.end()));
111 }
112 
113 bool CStdAnnotTypes::IsValidTypeStr(const string& typeStr, bool useCase)
114 {
115  bool result = false;
116  NStr::ECase caseSensitiveEqual = (useCase) ? NStr::eCase : NStr::eNocase;
119 
120  for (; (satdCit != satdEnd) && !result; ++satdCit) {
121  if (NStr::Equal(satdCit->second.first, typeStr.c_str(), caseSensitiveEqual)) {
122  result = true;
123  }
124  }
125  return result;
126 }
127 
128 bool CStdAnnotTypes::GetTypeAsString(int type, string& typeStr)
129 {
130  bool result = IsValidType(type);
131  if (result) {
133  typeStr = cit->second.first;
134  if (typeStr.length() == 0) result = false;
135  }
136  return result;
137 }
138 
139 int CStdAnnotTypes::GetTypeAsInt(const string& typeStr, bool useCase)
140 {
141  int result = m_invalidType;
142  NStr::ECase caseSensitiveEqual = (useCase) ? NStr::eCase : NStr::eNocase;
145 
146  for (; (satdCit != satdEnd); ++satdCit) {
147  if (NStr::Equal(satdCit->second.first, typeStr.c_str(), caseSensitiveEqual)) {
148  result = satdCit->first;
149  break;
150  }
151  }
152  return result;
153 }
154 
156 {
157  bool result = IsValidType(type);
158  if (result) {
160  typeNames = cit->second.second;
161  if (typeNames.size() == 0) result = false;
162  }
163  return result;
164 }
165 
166 bool CStdAnnotTypes::GetTypeNames(const string& typeStr, TTypeNames& typeNames, bool useCase)
167 {
168  int type = GetTypeAsInt(typeStr, useCase);
169  bool result = (type != m_invalidType);
170  if (result) {
172  typeNames = cit->second.second;
173  if (typeNames.size() == 0) result = false;
174  }
175  return result;
176 }
177 
179 {
180  bool result = IsValidType(type);
181  if (result) {
183  typeNamesPair.first = cit->second.first;
184  typeNamesPair.second = cit->second.second;
185  }
186  return result;
187 }
188 
189 bool CStdAnnotTypes::IsPredefinedDescrForType(int type, const string& descr, bool useCase)
190 {
191  bool result = false;
192  NStr::ECase caseSensitiveEqual = (useCase) ? NStr::eCase : NStr::eNocase;
194  if ((type != m_invalidType) && (cit != m_stdAnnotTypeData.end())) {
195  const TTypeNames& names = cit->second.second;
196  for (TTypeNames::const_iterator nCit = names.begin(); nCit != names.end(); ++nCit) {
197  if (NStr::Equal(nCit->c_str(), descr.c_str(), caseSensitiveEqual)) {
198  result = true;
199  break;
200  }
201  }
202  }
203  return result;
204 }
205 
206 bool CStdAnnotTypes::IsPredefinedDescr(const string& descr, int& type, int& typeIndex, bool useCase)
207 {
208  bool result = false;
209  int index;
210  NStr::ECase caseSensitiveEqual = (useCase) ? NStr::eCase : NStr::eNocase;
213 
215  typeIndex = m_invalidType;
216  for (; (satdCit != satdEnd) && !result; ++satdCit) {
217  index = 0;
218  const TTypeNames& names = satdCit->second.second;
219  for (TTypeNames::const_iterator nCit = names.begin(); nCit != names.end(); ++nCit, ++index) {
220  if (NStr::Equal(nCit->c_str(), descr.c_str(), caseSensitiveEqual)) {
221  result = true;
222  type = satdCit->first;
223  typeIndex = index;
224  break;
225  }
226  }
227  }
228  return result;
229 }
230 
231 
232 END_SCOPE(cd_utils)
CMemoryRegistry –.
Definition: ncbireg.hpp:584
static bool IsValidTypeStr(const string &typeStr, bool useCase=true)
static int GetTypeAsInt(const string &typeStr, bool useCase=true)
static bool GetTypeAsString(int type, string &typeStr)
static bool GetTypeNamesPair(int type, TTypeNamesPair &typeNamesPair)
static const int m_invalidType
static bool IsPredefinedDescr(const string &descr, int &type, int &typeIndex, bool useCase=true)
static bool LoadTypes(const string &iniFile)
static bool IsValidType(int type)
static bool IsPredefinedDescrForType(int type, const string &descr, bool useCase=true)
static TStandardTypesData m_stdAnnotTypeData
static bool GetTypeNames(int type, TTypeNames &typeNames)
container_type::const_iterator const_iterator
Definition: map.hpp:53
const_iterator begin() const
Definition: map.hpp:151
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
Include a standard set of the NCBI C++ Toolkit most basic headers.
vector< string > TTypeNames
pair< string, TTypeNames > TTypeNamesPair
static const struct name_t names[]
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Trace(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1179
virtual void EnumerateSections(list< string > *sections, TFlags flags=fAllLayers) const
Enumerate section names.
Definition: ncbireg.cpp:497
virtual int GetInt(const string &section, const string &name, int default_value, TFlags flags=0, EErrAction err_action=eThrow) const
Get integer value of specified parameter name.
Definition: ncbireg.cpp:362
virtual void EnumerateEntries(const string &section, list< string > *entries, TFlags flags=fAllLayers) const
Enumerate parameter names for a specified section.
Definition: ncbireg.cpp:514
IRWRegistry * Read(CNcbiIstream &is, TFlags flags=0, const string &path=kEmptyStr)
Read and parse the stream "is", and merge its content with current Registry entries.
Definition: ncbireg.cpp:605
virtual string GetString(const string &section, const string &name, const string &default_value, TFlags flags=0) const
Get the parameter string value.
Definition: ncbireg.cpp:321
@ fCaseFlags
Definition: ncbireg.hpp:105
@ fInternalSpaces
Allow internal whitespace in names.
Definition: ncbireg.hpp:92
@ eReturn
Return default value.
Definition: ncbireg.hpp:203
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
#define kEmptyStr
Definition: ncbistr.hpp:123
static void TruncateSpacesInPlace(string &str, ETrunc where=eTrunc_Both)
Truncate spaces in a string (in-place)
Definition: ncbistr.cpp:3201
ECase
Which type of string comparison.
Definition: ncbistr.hpp:1204
static bool Equal(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2, ECase use_case=eCase)
Test for equality of a substring with another string.
Definition: ncbistr.hpp:5384
@ eNocase
Case insensitive compare.
Definition: ncbistr.hpp:1206
@ eCase
Case sensitive compare.
Definition: ncbistr.hpp:1205
constexpr auto sort(_Init &&init)
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Process information in the NCBI Registry, including working with configuration files.
std::istream & in(std::istream &in_, double &x_)
Definition: type.c:6
else result
Definition: token2.c:20
Modified on Tue Apr 23 07:40:28 2024 by modify_doxy.py rev. 669887