NCBI C++ ToolKit
cuCdReadWriteASN.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 
2 #ifndef CU_CDREADWRITEASN_HPP
3 #define CU_CDREADWRITEASN_HPP
4 
5 #include <corelib/ncbiargs.hpp>
6 #include <corelib/ncbistre.hpp>
7 
8 #include <serial/serial.hpp>
9 #include <serial/objistrasn.hpp>
10 #include <serial/objistrasnb.hpp>
11 #include <serial/objostrasn.hpp>
12 #include <serial/objostrasnb.hpp>
13 #include <serial/objistr.hpp>
14 #include <serial/objostr.hpp>
15 #include <serial/objectio.hpp>
16 
17 
18 //BEGIN_NCBI_SCOPE
19 
20 //BEGIN_objects_SCOPE // namespace ncbi::objects::
22 //USING_SCOPE(cd_utils);
23 
24 //------------------------------------------------------------------------------------
25 // create a new copy of a C++ ASN data object (copied from Paul's asn_converter.hpp)
26 //------------------------------------------------------------------------------------
27 template < class ASNClassOld, class ASNClassNew >
28 //NCBI_CDUTILS_EXPORT
29 static void TransformASNObject(ASNClassNew& newObject,const ASNClassOld& originalObject, std::string *err)
30 {
31  err->erase();
32  try {
33  // create output stream and load object into it
34  ncbi::CNcbiStrstream asnIOstream;
35  ncbi::CObjectOStreamAsnBinary outObject(asnIOstream);
36  outObject << originalObject;
37 
38  // create input stream and load into new object
39  ncbi::CObjectIStreamAsnBinary inObject(asnIOstream);
40  //newObject.reset(new ASNClassNew());
41  inObject >> newObject;
42 
43  } catch (exception& e) {
44  *err = e.what();
45  }
46 }
47 
48 //------------------------------------------------------------------------------------
49 // create a new copy of a C++ ASN data object (copied from Paul's asn_converter.hpp)
50 //------------------------------------------------------------------------------------
51 template < class ASNClass >
52 //NCBI_CDUTILS_EXPORT
53 static ASNClass * CopyASNObject(const ASNClass& originalObject, std::string *err)
54 {
55  err->erase();
56  unique_ptr<ASNClass> newObject;
57  try {
58  // create output stream and load object into it
59  ncbi::CNcbiStrstream asnIOstream;
60  ncbi::CObjectOStreamAsnBinary outObject(asnIOstream);
61  outObject << originalObject;
62 
63  // create input stream and load into new object
64  ncbi::CObjectIStreamAsnBinary inObject(asnIOstream);
65  newObject.reset(new ASNClass());
66  inObject >> *newObject;
67 
68  } catch (exception& e) {
69  *err = e.what();
70  return NULL;
71  }
72  return newObject.release();
73 }
74 
75 
76 //------------------------------------------------------------------------------------
77 // a utility function for reading different types of ASN data from a file
78 // (copied from Paul's asn_reader.hpp)
79 //------------------------------------------------------------------------------------
80 template < class ASNClass >
81 //NCBI_CDUTILS_EXPORT
82 static bool ReadASNFromFile(const char *filename, ASNClass *ASNobject, bool isBinary, std::string *err)
83 {
84  err->erase();
85  // initialize the binary input stream
86  ncbi::CNcbiIfstream inStream(filename, IOS_BASE::in | IOS_BASE::binary);
87 // unique_ptr<ncbi::CNcbiIstream> inStream;
88 // inStream.reset(new ncbi::CNcbiIfstream(filename, IOS_BASE::in | IOS_BASE::binary));
89  if (!inStream) {
90  *err = "Cannot open file for reading";
91  return false;
92  }
93  unique_ptr<ncbi::CObjectIStream> inObject;
94  if (isBinary) {
95  // Associate ASN.1 binary serialization methods with the input
96  inObject.reset(new ncbi::CObjectIStreamAsnBinary(inStream));
97  } else {
98  // Associate ASN.1 text serialization methods with the input
99  inObject.reset(new ncbi::CObjectIStreamAsn(inStream));
100  }
101  // Read the asn data
102  try {
103  *inObject >> *ASNobject;
104  } catch (exception& e) {
105  *err = e.what();
106  return false;
107  }
108  inObject->Close();
109  inStream.close();
110  return true;
111 }
112 
113 template < class ASNClass >
114 //NCBI_CDUTILS_EXPORT
115 static bool ReadASNFromStream(ncbi::CNcbiIstream& is, ASNClass *ASNobject, bool isBinary, std::string *err)
116 {
117  err->erase();
118  if (!is.good()) {
119  *err = "Input stream is bad.";
120  return false;
121  }
122  unique_ptr<ncbi::CObjectIStream> inObject;
123  if (isBinary) {
124  // Associate ASN.1 binary serialization methods with the input
125  inObject.reset(new ncbi::CObjectIStreamAsnBinary(is));
126  } else {
127  // Associate ASN.1 text serialization methods with the input
128  inObject.reset(new ncbi::CObjectIStreamAsn(is));
129  }
130  // Read the asn data
131  try {
132  *inObject >> *ASNobject;
133  } catch (exception& e) {
134  *err = e.what();
135  return false;
136  }
137  return true;
138 }
139 
140 //------------------------------------------------------------------------------------
141 // for writing ASN data (copied from Paul's asn_reader.hpp)
142 //------------------------------------------------------------------------------------
143 template < class ASNClass >
144 //NCBI_CDUTILS_EXPORT
145 static bool WriteASNToFile(const char *filename, const ASNClass& ASNobject, bool isBinary,
147 {
148  err->erase();
149  // initialize a binary output stream
150  ncbi::CNcbiOfstream outStream(filename, IOS_BASE::out | IOS_BASE::binary);
151 // unique_ptr<ncbi::CNcbiOstream> outStream;
152 // outStream.reset(new ncbi::CNcbiOfstream(filename, IOS_BASE::out | IOS_BASE::binary));
153  if (!outStream) {
154  *err = "Cannot open file for writing";
155  return false;
156  }
157  unique_ptr<ncbi::CObjectOStream> outObject;
158  if (isBinary) {
159  // Associate ASN.1 binary serialization methods with the input
160  outObject.reset(new ncbi::CObjectOStreamAsnBinary(outStream, fixNonPrint));
161  } else {
162  // Associate ASN.1 text serialization methods with the input
163  outObject.reset(new ncbi::CObjectOStreamAsn(outStream, fixNonPrint));
164  }
165  // write the asn data
166  try {
167  *outObject << ASNobject;
168  } catch (exception& e) {
169  *err = e.what();
170  return false;
171  }
172  outObject->Close();
173  outStream.close();
174  return true;
175 }
176 
177 template < class ASNClass >
178 //NCBI_CDUTILS_EXPORT
179 static bool WriteASNToStream(ncbi::CNcbiOstream& os, const ASNClass& ASNobject, bool isBinary,
181 {
182  err->erase();
183  if (!(os.good())) {
184  *err = "Stream for writing is bad";
185  return false;
186  }
187  unique_ptr<ncbi::CObjectOStream> outObject;
188  if (isBinary) {
189  // Associate ASN.1 binary serialization methods with the input
190  outObject.reset(new ncbi::CObjectOStreamAsnBinary(os, fixNonPrint));
191  } else {
192  // Associate ASN.1 text serialization methods with the input
193  outObject.reset(new ncbi::CObjectOStreamAsn(os, fixNonPrint));
194  }
195  // write the asn data
196  try {
197  *outObject << ASNobject;
198  } catch (exception& e) {
199  *err = e.what();
200  return false;
201  }
202  return true;
203 }
204 
205 template < class ASNContainerClass >
207 {
208 private:
209  unique_ptr<CObjectIStream> inStream;
210 
211 public:
212  CObjectIStreamHelper(const string& filename, bool isBinary)
213  {
214  inStream.reset(CObjectIStream::Open((isBinary ? eSerial_AsnBinary : eSerial_AsnText), filename));
215  if (!inStream->InGoodState())
216  NCBI_USER_THROW("input stream not InGoodState()");
217  inStream->SkipFileHeader(ASNContainerClass::GetTypeInfo());
218  }
219 
220  CObjectIStream& GetInStream(void) { return *inStream; }
221 };
222 
223 
224 template < class ASNContainerClass , class ASNElementClass >
226 {
227 private:
230 
231 public:
232  // ctor will throw an exception if stream opening fails for any reason
233  ASNInputContainerStream(const string& filename, bool isBinary) :
234  inStream(filename, isBinary),
235  containerIterator(inStream.GetInStream(), CObjectTypeInfo(ASNContainerClass::GetTypeInfo()))
236  {
237  }
238 
239  bool ReadElement(ASNElementClass *element)
240  {
241  if (containerIterator.HaveMore()) {
242  element->Reset();
243  containerIterator >> *element;
244  ++containerIterator;
245  return true;
246  } else
247  return false;
248  }
249 };
250 
251 
252 //END_objects_SCOPE // namespace ncbi::objects::
253 
254 //END_NCBI_SCOPE
255 
256 #endif
ASNInputContainerStream(const string &filename, bool isBinary)
CIStreamContainerIterator containerIterator
CObjectIStreamHelper< ASNContainerClass > inStream
bool ReadElement(ASNElementClass *element)
Reading (iterating through) elements of containers (SET OF, SEQUENCE OF).
Definition: objectio.hpp:164
unique_ptr< CObjectIStream > inStream
CObjectIStream & GetInStream(void)
CObjectIStreamHelper(const string &filename, bool isBinary)
CObjectIStream –.
Definition: objistr.hpp:93
CObjectTypeInfo –.
Definition: objectinfo.hpp:94
static void TransformASNObject(ASNClassNew &newObject, const ASNClassOld &originalObject, std::string *err)
static bool ReadASNFromStream(ncbi::CNcbiIstream &is, ASNClass *ASNobject, bool isBinary, std::string *err)
static bool ReadASNFromFile(const char *filename, ASNClass *ASNobject, bool isBinary, std::string *err)
static ASNClass * CopyASNObject(const ASNClass &originalObject, std::string *err)
USING_NCBI_SCOPE
static bool WriteASNToFile(const char *filename, const ASNClass &ASNobject, bool isBinary, std::string *err, ncbi::EFixNonPrint fixNonPrint=ncbi::eFNP_Default)
static bool WriteASNToStream(ncbi::CNcbiOstream &os, const ASNClass &ASNobject, bool isBinary, std::string *err, ncbi::EFixNonPrint fixNonPrint=ncbi::eFNP_Default)
std::ofstream out("events_result.xml")
main entry point for tests
string
Definition: cgiapp.hpp:687
#define NULL
Definition: ncbistd.hpp:225
#define NCBI_USER_THROW(message)
Throw a quick-and-dirty runtime exception of type 'CException' with the given error message and error...
Definition: ncbiexpt.hpp:715
EFixNonPrint
How to process non-printing character in the ASN VisibleString.
Definition: serialdef.hpp:173
@ eFNP_Default
Definition: serialdef.hpp:181
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
@ eSerial_AsnBinary
ASN.1 binary.
Definition: serialdef.hpp:74
bool HaveMore(void) const
static CObjectIStream * Open(ESerialDataFormat format, CNcbiIstream &inStream, bool deleteInStream)
Create serial object reader and attach it to an input stream.
Definition: objistr.cpp:195
IO_PREFIX::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
CNcbistrstream_Base< IO_PREFIX::strstream, IOS_BASE::in|IOS_BASE::out > CNcbiStrstream
Definition: ncbistre.hpp:287
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
#define NCBI_CDUTILS_EXPORT
Definition: ncbi_export.h:376
Defines command line argument related classes.
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
std::istream & in(std::istream &in_, double &x_)
Modified on Thu Apr 11 15:08:27 2024 by modify_doxy.py rev. 669887