wsutil: Split format_size() enum

Use an enum to select units and a bit flag for the other options,
currently only prefix type.
This commit is contained in:
João Valverde 2021-11-29 19:29:55 +00:00 committed by Wireshark GitLab Utility
parent 51f2a56b7c
commit 504de90a3c
15 changed files with 67 additions and 72 deletions

View File

@ -674,7 +674,7 @@ print_stats(const gchar *filename, capture_info *cf_info)
if (machine_readable) {
printf ("%u\n", cf_info->packet_count);
} else {
size_string = format_size(cf_info->packet_count, format_size_unit_none);
size_string = format_size(cf_info->packet_count, FORMAT_SIZE_UNIT_NONE, 0);
printf ("%s\n", size_string);
g_free(size_string);
}
@ -684,7 +684,7 @@ print_stats(const gchar *filename, capture_info *cf_info)
if (machine_readable) {
printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
} else {
size_string = format_size(cf_info->filesize, format_size_unit_bytes);
size_string = format_size(cf_info->filesize, FORMAT_SIZE_UNIT_BYTES, 0);
printf ("%s\n", size_string);
g_free(size_string);
}
@ -694,7 +694,7 @@ print_stats(const gchar *filename, capture_info *cf_info)
if (machine_readable) {
printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
} else {
size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
size_string = format_size(cf_info->packet_bytes, FORMAT_SIZE_UNIT_BYTES, 0);
printf ("%s\n", size_string);
g_free(size_string);
}
@ -711,7 +711,7 @@ print_stats(const gchar *filename, capture_info *cf_info)
if (machine_readable) {
print_value("", 2, " bytes/sec", cf_info->data_rate);
} else {
size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
size_string = format_size((int64_t)cf_info->data_rate, FORMAT_SIZE_UNIT_BYTES_S, 0);
printf ("%s\n", size_string);
g_free(size_string);
}
@ -721,7 +721,7 @@ print_stats(const gchar *filename, capture_info *cf_info)
if (machine_readable) {
print_value("", 2, " bits/sec", cf_info->data_rate*8);
} else {
size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
size_string = format_size((int64_t)(cf_info->data_rate*8), FORMAT_SIZE_UNIT_BITS_S, 0);
printf ("%s\n", size_string);
g_free(size_string);
}
@ -734,7 +734,7 @@ print_stats(const gchar *filename, capture_info *cf_info)
if (machine_readable) {
print_value("", 2, " packets/sec", cf_info->packet_rate);
} else {
size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
size_string = format_size((int64_t)cf_info->packet_rate, FORMAT_SIZE_UNIT_PACKETS_S, 0);
printf ("%s\n", size_string);
g_free(size_string);
}

View File

