util.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 int
39 tvSubUsec(struct timeval t1, struct timeval t2)
40 {
41  return (t2.tv_sec - t1.tv_sec) * 1000000 +
42  (t2.tv_usec - t1.tv_usec);
43 }
44 
45 double
46 tvSubDsec(struct timeval t1, struct timeval t2)
47 {
48  return (double) (t2.tv_sec - t1.tv_sec) +
49  (double) (t2.tv_usec - t1.tv_usec) / 1000000.0;
50 }
51 
52 /* somewhat safer calculation of %s */
53 double
54 xpercent(double part, double whole)
55 {
56  return xdiv(100 * part, whole);
57 }
58 
59 int
60 xpercentInt(double part, double whole)
61 {
62 #if HAVE_RINT
63  return (int) rint(xpercent(part, whole));
64 #else
65  /* SCO 3.2v4.2 doesn't have rint() -- mauri@mbp.ee */
66  return (int) floor(xpercent(part, whole) + 0.5);
67 #endif
68 }
69 
70 /* somewhat safer division */
71 double
72 xdiv(double nom, double denom)
73 {
74  return (denom != 0.0) ? nom / denom : -1.0;
75 }
76 
77 /* integer to string */
78 const char *
79 xitoa(int num)
80 {
81  static char buf[24]; /* 2^64 = 18446744073709551616 */
82  snprintf(buf, sizeof(buf), "%d", num);
83  return buf;
84 }
85 
86 /* int64_t to string */
87 const char *
88 xint64toa(int64_t num)
89 {
90  static char buf[24]; /* 2^64 = 18446744073709551616 */
91  snprintf(buf, sizeof(buf), "%" PRId64, num);
92  return buf;
93 }
94 
95 void
97 {
98  g->gb += (g->bytes >> 30);
99  g->bytes &= (1 << 30) - 1;
100 }
101 
102 double
103 gb_to_double(const gb_t * g)
104 {
105  return ((double) g->gb) * ((double) (1 << 30)) + ((double) g->bytes);
106 }
107 
108 const char *
109 double_to_str(char *buf, int buf_size, double value)
110 {
111  /* select format */
112 
113  if (value < 1e9)
114  snprintf(buf, buf_size, "%.2f MB", value / 1e6);
115  else if (value < 1e12)
116  snprintf(buf, buf_size, "%.3f GB", value / 1e9);
117  else
118  snprintf(buf, buf_size, "%.4f TB", value / 1e12);
119 
120  return buf;
121 }
122 
123 const char *
124 gb_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++];
135 
136  if (call_id >= max_cc_calls)
137  call_id = 0;
138 
139  /* select format */
140  if (value < 1e9)
141  snprintf(buf, sizeof(GbBuf), "%.2f MB", value / 1e6);
142  else if (value < 1e12)
143  snprintf(buf, sizeof(GbBuf), "%.2f GB", value / 1e9);
144  else
145  snprintf(buf, sizeof(GbBuf), "%.2f TB", value / 1e12);
146 
147  return buf;
148 }
149 
153 unsigned int RoundTo(const unsigned int num, const unsigned int what)
154 {
155  return what * ((num + what -1)/what);
156 }
157 
double xdiv(double nom, double denom)
Definition: util.c:72
#define PRId64
Definition: types.h:110
const char * double_to_str(char *buf, int buf_size, double value)
Definition: util.c:109
double xpercent(double part, double whole)
Definition: util.c:54
size_t bytes
Definition: util.h:34
const char * xint64toa(int64_t num)
Definition: util.c:88
int xpercentInt(double part, double whole)
Definition: util.c:60
double tvSubDsec(struct timeval t1, struct timeval t2)
Definition: util.c:46
void const char * buf
Definition: stub_helper.cc:16
const char * xitoa(int num)
Definition: util.c:79
Definition: util.h:32
int tvSubUsec(struct timeval t1, struct timeval t2)
Definition: util.c:39
const char * gb_to_str(const gb_t *g)
Definition: util.c:124
unsigned int RoundTo(const unsigned int num, const unsigned int what)
Definition: util.c:153
size_t gb
Definition: util.h:35
void Tolower(char *q)
Definition: util.c:28
void gb_flush(gb_t *g)
Definition: util.c:96
#define xtolower(x)
Definition: xis.h:19
#define max_cc_calls
double gb_to_double(const gb_t *g)
Definition: util.c:103

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors