nsTextFormatter.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* -*- Mode: C++; tab-width: 8; 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. * This code was copied from xpcom/ds/nsTextFormatter r1.3
  7. * Memory model and Frozen linkage changes only.
  8. * -- Prasad <prasad@medhas.org>
  9. */
  10. #ifndef nsTextFormatter_h___
  11. #define nsTextFormatter_h___
  12. /*
  13. ** API for PR printf like routines. Supports the following formats
  14. ** %d - decimal
  15. ** %u - unsigned decimal
  16. ** %x - unsigned hex
  17. ** %X - unsigned uppercase hex
  18. ** %o - unsigned octal
  19. ** %hd, %hu, %hx, %hX, %ho - 16-bit versions of above
  20. ** %ld, %lu, %lx, %lX, %lo - 32-bit versions of above
  21. ** %lld, %llu, %llx, %llX, %llo - 64 bit versions of above
  22. ** %s - utf8 string
  23. ** %S - char16_t string
  24. ** %c - character
  25. ** %p - pointer (deals with machine dependent pointer size)
  26. ** %f - float
  27. ** %g - float
  28. */
  29. #include "prio.h"
  30. #include <stdio.h>
  31. #include <stdarg.h>
  32. #include "nscore.h"
  33. #include "nsStringGlue.h"
  34. #ifdef XPCOM_GLUE
  35. #error "nsTextFormatter is not available in the standalone glue due to NSPR dependencies."
  36. #endif
  37. class nsTextFormatter
  38. {
  39. public:
  40. /*
  41. * sprintf into a fixed size buffer. Guarantees that the buffer is null
  42. * terminated. Returns the length of the written output, NOT including the
  43. * null terminator, or (uint32_t)-1 if an error occurs.
  44. */
  45. static uint32_t snprintf(char16_t* aOut, uint32_t aOutLen,
  46. const char16_t* aFmt, ...);
  47. /*
  48. * sprintf into a moz_xmalloc'd buffer. Return a pointer to
  49. * buffer on success, nullptr on failure.
  50. */
  51. static char16_t* smprintf(const char16_t* aFmt, ...);
  52. static uint32_t ssprintf(nsAString& aOut, const char16_t* aFmt, ...);
  53. /*
  54. * va_list forms of the above.
  55. */
  56. static uint32_t vsnprintf(char16_t* aOut, uint32_t aOutLen, const char16_t* aFmt,
  57. va_list aAp);
  58. static char16_t* vsmprintf(const char16_t* aFmt, va_list aAp);
  59. static uint32_t vssprintf(nsAString& aOut, const char16_t* aFmt, va_list aAp);
  60. /*
  61. * Free the memory allocated, for the caller, by smprintf.
  62. * -- Deprecated --
  63. * Callers can substitute calling smprintf_free with free
  64. */
  65. static void smprintf_free(char16_t* aMem);
  66. };
  67. #endif /* nsTextFormatter_h___ */