File: thlocale.cxx

package info (click to toggle)
therion 5.3.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 16,888 kB
  • ctags: 14,686
  • sloc: ansic: 125,669; cpp: 89,022; tcl: 23,212; perl: 2,051; makefile: 920; asm: 174; python: 54; sh: 4
file content (125 lines) | stat: -rw-r--r-- 2,397 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "thlocale.h"
#include "thparse.h"
#include "thbuffer.h"
#include "thexception.h"
#include "thlang.h"
#include "thinit.h"
#ifdef THMSVC
#define snprintf _snprintf
#endif

#define LCBUFFNUM 10
#define LCBUFFLEN 1024
char lcbuffmain [LCBUFFNUM * LCBUFFLEN];
size_t lcbuffcurr = 0;
char * lcbuff;
char lcfmt1 [] = {37, 49, 46, 49, 102, 0}; // "%1.1f";
char lcfmt2 [] = {37, 49, 48, 46, 49, 102, 0}; // "%10.1f";


void init_lcbuff() {
  lcbuffcurr++;
  lcbuffcurr = lcbuffcurr % LCBUFFNUM;
  lcbuff = &(lcbuffmain[LCBUFFLEN * lcbuffcurr]);
}


thlocale::thlocale()
{
	this->units = TTLC_UNITS_METRIC;
	this->lang = thini.get_lang();
}


static const thstok thtt_locale_units[] = {
  {"imperial", TTLC_UNITS_IMPERIAL},
  {"metric", TTLC_UNITS_METRIC},
  {NULL, TTLC_UNKNOWN}
};

	
void thlocale::parse_units(char * cc)
{
	this->units = thmatch_token(cc, thtt_locale_units);
	if (this->units == TTLC_UNKNOWN)
		ththrow(("unknown units -- %s", cc))
}
	

char * thlocale::format_length(double length, int prec, int total)
{
  init_lcbuff();
	switch (this->units) {
		case TTLC_UNITS_IMPERIAL:
			length /= 0.3048;
		  break;
	}
	if (total < 10) {
			lcfmt1[1] = total + 48;
			lcfmt1[3] = prec + 48;
      snprintf(lcbuff, LCBUFFLEN, lcfmt1, length);
	} else {
			lcfmt2[1] = total % 10 + 48;
			lcfmt2[2] = total / 10 + 48;
			lcfmt2[4] = prec + 48;
      snprintf(lcbuff, LCBUFFLEN, lcfmt2, length);
	}
	lcbuff[LCBUFFLEN - 1] = 0;
	return lcbuff;
}


char * thlocale::format_human_length(double length)
{
  int prec = 0;
  double clength = length;
	switch (this->units) {
    case TTLC_UNITS_IMPERIAL:
      clength = length / 0.3048;
      break;
    default:
      if (length <= 5.0)
        prec = 1;
      else
        prec = 0;
      break;
	};
  if ((prec == 1) && ((double)(int)clength == clength))
    prec = 0;
  return this->format_length(length, prec);
}



const char * thlocale::format_length_units()
{
	switch (this->units) {
		case TTLC_UNITS_IMPERIAL:
			return "ft";
	  default:
			return "m";
	}
}

const char * thlocale::format_i18n_length_units()
{
	switch (this->units) {
		case TTLC_UNITS_IMPERIAL:
			return thT("units ft", this->lang);
	  default:
			return thT("units m", this->lang);
	}
}

double thlocale::convert_length(double length)
{
	switch (this->units) {
		case TTLC_UNITS_IMPERIAL:
			length /= 0.3048;
		  break;
	}
	return length;
}

thlocale thdeflocale;