1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* 3 * The contents of this file are subject to the Mozilla Public 4 * License Version 1.1 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of 6 * the License at http://www.mozilla.org/MPL/ 7 * 8 * Software distributed under the License is distributed on an "AS 9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 10 * implied. See the License for the specific language governing 11 * rights and limitations under the License. 12 * 13 * The Original Code is the Netscape Portable Runtime (NSPR). 14 * 15 * The Initial Developer of the Original Code is Netscape 16 * Communications Corporation. Portions created by Netscape are 17 * Copyright (C) 1998-2000 Netscape Communications Corporation. All 18 * Rights Reserved. 19 * 20 * Contributor(s): 21 * 22 * Alternatively, the contents of this file may be used under the 23 * terms of the GNU General Public License Version 2 or later (the 24 * "GPL"), in which case the provisions of the GPL are applicable 25 * instead of those above. If you wish to allow use of your 26 * version of this file only under the terms of the GPL and not to 27 * allow others to use your version of this file under the MPL, 28 * indicate your decision by deleting the provisions above and 29 * replace them with the notice and other provisions required by 30 * the GPL. If you do not delete the provisions above, a recipient 31 * may use your version of this file under either the MPL or the 32 * GPL. 33 */ 34 35 #ifndef prprf_h___ 36 #define prprf_h___ 37 38 /* 39 ** API for PR printf like routines. Supports the following formats 40 ** %d - decimal 41 ** %u - unsigned decimal 42 ** %x - unsigned hex 43 ** %X - unsigned uppercase hex 44 ** %o - unsigned octal 45 ** %hd, %hu, %hx, %hX, %ho - 16-bit versions of above 46 ** %ld, %lu, %lx, %lX, %lo - 32-bit versions of above 47 ** %lld, %llu, %llx, %llX, %llo - 64 bit versions of above 48 ** %s - string 49 ** %c - character 50 ** %p - pointer (deals with machine dependent pointer size) 51 ** %f - float 52 ** %g - float 53 */ 54 #include "prtypes.h" 55 #include "prio.h" 56 #include <stdio.h> 57 #include <stdarg.h> 58 59 PR_BEGIN_EXTERN_C 60 61 /* 62 ** sprintf into a fixed size buffer. Guarantees that a NUL is at the end 63 ** of the buffer. Returns the length of the written output, NOT including 64 ** the NUL, or (PRUint32)-1 if an error occurs. 65 */ 66 NSPR_API(PRUint32) PR_snprintf(char *out, PRUint32 outlen, const char *fmt, ...); 67 68 /* 69 ** sprintf into a PR_MALLOC'd buffer. Return a pointer to the malloc'd 70 ** buffer on success, NULL on failure. Call "PR_smprintf_free" to release 71 ** the memory returned. 72 */ 73 NSPR_API(char*) PR_smprintf(const char *fmt, ...); 74 75 /* 76 ** Free the memory allocated, for the caller, by PR_smprintf 77 */ 78 NSPR_API(void) PR_smprintf_free(char *mem); 79 80 /* 81 ** "append" sprintf into a PR_MALLOC'd buffer. "last" is the last value of 82 ** the PR_MALLOC'd buffer. sprintf will append data to the end of last, 83 ** growing it as necessary using realloc. If last is NULL, PR_sprintf_append 84 ** will allocate the initial string. The return value is the new value of 85 ** last for subsequent calls, or NULL if there is a malloc failure. 86 */ 87 NSPR_API(char*) PR_sprintf_append(char *last, const char *fmt, ...); 88 89 /* 90 ** sprintf into a function. The function "f" is called with a string to 91 ** place into the output. "arg" is an opaque pointer used by the stuff 92 ** function to hold any state needed to do the storage of the output 93 ** data. The return value is a count of the number of characters fed to 94 ** the stuff function, or (PRUint32)-1 if an error occurs. 95 */ 96 typedef PRIntn (*PRStuffFunc)(void *arg, const char *s, PRUint32 slen); 97 98 NSPR_API(PRUint32) PR_sxprintf(PRStuffFunc f, void *arg, const char *fmt, ...); 99 100 /* 101 ** fprintf to a PRFileDesc 102 */ 103 NSPR_API(PRUint32) PR_fprintf(struct PRFileDesc* fd, const char *fmt, ...); 104 105 /* 106 ** va_list forms of the above. 107 */ 108 NSPR_API(PRUint32) PR_vsnprintf(char *out, PRUint32 outlen, const char *fmt, va_list ap); 109 NSPR_API(char*) PR_vsmprintf(const char *fmt, va_list ap); 110 NSPR_API(char*) PR_vsprintf_append(char *last, const char *fmt, va_list ap); 111 NSPR_API(PRUint32) PR_vsxprintf(PRStuffFunc f, void *arg, const char *fmt, va_list ap); 112 NSPR_API(PRUint32) PR_vfprintf(struct PRFileDesc* fd, const char *fmt, va_list ap); 113 114 /* 115 *************************************************************************** 116 ** FUNCTION: PR_sscanf 117 ** DESCRIPTION: 118 ** PR_sscanf() scans the input character string, performs data 119 ** conversions, and stores the converted values in the data objects 120 ** pointed to by its arguments according to the format control 121 ** string. 122 ** 123 ** PR_sscanf() behaves the same way as the sscanf() function in the 124 ** Standard C Library (stdio.h), with the following exceptions: 125 ** - PR_sscanf() handles the NSPR integer and floating point types, 126 ** such as PRInt16, PRInt32, PRInt64, and PRFloat64, whereas 127 ** sscanf() handles the standard C types like short, int, long, 128 ** and double. 129 ** - PR_sscanf() has no multibyte character support, while sscanf() 130 ** does. 131 ** INPUTS: 132 ** const char *buf 133 ** a character string holding the input to scan 134 ** const char *fmt 135 ** the format control string for the conversions 136 ** ... 137 ** variable number of arguments, each of them is a pointer to 138 ** a data object in which the converted value will be stored 139 ** OUTPUTS: none 140 ** RETURNS: PRInt32 141 ** The number of values converted and stored. 142 ** RESTRICTIONS: 143 ** Multibyte characters in 'buf' or 'fmt' are not allowed. 144 *************************************************************************** 145 */ 146 147 NSPR_API(PRInt32) PR_sscanf(const char *buf, const char *fmt, ...); 148 149 PR_END_EXTERN_C 150 151 #endif /* prprf_h___ */ 152