1 /*
2  * Copyright (C) 2006 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 ANDROID_HARDWARE_TEXTOUTPUT_H
18 #define ANDROID_HARDWARE_TEXTOUTPUT_H
19 
20 #include <utils/Errors.h>
21 #include <utils/String8.h>
22 
23 #include <stdint.h>
24 #include <string.h>
25 #include <sstream>
26 
27 // ---------------------------------------------------------------------------
28 namespace android {
29 namespace hardware {
30 
31 class TextOutput
32 {
33 public:
34                         TextOutput();
35     virtual             ~TextOutput();
36 
37     virtual status_t    print(const char* txt, size_t len) = 0;
38     virtual void        moveIndent(int delta) = 0;
39 
40     class Bundle {
41     public:
Bundle(TextOutput & to)42         inline explicit Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); }
~Bundle()43         inline ~Bundle() { mTO.popBundle(); }
44     private:
45         TextOutput&     mTO;
46     };
47 
48     virtual void        pushBundle() = 0;
49     virtual void        popBundle() = 0;
50 };
51 
52 // ---------------------------------------------------------------------------
53 
54 // Text output stream for printing to the log (via utils/Log.h).
55 extern TextOutput& alog;
56 
57 typedef TextOutput& (*TextOutputManipFunc)(TextOutput&);
58 
59 TextOutput& endl(TextOutput& to);
60 TextOutput& indent(TextOutput& to);
61 TextOutput& dedent(TextOutput& to);
62 
63 template<typename T>
64 TextOutput& operator<<(TextOutput& to, const T& val)
65 {
66     std::stringstream strbuf;
67     strbuf << val;
68     std::string str = strbuf.str();
69     to.print(str.c_str(), str.size());
70     return to;
71 }
72 
73 TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func);
74 
75 class TypeCode
76 {
77 public:
78     inline explicit TypeCode(uint32_t code);
79     inline ~TypeCode();
80 
81     inline uint32_t typeCode() const;
82 
83 private:
84     uint32_t mCode;
85 };
86 
87 TextOutput& operator<<(TextOutput& to, const TypeCode& val);
88 
89 class HexDump
90 {
91 public:
92     HexDump(const void *buf, size_t size, size_t bytesPerLine=16);
93     inline ~HexDump();
94 
95     inline HexDump& setBytesPerLine(size_t bytesPerLine);
96     inline HexDump& setSingleLineCutoff(int32_t bytes);
97     inline HexDump& setAlignment(size_t alignment);
98     inline HexDump& setCArrayStyle(bool enabled);
99 
100     inline const void* buffer() const;
101     inline size_t size() const;
102     inline size_t bytesPerLine() const;
103     inline int32_t singleLineCutoff() const;
104     inline size_t alignment() const;
105     inline bool carrayStyle() const;
106 
107 private:
108     const void* mBuffer;
109     size_t mSize;
110     size_t mBytesPerLine;
111     int32_t mSingleLineCutoff;
112     size_t mAlignment;
113     bool mCArrayStyle;
114 };
115 
116 TextOutput& operator<<(TextOutput& to, const HexDump& val);
117 inline TextOutput& operator<<(TextOutput& to,
118                               decltype(std::endl<char,
119                                        std::char_traits<char>>)
120                               /*val*/) {
121     endl(to);
122     return to;
123 }
124 
125 inline TextOutput& operator<<(TextOutput& to, const char &c)
126 {
127     to.print(&c, 1);
128     return to;
129 }
130 
131 inline TextOutput& operator<<(TextOutput& to, const bool &val)
132 {
133     if (val) to.print("true", 4);
134     else to.print("false", 5);
135     return to;
136 }
137 
138 inline TextOutput& operator<<(TextOutput& to, const String16& val)
139 {
140     to << String8(val).string();
141     return to;
142 }
143 
144 // ---------------------------------------------------------------------------
145 // No user servicable parts below.
146 
endl(TextOutput & to)147 inline TextOutput& endl(TextOutput& to)
148 {
149     to.print("\n", 1);
150     return to;
151 }
152 
indent(TextOutput & to)153 inline TextOutput& indent(TextOutput& to)
154 {
155     to.moveIndent(1);
156     return to;
157 }
158 
dedent(TextOutput & to)159 inline TextOutput& dedent(TextOutput& to)
160 {
161     to.moveIndent(-1);
162     return to;
163 }
164 
165 inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func)
166 {
167     return (*func)(to);
168 }
169 
TypeCode(uint32_t code)170 inline TypeCode::TypeCode(uint32_t code) : mCode(code) { }
~TypeCode()171 inline TypeCode::~TypeCode() { }
typeCode()172 inline uint32_t TypeCode::typeCode() const { return mCode; }
173 
~HexDump()174 inline HexDump::~HexDump() { }
175 
setBytesPerLine(size_t bytesPerLine)176 inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) {
177     mBytesPerLine = bytesPerLine; return *this;
178 }
setSingleLineCutoff(int32_t bytes)179 inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) {
180     mSingleLineCutoff = bytes; return *this;
181 }
setAlignment(size_t alignment)182 inline HexDump& HexDump::setAlignment(size_t alignment) {
183     mAlignment = alignment; return *this;
184 }
setCArrayStyle(bool enabled)185 inline HexDump& HexDump::setCArrayStyle(bool enabled) {
186     mCArrayStyle = enabled; return *this;
187 }
188 
buffer()189 inline const void* HexDump::buffer() const { return mBuffer; }
size()190 inline size_t HexDump::size() const { return mSize; }
bytesPerLine()191 inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; }
singleLineCutoff()192 inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; }
alignment()193 inline size_t HexDump::alignment() const { return mAlignment; }
carrayStyle()194 inline bool HexDump::carrayStyle() const { return mCArrayStyle; }
195 
196 // ---------------------------------------------------------------------------
197 } // namespace hardware
198 } // namespace android
199 
200 #endif // ANDROID_HARDWARE_TEXTOUTPUT_H
201