1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2020-2020 Bareos GmbH & Co. KG
5 
6    This program is Free Software; you can redistribute it and/or
7    modify it under the terms of version three of the GNU Affero General Public
8    License as published by the Free Software Foundation and included
9    in the file LICENSE.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14    Affero General Public License for more details.
15 
16    You should have received a copy of the GNU Affero General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.
20 */
21 /**
22  * @file
23  * Output Formatter for Bareos Resources
24  */
25 
26 #ifndef BAREOS_LIB_OUTPUT_FORMATTER_RESOURCE_H_
27 #define BAREOS_LIB_OUTPUT_FORMATTER_RESOURCE_H_
28 
29 #include "lib/alist.h"
30 #define NEED_JANSSON_NAMESPACE 1
31 #include "lib/output_formatter.h"
32 
33 /**
34  * Actual output formatter class.
35  */
36 class OutputFormatterResource {
37  private:
38   /*
39    * Members
40    */
41   OutputFormatter* send_;
42   int indent_level_ = 0;
43 
44  private:
45   /*
46    * Methods
47    */
48   bool requiresEscaping(const char* o);
49   void KeyMultipleStringsOnePerLineAddItem(const char* key,
50                                            const char* item,
51                                            bool as_comment,
52                                            bool quoted_strings,
53                                            bool escape_strings);
54 
55 
56  public:
57   /*
58    * Methods
59    */
60   OutputFormatterResource(OutputFormatter* send, int indent_level = 0);
61   ~OutputFormatterResource();
62 
GetOutputFormatter()63   OutputFormatter* GetOutputFormatter() { return send_; }
64 
65   std::string GetKeyFormatString(bool inherited,
66                                  std::string baseformat = "%s = ");
67 
68   void ResourceStart(const char* resource_type_groupname,
69                      const char* resource_type_name,
70                      const char* resource_name,
71                      bool as_comment = false);
72   void ResourceEnd(const char* resource_type_groupname,
73                    const char* resource_type_name,
74                    const char* resource_name,
75                    bool as_comment = false);
76 
77   void SubResourceStart(const char* name,
78                         bool as_comment = false,
79                         std::string baseformat = "%s {\n");
80   void SubResourceEnd(const char* name,
81                       bool as_comment = false,
82                       std::string baseformat = "}\n");
83 
84   void KeyBool(const char* name, bool value, bool as_comment = false);
85 
86   void KeySignedInt(const char* name, int64_t value, bool as_comment = false);
87   void KeyUnsignedInt(const char* name, int64_t value, bool as_comment = false);
88 
89   void KeyString(const char* name, const char* value, bool as_comment = false)
90   {
91     KeyUnquotedString(name, value, as_comment);
92   }
93   void KeyString(const char* name, std::string value, bool as_comment = false)
94   {
95     KeyUnquotedString(name, value.c_str(), as_comment);
96   }
97   void KeyQuotedString(const char* name,
98                        const char* value,
99                        bool as_comment = false);
100   void KeyQuotedString(const char* name,
101                        const std::string value,
102                        bool as_comment = false);
103   void KeyUnquotedString(const char* name,
104                          const char* value,
105                          bool as_comment = false);
106   void KeyUnquotedString(const char* name,
107                          const std::string value,
108                          bool as_comment = false);
109 
110   void KeyMultipleStringsInOneLine(const char* key,
111                                    alist* list,
112                                    bool as_comment = false,
113                                    bool quoted_strings = true);
114 
115   void KeyMultipleStringsInOneLine(
116       const char* key,
117       alist* list,
118       std::function<const char*(void* item)> GetValue,
119       bool as_comment = false,
120       bool quoted_strings = true);
121 
122   void KeyMultipleStringsOnePerLine(const char* key,
123                                     alist* list,
124                                     bool as_comment = false,
125                                     bool quoted_strings = true,
126                                     bool escape_strings = false);
127 
128   void KeyMultipleStringsOnePerLine(const char* key,
129                                     alist* list,
130                                     std::function<const char*(void*)> GetValue,
131                                     bool as_comment = false,
132                                     bool quoted_strings = true,
133                                     bool escape_strings = false);
134 
135   void KeyMultipleStringsOnePerLine(const char* key,
136                                     const std::vector<std::string>&,
137                                     bool as_comment = false,
138                                     bool quoted_strings = true,
139                                     bool escape_strings = false);
140 
141   void ArrayStart(const char* key,
142                   bool as_comment = false,
143                   std::string baseformat = "");
144   void ArrayEnd(const char* key,
145                 bool as_comment = false,
146                 std::string baseformat = "");
147 };
148 
149 #endif
150