(file) Return to Formatter.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.24 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.11 //
  3 karl  1.19 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4            // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5            // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6 karl  1.16 // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.19 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8            // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9 karl  1.21 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.24 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12            // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.11 //
 14            // Permission is hereby granted, free of charge, to any person obtaining a copy
 15 kumpf 1.13 // of this software and associated documentation files (the "Software"), to
 16            // deal in the Software without restriction, including without limitation the
 17            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18 mike  1.11 // sell copies of the Software, and to permit persons to whom the Software is
 19            // furnished to do so, subject to the following conditions:
 20            // 
 21 kumpf 1.13 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22 mike  1.11 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24 kumpf 1.13 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27 mike  1.11 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29            //
 30            //==============================================================================
 31            //
 32            //%/////////////////////////////////////////////////////////////////////////////
 33            
 34            #ifndef Pegasus_Formatter_h
 35            #define Pegasus_Formatter_h
 36            
 37            #include <Pegasus/Common/Config.h>
 38            #include <Pegasus/Common/String.h>
 39 kumpf 1.14 #include <Pegasus/Common/Linkage.h>
 40 mike  1.22 #include <Pegasus/Common/StrLit.h>
 41 chuck 1.17 
 42 mike  1.11 PEGASUS_NAMESPACE_BEGIN
 43            
 44 kumpf 1.25 /**
 45 mike  1.11     Formatter is a class to build formatted strings from
 46                strings that contain variable defintions.  The
 47                variable definitions in the strings are of the form
 48                $<int>
 49                
 50                where <int> is a single digit integer (0 - 9).
 51                
 52                The variable subsituted may be String, Boolean Integer, Unsigned Integer
 53                or  real.
 54                
 55                The format subsitution may be escaped by preceding the
 56                $ with a \
 57                
 58                usage:
 59                Formatter::format (FormatString, variable0,.., variable9)
 60                
 61                Example:
 62                <pre>
 63                int total = 4;
 64                int count = 2;
 65                String name = "Output"
 66 mike  1.11     String output = Formatter::format(
 67 kumpf 1.26         "total $0 average $1 on $2", 
 68                    total,
 69                    total/count,
 70                    name);
 71            
 72 mike  1.11     produces the string
 73 kumpf 1.26 
 74 mike  1.11       "total 4 average 2 on Output"
 75                
 76                </pre>
 77            */
 78            class PEGASUS_COMMON_LINKAGE Formatter
 79            {
 80            public:
 81            
 82                class Arg
 83                {
 84                public:
 85            
 86 kumpf 1.26         enum Type { VOIDT, STRING, CSTRLIT, BOOLEAN, INTEGER, 
 87                        UINTEGER, LINTEGER, ULINTEGER, REAL };
 88            
 89                    Arg() : _type(VOIDT)
 90                    {
 91                    }
 92            
 93                    Arg(const String& x) : _string(x), _type(STRING)
 94                    {
 95                    }
 96            
 97                    Arg(const char* x) : _string(x), _type(STRING)
 98                    {
 99                    }
100            
101                    Arg(const StrLit& x) : _cstrlit(&x), _type(CSTRLIT)
102                    {
103                    }
104            
105                    Arg(Boolean x) : _boolean(x), _type(BOOLEAN)
106                    {
107 kumpf 1.26         }
108            
109                    Arg(Sint32 x) : _integer(x), _type(INTEGER)
110                    {
111                    }
112            
113                    Arg(Uint32 x) : _uinteger(x), _type(UINTEGER)
114                    {
115                    }
116            
117                    Arg(Sint64 x) : _lInteger(x), _type(LINTEGER)
118                    {
119                    }
120            
121                    Arg(Uint64 x) : _lUInteger(x), _type(ULINTEGER)
122                    {
123                    }
124            
125                    Arg(Real64 x) : _real(x), _type(REAL)
126                    {
127                    }
128 kumpf 1.26 
129                    String toString() const;
130            
131                    void appendToString(String& out) const;
132 mike  1.11 
133 kumpf 1.26         friend class MessageLoader;
134 mike  1.11 
135                private:
136            
137 kumpf 1.26         String _string;
138 mike  1.11 
139 kumpf 1.26         union
140                    {
141                        Sint32 _integer;
142                        Uint32 _uinteger;
143                        Real64 _real;
144                        int _boolean;
145                        Sint64 _lInteger;
146                        Uint64 _lUInteger;
147                        const StrLit* _cstrlit;
148                    };
149 mike  1.11 
150 kumpf 1.26         Type _type;
151 mike  1.11     };
152 karl  1.20 
153 kumpf 1.26     /** Format function for the formatter
154 mike  1.11     */
155                static String format(
156 kumpf 1.26         const String& formatString,
157                    const Arg& arg0 = Formatter::DEFAULT_ARG,
158                    const Arg& arg1 = Formatter::DEFAULT_ARG,
159                    const Arg& arg2 = Formatter::DEFAULT_ARG,
160                    const Arg& arg3 = Formatter::DEFAULT_ARG,
161                    const Arg& arg4 = Formatter::DEFAULT_ARG,
162                    const Arg& arg5 = Formatter::DEFAULT_ARG,
163                    const Arg& arg6 = Formatter::DEFAULT_ARG,
164                    const Arg& arg7 = Formatter::DEFAULT_ARG,
165                    const Arg& arg8 = Formatter::DEFAULT_ARG,
166                    const Arg& arg9 = Formatter::DEFAULT_ARG);
167 karl  1.20 
168                static const Formatter::Arg DEFAULT_ARG;
169 mike  1.11 };
170            
171            PEGASUS_NAMESPACE_END
172 chuck 1.17 
173 mike  1.11 #endif /* Pegasus_Formatter_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2