BRL-CAD
units.h
Go to the documentation of this file.
1/* U N I T S . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21#ifndef BU_UNITS_H
22#define BU_UNITS_H
23
24#include "common.h"
25
26#include "bu/defines.h"
27#include "bu/parse.h"
28#include "bu/vls.h"
29
30__BEGIN_DECLS
31
32/** @addtogroup bu_units
33 * @brief
34 * Module of libbu to handle units conversion between strings and mm.
35 */
36/** @{ */
37/** @file bu/units.h */
38
39/**
40 * Convert the provided string into a units conversion factor.
41 *
42 * Given a string for a unit of length (e.g., "feet", "yd"), volume
43 * (e.g., "cm^3", "cu yards"), or mass (e.g., "kg", "grain", or "oz")
44 * return the multiplier (aka conversion factor) that converts the
45 * unit into the default (millimeters for length, mm^3 for volume, and
46 * grams for mass.) Values may be optionally specified with the unit
47 * (e.g., "5ft") to get the conversion factor for a particular
48 * quantity.
49 *
50 * Returns 0.0 on error and >0.0 on success
51 */
52BU_EXPORT extern double bu_units_conversion(const char *str);
53
54
55/**
56 * Given a conversion factor to mm, search the table to find what unit
57 * this represents.
58 *
59 * To accommodate floating point fuzz, a "near miss" is allowed.
60 *
61 * Returns -
62 * char* units string
63 * NULL No known unit matches this conversion factor.
64 */
65BU_EXPORT extern const char *bu_units_string(const double mm);
66
67
68/** undocumented */
69BU_EXPORT extern struct bu_vls *bu_units_strings_vls(void);
70
71
72/**
73 * Given a conversion factor to mm, search the table to find the
74 * closest matching unit.
75 *
76 * Returns -
77 * char* units string
78 * NULL Invalid conversion factor (non-positive)
79 */
80BU_EXPORT extern const char *bu_nearest_units_string(const double mm);
81
82
83/**
84 * Given a string of the form "25cm" or "5.2ft" returns the
85 * corresponding distance in mm.
86 *
87 * Returns -
88 * -1 on error
89 * >0 on success
90 */
91BU_EXPORT extern double bu_mm_value(const char *s);
92
93
94/**
95 * Used primarily as a hooked function for bu_structparse tables to
96 * allow input of floating point values in other units.
97 */
98BU_EXPORT extern void bu_mm_cvt(const struct bu_structparse *sdp,
99 const char *name,
100 void *base,
101 const char *value,
102 void *data);
103
104
105/* Values for bu_humanize_number's flags parameter. */
106#define BU_HN_DECIMAL 0x01
107#define BU_HN_NOSPACE 0x02
108#define BU_HN_B 0x04
109#define BU_HN_DIVISOR_1000 0x08
110#define BU_HN_IEC_PREFIXES 0x10
111
112/* Values for bu_humanize_number's scale parameter. */
113#define BU_HN_GETSCALE 0x10
114#define BU_HN_AUTOSCALE 0x20
115
116
117/**
118 * Convert digital sizes to human readable form. Based off the
119 * BSD function humanize_number(3).
120 * Upon success, the humanize_number function returns the number of characters
121 * that would have been stored in buf (excluding the terminating NUL) if buf
122 * was large enough, or -1 upon failure. Even upon failure, the contents of
123 * buf may be modified. If BU_HN_GETSCALE is specified, the prefix index
124 * number will be returned instead.
125 */
126BU_EXPORT extern int bu_humanize_number(char *buf, size_t len, int64_t quotient, const char *suffix, size_t scale, int flags);
127
128
129/*
130 * Converts the number given in 'str', which may be given in a humanized
131 * form (as described in BSD's humanize_number(3), but with some limitations),
132 * to an int64_t without units.
133 * In case of success, 0 is returned and *size holds the value.
134 * Otherwise, -1 is returned and *size is untouched.
135 */
136BU_EXPORT extern int bu_dehumanize_number(const char *str, int64_t *size);
137
138
139__END_DECLS
140
141/** @} */
142
143#endif /* BU_UNITS_H */
144
145/*
146 * Local Variables:
147 * mode: C
148 * tab-width: 8
149 * indent-tabs-mode: t
150 * c-file-style: "stroustrup"
151 * End:
152 * ex: shiftwidth=4 tabstop=8
153 */
Header file for the BRL-CAD common definitions.
struct bu_vls * bu_units_strings_vls(void)
const char * bu_units_string(const double mm)
void bu_mm_cvt(const struct bu_structparse *sdp, const char *name, void *base, const char *value, void *data)
int bu_dehumanize_number(const char *str, int64_t *size)
double bu_units_conversion(const char *str)
int bu_humanize_number(char *buf, size_t len, int64_t quotient, const char *suffix, size_t scale, int flags)
double bu_mm_value(const char *s)
const char * bu_nearest_units_string(const double mm)
void int float float float * scale
Definition: tig.h:142
void float float int int int int float * size
Definition: tig.h:132
Definition: vls.h:53