2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include "parse_units.h"
43 * Parse string in `s' according to `units' and return value.
44 * def_unit defines the default unit.
48 parse_something (const char *s
, const struct units
*units
,
50 int (*func
)(int res
, int val
, unsigned mult
),
56 unsigned def_mult
= 1;
58 if (def_unit
!= NULL
) {
59 const struct units
*u
;
61 for (u
= units
; u
->name
; ++u
) {
62 if (strcasecmp (u
->name
, def_unit
) == 0) {
75 const struct units
*u
, *partial_unit
;
80 while(isspace((unsigned char)*p
) || *p
== ',')
83 val
= strtol(p
, &next
, 0);
91 while (isspace((unsigned char)*p
))
94 res
= (*func
)(res
, val
, def_mult
);
98 } else if (*p
== '+') {
101 } else if (*p
== '-') {
105 if (no_val_p
&& val
== 0)
107 u_len
= strcspn (p
, ", \t");
110 if (u_len
> 1 && p
[u_len
- 1] == 's')
112 for (u
= units
; u
->name
; ++u
) {
113 if (strncasecmp (p
, u
->name
, u_len
) == 0) {
114 if (u_len
== strlen (u
->name
)) {
116 res
= (*func
)(res
, val
, u
->mult
);
126 if (u
->name
== NULL
) {
129 res
= (*func
)(res
, val
, partial_unit
->mult
);
143 * The string consists of a sequence of `n unit'
147 acc_units(int res
, int val
, unsigned mult
)
149 return res
+ val
* mult
;
152 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
153 parse_units (const char *s
, const struct units
*units
,
154 const char *def_unit
)
156 return parse_something (s
, units
, def_unit
, acc_units
, 0, 0);
160 * The string consists of a sequence of `[+-]flag'. `orig' consists
161 * the original set of flags, those are then modified and returned as
162 * the function value.
166 acc_flags(int res
, int val
, unsigned mult
)
178 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
179 parse_flags (const char *s
, const struct units
*units
,
182 return parse_something (s
, units
, NULL
, acc_flags
, orig
, 1);
186 * Return a string representation according to `units' of `num' in `s'
187 * with maximum length `len'. The actual length is the function value.
191 unparse_something (int num
, const struct units
*units
, char *s
, size_t len
,
192 int (*print
) (char *, size_t, int, const char *, int),
193 int (*update
) (int, unsigned),
194 const char *zero_string
)
196 const struct units
*u
;
200 return snprintf (s
, len
, "%s", zero_string
);
202 for (u
= units
; num
> 0 && u
->name
; ++u
) {
205 divisor
= num
/ u
->mult
;
207 num
= (*update
) (num
, u
->mult
);
208 tmp
= (*print
) (s
, len
, divisor
, u
->name
, num
);
211 if (tmp
> (int) len
) {
225 print_unit (char *s
, size_t len
, int divisor
, const char *name
, int rem
)
227 return snprintf (s
, len
, "%u %s%s%s",
229 divisor
== 1 ? "" : "s",
234 update_unit (int in
, unsigned mult
)
240 update_unit_approx (int in
, unsigned mult
)
245 return update_unit (in
, mult
);
248 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
249 unparse_units (int num
, const struct units
*units
, char *s
, size_t len
)
251 return unparse_something (num
, units
, s
, len
,
257 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
258 unparse_units_approx (int num
, const struct units
*units
, char *s
, size_t len
)
260 return unparse_something (num
, units
, s
, len
,
266 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
267 print_units_table (const struct units
*units
, FILE *f
)
269 const struct units
*u
, *u2
;
272 for (u
= units
; u
->name
; ++u
) {
273 max_sz
= max(max_sz
, strlen(u
->name
));
276 for (u
= units
; u
->name
;) {
278 const struct units
*next
;
280 for (next
= u
+ 1; next
->name
&& next
->mult
== u
->mult
; ++next
)
285 u2
->name
&& u
->mult
% u2
->mult
!= 0;
288 if (u2
->name
== NULL
)
290 unparse_units (u
->mult
, u2
, buf
, sizeof(buf
));
291 fprintf (f
, "1 %*s = %s\n", max_sz
, u
->name
, buf
);
293 fprintf (f
, "1 %s\n", u
->name
);
300 print_flag (char *s
, size_t len
, int divisor
, const char *name
, int rem
)
302 return snprintf (s
, len
, "%s%s", name
, rem
> 0 ? ", " : "");
306 update_flag (int in
, unsigned mult
)
311 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
312 unparse_flags (int num
, const struct units
*units
, char *s
, size_t len
)
314 return unparse_something (num
, units
, s
, len
,
320 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
321 print_flags_table (const struct units
*units
, FILE *f
)
323 const struct units
*u
;
325 for(u
= units
; u
->name
; ++u
)
326 fprintf(f
, "%s%s", u
->name
, (u
+1)->name
? ", " : "\n");