| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
/* |
| 7 |
** Class definitions to format ASCII data. |
| 8 |
*/ |
| 9 |
|
| 10 |
#if defined(_RCASCII_H) |
| 11 |
#else |
| 12 |
#define _RCASCII_H |
| 13 |
|
| 14 |
/* |
| 15 |
** RCFormatStuff |
| 16 |
** This class maintains no state - that is the responsibility of |
| 17 |
** the class' client. For each call to Sx_printf(), the StuffFunction |
| 18 |
** will be called for each embedded "%" in the 'fmt' string and once |
| 19 |
** for each interveaning literal. |
| 20 |
*/ |
| 21 |
class PR_IMPLEMENT(RCFormatStuff) |
| 22 |
{ |
| 23 |
public: |
| 24 |
RCFormatStuff(); |
| 25 |
virtual ~RCFormatStuff(); |
| 26 |
|
| 27 |
/* |
| 28 |
** Process the arbitrary argument list using as indicated by |
| 29 |
** the 'fmt' string. Each segment of the processing the stuff |
| 30 |
** function is called with the relavent translation. |
| 31 |
*/ |
| 32 |
virtual PRInt32 Sx_printf(void *state, const char *fmt, ...); |
| 33 |
|
| 34 |
/* |
| 35 |
** The 'state' argument is not processed by the runtime. It |
| 36 |
** is merely passed from the Sx_printf() call. It is intended |
| 37 |
** to be used by the client to figure out what to do with the |
| 38 |
** new string. |
| 39 |
** |
| 40 |
** The new string ('stuff') is ASCII translation driven by the |
| 41 |
** Sx_printf()'s 'fmt' string. It is not guaranteed to be a null |
| 42 |
** terminated string. |
| 43 |
** |
| 44 |
** The return value is the number of bytes copied from the 'stuff' |
| 45 |
** string. It is accumulated for each of the calls to the stuff |
| 46 |
** function and returned from the original caller of Sx_printf(). |
| 47 |
*/ |
| 48 |
virtual PRSize StuffFunction( |
| 49 |
void *state, const char *stuff, PRSize stufflen) = 0; |
| 50 |
}; /* RCFormatStuff */ |
| 51 |
|
| 52 |
|
| 53 |
/* |
| 54 |
** RCFormatBuffer |
| 55 |
** The caller is supplying the buffer, the runtime is doing all |
| 56 |
** the conversion. The object contains no state, so is reusable |
| 57 |
** and reentrant. |
| 58 |
*/ |
| 59 |
class PR_IMPLEMENT(RCFormatBuffer): public RCFormatStuff |
| 60 |
{ |
| 61 |
public: |
| 62 |
RCFormatBuffer(); |
| 63 |
virtual ~RCFormatBuffer(); |
| 64 |
|
| 65 |
/* |
| 66 |
** Format the trailing arguments as indicated by the 'fmt' |
| 67 |
** string. Put the result in 'buffer'. Return the number |
| 68 |
** of bytes moved into 'buffer'. 'buffer' will always be |
| 69 |
** a properly terminated string even if the convresion fails. |
| 70 |
*/ |
| 71 |
virtual PRSize Sn_printf( |
| 72 |
char *buffer, PRSize length, const char *fmt, ...); |
| 73 |
|
| 74 |
virtual char *Sm_append(char *buffer, const char *fmt, ...); |
| 75 |
|
| 76 |
private: |
| 77 |
/* |
| 78 |
** This class overrides the stuff function, does not preserve |
| 79 |
** its virtual-ness and no longer allows the clients to call |
| 80 |
** it in the clear. In other words, it is now the implementation |
| 81 |
** for this class. |
| 82 |
*/ |
| 83 |
PRSize StuffFunction(void*, const char*, PRSize); |
| 84 |
|
| 85 |
}; /* RCFormatBuffer */ |
| 86 |
|
| 87 |
/* |
| 88 |
** RCFormat |
| 89 |
** The runtime is supplying the buffer. The object has state - the |
| 90 |
** buffer. Each operation must run to completion before the object |
| 91 |
** can be reused. When it is, the buffer is reset (whatever that |
| 92 |
** means). The result of a conversion is available via the extractor. |
| 93 |
** After extracted, the memory still belongs to the class - if the |
| 94 |
** caller wants to retain or modify, it must first be copied. |
| 95 |
*/ |
| 96 |
class PR_IMPLEMENT(RCFormat): pubic RCFormatBuffer |
| 97 |
{ |
| 98 |
public: |
| 99 |
RCFormat(); |
| 100 |
virtual ~RCFormat(); |
| 101 |
|
| 102 |
/* |
| 103 |
** Translate the trailing arguments according to the 'fmt' |
| 104 |
** string and store the results in the object. |
| 105 |
*/ |
| 106 |
virtual PRSize Sm_printf(const char *fmt, ...); |
| 107 |
|
| 108 |
/* |
| 109 |
** Extract the latest translation. |
| 110 |
** The object does not surrender the memory occupied by |
| 111 |
** the string. If the caller wishes to modify the data, |
| 112 |
** it must first be copied. |
| 113 |
*/ |
| 114 |
const char*(); |
| 115 |
|
| 116 |
private: |
| 117 |
char *buffer; |
| 118 |
|
| 119 |
RCFormat(const RCFormat&); |
| 120 |
RCFormat& operator=(const RCFormat&); |
| 121 |
}; /* RCFormat */ |
| 122 |
|
| 123 |
/* |
| 124 |
** RCPrint |
| 125 |
** The output is formatted and then written to an associated file |
| 126 |
** descriptor. The client can provide a suitable file descriptor |
| 127 |
** or can indicate that the output should be directed to one of |
| 128 |
** the well-known "console" devices. |
| 129 |
*/ |
| 130 |
class PR_IMPLEMENT(RCPrint): public RCFormat |
| 131 |
{ |
| 132 |
virtual ~RCPrint(); |
| 133 |
RCPrint(RCIO* output); |
| 134 |
RCPrint(RCFileIO::SpecialFile output); |
| 135 |
|
| 136 |
virtual PRSize Printf(const char *fmt, ...); |
| 137 |
private: |
| 138 |
RCPrint(); |
| 139 |
}; /* RCPrint */ |
| 140 |
|
| 141 |
#endif /* defined(_RCASCII_H) */ |
| 142 |
|
| 143 |
/* RCAscii.h */ |