michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** Class definitions to format ASCII data. michael@0: */ michael@0: michael@0: #if defined(_RCASCII_H) michael@0: #else michael@0: #define _RCASCII_H michael@0: michael@0: /* michael@0: ** RCFormatStuff michael@0: ** This class maintains no state - that is the responsibility of michael@0: ** the class' client. For each call to Sx_printf(), the StuffFunction michael@0: ** will be called for each embedded "%" in the 'fmt' string and once michael@0: ** for each interveaning literal. michael@0: */ michael@0: class PR_IMPLEMENT(RCFormatStuff) michael@0: { michael@0: public: michael@0: RCFormatStuff(); michael@0: virtual ~RCFormatStuff(); michael@0: michael@0: /* michael@0: ** Process the arbitrary argument list using as indicated by michael@0: ** the 'fmt' string. Each segment of the processing the stuff michael@0: ** function is called with the relavent translation. michael@0: */ michael@0: virtual PRInt32 Sx_printf(void *state, const char *fmt, ...); michael@0: michael@0: /* michael@0: ** The 'state' argument is not processed by the runtime. It michael@0: ** is merely passed from the Sx_printf() call. It is intended michael@0: ** to be used by the client to figure out what to do with the michael@0: ** new string. michael@0: ** michael@0: ** The new string ('stuff') is ASCII translation driven by the michael@0: ** Sx_printf()'s 'fmt' string. It is not guaranteed to be a null michael@0: ** terminated string. michael@0: ** michael@0: ** The return value is the number of bytes copied from the 'stuff' michael@0: ** string. It is accumulated for each of the calls to the stuff michael@0: ** function and returned from the original caller of Sx_printf(). michael@0: */ michael@0: virtual PRSize StuffFunction( michael@0: void *state, const char *stuff, PRSize stufflen) = 0; michael@0: }; /* RCFormatStuff */ michael@0: michael@0: michael@0: /* michael@0: ** RCFormatBuffer michael@0: ** The caller is supplying the buffer, the runtime is doing all michael@0: ** the conversion. The object contains no state, so is reusable michael@0: ** and reentrant. michael@0: */ michael@0: class PR_IMPLEMENT(RCFormatBuffer): public RCFormatStuff michael@0: { michael@0: public: michael@0: RCFormatBuffer(); michael@0: virtual ~RCFormatBuffer(); michael@0: michael@0: /* michael@0: ** Format the trailing arguments as indicated by the 'fmt' michael@0: ** string. Put the result in 'buffer'. Return the number michael@0: ** of bytes moved into 'buffer'. 'buffer' will always be michael@0: ** a properly terminated string even if the convresion fails. michael@0: */ michael@0: virtual PRSize Sn_printf( michael@0: char *buffer, PRSize length, const char *fmt, ...); michael@0: michael@0: virtual char *Sm_append(char *buffer, const char *fmt, ...); michael@0: michael@0: private: michael@0: /* michael@0: ** This class overrides the stuff function, does not preserve michael@0: ** its virtual-ness and no longer allows the clients to call michael@0: ** it in the clear. In other words, it is now the implementation michael@0: ** for this class. michael@0: */ michael@0: PRSize StuffFunction(void*, const char*, PRSize); michael@0: michael@0: }; /* RCFormatBuffer */ michael@0: michael@0: /* michael@0: ** RCFormat michael@0: ** The runtime is supplying the buffer. The object has state - the michael@0: ** buffer. Each operation must run to completion before the object michael@0: ** can be reused. When it is, the buffer is reset (whatever that michael@0: ** means). The result of a conversion is available via the extractor. michael@0: ** After extracted, the memory still belongs to the class - if the michael@0: ** caller wants to retain or modify, it must first be copied. michael@0: */ michael@0: class PR_IMPLEMENT(RCFormat): pubic RCFormatBuffer michael@0: { michael@0: public: michael@0: RCFormat(); michael@0: virtual ~RCFormat(); michael@0: michael@0: /* michael@0: ** Translate the trailing arguments according to the 'fmt' michael@0: ** string and store the results in the object. michael@0: */ michael@0: virtual PRSize Sm_printf(const char *fmt, ...); michael@0: michael@0: /* michael@0: ** Extract the latest translation. michael@0: ** The object does not surrender the memory occupied by michael@0: ** the string. If the caller wishes to modify the data, michael@0: ** it must first be copied. michael@0: */ michael@0: const char*(); michael@0: michael@0: private: michael@0: char *buffer; michael@0: michael@0: RCFormat(const RCFormat&); michael@0: RCFormat& operator=(const RCFormat&); michael@0: }; /* RCFormat */ michael@0: michael@0: /* michael@0: ** RCPrint michael@0: ** The output is formatted and then written to an associated file michael@0: ** descriptor. The client can provide a suitable file descriptor michael@0: ** or can indicate that the output should be directed to one of michael@0: ** the well-known "console" devices. michael@0: */ michael@0: class PR_IMPLEMENT(RCPrint): public RCFormat michael@0: { michael@0: virtual ~RCPrint(); michael@0: RCPrint(RCIO* output); michael@0: RCPrint(RCFileIO::SpecialFile output); michael@0: michael@0: virtual PRSize Printf(const char *fmt, ...); michael@0: private: michael@0: RCPrint(); michael@0: }; /* RCPrint */ michael@0: michael@0: #endif /* defined(_RCASCII_H) */ michael@0: michael@0: /* RCAscii.h */