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

Go to the SVN repository for this file.

1 /* $Id: blob_id.cpp 100589 2023-08-14 14:23:37Z grichenk $
2  * ===========================================================================
3  * PUBLIC DOMAIN NOTICE
4  * National Center for Biotechnology Information
5  *
6  * This software/database is a "United States Government Work" under the
7  * terms of the United States Copyright Act. It was written as part of
8  * the author's official duties as a United States Government employee and
9  * thus cannot be copyrighted. This software/database is freely available
10  * to the public for use. The National Library of Medicine and the U.S.
11  * Government have not placed any restriction on its use or reproduction.
12  *
13  * Although all reasonable efforts have been taken to ensure the accuracy
14  * and reliability of the software and data, the NLM and the U.S.
15  * Government do not and cannot warrant the performance or results that
16  * may be obtained by using this software or data. The NLM and the U.S.
17  * Government disclaim all warranties, express or implied, including
18  * warranties of performance, merchantability or fitness for any particular
19  * purpose.
20  *
21  * Please cite the author in any work or product based on this material.
22  * ===========================================================================
23  *
24  * Author: Eugene Vasilchenko
25  *
26  * File Description: Base data reader interface
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
33 
34 #include <stdio.h> // for sscanf
35 
38 
39 
41  : m_Sat(-1), m_SubSat(0), m_SatKey(0)
42 {
43  string ssat, ssatkey;
44  if (!NStr::SplitInTwo(str_id, ".", ssat, ssatkey)) return;
45  try {
46  TSat sat = NStr::StringToNumeric<TSat>(ssat);
47  TSatKey satkey = NStr::StringToNumeric<TSatKey>(ssatkey);
48  SetSat(sat);
49  SetSatKey(satkey);
50  }
51  catch (CStringException&) {
52  }
53 }
54 
55 
57 {
58  ostr << "Blob(";
59  ostr << GetSat()<<','<<GetSatKey();
60  if ( !IsMainBlob() )
61  ostr << ",sub="<<GetSubSat();
62  ostr << ')';
63  return ostr;
64 }
65 
66 
67 string CBlob_id::ToString(void) const
68 {
69  CNcbiOstrstream ostr;
70  Dump(ostr);
71  return CNcbiOstrstreamToString(ostr);
72 }
73 
74 
75 string CBlob_id::ToPsgId(void) const
76 {
77  CNcbiOstrstream ostr;
78  ostr << GetSat() << '.' << GetSatKey();
79  return CNcbiOstrstreamToString(ostr);
80 }
81 
82 
83 /* static */
85 {
86  TSat sat = -1;
87  TSatKey sat_key = 0;
88  TSubSat sub_sat = 0;
89  if (str.find(",sub=") != string::npos) {
90  if( sscanf(str.c_str(), "Blob(%d,%d,sub=%d)", &sat, &sat_key, &sub_sat) != 3)
91  NCBI_THROW(CLoaderException, eOtherError,
92  "\"" + str + "\" is not a valid Genbank BlobId");
93 
94  } else {
95  if( sscanf(str.c_str(), "Blob(%d,%d)", &sat, &sat_key) != 2)
96  NCBI_THROW(CLoaderException, eOtherError,
97  "\"" + str + "\" is not a valid Genbank BlobId");
98  }
99 
100  CRef<CBlob_id> blobid(new CBlob_id);
101  blobid->SetSat(sat);
102  blobid->SetSubSat(sub_sat);
103  blobid->SetSatKey(sat_key);
104  return blobid;
105 }
106 
107 
108 bool CBlob_id::operator==(const CBlobId& id_ref) const
109 {
110  const CBlob_id* id = dynamic_cast<const CBlob_id*>(&id_ref);
111  return id && *this == *id;
112 }
113 
114 
115 bool CBlob_id::operator<(const CBlobId& id_ref) const
116 {
117  const CBlob_id* id = dynamic_cast<const CBlob_id*>(&id_ref);
118  if ( !id ) {
119  return LessByTypeId(id_ref);
120  }
121  return *this < *id;
122 }
123 
124 
TSat GetSat() const
Definition: blob_id.hpp:56
Int4 TSatKey
Definition: blob_id.hpp:54
bool IsMainBlob(void) const
Definition: blob_id.hpp:100
TSatKey GetSatKey() const
Definition: blob_id.hpp:64
Int4 TSat
Definition: blob_id.hpp:52
Int4 TSubSat
Definition: blob_id.hpp:53
static CBlob_id * CreateFromString(const string &str)
Definition: blob_id.cpp:84
CNcbiOstream & Dump(CNcbiOstream &ostr) const
Definition: blob_id.cpp:56
bool operator<(const CBlobId &blob_id) const
Definition: blob_id.cpp:115
TSubSat GetSubSat() const
Definition: blob_id.hpp:60
string ToPsgId(void) const
Definition: blob_id.cpp:75
string ToString(void) const
Get string representation of blob id.
Definition: blob_id.cpp:67
bool operator==(const CBlobId &blob_id) const
Definition: blob_id.cpp:108
Data loader exceptions, used by GenBank loader.
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:802
CRef –.
Definition: ncbiobj.hpp:618
CStringException –.
Definition: ncbistr.hpp:4506
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
static const char * str(char *buf, int n)
Definition: stats.c:84
#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
bool LessByTypeId(const CBlobId &id2) const
#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::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
static bool SplitInTwo(const CTempString str, const CTempString delim, string &str1, string &str2, TSplitFlags flags=0)
Split a string into two pieces using the specified delimiters.
Definition: ncbistr.cpp:3554
Modified on Wed Apr 24 14:10:05 2024 by modify_doxy.py rev. 669887