Debian Codegraph @

storage/innobase/ut/ut.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1994, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file ut/ut.cc
29 Various utilities for Innobase.
30
31 Created 5/11/1994 Heikki Tuuri
32 ********************************************************************/
33
34 /** NOTE: The functions in this file should only use functions from
35 other files in library. The code in this file is used to make a library for
36 external tools. */
37
38 #include <sys/types.h>
39 #include <time.h>
40
41 #include <iomanip>
42
43 #include "univ.i"
44 #include "ut/ut.h"
45
46 /** Prints the contents of a memory buffer in hex and ascii. */
47 void ut_print_buf(FILE *file, /*!< in: file where to print */
48 const void *buf, /*!< in: memory buffer */
49 ulint len) /*!< in: length of the buffer */
50 {
51 const byte *data;
52 ulint i;
53
54 UNIV_MEM_ASSERT_RW(buf, len);
55
56 fprintf(file, " len " ULINTPF "; hex ", len);
57
58 for (data = (const byte *)buf, i = 0; i < len; i++) {
59 fprintf(file, "%02lx", (ulong)*data++);
60 }
61
62 fputs("; asc ", file);
63
64 data = (const byte *)buf;
65
66 for (i = 0; i < len; i++) {
67 int c = (int)*data++;
68 putc(isprint(c) ? c : ' ', file);
69 }
70
71 putc(';', file);
72 }
73
74 /** Prints the contents of a memory buffer in hex.
75 @param[in,out] o Output stream
76 @param[in] buf Memory buffer
77 @param[in] len Length of the buffer */
78 void ut_print_buf_hex(std::ostream &o, const void *buf, ulint len) {
79 auto ptr = reinterpret_cast<const byte *>(buf);
80 const auto end = ptr + len;
81
82 o << "(0x";
83 while (ptr < end) {
84 o << std::setfill('0') << std::setw(2) << std::hex << (int)*ptr;
85 ++ptr;
86 }
87 o << ")";
88 }
89
90 /** Prints the contents of a memory buffer in hex and ascii.
91 @param[in,out] o Output stream
92 @param[in] buf Memory buffer
93 @param[in] len Length of the buffer */
94 void ut_print_buf(std::ostream &o, const void *buf, ulint len) {
95 const byte *data;
96 ulint i;
97
98 UNIV_MEM_ASSERT_RW(buf, len);
99
100 for (data = static_cast<const byte *>(buf), i = 0; i < len; i++) {
101 int c = static_cast<int>(*data++);
102 o << (isprint(c) ? static_cast<char>(c) : ' ');
103 }
104
105 ut_print_buf_hex(o, buf, len);
106 }
107
108 /** Prints a timestamp to a file. */
109 void ut_print_timestamp(FILE *file) /*!< in: file where to print */
110 {
111 #ifdef _WIN32
112 SYSTEMTIME cal_tm;
113
114 GetLocalTime(&cal_tm);
115
116 fprintf(file, "%d-%02d-%02d %02d:%02d:%02d %s", (int)cal_tm.wYear,
117 (int)cal_tm.wMonth, (int)cal_tm.wDay, (int)cal_tm.wHour,
118 (int)cal_tm.wMinute, (int)cal_tm.wSecond,
119 to_string(std::this_thread::get_id(), true).c_str());
120 #else
121 struct tm *cal_tm_ptr;
122 time_t tm;
123
124 struct tm cal_tm;
125 time(&tm);
126 localtime_r(&tm, &cal_tm);
127 cal_tm_ptr = &cal_tm;
128 fprintf(file, "%d-%02d-%02d %02d:%02d:%02d %s", cal_tm_ptr->tm_year + 1900,
129 cal_tm_ptr->tm_mon + 1, cal_tm_ptr->tm_mday, cal_tm_ptr->tm_hour,
130 cal_tm_ptr->tm_min, cal_tm_ptr->tm_sec,
131 to_string(std::this_thread::get_id()).c_str());
132 #endif /* _WIN32 */
133 }
134
135 /** Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
136 void ut_sprintf_timestamp(char *buf) /*!< in: buffer where to sprintf */
137 {
138 #ifdef _WIN32
139 SYSTEMTIME cal_tm;
140
141 GetLocalTime(&cal_tm);
142
143 sprintf(buf, "%02d%02d%02d %2d:%02d:%02d", (int)cal_tm.wYear % 100,
144 (int)cal_tm.wMonth, (int)cal_tm.wDay, (int)cal_tm.wHour,
145 (int)cal_tm.wMinute, (int)cal_tm.wSecond);
146 #else
147 struct tm *cal_tm_ptr;
148 time_t tm;
149
150 struct tm cal_tm;
151 time(&tm);
152 localtime_r(&tm, &cal_tm);
153 cal_tm_ptr = &cal_tm;
154 sprintf(buf, "%02d%02d%02d %2d:%02d:%02d", cal_tm_ptr->tm_year % 100,
155 cal_tm_ptr->tm_mon + 1, cal_tm_ptr->tm_mday, cal_tm_ptr->tm_hour,
156 cal_tm_ptr->tm_min, cal_tm_ptr->tm_sec);
157 #endif
158 }
159
160 /** Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
161 (size - 1) bytes of src, not the first.
162 @return strlen(src) */
163 ulint ut_strlcpy_rev(char *dst, /*!< in: destination buffer */
164 const char *src, /*!< in: source buffer */
165 ulint size) /*!< in: size of destination buffer */
166 {
167 ulint src_size = strlen(src);
168
169 if (size != 0) {
170 ulint n = std::min(src_size, size - 1);
171
172 memcpy(dst, src + src_size - n, n + 1);
173 }
174
175 return (src_size);
176 }