2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2019-2020 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Aaron LI <aly@aaronly.me>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 #define nitems(x) (sizeof(x) / sizeof((x)[0]))
54 * Return true if string $s1 starts with the string $s2.
57 string_startswith(const char *s1, const char *s2)
59 return (s1 && s2 && strncmp(s1, s2, strlen(s2)) == 0);
63 * Count the number of character $ch in string $s.
66 count_char(const char *s, int ch)
79 * Trim the leading whitespaces of the given string $s
80 * and return the trimed string.
85 while (isspace((unsigned char)*s))
92 * Trim the trailing whitespaces of the given string $s
93 * and return the trimed string.
100 while (l > 0 && isspace((unsigned char) s[l-1]))
109 * Swap the values of two integers.
120 * Divide integer $x by integer $y, rounding towards minus infinity.
123 div_floor(int x, int y)
127 if ((r != 0) && ((r < 0) != (y < 0)))
133 * Calculate the remainder of $x divided by $y; the result has the same
135 * Ref: Sec.(1.7), Eq.(1.17)
140 return x - y * div_floor(x, y);
144 mod_f(double x, double y)
146 return x - y * floor(x / y);
150 * Return the value of ($x % $y) with $y instead of 0, i.e., with value
151 * range being [1, $y].
156 return y + mod(x, -y);
160 * Calculate the interval modulus of $x, i.e., shifted into the range
161 * [$a, $b). Return $x if $a = $b.
162 * Ref: Sec.(1.7), Eq.(1.24)
165 mod3(int x, int a, int b)
170 return a + mod(x - a, b - a);
174 mod3_f(double x, double a, double b)
176 static const double eps = 1e-6;
178 if (fabs(a - b) < eps)
181 return a + mod_f(x - a, b - a);
186 * Calculate the sine value of degree $deg.
191 return sin(M_PI * deg / 180.0);
195 * Calculate the cosine value of degree $deg.
200 return cos(M_PI * deg / 180.0);
204 * Calculate the tangent value of degree $deg.
209 return tan(M_PI * deg / 180.0);
213 * Calculate the arc sine value (in degrees) of $x.
218 return asin(x) * 180.0 / M_PI;
222 * Calculate the arc cosine value (in degrees) of $x.
227 return acos(x) * 180.0 / M_PI;
231 * Calculate the arc tangent value (in degrees from 0 to 360) of $y / $x.
232 * Error if $x and $y are both zero.
235 arctan_deg(double y, double x)
238 double v = atan2(y, x);
240 errx(10, "%s(%g, %g) invalid!", __func__, y, x);
241 return mod_f(v * 180.0 / M_PI, 360);
245 * Convert angle in (degree, arcminute, arcsecond) to degree.
248 angle2deg(int deg, int min, double sec)
250 return deg + min/60.0 + sec/3600.0;
254 double poly(double x, const double *coefs, size_t n);
255 double invert_angular(double (*f)(double), double y, double a, double b);
257 void * xmalloc(size_t size);
258 void * xcalloc(size_t number, size_t size);
259 void * xrealloc(void *ptr, size_t size);
260 char * xstrdup(const char *str);
263 struct node * list_newnode(char *name, void *data);
264 struct node * list_addfront(struct node *listp, struct node *newp);
265 bool list_lookup(struct node *listp, const char *name,
266 int (*cmp)(const char *, const char *),
268 void list_freeall(struct node *listp, void (*free_name)(void *),
269 void (*free_data)(void *));