DCERPC: factor out proto_tree_add_dcerpc_drep()
[wireshark-wip.git] / wsutil / str_util.c
blob9c0aa6e8021e76bce1ac662efce9deec33867d41
1 /* str_util.c
2 * String utility routines
4 * $Id$
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.
25 #include "config.h"
27 #include <glib.h>
28 #include "str_util.h"
30 #include <ctype.h>
32 /* Convert all ASCII letters to lower case, in place. */
33 gchar *
34 ascii_strdown_inplace(gchar *str)
36 gchar *s;
38 for (s = str; *s; s++)
39 *s = g_ascii_tolower (*s);
41 return (str);
44 /* Convert all ASCII letters to upper case, in place. */
45 gchar *
46 ascii_strup_inplace(gchar *str)
48 gchar *s;
50 for (s = str; *s; s++)
51 *s = g_ascii_toupper (*s);
53 return (str);
56 /* Check if an entire string is printable. */
57 gboolean
58 isprint_string(const gchar *str)
60 guint pos;
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 */
66 return FALSE;
70 /* The string contains only printable characters */
71 return TRUE;
74 /* Check if an entire string is digits. */
75 gboolean
76 isdigit_string(guchar *str)
78 guint pos;
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 */
84 return FALSE;
88 /* The string contains only digits */
89 return TRUE;
92 #define FORMAT_SIZE_UNIT_MASK 0x00ff
93 #define FORMAT_SIZE_PFX_MASK 0xff00
95 #ifdef HAVE_GLIB_PRINTF_GROUPING
96 #define GROUP_FLAG "'"
97 #else
98 #define GROUP_FLAG ""
99 #endif
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("");
104 int power = 1000;
105 int pfx_off = 0;
106 gboolean is_small = FALSE;
107 static const gchar *prefix[] = {"T", "G", "M", "k", "Ti", "Gi", "Mi", "Ki"};
108 gchar *ret_val;
110 if ((flags & FORMAT_SIZE_PFX_MASK) == format_size_prefix_iec) {
111 pfx_off = 4;
112 power = 1024;
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]);
123 } else {
124 g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d ", size);
125 is_small = TRUE;
128 switch (flags & FORMAT_SIZE_UNIT_MASK) {
129 case format_size_unit_none:
130 break;
131 case format_size_unit_bytes:
132 g_string_append(human_str, is_small ? "bytes" : "B");
133 break;
134 case format_size_unit_bits:
135 g_string_append(human_str, is_small ? "bits" : "b");
136 break;
137 case format_size_unit_bits_s:
138 g_string_append(human_str, is_small ? "bits/s" : "bps");
139 break;
140 case format_size_unit_bytes_s:
141 g_string_append(human_str, is_small ? "bytes/s" : "Bps");
142 break;
143 default:
144 g_assert_not_reached();
147 ret_val = human_str->str;
148 g_string_free(human_str, FALSE);
149 return ret_val;