michael@0: /* michael@0: * Copyright (C) 2006 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef ANDROID_TEXTOUTPUT_H michael@0: #define ANDROID_TEXTOUTPUT_H michael@0: michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: namespace android { michael@0: michael@0: class TextOutput michael@0: { michael@0: public: michael@0: TextOutput(); michael@0: virtual ~TextOutput(); michael@0: michael@0: virtual status_t print(const char* txt, size_t len) = 0; michael@0: virtual void moveIndent(int delta) = 0; michael@0: michael@0: class Bundle { michael@0: public: michael@0: inline Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); } michael@0: inline ~Bundle() { mTO.popBundle(); } michael@0: private: michael@0: TextOutput& mTO; michael@0: }; michael@0: michael@0: virtual void pushBundle() = 0; michael@0: virtual void popBundle() = 0; michael@0: }; michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: // Text output stream for printing to the log (via utils/Log.h). michael@0: extern TextOutput& alog; michael@0: michael@0: // Text output stream for printing to stdout. michael@0: extern TextOutput& aout; michael@0: michael@0: // Text output stream for printing to stderr. michael@0: extern TextOutput& aerr; michael@0: michael@0: typedef TextOutput& (*TextOutputManipFunc)(TextOutput&); michael@0: michael@0: TextOutput& endl(TextOutput& to); michael@0: TextOutput& indent(TextOutput& to); michael@0: TextOutput& dedent(TextOutput& to); michael@0: michael@0: TextOutput& operator<<(TextOutput& to, const char* str); michael@0: TextOutput& operator<<(TextOutput& to, char); // writes raw character michael@0: TextOutput& operator<<(TextOutput& to, bool); michael@0: TextOutput& operator<<(TextOutput& to, int); michael@0: TextOutput& operator<<(TextOutput& to, long); michael@0: TextOutput& operator<<(TextOutput& to, unsigned int); michael@0: TextOutput& operator<<(TextOutput& to, unsigned long); michael@0: TextOutput& operator<<(TextOutput& to, long long); michael@0: TextOutput& operator<<(TextOutput& to, unsigned long long); michael@0: TextOutput& operator<<(TextOutput& to, float); michael@0: TextOutput& operator<<(TextOutput& to, double); michael@0: TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func); michael@0: TextOutput& operator<<(TextOutput& to, const void*); michael@0: michael@0: class TypeCode michael@0: { michael@0: public: michael@0: inline TypeCode(uint32_t code); michael@0: inline ~TypeCode(); michael@0: michael@0: inline uint32_t typeCode() const; michael@0: michael@0: private: michael@0: uint32_t mCode; michael@0: }; michael@0: michael@0: TextOutput& operator<<(TextOutput& to, const TypeCode& val); michael@0: michael@0: class HexDump michael@0: { michael@0: public: michael@0: HexDump(const void *buf, size_t size, size_t bytesPerLine=16); michael@0: inline ~HexDump(); michael@0: michael@0: inline HexDump& setBytesPerLine(size_t bytesPerLine); michael@0: inline HexDump& setSingleLineCutoff(int32_t bytes); michael@0: inline HexDump& setAlignment(size_t alignment); michael@0: inline HexDump& setCArrayStyle(bool enabled); michael@0: michael@0: inline const void* buffer() const; michael@0: inline size_t size() const; michael@0: inline size_t bytesPerLine() const; michael@0: inline int32_t singleLineCutoff() const; michael@0: inline size_t alignment() const; michael@0: inline bool carrayStyle() const; michael@0: michael@0: private: michael@0: const void* mBuffer; michael@0: size_t mSize; michael@0: size_t mBytesPerLine; michael@0: int32_t mSingleLineCutoff; michael@0: size_t mAlignment; michael@0: bool mCArrayStyle; michael@0: }; michael@0: michael@0: TextOutput& operator<<(TextOutput& to, const HexDump& val); michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: // No user servicable parts below. michael@0: michael@0: inline TextOutput& endl(TextOutput& to) michael@0: { michael@0: to.print("\n", 1); michael@0: return to; michael@0: } michael@0: michael@0: inline TextOutput& indent(TextOutput& to) michael@0: { michael@0: to.moveIndent(1); michael@0: return to; michael@0: } michael@0: michael@0: inline TextOutput& dedent(TextOutput& to) michael@0: { michael@0: to.moveIndent(-1); michael@0: return to; michael@0: } michael@0: michael@0: inline TextOutput& operator<<(TextOutput& to, const char* str) michael@0: { michael@0: to.print(str, strlen(str)); michael@0: return to; michael@0: } michael@0: michael@0: inline TextOutput& operator<<(TextOutput& to, char c) michael@0: { michael@0: to.print(&c, 1); michael@0: return to; michael@0: } michael@0: michael@0: inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func) michael@0: { michael@0: return (*func)(to); michael@0: } michael@0: michael@0: inline TypeCode::TypeCode(uint32_t code) : mCode(code) { } michael@0: inline TypeCode::~TypeCode() { } michael@0: inline uint32_t TypeCode::typeCode() const { return mCode; } michael@0: michael@0: inline HexDump::~HexDump() { } michael@0: michael@0: inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) { michael@0: mBytesPerLine = bytesPerLine; return *this; michael@0: } michael@0: inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) { michael@0: mSingleLineCutoff = bytes; return *this; michael@0: } michael@0: inline HexDump& HexDump::setAlignment(size_t alignment) { michael@0: mAlignment = alignment; return *this; michael@0: } michael@0: inline HexDump& HexDump::setCArrayStyle(bool enabled) { michael@0: mCArrayStyle = enabled; return *this; michael@0: } michael@0: michael@0: inline const void* HexDump::buffer() const { return mBuffer; } michael@0: inline size_t HexDump::size() const { return mSize; } michael@0: inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; } michael@0: inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; } michael@0: inline size_t HexDump::alignment() const { return mAlignment; } michael@0: inline bool HexDump::carrayStyle() const { return mCArrayStyle; } michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: }; // namespace android michael@0: michael@0: #endif // ANDROID_TEXTOUTPUT_H