Squid Web Cache master
Loading...
Searching...
No Matches
util.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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
27void
28Tolower(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 */
39double
40xpercent(double part, double whole)
41{
42 return xdiv(100 * part, whole);
43}
44
45int
46xpercentInt(double part, double whole)
47{
48 return (int) rint(xpercent(part, whole));
49}
50
51/* somewhat safer division */
52double
53xdiv(double nom, double denom)
54{
55 return (denom != 0.0) ? nom / denom : -1.0;
56}
57
58/* integer to string */
59const char *
60xitoa(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 */
68const char *
69xint64toa(int64_t num)
70{
71 static char buf[24]; /* 2^64 = 18446744073709551616 */
72 snprintf(buf, sizeof(buf), "%" PRId64, num);
73 return buf;
74}
75
76const char *
77double_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
#define PRId64
Definition types.h:104
double xpercent(double part, double whole)
Definition util.cc:40
double xdiv(double nom, double denom)
Definition util.cc:53
const char * xitoa(int num)
Definition util.cc:60
const char * double_to_str(char *buf, int buf_size, double value)
Definition util.cc:77
int xpercentInt(double part, double whole)
Definition util.cc:46
void Tolower(char *q)
Definition util.cc:28
const char * xint64toa(int64_t num)
Definition util.cc:69
#define xtolower(x)
Definition xis.h:17