-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.body.c
89 lines (70 loc) · 2.33 KB
/
unit.body.c
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
double unit_to_base_unit(double quantity, enum unit unit, enum base *base_unit)
{
double X = quantity;
switch (unit) {
case PresentationUnitNone:
case PresentationUnitUnknown:
*base_unit = BaseUnitNone;
break;
#define u(symbol, name, base, scale) case name : *base_unit = base ; X *= scale ; break ;
#define c(symbol, name, base, tobase, frombase) case name : *base_unit = base ; X = tobase ; break ;
#include "unit.hi"
}
return X;
}
const wchar_t *symbol_of_unit(enum unit unit)
{
switch (unit) {
case PresentationUnitNone:
case PresentationUnitUnknown:
break;
#define u(symbol, name, base, scale) case name : return symbol ;
#define c(symbol, name, base, tobase, frombase) case name : return symbol ;
#include "unit.hi"
}
return NULL;
}
char *base_unit_render(double quantity, enum base base_unit)
{
char *s = NULL;
switch (base_unit) {
case BaseUnitNone:
break;
#define b(symbol, name) case name : asprintf(&s, "%g %ls", quantity, symbol) ; break ;
#include "unit.hi"
}
return s;
}
bool can_render_base_unit_as(enum base base_unit, enum unit unit)
{
switch (unit) {
case PresentationUnitNone:
case PresentationUnitUnknown:
break;
#define u(symbol, name, base, scale) case name : return base_unit == base;
#define c(symbol, name, base, tobase, frombase) case name : return base_unit == base;
#include "unit.hi"
}
return false;
}
char *base_unit_render_as(double quantity, enum base base_unit, enum unit unit)
{
char *s = NULL;
double X = quantity;
switch (unit) {
case PresentationUnitNone:
case PresentationUnitUnknown:
break;
#define u(symbol, name, base, scale) case name : if (base_unit == base) { X /= scale ; asprintf(&s, "%g %ls", X, symbol); } break ;
#define c(symbol, name, base, tobase, frombase) case name : if (base_unit == base) { X = frombase ; asprintf(&s, "%g %ls", X, symbol); } break ;
#include "unit.hi"
}
if (unit == PresentationUnitFeetAndInches) {
// Exception.
const double ScaleFractionalFeetToInch = 12;
double y = fmod(X, 1) * ScaleFractionalFeetToInch;
free(s);
asprintf(&s, "%ld ' %g \"", (long)X, y);
}
return s;
}