Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 1 | #ifndef _FMT_H_ |
| 2 | #define _FMT_H_ 1 |
| 3 | #if defined(__cplusplus) |
Russ Cox | 04a77ac | 2009-08-31 16:38:50 -0700 | [diff] [blame] | 4 | extern "C" { |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 5 | #endif |
| 6 | /* |
| 7 | * The authors of this software are Rob Pike and Ken Thompson. |
| 8 | * Copyright (c) 2002 by Lucent Technologies. |
| 9 | * Permission to use, copy, modify, and distribute this software for any |
| 10 | * purpose without fee is hereby granted, provided that this entire notice |
| 11 | * is included in all copies of any software which is or includes a copy |
| 12 | * or modification of this software and in all copies of the supporting |
| 13 | * documentation for such software. |
| 14 | * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED |
| 15 | * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY |
| 16 | * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY |
| 17 | * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. |
| 18 | */ |
| 19 | |
| 20 | #include <stdarg.h> |
| 21 | #include <utf.h> |
| 22 | |
| 23 | typedef struct Fmt Fmt; |
| 24 | struct Fmt{ |
| 25 | unsigned char runes; /* output buffer is runes or chars? */ |
| 26 | void *start; /* of buffer */ |
| 27 | void *to; /* current place in the buffer */ |
| 28 | void *stop; /* end of the buffer; overwritten if flush fails */ |
| 29 | int (*flush)(Fmt *); /* called when to == stop */ |
| 30 | void *farg; /* to make flush a closure */ |
| 31 | int nfmt; /* num chars formatted so far */ |
| 32 | va_list args; /* args passed to dofmt */ |
Russ Cox | 04a77ac | 2009-08-31 16:38:50 -0700 | [diff] [blame] | 33 | Rune r; /* % format Rune */ |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 34 | int width; |
| 35 | int prec; |
| 36 | unsigned long flags; |
| 37 | char *decimal; /* decimal point; cannot be "" */ |
| 38 | |
| 39 | /* For %'d */ |
| 40 | char *thousands; /* separator for thousands */ |
Russ Cox | 04a77ac | 2009-08-31 16:38:50 -0700 | [diff] [blame] | 41 | |
| 42 | /* |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 43 | * Each char is an integer indicating #digits before next separator. Values: |
| 44 | * \xFF: no more grouping (or \x7F; defined to be CHAR_MAX in POSIX) |
| 45 | * \x00: repeat previous indefinitely |
| 46 | * \x**: count that many |
| 47 | */ |
| 48 | char *grouping; /* descriptor of separator placement */ |
| 49 | }; |
| 50 | |
| 51 | enum{ |
| 52 | FmtWidth = 1, |
| 53 | FmtLeft = FmtWidth << 1, |
| 54 | FmtPrec = FmtLeft << 1, |
| 55 | FmtSharp = FmtPrec << 1, |
| 56 | FmtSpace = FmtSharp << 1, |
| 57 | FmtSign = FmtSpace << 1, |
| 58 | FmtApost = FmtSign << 1, |
| 59 | FmtZero = FmtApost << 1, |
| 60 | FmtUnsigned = FmtZero << 1, |
| 61 | FmtShort = FmtUnsigned << 1, |
| 62 | FmtLong = FmtShort << 1, |
| 63 | FmtVLong = FmtLong << 1, |
| 64 | FmtComma = FmtVLong << 1, |
| 65 | FmtByte = FmtComma << 1, |
| 66 | FmtLDouble = FmtByte << 1, |
| 67 | |
| 68 | FmtFlag = FmtLDouble << 1 |
| 69 | }; |
| 70 | |
| 71 | extern int (*fmtdoquote)(int); |
| 72 | |
| 73 | /* Edit .+1,/^$/ | cfn $PLAN9/src/lib9/fmt/?*.c | grep -v static |grep -v __ */ |
| 74 | int dofmt(Fmt *f, char *fmt); |
| 75 | int dorfmt(Fmt *f, const Rune *fmt); |
| 76 | double fmtcharstod(int(*f)(void*), void *vp); |
| 77 | int fmtfdflush(Fmt *f); |
| 78 | int fmtfdinit(Fmt *f, int fd, char *buf, int size); |
| 79 | int fmtinstall(int c, int (*f)(Fmt*)); |
| 80 | int fmtnullinit(Fmt*); |
| 81 | void fmtlocaleinit(Fmt*, char*, char*, char*); |
| 82 | int fmtprint(Fmt *f, char *fmt, ...); |
| 83 | int fmtrune(Fmt *f, int r); |
| 84 | int fmtrunestrcpy(Fmt *f, Rune *s); |
| 85 | int fmtstrcpy(Fmt *f, char *s); |
| 86 | char* fmtstrflush(Fmt *f); |
| 87 | int fmtstrinit(Fmt *f); |
| 88 | double fmtstrtod(const char *as, char **aas); |
| 89 | int fmtvprint(Fmt *f, char *fmt, va_list args); |
| 90 | int fprint(int fd, char *fmt, ...); |
| 91 | int print(char *fmt, ...); |
| 92 | void quotefmtinstall(void); |
| 93 | int quoterunestrfmt(Fmt *f); |
| 94 | int quotestrfmt(Fmt *f); |
| 95 | Rune* runefmtstrflush(Fmt *f); |
| 96 | int runefmtstrinit(Fmt *f); |
| 97 | Rune* runeseprint(Rune *buf, Rune *e, char *fmt, ...); |
| 98 | Rune* runesmprint(char *fmt, ...); |
| 99 | int runesnprint(Rune *buf, int len, char *fmt, ...); |
| 100 | int runesprint(Rune *buf, char *fmt, ...); |
| 101 | Rune* runevseprint(Rune *buf, Rune *e, char *fmt, va_list args); |
| 102 | Rune* runevsmprint(char *fmt, va_list args); |
| 103 | int runevsnprint(Rune *buf, int len, char *fmt, va_list args); |
| 104 | char* seprint(char *buf, char *e, char *fmt, ...); |
| 105 | char* smprint(char *fmt, ...); |
| 106 | int snprint(char *buf, int len, char *fmt, ...); |
| 107 | int sprint(char *buf, char *fmt, ...); |
| 108 | int vfprint(int fd, char *fmt, va_list args); |
| 109 | char* vseprint(char *buf, char *e, char *fmt, va_list args); |
| 110 | char* vsmprint(char *fmt, va_list args); |
| 111 | int vsnprint(char *buf, int len, char *fmt, va_list args); |
| 112 | |
| 113 | #if defined(__cplusplus) |
| 114 | } |
| 115 | #endif |
| 116 | #endif |