]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
9f95a23c | 2 | // vim: ts=8 sw=2 smarttab ft=cpp |
7c673cae | 3 | |
1e59de90 | 4 | #pragma once |
7c673cae FG |
5 | |
6 | #include "common/Formatter.h" | |
7 | ||
8 | #include <list> | |
9 | #include <stdint.h> | |
10 | #include <string> | |
11 | #include <ostream> | |
12 | ||
13 | struct plain_stack_entry { | |
14 | int size; | |
15 | bool is_array; | |
16 | }; | |
17 | ||
18 | /* FIXME: this class is mis-named. | |
19 | * FIXME: This was a hack to send certain swift messages. | |
20 | * There is a much better way to do this. | |
21 | */ | |
22 | class RGWFormatter_Plain : public Formatter { | |
23 | void reset_buf(); | |
24 | public: | |
25 | explicit RGWFormatter_Plain(bool use_kv = false); | |
26 | ~RGWFormatter_Plain() override; | |
27 | ||
28 | void set_status(int status, const char* status_name) override {}; | |
29 | void output_header() override {}; | |
30 | void output_footer() override {}; | |
181888fb | 31 | void enable_line_break() override {}; |
20effc67 | 32 | void flush(std::ostream& os) override; |
7c673cae FG |
33 | void reset() override; |
34 | ||
9f95a23c TL |
35 | void open_array_section(std::string_view name) override; |
36 | void open_array_section_in_ns(std::string_view name, const char *ns) override; | |
37 | void open_object_section(std::string_view name) override; | |
38 | void open_object_section_in_ns(std::string_view name, const char *ns) override; | |
7c673cae | 39 | void close_section() override; |
aee94f69 | 40 | void dump_null(std::string_view name) override; |
9f95a23c TL |
41 | void dump_unsigned(std::string_view name, uint64_t u) override; |
42 | void dump_int(std::string_view name, int64_t u) override; | |
43 | void dump_float(std::string_view name, double d) override; | |
44 | void dump_string(std::string_view name, std::string_view s) override; | |
45 | std::ostream& dump_stream(std::string_view name) override; | |
46 | void dump_format_va(std::string_view name, const char *ns, bool quoted, const char *fmt, va_list ap) override; | |
7c673cae FG |
47 | int get_len() const override; |
48 | void write_raw_data(const char *data) override; | |
49 | ||
50 | private: | |
51 | void write_data(const char *fmt, ...); | |
9f95a23c | 52 | void dump_value_int(std::string_view name, const char *fmt, ...); |
7c673cae FG |
53 | |
54 | char *buf = nullptr; | |
55 | int len = 0; | |
56 | int max_len = 0; | |
57 | ||
58 | std::list<struct plain_stack_entry> stack; | |
59 | size_t min_stack_level = 0; | |
60 | bool use_kv; | |
61 | bool wrote_something = 0; | |
62 | }; | |
63 | ||
64 | ||
65 | /* This is a presentation layer. No logic inside, please. */ | |
66 | class RGWSwiftWebsiteListingFormatter { | |
67 | std::ostream& ss; | |
68 | const std::string prefix; | |
69 | protected: | |
70 | std::string format_name(const std::string& item_name) const; | |
71 | public: | |
72 | RGWSwiftWebsiteListingFormatter(std::ostream& ss, | |
73 | std::string prefix) | |
74 | : ss(ss), | |
75 | prefix(std::move(prefix)) { | |
76 | } | |
77 | ||
78 | /* The supplied css_path can be empty. In such situation a default, | |
79 | * embedded style sheet will be generated. */ | |
80 | void generate_header(const std::string& dir_path, | |
81 | const std::string& css_path); | |
82 | void generate_footer(); | |
83 | void dump_object(const rgw_bucket_dir_entry& objent); | |
84 | void dump_subdir(const std::string& name); | |
85 | }; | |
86 | ||
87 | ||
88 | class RGWFormatterFlusher { | |
89 | protected: | |
90 | Formatter *formatter; | |
91 | bool flushed; | |
92 | bool started; | |
93 | virtual void do_flush() = 0; | |
94 | virtual void do_start(int ret) {} | |
95 | void set_formatter(Formatter *f) { | |
96 | formatter = f; | |
97 | } | |
98 | public: | |
99 | explicit RGWFormatterFlusher(Formatter *f) : formatter(f), flushed(false), started(false) {} | |
100 | virtual ~RGWFormatterFlusher() {} | |
101 | ||
102 | void flush() { | |
103 | do_flush(); | |
104 | flushed = true; | |
105 | } | |
106 | ||
107 | virtual void start(int client_ret) { | |
108 | if (!started) | |
109 | do_start(client_ret); | |
110 | started = true; | |
111 | } | |
112 | ||
113 | Formatter *get_formatter() { return formatter; } | |
114 | bool did_flush() { return flushed; } | |
115 | bool did_start() { return started; } | |
116 | }; | |
117 | ||
118 | class RGWStreamFlusher : public RGWFormatterFlusher { | |
20effc67 | 119 | std::ostream& os; |
7c673cae FG |
120 | protected: |
121 | void do_flush() override { | |
122 | formatter->flush(os); | |
123 | } | |
124 | public: | |
20effc67 | 125 | RGWStreamFlusher(Formatter *f, std::ostream& _os) : RGWFormatterFlusher(f), os(_os) {} |
7c673cae FG |
126 | }; |
127 | ||
11fdf7f2 TL |
128 | class RGWNullFlusher : public RGWFormatterFlusher { |
129 | protected: | |
130 | void do_flush() override { | |
131 | } | |
132 | public: | |
133 | RGWNullFlusher() : RGWFormatterFlusher(nullptr) {} | |
134 | }; |