util.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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
76void
78{
79 g->gb += (g->bytes >> 30);
80 g->bytes &= (1 << 30) - 1;
81}
82
83double
85{
86 return ((double) g->gb) * ((double) (1 << 30)) + ((double) g->bytes);
87}
88
89const char *
90double_to_str(char *buf, int buf_size, double value)
91{
92 /* select format */
93
94 if (value < 1e9)
95 snprintf(buf, buf_size, "%.2f MB", value / 1e6);
96 else if (value < 1e12)
97 snprintf(buf, buf_size, "%.3f GB", value / 1e9);
98 else
99 snprintf(buf, buf_size, "%.4f TB", value / 1e12);
100
101 return buf;
102}
103
104const char *
105gb_to_str(const gb_t * g)
106{
107 /*
108 * it is often convenient to call gb_to_str several times for _one_ printf
109 */
110#define max_cc_calls 5
111 typedef char GbBuf[32];
112 static GbBuf bufs[max_cc_calls];
113 static int call_id = 0;
114 double value = gb_to_double(g);
115 char *buf = bufs[call_id++];
116
117 if (call_id >= max_cc_calls)
118 call_id = 0;
119
120 /* select format */
121 if (value < 1e9)
122 snprintf(buf, sizeof(GbBuf), "%.2f MB", value / 1e6);
123 else if (value < 1e12)
124 snprintf(buf, sizeof(GbBuf), "%.2f GB", value / 1e9);
125 else
126 snprintf(buf, sizeof(GbBuf), "%.2f TB", value / 1e12);
127
128 return buf;
129}
130
134unsigned int RoundTo(const unsigned int num, const unsigned int what)
135{
136 return what * ((num + what -1)/what);
137}
138
Definition: util.h:25
size_t bytes
Definition: util.h:27
size_t gb
Definition: util.h:28
void EVH void double
Definition: stub_event.cc:16
#define PRId64
Definition: types.h:110
const char * gb_to_str(const gb_t *g)
Definition: util.c:105
double xpercent(double part, double whole)
Definition: util.c:40
double xdiv(double nom, double denom)
Definition: util.c:53
unsigned int RoundTo(const unsigned int num, const unsigned int what)
Definition: util.c:134
const char * xitoa(int num)
Definition: util.c:60
double gb_to_double(const gb_t *g)
Definition: util.c:84
const char * double_to_str(char *buf, int buf_size, double value)
Definition: util.c:90
int xpercentInt(double part, double whole)
Definition: util.c:46
void Tolower(char *q)
Definition: util.c:28
#define max_cc_calls
const char * xint64toa(int64_t num)
Definition: util.c:69
void gb_flush(gb_t *g)
Definition: util.c:77
#define xtolower(x)
Definition: xis.h:19

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors