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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2