fmtlocale.c (1358B)
1 /* Copyright (c) 2004 Google Inc.; see LICENSE */ 2 3 #include "fmt.h" 4 #include "fmtdef.h" 5 #include "plan9.h" 6 7 #include <stdarg.h> 8 #include <string.h> 9 10 /* 11 * Fill in the internationalization stuff in the State structure. 12 * For nil arguments, provide the sensible defaults: 13 * decimal is a period 14 * thousands separator is a comma 15 * thousands are marked every three digits 16 */ 17 void fmtlocaleinit(Fmt* f, char* decimal, char* thousands, char* grouping) { 18 if (decimal == nil || decimal[0] == '\0') 19 decimal = "."; 20 if (thousands == nil) 21 thousands = ","; 22 if (grouping == nil) 23 grouping = "\3"; 24 f->decimal = decimal; 25 f->thousands = thousands; 26 f->grouping = grouping; 27 } 28 29 /* 30 * We are about to emit a digit in e.g. %'d. If that digit would 31 * overflow a thousands (e.g.) grouping, tell the caller to emit 32 * the thousands separator. Always advance the digit counter 33 * and pointer into the grouping descriptor. 34 */ 35 int __needsep(int* ndig, char** grouping) { 36 int group; 37 38 (*ndig)++; 39 group = *(unsigned char*) *grouping; 40 /* CHAR_MAX means no further grouping. \0 means we got the empty string */ 41 if (group == 0xFF || group == 0x7f || group == 0x00) 42 return 0; 43 if (*ndig > group) { 44 /* if we're at end of string, continue with this grouping; else advance */ 45 if ((*grouping)[1] != '\0') 46 (*grouping)++; 47 *ndig = 1; 48 return 1; 49 } 50 return 0; 51 }