blob: 5d22f38f9defe24aab8f895cb6e2401a8e7fdf04 [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef FORMATTER_H_
18
19#define FORMATTER_H_
20
21#include <android-base/macros.h>
22#include <string>
23
24namespace android {
25
26struct Formatter {
Andreas Huber881227d2016-08-02 14:20:21 -070027 // Assumes ownership of file. Directed to stdout if file == NULL.
28 Formatter(FILE *file = NULL);
29 ~Formatter();
Andreas Huberc9410c72016-07-28 12:18:40 -070030
31 void indent();
32 void unindent();
33
34 Formatter &operator<<(const std::string &out);
35 Formatter &operator<<(size_t n);
36
Andreas Huber0e00de42016-08-03 09:56:02 -070037 // Any substrings matching "space" will be stripped out of the output.
38 void setNamespace(const std::string &space);
39
Andreas Huberc9410c72016-07-28 12:18:40 -070040private:
Andreas Huber881227d2016-08-02 14:20:21 -070041 FILE *mFile;
Andreas Huberc9410c72016-07-28 12:18:40 -070042 size_t mIndentDepth;
43 bool mAtStartOfLine;
44
Andreas Huber0e00de42016-08-03 09:56:02 -070045 std::string mSpace;
46
47 void output(const std::string &text) const;
48
Andreas Huberc9410c72016-07-28 12:18:40 -070049 DISALLOW_COPY_AND_ASSIGN(Formatter);
50};
51
52} // namespace android
53
54#endif // FORMATTER_H_
55