prprf.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef prprf_h___
  38. #define prprf_h___
  39. /*
  40. ** API for PR printf like routines. Supports the following formats
  41. ** %d - decimal
  42. ** %u - unsigned decimal
  43. ** %x - unsigned hex
  44. ** %X - unsigned uppercase hex
  45. ** %o - unsigned octal
  46. ** %hd, %hu, %hx, %hX, %ho - 16-bit versions of above
  47. ** %ld, %lu, %lx, %lX, %lo - 32-bit versions of above
  48. ** %lld, %llu, %llx, %llX, %llo - 64 bit versions of above
  49. ** %s - string
  50. ** %c - character
  51. ** %p - pointer (deals with machine dependent pointer size)
  52. ** %f - float
  53. ** %g - float
  54. */
  55. #include "prtypes.h"
  56. #include "prio.h"
  57. #include <stdio.h>
  58. #include <stdarg.h>
  59. PR_BEGIN_EXTERN_C
  60. /*
  61. ** sprintf into a fixed size buffer. Guarantees that a NUL is at the end
  62. ** of the buffer. Returns the length of the written output, NOT including
  63. ** the NUL, or (PRUint32)-1 if an error occurs.
  64. */
  65. NSPR_API(PRUint32) PR_snprintf(char *out, PRUint32 outlen, const char *fmt, ...);
  66. /*
  67. ** sprintf into a PR_MALLOC'd buffer. Return a pointer to the malloc'd
  68. ** buffer on success, NULL on failure. Call "PR_smprintf_free" to release
  69. ** the memory returned.
  70. */
  71. NSPR_API(char*) PR_smprintf(const char *fmt, ...);
  72. /*
  73. ** Free the memory allocated, for the caller, by PR_smprintf
  74. */
  75. NSPR_API(void) PR_smprintf_free(char *mem);
  76. /*
  77. ** "append" sprintf into a PR_MALLOC'd buffer. "last" is the last value of
  78. ** the PR_MALLOC'd buffer. sprintf will append data to the end of last,
  79. ** growing it as necessary using realloc. If last is NULL, PR_sprintf_append
  80. ** will allocate the initial string. The return value is the new value of
  81. ** last for subsequent calls, or NULL if there is a malloc failure.
  82. */
  83. NSPR_API(char*) PR_sprintf_append(char *last, const char *fmt, ...);
  84. /*
  85. ** sprintf into a function. The function "f" is called with a string to
  86. ** place into the output. "arg" is an opaque pointer used by the stuff
  87. ** function to hold any state needed to do the storage of the output
  88. ** data. The return value is a count of the number of characters fed to
  89. ** the stuff function, or (PRUint32)-1 if an error occurs.
  90. */
  91. typedef PRIntn (*PRStuffFunc)(void *arg, const char *s, PRUint32 slen);
  92. NSPR_API(PRUint32) PR_sxprintf(PRStuffFunc f, void *arg, const char *fmt, ...);
  93. /*
  94. ** fprintf to a PRFileDesc
  95. */
  96. NSPR_API(PRUint32) PR_fprintf(struct PRFileDesc* fd, const char *fmt, ...);
  97. /*
  98. ** va_list forms of the above.
  99. */
  100. NSPR_API(PRUint32) PR_vsnprintf(char *out, PRUint32 outlen, const char *fmt, va_list ap);
  101. NSPR_API(char*) PR_vsmprintf(const char *fmt, va_list ap);
  102. NSPR_API(char*) PR_vsprintf_append(char *last, const char *fmt, va_list ap);
  103. NSPR_API(PRUint32) PR_vsxprintf(PRStuffFunc f, void *arg, const char *fmt, va_list ap);
  104. NSPR_API(PRUint32) PR_vfprintf(struct PRFileDesc* fd, const char *fmt, va_list ap);
  105. /*
  106. ***************************************************************************
  107. ** FUNCTION: PR_sscanf
  108. ** DESCRIPTION:
  109. ** PR_sscanf() scans the input character string, performs data
  110. ** conversions, and stores the converted values in the data objects
  111. ** pointed to by its arguments according to the format control
  112. ** string.
  113. **
  114. ** PR_sscanf() behaves the same way as the sscanf() function in the
  115. ** Standard C Library (stdio.h), with the following exceptions:
  116. ** - PR_sscanf() handles the NSPR integer and floating point types,
  117. ** such as PRInt16, PRInt32, PRInt64, and PRFloat64, whereas
  118. ** sscanf() handles the standard C types like short, int, long,
  119. ** and double.
  120. ** - PR_sscanf() has no multibyte character support, while sscanf()
  121. ** does.
  122. ** INPUTS:
  123. ** const char *buf
  124. ** a character string holding the input to scan
  125. ** const char *fmt
  126. ** the format control string for the conversions
  127. ** ...
  128. ** variable number of arguments, each of them is a pointer to
  129. ** a data object in which the converted value will be stored
  130. ** OUTPUTS: none
  131. ** RETURNS: PRInt32
  132. ** The number of values converted and stored.
  133. ** RESTRICTIONS:
  134. ** Multibyte characters in 'buf' or 'fmt' are not allowed.
  135. ***************************************************************************
  136. */
  137. NSPR_API(PRInt32) PR_sscanf(const char *buf, const char *fmt, ...);
  138. PR_END_EXTERN_C
  139. #endif /* prprf_h___ */