]> git.proxmox.com Git - ceph.git/blob - ceph/src/dmclock/sim/src/ConfUtils.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / dmclock / sim / src / 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
23 /*
24 * Ceph configuration file support.
25 *
26 * This class loads an INI-style configuration from a file or bufferlist, and
27 * holds it in memory. In general, an INI configuration file is composed of
28 * sections, which contain key/value pairs. You can put comments on the end of
29 * lines by using either a hash mark (#) or the semicolon (;).
30 *
31 * You can get information out of ConfFile by calling get_key or by examining
32 * individual sections.
33 *
34 * This class could be extended to support modifying configuration files and
35 * writing them back out without too much difficulty. Currently, this is not
36 * implemented, and the file is read-only.
37 */
38 class ConfLine {
39 public:
40 ConfLine(const std::string &key_, const std::string val_,
41 const std::string newsection_, const std::string comment_, int line_no_);
42 bool operator<(const ConfLine &rhs) const;
43 friend std::ostream &operator<<(std::ostream& oss, const ConfLine &l);
44
45 std::string key, val, newsection;
46 };
47
48 class ConfSection {
49 public:
50 typedef std::set <ConfLine>::const_iterator const_line_iter_t;
51
52 std::set <ConfLine> lines;
53 };
54
55 class ConfFile {
56 public:
57 typedef std::map <std::string, ConfSection>::iterator section_iter_t;
58 typedef std::map <std::string, ConfSection>::const_iterator const_section_iter_t;
59
60 ConfFile();
61 ~ConfFile();
62 void clear();
63 int parse_file(const std::string &fname, std::deque<std::string> *errors, std::ostream *warnings);
64 int read(const std::string &section, const std::string &key,
65 std::string &val) const;
66
67 const_section_iter_t sections_begin() const;
68 const_section_iter_t sections_end() const;
69
70 static void trim_whitespace(std::string &str, bool strip_internal);
71 static std::string normalize_key_name(const std::string &key);
72 friend std::ostream &operator<<(std::ostream &oss, const ConfFile &cf);
73
74 private:
75 void load_from_buffer(const char *buf, size_t sz,
76 std::deque<std::string> *errors, std::ostream *warnings);
77 static ConfLine* process_line(int line_no, const char *line,
78 std::deque<std::string> *errors);
79
80 std::map <std::string, ConfSection> sections;
81 };
82
83 #endif