9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

fmtlocale.c (1348B)


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