TextStream.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_TEXTSTREAM_H
00014 #define _SUPPORT_TEXTSTREAM_H
00015 
00021 #include <support/ITextStream.h>
00022 #include <support/ByteStream.h>
00023 #include <support/Locker.h>
00024 #include <support/VectorIO.h>
00025 
00026 #if _SUPPORTS_NAMESPACE
00027 namespace palmos {
00028 namespace support {
00029 #endif
00030 
00035 
00036 enum {
00037     B_TEXT_OUTPUT_THREADED          = 0x00000001,   
00038     B_TEXT_OUTPUT_COLORED           = 0x00000002,   
00039     B_TEXT_OUTPUT_TAG_THREAD        = 0x00000004,   
00040     B_TEXT_OUTPUT_TAG_TEAM          = 0x00000008,   
00041     B_TEXT_OUTPUT_TAG_TIME          = 0x00000010,   
00042     B_TEXT_OUTPUT_FROM_ENV          = 0x10000000,   
00043 
00044     B_TEXT_OUTPUT_COLORED_RED       = 0x00010000 | B_TEXT_OUTPUT_COLORED,
00045     B_TEXT_OUTPUT_COLORED_GREEN     = 0x00020000 | B_TEXT_OUTPUT_COLORED,
00046     B_TEXT_OUTPUT_COLORED_BROWN     = 0x00030000 | B_TEXT_OUTPUT_COLORED,
00047     B_TEXT_OUTPUT_COLORED_BLUE      = 0x00040000 | B_TEXT_OUTPUT_COLORED,
00048     B_TEXT_OUTPUT_COLORED_PURPLE    = 0x00050000 | B_TEXT_OUTPUT_COLORED,
00049     B_TEXT_OUTPUT_COLORED_CYAN      = 0x00060000 | B_TEXT_OUTPUT_COLORED,
00050     B_TEXT_OUTPUT_COLORED_GRAY      = 0x00070000 | B_TEXT_OUTPUT_COLORED,
00051     B_TEXT_OUTPUT_COLORED_MASK      = 0x000f0000
00052 };
00053 
00054 #define TEXTOUTPUT_SMALL_STACK 1
00055 
00056 /*-----------------------------------------------------------------*/
00057 
00058 class BTextOutput : public ITextOutput
00059 {
00060     public:
00061 
00062                                         BTextOutput(const sptr<IByteOutput>& stream, uint32_t flags = 0);
00063         virtual                         ~BTextOutput();
00064 
00065         virtual status_t                Print(  const char *debugText,
00066                                                 ssize_t len = -1);
00067         virtual void                    MoveIndent( int32_t delta);
00068         
00069         virtual status_t                LogV(   const log_info& info,
00070                                                 const iovec *vector,
00071                                                 ssize_t count,
00072                                                 uint32_t flags = 0);
00073 
00074         virtual void                    Flush();
00075         virtual status_t                Sync();
00076 
00077                 class _IMPEXP_SUPPORT style_state
00078                 {
00079                 public:
00080                     int32_t tag;
00081                     int32_t indent;
00082                     int32_t startIndent;
00083                     int32_t front;
00084                     
00085                     int32_t buffering;
00086                     char*   buffer;
00087                     ssize_t bufferLen;
00088                     ssize_t bufferAvail;
00089                     
00090 #if TEXTOUTPUT_SMALL_STACK
00091                     // Formatting state to avoid using a lot of stack
00092                     // space.  This is really problematic, because it
00093                     // means this doesn't work at all if B_TEXT_OUTPUT_THREADED
00094                     // is not enabled and multiple threads are using
00095                     // the stream.  *sigh*
00096                     log_info    tmp_log;
00097                     SVectorIO   tmp_vecio;
00098                     char        tmp_prefix[64];
00099 #endif
00100 
00101                             style_state();
00102                             ~style_state();
00103                 };
00104     protected:
00105 
00106                                         BTextOutput(IByteOutput *This, uint32_t flags = 0);
00107     
00108         // TO DO: Implement LTextOutput and RTextOutput.
00109         virtual sptr<IBinder>       AsBinderImpl();
00110         virtual sptr<const IBinder> AsBinderImpl() const;
00111         
00112     private:
00113                 struct thread_styles;
00114                 
00115                 void                    InitStyles();
00116                 const char*             MakeIndent(style_state *style, int32_t* out_indent);
00117                 const char*             MakeIndent(int32_t* inout_indent);
00118                 style_state *           Style();
00119         static  void                    DeleteStyle(void *style);
00120 
00121                 IByteOutput *           m_stream;
00122                 uint32_t                m_flags;
00123                 
00124                 style_state             m_globalStyle;
00125                 thread_styles*          m_threadStyles;
00126                 int32_t                 m_nextTag;
00127                 
00128                 enum {
00129                     MAX_COLORS = 16
00130                 };
00131                 size_t                  m_numColors;
00132                 char                    m_colors[MAX_COLORS];
00133 
00134 private:
00135                                     BTextOutput(const BTextOutput& o);  // no implementation
00136         BTextOutput&                operator=(const BTextOutput& o);    // no implementation
00137 };
00138 
00139 /*-----------------------------------------------------------------*/
00140 
00141 class BTextInput : public ITextInput
00142 {
00143     public:
00144 
00145                                         BTextInput(const sptr<IByteInput>& stream, uint32_t flags = 0);
00146         virtual                         ~BTextInput();
00147 
00148         virtual ssize_t                 ReadChar();
00149         virtual ssize_t                 ReadLineBuffer(char* buffer, size_t size);
00150         // AppendLineTo: reads a line from the stream and appends it to
00151         // outString.
00152         virtual ssize_t                 AppendLineTo(SString* outString);
00153 
00154     protected:
00155 
00156                                         BTextInput(IByteInput *This, uint32_t flags = 0);
00157     
00158         // TO DO: Implement LTextOutput and RTextOutput.
00159         virtual sptr<IBinder>       AsBinderImpl();
00160         virtual sptr<const IBinder> AsBinderImpl() const;
00161         
00162     private:
00163                 IByteInput *            m_stream;
00164                 uint32_t                m_flags;
00165                 
00166                 SLocker                 m_lock;
00167                 char                    m_buffer[128];
00168                 ssize_t                 m_pos;
00169                 ssize_t                 m_avail;
00170                 bool                    m_inCR;
00171 };
00172 
00173 /*-----------------------------------------------------------------*/
00174 
00177 #if _SUPPORTS_NAMESPACE
00178 } } // namespace palmos::support
00179 #endif
00180 
00181 #endif /* _SUPPORT_TEXTSTREAM_H */