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