]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/util.cc
Remove unnecessary C linkage and unused code (#1677)
[thirdparty/squid.git] / lib / util.cc
1 /*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #define _etext etext
10
11 #include "squid.h"
12 #include "util.h"
13
14 #if HAVE_STRING_H
15 #include <string.h>
16 #endif
17 #if HAVE_CTYPE_H
18 #include <ctype.h>
19 #endif
20 #if HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23 #if HAVE_MATH_H
24 #include <math.h>
25 #endif
26
27 void
28 Tolower(char *q)
29 {
30 char *s = q;
31
32 while (*s) {
33 *s = xtolower(*s);
34 s++;
35 }
36 }
37
38 /* somewhat safer calculation of %s */
39 double
40 xpercent(double part, double whole)
41 {
42 return xdiv(100 * part, whole);
43 }
44
45 int
46 xpercentInt(double part, double whole)
47 {
48 return (int) rint(xpercent(part, whole));
49 }
50
51 /* somewhat safer division */
52 double
53 xdiv(double nom, double denom)
54 {
55 return (denom != 0.0) ? nom / denom : -1.0;
56 }
57
58 /* integer to string */
59 const char *
60 xitoa(int num)
61 {
62 static char buf[24]; /* 2^64 = 18446744073709551616 */
63 snprintf(buf, sizeof(buf), "%d", num);
64 return buf;
65 }
66
67 /* int64_t to string */
68 const char *
69 xint64toa(int64_t num)
70 {
71 static char buf[24]; /* 2^64 = 18446744073709551616 */
72 snprintf(buf, sizeof(buf), "%" PRId64, num);
73 return buf;
74 }
75
76 const char *
77 double_to_str(char *buf, int buf_size, double value)
78 {
79 /* select format */
80
81 if (value < 1e9)
82 snprintf(buf, buf_size, "%.2f MB", value / 1e6);
83 else if (value < 1e12)
84 snprintf(buf, buf_size, "%.3f GB", value / 1e9);
85 else
86 snprintf(buf, buf_size, "%.4f TB", value / 1e12);
87
88 return buf;
89 }
90