]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2011 New Dream Network | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #ifndef CEPH_CONFUTILS_H | |
16 | #define CEPH_CONFUTILS_H | |
17 | ||
18 | #include <deque> | |
19 | #include <map> | |
20 | #include <set> | |
21 | #include <string> | |
9f95a23c TL |
22 | #include <string_view> |
23 | #include <vector> | |
7c673cae FG |
24 | |
25 | #include "include/buffer_fwd.h" | |
26 | ||
27 | /* | |
28 | * Ceph configuration file support. | |
29 | * | |
30 | * This class loads an INI-style configuration from a file or bufferlist, and | |
31 | * holds it in memory. In general, an INI configuration file is composed of | |
32 | * sections, which contain key/value pairs. You can put comments on the end of | |
33 | * lines by using either a hash mark (#) or the semicolon (;). | |
34 | * | |
35 | * You can get information out of ConfFile by calling get_key or by examining | |
36 | * individual sections. | |
37 | * | |
38 | * This class could be extended to support modifying configuration files and | |
39 | * writing them back out without too much difficulty. Currently, this is not | |
40 | * implemented, and the file is read-only. | |
41 | */ | |
9f95a23c TL |
42 | struct conf_line_t { |
43 | conf_line_t() = default; | |
44 | conf_line_t(const std::string& key, const std::string& val); | |
45 | bool operator<(const conf_line_t& rhs) const; | |
46 | std::string key; | |
47 | std::string val; | |
7c673cae FG |
48 | }; |
49 | ||
9f95a23c | 50 | std::ostream &operator<<(std::ostream& oss, const conf_line_t& line); |
7c673cae | 51 | |
9f95a23c TL |
52 | class conf_section_t : public std::set<conf_line_t> { |
53 | public: | |
54 | conf_section_t() = default; | |
55 | conf_section_t(const std::string& heading, | |
56 | const std::vector<conf_line_t>& lines); | |
57 | std::string heading; | |
58 | friend std::ostream& operator<<(std::ostream& os, const conf_section_t&); | |
7c673cae FG |
59 | }; |
60 | ||
f67539c2 TL |
61 | class ConfFile : public std::map<std::string, conf_section_t, std::less<>> { |
62 | using base_type = std::map<std::string, conf_section_t, std::less<>>; | |
7c673cae | 63 | public: |
9f95a23c TL |
64 | ConfFile() |
65 | : ConfFile{std::vector<conf_section_t>{}} | |
66 | {} | |
67 | ConfFile(const conf_line_t& line) | |
68 | : ConfFile{{conf_section_t{"global", {line}}}} | |
69 | {} | |
70 | ConfFile(const std::vector<conf_section_t>& sections); | |
71 | int parse_file(const std::string &fname, std::ostream *warnings); | |
72 | int parse_bufferlist(ceph::bufferlist *bl, std::ostream *warnings); | |
f67539c2 TL |
73 | bool parse_buffer(std::string_view buf, std::ostream* warning); |
74 | int read(std::string_view section, std::string_view key, | |
9f95a23c TL |
75 | std::string &val) const; |
76 | static std::string normalize_key_name(std::string_view key); | |
f67539c2 TL |
77 | // print warnings to os if any old-style section name is found |
78 | // | |
79 | // consider a section name as old-style name if it starts with any of the | |
80 | // given prefixes, but does not follow with a "." | |
81 | void check_old_style_section_names(const std::vector<std::string>& prefixes, | |
82 | std::ostream& os); | |
83 | ||
7c673cae FG |
84 | }; |
85 | ||
9f95a23c TL |
86 | std::ostream &operator<<(std::ostream& oss, const ConfFile& cf); |
87 | ||
7c673cae | 88 | #endif |