File: suffix.c

package info (click to toggle)
nvme-cli 1.0-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,496 kB
  • ctags: 2,281
  • sloc: ansic: 9,747; sh: 380; makefile: 282
file content (132 lines) | stat: -rw-r--r-- 2,829 bytes parent folder | download | duplicates (2)
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
126
127
128
129
130
131
132
////////////////////////////////////////////////////////////////////////
//
// Copyright 2014 PMC-Sierra, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
//
//   Author: Logan Gunthorpe
//
//   Date:   Oct 23 2014
//
//   Description:
//     Functions for dealing with number suffixes
//
////////////////////////////////////////////////////////////////////////

#include "suffix.h"

#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>

static struct si_suffix {
	double magnitude;
	const char *suffix;
} si_suffixes[] = {
	{1e15, "P"},
	{1e12, "T"},
	{1e9, "G"},
	{1e6, "M"},
	{1e3, "k"},
	{1e0, ""},
	{1e-3, "m"},
	{1e-6, "u"},
	{1e-9, "n"},
	{1e-12, "p"},
	{1e-15, "f"},
	{0}
};

const char *suffix_si_get(double *value)
{
	struct si_suffix *s;

	for (s = si_suffixes; s->magnitude != 0; s++) {
		if (*value >= s->magnitude) {
			*value /= s->magnitude;
			return s->suffix;
		}
	}

	return "";
}

static struct binary_suffix {
	int shift;
	const char *suffix;
} binary_suffixes[] = {
	{50, "Pi"},
	{40, "Ti"},
	{30, "Gi"},
	{20, "Mi"},
	{10, "Ki"},
	{0, ""}
};

const char *suffix_binary_get(long long *value)
{
	struct binary_suffix *s;

	for (s = binary_suffixes; s->shift != 0; s++) {
		if (llabs(*value) >= (1LL << s->shift)) {
			*value =
			    (*value + (1 << (s->shift - 1))) / (1 << s->shift);
			return s->suffix;
		}
	}

	return "";
}

const char *suffix_dbinary_get(double *value)
{
	struct binary_suffix *s;

	for (s = binary_suffixes; s->shift != 0; s++) {
		if (fabs(*value) >= (1LL << s->shift)) {
			*value = *value / (1 << s->shift);
			return s->suffix;
		}
	}

	return "";
}

long long suffix_binary_parse(const char *value)
{
	char *suffix;
	errno = 0;
	long long ret = strtol(value, &suffix, 0);
	if (errno)
		return 0;

	struct binary_suffix *s;
	for (s = binary_suffixes; s->shift != 0; s++) {
		if (tolower(suffix[0]) == tolower(s->suffix[0])) {
			ret <<= s->shift;
			return ret;
		}
	}

	if (suffix[0] != '\0')
		errno = EINVAL;

	return ret;
}