2 * String utility routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "ws_diag_control.h"
42 case 'a': case 'A': return 10;
43 case 'b': case 'B': return 11;
44 case 'c': case 'C': return 12;
45 case 'd': case 'D': return 13;
46 case 'e': case 'E': return 14;
47 case 'f': case 'F': return 15;
52 /* Convert all ASCII letters to lower case, in place. */
54 ascii_strdown_inplace(gchar *str)
58 for (s = str; *s; s++)
59 /* What 'g_ascii_tolower (gchar c)' does, this should be slightly more efficient */
60 *s = g_ascii_isupper (*s) ? *s - 'A' + 'a' : *s;
65 /* Convert all ASCII letters to upper case, in place. */
67 ascii_strup_inplace(gchar *str)
71 for (s = str; *s; s++)
72 /* What 'g_ascii_toupper (gchar c)' does, this should be slightly more efficient */
73 *s = g_ascii_islower (*s) ? *s - 'a' + 'A' : *s;
78 /* Check if an entire string is printable. */
80 isprint_string(const gchar *str)
84 /* Loop until we reach the end of the string (a null) */
85 for(pos = 0; str[pos] != '\0'; pos++){
86 if(!g_ascii_isprint(str[pos])){
87 /* The string contains a non-printable character */
92 /* The string contains only printable characters */
96 /* Check if an entire string is digits. */
98 isdigit_string(guchar *str)
102 /* Loop until we reach the end of the string (a null) */
103 for(pos = 0; str[pos] != '\0'; pos++){
104 if(!g_ascii_isdigit(str[pos])){
105 /* The string contains a non-digit character */
110 /* The string contains only digits */
114 #define FORMAT_SIZE_UNIT_MASK 0x00ff
115 #define FORMAT_SIZE_PFX_MASK 0xff00
117 #ifdef HAVE_GLIB_PRINTF_GROUPING
118 #define GROUP_FLAG "'"
120 #define GROUP_FLAG ""
123 /* Given a size, return its value in a human-readable format */
124 /* This doesn't handle fractional values. We might want to make size a double. */
126 format_size(gint64 size, format_size_flags_e flags)
128 GString *human_str = g_string_new("");
131 gboolean is_small = FALSE;
132 static const gchar *prefix[] = {"T", "G", "M", "k", "Ti", "Gi", "Mi", "Ki"};
135 if ((flags & FORMAT_SIZE_PFX_MASK) == format_size_prefix_iec) {
141 if (size / power / power / power / power >= 10) {
142 g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d %s", size / power / power / power / power, prefix[pfx_off]);
143 } else if (size / power / power / power >= 10) {
144 g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d %s", size / power / power / power, prefix[pfx_off+1]);
145 } else if (size / power / power >= 10) {
146 g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d %s", size / power / power, prefix[pfx_off+2]);
147 } else if (size / power >= 10) {
148 g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d %s", size / power, prefix[pfx_off+3]);
150 g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d", size);
155 switch (flags & FORMAT_SIZE_UNIT_MASK) {
156 case format_size_unit_none:
158 case format_size_unit_bytes:
159 g_string_append(human_str, is_small ? " bytes" : "B");
161 case format_size_unit_bits:
162 g_string_append(human_str, is_small ? " bits" : "b");
164 case format_size_unit_bits_s:
165 g_string_append(human_str, is_small ? " bits/s" : "bps");
167 case format_size_unit_bytes_s:
168 g_string_append(human_str, is_small ? " bytes/s" : "Bps");
170 case format_size_unit_packets:
171 g_string_append(human_str, is_small ? " packets" : "packets");
173 case format_size_unit_packets_s:
174 g_string_append(human_str, is_small ? " packets/s" : "packets/s");
177 g_assert_not_reached();
180 ret_val = g_string_free(human_str, FALSE);
181 return g_strchomp(ret_val);
185 printable_char_or_period(gchar c)
187 return g_ascii_isprint(c) ? c : '.';
191 * Editor modelines - http://www.wireshark.org/tools/modelines.html
196 * indent-tabs-mode: t
199 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
200 * :indentSize=8:tabSize=8:noTabs=false: