nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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 | |||
27 | int |
||
28 | ws_xton(char ch) |
||
29 | { |
||
30 | switch (ch) { |
||
31 | case '0': return 0; |
||
32 | case '1': return 1; |
||
33 | case '2': return 2; |
||
34 | case '3': return 3; |
||
35 | case '4': return 4; |
||
36 | case '5': return 5; |
||
37 | case '6': return 6; |
||
38 | case '7': return 7; |
||
39 | case '8': return 8; |
||
40 | case '9': return 9; |
||
41 | case 'a': case 'A': return 10; |
||
42 | case 'b': case 'B': return 11; |
||
43 | case 'c': case 'C': return 12; |
||
44 | case 'd': case 'D': return 13; |
||
45 | case 'e': case 'E': return 14; |
||
46 | case 'f': case 'F': return 15; |
||
47 | default: return -1; |
||
48 | } |
||
49 | } |
||
50 | |||
51 | /* Convert all ASCII letters to lower case, in place. */ |
||
52 | gchar * |
||
53 | ascii_strdown_inplace(gchar *str) |
||
54 | { |
||
55 | gchar *s; |
||
56 | |||
57 | for (s = str; *s; s++) |
||
58 | /* What 'g_ascii_tolower (gchar c)' does, this should be slightly more efficient */ |
||
59 | *s = g_ascii_isupper (*s) ? *s - 'A' + 'a' : *s; |
||
60 | |||
61 | return (str); |
||
62 | } |
||
63 | |||
64 | /* Convert all ASCII letters to upper case, in place. */ |
||
65 | gchar * |
||
66 | ascii_strup_inplace(gchar *str) |
||
67 | { |
||
68 | gchar *s; |
||
69 | |||
70 | for (s = str; *s; s++) |
||
71 | /* What 'g_ascii_toupper (gchar c)' does, this should be slightly more efficient */ |
||
72 | *s = g_ascii_islower (*s) ? *s - 'a' + 'A' : *s; |
||
73 | |||
74 | return (str); |
||
75 | } |
||
76 | |||
77 | /* Check if an entire string is printable. */ |
||
78 | gboolean |
||
79 | isprint_string(const gchar *str) |
||
80 | { |
||
81 | guint pos; |
||
82 | |||
83 | /* Loop until we reach the end of the string (a null) */ |
||
84 | for(pos = 0; str[pos] != '\0'; pos++){ |
||
85 | if(!g_ascii_isprint(str[pos])){ |
||
86 | /* The string contains a non-printable character */ |
||
87 | return FALSE; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /* The string contains only printable characters */ |
||
92 | return TRUE; |
||
93 | } |
||
94 | |||
95 | /* Check if an entire string is digits. */ |
||
96 | gboolean |
||
97 | isdigit_string(guchar *str) |
||
98 | { |
||
99 | guint pos; |
||
100 | |||
101 | /* Loop until we reach the end of the string (a null) */ |
||
102 | for(pos = 0; str[pos] != '\0'; pos++){ |
||
103 | if(!g_ascii_isdigit(str[pos])){ |
||
104 | /* The string contains a non-digit character */ |
||
105 | return FALSE; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /* The string contains only digits */ |
||
110 | return TRUE; |
||
111 | } |
||
112 | |||
113 | #define FORMAT_SIZE_UNIT_MASK 0x00ff |
||
114 | #define FORMAT_SIZE_PFX_MASK 0xff00 |
||
115 | |||
116 | #ifdef HAVE_GLIB_PRINTF_GROUPING |
||
117 | #define GROUP_FLAG "'" |
||
118 | #else |
||
119 | #define GROUP_FLAG "" |
||
120 | #endif |
||
121 | |||
122 | /* Given a size, return its value in a human-readable format */ |
||
123 | /* This doesn't handle fractional values. We might want to make size a double. */ |
||
124 | gchar * |
||
125 | format_size(gint64 size, format_size_flags_e flags) |
||
126 | { |
||
127 | GString *human_str = g_string_new(""); |
||
128 | int power = 1000; |
||
129 | int pfx_off = 0; |
||
130 | gboolean is_small = FALSE; |
||
131 | static const gchar *prefix[] = {"T", "G", "M", "k", "Ti", "Gi", "Mi", "Ki"}; |
||
132 | gchar *ret_val; |
||
133 | |||
134 | if ((flags & FORMAT_SIZE_PFX_MASK) == format_size_prefix_iec) { |
||
135 | pfx_off = 4; |
||
136 | power = 1024; |
||
137 | } |
||
138 | |||
139 | DIAG_OFF(format) |
||
140 | if (size / power / power / power / power >= 10) { |
||
141 | g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d %s", size / power / power / power / power, prefix[pfx_off]); |
||
142 | } else if (size / power / power / power >= 10) { |
||
143 | g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d %s", size / power / power / power, prefix[pfx_off+1]); |
||
144 | } else if (size / power / power >= 10) { |
||
145 | g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d %s", size / power / power, prefix[pfx_off+2]); |
||
146 | } else if (size / power >= 10) { |
||
147 | g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d %s", size / power, prefix[pfx_off+3]); |
||
148 | } else { |
||
149 | g_string_printf(human_str, "%" GROUP_FLAG G_GINT64_MODIFIER "d", size); |
||
150 | is_small = TRUE; |
||
151 | } |
||
152 | DIAG_ON(format) |
||
153 | |||
154 | switch (flags & FORMAT_SIZE_UNIT_MASK) { |
||
155 | case format_size_unit_none: |
||
156 | break; |
||
157 | case format_size_unit_bytes: |
||
158 | g_string_append(human_str, is_small ? " bytes" : "B"); |
||
159 | break; |
||
160 | case format_size_unit_bits: |
||
161 | g_string_append(human_str, is_small ? " bits" : "b"); |
||
162 | break; |
||
163 | case format_size_unit_bits_s: |
||
164 | g_string_append(human_str, is_small ? " bits/s" : "bps"); |
||
165 | break; |
||
166 | case format_size_unit_bytes_s: |
||
167 | g_string_append(human_str, is_small ? " bytes/s" : "Bps"); |
||
168 | break; |
||
169 | case format_size_unit_packets: |
||
170 | g_string_append(human_str, is_small ? " packets" : "packets"); |
||
171 | break; |
||
172 | case format_size_unit_packets_s: |
||
173 | g_string_append(human_str, is_small ? " packets/s" : "packets/s"); |
||
174 | break; |
||
175 | default: |
||
176 | g_assert_not_reached(); |
||
177 | } |
||
178 | |||
179 | ret_val = g_string_free(human_str, FALSE); |
||
180 | return g_strchomp(ret_val); |
||
181 | } |
||
182 | |||
183 | gchar |
||
184 | printable_char_or_period(gchar c) |
||
185 | { |
||
186 | return g_ascii_isprint(c) ? c : '.'; |
||
187 | } |
||
188 | |||
189 | /* |
||
190 | * Editor modelines - http://www.wireshark.org/tools/modelines.html |
||
191 | * |
||
192 | * Local variables: |
||
193 | * c-basic-offset: 8 |
||
194 | * tab-width: 8 |
||
195 | * indent-tabs-mode: t |
||
196 | * End: |
||
197 | * |
||
198 | * vi: set shiftwidth=8 tabstop=8 noexpandtab: |
||
199 | * :indentSize=8:tabSize=8:noTabs=false: |
||
200 | */ |