diff options
author | finder <finder@yandex-team.ru> | 2022-02-10 16:49:24 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:24 +0300 |
commit | 2ab6bab688484680d3ebc5c4a7e9cd3ff04fda04 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library | |
parent | abbbaf4075fbaa0ff4ce9faa1188089466a21dbe (diff) | |
download | ydb-2ab6bab688484680d3ebc5c4a7e9cd3ff04fda04.tar.gz |
Restoring authorship annotation for <finder@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library')
-rw-r--r-- | library/cpp/binsaver/bin_saver.h | 64 | ||||
-rw-r--r-- | library/cpp/binsaver/class_factory.h | 2 | ||||
-rw-r--r-- | library/cpp/binsaver/util_stream_io.h | 66 | ||||
-rw-r--r-- | library/cpp/containers/2d_array/2d_array.h | 38 | ||||
-rw-r--r-- | library/cpp/containers/comptrie/comptrie_ut.cpp | 44 | ||||
-rw-r--r-- | library/cpp/json/easy_parse/json_easy_parser.cpp | 96 | ||||
-rw-r--r-- | library/cpp/json/easy_parse/json_easy_parser.h | 42 | ||||
-rw-r--r-- | library/cpp/json/easy_parse/json_easy_parser_impl.h | 38 | ||||
-rw-r--r-- | library/cpp/json/easy_parse/ya.make | 22 | ||||
-rw-r--r-- | library/cpp/json/writer/json.cpp | 4 | ||||
-rw-r--r-- | library/cpp/packers/region_packer.h | 44 | ||||
-rw-r--r-- | library/cpp/xml/document/libxml-guards.h | 58 | ||||
-rw-r--r-- | library/cpp/xml/document/node-attr.h | 174 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document-decl.h | 520 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document.cpp | 246 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document.h | 4 | ||||
-rw-r--r-- | library/cpp/xml/document/ya.make | 18 |
17 files changed, 740 insertions, 740 deletions
diff --git a/library/cpp/binsaver/bin_saver.h b/library/cpp/binsaver/bin_saver.h index c346e48ea0..412424889f 100644 --- a/library/cpp/binsaver/bin_saver.h +++ b/library/cpp/binsaver/bin_saver.h @@ -13,7 +13,7 @@ #include <util/generic/variant.h> #include <util/generic/ylimits.h> #include <util/memory/blob.h> -#include <util/digest/murmur.h> +#include <util/digest/murmur.h> #include <array> #include <bitset> @@ -186,25 +186,25 @@ private: template <class T> void DoAnySet(T& data) { - if (IsReading()) { - data.clear(); + if (IsReading()) { + data.clear(); TStoredSize nSize; Add(2, &nSize); for (TStoredSize i = 0; i < nSize; ++i) { typename T::value_type member; - Add(1, &member); - data.insert(member); - } - } else { + Add(1, &member); + data.insert(member); + } + } else { TStoredSize nSize = data.size(); CheckOverflow(nSize, data.size()); Add(2, &nSize); for (const auto& elem : data) { auto member = elem; - Add(1, &member); - } - } - } + Add(1, &member); + } + } + } // 2D array template <class T> @@ -240,19 +240,19 @@ private: TStoredSize nCount = data.size(); CheckOverflow(nCount, data.size()); File.Write(&nCount, sizeof(TStoredSize)); - File.Write(data.c_str(), nCount * elemSize); + File.Write(data.c_str(), nCount * elemSize); } } void DataChunkString(std::string& data) { - DataChunkStr(data, sizeof(char)); - } + DataChunkStr(data, sizeof(char)); + } void DataChunkStroka(TString& data) { DataChunkStr(data, sizeof(TString::char_type)); - } + } void DataChunkWtroka(TUtf16String& data) { DataChunkStr(data, sizeof(wchar16)); - } - + } + void DataChunk(void* pData, i64 nSize) { i64 chunkSize = 1 << 30; for (i64 offset = 0; offset < nSize; offset += chunkSize) { @@ -302,13 +302,13 @@ public: return 0; } int Add(const chunk_id, TString* pStr) { - DataChunkStroka(*pStr); + DataChunkStroka(*pStr); return 0; - } + } int Add(const chunk_id, TUtf16String* pStr) { - DataChunkWtroka(*pStr); + DataChunkWtroka(*pStr); return 0; - } + } int Add(const chunk_id, TBlob* blob) { if (bRead) { ui64 size = 0; @@ -367,7 +367,7 @@ public: int Add(const chunk_id, THashSet<T1, T2, T3, T4>* pHash) { DoAnySet(*pHash); return 0; - } + } template <class T1> int Add(const chunk_id, TArray2D<T1>* pArr) { @@ -524,17 +524,17 @@ public: return 0; } - template <typename TOne> + template <typename TOne> void AddMulti(TOne& one) { - Add(0, &one); - } - + Add(0, &one); + } + template <typename THead, typename... TTail> void AddMulti(THead& head, TTail&... tail) { - Add(0, &head); + Add(0, &head); AddMulti(tail...); - } - + } + template <class T, typename = decltype(std::declval<T&>() & std::declval<IBinSaver&>())> static bool HasNonTrivialSerializer(ui32) { return true; @@ -580,8 +580,8 @@ struct TRegisterSaveLoadType { #define Y_BINSAVER_REGISTER(name) \ BASIC_REGISTER_CLASS(name) \ - static TRegisterSaveLoadType<name> init##name(MurmurHash<int>(#name, sizeof(#name))); - + static TRegisterSaveLoadType<name> init##name(MurmurHash<int>(#name, sizeof(#name))); + #define REGISTER_SAVELOAD_CLASS(N, name) \ BASIC_REGISTER_CLASS(name) \ static TRegisterSaveLoadType<name> init##name##N(N); @@ -618,7 +618,7 @@ struct TRegisterSaveLoadType { #define REGISTER_SAVELOAD_CLASS_NS_PREF(N, cls, ns, pref) \ REGISTER_SAVELOAD_CLASS_NAME(N, ns ::cls, _##pref##_##cls) - + #define SAVELOAD(...) \ int operator&(IBinSaver& f) { \ f.AddMulti(__VA_ARGS__); \ diff --git a/library/cpp/binsaver/class_factory.h b/library/cpp/binsaver/class_factory.h index 90288612e1..e83512331b 100644 --- a/library/cpp/binsaver/class_factory.h +++ b/library/cpp/binsaver/class_factory.h @@ -93,7 +93,7 @@ void TClassFactory<T>::RegisterTypeBase(int nTypeID, newFunc func, VFT vft) { CTypeIndexHash::iterator typeIndexIt = typeIndex.find(vft); if (typeIndexIt != typeIndex.end() && nTypeID != typeIndexIt->second) { fprintf(stderr, "IBinSaver: class (Type ID 0x%08X) has been already registered (Type ID 0x%08X)\n", nTypeID, typeIndexIt->second); - abort(); + abort(); } typeIndex[vft] = nTypeID; typeInfo[nTypeID] = func; diff --git a/library/cpp/binsaver/util_stream_io.h b/library/cpp/binsaver/util_stream_io.h index b486659cb8..d65d630b93 100644 --- a/library/cpp/binsaver/util_stream_io.h +++ b/library/cpp/binsaver/util_stream_io.h @@ -1,21 +1,21 @@ -#pragma once +#pragma once #include "bin_saver.h" - -#include <util/stream/input.h> -#include <util/stream/output.h> + +#include <util/stream/input.h> +#include <util/stream/output.h> #include <util/stream/file.h> - + class TYaStreamInput: public IBinaryStream { IInputStream& Stream; int WriteImpl(const void*, int) override { Y_ASSERT(0); - return 0; - } + return 0; + } int ReadImpl(void* userBuffer, int size) override { return (int)Stream.Read(userBuffer, (size_t)size); - } + } bool IsValid() const override { return true; } @@ -23,22 +23,22 @@ class TYaStreamInput: public IBinaryStream { return false; } -public: +public: TYaStreamInput(IInputStream& stream) : Stream(stream) { - } -}; - + } +}; + template <class T> inline void SerializeFromStream(IInputStream& stream, T& c) { - TYaStreamInput f(stream); - { - IBinSaver bs(f, true); - bs.Add(1, &c); - } -} - + TYaStreamInput f(stream); + { + IBinSaver bs(f, true); + bs.Add(1, &c); + } +} + template <class T> inline void SerializeFromFile(const TString& fileName, T& c) { TIFStream in(fileName); @@ -50,12 +50,12 @@ class TYaStreamOutput: public IBinaryStream { int WriteImpl(const void* what, int size) override { Stream.Write(what, (size_t)size); - return size; - } + return size; + } int ReadImpl(void*, int) override { Y_ASSERT(0); - return 0; - } + return 0; + } bool IsValid() const override { return true; } @@ -63,21 +63,21 @@ class TYaStreamOutput: public IBinaryStream { return false; } -public: +public: TYaStreamOutput(IOutputStream& stream) : Stream(stream) { - } -}; - + } +}; + template <class T> inline void SerializeToArcadiaStream(IOutputStream& stream, T& c) { - TYaStreamOutput f(stream); - { - IBinSaver bs(f, false); - bs.Add(1, &c); - } -} + TYaStreamOutput f(stream); + { + IBinSaver bs(f, false); + bs.Add(1, &c); + } +} template <class T> inline void SerializeToFile(const TString& fileName, T& c) { diff --git a/library/cpp/containers/2d_array/2d_array.h b/library/cpp/containers/2d_array/2d_array.h index d5ca68d8f0..9e24650637 100644 --- a/library/cpp/containers/2d_array/2d_array.h +++ b/library/cpp/containers/2d_array/2d_array.h @@ -1,8 +1,8 @@ -#pragma once +#pragma once #include <util/system/yassert.h> #include <util/generic/algorithm.h> - + #ifdef _DEBUG template <class T> struct TBoundCheck { @@ -16,10 +16,10 @@ struct TBoundCheck { Y_ASSERT(i >= 0 && i < Size); return Data[i]; } -}; +}; #endif - -template <class T> + +template <class T> class TArray2D { private: typedef T* PT; @@ -47,7 +47,7 @@ private: PData[i] = Data + i * XSize; } -public: +public: TArray2D(size_t xsize = 1, size_t ysize = 1) { XSize = xsize; YSize = ysize; @@ -75,17 +75,17 @@ public: void Clear() { SetSizes(1, 1); } -#ifdef _DEBUG +#ifdef _DEBUG TBoundCheck<T> operator[](size_t i) const { Y_ASSERT(i < YSize); return TBoundCheck<T>(PData[i], XSize); } -#else +#else T* operator[](size_t i) const { Y_ASSERT(i < YSize); return PData[i]; } -#endif +#endif size_t GetXSize() const { return XSize; } @@ -105,20 +105,20 @@ public: std::swap(XSize, a.XSize); std::swap(YSize, a.YSize); } -}; - +}; + template <class T> inline bool operator==(const TArray2D<T>& a, const TArray2D<T>& b) { - if (a.GetXSize() != b.GetXSize() || a.GetYSize() != b.GetYSize()) - return false; + if (a.GetXSize() != b.GetXSize() || a.GetYSize() != b.GetYSize()) + return false; for (size_t y = 0; y < a.GetYSize(); ++y) { for (size_t x = 0; x < a.GetXSize(); ++x) - if (a[y][x] != b[y][x]) - return false; - } - return true; -} - + if (a[y][x] != b[y][x]) + return false; + } + return true; +} + template <class T> inline bool operator!=(const TArray2D<T>& a, const TArray2D<T>& b) { return !(a == b); diff --git a/library/cpp/containers/comptrie/comptrie_ut.cpp b/library/cpp/containers/comptrie/comptrie_ut.cpp index 0b95128191..74bee09b5d 100644 --- a/library/cpp/containers/comptrie/comptrie_ut.cpp +++ b/library/cpp/containers/comptrie/comptrie_ut.cpp @@ -70,9 +70,9 @@ private: UNIT_TEST(TestUnique); UNIT_TEST(TestAddRetValue); UNIT_TEST(TestClear); - + UNIT_TEST(TestIterateEmptyKey); - + UNIT_TEST(TestTrieSet); UNIT_TEST(TestTrieForVectorInt64); @@ -207,8 +207,8 @@ public: void TestUnique(); void TestAddRetValue(); void TestClear(); - - void TestIterateEmptyKey(); + + void TestIterateEmptyKey(); void TestTrieSet(); @@ -1042,24 +1042,24 @@ public: return RandomNumber<std::make_unsigned_t<T>>(); } }; - -void TCompactTrieTest::TestIterateEmptyKey() { - TBuffer trieBuffer; - { - TCompactTrieBuilder<char, ui32> builder; - UNIT_ASSERT(builder.Add("", 1)); - TBufferStream trieBufferO(trieBuffer); - builder.Save(trieBufferO); - } - TCompactTrie<char, ui32> trie(TBlob::FromBuffer(trieBuffer)); - ui32 val; - UNIT_ASSERT(trie.Find("", &val)); - UNIT_ASSERT(val == 1); - TCompactTrie<char, ui32>::TConstIterator it = trie.Begin(); - UNIT_ASSERT(it.GetKey().empty()); - UNIT_ASSERT(it.GetValue() == 1); -} - + +void TCompactTrieTest::TestIterateEmptyKey() { + TBuffer trieBuffer; + { + TCompactTrieBuilder<char, ui32> builder; + UNIT_ASSERT(builder.Add("", 1)); + TBufferStream trieBufferO(trieBuffer); + builder.Save(trieBufferO); + } + TCompactTrie<char, ui32> trie(TBlob::FromBuffer(trieBuffer)); + ui32 val; + UNIT_ASSERT(trie.Find("", &val)); + UNIT_ASSERT(val == 1); + TCompactTrie<char, ui32>::TConstIterator it = trie.Begin(); + UNIT_ASSERT(it.GetKey().empty()); + UNIT_ASSERT(it.GetValue() == 1); +} + void TCompactTrieTest::TestTrieSet() { TBuffer buffer; { diff --git a/library/cpp/json/easy_parse/json_easy_parser.cpp b/library/cpp/json/easy_parse/json_easy_parser.cpp index 95eb59c744..3c781f544b 100644 --- a/library/cpp/json/easy_parse/json_easy_parser.cpp +++ b/library/cpp/json/easy_parse/json_easy_parser.cpp @@ -1,14 +1,14 @@ -#include "json_easy_parser.h" +#include "json_easy_parser.h" #include <library/cpp/json/json_reader.h> #include <util/string/cast.h> -#include <util/string/split.h> -#include <util/string/strip.h> - -namespace NJson { +#include <util/string/split.h> +#include <util/string/strip.h> + +namespace NJson { static TString MAP_IDENTIFIER = "{}"; static TString ARRAY_IDENTIFIER = "[]"; static TString ANY_IDENTIFIER = "*"; - + static void ParsePath(TString path, TVector<TPathElem>* res) { TVector<const char*> parts; Split(path.begin(), '/', &parts); @@ -26,24 +26,24 @@ namespace NJson { arrayCounter = -1; } res->push_back(TPathElem(arrayCounter)); - } - } - } - } - + } + } + } + } + void TJsonParser::AddField(const TString& path, bool nonEmpty) { Fields.emplace_back(); Fields.back().NonEmpty = nonEmpty; ParsePath(path, &Fields.back().Path); } - + TString TJsonParser::ConvertToTabDelimited(const TString& json) const { TStringInput in(json); TStringStream out; ConvertToTabDelimited(in, out); return out.Str(); } - + class TRewriteJsonImpl: public NJson::TJsonCallbacks { const TJsonParser& Parent; TVector<TString> FieldValues; @@ -51,7 +51,7 @@ namespace NJson { bool ShouldUpdateOnArrayChange; int CurrentFieldIdx; bool HasFormatError; - + private: static bool PathElementMatch(const TPathElem& templ, const TPathElem& real) { if (templ.Type != real.Type) @@ -62,31 +62,31 @@ namespace NJson { return templ.Key == ANY_IDENTIFIER || templ.Key == real.Key; return true; } - + bool CheckFilter(const TVector<TPathElem>& path) const { if (Stack.size() < path.size()) - return false; + return false; for (size_t n = 0; n < path.size(); ++n) { if (!PathElementMatch(path[n], Stack[n])) return false; } return true; - } - + } + void UpdateRule() { for (size_t n = 0; n < Parent.Fields.size(); ++n) { if (FieldValues[n].empty() && CheckFilter(Parent.Fields[n].Path)) { CurrentFieldIdx = n; return; } - } + } CurrentFieldIdx = -1; - } - + } + void Pop() { Stack.pop_back(); } - + void IncreaseArrayCounter() { if (!Stack.empty() && Stack.back().Type == NImpl::ARRAY) { ++Stack.back().ArrayCounter; @@ -100,11 +100,11 @@ namespace NJson { IncreaseArrayCounter(); if (CurrentFieldIdx >= 0) { FieldValues[CurrentFieldIdx] = ToString(val); - UpdateRule(); + UpdateRule(); } return true; - } - + } + public: TRewriteJsonImpl(const TJsonParser& parent) : Parent(parent) @@ -117,8 +117,8 @@ namespace NJson { if (!Parent.Fields[n].Path.empty() && Parent.Fields[n].Path.back().Type == NImpl::ARRAY) ShouldUpdateOnArrayChange = true; } - } - + } + bool OnOpenMap() override { IncreaseArrayCounter(); Stack.push_back(TPathElem(NImpl::MAP)); @@ -127,8 +127,8 @@ namespace NJson { else UpdateRule(); return true; - } - + } + bool OnOpenArray() override { IncreaseArrayCounter(); Stack.push_back(TPathElem(-1)); @@ -144,17 +144,17 @@ namespace NJson { Pop(); if (!Stack.empty()) Pop(); - UpdateRule(); + UpdateRule(); return true; } - + bool OnCloseArray() override { if (!Stack.empty()) Pop(); - UpdateRule(); + UpdateRule(); return true; } - + bool OnMapKey(const TStringBuf& key) override { if (!Stack.empty() && Stack.back().Type == NImpl::MAP_KEY) { Pop(); @@ -167,23 +167,23 @@ namespace NJson { UpdateRule(); return true; } - + bool OnBoolean(bool b) override { return OnValue(b); } - + bool OnInteger(long long i) override { return OnValue(i); - } - + } + bool OnDouble(double f) override { return OnValue(f); } - + bool OnString(const TStringBuf& str) override { return OnValue(str); } - + bool IsOK() const { if (HasFormatError) return false; @@ -192,17 +192,17 @@ namespace NJson { return false; return true; } - + void WriteTo(IOutputStream& out) const { for (size_t n = 0; n < FieldValues.size(); ++n) out << "\t" << FieldValues[n]; } - + void WriteTo(TVector<TString>* res) const { *res = FieldValues; } }; - + void TJsonParser::ConvertToTabDelimited(IInputStream& in, IOutputStream& out) const { TRewriteJsonImpl impl(*this); ReadJson(&in, &impl); @@ -211,8 +211,8 @@ namespace NJson { impl.WriteTo(out); out.Flush(); } - } - + } + bool TJsonParser::Parse(const TString& json, TVector<TString>* res) const { TRewriteJsonImpl impl(*this); TStringInput in(json); @@ -222,8 +222,8 @@ namespace NJson { return true; } else return false; - } - + } + //struct TTestMe { // TTestMe() { // TJsonParser worker; @@ -232,5 +232,5 @@ namespace NJson { // TString ret2 = worker.ConvertToTabDelimited(" [1, 2, 3, 4, 5] "); // } //} testMe; - -} + +} diff --git a/library/cpp/json/easy_parse/json_easy_parser.h b/library/cpp/json/easy_parse/json_easy_parser.h index 64c483aaba..59d7791ab1 100644 --- a/library/cpp/json/easy_parse/json_easy_parser.h +++ b/library/cpp/json/easy_parse/json_easy_parser.h @@ -1,12 +1,12 @@ -#pragma once +#pragma once #include <util/generic/string.h> -#include <util/generic/vector.h> -#include <util/stream/input.h> -#include <util/stream/output.h> -#include "json_easy_parser_impl.h" - -namespace NJson { +#include <util/generic/vector.h> +#include <util/stream/input.h> +#include <util/stream/output.h> +#include "json_easy_parser_impl.h" + +namespace NJson { /* This class filters out nodes from a source JSON by a xpath-style description. It represent these nodes as a tab-delimited string (or a vector). * It is useful if you need to parse a data which comes into JSON in a known and fixed format. * Fields are set as a list of keys separated by slash, for example: @@ -21,26 +21,26 @@ namespace NJson { * NB! Library can not extract values of not a simple type (namely it doesn't support the case when a result is a vocabulary or an array) from JSON. * If you expect such a case, please check json_value.h. */ - - class TJsonParser { + + class TJsonParser { TString Prefix; - - struct TField { + + struct TField { TVector<TPathElem> Path; - bool NonEmpty; - }; + bool NonEmpty; + }; TVector<TField> Fields; - - friend class TRewriteJsonImpl; - + + friend class TRewriteJsonImpl; + void ConvertToTabDelimited(IInputStream& in, IOutputStream& out) const; - public: + public: void SetPrefix(const TString& prefix) { - Prefix = prefix; - } + Prefix = prefix; + } void AddField(const TString& path, bool mustExist); TString ConvertToTabDelimited(const TString& json) const; bool Parse(const TString& json, TVector<TString>* res) const; - }; -} + }; +} diff --git a/library/cpp/json/easy_parse/json_easy_parser_impl.h b/library/cpp/json/easy_parse/json_easy_parser_impl.h index 11941fb6d0..ec55d838b3 100644 --- a/library/cpp/json/easy_parse/json_easy_parser_impl.h +++ b/library/cpp/json/easy_parse/json_easy_parser_impl.h @@ -1,40 +1,40 @@ -#pragma once - +#pragma once + #include <util/generic/string.h> -namespace NJson { +namespace NJson { namespace NImpl { - enum EType { - ARRAY, - MAP, - MAP_KEY - }; + enum EType { + ARRAY, + MAP, + MAP_KEY + }; } template <class TStringType> struct TPathElemImpl { NImpl::EType Type; TStringType Key; - int ArrayCounter; - + int ArrayCounter; + TPathElemImpl(NImpl::EType type) : Type(type) , ArrayCounter() { - } - + } + TPathElemImpl(const TStringType& key) : Type(NImpl::MAP_KEY) , Key(key) , ArrayCounter() { - } - - TPathElemImpl(int arrayCounter) + } + + TPathElemImpl(int arrayCounter) : Type(NImpl::ARRAY) , ArrayCounter(arrayCounter) { - } - }; - + } + }; + typedef TPathElemImpl<TString> TPathElem; -} +} diff --git a/library/cpp/json/easy_parse/ya.make b/library/cpp/json/easy_parse/ya.make index d84feb7ff6..2304c542f2 100644 --- a/library/cpp/json/easy_parse/ya.make +++ b/library/cpp/json/easy_parse/ya.make @@ -1,13 +1,13 @@ -OWNER(finder) - -LIBRARY() - +OWNER(finder) + +LIBRARY() + SRCS( - json_easy_parser.cpp -) - -PEERDIR( + json_easy_parser.cpp +) + +PEERDIR( library/cpp/json -) - -END() +) + +END() diff --git a/library/cpp/json/writer/json.cpp b/library/cpp/json/writer/json.cpp index 7425b3b4bd..02370c2d79 100644 --- a/library/cpp/json/writer/json.cpp +++ b/library/cpp/json/writer/json.cpp @@ -2,7 +2,7 @@ #include <library/cpp/json/json_value.h> -#include <util/string/cast.h> +#include <util/string/cast.h> #include <util/string/strspn.h> #include <util/generic/algorithm.h> #include <util/generic/ymath.h> @@ -263,7 +263,7 @@ namespace NJsonWriter { UnsafeWriteValue(buf, len); return TValueContext(*this); } - + TValueContext TBuf::WriteFloat(float f, EFloatToStringMode mode, int ndigits) { return WriteFloatImpl(f, mode, ndigits); } diff --git a/library/cpp/packers/region_packer.h b/library/cpp/packers/region_packer.h index 682b55db7e..2c661cb5bc 100644 --- a/library/cpp/packers/region_packer.h +++ b/library/cpp/packers/region_packer.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include "packers.h" @@ -7,36 +7,36 @@ // Stores an array of PODs in the trie (copying them with memcpy). // Byte order and alignment are your problem. -template <class TRecord> -class TRegionPacker { -public: +template <class TRecord> +class TRegionPacker { +public: typedef TArrayRef<TRecord> TRecords; - + void UnpackLeaf(const char* p, TRecords& result) const { - size_t len; + size_t len; NPackers::TIntegralPacker<size_t>().UnpackLeaf(p, len); size_t start = NPackers::TIntegralPacker<size_t>().SkipLeaf(p); - result = TRecords((TRecord*)(p + start), len); - } - - void PackLeaf(char* buf, const TRecords& data, size_t computedSize) const { + result = TRecords((TRecord*)(p + start), len); + } + + void PackLeaf(char* buf, const TRecords& data, size_t computedSize) const { size_t len = data.size(); - size_t lenChar = len * sizeof(TRecord); - size_t start = computedSize - lenChar; + size_t lenChar = len * sizeof(TRecord); + size_t start = computedSize - lenChar; NPackers::TIntegralPacker<size_t>().PackLeaf(buf, len, NPackers::TIntegralPacker<size_t>().MeasureLeaf(len)); memcpy(buf + start, data.data(), lenChar); - } - - size_t MeasureLeaf(const TRecords& data) const { + } + + size_t MeasureLeaf(const TRecords& data) const { size_t len = data.size(); return NPackers::TIntegralPacker<size_t>().MeasureLeaf(len) + len * sizeof(TRecord); - } - + } + size_t SkipLeaf(const char* p) const { size_t result = NPackers::TIntegralPacker<size_t>().SkipLeaf(p); - size_t len; + size_t len; NPackers::TIntegralPacker<size_t>().UnpackLeaf(p, len); - result += len * sizeof(TRecord); - return result; - } -}; + result += len * sizeof(TRecord); + return result; + } +}; diff --git a/library/cpp/xml/document/libxml-guards.h b/library/cpp/xml/document/libxml-guards.h index 4028c53e9f..4188cecff1 100644 --- a/library/cpp/xml/document/libxml-guards.h +++ b/library/cpp/xml/document/libxml-guards.h @@ -1,50 +1,50 @@ -#pragma once +#pragma once #include <library/cpp/xml/init/ptr.h> -#include <util/generic/ptr.h> +#include <util/generic/ptr.h> #include <libxml/xmlstring.h> #include <libxml/tree.h> #include <libxml/xpath.h> #include <libxml/uri.h> #include <libxml/xmlsave.h> - -namespace NXml { - namespace NDetail { - struct TSignedCharPtrTraits { - static void Destroy(char* handle) { - xmlFree(handle); - } - }; - - struct TCharPtrTraits { - static void Destroy(xmlChar* handle) { - xmlFree(handle); - } - }; - - struct TOutputBufferPtrTraits { - static void Destroy(xmlOutputBufferPtr handle) { - xmlOutputBufferClose(handle); - } - }; - + +namespace NXml { + namespace NDetail { + struct TSignedCharPtrTraits { + static void Destroy(char* handle) { + xmlFree(handle); + } + }; + + struct TCharPtrTraits { + static void Destroy(xmlChar* handle) { + xmlFree(handle); + } + }; + + struct TOutputBufferPtrTraits { + static void Destroy(xmlOutputBufferPtr handle) { + xmlOutputBufferClose(handle); + } + }; + struct TSaveCtxtPtrTraits { static void Destroy(xmlSaveCtxtPtr handle) { xmlSaveClose(handle); } }; - } - + } + typedef TxmlXPathContextPtr TXPathContextPtr; typedef TxmlXPathObjectPtr TXPathObjectPtr; - typedef TAutoPtr<char, NDetail::TSignedCharPtrTraits> TSignedCharPtr; - typedef TAutoPtr<xmlChar, NDetail::TCharPtrTraits> TCharPtr; + typedef TAutoPtr<char, NDetail::TSignedCharPtrTraits> TSignedCharPtr; + typedef TAutoPtr<xmlChar, NDetail::TCharPtrTraits> TCharPtr; typedef TxmlDocHolder TDocHolder; typedef TxmlURIPtr TURIPtr; typedef TxmlNodePtr TNodePtr; - typedef TAutoPtr<xmlOutputBuffer, NDetail::TOutputBufferPtrTraits> TOutputBufferPtr; + typedef TAutoPtr<xmlOutputBuffer, NDetail::TOutputBufferPtrTraits> TOutputBufferPtr; typedef TxmlParserCtxtPtr TParserCtxtPtr; typedef TAutoPtr<xmlSaveCtxt, NDetail::TSaveCtxtPtrTraits> TSaveCtxtPtr; - + } diff --git a/library/cpp/xml/document/node-attr.h b/library/cpp/xml/document/node-attr.h index 1c4082e6fe..6e74403943 100644 --- a/library/cpp/xml/document/node-attr.h +++ b/library/cpp/xml/document/node-attr.h @@ -1,13 +1,13 @@ -#pragma once - +#pragma once + #include "xml-document-decl.h" #include "libxml-guards.h" -#include <util/stream/str.h> +#include <util/stream/str.h> #include <util/string/cast.h> - -namespace NXml { + +namespace NXml { #define THROW(x, y) ythrow yexception() << #x << ": " << y - + // libxml defines unsigned char -> xmlChar, // and all functions use xmlChar. inline static const char* CAST2CHAR(const xmlChar* x) { @@ -17,123 +17,123 @@ namespace NXml { return reinterpret_cast<const xmlChar*>(x); } - template <class T> + template <class T> void TNode::AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const { - try { - res = FromString<T>(CAST2CHAR(value.Get())); + try { + res = FromString<T>(CAST2CHAR(value.Get())); } catch (TFromStringException&) { THROW(XmlException, "Failed to convert string " << TString{TStringBuf(CAST2CHAR(value.Get())).substr(0, 50)}.Quote() << " from '" << errContext << "' to requested type"); - } - } - - template <> + } + } + + template <> inline void TNode::AttrInternal(TCharPtr& value, TString& res, TStringBuf /*errContext*/) const { TString tmp(CAST2CHAR(value.Get())); - res.swap(tmp); - } - - template <class T> + res.swap(tmp); + } + + template <class T> T TNode::Attr(TZtStringBuf name) const { TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!value) { THROW(AttributeNotFound, Path() << "@" << name); - } - - T t; + } + + T t; AttrInternal(value, t, name); - return t; - } - - template <class T> + return t; + } + + template <class T> T TNode::Attr(TZtStringBuf name, const T& defvalue) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { - return defvalue; - } - - T t; + return defvalue; + } + + T t; AttrInternal(attr, t, name); - return t; - } - - template <class T> + return t; + } + + template <class T> void TNode::Attr(TZtStringBuf name, T& value) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { THROW(AttributeNotFound, Path() << name); - } - + } + AttrInternal(attr, value, name); - } - - template <class T> + } + + template <class T> void TNode::Attr(TZtStringBuf name, T& value, const T& defvalue) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); - + if (!attr) { - value = defvalue; + value = defvalue; } else { AttrInternal(attr, value, name); - } - } - - template <class T> + } + } + + template <class T> T TNode::Value() const { if (!NodePointer || xmlIsBlankNode(NodePointer)) { THROW(NodeIsBlank, Path()); - } - + } + TCharPtr val(xmlNodeGetContent(NodePointer)); - T t; + T t; AttrInternal(val, t, this->Name()); - return t; - } - - template <class T> + return t; + } + + template <class T> T TNode::Value(const T& defvalue) const { if (!NodePointer || xmlIsBlankNode(NodePointer)) { - return defvalue; - } - + return defvalue; + } + TCharPtr val(xmlNodeGetContent(NodePointer)); - T t; + T t; AttrInternal(val, t, this->Name()); - return t; - } - - template <class T> + return t; + } + + template <class T> typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type TNode::SetValue(const T& value) { - TStringStream ss; - ss << value; + TStringStream ss; + ss << value; SetValue(ss.Str()); } inline void TNode::SetValue(TStringBuf value) { - xmlNodeSetContent(NodePointer, XMLCHAR("")); + xmlNodeSetContent(NodePointer, XMLCHAR("")); xmlNodeAddContentLen(NodePointer, XMLCHAR(value.data()), value.Size()); - } - + } + inline void TNode::SetAttr(TZtStringBuf name, TZtStringBuf value) { xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), XMLCHAR(value.c_str())); - + if (!attr) { THROW(XmlException, "Can't set node attribute <" << name << "> to <" << value << ">"); - } - } - - template <class T> + } + } + + template <class T> typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type TNode::SetAttr(TZtStringBuf name, const T& value) { - TStringStream ss; - ss << value; + TStringStream ss; + ss << value; SetAttr(name, TZtStringBuf(ss.Str())); - } - + } + inline void TNode::SetAttr(TZtStringBuf name) { xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), nullptr); @@ -151,37 +151,37 @@ namespace NXml { << ">"); } - template <class T> + template <class T> typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type TNode::AddChild(TZtStringBuf name, const T& value) { - TStringStream ss; - ss << value; + TStringStream ss; + ss << value; return AddChild(name, TZtStringBuf(ss.Str())); - } - + } + inline TNode TNode::AddChild(TZtStringBuf name, TZtStringBuf value) { if (IsNull()) { THROW(XmlException, "addChild [name=" << name << ", value=" << value << "]: can't add child to null node"); - } - + } + xmlNode* child = nullptr; - + if (value.empty()) { child = xmlNewTextChild(NodePointer, nullptr, XMLCHAR(name.c_str()), nullptr); } else { - child = xmlNewTextChild( + child = xmlNewTextChild( NodePointer, nullptr, XMLCHAR(name.c_str()), XMLCHAR(value.c_str())); - } - + } + if (!child) { THROW(XmlException, "addChild [name=" << name << ", value=" << value << "]: xmlNewTextChild returned NULL"); - } - + } + return TNode(DocPointer, child); - } - + } + template <class T> typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type TNode::AddText(const T& value) { diff --git a/library/cpp/xml/document/xml-document-decl.h b/library/cpp/xml/document/xml-document-decl.h index dbc289ee04..bfda1fb7e6 100644 --- a/library/cpp/xml/document/xml-document-decl.h +++ b/library/cpp/xml/document/xml-document-decl.h @@ -1,52 +1,52 @@ -#pragma once +#pragma once #include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> #include <util/generic/string.h> -#include <util/generic/vector.h> +#include <util/generic/vector.h> #include <util/stream/output.h> #include <util/stream/str.h> -#include <algorithm> +#include <algorithm> #include "libxml-guards.h" - -namespace NXml { + +namespace NXml { class TNode; - - class TConstNodes; + + class TConstNodes; class TConstNode; - + using TXPathContext = xmlXPathContext; class TDocument { - public: + public: enum Source { - File, - String, - RootName, - }; + File, + String, + RootName, + }; - public: - /** + public: + /** * create TDocument * @param source: filename, XML string, or name for the root element (depends on @src) - * @param src: source type: File | String | RootName - * throws if file not found or cannot be parsed - */ + * @param src: source type: File | String | RootName + * throws if file not found or cannot be parsed + */ TDocument(const TString& source, Source type = File); - - public: + + public: TDocument(const TDocument& that) = delete; TDocument& operator=(const TDocument& that) = delete; - + TDocument(TDocument&& that); TDocument& operator=(TDocument&& that); - - /** - * get root element - */ + + /** + * get root element + */ TNode Root(); TConstNode Root() const; - + void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = true) const { int bufferSize = 0; xmlChar* xmlBuff = nullptr; @@ -55,7 +55,7 @@ namespace NXml { TCharPtr xmlCharBuffPtr(xmlBuff); stream.Write(xmlBuff, bufferSize); } - + TString ToString(TZtStringBuf enc = "", bool shouldFormat = true) const { TStringStream s; Save(s, enc, shouldFormat); @@ -64,65 +64,65 @@ namespace NXml { void Swap(TDocument& that) { std::swap(this->Doc, that.Doc); - } - + } + xmlDocPtr GetImpl() { return Doc.Get(); } - private: + private: void ParseFile(const TString& file); void ParseString(TZtStringBuf xml); - + TDocument(TDocHolder doc) : Doc(std::move(doc)) { } - + TDocHolder Doc; - }; - - struct TNamespaceForXPath { + }; + + struct TNamespaceForXPath { TString Prefix; TString Url; - }; + }; typedef TVector<TNamespaceForXPath> TNamespacesForXPath; - + class TConstNodes { - private: + private: struct TConstNodesRef { explicit TConstNodesRef(TConstNodes& n) : r_(n) { } - TConstNodes& r_; - }; + TConstNodes& r_; + }; - public: + public: TConstNodes(const TConstNodes& nodes); TConstNodes& operator=(const TConstNodes& nodes); - - TConstNodes(TConstNodesRef ref); + + TConstNodes(TConstNodesRef ref); TConstNodes& operator=(TConstNodesRef ref); - - operator TConstNodesRef(); - - /** - * get node by id - * @param number: node id - */ + + operator TConstNodesRef(); + + /** + * get node by id + * @param number: node id + */ TConstNode operator[](size_t number) const; - - /** - * get number of nodes - */ + + /** + * get number of nodes + */ size_t Size() const { return SizeValue; } size_t size() const { return SizeValue; } - + struct TNodeIter { const TConstNodes& Nodes; size_t Index; @@ -145,30 +145,30 @@ namespace NXml { return TNodeIter{*this, size()}; } - private: - friend class TDocument; + private: + friend class TDocument; friend class TConstNode; friend class TNode; - + TConstNodes(xmlDoc* doc, TXPathObjectPtr obj); - + size_t SizeValue; xmlDoc* Doc; TXPathObjectPtr Obj; - }; - + }; + class TNode { - public: - friend class TDocument; + public: + friend class TDocument; friend class TConstNode; friend class TTextReader; - - /** - * check if node is null - */ + + /** + * check if node is null + */ bool IsNull() const; - - /** + + /** * check if node is element node */ bool IsElementNode() const; @@ -187,16 +187,16 @@ namespace NXml { /** * get all element nodes matching given xpath expression - * @param xpath: xpath expression - * @param quiet: don't throw exception if zero nodes found - * @param ns: explicitly specify XML namespaces to use and their prefixes + * @param xpath: xpath expression + * @param quiet: don't throw exception if zero nodes found + * @param ns: explicitly specify XML namespaces to use and their prefixes * * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. - */ + */ TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; - - /** + + /** * get all element nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found @@ -225,19 +225,19 @@ namespace NXml { /** * get the first element node matching given xpath expression - * @param xpath: path to node (from current node) - * @param quiet: don't throw exception if node not found, + * @param xpath: path to node (from current node) + * @param quiet: don't throw exception if node not found, * return null node (@see IsNull()) - * @param ns: explicitly specify XML namespaces to use and their prefixes + * @param ns: explicitly specify XML namespaces to use and their prefixes * * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. - */ + */ /// @todo: quiet should be default, empty nodeset is not an error TNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()); TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; - - /** + + /** * get the first element node matching given xpath expression * @param xpath: path to node (from current node) * @param quiet: don't throw exception if node not found, @@ -251,18 +251,18 @@ namespace NXml { TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; /** - * get node first child - * @param name: child name + * get node first child + * @param name: child name * @note if name is empty, returns the first child node of type "element" * @note returns null node if no child found - */ + */ TNode FirstChild(TZtStringBuf name); TConstNode FirstChild(TZtStringBuf name) const; - + TNode FirstChild(); TConstNode FirstChild() const; - /** + /** * get parent node * throws exception if has no parent */ @@ -270,36 +270,36 @@ namespace NXml { TConstNode Parent() const; /** - * get node neighbour - * @param name: neighbour name + * get node neighbour + * @param name: neighbour name * @note if name is empty, returns the next sibling node of type "element" * @node returns null node if no neighbour found - */ + */ TNode NextSibling(TZtStringBuf name); TConstNode NextSibling(TZtStringBuf name) const; - + TNode NextSibling(); TConstNode NextSibling() const; - /** - * create child node - * @param name: child name - * returns new empty node - */ + /** + * create child node + * @param name: child name + * returns new empty node + */ TNode AddChild(TZtStringBuf name); - - /** - * create child node with given value - * @param name: child name - * @param value: node value - */ - template <class T> + + /** + * create child node with given value + * @param name: child name + * @param value: node value + */ + template <class T> typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type AddChild(TZtStringBuf name, const T& value); - + TNode AddChild(TZtStringBuf name, TZtStringBuf value); - /** + /** * add child node, making recursive copy of original * @param node: node to copy from * returns added node @@ -318,80 +318,80 @@ namespace NXml { TNode AddText(TStringBuf value); /** - * get node attribute - * @param name: attribute name - * throws exception if attribute not found - */ - template <class T> + * get node attribute + * @param name: attribute name + * throws exception if attribute not found + */ + template <class T> T Attr(TZtStringBuf name) const; - - /** - * get node attribute - * @param name: attribute name - * returns default value if attribute not found - */ - template <class T> + + /** + * get node attribute + * @param name: attribute name + * returns default value if attribute not found + */ + template <class T> T Attr(TZtStringBuf name, const T& defvalue) const; - - /** - * get node attribute - * @param name: attribute name - * @param value: return-value - * throws exception if attribute not found - */ - template <class T> + + /** + * get node attribute + * @param name: attribute name + * @param value: return-value + * throws exception if attribute not found + */ + template <class T> void Attr(TZtStringBuf name, T& value) const; - - /** - * get node attribute - * @param name: attribute name - * @param defvalue: default value - * @param value: return-value - * returns default value if attribute not found, attr value otherwise - */ - template <class T> + + /** + * get node attribute + * @param name: attribute name + * @param defvalue: default value + * @param value: return-value + * returns default value if attribute not found, attr value otherwise + */ + template <class T> void Attr(TZtStringBuf name, T& value, const T& defvalue) const; - - /** - * get node value (text) - * @throws exception if node is blank - */ - template <class T> + + /** + * get node value (text) + * @throws exception if node is blank + */ + template <class T> T Value() const; - - /** - * get node value - * @param defvalue: default value - * returns default value if node is blank - */ - template <class T> + + /** + * get node value + * @param defvalue: default value + * returns default value if node is blank + */ + template <class T> T Value(const T& defvalue) const; - - /** - * set node value - * @param value: new text value - */ + + /** + * set node value + * @param value: new text value + */ template <class T> typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type SetValue(const T& value); - + void SetValue(TStringBuf value); - /** - * set/reset node attribute value, - * if attribute does not exist, it'll be created - * @param name: attribute name - * @param value: attribute value - */ + /** + * set/reset node attribute value, + * if attribute does not exist, it'll be created + * @param name: attribute name + * @param value: attribute value + */ template<class T> typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type SetAttr(TZtStringBuf name, const T& value); - + void SetAttr(TZtStringBuf name, TZtStringBuf value); void SetAttr(TZtStringBuf name); - /** + /** * delete node attribute * @param name: attribute name */ @@ -414,10 +414,10 @@ namespace NXml { TString Name() const; /** - * get node xpath - */ + * get node xpath + */ TString Path() const; - + /** * get node xml representation */ @@ -445,15 +445,15 @@ namespace NXml { */ void Remove(); - /** - * constructs null node - */ + /** + * constructs null node + */ TNode() : NodePointer(nullptr) , DocPointer(nullptr) { } - + private: friend class TConstNodes; @@ -462,30 +462,30 @@ namespace NXml { , DocPointer(doc) { } - + TNode Find(xmlNode* start, TZtStringBuf name); - + template <class T> void AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const; - + void SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const; xmlNode* NodePointer; xmlDoc* DocPointer; - }; - + }; + class TConstNode { - public: - friend class TDocument; - friend class TConstNodes; + public: + friend class TDocument; + friend class TConstNodes; friend class TNode; - /** - * check if node is null - */ + /** + * check if node is null + */ bool IsNull() const { return ActualNode.IsNull(); - } - + } + bool IsElementNode() const { return ActualNode.IsElementNode(); } @@ -494,7 +494,7 @@ namespace NXml { return ActualNode.Parent(); } - /** + /** * Create xpath context to be used later for fast xpath evaluation. * @param nss: explicitly specify XML namespaces to use and their prefixes */ @@ -504,18 +504,18 @@ namespace NXml { /** * get all element nodes matching given xpath expression - * @param xpath: xpath expression - * @param quiet: don't throw exception if zero nodes found + * @param xpath: xpath expression + * @param quiet: don't throw exception if zero nodes found * @param ns: explicitly specify XML namespaces to use and their prefixes * * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. - */ + */ TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { return ActualNode.Nodes(xpath, quiet, ns); - } - - /** + } + + /** * get all element nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found @@ -550,18 +550,18 @@ namespace NXml { /** * get the first element node matching given xpath expression - * @param xpath: path to node (from current node) - * @param quiet: don't throw exception if node not found, + * @param xpath: path to node (from current node) + * @param quiet: don't throw exception if node not found, * return null node (@see IsNull()) * @param ns: explicitly specify XML namespaces to use and their prefixes * * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. - */ + */ TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { return ActualNode.Node(xpath, quiet, ns); - } - + } + /** * get the first element node matching given xpath expression * @param xpath: path to node (from current node) @@ -578,88 +578,88 @@ namespace NXml { TConstNode FirstChild(TZtStringBuf name) const { return ActualNode.FirstChild(name); - } - + } + TConstNode FirstChild() const { return ActualNode.FirstChild(); } - /** - * get node neighbour - * @param name: neighbour name - * throws exception if no neighbour found - */ + /** + * get node neighbour + * @param name: neighbour name + * throws exception if no neighbour found + */ TConstNode NextSibling(TZtStringBuf name) const { return ActualNode.NextSibling(name); - } - + } + TConstNode NextSibling() const { return ActualNode.NextSibling(); } - /** - * get node attribute - * @param name: attribute name - * throws exception if attribute not found - */ - template <class T> + /** + * get node attribute + * @param name: attribute name + * throws exception if attribute not found + */ + template <class T> T Attr(TZtStringBuf name) const { return ActualNode.Attr<T>(name); - } - - /** - * get node attribute - * @param name: attribute name - * returns default value if attribute not found - */ - template <class T> + } + + /** + * get node attribute + * @param name: attribute name + * returns default value if attribute not found + */ + template <class T> T Attr(TZtStringBuf name, const T& defvalue) const { return ActualNode.Attr(name, defvalue); - } - - /** - * get node attribute - * @param name: attribute name - * @param value: return-value - * throws exception if attribute not found - */ - template <class T> + } + + /** + * get node attribute + * @param name: attribute name + * @param value: return-value + * throws exception if attribute not found + */ + template <class T> void Attr(TZtStringBuf name, T& value) const { return ActualNode.Attr(name, value); - } - - /** - * get node attribute - * @param name: attribute name - * @param defvalue: default value - * @param value: return-value - * returns default value if attribute not found, attr value otherwise - */ - template <class T> + } + + /** + * get node attribute + * @param name: attribute name + * @param defvalue: default value + * @param value: return-value + * returns default value if attribute not found, attr value otherwise + */ + template <class T> void Attr(TZtStringBuf name, T& value, const T& defvalue) const { return ActualNode.Attr(name, value, defvalue); - } - - /** - * get node value (text) - * @throws exception if node is blank - */ - template <class T> + } + + /** + * get node value (text) + * @throws exception if node is blank + */ + template <class T> T Value() const { return ActualNode.Value<T>(); - } - - /** - * get node value - * @param defvalue: default value - * returns default value if node is blank - */ - template <class T> + } + + /** + * get node value + * @param defvalue: default value + * returns default value if node is blank + */ + template <class T> T Value(const T& defvalue) const { return ActualNode.Value(defvalue); - } - - /** + } + + /** * get node name */ TString Name() const { @@ -688,12 +688,12 @@ namespace NXml { } /** - * get node xpath - */ + * get node xpath + */ TString Path() const { return ActualNode.Path(); - } - + } + /** * get node xml representation */ @@ -706,13 +706,13 @@ namespace NXml { : ActualNode(node) { } - + TNode ConstCast() const { return ActualNode; } - private: + private: TNode ActualNode; - }; - + }; + } diff --git a/library/cpp/xml/document/xml-document.cpp b/library/cpp/xml/document/xml-document.cpp index 3e29147b53..18a554d732 100644 --- a/library/cpp/xml/document/xml-document.cpp +++ b/library/cpp/xml/document/xml-document.cpp @@ -1,8 +1,8 @@ -#include "xml-document.h" - +#include "xml-document.h" + #include <libxml/xinclude.h> #include <libxml/xpathInternals.h> - + #include <library/cpp/xml/init/init.h> #include <util/generic/yexception.h> @@ -16,7 +16,7 @@ namespace { } initer; } -namespace NXml { +namespace NXml { TDocument::TDocument(const TString& xml, Source type) { switch (type) { case File: @@ -27,87 +27,87 @@ namespace NXml { break; case RootName: { TDocHolder doc(xmlNewDoc(XMLCHAR("1.0"))); - if (!doc) - THROW(XmlException, "Can't create xml document."); + if (!doc) + THROW(XmlException, "Can't create xml document."); doc->encoding = xmlStrdup(XMLCHAR("utf-8")); TNodePtr node(xmlNewNode(nullptr, XMLCHAR(xml.c_str()))); if (!node) - THROW(XmlException, "Can't create root node."); - xmlDocSetRootElement(doc.Get(), node.Get()); + THROW(XmlException, "Can't create root node."); + xmlDocSetRootElement(doc.Get(), node.Get()); Y_UNUSED(node.Release()); Doc = std::move(doc); } break; default: THROW(InvalidArgument, "Wrong source type"); - } - } - + } + } + TDocument::TDocument(TDocument&& doc) : Doc(std::move(doc.Doc)) { } - + TDocument& TDocument::operator=(TDocument&& doc) { - if (this != &doc) + if (this != &doc) doc.Swap(*this); - - return *this; - } - + + return *this; + } + void TDocument::ParseFile(const TString& file) { if (!NFs::Exists(file)) - THROW(XmlException, "File " << file << " doesn't exist"); - - TParserCtxtPtr pctx(xmlNewParserCtxt()); - if (!pctx) - THROW(XmlException, "Can't create parser context"); - + THROW(XmlException, "File " << file << " doesn't exist"); + + TParserCtxtPtr pctx(xmlNewParserCtxt()); + if (!pctx) + THROW(XmlException, "Can't create parser context"); + TDocHolder doc(xmlCtxtReadFile(pctx.Get(), file.c_str(), nullptr, XML_PARSE_NOCDATA)); - if (!doc) - THROW(XmlException, "Can't parse file " << file); - - int res = xmlXIncludeProcessFlags(doc.Get(), XML_PARSE_XINCLUDE | XML_PARSE_NOCDATA | XML_PARSE_NOXINCNODE); - - if (res == -1) - THROW(XmlException, "XIncludes processing failed"); - + if (!doc) + THROW(XmlException, "Can't parse file " << file); + + int res = xmlXIncludeProcessFlags(doc.Get(), XML_PARSE_XINCLUDE | XML_PARSE_NOCDATA | XML_PARSE_NOXINCNODE); + + if (res == -1) + THROW(XmlException, "XIncludes processing failed"); + Doc = std::move(doc); - } - + } + void TDocument::ParseString(TZtStringBuf xml) { - TParserCtxtPtr pctx(xmlNewParserCtxt()); + TParserCtxtPtr pctx(xmlNewParserCtxt()); if (pctx.Get() == nullptr) - THROW(XmlException, "Can't create parser context"); - + THROW(XmlException, "Can't create parser context"); + TDocHolder doc(xmlCtxtReadMemory(pctx.Get(), xml.c_str(), (int)xml.size(), nullptr, nullptr, XML_PARSE_NOCDATA)); - - if (!doc) - THROW(XmlException, "Can't parse string"); - + + if (!doc) + THROW(XmlException, "Can't parse string"); + Doc = std::move(doc); - } - + } + TNode TDocument::Root() { xmlNode* r = xmlDocGetRootElement(Doc.Get()); if (r == nullptr) - THROW(XmlException, "TDocument hasn't root element"); - + THROW(XmlException, "TDocument hasn't root element"); + return TNode(Doc.Get(), r); - } - + } + TConstNode TDocument::Root() const { xmlNode* r = xmlDocGetRootElement(Doc.Get()); if (r == nullptr) - THROW(XmlException, "TDocument hasn't root element"); - + THROW(XmlException, "TDocument hasn't root element"); + return TConstNode(TNode(Doc.Get(), r)); - } - + } + bool TNode::IsNull() const { return NodePointer == nullptr; - } - + } + bool TNode::IsElementNode() const { return !IsNull() && (NodePointer->type == XML_ELEMENT_NODE); } @@ -130,7 +130,7 @@ namespace NXml { TXPathContextPtr ctxt = CreateXPathContext(ns); return XPath(xpath, quiet, *ctxt); } - + TConstNodes TNode::XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { if (xmlXPathSetContextNode(NodePointer, &ctxt) != 0) THROW(XmlException, "Can't set xpath context node, probably the context is associated with another document"); @@ -142,11 +142,11 @@ namespace NXml { TConstNodes nodes(DocPointer, obj); if (nodes.Size() == 0 && !quiet) - THROW(NodeNotFound, xpath); - - return nodes; - } - + THROW(NodeNotFound, xpath); + + return nodes; + } + TConstNodes TNode::Nodes(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { TXPathContextPtr ctxt = CreateXPathContext(ns); return Nodes(xpath, quiet, *ctxt); @@ -158,12 +158,12 @@ namespace NXml { THROW(XmlException, "xpath points to non-element nodes: " << xpath); return nodes; } - + TNode TNode::Node(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) { TXPathContextPtr ctxt = CreateXPathContext(ns); return Node(xpath, quiet, *ctxt); } - + TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { TXPathContextPtr ctxt = CreateXPathContext(ns); return Node(xpath, quiet, *ctxt); @@ -173,29 +173,29 @@ namespace NXml { TConstNodes n = Nodes(xpath, quiet, ctxt); if (n.Size() == 0 && !quiet) - THROW(NodeNotFound, xpath); - + THROW(NodeNotFound, xpath); + if (n.Size() == 0) return TNode(); - else + else return n[0].ConstCast(); - } - + } + TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { return const_cast<TNode*>(this)->Node(xpath, quiet, ctxt); - } - + } + TNode TNode::FirstChild(TZtStringBuf name) { if (IsNull()) - THROW(XmlException, "Node is null"); - + THROW(XmlException, "Node is null"); + return Find(NodePointer->children, name); - } - + } + TConstNode TNode::FirstChild(TZtStringBuf name) const { return const_cast<TNode*>(this)->FirstChild(name); - } - + } + TNode TNode::FirstChild() { if (IsNull()) THROW(XmlException, "Node is null"); @@ -220,15 +220,15 @@ namespace NXml { TNode TNode::NextSibling(TZtStringBuf name) { if (IsNull()) - THROW(XmlException, "Node is null"); - + THROW(XmlException, "Node is null"); + return Find(NodePointer->next, name); - } - + } + TConstNode TNode::NextSibling(TZtStringBuf name) const { return const_cast<TNode*>(this)->NextSibling(name); - } - + } + TNode TNode::NextSibling() { if (IsNull()) THROW(XmlException, "Node is null"); @@ -240,12 +240,12 @@ namespace NXml { return const_cast<TNode*>(this)->NextSibling(); } - /* NOTE: by default child will inherit it's parent ns */ - + /* NOTE: by default child will inherit it's parent ns */ + TNode TNode::AddChild(TZtStringBuf name) { - return AddChild(name, ""); - } - + return AddChild(name, ""); + } + /* NOTE: source node will be copied, as otherwise it will be double-freed from this and its own document */ TNode TNode::AddChild(const TConstNode& node) { @@ -257,34 +257,34 @@ namespace NXml { void TNode::SetPrivate(void* priv) { NodePointer->_private = priv; } - + void* TNode::GetPrivate() const { return NodePointer->_private; } TNode TNode::Find(xmlNode* start, TZtStringBuf name) { - for (; start; start = start->next) + for (; start; start = start->next) if (start->type == XML_ELEMENT_NODE && (name.empty() || !xmlStrcmp(start->name, XMLCHAR(name.c_str())))) return TNode(DocPointer, start); - + return TNode(); - } - + } + TString TNode::Name() const { if (IsNull()) THROW(XmlException, "Node is null"); return CAST2CHAR(NodePointer->name); } - + TString TNode::Path() const { TCharPtr path(xmlGetNodePath(NodePointer)); - if (!!path) - return CAST2CHAR(path.Get()); - else - return ""; - } - + if (!!path) + return CAST2CHAR(path.Get()); + else + return ""; + } + xmlNode* TNode::GetPtr() { return NodePointer; } @@ -335,57 +335,57 @@ namespace NXml { : SizeValue(nodes.Size()) , Doc(nodes.Doc) , Obj(nodes.Obj) - { - } - + { + } + TConstNodes& TConstNodes::operator=(const TConstNodes& nodes) { if (this != &nodes) { SizeValue = nodes.Size(); Doc = nodes.Doc; Obj = nodes.Obj; - } - - return *this; - } - - TConstNodes::TConstNodes(TConstNodesRef ref) + } + + return *this; + } + + TConstNodes::TConstNodes(TConstNodesRef ref) : SizeValue(ref.r_.Size()) , Doc(ref.r_.Doc) , Obj(ref.r_.Obj) - { - } - + { + } + TConstNodes& TConstNodes::operator=(TConstNodesRef ref) { if (this != &ref.r_) { SizeValue = ref.r_.Size(); Doc = ref.r_.Doc; Obj = ref.r_.Obj; - } - return *this; - } - + } + return *this; + } + TConstNodes::operator TConstNodesRef() { - return TConstNodesRef(*this); - } - + return TConstNodesRef(*this); + } + TConstNodes::TConstNodes(xmlDoc* doc, TXPathObjectPtr obj) : SizeValue(obj && obj->nodesetval ? obj->nodesetval->nodeNr : 0) , Doc(doc) , Obj(obj) - { - } - + { + } + TConstNode TConstNodes::operator[](size_t number) const { if (number + 1 > Size()) - THROW(XmlException, "index out of range " << number); - + THROW(XmlException, "index out of range " << number); + if (!Obj || !Obj->nodesetval) THROW(XmlException, "Broken TConstNodes object, Obj is null"); - + xmlNode* node = Obj->nodesetval->nodeTab[number]; return TNode(Doc, node); - } - + } + TConstNode TConstNodes::TNodeIter::operator*() const { return Nodes[Index]; } diff --git a/library/cpp/xml/document/xml-document.h b/library/cpp/xml/document/xml-document.h index ff496bb138..829ba09cc4 100644 --- a/library/cpp/xml/document/xml-document.h +++ b/library/cpp/xml/document/xml-document.h @@ -1,4 +1,4 @@ -#pragma once - +#pragma once + #include "xml-document-decl.h" #include "node-attr.h" diff --git a/library/cpp/xml/document/ya.make b/library/cpp/xml/document/ya.make index 1f284951e8..86bbd639cf 100644 --- a/library/cpp/xml/document/ya.make +++ b/library/cpp/xml/document/ya.make @@ -1,17 +1,17 @@ LIBRARY() - + OWNER(finder) SRCS( - xml-document.cpp + xml-document.cpp xml-textreader.cpp xml-options.cpp -) - -PEERDIR( +) + +PEERDIR( library/cpp/xml/init - contrib/libs/libxml + contrib/libs/libxml library/cpp/string_utils/ztstrbuf -) - -END() +) + +END() |