aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/ipreg/writer.cpp
diff options
context:
space:
mode:
authorvvvv <vvvv@ydb.tech>2023-07-31 18:21:04 +0300
committervvvv <vvvv@ydb.tech>2023-07-31 18:21:04 +0300
commitdec41c40e51aa407edef81a3c566a5a15780fc49 (patch)
tree4f197b596b32f35eca368121f0dff913419da9af /library/cpp/ipreg/writer.cpp
parent3ca8b54c96e09eb2b65be7f09675623438d559c7 (diff)
downloadydb-dec41c40e51aa407edef81a3c566a5a15780fc49.tar.gz
YQL-16239 Move purecalc to public
Diffstat (limited to 'library/cpp/ipreg/writer.cpp')
-rw-r--r--library/cpp/ipreg/writer.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/library/cpp/ipreg/writer.cpp b/library/cpp/ipreg/writer.cpp
new file mode 100644
index 0000000000..89f8c8b629
--- /dev/null
+++ b/library/cpp/ipreg/writer.cpp
@@ -0,0 +1,91 @@
+#include "writer.h"
+
+#include <util/stream/file.h>
+
+namespace NIPREG {
+
+TWriter::TWriter(const TString& fname)
+ : OwnedStreamPtr(fname.empty() ? nullptr : new TFileOutput(fname))
+ , Stream(OwnedStreamPtr ? *OwnedStreamPtr.Get() : Cout)
+ , AddrSeparator(ADDR_SEP)
+ , DataSeparator(DATA_SEP)
+ , SplitMixed(false)
+{
+}
+
+TWriter::TWriter(IOutputStream& stream, EAddressFormat addressFormat, const TString& addrSep, const TString& dataSep, const bool splitMixed)
+ : Stream(stream)
+ , AddressFormat(addressFormat)
+ , AddrSeparator(addrSep)
+ , DataSeparator(dataSep)
+ , SplitMixed(splitMixed)
+{
+}
+
+namespace {
+ const TAddress IPv4Start = TAddress::ParseIPv4("0.0.0.0");
+ const TAddress IPv4End = TAddress::ParseIPv4("255.255.255.255");
+
+ const TAddress IPv6BeforeV4 = IPv4Start.Prev();
+ const TAddress IPv6AfterV4 = IPv4End.Next();
+}
+
+void TWriter::Write(const TAddress& first, const TAddress& last, const TString& data, bool printRange) {
+ if (SplitMixed) {
+ if (first < IPv4Start && IPv4Start < last) {
+ Write(first, IPv6BeforeV4, data, printRange);
+ Write(IPv4Start, last, data, printRange);
+ return;
+ }
+
+ if (first < IPv4End && IPv4End < last) {
+ Write(first, IPv4End, data, printRange);
+ Write(IPv6AfterV4, last, data, printRange);
+ return;
+ }
+ }
+ WriteImpl(first, last, data, printRange);
+}
+
+void TWriter::WriteImpl(const TAddress& first, const TAddress& last, const TString& data, bool printRange) {
+ if (printRange) {
+ Stream << first.Format(AddressFormat) << AddrSeparator << last.Format(AddressFormat);
+ }
+ if (!data.empty()) {
+ if (printRange) {
+ Stream << DataSeparator;
+ }
+ Stream << data;
+ }
+ if (!data.empty() || printRange) {
+ Stream << "\n";
+ }
+}
+
+void TWriter::Finalize() {
+}
+
+TMergingWriter::TMergingWriter(IOutputStream& stream, EAddressFormat addressFormat, const TString& addrSep, const TString& dataSep, const bool splitMixed)
+ : TWriter(stream, addressFormat, addrSep, dataSep, splitMixed) {
+}
+
+void TMergingWriter::Write(const TAddress& first, const TAddress& last, const TString& data, bool) {
+ if (Initialized && data == StoredData && first == StoredLast.Next()) {
+ StoredLast = last;
+ } else {
+ if (Initialized)
+ TWriter::Write(StoredFirst, StoredLast, StoredData);
+ StoredFirst = first;
+ StoredLast = last;
+ StoredData = data;
+ Initialized = true;
+ }
+}
+
+void TMergingWriter::Finalize() {
+ if (Initialized)
+ TWriter::Write(StoredFirst, StoredLast, StoredData);
+ Initialized = false;
+}
+
+} // NIPREG