Only store frame number and not a pointer to frame_data structure in seq_analysis_item_t
[metze/wireshark/wip.git] / wsutil / str_util.c
1 /* str_util.c
2  * String utility routines
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include "config.h"
24
25 #include "str_util.h"
26 #include "ws_diag_control.h"
27
28 int
29 ws_xton(char ch)
30 {
31         switch (ch) {
32                 case '0': return 0;
33                 case '1': return 1;
34                 case '2': return 2;
35                 case '3': return 3;
36                 case '4': return 4;
37                 case '5': return 5;
38                 case '6': return 6;
39                 case '7': return 7;
40                 case '8': return 8;
41                 case '9': return 9;
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;
48                 default: return -1;
49         }
50 }
51
52 /* Convert all ASCII letters to lower case, in place. */
53 gchar *
54 ascii_strdown_inplace(gchar *str)
55 {
56         gchar *s;
57
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;
61
62         return (str);
63 }
64
65 /* Convert all ASCII letters to upper case, in place. */
66 gchar *
67 ascii_strup_inplace(gchar *str)
68 {
69         gchar *s;
70
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;
74
75         return (str);
76 }
77
78 /* Check if an entire string is printable. */
79 gboolean
80 isprint_string(const gchar *str)
81 {
82         guint pos;
83
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 */
88                         return FALSE;
89                 }
90         }
91
92         /* The string contains only printable characters */
93         return TRUE;
94 }
95
96 /* Check if an entire string is digits. */
97 gboolean
98 isdigit_string(guchar *str)
99 {
100         guint pos;
101
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 */
106                         return FALSE;
107                 }
108         }
109
110         /* The string contains only digits */
111         return TRUE;
112 }
113
114 #define FORMAT_SIZE_UNIT_MASK 0x00ff
115 #define FORMAT_SIZE_PFX_MASK 0xff00
116
117 #ifdef HAVE_GLIB_PRINTF_GROUPING
118 #define GROUP_FLAG "'"
119 #else
120 #define GROUP_FLAG ""
121 #endif
122
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. */
125 gchar *
126 format_size(gint64 size, format_size_flags_e flags)
127 {
128         GString *human_str = g_string_new("");
129         int power = 1000;
130         int pfx_off = 0;
131         gboolean is_small = FALSE;
132         static const gchar *prefix[] = {"T", "G", "M", "k", "Ti", "Gi", "Mi", "Ki"};
133         gchar *ret_val;
134
135         if ((flags & FORMAT_SIZE_PFX_MASK) == format_size_prefix_iec) {
136                 pfx_off = 4;
137                 power = 1024;
138         }
139
140 DIAG_OFF(format)
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]);
149         } else {
150                 g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d", size);
151                 is_small = TRUE;
152         }
153 DIAG_ON(format)
154
155         switch (flags & FORMAT_SIZE_UNIT_MASK) {
156                 case format_size_unit_none:
157                         break;
158                 case format_size_unit_bytes:
159                         g_string_append(human_str, is_small ? " bytes" : "B");
160                         break;
161                 case format_size_unit_bits:
162                         g_string_append(human_str, is_small ? " bits" : "b");
163                         break;
164                 case format_size_unit_bits_s:
165                         g_string_append(human_str, is_small ? " bits/s" : "bps");
166                         break;
167                 case format_size_unit_bytes_s:
168                         g_string_append(human_str, is_small ? " bytes/s" : "Bps");
169                         break;
170                 case format_size_unit_packets:
171                         g_string_append(human_str, is_small ? " packets" : "packets");
172                         break;
173                 case format_size_unit_packets_s:
174                         g_string_append(human_str, is_small ? " packets/s" : "packets/s");
175                         break;
176                 default:
177                         g_assert_not_reached();
178         }
179
180         ret_val = g_string_free(human_str, FALSE);
181         return g_strchomp(ret_val);
182 }
183
184 gchar
185 printable_char_or_period(gchar c)
186 {
187         return g_ascii_isprint(c) ? c : '.';
188 }
189
190 /*
191  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
192  *
193  * Local variables:
194  * c-basic-offset: 8
195  * tab-width: 8
196  * indent-tabs-mode: t
197  * End:
198  *
199  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
200  * :indentSize=8:tabSize=8:noTabs=false:
201  */