]>
git.proxmox.com Git - ceph.git/blob - 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
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
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.
15 #ifndef CEPH_CONFUTILS_H
16 #define CEPH_CONFUTILS_H
22 #include <string_view>
25 #include "include/buffer_fwd.h"
28 * Ceph configuration file support.
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 (;).
35 * You can get information out of ConfFile by calling get_key or by examining
36 * individual sections.
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.
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;
50 std::ostream
&operator<<(std::ostream
& oss
, const conf_line_t
& line
);
52 class conf_section_t
: public std::set
<conf_line_t
> {
54 conf_section_t() = default;
55 conf_section_t(const std::string
& heading
,
56 const std::vector
<conf_line_t
>& lines
);
58 friend std::ostream
& operator<<(std::ostream
& os
, const conf_section_t
&);
61 class ConfFile
: public std::map
<std::string
, conf_section_t
> {
62 using base_type
= std::map
<std::string
, conf_section_t
>;
65 : ConfFile
{std::vector
<conf_section_t
>{}}
67 ConfFile(const conf_line_t
& line
)
68 : ConfFile
{{conf_section_t
{"global", {line
}}}}
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
);
77 bool load_from_buffer(std::string_view buf
, std::ostream
* warning
);
80 std::ostream
&operator<<(std::ostream
& oss
, const ConfFile
& cf
);