fmtdef.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #define uchar _fmtuchar
  22. #define ushort _fmtushort
  23. #define uint _fmtuint
  24. #define ulong _fmtulong
  25. #define vlong _fmtvlong
  26. #define uvlong _fmtuvlong
  27. #define USED(x) if(x);else
  28. typedef unsigned char uchar;
  29. typedef unsigned short ushort;
  30. typedef unsigned int uint;
  31. typedef unsigned long ulong;
  32. #ifndef NOVLONGS
  33. typedef unsigned long long uvlong;
  34. typedef long long vlong;
  35. #endif
  36. /* #define nil 0 /* cannot be ((void*)0) because used for function pointers */
  37. typedef int (*Fmts)(Fmt*);
  38. typedef struct Quoteinfo Quoteinfo;
  39. struct Quoteinfo
  40. {
  41. int quoted; /* if set, string must be quoted */
  42. int nrunesin; /* number of input runes that can be accepted */
  43. int nbytesin; /* number of input bytes that can be accepted */
  44. int nrunesout; /* number of runes that will be generated */
  45. int nbytesout; /* number of bytes that will be generated */
  46. };
  47. void *__fmtflush(Fmt*, void*, int);
  48. void *__fmtdispatch(Fmt*, void*, int);
  49. int __floatfmt(Fmt*, double);
  50. int __fmtpad(Fmt*, int);
  51. int __rfmtpad(Fmt*, int);
  52. int __fmtFdFlush(Fmt*);
  53. int __efgfmt(Fmt*);
  54. int __charfmt(Fmt*);
  55. int __runefmt(Fmt*);
  56. int __runesfmt(Fmt*);
  57. int __countfmt(Fmt*);
  58. int __flagfmt(Fmt*);
  59. int __percentfmt(Fmt*);
  60. int __ifmt(Fmt*);
  61. int __strfmt(Fmt*);
  62. int __badfmt(Fmt*);
  63. int __fmtcpy(Fmt*, const void*, int, int);
  64. int __fmtrcpy(Fmt*, const void*, int n);
  65. int __errfmt(Fmt *f);
  66. double __fmtpow10(int);
  67. void __fmtlock(void);
  68. void __fmtunlock(void);
  69. #define FMTCHAR(f, t, s, c)\
  70. do{\
  71. if(t + 1 > (char*)s){\
  72. t = __fmtflush(f, t, 1);\
  73. if(t != nil)\
  74. s = f->stop;\
  75. else\
  76. return -1;\
  77. }\
  78. *t++ = c;\
  79. }while(0)
  80. #define FMTRCHAR(f, t, s, c)\
  81. do{\
  82. if(t + 1 > (Rune*)s){\
  83. t = __fmtflush(f, t, sizeof(Rune));\
  84. if(t != nil)\
  85. s = f->stop;\
  86. else\
  87. return -1;\
  88. }\
  89. *t++ = c;\
  90. }while(0)
  91. #define FMTRUNE(f, t, s, r)\
  92. do{\
  93. Rune _rune;\
  94. int _runelen;\
  95. if(t + UTFmax > (char*)s && t + (_runelen = runelen(r)) > (char*)s){\
  96. t = __fmtflush(f, t, _runelen);\
  97. if(t != nil)\
  98. s = f->stop;\
  99. else\
  100. return -1;\
  101. }\
  102. if(r < Runeself)\
  103. *t++ = r;\
  104. else{\
  105. _rune = r;\
  106. t += runetochar(t, &_rune);\
  107. }\
  108. }while(0)