Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2006 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #ifndef ANDROID_TEXTOUTPUT_H |
michael@0 | 18 | #define ANDROID_TEXTOUTPUT_H |
michael@0 | 19 | |
michael@0 | 20 | #include <utils/Errors.h> |
michael@0 | 21 | |
michael@0 | 22 | #include <stdint.h> |
michael@0 | 23 | #include <string.h> |
michael@0 | 24 | |
michael@0 | 25 | // --------------------------------------------------------------------------- |
michael@0 | 26 | namespace android { |
michael@0 | 27 | |
michael@0 | 28 | class TextOutput |
michael@0 | 29 | { |
michael@0 | 30 | public: |
michael@0 | 31 | TextOutput(); |
michael@0 | 32 | virtual ~TextOutput(); |
michael@0 | 33 | |
michael@0 | 34 | virtual status_t print(const char* txt, size_t len) = 0; |
michael@0 | 35 | virtual void moveIndent(int delta) = 0; |
michael@0 | 36 | |
michael@0 | 37 | class Bundle { |
michael@0 | 38 | public: |
michael@0 | 39 | inline Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); } |
michael@0 | 40 | inline ~Bundle() { mTO.popBundle(); } |
michael@0 | 41 | private: |
michael@0 | 42 | TextOutput& mTO; |
michael@0 | 43 | }; |
michael@0 | 44 | |
michael@0 | 45 | virtual void pushBundle() = 0; |
michael@0 | 46 | virtual void popBundle() = 0; |
michael@0 | 47 | }; |
michael@0 | 48 | |
michael@0 | 49 | // --------------------------------------------------------------------------- |
michael@0 | 50 | |
michael@0 | 51 | // Text output stream for printing to the log (via utils/Log.h). |
michael@0 | 52 | extern TextOutput& alog; |
michael@0 | 53 | |
michael@0 | 54 | // Text output stream for printing to stdout. |
michael@0 | 55 | extern TextOutput& aout; |
michael@0 | 56 | |
michael@0 | 57 | // Text output stream for printing to stderr. |
michael@0 | 58 | extern TextOutput& aerr; |
michael@0 | 59 | |
michael@0 | 60 | typedef TextOutput& (*TextOutputManipFunc)(TextOutput&); |
michael@0 | 61 | |
michael@0 | 62 | TextOutput& endl(TextOutput& to); |
michael@0 | 63 | TextOutput& indent(TextOutput& to); |
michael@0 | 64 | TextOutput& dedent(TextOutput& to); |
michael@0 | 65 | |
michael@0 | 66 | TextOutput& operator<<(TextOutput& to, const char* str); |
michael@0 | 67 | TextOutput& operator<<(TextOutput& to, char); // writes raw character |
michael@0 | 68 | TextOutput& operator<<(TextOutput& to, bool); |
michael@0 | 69 | TextOutput& operator<<(TextOutput& to, int); |
michael@0 | 70 | TextOutput& operator<<(TextOutput& to, long); |
michael@0 | 71 | TextOutput& operator<<(TextOutput& to, unsigned int); |
michael@0 | 72 | TextOutput& operator<<(TextOutput& to, unsigned long); |
michael@0 | 73 | TextOutput& operator<<(TextOutput& to, long long); |
michael@0 | 74 | TextOutput& operator<<(TextOutput& to, unsigned long long); |
michael@0 | 75 | TextOutput& operator<<(TextOutput& to, float); |
michael@0 | 76 | TextOutput& operator<<(TextOutput& to, double); |
michael@0 | 77 | TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func); |
michael@0 | 78 | TextOutput& operator<<(TextOutput& to, const void*); |
michael@0 | 79 | |
michael@0 | 80 | class TypeCode |
michael@0 | 81 | { |
michael@0 | 82 | public: |
michael@0 | 83 | inline TypeCode(uint32_t code); |
michael@0 | 84 | inline ~TypeCode(); |
michael@0 | 85 | |
michael@0 | 86 | inline uint32_t typeCode() const; |
michael@0 | 87 | |
michael@0 | 88 | private: |
michael@0 | 89 | uint32_t mCode; |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | TextOutput& operator<<(TextOutput& to, const TypeCode& val); |
michael@0 | 93 | |
michael@0 | 94 | class HexDump |
michael@0 | 95 | { |
michael@0 | 96 | public: |
michael@0 | 97 | HexDump(const void *buf, size_t size, size_t bytesPerLine=16); |
michael@0 | 98 | inline ~HexDump(); |
michael@0 | 99 | |
michael@0 | 100 | inline HexDump& setBytesPerLine(size_t bytesPerLine); |
michael@0 | 101 | inline HexDump& setSingleLineCutoff(int32_t bytes); |
michael@0 | 102 | inline HexDump& setAlignment(size_t alignment); |
michael@0 | 103 | inline HexDump& setCArrayStyle(bool enabled); |
michael@0 | 104 | |
michael@0 | 105 | inline const void* buffer() const; |
michael@0 | 106 | inline size_t size() const; |
michael@0 | 107 | inline size_t bytesPerLine() const; |
michael@0 | 108 | inline int32_t singleLineCutoff() const; |
michael@0 | 109 | inline size_t alignment() const; |
michael@0 | 110 | inline bool carrayStyle() const; |
michael@0 | 111 | |
michael@0 | 112 | private: |
michael@0 | 113 | const void* mBuffer; |
michael@0 | 114 | size_t mSize; |
michael@0 | 115 | size_t mBytesPerLine; |
michael@0 | 116 | int32_t mSingleLineCutoff; |
michael@0 | 117 | size_t mAlignment; |
michael@0 | 118 | bool mCArrayStyle; |
michael@0 | 119 | }; |
michael@0 | 120 | |
michael@0 | 121 | TextOutput& operator<<(TextOutput& to, const HexDump& val); |
michael@0 | 122 | |
michael@0 | 123 | // --------------------------------------------------------------------------- |
michael@0 | 124 | // No user servicable parts below. |
michael@0 | 125 | |
michael@0 | 126 | inline TextOutput& endl(TextOutput& to) |
michael@0 | 127 | { |
michael@0 | 128 | to.print("\n", 1); |
michael@0 | 129 | return to; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | inline TextOutput& indent(TextOutput& to) |
michael@0 | 133 | { |
michael@0 | 134 | to.moveIndent(1); |
michael@0 | 135 | return to; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | inline TextOutput& dedent(TextOutput& to) |
michael@0 | 139 | { |
michael@0 | 140 | to.moveIndent(-1); |
michael@0 | 141 | return to; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | inline TextOutput& operator<<(TextOutput& to, const char* str) |
michael@0 | 145 | { |
michael@0 | 146 | to.print(str, strlen(str)); |
michael@0 | 147 | return to; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | inline TextOutput& operator<<(TextOutput& to, char c) |
michael@0 | 151 | { |
michael@0 | 152 | to.print(&c, 1); |
michael@0 | 153 | return to; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func) |
michael@0 | 157 | { |
michael@0 | 158 | return (*func)(to); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | inline TypeCode::TypeCode(uint32_t code) : mCode(code) { } |
michael@0 | 162 | inline TypeCode::~TypeCode() { } |
michael@0 | 163 | inline uint32_t TypeCode::typeCode() const { return mCode; } |
michael@0 | 164 | |
michael@0 | 165 | inline HexDump::~HexDump() { } |
michael@0 | 166 | |
michael@0 | 167 | inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) { |
michael@0 | 168 | mBytesPerLine = bytesPerLine; return *this; |
michael@0 | 169 | } |
michael@0 | 170 | inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) { |
michael@0 | 171 | mSingleLineCutoff = bytes; return *this; |
michael@0 | 172 | } |
michael@0 | 173 | inline HexDump& HexDump::setAlignment(size_t alignment) { |
michael@0 | 174 | mAlignment = alignment; return *this; |
michael@0 | 175 | } |
michael@0 | 176 | inline HexDump& HexDump::setCArrayStyle(bool enabled) { |
michael@0 | 177 | mCArrayStyle = enabled; return *this; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | inline const void* HexDump::buffer() const { return mBuffer; } |
michael@0 | 181 | inline size_t HexDump::size() const { return mSize; } |
michael@0 | 182 | inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; } |
michael@0 | 183 | inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; } |
michael@0 | 184 | inline size_t HexDump::alignment() const { return mAlignment; } |
michael@0 | 185 | inline bool HexDump::carrayStyle() const { return mCArrayStyle; } |
michael@0 | 186 | |
michael@0 | 187 | // --------------------------------------------------------------------------- |
michael@0 | 188 | }; // namespace android |
michael@0 | 189 | |
michael@0 | 190 | #endif // ANDROID_TEXTOUTPUT_H |