diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /util/stream/input.cpp | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'util/stream/input.cpp')
-rw-r--r-- | util/stream/input.cpp | 352 |
1 files changed, 176 insertions, 176 deletions
diff --git a/util/stream/input.cpp b/util/stream/input.cpp index 11785b1e9e..6e8170f2f9 100644 --- a/util/stream/input.cpp +++ b/util/stream/input.cpp @@ -1,45 +1,45 @@ -#include "input.h" +#include "input.h" #include "output.h" #include "str.h" - + #include <util/charset/wide.h> -#include <util/memory/tempbuf.h> +#include <util/memory/tempbuf.h> #include <util/generic/string.h> #include <util/generic/yexception.h> -#include <util/generic/singleton.h> +#include <util/generic/singleton.h> #include <util/string/cast.h> -#include <util/system/compat.h> -#include <util/system/spinlock.h> - +#include <util/system/compat.h> +#include <util/system/spinlock.h> + #include <cstdlib> IInputStream::IInputStream() noexcept = default; - + IInputStream::~IInputStream() = default; - + size_t IInputStream::DoReadTo(TString& st, char to) { - char ch; - - if (!Read(&ch, 1)) { + char ch; + + if (!Read(&ch, 1)) { return 0; - } - - st.clear(); - + } + + st.clear(); + size_t result = 0; - do { + do { ++result; - if (ch == to) { - break; - } - - st += ch; - } while (Read(&ch, 1)); - + if (ch == to) { + break; + } + + st += ch; + } while (Read(&ch, 1)); + return result; -} - +} + ui64 IInputStream::DoReadAll(IOutputStream& out) { TTempBuf buffer; void* ptr = buffer.Data(); @@ -55,22 +55,22 @@ ui64 IInputStream::DoReadAll(IOutputStream& out) { } size_t IInputStream::Load(void* buf_in, size_t len) { - char* buf = (char*)buf_in; - - while (len) { - const size_t ret = Read(buf, len); - - buf += ret; - len -= ret; - - if (ret == 0) { - break; - } - } - - return buf - (char*)buf_in; -} - + char* buf = (char*)buf_in; + + while (len) { + const size_t ret = Read(buf, len); + + buf += ret; + len -= ret; + + if (ret == 0) { + break; + } + } + + return buf - (char*)buf_in; +} + void IInputStream::LoadOrFail(void* buf, size_t len) { const size_t realLen = Load(buf, len); if (Y_UNLIKELY(realLen != len)) { @@ -80,14 +80,14 @@ void IInputStream::LoadOrFail(void* buf, size_t len) { size_t IInputStream::ReadLine(TString& st) { const size_t ret = ReadTo(st, '\n'); - + if (ret && !st.empty() && st.back() == '\r') { - st.pop_back(); - } - + st.pop_back(); + } + return ret; -} - +} + size_t IInputStream::ReadLine(TUtf16String& w) { TString s; size_t result = ReadLine(s); @@ -101,46 +101,46 @@ size_t IInputStream::ReadLine(TUtf16String& w) { TString IInputStream::ReadLine() { TString ret; - - if (!ReadLine(ret)) { + + if (!ReadLine(ret)) { ythrow yexception() << "can not read line from stream"; - } - - return ret; -} - + } + + return ret; +} + TString IInputStream::ReadTo(char ch) { TString ret; - - if (!ReadTo(ret, ch)) { + + if (!ReadTo(ret, ch)) { ythrow yexception() << "can not read from stream"; - } - - return ret; -} - + } + + return ret; +} + size_t IInputStream::Skip(size_t sz) { return DoSkip(sz); } size_t IInputStream::DoSkip(size_t sz) { - if (sz < 128) { - return Load(alloca(sz), sz); - } - + if (sz < 128) { + return Load(alloca(sz), sz); + } + TTempBuf buf; size_t total = 0; - - while (sz) { - const size_t lresult = Read(buf.Data(), Min<size_t>(sz, buf.Size())); - - if (lresult == 0) { + + while (sz) { + const size_t lresult = Read(buf.Data(), Min<size_t>(sz, buf.Size())); + + if (lresult == 0) { return total; - } - + } + total += lresult; sz -= lresult; - } + } return total; } @@ -148,115 +148,115 @@ size_t IInputStream::DoSkip(size_t sz) { TString IInputStream::ReadAll() { TString result; TStringOutput stream(result); - + DoReadAll(stream); return result; } - + ui64 IInputStream::ReadAll(IOutputStream& out) { return DoReadAll(out); -} - +} + ui64 TransferData(IInputStream* in, IOutputStream* out) { return in->ReadAll(*out); } -namespace { +namespace { struct TStdIn: public IInputStream { ~TStdIn() override = default; - + size_t DoRead(void* buf, size_t len) override { - const size_t ret = fread(buf, 1, len, F_); - - if (ret < len && ferror(F_)) { + const size_t ret = fread(buf, 1, len, F_); + + if (ret < len && ferror(F_)) { ythrow TSystemError() << "can not read from stdin"; - } - - return ret; - } - - FILE* F_ = stdin; - }; - -#if defined(_win_) - using TGetLine = TStdIn; -#else - #if defined(_bionic_) - using TGetLineBase = TStdIn; - #else - struct TGetLineBase: public TStdIn { - ~TGetLineBase() override { - free(B_); - } - + } + + return ret; + } + + FILE* F_ = stdin; + }; + +#if defined(_win_) + using TGetLine = TStdIn; +#else + #if defined(_bionic_) + using TGetLineBase = TStdIn; + #else + struct TGetLineBase: public TStdIn { + ~TGetLineBase() override { + free(B_); + } + size_t DoReadTo(TString& st, char ch) override { - auto&& guard = Guard(M_); - - (void)guard; - - const auto r = getdelim(&B_, &L_, ch, F_); - - if (r < 0) { - if (ferror(F_)) { - ythrow TSystemError() << "can not read from stdin"; + auto&& guard = Guard(M_); + + (void)guard; + + const auto r = getdelim(&B_, &L_, ch, F_); + + if (r < 0) { + if (ferror(F_)) { + ythrow TSystemError() << "can not read from stdin"; } - - st.clear(); - - return 0; + + st.clear(); + + return 0; } - - st.AssignNoAlias(B_, r); - + + st.AssignNoAlias(B_, r); + if (st && st.back() == ch) { - st.pop_back(); + st.pop_back(); } - - return r; + + return r; } - TAdaptiveLock M_; - char* B_ = nullptr; - size_t L_ = 0; - }; - #endif - - #if defined(_glibc_) || defined(_cygwin_) + TAdaptiveLock M_; + char* B_ = nullptr; + size_t L_ = 0; + }; + #endif + + #if defined(_glibc_) || defined(_cygwin_) // glibc does not have fgetln - using TGetLine = TGetLineBase; - #else - struct TGetLine: public TGetLineBase { + using TGetLine = TGetLineBase; + #else + struct TGetLine: public TGetLineBase { size_t DoReadTo(TString& st, char ch) override { - if (ch == '\n') { - size_t len = 0; - auto r = fgetln(F_, &len); - - if (r) { - st.AssignNoAlias(r, len); - - if (st && st.back() == '\n') { - st.pop_back(); - } - - return len; - } - } - - return TGetLineBase::DoReadTo(st, ch); - } - }; - #endif -#endif -} - + if (ch == '\n') { + size_t len = 0; + auto r = fgetln(F_, &len); + + if (r) { + st.AssignNoAlias(r, len); + + if (st && st.back() == '\n') { + st.pop_back(); + } + + return len; + } + } + + return TGetLineBase::DoReadTo(st, ch); + } + }; + #endif +#endif +} + IInputStream& NPrivate::StdInStream() noexcept { - return *SingletonWithPriority<TGetLine, 4>(); -} + return *SingletonWithPriority<TGetLine, 4>(); +} // implementation of >> operator -// helper functions +// helper functions static inline bool IsStdDelimiter(char c) { return (c == '\0') || (c == ' ') || (c == '\r') || (c == '\n') || (c == '\t'); @@ -295,13 +295,13 @@ void In<TUtf16String>(IInputStream& i, TUtf16String& w) { } } -// specialization for char types +// specialization for char types -#define SPEC_FOR_CHAR(T) \ - template <> \ +#define SPEC_FOR_CHAR(T) \ + template <> \ void In<T>(IInputStream & i, T & t) { \ - i.ReadChar((char&)t); \ - } + i.ReadChar((char&)t); \ + } SPEC_FOR_CHAR(char) SPEC_FOR_CHAR(unsigned char) @@ -311,22 +311,22 @@ SPEC_FOR_CHAR(signed char) // specialization for number types -#define SPEC_FOR_NUMBER(T) \ - template <> \ +#define SPEC_FOR_NUMBER(T) \ + template <> \ void In<T>(IInputStream & i, T & t) { \ - char buf[128]; \ - size_t pos = 0; \ - while (i.ReadChar(buf[0])) { \ - if (!IsStdDelimiter(buf[0])) { \ - ++pos; \ - break; \ - } \ - } \ - while (i.ReadChar(buf[pos]) && !IsStdDelimiter(buf[pos]) && pos < 127) { \ - ++pos; \ - } \ - t = FromString<T, char>(buf, pos); \ - } + char buf[128]; \ + size_t pos = 0; \ + while (i.ReadChar(buf[0])) { \ + if (!IsStdDelimiter(buf[0])) { \ + ++pos; \ + break; \ + } \ + } \ + while (i.ReadChar(buf[pos]) && !IsStdDelimiter(buf[pos]) && pos < 127) { \ + ++pos; \ + } \ + t = FromString<T, char>(buf, pos); \ + } SPEC_FOR_NUMBER(signed short) SPEC_FOR_NUMBER(signed int) |