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

Go to the SVN repository for this file.

1 #ifndef CORELIB___NCBISTRE__HPP
2 #define CORELIB___NCBISTRE__HPP
3 
4 /* $Id: ncbistre.hpp 77788 2017-05-08 13:25:16Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Denis Vakatov, Anton Lavrentiev
30  *
31  *
32  */
33 
34 /// @file ncbistre.hpp
35 /// NCBI C++ stream class wrappers for triggering between "new" and
36 /// "old" C++ stream libraries.
37 
38 
39 #include <corelib/ncbictype.hpp>
40 
41 /// Determine which iostream library to use, include appropriate
42 /// headers, and #define specific preprocessor variables.
43 /// The default is the new(template-based, std::) one.
44 
45 #if !defined(HAVE_IOSTREAM) && !defined(NCBI_USE_OLD_IOSTREAM)
46 # define NCBI_USE_OLD_IOSTREAM
47 #endif
48 
49 #if defined(HAVE_IOSTREAM_H) && defined(NCBI_USE_OLD_IOSTREAM)
50 # include <iostream.h>
51 # include <fstream.h>
52 # if defined(HAVE_STRSTREA_H)
53 # include <strstrea.h>
54 # else
55 # include <strstream.h>
56 # endif
57 # include <iomanip.h>
58 # define IO_PREFIX
59 # define IOS_BASE ::ios
60 # define IOS_PREFIX ::ios
61 # define PUBSYNC sync
62 # define PUBSEEKPOS seekpos
63 # define PUBSEEKOFF seekoff
64 
65 #elif defined(HAVE_IOSTREAM)
66 # if defined(NCBI_USE_OLD_IOSTREAM)
67 # undef NCBI_USE_OLD_IOSTREAM
68 # endif
69 # if defined(NCBI_COMPILER_GCC)
70 # if NCBI_COMPILER_VERSION < 300
71 # define NO_PUBSYNC
72 # elif NCBI_COMPILER_VERSION >= 310
73 // Don't bug us about including <strstream>.
74 # define _CPP_BACKWARD_BACKWARD_WARNING_H 1
75 # define _BACKWARD_BACKWARD_WARNING_H 1
76 # endif
77 # endif
78 # include <iostream>
79 # include <fstream>
80 # if defined(NCBI_COMPILER_ICC) && defined(__GNUC__) && !defined(__INTEL_CXXLIB_ICC)
81 # define _BACKWARD_BACKWARD_WARNING_H 1
82 # include <backward/strstream>
83 # else
84 # include <strstream>
85 # endif
86 # include <iomanip>
87 # define IO_PREFIX NCBI_NS_STD
88 # define IOS_BASE IO_PREFIX::ios_base
89 # define IOS_PREFIX IO_PREFIX::ios
90 
91 # ifdef NO_PUBSYNC
92 # define PUBSYNC sync
93 # define PUBSEEKOFF seekoff
94 # define PUBSEEKPOS seekpos
95 # else
96 # define PUBSYNC pubsync
97 # define PUBSEEKOFF pubseekoff
98 # define PUBSEEKPOS pubseekpos
99 # endif
100 
101 # ifdef _LIBCPP_VERSION
102 # define NCBI_SHUN_OSTRSTREAM 1
103 # include <sstream>
104 # endif
105 
106 #else
107 # error "Neither <iostream> nor <iostream.h> can be found!"
108 #endif
109 
110 // Obsolete
111 #define SEEKOFF PUBSEEKOFF
112 
113 #include <stddef.h>
114 
115 
116 // (BEGIN_NCBI_SCOPE must be followed by END_NCBI_SCOPE later in this file)
118 
119 /** @addtogroup Stream
120  *
121  * @{
122  */
123 
124 // I/O classes
125 
126 /// Portable alias for streampos.
127 typedef IO_PREFIX::streampos CNcbiStreampos;
128 
129 /// Portable alias for streamoff.
130 typedef IO_PREFIX::streamoff CNcbiStreamoff;
131 
132 /// Portable alias for ios.
133 typedef IO_PREFIX::ios CNcbiIos;
134 
135 /// Portable alias for streambuf.
136 typedef IO_PREFIX::streambuf CNcbiStreambuf;
137 
138 /// Portable alias for istream.
139 typedef IO_PREFIX::istream CNcbiIstream;
140 
141 /// Portable alias for ostream.
142 typedef IO_PREFIX::ostream CNcbiOstream;
143 
144 /// Portable alias for iostream.
145 typedef IO_PREFIX::iostream CNcbiIostream;
146 
147 /// Portable alias for strstreambuf.
148 typedef IO_PREFIX::strstreambuf CNcbiStrstreambuf;
149 
150 /// Portable alias for istrstream.
151 typedef IO_PREFIX::istrstream CNcbiIstrstream;
152 
153 #ifndef NCBI_SHUN_OSTRSTREAM
154 /// Portable alias for ostrstream.
155 typedef IO_PREFIX::ostrstream CNcbiOstrstream;
156 
157 /// Portable alias for strstream.
158 typedef IO_PREFIX::strstream CNcbiStrstream;
159 # define NCBI_STRSTREAM_INIT(p, l) (p), (l)
160 #else
161 typedef IO_PREFIX::ostringstream CNcbiOstrstream;
162 typedef IO_PREFIX::stringstream CNcbiStrstream;
163 # define NCBI_STRSTREAM_INIT(p, l) string(p, l)
164 #endif
165 
166 /// Portable alias for filebuf.
167 typedef IO_PREFIX::filebuf CNcbiFilebuf;
168 
169 
170 #if defined(NCBI_OS_MSWIN) && defined(_UNICODE)
171 // this is helper method for fstream classes only
172 // do not use it elsewhere
174 wstring ncbi_Utf8ToWstring(const char *utf8);
175 
176 class CNcbiIfstream : public IO_PREFIX::ifstream
177 {
178 public:
179  CNcbiIfstream( ) {
180  }
181  explicit CNcbiIfstream(
182  const char *_Filename,
183  IOS_BASE::openmode _Mode = IOS_BASE::in,
184  int _Prot = (int)IOS_BASE::_Openprot
185  ) : IO_PREFIX::ifstream(
186  ncbi_Utf8ToWstring(_Filename).c_str(), _Mode, _Prot) {
187  }
188  explicit CNcbiIfstream(
189  const wchar_t *_Filename,
190  IOS_BASE::openmode _Mode = IOS_BASE::in,
191  int _Prot = (int)IOS_BASE::_Openprot
192  ) : IO_PREFIX::ifstream(_Filename,_Mode,_Prot) {
193  }
194 
195  void open(
196  const char *_Filename,
197  IOS_BASE::openmode _Mode = IOS_BASE::in,
198  int _Prot = (int)IOS_BASE::_Openprot) {
199  IO_PREFIX::ifstream::open(
200  ncbi_Utf8ToWstring(_Filename).c_str(), _Mode, _Prot);
201  }
202  void open(const wchar_t *_Filename,
203  IOS_BASE::openmode _Mode = IOS_BASE::in,
204  int _Prot = (int)ios_base::_Openprot) {
205  IO_PREFIX::ifstream::open(_Filename,_Mode,_Prot);
206  }
207 };
208 #elif defined(NCBI_COMPILER_MSVC)
209 # if _MSC_VER >= 1200 && _MSC_VER < 1300
210 class CNcbiIfstream : public IO_PREFIX::ifstream
211 {
212 public:
213  CNcbiIfstream() : m_Fp(0)
214  {
215  }
216 
217  explicit CNcbiIfstream(const char* s,
218  IOS_BASE::openmode mode = IOS_BASE::in)
219  {
220  fastopen(s, mode);
221  }
222 
223  void fastopen(const char* s, IOS_BASE::openmode mode = IOS_BASE::in)
224  {
225  if (is_open() || !(m_Fp = __Fiopen(s, mode | in)))
226  setstate(failbit);
227  else
228  (void) new (rdbuf()) basic_filebuf<char, char_traits<char> >(m_Fp);
229  }
230 
231  virtual ~CNcbiIfstream(void)
232  {
233  if (m_Fp)
234  fclose(m_Fp);
235  }
236 private:
237  FILE* m_Fp;
238 };
239 # else
240 /// Portable alias for ifstream.
241 typedef IO_PREFIX::ifstream CNcbiIfstream;
242 # endif
243 #else
244 /// Portable alias for ifstream.
245 typedef IO_PREFIX::ifstream CNcbiIfstream;
246 #endif
247 
248 #if defined(NCBI_OS_MSWIN) && defined(_UNICODE)
249 class CNcbiOfstream : public IO_PREFIX::ofstream
250 {
251 public:
252  CNcbiOfstream( ) {
253  }
254  explicit CNcbiOfstream(
255  const char *_Filename,
256  IOS_BASE::openmode _Mode = IOS_BASE::out,
257  int _Prot = (int)IOS_BASE::_Openprot
258  ) : IO_PREFIX::ofstream(
259  ncbi_Utf8ToWstring(_Filename).c_str(), _Mode, _Prot) {
260  }
261  explicit CNcbiOfstream(
262  const wchar_t *_Filename,
263  IOS_BASE::openmode _Mode = IOS_BASE::out,
264  int _Prot = (int)IOS_BASE::_Openprot
265  ) : IO_PREFIX::ofstream(_Filename,_Mode,_Prot) {
266  }
267 
268  void open(
269  const char *_Filename,
270  IOS_BASE::openmode _Mode = IOS_BASE::out,
271  int _Prot = (int)IOS_BASE::_Openprot) {
272  IO_PREFIX::ofstream::open(
273  ncbi_Utf8ToWstring(_Filename).c_str(), _Mode, _Prot);
274  }
275  void open(const wchar_t *_Filename,
276  IOS_BASE::openmode _Mode = IOS_BASE::out,
277  int _Prot = (int)IOS_BASE::_Openprot) {
278  IO_PREFIX::ofstream::open(_Filename,_Mode,_Prot);
279  }
280 };
281 #elif defined(NCBI_COMPILER_MSVC)
282 # if _MSC_VER >= 1200 && _MSC_VER < 1300
283 class CNcbiOfstream : public IO_PREFIX::ofstream
284 {
285 public:
286  CNcbiOfstream() : m_Fp(0)
287  {
288  }
289 
290  explicit CNcbiOfstream(const char* s,
291  IOS_BASE::openmode mode = IOS_BASE::out)
292  {
293  fastopen(s, mode);
294  }
295 
296  void fastopen(const char* s, IOS_BASE::openmode mode = IOS_BASE::out)
297  {
298  if (is_open() || !(m_Fp = __Fiopen(s, mode | out)))
299  setstate(failbit);
300  else
301  (void) new (rdbuf()) basic_filebuf<char, char_traits<char> >(m_Fp);
302  }
303 
304  virtual ~CNcbiOfstream(void)
305  {
306  if (m_Fp)
307  fclose(m_Fp);
308  }
309 private:
310  FILE* m_Fp;
311 };
312 # else
313 /// Portable alias for ofstream.
314 typedef IO_PREFIX::ofstream CNcbiOfstream;
315 # endif
316 #else
317 /// Portable alias for ofstream.
318 typedef IO_PREFIX::ofstream CNcbiOfstream;
319 #endif
320 
321 #if defined(NCBI_OS_MSWIN) && defined(_UNICODE)
322 class CNcbiFstream : public IO_PREFIX::fstream
323 {
324 public:
325  CNcbiFstream( ) {
326  }
327  explicit CNcbiFstream(
328  const char *_Filename,
329  IOS_BASE::openmode _Mode = IOS_BASE::in | IOS_BASE::out,
330  int _Prot = (int)IOS_BASE::_Openprot
331  ) : IO_PREFIX::fstream(
332  ncbi_Utf8ToWstring(_Filename).c_str(), _Mode, _Prot) {
333  }
334  explicit CNcbiFstream(
335  const wchar_t *_Filename,
336  IOS_BASE::openmode _Mode = IOS_BASE::in | IOS_BASE::out,
337  int _Prot = (int)IOS_BASE::_Openprot
338  ) : IO_PREFIX::fstream(_Filename,_Mode,_Prot) {
339  }
340 
341  void open(
342  const char *_Filename,
343  IOS_BASE::openmode _Mode = IOS_BASE::in | IOS_BASE::out,
344  int _Prot = (int)IOS_BASE::_Openprot) {
345  IO_PREFIX::fstream::open(
346  ncbi_Utf8ToWstring(_Filename).c_str(), _Mode, _Prot);
347  }
348  void open(const wchar_t *_Filename,
349  IOS_BASE::openmode _Mode = IOS_BASE::in | IOS_BASE::out,
350  int _Prot = (int)ios_base::_Openprot) {
351  IO_PREFIX::fstream::open(_Filename,_Mode,_Prot);
352  }
353 };
354 #elif defined(NCBI_COMPILER_MSVC)
355 # if _MSC_VER >= 1200 && _MSC_VER < 1300
356 class CNcbiFstream : public IO_PREFIX::fstream
357 {
358 public:
359  CNcbiFstream() : m_Fp(0)
360  {
361  }
362 
363  explicit CNcbiFstream(const char* s,
364  IOS_BASE::openmode
366  {
367  fastopen(s, mode);
368  }
369 
370  void fastopen(const char* s, IOS_BASE::openmode
372  {
373  if (is_open() || !(m_Fp = __Fiopen(s, mode)))
374  setstate(failbit);
375  else
376  (void) new (rdbuf()) basic_filebuf<char, char_traits<char> >(m_Fp);
377  }
378 
379  virtual ~CNcbiFstream(void)
380  {
381  if (m_Fp)
382  fclose(m_Fp);
383  }
384 private:
385  FILE* m_Fp;
386 };
387 # else
388 /// Portable alias for fstream.
389 typedef IO_PREFIX::fstream CNcbiFstream;
390 # endif
391 #else
392 /// Portable alias for fstream.
393 typedef IO_PREFIX::fstream CNcbiFstream;
394 #endif
395 
396 // Standard I/O streams
397 #define NcbiCin IO_PREFIX::cin
398 #define NcbiCout IO_PREFIX::cout
399 #define NcbiCerr IO_PREFIX::cerr
400 #define NcbiClog IO_PREFIX::clog
401 
402 // I/O manipulators (the list may be incomplete)
403 #define NcbiEndl IO_PREFIX::endl
404 #define NcbiEnds IO_PREFIX::ends
405 #define NcbiFlush IO_PREFIX::flush
406 
407 #define NcbiDec IO_PREFIX::dec
408 #define NcbiHex IO_PREFIX::hex
409 #define NcbiOct IO_PREFIX::oct
410 #define NcbiWs IO_PREFIX::ws
411 
412 #define NcbiFixed IO_PREFIX::fixed
413 #define NcbiScientific IO_PREFIX::scientific
414 
415 #define NcbiSetbase IO_PREFIX::setbase
416 #define NcbiResetiosflags IO_PREFIX::resetiosflags
417 #define NcbiSetiosflags IO_PREFIX::setiosflags
418 #define NcbiSetfill IO_PREFIX::setfill
419 #define NcbiSetprecision IO_PREFIX::setprecision
420 #define NcbiSetw IO_PREFIX::setw
421 
422 // I/O state
423 #define NcbiGoodbit IOS_PREFIX::goodbit
424 #define NcbiEofbit IOS_PREFIX::eofbit
425 #define NcbiFailbit IOS_PREFIX::failbit
426 #define NcbiBadbit IOS_PREFIX::badbit
427 #define NcbiHardfail IOS_PREFIX::hardfail
428 
429 
430 /// Platform-specific EndOfLine
432 extern const char* Endl(void);
433 
434 /// Read from "is" to "str" up to the delimiter symbol "delim" (or EOF)
436 extern CNcbiIstream& NcbiGetline(CNcbiIstream& is, string& str, char delim,
437  string::size_type* count = NULL);
438 
439 /// Read from "is" to "str" up to any symbol contained within "delims" (or EOF)
441 extern CNcbiIstream& NcbiGetline(CNcbiIstream& is, string& str,
442  const string& delims,
443  string::size_type* count = NULL);
444 
445 /// Read from "is" to "str" the next line
446 /// (taking into account platform specifics of End-of-Line)
448 extern CNcbiIstream& NcbiGetlineEOL(CNcbiIstream& is, string& str,
449  string::size_type* count = NULL);
450 
451 
452 /// Copy entire contents of stream "is" into "os".
453 /// @return
454 /// "true" if the operation was successful, i.e. "is" had been read entirely
455 /// with all of its _available_ contents (including none) written to "os";
456 /// "false" if either extraction from "is" or insertion into "os" failed.
457 ///
458 /// Note that input ("is") stream state is not always asserted accurately, in
459 /// particular, upon successful completion "is.eof()" may not necessarily be
460 /// true. The call may throw exceptions only if they are enabled on the
461 /// respective stream(s).
462 ///
463 /// Note that the call is an extension to the standard
464 /// ostream& ostream::operator<<(streambuf*),
465 /// which severely lacks error checking (esp. for partial write failures).
466 ///
467 /// NOTE that the call (as well as the mentioned STL counterpart) provides
468 /// only a mechanism of delivering data to the destination "os" stream(buf);
469 /// and the successful return result does not generally guarantee that the
470 /// data have yet reached the physical destination. Other "os"-specific API
471 /// must be performed to assure the data integrity at the receiving device;
472 /// such as checking for errors after doing a "close()" on an ofstream "os".
473 /// E.g. data uploading into the Toolkit FTP stream must be finalized with a
474 /// read for the byte count delivered; otherwise, it may not work correctly.
475 /// @sa
476 /// CConn_IOStream
478 extern bool NcbiStreamCopy(CNcbiOstream& os, CNcbiIstream& is);
479 
480 
481 /// Same as NcbiStreamCopy() but throws an CCoreException when copy fails.
482 /// @sa
483 /// NcbiStreamCopy, CCoreException
485 extern void NcbiStreamCopyThrow(CNcbiOstream& os, CNcbiIstream& is);
486 
487 
488 /// Input the entire contents of an istream into a string (NULL causes drain).
489 /// "is" gets its failbit set if nothing was extracted from it; and gets its
490 /// eofbit (w/o failbit) set if the stream has reached an EOF condition.
491 ///
492 /// @param pos
493 /// Where in "*s" to begin saving data (ignored when "s" == NULL).
494 /// @return
495 /// Size of copied data if the operation was successful (i.e. "is" had
496 /// reached EOF), 0 otherwise.
497 /// @note
498 /// If "s" != NULL, then "s->size() >= pos" always upon return.
499 /// @sa
500 /// NcbiStreamCopy
502 extern size_t NcbiStreamToString(string* s, CNcbiIstream& is, size_t pos = 0);
503 
504 
505 /// Compare stream contents in binary form.
506 ///
507 /// @param is1
508 /// First stream to compare.
509 /// @param is2
510 /// Second stream to compare.
511 /// @return
512 /// TRUE if streams content is equal; FALSE otherwise.
514 extern bool NcbiStreamCompare(CNcbiIstream& is1, CNcbiIstream& is2);
515 
516 /// Mode to compare streams in text form.
518  /// Skip end-of-line characters ('\r' and '\n')
520  ///< Skip white spaces (in terms of isspace(), including end-of-line)
522 };
523 
524 /// Compare stream contents in text form.
525 ///
526 /// @param is1
527 /// First stream to compare.
528 /// @param is2
529 /// Second stream to compare.
530 /// @param mode
531 /// Type of white space characters to ignore.
532 /// @param buf_size
533 /// Size of buffer to read stream.
534 /// Zero value means using default buffer size.
535 /// @return
536 /// TRUE if streams content is equal; FALSE otherwise.
538 extern bool NcbiStreamCompareText(CNcbiIstream& is1, CNcbiIstream& is2,
539  ECompareTextMode mode, size_t buf_size = 0);
540 
541 /// Compare stream content with string in text form.
542 ///
543 /// @param is
544 /// Stream to compare.
545 /// @param str
546 /// String to compare.
547 /// @param mode
548 /// Type of white space characters to ignore.
549 /// @param buf_size
550 /// Size of buffer to read stream.
551 /// Zero value means using default buffer size.
552 /// @return
553 /// TRUE if stream and string content is equal; FALSE otherwise.
555 extern bool NcbiStreamCompareText(CNcbiIstream& is, const string& str,
556  ECompareTextMode mode, size_t buf_size = 0);
557 
558 
559 # define CT_INT_TYPE NCBI_NS_STD::char_traits<char>::int_type
560 # define CT_CHAR_TYPE NCBI_NS_STD::char_traits<char>::char_type
561 # define CT_POS_TYPE NCBI_NS_STD::char_traits<char>::pos_type
562 # define CT_OFF_TYPE NCBI_NS_STD::char_traits<char>::off_type
563 # define CT_EOF NCBI_NS_STD::char_traits<char>::eof()
564 # define CT_NOT_EOF NCBI_NS_STD::char_traits<char>::not_eof
565 # define CT_TO_INT_TYPE NCBI_NS_STD::char_traits<char>::to_int_type
566 # define CT_TO_CHAR_TYPE NCBI_NS_STD::char_traits<char>::to_char_type
567 # define CT_EQ_INT_TYPE NCBI_NS_STD::char_traits<char>::eq_int_type
568 
569 
570 #ifdef NCBI_COMPILER_MIPSPRO
571 /// Special workaround for MIPSPro 1-byte look-ahead issues
572 class CMIPSPRO_ReadsomeTolerantStreambuf : public CNcbiStreambuf
573 {
574 public:
575  /// NB: Do not use these two ugly, weird, ad-hoc methods, ever!!!
576  void MIPSPRO_ReadsomeBegin(void)
577  {
578  if (!m_MIPSPRO_ReadsomeGptrSetLevel++)
579  m_MIPSPRO_ReadsomeGptr = gptr();
580  }
581  void MIPSPRO_ReadsomeEnd (void)
582  {
583  --m_MIPSPRO_ReadsomeGptrSetLevel;
584  }
585 protected:
586  CMIPSPRO_ReadsomeTolerantStreambuf() : m_MIPSPRO_ReadsomeGptrSetLevel(0) {}
587 
588  const CT_CHAR_TYPE* m_MIPSPRO_ReadsomeGptr;
589  unsigned int m_MIPSPRO_ReadsomeGptrSetLevel;
590 };
591 #endif // NCBI_COMPILER_MIPSPRO
592 
593 
594 /// Convert stream position to 64-bit int
595 ///
596 /// On most systems stream position is a structure,
597 /// this function converts it to plain numeric value.
598 ///
599 /// @sa NcbiInt8ToStreampos
600 ///
601 inline
603 {
604  return (CT_OFF_TYPE)(stream_pos - (CT_POS_TYPE)((CT_OFF_TYPE)0));
605 }
606 
607 
608 /// Convert plain numeric stream position (offset) into
609 /// stream position usable with STL stream library.
610 ///
611 /// @sa NcbiStreamposToInt8
612 inline
614 {
615  return (CT_POS_TYPE)((CT_OFF_TYPE) 0) + (CT_OFF_TYPE)(pos);
616 }
617 
618 
619 // CNcbiOstrstreamToString class helps to convert CNcbiOstream buffer to string
620 // Sample usage:
621 /*
622 string GetString(void)
623 {
624  CNcbiOstrstream buffer;
625  buffer << "some text";
626  return CNcbiOstrstreamToString(buffer);
627 }
628 */
629 // Note: there is no requirement to put '\0' char at the end of buffer;
630 // there is no need to explicitly "unfreeze" the "out" stream.
631 
633 {
635  CNcbiOstrstreamToString& operator= (const CNcbiOstrstreamToString&);
636 public:
637  CNcbiOstrstreamToString(CNcbiOstrstream& out)
638  : m_Out(out)
639  {
640  }
641  operator string(void) const;
642 private:
643  friend NCBI_XNCBI_EXPORT CNcbiOstream& operator<<(CNcbiOstream& out, const CNcbiOstrstreamToString& s);
644 
645  CNcbiOstrstream& m_Out;
646 };
647 
649 CNcbiOstream& operator<<(CNcbiOstream& out, const CNcbiOstrstreamToString& s);
650 
651 inline
652 Int8
653 GetOssSize(CNcbiOstrstream& oss)
654 {
655 #ifdef NCBI_SHUN_OSTRSTREAM
656  return NcbiStreamposToInt8(oss.tellp());
657 #else
658  return oss.pcount();
659 #endif
660 }
661 
662 inline
663 bool
664 IsOssEmpty(CNcbiOstrstream& oss)
665 {
666  return GetOssSize(oss) == 0;
667 }
668 
669 /// Utility class for automatic conversion of strings to all uppercase letters.
670 /// Sample usage:
671 /// out << "Original: \"" << str << '"' << endl;
672 /// out << "Uppercase: \"" << Upcase(str) << '"' << endl;
673 
675 {
676 public:
677  explicit CUpcaseStringConverter(const string& s) : m_String(s) { }
678  const string& m_String;
679 };
680 
682 {
683 public:
684  explicit CUpcaseCharPtrConverter(const char* s) : m_String(s) { }
685  const char* m_String;
686 };
687 
688 
689 /// Utility class for automatic conversion of strings to all lowercase letters.
690 /// Sample usage:
691 /// out << "Original: \"" << str << '"' << endl;
692 /// out << "Lowercase: \"" << Locase(str) << '"' << endl;
693 
695 {
696 public:
697  explicit CLocaseStringConverter(const string& s) : m_String(s) { }
698  const string& m_String;
699 };
700 
702 {
703 public:
704  explicit CLocaseCharPtrConverter(const char* s) : m_String(s) { }
705  const char* m_String;
706 };
707 
708 
709 /// Utility class for automatic conversion of strings (that may contain
710 /// non-graphical characters) to a safe "printable" form.
711 /// The safe printable form utilizes '\'-quoted special sequences, as well
712 /// as either contracted or full 3-digit octal representation of non-printable
713 /// characters, always making sure there is no ambiguity in the string
714 /// interpretation (so that if the printed form is used back in a C program,
715 /// it will be equivalent to the original string).
716 /// Sample usage:
717 /// out << "Printable: \"" << Printable(str) << '"' << endl;
718 
720 {
721 public:
722  explicit CPrintableStringConverter(const string& s) : m_String(s) { }
723  const string& m_String;
724 };
725 
727 {
728 public:
729  explicit CPrintableCharPtrConverter(const char* s) : m_String(s) { }
730  const char* m_String;
731 };
732 
733 
734 /* @} */
735 
736 
737 /// Convert one single character to a "printable" form.
738 /// A "printable" form is one of well-known C-style backslash-quoted sequences
739 /// ('\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\'', '\"'),
740 /// or '\xXX' for other non-printable characters (per isprint()), or a
741 /// graphical representation of 'c' as an ASCII character.
742 /// @note *DO NOT USE* to convert strings! Because of the '\xXX' notation,
743 /// such conversions can result in ambiguity (e.g. "\xAAA" is a valid
744 /// _single_-character string per the standard).
746 NCBI_XNCBI_EXPORT extern string Printable(char c);
747 
748 
749 inline
750 char Upcase(char c)
751 {
752  return static_cast<char>(toupper((unsigned char) c));
753 }
754 
755 inline
757 {
758  return CUpcaseStringConverter(s);
759 }
760 
761 inline
763 {
764  return CUpcaseCharPtrConverter(s);
765 }
766 
767 inline
768 char Locase(char c)
769 {
770  return static_cast<char>(tolower((unsigned char) c));
771 }
772 
773 inline
775 {
776  return CLocaseStringConverter(s);
777 }
778 
779 inline
781 {
782  return CLocaseCharPtrConverter(s);
783 }
784 
785 inline
787 {
788  return CPrintableStringConverter(s);
789 }
790 
791 inline
793 {
794  return CPrintableCharPtrConverter(s);
795 }
796 
798 CNcbiOstream& operator<<(CNcbiOstream& out, CUpcaseStringConverter s);
799 
801 CNcbiOstream& operator<<(CNcbiOstream& out, CUpcaseCharPtrConverter s);
802 
804 CNcbiOstream& operator<<(CNcbiOstream& out, CLocaseStringConverter s);
805 
807 CNcbiOstream& operator<<(CNcbiOstream& out, CLocaseCharPtrConverter s);
808 
810 CNcbiOstream& operator<<(CNcbiOstream& out, CPrintableStringConverter s);
811 
813 CNcbiOstream& operator<<(CNcbiOstream& out, CPrintableCharPtrConverter s);
814 
815 #ifdef NCBI_COMPILER_MSVC
816 # if _MSC_VER >= 1200 && _MSC_VER < 1300
818 CNcbiOstream& operator<<(CNcbiOstream& out, __int64 val);
819 # endif
820 #endif
821 
822 
823 /////////////////////////////////////////////////////////////////////////////
824 ///
825 /// Helper functions to read plain-text data streams.
826 /// It understands Byte Order Mark (BOM) and converts the input if needed.
827 ///
828 /// See clause 13.6 in
829 /// http://www.unicode.org/unicode/uni2book/ch13.pdf
830 /// and also
831 /// http://unicode.org/faq/utf_bom.html#BOM
832 ///
833 /// @sa ReadIntoUtf8, GetTextEncodingForm
835  /// Stream has no BOM.
837  /// Stream has no BOM.
839  /// Stream has no BOM.
841  /// Stream has UTF8 BOM.
843  /// Stream has UTF16 BOM. Byte order is native for this OS
845  /// Stream has UTF16 BOM. Byte order is nonnative for this OS
847 };
848 
849 
850 /// How to read the text if the encoding form is not known (i.e. passed
851 /// "eEncodingForm_Unknown" and the stream does not have BOM too)
852 ///
853 /// @sa ReadIntoUtf8
855  /// Read the text "as is" (raw octal data). The read data can then
856  /// be accessed using the regular std::string API (rather than the
857  /// CStringUTF8 one).
859 
860  /// Try to guess the text's encoding form.
861  ///
862  /// @note
863  /// In this case the encoding is a guesswork, which is not necessarily
864  /// correct. If the guess is wrong then the data may be distorted on
865  /// read. Use CStringUTF8::IsValid() to verify that guess. If it
866  /// does not verify, then the read data can be accessed using the
867  /// regular std::string API (rather than the CStringUTF8 one).
869 };
870 
871 
872 /// Read all input data from stream and try convert it into UTF8 string.
873 ///
874 /// @param input
875 /// Input text stream
876 /// @param result
877 /// UTF8 string (but it can be a raw octet string if the encoding is unknown)
878 /// @param what_if_no_bom
879 /// What to do if the 'encoding_form' is passed "eEncodingForm_Unknown" and
880 /// the BOM is not detected in the stream
881 /// @return
882 /// The encoding as detected based on the BOM
883 /// ("eEncodingForm_Unknown" if there was no BOM).
886  CNcbiIstream& input,
888  EEncodingForm encoding_form = eEncodingForm_Unknown,
889  EReadUnknownNoBOM what_if_no_bom = eNoBOM_GuessEncoding
890 );
891 
892 
893 /// Whether to discard BOM or to keep it in the input stream
894 ///
895 /// @sa GetTextEncodingForm
897  eBOM_Discard, ///< Discard the read BOM bytes
898  eBOM_Keep ///< Push the read BOM bytes back into the input stream
899 };
900 
901 
902 /// Detect if the stream has BOM.
903 ///
904 /// @param input
905 /// Input stream
906 /// @param discard_bom
907 /// Whether to discard the read BOM bytes or to push them back to the stream
908 ///
909 /// NOTE: If the function needs to push back more than one char then it uses
910 /// CStreamUtils::Pushback().
911 /// @sa CStreamUtils::Pushback()
914  EBOMDiscard discard_bom);
915 
916 /// Byte Order Mark helper class to use in serialization
917 ///
918 /// @sa GetTextEncodingForm
920 {
921 public:
924  }
925 
927  : m_EncodingForm(encodingForm) {
928  }
929 
931  return m_EncodingForm;
932  }
933  void SetEncodingForm(EEncodingForm encodingForm) {
934  m_EncodingForm = encodingForm;
935  }
936 private:
938 };
939 
940 /// Write Byte Order Mark into output stream
941 NCBI_XNCBI_EXPORT CNcbiOstream& operator<< (CNcbiOstream& str, const CByteOrderMark& bom);
942 
943 /// Read Byte Order Mark, if present, from input stream
944 ///
945 /// @note
946 /// If BOM is found, stream position advances,
947 /// otherwise, stream position remains unchanged
948 ///
949 /// @sa GetTextEncodingForm
950 inline
951 CNcbiIstream& operator>> (CNcbiIstream& str, CByteOrderMark& bom) {
953  return str;
954 }
955 
956 #include <corelib/ncbi_base64.h>
957 
958 
960 
961 
962 // Provide formatted I/O of standard C++ "string" by "old-fashioned" IOSTREAMs
963 // NOTE: these must have been inside the _NCBI_SCOPE and without the
964 // "ncbi::" and "std::" prefixes, but there is some bug in SunPro 5.0...
965 #if defined(NCBI_USE_OLD_IOSTREAM)
967  const NCBI_NS_STD::string& str);
970 #endif // NCBI_USE_OLD_IOSTREAM
971 
972 
973 #endif /* NCBISTRE__HPP */
Read the text "as is" (raw octal data).
Definition: ncbistre.hpp:858
IO_PREFIX::streambuf CNcbiStreambuf
Portable alias for streambuf.
Definition: ncbistre.hpp:136
IO_PREFIX::fstream CNcbiFstream
Portable alias for fstream.
Definition: ncbistre.hpp:393
EEncodingForm ReadIntoUtf8(CNcbiIstream &input, CStringUTF8 *result, EEncodingForm encoding_form=eEncodingForm_Unknown, EReadUnknownNoBOM what_if_no_bom=eNoBOM_GuessEncoding)
Read all input data from stream and try convert it into UTF8 string.
Definition: ncbistre.cpp:624
const string & m_String
Definition: ncbistre.hpp:678
const char * m_String
Definition: ncbistre.hpp:705
IO_PREFIX::strstream CNcbiStrstream
Portable alias for strstream.
Definition: ncbistre.hpp:158
int tolower(Uchar c)
Definition: ncbictype.hpp:72
Try to guess the text's encoding form.
Definition: ncbistre.hpp:868
EEncodingForm
Helper functions to read plain-text data streams.
Definition: ncbistre.hpp:834
void NcbiStreamCopyThrow(CNcbiOstream &os, CNcbiIstream &is)
Same as NcbiStreamCopy() but throws an CCoreException when copy fails.
Definition: ncbistre.cpp:256
IO_PREFIX::iostream CNcbiIostream
Portable alias for iostream.
Definition: ncbistre.hpp:145
std::ofstream out("events_result.xml")
main entry point for tests
CNcbiIstream & NcbiGetlineEOL(CNcbiIstream &is, string &str, string::size_type *count=NULL)
Read from "is" to "str" the next line (taking into account platform specifics of End-of-Line) ...
EEncodingForm GetEncodingForm(void) const
Definition: ncbistre.hpp:930
Utility class for automatic conversion of strings to all uppercase letters.
Definition: ncbistre.hpp:674
char Upcase(char c)
Definition: ncbistre.hpp:750
IO_PREFIX::ios CNcbiIos
Portable alias for ios.
Definition: ncbistre.hpp:133
int toupper(Uchar c)
Definition: ncbictype.hpp:73
EEncodingForm GetTextEncodingForm(CNcbiIstream &input, EBOMDiscard discard_bom)
Detect if the stream has BOM.
Definition: ncbistre.cpp:756
#define CT_POS_TYPE
Definition: ncbistre.hpp:561
IO_PREFIX::streampos CNcbiStreampos
Portable alias for streampos.
Definition: ncbistre.hpp:127
string
Definition: cgiapp.hpp:498
bool NcbiStreamCompareText(CNcbiIstream &is1, CNcbiIstream &is2, ECompareTextMode mode, size_t buf_size=0)
Compare stream contents in text form.
Definition: ncbistre.cpp:367
void SetEncodingForm(EEncodingForm encodingForm)
Definition: ncbistre.hpp:933
CNcbiOstrstream & m_Out
Definition: ncbistre.hpp:645
signed NCBI_INT8_TYPE Int8
Signed 8 byte sized integer.
Definition: ncbitype.h:143
#define NCBI_XNCBI_EXPORT
Definition: ncbi_export.h:1266
Utility class for automatic conversion of strings to all lowercase letters.
Definition: ncbistre.hpp:694
Stream has no BOM.
Definition: ncbistre.hpp:836
#define NULL
Definition: ncbistd.hpp:225
Skip end-of-line characters ('' and ' ')
Definition: ncbistre.hpp:519
Push the read BOM bytes back into the input stream.
Definition: ncbistre.hpp:898
CNcbiOstrstreamToString(CNcbiOstrstream &out)
Definition: ncbistre.hpp:637
Discard the read BOM bytes.
Definition: ncbistre.hpp:897
IO_PREFIX::strstreambuf CNcbiStrstreambuf
Portable alias for strstreambuf.
Definition: ncbistre.hpp:148
Byte Order Mark helper class to use in serialization.
Definition: ncbistre.hpp:919
Stream has no BOM.
Definition: ncbistre.hpp:840
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
Int8 NcbiStreamposToInt8(NCBI_NS_STD::char_traits< char >::pos_type stream_pos)
Convert stream position to 64-bit int.
Definition: ncbistre.hpp:602
Stream has UTF8 BOM.
Definition: ncbistre.hpp:842
const char * m_String
Definition: ncbistre.hpp:685
mdb_mode_t mode
Definition: lmdb++.h:46
EReadUnknownNoBOM
How to read the text if the encoding form is not known (i.e.
Definition: ncbistre.hpp:854
IO_PREFIX::istrstream CNcbiIstrstream
Portable alias for istrstream.
Definition: ncbistre.hpp:151
bool NcbiStreamCompare(CNcbiIstream &is1, CNcbiIstream &is2)
Compare stream contents in binary form.
Definition: ncbistre.cpp:329
#define CT_OFF_TYPE
Definition: ncbistre.hpp:562
std::string CStringUTF8
Definition: ncbistl.hpp:205
CPrintableStringConverter(const string &s)
Definition: ncbistre.hpp:722
#define CT_CHAR_TYPE
Definition: ncbistre.hpp:560
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:142
CPrintableCharPtrConverter(const char *s)
Definition: ncbistre.hpp:729
bool IsOssEmpty(CNcbiOstrstream &oss)
Definition: ncbistre.hpp:664
EEncodingForm m_EncodingForm
Definition: ncbistre.hpp:937
Stream has UTF16 BOM. Byte order is nonnative for this OS.
Definition: ncbistre.hpp:846
Int8 GetOssSize(CNcbiOstrstream &oss)
Definition: ncbistre.hpp:653
CByteOrderMark(EEncodingForm encodingForm)
Definition: ncbistre.hpp:926
Utility class for automatic conversion of strings (that may contain non-graphical characters) to a sa...
Definition: ncbistre.hpp:719
CLocaseStringConverter(const string &s)
Definition: ncbistre.hpp:697
CNcbiIstream & operator>>(CNcbiIstream &str, CByteOrderMark &bom)
Read Byte Order Mark, if present, from input stream.
Definition: ncbistre.hpp:951
bool NcbiStreamCopy(CNcbiOstream &os, CNcbiIstream &is)
Copy entire contents of stream "is" into "os".
Definition: ncbistre.cpp:216
Stream has no BOM.
Definition: ncbistre.hpp:838
static BOOL utf8
Definition: pcregrep.c:171
static const char * str(char *buf, int n)
Definition: stats.c:84
Stream has UTF16 BOM. Byte order is native for this OS.
Definition: ncbistre.hpp:844
IO_PREFIX::streamoff CNcbiStreamoff
Portable alias for streamoff.
Definition: ncbistre.hpp:130
Catch attempts to call ctype functions with bad types.
else result
Definition: token2.c:20
CNcbiIstream & NcbiGetline(CNcbiIstream &is, string &str, char delim, string::size_type *count=NULL)
Read from "is" to "str" up to the delimiter symbol "delim" (or EOF)
ECompareTextMode
Mode to compare streams in text form.
Definition: ncbistre.hpp:517
IO_PREFIX::ostrstream CNcbiOstrstream
Portable alias for ostrstream.
Definition: ncbistre.hpp:155
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:245
#define NCBI_DEPRECATED
Definition: ncbiconf_msvc.h:71
CNcbiOstream & operator<<(CNcbiOstream &out, const CNcbiOstrstreamToString &s)
Definition: ncbistre.cpp:416
NCBI_NS_STD::char_traits< char >::pos_type NcbiInt8ToStreampos(Int8 pos)
Convert plain numeric stream position (offset) into stream position usable with STL stream library...
Definition: ncbistre.hpp:613
EBOMDiscard
Whether to discard BOM or to keep it in the input stream.
Definition: ncbistre.hpp:896
CUpcaseCharPtrConverter(const char *s)
Definition: ncbistre.hpp:684
const char * Endl(void)
Platform-specific EndOfLine.
Definition: ncbistre.cpp:189
std::istream & in(std::istream &in_, double &x_)
const string & m_String
Definition: ncbistre.hpp:723
size_t NcbiStreamToString(string *s, CNcbiIstream &is, size_t pos=0)
Input the entire contents of an istream into a string (NULL causes drain).
Definition: ncbistre.cpp:269
IO_PREFIX::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:318
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:139
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
CByteOrderMark(void)
Definition: ncbistre.hpp:922
CUpcaseStringConverter(const string &s)
Definition: ncbistre.hpp:677
string Printable(char c)
Convert one single character to a "printable" form.
Definition: ncbistre.cpp:478
char Locase(char c)
Definition: ncbistre.hpp:768
const string & m_String
Definition: ncbistre.hpp:698
IO_PREFIX::filebuf CNcbiFilebuf
Portable alias for filebuf.
Definition: ncbistre.hpp:167
static int input()
CLocaseCharPtrConverter(const char *s)
Definition: ncbistre.hpp:704
Modified on Mon Oct 16 16:20:44 2017 by modify_doxy.py rev. 546573