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

Go to the SVN repository for this file.

1 /* $Id: Comment_set.cpp 97219 2022-06-27 18:38:50Z gotvyans $
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: .......
27  *
28  * File Description:
29  * .......
30  *
31  * Remark:
32  * This code was originally generated by application DATATOOL
33  * using the following specifications:
34  * 'valid.asn'.
35  */
36 
37 // standard includes
38 #include <ncbi_pch.hpp>
39 
40 // generated includes
43 
44 // for default structured comment rules file
45 #include "validrules.inc"
46 #include <util/util_misc.hpp>
47 #include <serial/serialimpl.hpp>
48 
49 
50 // generated classes
51 
53 
54 BEGIN_objects_SCOPE // namespace ncbi::objects::
55 
56 
57 // destructor
59 {
60 }
61 
63 {
64  string search = prefix;
67  const CComment_rule& rule = **it;
68  string this_prefix = rule.GetPrefix();
69  CComment_rule::NormalizePrefix(this_prefix);
70  if (NStr::EqualNocase(this_prefix, search)) {
71  return *it;
72  }
73  }
74 
75  // NCBI_THROW (CCoreException, eNullPtr, "FindCommentRuleEx failed");
76 
77  return CConstRef<CComment_rule>();
78 }
79 
80 
82 {
83  auto rule = FindCommentRuleEx(prefix);
84  if ( rule.Empty() ) {
85  NCBI_THROW (CCoreException, eNullPtr, "FindCommentRule failed");
86  } else {
87  return *rule;
88  }
89 }
90 
91 
92 static bool s_FieldRuleCompare (
93  const CRef<CField_rule>& p1,
94  const CRef<CField_rule>& p2
95 )
96 
97 {
98  return NStr::Compare(p1->GetField_name(), p2->GetField_name()) < 0;
99 }
100 
101 
103 {
104  auto s_CommentRules = Ref(new CComment_set());
105 
106  string file = g_FindDataFile("validrules.prt");
107 
108  if ( !file.empty() ) {
109  unique_ptr<CObjectIStream> in;
111  string header = in->ReadFileHeader();
112  in->Read(ObjectInfo(*s_CommentRules), CObjectIStream::eNoFileHeader);
113  if (getenv("NCBI_DEBUG")) {
114  LOG_POST("Reading from " + file + " for structured comment rules.");
115  }
116  }
117  if (!s_CommentRules->IsSet()) {
118  if (getenv("NCBI_DEBUG")) {
119  LOG_POST("Falling back on built-in data for structured comment rules");
120  }
121  size_t num_lines = sizeof (s_Defaultvalidrules) / sizeof (char *);
122  string all_rules = "";
123  for (size_t i = 0; i < num_lines; i++) {
124  all_rules += s_Defaultvalidrules[i];
125  }
126  CNcbiIstrstream istr(all_rules);
127  istr >> MSerial_AsnText >> *s_CommentRules;
128  }
129  if (s_CommentRules->IsSet()) {
130  for(auto& rule: s_CommentRules->Set()) {
131  if (!rule->GetRequire_order() && rule->IsSetFields()) {
132  CField_set& fields = rule->SetFields();
133  fields.Set().sort(s_FieldRuleCompare);
134  }
135  }
136  }
137 
138  return s_CommentRules;
139 }
140 
141 
143 {
145  return rules;
146 }
147 
148 
149 vector<string> CComment_set::GetFieldNames(const string& prefix)
150 {
151  vector<string> options;
152 
153  string prefix_to_use = CComment_rule::MakePrefixFromRoot(prefix);
154 
155  // look up mandatory and required field names from validator rules
157 
158  if (rules) {
159  CConstRef<CComment_rule> ruler = rules->FindCommentRuleEx(prefix_to_use);
160  if (ruler) {
161  const CComment_rule& rule = *ruler;
163  options.push_back((*it)->GetField_name());
164  }
165  }
166  }
167 
168  return options;
169 }
170 
171 
172 list<string> CComment_set::GetKeywords(const CUser_object& user)
173 {
174  list<string> keywords;
175 
177  string prefix_to_use = CComment_rule::MakePrefixFromRoot(prefix);
178 
179  // look up mandatory and required field names from validator rules
181 
182  if (rules) {
183  CConstRef<CComment_rule> ruler = rules->FindCommentRuleEx(prefix_to_use);
184  if (ruler) {
185  const CComment_rule& rule = *ruler;
186  CComment_rule::TErrorList errors = rule.IsValid(user);
187  if (errors.size() == 0) {
188  string kywd = CComment_rule::KeywordForPrefix( prefix );
189  NStr::Split(kywd, ";", keywords, NStr::fSplit_Tokenize);
190  }
191  }
192  }
193 
194  return keywords;
195 }
196 
197 
198 END_objects_SCOPE // namespace ncbi::objects::
199 
201 
202 /* Original file checksum: lines: 57, chars: 1726, CRC32: 31eca82f */
User-defined methods of the data storage class.
static CConstRef< CComment_set > s_InitializeStructuredCommentRules()
static bool s_FieldRuleCompare(const CRef< CField_rule > &p1, const CRef< CField_rule > &p2)
Definition: Comment_set.cpp:92
User-defined methods of the data storage class.
vector< TError > TErrorList
static string KeywordForPrefix(const string &prefix)
static string MakePrefixFromRoot(const string &root)
TErrorList IsValid(const CUser_object &user) const
static void NormalizePrefix(string &prefix)
static string GetStructuredCommentPrefix(const CUser_object &user, bool normalize=true)
static list< string > GetKeywords(const CUser_object &user)
CConstRef< CComment_rule > FindCommentRuleEx(const string &prefix) const
Definition: Comment_set.cpp:62
static vector< string > GetFieldNames(const string &prefix)
const CComment_rule & FindCommentRule(const string &prefix) const
Definition: Comment_set.cpp:81
static CConstRef< CComment_set > GetCommentRules()
~CComment_set(void)
Definition: Comment_set.cpp:58
CConstRef –.
Definition: ncbiobj.hpp:1266
CCoreException –.
Definition: ncbiexpt.hpp:1476
CField_set –.
Definition: Field_set.hpp:66
CRef –.
Definition: ncbiobj.hpp:618
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
pair< TObjectPtr, TTypeInfo > ObjectInfo(C &obj)
Definition: objectinfo.hpp:762
static CObjectIStream * Open(ESerialDataFormat format, CNcbiIstream &inStream, bool deleteInStream)
Create serial object reader and attach it to an input stream.
Definition: objistr.cpp:195
CRef< C > Ref(C *object)
Helper functions to get CRef<> and CConstRef<> objects.
Definition: ncbiobj.hpp:2015
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3461
static int Compare(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2, ECase use_case=eCase)
Compare of a substring with another string.
Definition: ncbistr.hpp:5297
static bool EqualNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive equality of a substring with another string.
Definition: ncbistr.hpp:5353
@ fSplit_Tokenize
All delimiters are merged and trimmed, to get non-empty tokens only.
Definition: ncbistr.hpp:2508
const TPrefix & GetPrefix(void) const
Get the Prefix member data.
const Tdata & Get(void) const
Get the member data.
list< CRef< CComment_rule > > Tdata
list< CRef< CField_rule > > Tdata
Definition: Field_set_.hpp:89
const TFields & GetFields(void) const
Get the Fields member data.
Tdata & Set(void)
Assign a value to data member.
Definition: Field_set_.hpp:170
const Tdata & Get(void) const
Get the member data.
Definition: Field_set_.hpp:164
FILE * file
int i
std::istream & in(std::istream &in_, double &x_)
static const char * prefix[]
Definition: pcregrep.c:405
string g_FindDataFile(const CTempString &name, CDirEntry::EType type=CDirEntry::eFile)
Look for an NCBI application data file or directory of the given name and type; in general,...
Definition: util_misc.cpp:139
Modified on Sat May 18 11:36:29 2024 by modify_doxy.py rev. 669887