conf.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #pragma once
  2. #include <library/cpp/logger/all.h>
  3. #include <util/str_stl.h>
  4. #include <library/cpp/charset/ci_string.h>
  5. #include <util/generic/map.h>
  6. #include <util/generic/ptr.h>
  7. #include <util/generic/stack.h>
  8. #include <util/generic/string.h>
  9. #include <util/generic/vector.h>
  10. #include <library/cpp/string_utils/parse_vector/vector_parser.h>
  11. #include <util/generic/yexception.h>
  12. #include <util/stream/output.h>
  13. #include <util/string/cast.h>
  14. #include <util/system/defaults.h>
  15. #include <util/system/yassert.h>
  16. #include <util/generic/noncopyable.h>
  17. class TSectionDesc;
  18. class TYandexConfig: public TSimpleRefCount<TYandexConfig>, TNonCopyable {
  19. public:
  20. class Directives;
  21. typedef TMap<const char*, const char*, ci_less> SectionAttrs;
  22. struct Section;
  23. typedef TMultiMap<TCiString, Section*> TSectionsMap;
  24. struct Section {
  25. const char* Name;
  26. SectionAttrs Attrs;
  27. Directives* Cookie;
  28. Section* Parent;
  29. Section* Next;
  30. Section* Child;
  31. bool Owner;
  32. TSectionDesc* Desc;
  33. Section()
  34. : Name("")
  35. , Cookie(nullptr)
  36. , Parent(nullptr)
  37. , Next(nullptr)
  38. , Child(nullptr)
  39. , Owner(false)
  40. , Desc(nullptr)
  41. {
  42. }
  43. Directives& GetDirectives() {
  44. Y_ASSERT(Cookie);
  45. return *Cookie;
  46. }
  47. const Directives& GetDirectives() const {
  48. Y_ASSERT(Cookie);
  49. return *Cookie;
  50. }
  51. bool Parsed() const {
  52. return Cookie != nullptr;
  53. }
  54. TSectionsMap GetAllChildren() const;
  55. };
  56. public:
  57. TYandexConfig()
  58. : FileData(nullptr)
  59. {
  60. Clear();
  61. }
  62. virtual ~TYandexConfig() {
  63. Clear();
  64. }
  65. [[nodiscard]] bool Read(const TString& path);
  66. [[nodiscard]] bool ReadMemory(const char* buffer, const char* configPath = nullptr);
  67. [[nodiscard]] bool ReadMemory(const TStringBuf& buffer, const char* configPath = nullptr);
  68. [[nodiscard]] bool Parse(const TString& path, bool process_directives = true);
  69. [[nodiscard]] bool ParseMemory(const char* buffer, bool process_directives = true, const char* configPath = nullptr);
  70. [[nodiscard]] bool ParseMemory(const TStringBuf& buffer, bool processDirectives = true, const char* configPath = nullptr);
  71. [[nodiscard]] bool ParseSection(const char* SecName, const char* idname = nullptr, const char* idvalue = nullptr);
  72. void AddSection(Section* sec);
  73. void Clear();
  74. void ReportError(const char* ptr, const char* err, bool warning = false);
  75. void ReportError(const char* ptr, bool warning, const char* format, ...) Y_PRINTF_FORMAT(4, 5);
  76. void PrintErrors(TLog* Log);
  77. void PrintErrors(TString& Err);
  78. template <typename TLogWriter>
  79. void PrintErrors(TLogWriter& writer) {
  80. for (const auto& s : Errors) {
  81. writer() << "In '" << ConfigPath << "': " << s << '\n';
  82. }
  83. Errors.clear();
  84. }
  85. Section* GetFirstChild(const char* Name, Section* CurSection = nullptr);
  86. const char* GetConfigPath() const {
  87. return ConfigPath.data();
  88. }
  89. Section* GetRootSection() {
  90. Y_ASSERT(!AllSections.empty());
  91. return AllSections[0];
  92. }
  93. const Section* GetRootSection() const {
  94. Y_ASSERT(!AllSections.empty());
  95. return AllSections[0];
  96. }
  97. void PrintConfig(IOutputStream& os) const;
  98. static void PrintSectionConfig(const TYandexConfig::Section* section, IOutputStream& os, bool printNextSection = true);
  99. protected:
  100. //the followind three functions return 'false' only for fatal errors to break the parsing
  101. virtual bool AddKeyValue(Section& sec, const char* key, const char* value);
  102. virtual bool OnBeginSection(Section& sec); //keep sec.Cookie==0 to skip the section
  103. virtual bool OnEndSection(Section& sec);
  104. private:
  105. bool PrepareLines();
  106. void ProcessComments();
  107. bool ProcessRoot(bool process_directives);
  108. bool ProcessAll(bool process_directives);
  109. bool ProcessBeginSection();
  110. bool ProcessEndSection();
  111. bool ProcessDirective();
  112. void ProcessLineBreak(char*& LineBreak, char toChange);
  113. bool FindEndOfSection(const char* SecName, const char* begin, char*& endsec, char*& endptr);
  114. private:
  115. char* FileData;
  116. ui32 Len;
  117. char* CurrentMemoryPtr;
  118. TStack<Section*> CurSections;
  119. TVector<Section*> AllSections;
  120. TVector<TString> Errors;
  121. TVector<const char*> EndLines;
  122. TString ConfigPath;
  123. };
  124. class TYandexConfig::Directives: public TMap<TCiString, const char*, std::less<>> {
  125. public:
  126. Directives(bool isStrict)
  127. : strict(isStrict)
  128. {
  129. }
  130. Directives()
  131. : strict(true)
  132. {
  133. }
  134. virtual ~Directives() = default;
  135. bool IsStrict() const {
  136. return strict;
  137. }
  138. bool AddKeyValue(const TString& key, const char* value);
  139. bool GetValue(TStringBuf key, TString& value) const;
  140. bool GetNonEmptyValue(TStringBuf key, TString& value) const;
  141. bool GetValue(TStringBuf key, bool& value) const;
  142. template <class T>
  143. inline bool GetValue(TStringBuf key, T& value) const {
  144. TString tmp;
  145. if (GetValue(key, tmp)) {
  146. value = FromString<T>(tmp);
  147. return true;
  148. }
  149. return false;
  150. }
  151. template <class T>
  152. inline T Value(TStringBuf key, T def = T()) const {
  153. GetValue(key, def);
  154. return def;
  155. }
  156. template <class T, class TDelim = char, bool emptyOK = true>
  157. bool TryFillArray(TStringBuf key, TVector<T>& result, const TDelim delim = ',') const {
  158. auto it = find(key);
  159. if (it != end() && (*it).second != nullptr) {
  160. TVector<T> localResult;
  161. if (!TryParseStringToVector((*it).second, localResult, delim)) {
  162. return false;
  163. } else {
  164. std::swap(localResult, result);
  165. return true;
  166. }
  167. } else {
  168. if (emptyOK) {
  169. result.clear();
  170. }
  171. }
  172. return emptyOK;
  173. }
  174. bool FillArray(TStringBuf key, TVector<TString>& values) const;
  175. void Clear();
  176. void declare(const char* directive_name) {
  177. insert(value_type(directive_name, nullptr));
  178. }
  179. virtual bool CheckOnEnd(TYandexConfig& yc, TYandexConfig::Section& sec);
  180. protected:
  181. bool strict;
  182. };
  183. #define DECLARE_CONFIG(ConfigClass) \
  184. class ConfigClass: public TYandexConfig { \
  185. public: \
  186. ConfigClass() \
  187. : TYandexConfig() { \
  188. } \
  189. \
  190. protected: \
  191. virtual bool OnBeginSection(Section& sec); \
  192. \
  193. private: \
  194. ConfigClass(const ConfigClass&); \
  195. ConfigClass& operator=(const ConfigClass&); \
  196. };
  197. #define DECLARE_SECTION(SectionClass) \
  198. class SectionClass: public TYandexConfig::Directives { \
  199. public: \
  200. SectionClass(); \
  201. };
  202. #define DECLARE_SECTION_CHECK(SectionClass) \
  203. class SectionClass: public TYandexConfig::Directives { \
  204. public: \
  205. SectionClass(); \
  206. bool CheckOnEnd(TYandexConfig& yc, TYandexConfig::Section& sec); \
  207. };
  208. #define BEGIN_CONFIG(ConfigClass) \
  209. bool ConfigClass::OnBeginSection(Section& sec) { \
  210. if (sec.Parent == &sec) /* it's root */ { \
  211. assert(*sec.Name == 0); \
  212. /* do not allow any directives at root */ \
  213. sec.Cookie = new TYandexConfig::Directives; \
  214. sec.Owner = true; \
  215. return true; \
  216. }
  217. #define BEGIN_TOPSECTION2(SectionName, DirectivesClass) \
  218. if (*sec.Parent->Name == 0) { /* it's placed at root */ \
  219. if (stricmp(sec.Name, #SectionName) == 0) { \
  220. sec.Cookie = new DirectivesClass; \
  221. sec.Owner = true; \
  222. return true; \
  223. } \
  224. } else if (stricmp(sec.Parent->Name, #SectionName) == 0) {
  225. #define BEGIN_SUBSECTION(SectionName, SubSectionName) \
  226. if (stricmp(sec.Parent->Name, #SubSectionName) == 0 && stricmp(sec.Parent->Parent->Name, #SectionName) == 0) {
  227. #define SUBSECTION2(SubSectionName, DirectivesClass) \
  228. if (stricmp(sec.Name, #SubSectionName) == 0) { \
  229. sec.Cookie = new DirectivesClass; \
  230. sec.Owner = true; \
  231. return true; \
  232. }
  233. #define FAKESECTION(SubSectionName) \
  234. if (stricmp(sec.Name, #SubSectionName) == 0) { \
  235. Y_ASSERT(sec.Cookie == 0); \
  236. return true; \
  237. }
  238. #define END_SECTION() \
  239. }
  240. #define END_CONFIG() \
  241. if (!sec.Parent->Parsed()) \
  242. return true; \
  243. ReportError(sec.Name, true, "section \'%s\' not allowed here and will be ignored", sec.Name); \
  244. return true; \
  245. }
  246. #define SUBSECTION(SectionName) SUBSECTION2(SectionName, SectionName)
  247. #define BEGIN_TOPSECTION(SectionName) BEGIN_TOPSECTION2(SectionName, SectionName)
  248. #define BEGIN_SECTION(SectionClass) \
  249. SectionClass::SectionClass() {
  250. #define DEFINE_SECTION(SectionClass) \
  251. class SectionClass: public TYandexConfig::Directives { \
  252. public: \
  253. SectionClass() {
  254. #define DIRECTIVE(DirectiveName) declare(#DirectiveName);
  255. #define END_DEFINE_SECTION \
  256. } \
  257. } \
  258. ;
  259. #define END_DEFINE_SECTION_CHECK \
  260. } \
  261. bool CheckOnEnd(TYandexConfig& yc, TYandexConfig::Section& sec); \
  262. } \
  263. ;
  264. #define DEFINE_INDEFINITE_SECTION(SectionClass) \
  265. class SectionClass: public TYandexConfig::Directives { \
  266. public: \
  267. SectionClass() { \
  268. strict = false; \
  269. } \
  270. };
  271. #define BEGIN_SECTION_CHECK(SectionClass) \
  272. bool SectionClass::CheckOnEnd(TYandexConfig& yc, TYandexConfig::Section& sec) { \
  273. (void)yc; \
  274. (void)sec; \
  275. SectionClass& type = *this; \
  276. (void)type;
  277. #define DIR_ABSENT(DirectiveName) (type[#DirectiveName] == 0)
  278. #define DIR_ARG_ABSENT(DirectiveName) (type[#DirectiveName] == 0 || *(type[#DirectiveName]) == 0)
  279. #define DIR_PRESENT(DirectiveName) (type[#DirectiveName] != 0)
  280. #define DIR_ARG_PRESENT(DirectiveName) (type[#DirectiveName] != 0 && *(type[#DirectiveName]) != 0)
  281. #define DIRECTIVE_MUSTBE(DirectiveName) \
  282. if (DIR_ARG_ABSENT(DirectiveName)) { \
  283. yc.ReportError(sec.Name, true, \
  284. "Section \'%s\' must include directive \'%s\'. Section will be ignored", \
  285. sec.Name, #DirectiveName); \
  286. return false; \
  287. }
  288. #define DIRECTIVE_ATLEAST(DirectiveName1, DirectiveName2) \
  289. if (DIR_ARG_ABSENT(DirectiveName1) && DIR_ARG_ABSENT(DirectiveName2)) { \
  290. yc.ReportError(sec.Name, true, \
  291. "Section \'%s\' must include directives \'%s\' or \'%s\'. Section will be ignored", \
  292. sec.Name, #DirectiveName1, #DirectiveName2); \
  293. return false; \
  294. }
  295. #define DIRECTIVE_BOTH(DirectiveName1, DirectiveName2) \
  296. if (DIR_ARG_ABSENT(DirectiveName1) && DIR_ARG_PRESENT(DirectiveName2) || DIR_ARG_ABSENT(DirectiveName2) && DIR_ARG_PRESENT(DirectiveName1)) { \
  297. yc.ReportError(sec.Name, true, \
  298. "Section \'%s\' must include directives \'%s\' and \'%s\' simultaneously. Section will be ignored", \
  299. sec.Name, #DirectiveName1, #DirectiveName2); \
  300. return false; \
  301. }
  302. #define END_SECTION_CHECK() \
  303. return true; \
  304. }
  305. class config_exception: public yexception {
  306. public:
  307. config_exception(const char* fp) {
  308. filepoint = fp;
  309. }
  310. const char* where() const noexcept {
  311. return filepoint;
  312. }
  313. private:
  314. const char* filepoint;
  315. };
  316. #define DEFINE_UNSTRICT_SECTION(SectionClasse) \
  317. class SectionClasse \
  318. : public TYandexConfig::Directives { \
  319. public: \
  320. SectionClasse(const TYandexConfig::Directives& obj) \
  321. : TYandexConfig::Directives(obj) { \
  322. strict = false; \
  323. } \
  324. SectionClasse() { \
  325. strict = false;
  326. DEFINE_UNSTRICT_SECTION(AnyDirectives)
  327. END_DEFINE_SECTION
  328. #define EMBEDDED_CONFIG(SectionName) \
  329. if (sec.Parent != &sec) /* not root not placed at root */ { \
  330. Section* parent = sec.Parent; \
  331. while (*parent->Name != 0) { /* any child of SectionName */ \
  332. if (stricmp(parent->Name, #SectionName) == 0) { \
  333. sec.Cookie = new AnyDirectives(); \
  334. sec.Owner = true; \
  335. return true; \
  336. } \
  337. parent = parent->Parent; \
  338. } \
  339. }
  340. #define ONE_EMBEDDED_CONFIG(SectionName) \
  341. if (sec.Parent != &sec) /* not root not placed at root */ { \
  342. Section* parent = sec.Parent; \
  343. while (*parent->Name != 0) { /* any child of SectionName */ \
  344. if (stricmp(parent->Name, #SectionName) == 0) { \
  345. if (!parent->Child->Next) { \
  346. sec.Cookie = new AnyDirectives(); \
  347. sec.Owner = true; \
  348. return true; \
  349. } else { \
  350. break; \
  351. } \
  352. } \
  353. parent = parent->Parent; \
  354. } \
  355. }