@ -2775,7 +2775,7 @@ dissect_meta_record_tags(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
case ERF_META_TAG_if_speed:
case ERF_META_TAG_if_tx_speed:
value64 = tvb_get_ntoh64(tvb, offset + 4);
tmp = format_size((gint64) value64, (format_size_flags_e)(format_size_unit_bits_s|format_size_prefix_si));
tmp = format_size((int64_t)value64, FORMAT_SIZE_UNIT_BITS_S, FORMAT_SIZE_PREFIX_SI);
tag_pi = proto_tree_add_uint64_format_value(section_tree, tag_info->hf_value, tvb, offset + 4, taglength, value64, "%s (%" G_GINT64_MODIFIER "u bps)", tmp, value64);
g_free(tmp);
break;
@ -2806,7 +2806,7 @@ dissect_meta_record_tags(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
case ERF_META_TAG_mem:
value64 = tvb_get_ntoh64(tvb, offset + 4);
tmp = format_size((gint64) value64, (format_size_flags_e)(format_size_unit_bytes|format_size_prefix_iec));
tmp = format_size((int64_t)value64, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_IEC);
tag_pi = proto_tree_add_uint64_format_value(section_tree, tag_info->hf_value, tvb, offset + 4, taglength, value64, "%s (%" G_GINT64_MODIFIER"u bytes)", tmp, value64);
g_free(tmp);
break;

View File

@ -98,9 +98,9 @@ iousers_draw(void *arg)
if (tot_frames == last_frames) {
char *rx_bytes, *tx_bytes, *total_bytes;
rx_bytes = format_size(iui->rx_bytes, format_size_unit_bytes);
tx_bytes = format_size(iui->tx_bytes, format_size_unit_bytes);
total_bytes = format_size(iui->tx_bytes + iui->rx_bytes, format_size_unit_bytes);
rx_bytes = format_size(iui->rx_bytes, FORMAT_SIZE_UNIT_BYTES, 0);
tx_bytes = format_size(iui->tx_bytes, FORMAT_SIZE_UNIT_BYTES, 0);
total_bytes = format_size(iui->tx_bytes + iui->rx_bytes, FORMAT_SIZE_UNIT_BYTES, 0);
/* XXX - TODO: make name / port resolution configurable (through gbl_resolv_flags?) */
src_addr = get_conversation_address(NULL, &iui->src_address, TRUE);

View File

@ -949,7 +949,7 @@ void CaptureFileDialog::preview(const QString & path)
// Size
gint64 filesize = wtap_file_size(wth, &err);
// Finder and Windows Explorer use IEC. What do the various Linux file managers use?
QString size_str(gchar_free_to_qstring(format_size(filesize, format_size_unit_bytes|format_size_prefix_iec)));
QString size_str(gchar_free_to_qstring(format_size(filesize, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_IEC)));
status = get_stats_for_preview(wth, &stats, &err, &err_info);

View File

@ -483,15 +483,15 @@ QString CaptureFilePropertiesDialog::summaryToHtml()
captured_str = displayed_str = marked_str = n_a;
if (seconds > 0) {
captured_str =
gchar_free_to_qstring(format_size(summary.bytes / seconds, format_size_unit_none|format_size_prefix_si));
gchar_free_to_qstring(format_size(summary.bytes / seconds, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
}
if (disp_seconds > 0) {
displayed_str =
gchar_free_to_qstring(format_size(summary.filtered_bytes / disp_seconds, format_size_unit_none|format_size_prefix_si));
gchar_free_to_qstring(format_size(summary.filtered_bytes / disp_seconds, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
}
if (marked_seconds > 0) {
marked_str =
gchar_free_to_qstring(format_size(summary.marked_bytes / marked_seconds, format_size_unit_none|format_size_prefix_si));
gchar_free_to_qstring(format_size(summary.marked_bytes / marked_seconds, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
}
out << table_row_begin
<< table_data_tmpl.arg(tr("Average bytes/s"))
@ -504,15 +504,15 @@ QString CaptureFilePropertiesDialog::summaryToHtml()
captured_str = displayed_str = marked_str = n_a;
if (seconds > 0) {
captured_str =
gchar_free_to_qstring(format_size(summary.bytes * 8 / seconds, format_size_unit_none|format_size_prefix_si));
gchar_free_to_qstring(format_size(summary.bytes * 8 / seconds, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
}
if (disp_seconds > 0) {
displayed_str =
gchar_free_to_qstring(format_size(summary.filtered_bytes * 8 / disp_seconds, format_size_unit_none|format_size_prefix_si));
gchar_free_to_qstring(format_size(summary.filtered_bytes * 8 / disp_seconds, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
}
if (marked_seconds > 0) {
marked_str =
gchar_free_to_qstring(format_size(summary.marked_bytes * 8 / marked_seconds, format_size_unit_none|format_size_prefix_si));
gchar_free_to_qstring(format_size(summary.marked_bytes * 8 / marked_seconds, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
}
out << table_row_begin
<< table_data_tmpl.arg(tr("Average bits/s"))

View File

@ -369,15 +369,15 @@ public:
case CONV_COLUMN_PACKETS:
return QString("%L1").arg(conv_item->tx_frames + conv_item->rx_frames);
case CONV_COLUMN_BYTES:
return gchar_free_to_qstring(format_size(conv_item->tx_bytes + conv_item->rx_bytes, format_size_unit_none|format_size_prefix_si));
return gchar_free_to_qstring(format_size(conv_item->tx_bytes + conv_item->rx_bytes, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
case CONV_COLUMN_PKT_AB:
return QString("%L1").arg(conv_item->tx_frames);
case CONV_COLUMN_BYTES_AB:
return gchar_free_to_qstring(format_size(conv_item->tx_bytes, format_size_unit_none|format_size_prefix_si));
return gchar_free_to_qstring(format_size(conv_item->tx_bytes, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
case CONV_COLUMN_PKT_BA:
return QString("%L1").arg(conv_item->rx_frames);
case CONV_COLUMN_BYTES_BA:
return gchar_free_to_qstring(format_size(conv_item->rx_bytes, format_size_unit_none|format_size_prefix_si));
return gchar_free_to_qstring(format_size(conv_item->rx_bytes, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
case CONV_COLUMN_START:
{
bool use_ns = treeWidget()->window()->property("nanosecond_precision").toBool();
@ -406,12 +406,12 @@ public:
}
case CONV_COLUMN_BPS_AB:
if (duration > min_bw_calc_duration_) {
bps_ab = gchar_free_to_qstring(format_size((gint64) conv_item->tx_bytes * 8 / duration, format_size_unit_none|format_size_prefix_si));
bps_ab = gchar_free_to_qstring(format_size((gint64) conv_item->tx_bytes * 8 / duration, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
}
return bps_ab;
case CONV_COLUMN_BPS_BA:
if (duration > min_bw_calc_duration_) {
bps_ba = gchar_free_to_qstring(format_size((gint64) conv_item->rx_bytes * 8 / duration, format_size_unit_none|format_size_prefix_si));
bps_ba = gchar_free_to_qstring(format_size((gint64) conv_item->rx_bytes * 8 / duration, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
}
return bps_ba;
default:

View File

@ -331,15 +331,15 @@ public:
case ENDP_COLUMN_PACKETS:
return QString("%L1").arg(endp_item->tx_frames + endp_item->rx_frames);
case ENDP_COLUMN_BYTES:
return gchar_free_to_qstring(format_size(endp_item->tx_bytes + endp_item->rx_bytes, format_size_unit_none|format_size_prefix_si));
return gchar_free_to_qstring(format_size(endp_item->tx_bytes + endp_item->rx_bytes, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
case ENDP_COLUMN_PKT_AB:
return QString("%L1").arg(endp_item->tx_frames);
case ENDP_COLUMN_BYTES_AB:
return gchar_free_to_qstring(format_size(endp_item->tx_bytes, format_size_unit_none|format_size_prefix_si));
return gchar_free_to_qstring(format_size(endp_item->tx_bytes, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
case ENDP_COLUMN_PKT_BA:
return QString("%L1").arg(endp_item->rx_frames);
case ENDP_COLUMN_BYTES_BA:
return gchar_free_to_qstring(format_size(endp_item->rx_bytes, format_size_unit_none|format_size_prefix_si));
return gchar_free_to_qstring(format_size(endp_item->rx_bytes, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
default:
QVariant col_data = colData(column, resolve_names);
if (col_data.isValid()) return col_data;

View File

@ -1152,7 +1152,7 @@ bool FollowStreamDialog::follow(QString previous_filter, bool use_stream_index,
.arg(hostname1).arg(port1)
.arg(gchar_free_to_qstring(format_size(
follow_info_.bytes_written[0],
format_size_unit_bytes|format_size_prefix_si)));
FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_SI)));
client_to_server_string =
QString("%1:%2 %3 %4:%5 (%6)")
@ -1161,7 +1161,7 @@ bool FollowStreamDialog::follow(QString previous_filter, bool use_stream_index,
.arg(hostname0).arg(port0)
.arg(gchar_free_to_qstring(format_size(
follow_info_.bytes_written[1],
format_size_unit_bytes|format_size_prefix_si)));
FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_SI)));
wmem_free(NULL, port0);
wmem_free(NULL, port1);
@ -1169,7 +1169,7 @@ bool FollowStreamDialog::follow(QString previous_filter, bool use_stream_index,
both_directions_string = tr("Entire conversation (%1)")
.arg(gchar_free_to_qstring(format_size(
follow_info_.bytes_written[0] + follow_info_.bytes_written[1],
format_size_unit_bytes|format_size_prefix_si)));
FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_SI)));
setWindowSubtitle(tr("Follow %1 Stream (%2)").arg(proto_get_protocol_short_name(find_protocol_by_id(get_follow_proto_id(follower_))))
.arg(follow_filter));

View File

@ -1483,7 +1483,7 @@ QString PacketList::allPacketComments()
buf_str.append(QString(tr("Frame %1: %2\n\n")).arg(framenum).arg(comment_text));
if (buf_str.length() > max_comments_to_fetch_) {
buf_str.append(QString(tr("[ Comment text exceeds %1. Stopping. ]"))
.arg(format_size(max_comments_to_fetch_, format_size_unit_bytes|format_size_prefix_si)));
.arg(format_size(max_comments_to_fetch_, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_SI)));
return buf_str;
}
}

View File

@ -622,11 +622,11 @@ void TCPStreamDialog::fillGraph(bool reset_axes, bool set_focus)
stream_desc_ = tr("%1 %2 pkts, %3 %4 %5 pkts, %6 ")
.arg(UTF8_RIGHTWARDS_ARROW)
.arg(gchar_free_to_qstring(format_size(pkts_fwd, format_size_unit_none|format_size_prefix_si)))
.arg(gchar_free_to_qstring(format_size(bytes_fwd, format_size_unit_bytes|format_size_prefix_si)))
.arg(gchar_free_to_qstring(format_size(pkts_fwd, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI)))
.arg(gchar_free_to_qstring(format_size(bytes_fwd, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_SI)))
.arg(UTF8_LEFTWARDS_ARROW)
.arg(gchar_free_to_qstring(format_size(pkts_rev, format_size_unit_none|format_size_prefix_si)))
.arg(gchar_free_to_qstring(format_size(bytes_rev, format_size_unit_bytes|format_size_prefix_si)));
.arg(gchar_free_to_qstring(format_size(pkts_rev, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI)))
.arg(gchar_free_to_qstring(format_size(bytes_rev, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_SI)));
mouseMoved(NULL);
if (reset_axes)
resetAxes();

View File

@ -35,11 +35,6 @@
#include <QUuid>
#include <QScreen>
/* Make the format_size_flags_e enum usable in C++ */
format_size_flags_e operator|(format_size_flags_e lhs, format_size_flags_e rhs) {
return (format_size_flags_e) ((int)lhs| (int)rhs);
}
/*
* We might want to create our own "wsstring" class with convenience
* methods for handling g_malloc()ed strings, GStrings, and a shortcut
@ -148,13 +143,13 @@ const QString range_to_qstring(const epan_range *range)
const QString bits_s_to_qstring(const double bits_s)
{
return gchar_free_to_qstring(
format_size(bits_s, format_size_unit_none|format_size_prefix_si));
format_size(bits_s, FORMAT_SIZE_UNIT_NONE, FORMAT_SIZE_PREFIX_SI));
}
const QString file_size_to_qstring(const gint64 size)
{
return gchar_free_to_qstring(
format_size(size, format_size_unit_bytes|format_size_prefix_si));
format_size(size, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_SI));
}
const QString time_t_to_qstring(time_t ti_time)

View File

@ -773,7 +773,7 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) {
/* Size */
filesize = wtap_file_size(wth, &err);
// Windows Explorer uses IEC.
size_str = format_size(filesize, format_size_unit_bytes|format_size_prefix_iec);
size_str = format_size(filesize, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_IEC);
status = get_stats_for_preview(wth, &stats, &err, &err_info);

View File

@ -197,8 +197,9 @@ DIAG_ON(format)
/* Given a size, return its value in a human-readable format */
/* This doesn't handle fractional values. We might want to make size a double. */
gchar *
format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e flags)
char *
format_size_wmem(wmem_allocator_t *allocator, int64_t size,
format_size_units_e unit, uint16_t flags)
{
wmem_strbuf_t *human_str = wmem_strbuf_new(allocator, NULL);
int power = 1000;
@ -210,7 +211,7 @@ format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e f
if (thousands_grouping_fmt == NULL)
test_printf_thousands_grouping();
if ((flags & FORMAT_SIZE_PFX_MASK) == format_size_prefix_iec) {
if (flags & FORMAT_SIZE_PREFIX_IEC) {
pfx_off = 4;
power = 1024;
}
@ -232,25 +233,25 @@ format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e f
is_small = TRUE;
}
switch (flags & FORMAT_SIZE_UNIT_MASK) {
case format_size_unit_none:
switch (unit) {
case FORMAT_SIZE_UNIT_NONE:
break;
case format_size_unit_bytes:
case FORMAT_SIZE_UNIT_BYTES:
wmem_strbuf_append(human_str, is_small ? " bytes" : "B");
break;
case format_size_unit_bits:
case FORMAT_SIZE_UNIT_BITS:
wmem_strbuf_append(human_str, is_small ? " bits" : "b");
break;
case format_size_unit_bits_s:
case FORMAT_SIZE_UNIT_BITS_S:
wmem_strbuf_append(human_str, is_small ? " bits/s" : "bps");
break;
case format_size_unit_bytes_s:
case FORMAT_SIZE_UNIT_BYTES_S:
wmem_strbuf_append(human_str, is_small ? " bytes/s" : "Bps");
break;
case format_size_unit_packets:
case FORMAT_SIZE_UNIT_PACKETS:
wmem_strbuf_append(human_str, is_small ? " packets" : "packets");
break;
case format_size_unit_packets_s:
case FORMAT_SIZE_UNIT_PACKETS_S:
wmem_strbuf_append(human_str, is_small ? " packets/s" : "packets/s");
break;
default:

View File

@ -112,17 +112,17 @@ WS_DLL_PUBLIC
int ws_xton(char ch);
typedef enum {
format_size_unit_none = 0, /**< No unit will be appended. You must supply your own. */
format_size_unit_bytes = 1, /**< "bytes" for un-prefixed sizes, "B" otherwise. */
format_size_unit_bits = 2, /**< "bits" for un-prefixed sizes, "b" otherwise. */
format_size_unit_bits_s = 3, /**< "bits/s" for un-prefixed sizes, "bps" otherwise. */
format_size_unit_bytes_s = 4, /**< "bytes/s" for un-prefixed sizes, "Bps" otherwise. */
format_size_unit_packets = 5, /**< "packets" */
format_size_unit_packets_s = 6, /**< "packets/s" */
format_size_prefix_si = 0 << 8, /**< SI (power of 1000) prefixes will be used. */
format_size_prefix_iec = 1 << 8 /**< IEC (power of 1024) prefixes will be used. */
/* XXX format_size_prefix_default_for_this_particular_os ? */
} format_size_flags_e;
FORMAT_SIZE_UNIT_NONE, /**< No unit will be appended. You must supply your own. */
FORMAT_SIZE_UNIT_BYTES, /**< "bytes" for un-prefixed sizes, "B" otherwise. */
FORMAT_SIZE_UNIT_BITS, /**< "bits" for un-prefixed sizes, "b" otherwise. */
FORMAT_SIZE_UNIT_BITS_S, /**< "bits/s" for un-prefixed sizes, "bps" otherwise. */
FORMAT_SIZE_UNIT_BYTES_S, /**< "bytes/s" for un-prefixed sizes, "Bps" otherwise. */
FORMAT_SIZE_UNIT_PACKETS, /**< "packets" */
FORMAT_SIZE_UNIT_PACKETS_S, /**< "packets/s" */
} format_size_units_e;
#define FORMAT_SIZE_PREFIX_SI (1 << 0) /**< SI (power of 1000) prefixes will be used. */
#define FORMAT_SIZE_PREFIX_IEC (1 << 1) /**< IEC (power of 1024) prefixes will be used. */
/** Given a size, return its value in a human-readable format
*
@ -134,9 +134,11 @@ typedef enum {
* @return A newly-allocated string representing the value.
*/
WS_DLL_PUBLIC
gchar *format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e flags);
char *format_size_wmem(wmem_allocator_t *allocator, int64_t size,
format_size_units_e unit, uint16_t flags);
#define format_size(size, flags) format_size_wmem(NULL, size, flags)
#define format_size(size, unit, flags) \
format_size_wmem(NULL, size, unit, flags)
WS_DLL_PUBLIC
gchar printable_char_or_period(gchar c);
@ -150,9 +152,6 @@ gchar printable_char_or_period(gchar c);
#ifdef __cplusplus
}
/* Should we just have separate unit and prefix enums instead? */
extern format_size_flags_e operator|(format_size_flags_e lhs, format_size_flags_e rhs);
#endif /* __cplusplus */
#endif /* __STR_UTIL_H__ */

View File

@ -19,15 +19,15 @@ static void test_format_size(void)
{
char *str;
str = format_size(10000, format_size_unit_bytes);
str = format_size(10000, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_SI);
g_assert_cmpstr(str, ==, "10 kB");
g_free(str);
str = format_size(100000, format_size_unit_bytes|format_size_prefix_iec);
str = format_size(100000, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_IEC);
g_assert_cmpstr(str, ==, "97 KiB");
g_free(str);
str = format_size(20971520, format_size_unit_bits|format_size_prefix_iec);
str = format_size(20971520, FORMAT_SIZE_UNIT_BITS, FORMAT_SIZE_PREFIX_IEC);
g_assert_cmpstr(str, ==, "20 Mib");
g_free(str);
}