]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/ConfUtils.h
d/control: depend on python3-yaml for ceph-mgr
[ceph.git] / ceph / src / common / ConfUtils.h
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>
22 #include <string_view>
23 #include <vector>
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 */
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;
48 };
49
50 std::ostream &operator<<(std::ostream& oss, const conf_line_t& line);
51
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&);
59 };
60
61 class ConfFile : public std::map<std::string, conf_section_t> {
62 using base_type = std::map<std::string, conf_section_t>;
63 public:
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);
73 int read(const std::string& section, std::string_view key,
74 std::string &val) const;
75 static std::string normalize_key_name(std::string_view key);
76 private:
77 bool load_from_buffer(std::string_view buf, std::ostream* warning);
78 };
79
80 std::ostream &operator<<(std::ostream& oss, const ConfFile& cf);
81
82 #endif