2 * String utility routines
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 /* Convert all ASCII letters to lower case, in place. */
34 ascii_strdown_inplace(gchar
*str
)
38 for (s
= str
; *s
; s
++)
39 *s
= g_ascii_tolower (*s
);
44 /* Convert all ASCII letters to upper case, in place. */
46 ascii_strup_inplace(gchar
*str
)
50 for (s
= str
; *s
; s
++)
51 *s
= g_ascii_toupper (*s
);
56 /* Check if an entire string is printable. */
58 isprint_string(const gchar
*str
)
62 /* Loop until we reach the end of the string (a null) */
63 for(pos
= 0; str
[pos
] != '\0'; pos
++){
64 if(!isprint(str
[pos
])){
65 /* The string contains a non-printable character */
70 /* The string contains only printable characters */
74 /* Check if an entire string is digits. */
76 isdigit_string(guchar
*str
)
80 /* Loop until we reach the end of the string (a null) */
81 for(pos
= 0; str
[pos
] != '\0'; pos
++){
82 if(!isdigit(str
[pos
])){
83 /* The string contains a non-digit character */
88 /* The string contains only digits */
92 #define FORMAT_SIZE_UNIT_MASK 0x00ff
93 #define FORMAT_SIZE_PFX_MASK 0xff00
95 #ifdef HAVE_GLIB_PRINTF_GROUPING
96 #define GROUP_FLAG "'"
101 /* Given a size, return its value in a human-readable format */
102 gchar
*format_size(gint64 size
, format_size_flags_e flags
) {
103 GString
*human_str
= g_string_new("");
106 gboolean is_small
= FALSE
;
107 static const gchar
*prefix
[] = {"T", "G", "M", "k", "Ti", "Gi", "Mi", "Ki"};
110 if ((flags
& FORMAT_SIZE_PFX_MASK
) == format_size_prefix_iec
) {
115 if (size
/ power
/ power
/ power
/ power
>= 10) {
116 g_string_printf(human_str
, "%" GROUP_FLAG G_GINT64_MODIFIER
"d %s", size
/ power
/ power
/ power
/ power
, prefix
[pfx_off
]);
117 } else if (size
/ power
/ power
/ power
>= 10) {
118 g_string_printf(human_str
, "%" GROUP_FLAG G_GINT64_MODIFIER
"d %s", size
/ power
/ power
/ power
, prefix
[pfx_off
+1]);
119 } else if (size
/ power
/ power
>= 10) {
120 g_string_printf(human_str
, "%" GROUP_FLAG G_GINT64_MODIFIER
"d %s", size
/ power
/ power
, prefix
[pfx_off
+2]);
121 } else if (size
/ power
>= 10) {
122 g_string_printf(human_str
, "%" GROUP_FLAG G_GINT64_MODIFIER
"d %s", size
/ power
, prefix
[pfx_off
+3]);
124 g_string_printf(human_str
, "%" GROUP_FLAG G_GINT64_MODIFIER
"d ", size
);
128 switch (flags
& FORMAT_SIZE_UNIT_MASK
) {
129 case format_size_unit_none
:
131 case format_size_unit_bytes
:
132 g_string_append(human_str
, is_small
? "bytes" : "B");
134 case format_size_unit_bits
:
135 g_string_append(human_str
, is_small
? "bits" : "b");
137 case format_size_unit_bits_s
:
138 g_string_append(human_str
, is_small
? "bits/s" : "bps");
140 case format_size_unit_bytes_s
:
141 g_string_append(human_str
, is_small
? "bytes/s" : "Bps");
144 g_assert_not_reached();
147 ret_val
= human_str
->str
;
148 g_string_free(human_str
, FALSE
);