Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / src / cplus / rcascii.h
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is the Netscape Portable Runtime (NSPR).
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998-2000
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 /*
39 ** Class definitions to format ASCII data.
40 */
41
42 #if defined(_RCASCII_H)
43 #else
44 #define _RCASCII_H
45
46 /*
47 ** RCFormatStuff
48 **  This class maintains no state - that is the responsibility of
49 **  the class' client. For each call to Sx_printf(), the StuffFunction
50 **  will be called for each embedded "%" in the 'fmt' string and once
51 **  for each interveaning literal.
52 */
53 class PR_IMPLEMENT(RCFormatStuff)
54 {
55 public:
56     RCFormatStuff();
57     virtual ~RCFormatStuff();
58
59     /*
60     ** Process the arbitrary argument list using as indicated by
61     ** the 'fmt' string. Each segment of the processing the stuff
62     ** function is called with the relavent translation.
63     */
64     virtual PRInt32 Sx_printf(void *state, const char *fmt, ...);
65
66     /*
67     ** The 'state' argument is not processed by the runtime. It
68     ** is merely passed from the Sx_printf() call. It is intended
69     ** to be used by the client to figure out what to do with the
70     ** new string.
71     **
72     ** The new string ('stuff') is ASCII translation driven by the
73     ** Sx_printf()'s 'fmt' string. It is not guaranteed to be a null
74     ** terminated string.
75     **
76     ** The return value is the number of bytes copied from the 'stuff'
77     ** string. It is accumulated for each of the calls to the stuff
78     ** function and returned from the original caller of Sx_printf().
79     */
80     virtual PRSize StuffFunction(
81         void *state, const char *stuff, PRSize stufflen) = 0;
82 };  /* RCFormatStuff */
83
84
85 /*
86 ** RCFormatBuffer
87 **  The caller is supplying the buffer, the runtime is doing all
88 **  the conversion. The object contains no state, so is reusable
89 **  and reentrant.
90 */
91 class PR_IMPLEMENT(RCFormatBuffer): public RCFormatStuff
92 {
93 public:
94     RCFormatBuffer();
95     virtual ~RCFormatBuffer();
96
97     /*
98     ** Format the trailing arguments as indicated by the 'fmt'
99     ** string. Put the result in 'buffer'. Return the number
100     ** of bytes moved into 'buffer'. 'buffer' will always be
101     ** a properly terminated string even if the convresion fails.
102     */
103     virtual PRSize Sn_printf(
104         char *buffer, PRSize length, const char *fmt, ...);
105
106     virtual char *Sm_append(char *buffer, const char *fmt, ...);
107
108 private:
109     /*
110     ** This class overrides the stuff function, does not preserve
111     ** its virtual-ness and no longer allows the clients to call
112     ** it in the clear. In other words, it is now the implementation
113     ** for this class.
114     */
115     PRSize StuffFunction(void*, const char*, PRSize);
116         
117 };  /* RCFormatBuffer */
118
119 /*
120 ** RCFormat
121 **  The runtime is supplying the buffer. The object has state - the
122 **  buffer. Each operation must run to completion before the object
123 **  can be reused. When it is, the buffer is reset (whatever that
124 **  means). The result of a conversion is available via the extractor.
125 **  After extracted, the memory still belongs to the class - if the
126 **  caller wants to retain or modify, it must first be copied.
127 */
128 class PR_IMPLEMENT(RCFormat): pubic RCFormatBuffer
129 {
130 public:
131     RCFormat();
132     virtual ~RCFormat();
133
134     /*
135     ** Translate the trailing arguments according to the 'fmt'
136     ** string and store the results in the object.
137     */
138     virtual PRSize Sm_printf(const char *fmt, ...);
139
140     /*
141     ** Extract the latest translation.
142     ** The object does not surrender the memory occupied by
143     ** the string. If the caller wishes to modify the data,
144     ** it must first be copied.
145     */
146     const char*();
147
148 private:
149     char *buffer;
150
151     RCFormat(const RCFormat&);
152     RCFormat& operator=(const RCFormat&);
153 }; /* RCFormat */
154
155 /*
156 ** RCPrint
157 **  The output is formatted and then written to an associated file
158 **  descriptor. The client can provide a suitable file descriptor
159 **  or can indicate that the output should be directed to one of
160 **  the well-known "console" devices.
161 */
162 class PR_IMPLEMENT(RCPrint): public RCFormat
163 {
164     virtual ~RCPrint();
165     RCPrint(RCIO* output);
166     RCPrint(RCFileIO::SpecialFile output);
167
168     virtual PRSize Printf(const char *fmt, ...);
169 private:
170     RCPrint();
171 };  /* RCPrint */
172
173 #endif /* defined(_RCASCII_H) */
174
175 /* RCAscii.h */