36 #include <corelib/ncbisys.hpp>
39 #if defined(NCBI_OS_UNIX)
47 #if defined(NCBI_OS_MSWIN) && defined(_UNICODE)
48 wstring ncbi_Utf8ToWstring(
const char *
utf8)
60 IOS_BASE::fmtflags
f = is.flags();
61 is.unsetf(IOS_BASE::skipws);
69 CNcbiIstream::sentry s(is);
91 if (delim_pos !=
NPOS) {
96 ch = is.rdbuf()->sgetc();
106 if (
size == max_size) {
113 if (pos ==
sizeof(
buf)) {
122 *count =
size + delim_count;
140 #if defined(NCBI_USE_OLD_IOSTREAM)
162 if (
size == max_size) {
167 is.get(
buf,
n <
sizeof(
buf) ?
n :
sizeof(
buf), delim);
168 n = (size_t) is.gcount();
172 }
while ( is.good() );
186 #if defined(NCBI_OS_MSWIN)
187 static const char s_Endl[] =
"\r\n";
189 static const char s_Endl[] =
"\n";
198 #if defined(NCBI_OS_MSWIN)
200 if (!
str.empty() &&
str[
str.length() - 1] ==
'\r')
201 str.resize(
str.length() - 1);
202 #elif defined(NCBI_OS_DARWIN)
213 if (!os.good() || is.bad())
221 return os.good() && os.flush() ?
true :
false;
227 bool success =
false;
247 "Output stream already in bad state");
254 "Input stream already in bad state (at EOF)");
258 SIZE_TYPE ninstream = count, ninbuffer = 0;
260 while (ninstream > 0 || ninbuffer > 0) {
262 auto nwanted =
min(
sizeof(
buffer) - ninbuffer, ninstream);
263 streamsize nread = 0;
265 is.read(
buffer + ninbuffer, nwanted);
272 auto nwritten =
outbuf->sputn(
buffer, nread + ninbuffer);
273 if ( os.bad() || nwritten == 0) {
278 ninbuffer = nread + ninbuffer - nwritten;
307 size_t buf_size =
sizeof(
buf);
312 if (
str->size() < str_size + buf_size)
313 str->resize(str_size + buf_size);
319 is.read(
str ? &(*
str)[str_size] :
buf, buf_size);
322 str->resize(str_size);
325 streamsize count = is.gcount();
326 str_size += (size_t) count;
328 if ((
size_t) count == buf_size) {
329 if (buf_size < (1UL << 20))
331 str->resize(str_size + buf_size);
339 str->resize(str_size);
341 if (!(str_size -= pos)) {
348 IOS_BASE::iostate iostate = is.rdstate();
359 char c1 = (char)is1.get();
360 char c2 = (char)is2.get();
365 return is1.eof() && is2.eof();
371 char*
buf,
size_t buf_size,
char*& pos,
size_t& sizeleft)
376 is.read(
buf, buf_size);
377 sizeleft = (size_t) is.gcount();
387 && (c ==
'\n' || c ==
'\r')) ||
389 &&
isspace((
unsigned char) c)) );
400 char* buf1 =
new char[buf_size];
401 char* buf2 =
new char[buf_size];
402 size_t size1 = 0, size2 = 0;
403 char *pos1 = 0, *pos2 = 0;
415 return equal && is1.eof() && is2.eof();
427 CNcbiOstrstreamToString::operator
string(
void)
const
429 #ifdef NCBI_SHUN_OSTRSTREAM
436 const char*
str = m_Out.str();
445 #ifdef NCBI_SHUN_OSTRSTREAM
451 s.
m_Out.freeze(
false);
479 for (
const char* c = s.
m_String; *c; ++c ) {
488 for (
const char* c = s.
m_String; *c; ++c ) {
495 #ifdef NCBI_COMPILER_MSVC
496 # if _MSC_VER >= 1200 && _MSC_VER < 1300
507 static const char kHex[] =
"0123456789ABCDEF";
511 case '\0': s =
"\\0";
break;
512 case '\a': s =
"\\a";
break;
513 case '\b': s =
"\\b";
break;
514 case '\f': s =
"\\f";
break;
515 case '\n': s =
"\\n";
break;
516 case '\r': s =
"\\r";
break;
517 case '\t': s =
"\\t";
break;
518 case '\v': s =
"\\v";
break;
519 case '\\': s =
"\\\\";
break;
520 case '\'': s =
"\\'";
break;
521 case '"': s =
"\\\"";
break;
523 if ( !
isprint((
unsigned char) c) ) {
525 s += kHex[(
unsigned char) c / 16];
526 s += kHex[(
unsigned char) c % 16];
539 case '\a':
out.write(
"\\a", 2);
return;
540 case '\b':
out.write(
"\\b", 2);
return;
541 case '\f':
out.write(
"\\f", 2);
return;
542 case '\n':
out.write(
"\\n", 2);
return;
543 case '\r':
out.write(
"\\r", 2);
return;
544 case '\t':
out.write(
"\\t", 2);
return;
545 case '\v':
out.write(
"\\v", 2);
return;
546 case '\\':
out.write(
"\\\\", 2);
return;
547 case '\'':
out.write(
"\\'", 2);
return;
548 case '"':
out.write(
"\\\"", 2);
return;
550 if (p ==
'?' ||
n ==
'?')
556 if (
isprint((
unsigned char) c) ) {
563 bool full =
'0' <=
n &&
n <=
'7' ?
true :
false;
569 v = (
unsigned char) c >> 6;
571 octal[k++] = char(
'0' + v);
574 v = ((
unsigned char) c >> 3) & 7;
576 octal[k++] = char(
'0' + v);
578 v = (
unsigned char) c & 7;
579 octal[k++] = char(
'0' + v);
589 const char* data = s.
m_String.data();
590 for (
size_t i = 0;
i <
size - 1; ++
i) {
615 #if defined(NCBI_COMPILER_WORKSHOP)
617 # if (NCBI_COMPILER_VERSION == 530)
621 istream& istream::read(
char *s, streamsize
n)
623 sentry ipfx(*
this, 1);
626 if (rdbuf()->sgetc() == traits_type::eof()) {
632 __chcount = rdbuf()->sgetn(s,
n);
633 if (__chcount == 0) {
635 }
else if (__chcount <
n) {
636 setstate(eofbit | failbit);
641 setstate(badbit | failbit);
663 const int buf_size = 4096;
664 char tmp[buf_size+2];
668 const int bom_max = 4;
669 memset(
tmp,0,bom_max);
675 if (
n >= 3 && uc[0] == 0xEF && uc[1] == 0xBB && uc[2] == 0xBF) {
680 else if (
n >= 2 && (us[0] == 0xFEFF || us[0] == 0xFFFE)) {
681 if (us[0] == 0xFEFF) {
718 for (
n =
n/2;
n--; ++u) {
757 "ReadIntoUtf8: cannot guess text encoding");
787 const int bom_max = 4;
789 memset(
tmp, 0, bom_max);
794 if (
n == 1 && (uc[0] == 0xEF || uc[0] == 0xFE || uc[0] == 0xFF)){
796 if (
input.gcount() == 1) {
798 if (us[0] == 0xFEFF) {
800 }
else if (us[0] == 0xFFFE) {
802 }
else if (uc[1] == 0xBB) {
804 if (
input.gcount() == 1) {
841 #ifdef WORDS_BIGENDIAN
848 #ifdef WORDS_BIGENDIAN
867 #if defined(NCBI_USE_OLD_IOSTREAM)
872 return str.empty() ? os : os <<
str.c_str();
887 end = (streamsize) end < is.width() ?
end : is.width();
890 for (ch = is.rdbuf()->sbumpc();
891 ch != EOF && !
isspace((
unsigned char) ch);
892 ch = is.rdbuf()->sbumpc()) {
893 str.append(1, (
char) ch);
Byte Order Mark helper class to use in serialization.
Utility class for automatic conversion of strings to all lowercase letters.
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Utility class for automatic conversion of strings (that may contain non-graphical characters) to a sa...
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Utility class for automatic conversion of strings to all uppercase letters.
Include a standard set of the NCBI C++ Toolkit most basic headers.
constexpr auto end(const ct_const_array< T, N > &in) noexcept
static DLIST_TYPE *DLIST_NAME() prev(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
std::ofstream out("events_result.xml")
main entry point for tests
CNcbiIstream & operator>>(CNcbiIstream &s, const getcontig &c)
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
#define NCBI_CATCH_ALL(message)
This macro is deprecated - use *_X or *_XX variant instead of it.
uint8_t Uint1
1-byte (8-bit) unsigned integer
unsigned char Uchar
Alias for unsigned char.
uint16_t Uint2
2-byte (16-bit) unsigned integer
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
void NcbiStreamCopyThrow(CNcbiOstream &os, CNcbiIstream &is)
Same as NcbiStreamCopy() but throws an CCoreException when copy fails.
EEncodingForm
Helper functions to read plain-text data streams.
CNcbiOstream & operator<<(CNcbiOstream &out, const CNcbiOstrstreamToString &s)
string Printable(char c)
Convert one single character to a "printable" form.
EEncodingForm GetTextEncodingForm(CNcbiIstream &input, EBOMDiscard discard_bom)
Detect if the stream has BOM.
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
EBOMDiscard
Whether to discard BOM or to keep it in the input stream.
static void Pushback(CNcbiIstream &is, CT_CHAR_TYPE *buf, streamsize buf_size, void *del_ptr)
bool NcbiStreamCompare(CNcbiIstream &is1, CNcbiIstream &is2)
Compare stream contents in binary form.
const char * Endl(void)
Platform-specific EndOfLine.
ECompareTextMode
Mode to compare streams in text form.
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
EEncodingForm ReadIntoUtf8(CNcbiIstream &input, CStringUTF8 *result, EEncodingForm ef, EReadUnknownNoBOM what_if_no_bom)
Read all input data from stream and try convert it into UTF8 string.
bool NcbiStreamCopy(CNcbiOstream &os, CNcbiIstream &is)
Copy the entire contents of stream "is" to stream "os".
EReadUnknownNoBOM
How to read the text if the encoding form is not known (i.e.
bool NcbiStreamCompareText(CNcbiIstream &is1, CNcbiIstream &is2, ECompareTextMode mode, size_t buf_size)
Compare stream contents in text form.
size_t NcbiStreamToString(string *str, CNcbiIstream &is, size_t pos)
Input the entire contents of an istream into a string (NULL causes drain).
EEncodingForm GetEncodingForm(void) const
@ eEncodingForm_Utf16Foreign
Stream has UTF16 BOM. Byte order is nonnative for this OS.
@ eEncodingForm_Utf8
Stream has UTF8 BOM.
@ eEncodingForm_ISO8859_1
Stream has no BOM.
@ eEncodingForm_Windows_1252
Stream has no BOM.
@ eEncodingForm_Unknown
Stream has no BOM.
@ eEncodingForm_Utf16Native
Stream has UTF16 BOM. Byte order is native for this OS.
@ eBOM_Keep
Push the read BOM bytes back into the input stream.
@ eCompareText_IgnoreEol
Skip end-of-line characters ('\r' and ' ')
@ eCompareText_IgnoreWhiteSpace
@ eNoBOM_GuessEncoding
Try to guess the text's encoding form.
NCBI_NS_STD::string::size_type SIZE_TYPE
static string Int8ToString(Int8 value, TNumToStringFlags flags=0, int base=10)
Convert Int8 to string.
static EEncoding GuessEncoding(const CTempString &src)
Guess the encoding of the C/C++ string.
static CStringUTF8 AsUTF8(const CTempString &src, EEncoding encoding, EValidate validate=eNoValidate)
Convert into UTF8 from a C/C++ string.
static SIZE_TYPE GetValidBytesCount(const CTempString &src)
Get the number of valid UTF-8 bytes (code units) in buffer.
@ eEncoding_ISO8859_1
Note: From the point of view of the C++.
unsigned int
A callback function used to compare two keys in a database.
const struct ncbi::grid::netcache::search::fields::SIZE size
void NcbiStreamCopyHead(CNcbiOstream &os, CNcbiIstream &is, SIZE_TYPE count)
CNcbiIstream & NcbiGetline(CNcbiIstream &is, string &str, const string &delims, SIZE_TYPE *count)
static void s_WritePrintable(CNcbiOstream &out, char p, char c, char n)
CNcbiIstream & NcbiGetlineEOL(CNcbiIstream &is, string &str, SIZE_TYPE *count)
static char x_GetChar(CNcbiIstream &is, ECompareTextMode mode, char *buf, size_t buf_size, char *&pos, size_t &sizeleft)
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
double f(double x_, const double &y_)
static pcre_uint8 * buffer
static const char * str(char *buf, int n)
uchar outbuf[(1000000+1000000)]