fmtdef.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * The authors of this software are Rob Pike and Ken Thompson.
  3. * Copyright (c) 2002 by Lucent Technologies.
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose without fee is hereby granted, provided that this entire notice
  6. * is included in all copies of any software which is or includes a copy
  7. * or modification of this software and in all copies of the supporting
  8. * documentation for such software.
  9. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  10. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
  11. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  12. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  13. */
  14. /*
  15. * dofmt -- format to a buffer
  16. * the number of characters formatted is returned,
  17. * or -1 if there was an error.
  18. * if the buffer is ever filled, flush is called.
  19. * it should reset the buffer and return whether formatting should continue.
  20. */
  21. typedef int (*Fmts)(Fmt*);
  22. typedef struct Quoteinfo Quoteinfo;
  23. struct Quoteinfo
  24. {
  25. int quoted; /* if set, string must be quoted */
  26. int nrunesin; /* number of input runes that can be accepted */
  27. int nbytesin; /* number of input bytes that can be accepted */
  28. int nrunesout; /* number of runes that will be generated */
  29. int nbytesout; /* number of bytes that will be generated */
  30. };
  31. void *_fmtflush(Fmt*, void*, int);
  32. void *_fmtdispatch(Fmt*, void*, int);
  33. int _floatfmt(Fmt*, double);
  34. int _fmtpad(Fmt*, int);
  35. int _rfmtpad(Fmt*, int);
  36. int _fmtFdFlush(Fmt*);
  37. int _efgfmt(Fmt*);
  38. int _charfmt(Fmt*);
  39. int _countfmt(Fmt*);
  40. int _flagfmt(Fmt*);
  41. int _percentfmt(Fmt*);
  42. int _ifmt(Fmt*);
  43. int _runefmt(Fmt*);
  44. int _runesfmt(Fmt*);
  45. int _strfmt(Fmt*);
  46. int _badfmt(Fmt*);
  47. int _fmtcpy(Fmt*, void*, int, int);
  48. int _fmtrcpy(Fmt*, void*, int n);
  49. void _fmtlock(void);
  50. void _fmtunlock(void);
  51. #define FMTCHAR(f, t, s, c)\
  52. do{\
  53. if(t + 1 > (char*)s){\
  54. t = _fmtflush(f, t, 1);\
  55. if(t != nil)\
  56. s = f->stop;\
  57. else\
  58. return -1;\
  59. }\
  60. *t++ = c;\
  61. }while(0)
  62. #define FMTRCHAR(f, t, s, c)\
  63. do{\
  64. if(t + 1 > (Rune*)s){\
  65. t = _fmtflush(f, t, sizeof(Rune));\
  66. if(t != nil)\
  67. s = f->stop;\
  68. else\
  69. return -1;\
  70. }\
  71. *t++ = c;\
  72. }while(0)
  73. #define FMTRUNE(f, t, s, r)\
  74. do{\
  75. Rune _rune;\
  76. int _runelen;\
  77. if(t + UTFmax > (char*)s && t + (_runelen = runelen(r)) > (char*)s){\
  78. t = _fmtflush(f, t, _runelen);\
  79. if(t != nil)\
  80. s = f->stop;\
  81. else\
  82. return -1;\
  83. }\
  84. if(r < Runeself)\
  85. *t++ = r;\
  86. else{\
  87. _rune = r;\
  88. t += runetochar(t, &_rune);\
  89. }\
  90. }while(0)
  91. #ifndef va_copy
  92. #define va_copy(a, b) ((a) = (b))
  93. #endif