]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/util.c
Source Format Enforcement (#532)
[thirdparty/squid.git] / lib / util.c
CommitLineData
30a4f2a8 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
e25c139f 3 *
0545caaa
AJ
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.
30a4f2a8 7 */
7fc9f088 8
e25c139f 9#define _etext etext
0d94e9fe 10
f7f3304a 11#include "squid.h"
25f98340 12#include "util.h"
30a4f2a8 13
30a4f2a8 14#if HAVE_STRING_H
090089c4 15#include <string.h>
30a4f2a8 16#endif
673d7a4d 17#if HAVE_CTYPE_H
18#include <ctype.h>
19#endif
30a4f2a8 20#if HAVE_UNISTD_H
090089c4 21#include <unistd.h>
30a4f2a8 22#endif
02922e76 23#if HAVE_MATH_H
24#include <math.h>
25#endif
68b468e5 26
b8d8561b 27void
28Tolower(char *q)
673d7a4d 29{
30 char *s = q;
e45d4fb5 31
673d7a4d 32 while (*s) {
e4755e29 33 *s = xtolower(*s);
e45d4fb5 34 s++;
673d7a4d 35 }
36}
9d90e665 37
38int
88738790 39tvSubUsec(struct timeval t1, struct timeval t2)
40{
41 return (t2.tv_sec - t1.tv_sec) * 1000000 +
e45d4fb5 42 (t2.tv_usec - t1.tv_usec);
88738790 43}
44
260e56a3 45double
46tvSubDsec(struct timeval t1, struct timeval t2)
47{
48 return (double) (t2.tv_sec - t1.tv_sec) +
e45d4fb5 49 (double) (t2.tv_usec - t1.tv_usec) / 1000000.0;
260e56a3 50}
51
7021844c 52/* somewhat safer calculation of %s */
53double
54xpercent(double part, double whole)
55{
56 return xdiv(100 * part, whole);
57}
58
02922e76 59int
60xpercentInt(double part, double whole)
61{
4915be3b 62#if HAVE_RINT
ebd3f27a 63 return (int) rint(xpercent(part, whole));
4915be3b 64#else
65 /* SCO 3.2v4.2 doesn't have rint() -- mauri@mbp.ee */
ebd3f27a 66 return (int) floor(xpercent(part, whole) + 0.5);
4915be3b 67#endif
02922e76 68}
69
7021844c 70/* somewhat safer division */
71double
72xdiv(double nom, double denom)
73{
74 return (denom != 0.0) ? nom / denom : -1.0;
75}
de336bbe 76
77/* integer to string */
78const char *
79xitoa(int num)
80{
f53969cc 81 static char buf[24]; /* 2^64 = 18446744073709551616 */
de336bbe 82 snprintf(buf, sizeof(buf), "%d", num);
83 return buf;
84}
5ac23588 85
47f6e231 86/* int64_t to string */
87const char *
88xint64toa(int64_t num)
89{
f53969cc 90 static char buf[24]; /* 2^64 = 18446744073709551616 */
4d883d1f 91 snprintf(buf, sizeof(buf), "%" PRId64, num);
47f6e231 92 return buf;
93}
94
d96ceb8e 95void
96gb_flush(gb_t * g)
97{
98 g->gb += (g->bytes >> 30);
99 g->bytes &= (1 << 30) - 1;
100}
101
102double
103gb_to_double(const gb_t * g)
104{
105 return ((double) g->gb) * ((double) (1 << 30)) + ((double) g->bytes);
106}
107
108const char *
109double_to_str(char *buf, int buf_size, double value)
110{
111 /* select format */
e45d4fb5 112
d96ceb8e 113 if (value < 1e9)
e45d4fb5 114 snprintf(buf, buf_size, "%.2f MB", value / 1e6);
d96ceb8e 115 else if (value < 1e12)
e45d4fb5 116 snprintf(buf, buf_size, "%.3f GB", value / 1e9);
d96ceb8e 117 else
e45d4fb5 118 snprintf(buf, buf_size, "%.4f TB", value / 1e12);
119
d96ceb8e 120 return buf;
121}
122
123const char *
124gb_to_str(const gb_t * g)
125{
126 /*
127 * it is often convenient to call gb_to_str several times for _one_ printf
128 */
129#define max_cc_calls 5
130 typedef char GbBuf[32];
131 static GbBuf bufs[max_cc_calls];
132 static int call_id = 0;
133 double value = gb_to_double(g);
134 char *buf = bufs[call_id++];
e45d4fb5 135
d96ceb8e 136 if (call_id >= max_cc_calls)
e45d4fb5 137 call_id = 0;
138
d96ceb8e 139 /* select format */
140 if (value < 1e9)
e45d4fb5 141 snprintf(buf, sizeof(GbBuf), "%.2f MB", value / 1e6);
d96ceb8e 142 else if (value < 1e12)
e45d4fb5 143 snprintf(buf, sizeof(GbBuf), "%.2f GB", value / 1e9);
d96ceb8e 144 else
e45d4fb5 145 snprintf(buf, sizeof(GbBuf), "%.2f TB", value / 1e12);
146
d96ceb8e 147 return buf;
148}
43d1bbe4
FC
149
150/**
151 * rounds num to the next upper integer multiple of what
152 */
153unsigned int RoundTo(const unsigned int num, const unsigned int what)
154{
155 return what * ((num + what -1)/what);
156}
f53969cc 157