prprf.h 4.4 KB

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