您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

vsprintf.h 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef VSPRINTF_H
  2. #define VSPRINTF_H
  3. /** @file
  4. *
  5. * printf() and friends
  6. *
  7. * Etherboot's printf() functions understand the following subset of
  8. * the standard C printf()'s format specifiers:
  9. *
  10. * - Flag characters
  11. * - '#' - Alternate form (i.e. "0x" prefix)
  12. * - '0' - Zero-pad
  13. * - Field widths
  14. * - Length modifiers
  15. * - 'hh' - Signed / unsigned char
  16. * - 'h' - Signed / unsigned short
  17. * - 'l' - Signed / unsigned long
  18. * - 'll' - Signed / unsigned long long
  19. * - 'z' - Signed / unsigned size_t
  20. * - Conversion specifiers
  21. * - 'd' - Signed decimal
  22. * - 'x','X' - Unsigned hexadecimal
  23. * - 'c' - Character
  24. * - 's' - String
  25. * - 'p' - Pointer
  26. *
  27. * Hexadecimal numbers are always zero-padded to the specified field
  28. * width (if any); decimal numbers are always space-padded. Decimal
  29. * long longs are not supported.
  30. *
  31. */
  32. #include <stdarg.h>
  33. #define PRINTF_NO_LENGTH ( ( size_t ) -1 )
  34. /**
  35. * A printf context
  36. *
  37. * Contexts are used in order to be able to share code between
  38. * vprintf() and vsnprintf(), without requiring the allocation of a
  39. * buffer for vprintf().
  40. */
  41. struct printf_context {
  42. /**
  43. * Character handler
  44. *
  45. * @v ctx Context
  46. * @v c Character
  47. *
  48. * This method is called for each character written to the
  49. * formatted string.
  50. */
  51. void ( * handler ) ( struct printf_context *ctx, unsigned int c );
  52. /** Length of formatted string
  53. *
  54. * When handler() is called, @len will be set to the number of
  55. * characters written so far (i.e. zero for the first call to
  56. * handler()).
  57. */
  58. size_t len;
  59. };
  60. extern size_t vcprintf ( struct printf_context *ctx, const char *fmt,
  61. va_list args );
  62. extern int vsnprintf ( char *buf, size_t size, const char *fmt, va_list args );
  63. extern int vprintf ( const char *fmt, va_list args );
  64. extern int __attribute__ (( format ( printf, 3, 4 ) ))
  65. snprintf ( char *buf, size_t size, const char *fmt, ... );
  66. extern int __attribute__ (( format ( printf, 1, 2 ) ))
  67. printf ( const char *fmt, ... );
  68. /**
  69. * Write a formatted string to a buffer
  70. *
  71. * @v buf Buffer into which to write the string
  72. * @v fmt Format string
  73. * @v ... Arguments corresponding to the format string
  74. * @ret len Length of formatted string
  75. */
  76. #define sprintf( buf, fmt, ... ) \
  77. snprintf ( (buf), PRINTF_NO_LENGTH, (fmt), ## __VA_ARGS__ )
  78. /**
  79. * Write a formatted string to a buffer
  80. *
  81. * @v buf Buffer into which to write the string
  82. * @v fmt Format string
  83. * @v args Arguments corresponding to the format string
  84. * @ret len Length of formatted string
  85. */
  86. static inline int vsprintf ( char *buf, const char *fmt, va_list args ) {
  87. return vsnprintf ( buf, PRINTF_NO_LENGTH, fmt, args );
  88. }
  89. #endif /* VSPRINTF_H */