fmtdef.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * dofmt -- format to a buffer
  3. * the number of characters formatted is returned,
  4. * or -1 if there was an error.
  5. * if the buffer is ever filled, flush is called.
  6. * it should reset the buffer and return whether formatting should continue.
  7. */
  8. typedef int (*Fmts)(Fmt*);
  9. typedef struct Quoteinfo Quoteinfo;
  10. struct Quoteinfo
  11. {
  12. int quoted; /* if set, string must be quoted */
  13. int nrunesin; /* number of input runes that can be accepted */
  14. int nbytesin; /* number of input bytes that can be accepted */
  15. int nrunesout; /* number of runes that will be generated */
  16. int nbytesout; /* number of bytes that will be generated */
  17. };
  18. void *_fmtflush(Fmt*, void*, int);
  19. void *_fmtdispatch(Fmt*, void*, int);
  20. int _floatfmt(Fmt*, double);
  21. int _fmtpad(Fmt*, int);
  22. int _rfmtpad(Fmt*, int);
  23. int _fmtFdFlush(Fmt*);
  24. int _efgfmt(Fmt*);
  25. int _charfmt(Fmt*);
  26. int _countfmt(Fmt*);
  27. int _flagfmt(Fmt*);
  28. int _percentfmt(Fmt*);
  29. int _ifmt(Fmt*);
  30. int _runefmt(Fmt*);
  31. int _runesfmt(Fmt*);
  32. int _strfmt(Fmt*);
  33. int _badfmt(Fmt*);
  34. int _fmtcpy(Fmt*, void*, int, int);
  35. int _fmtrcpy(Fmt*, void*, int n);
  36. void _fmtlock(void);
  37. void _fmtunlock(void);
  38. #define FMTCHAR(f, t, s, c)\
  39. do{\
  40. if(t + 1 > (char*)s){\
  41. t = _fmtflush(f, t, 1);\
  42. if(t != nil)\
  43. s = f->stop;\
  44. else\
  45. return -1;\
  46. }\
  47. *t++ = c;\
  48. }while(0)
  49. #define FMTRCHAR(f, t, s, c)\
  50. do{\
  51. if(t + 1 > (Rune*)s){\
  52. t = _fmtflush(f, t, sizeof(Rune));\
  53. if(t != nil)\
  54. s = f->stop;\
  55. else\
  56. return -1;\
  57. }\
  58. *t++ = c;\
  59. }while(0)
  60. #define FMTRUNE(f, t, s, r)\
  61. do{\
  62. Rune _rune;\
  63. int _runelen;\
  64. if(t + UTFmax > (char*)s && t + (_runelen = runelen(r)) > (char*)s){\
  65. t = _fmtflush(f, t, _runelen);\
  66. if(t != nil)\
  67. s = f->stop;\
  68. else\
  69. return -1;\
  70. }\
  71. if(r < Runeself)\
  72. *t++ = r;\
  73. else{\
  74. _rune = r;\
  75. t += runetochar(t, &_rune);\
  76. }\
  77. }while(0)