Back to index

lightning-sunbird  0.9+nobinonly
rcascii.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** Class definitions to format ASCII data.
00040 */
00041 
00042 #if defined(_RCASCII_H)
00043 #else
00044 #define _RCASCII_H
00045 
00046 /*
00047 ** RCFormatStuff
00048 **  This class maintains no state - that is the responsibility of
00049 **  the class' client. For each call to Sx_printf(), the StuffFunction
00050 **  will be called for each embedded "%" in the 'fmt' string and once
00051 **  for each interveaning literal.
00052 */
00053 class PR_IMPLEMENT(RCFormatStuff)
00054 {
00055 public:
00056     RCFormatStuff();
00057     virtual ~RCFormatStuff();
00058 
00059     /*
00060     ** Process the arbitrary argument list using as indicated by
00061     ** the 'fmt' string. Each segment of the processing the stuff
00062     ** function is called with the relavent translation.
00063     */
00064     virtual PRInt32 Sx_printf(void *state, const char *fmt, ...);
00065 
00066     /*
00067     ** The 'state' argument is not processed by the runtime. It
00068     ** is merely passed from the Sx_printf() call. It is intended
00069     ** to be used by the client to figure out what to do with the
00070     ** new string.
00071     **
00072     ** The new string ('stuff') is ASCII translation driven by the
00073     ** Sx_printf()'s 'fmt' string. It is not guaranteed to be a null
00074     ** terminated string.
00075     **
00076     ** The return value is the number of bytes copied from the 'stuff'
00077     ** string. It is accumulated for each of the calls to the stuff
00078     ** function and returned from the original caller of Sx_printf().
00079     */
00080     virtual PRSize StuffFunction(
00081         void *state, const char *stuff, PRSize stufflen) = 0;
00082 };  /* RCFormatStuff */
00083 
00084 
00085 /*
00086 ** RCFormatBuffer
00087 **  The caller is supplying the buffer, the runtime is doing all
00088 **  the conversion. The object contains no state, so is reusable
00089 **  and reentrant.
00090 */
00091 class PR_IMPLEMENT(RCFormatBuffer): public RCFormatStuff
00092 {
00093 public:
00094     RCFormatBuffer();
00095     virtual ~RCFormatBuffer();
00096 
00097     /*
00098     ** Format the trailing arguments as indicated by the 'fmt'
00099     ** string. Put the result in 'buffer'. Return the number
00100     ** of bytes moved into 'buffer'. 'buffer' will always be
00101     ** a properly terminated string even if the convresion fails.
00102     */
00103     virtual PRSize Sn_printf(
00104         char *buffer, PRSize length, const char *fmt, ...);
00105 
00106     virtual char *Sm_append(char *buffer, const char *fmt, ...);
00107 
00108 private:
00109     /*
00110     ** This class overrides the stuff function, does not preserve
00111     ** its virtual-ness and no longer allows the clients to call
00112     ** it in the clear. In other words, it is now the implementation
00113     ** for this class.
00114     */
00115     PRSize StuffFunction(void*, const char*, PRSize);
00116         
00117 };  /* RCFormatBuffer */
00118 
00119 /*
00120 ** RCFormat
00121 **  The runtime is supplying the buffer. The object has state - the
00122 **  buffer. Each operation must run to completion before the object
00123 **  can be reused. When it is, the buffer is reset (whatever that
00124 **  means). The result of a conversion is available via the extractor.
00125 **  After extracted, the memory still belongs to the class - if the
00126 **  caller wants to retain or modify, it must first be copied.
00127 */
00128 class PR_IMPLEMENT(RCFormat): pubic RCFormatBuffer
00129 {
00130 public:
00131     RCFormat();
00132     virtual ~RCFormat();
00133 
00134     /*
00135     ** Translate the trailing arguments according to the 'fmt'
00136     ** string and store the results in the object.
00137     */
00138     virtual PRSize Sm_printf(const char *fmt, ...);
00139 
00140     /*
00141     ** Extract the latest translation.
00142     ** The object does not surrender the memory occupied by
00143     ** the string. If the caller wishes to modify the data,
00144     ** it must first be copied.
00145     */
00146     const char*();
00147 
00148 private:
00149     char *buffer;
00150 
00151     RCFormat(const RCFormat&);
00152     RCFormat& operator=(const RCFormat&);
00153 }; /* RCFormat */
00154 
00155 /*
00156 ** RCPrint
00157 **  The output is formatted and then written to an associated file
00158 **  descriptor. The client can provide a suitable file descriptor
00159 **  or can indicate that the output should be directed to one of
00160 **  the well-known "console" devices.
00161 */
00162 class PR_IMPLEMENT(RCPrint): public RCFormat
00163 {
00164     virtual ~RCPrint();
00165     RCPrint(RCIO* output);
00166     RCPrint(RCFileIO::SpecialFile output);
00167 
00168     virtual PRSize Printf(const char *fmt, ...);
00169 private:
00170     RCPrint();
00171 };  /* RCPrint */
00172 
00173 #endif /* defined(_RCASCII_H) */
00174 
00175 /* RCAscii.h */