2 # Wireshark - Network traffic analyzer
3 # By Gerald Combs <gerald@wireshark.org>
4 # Copyright 1998 Gerald Combs
6 # SPDX-License-Identifier: GPL-2.0-or-later
8 convert-glib-types.py - Convert glib types to their C and C99 equivalents.
24 'guchar': 'unsigned char',
26 'gushort': 'uint16_t',
28 'guint': 'unsigned', # Matches README.developer
29 # Our remaining glong instances probably shouldn't be converted, e.g.
30 # sequence_analysis.c:350
32 'gulong': 'unsigned long',
38 'guint16': 'uint16_t',
39 'guint32': 'uint32_t',
40 'guint64': 'uint64_t',
43 'gpointer ': 'void *', # 'void *foo' instead of 'void * foo'
45 'gconstpointer ': 'const void *', # 'void *foo' instead of 'void * foo'
46 'gconstpointer': 'const void *',
47 'gintptr': 'intptr_t',
48 'guintptr': 'uintptr_t',
49 # Is gsize the same as size_t on the platforms we support?
50 # https://gitlab.gnome.org/GNOME/glib/-/issues/2493
56 'G_MAXINT8': 'INT8_MAX',
57 'G_MAXINT16': 'INT16_MAX',
58 'G_MAXINT32': 'INT32_MAX',
59 'G_MAXINT64': 'INT64_MAX',
60 'G_MAXINT': 'INT_MAX',
61 'G_MAXUINT8': 'UINT8_MAX',
62 'G_MAXUINT16': 'UINT16_MAX',
63 'G_MAXUINT32': 'UINT32_MAX',
64 'G_MAXUINT64': 'UINT64_MAX',
65 'G_MAXUINT': 'UINT_MAX',
66 'G_MININT8': 'INT8_MIN',
67 'G_MININT16': 'INT16_MIN',
68 'G_MININT32': 'INT32_MIN',
69 'G_MININT64': 'INT64_MIN',
70 'G_MININT': 'INT_MIN',
71 'G_MINFLOAT': 'FLT_MIN',
72 'G_MAXFLOAT': 'FLT_MAX',
73 'G_MINDOUBLE': 'DBL_MIN',
74 'G_MAXDOUBLE': 'DBL_MAX',
75 'G_GINT64_CONSTANT': 'INT64_C',
76 'G_GUINT64_CONSTANT': 'UINT64_C',
85 'G_GINT64_FORMAT': 'PRId64',
86 'G_GUINT64_FORMAT': 'PRIu64',
90 'tvb_get_guint8': 'tvb_get_uint8',
91 'tvb_get_gint8': 'tvb_get_int8',
92 'tvb_get_guint16': 'tvb_get_uint16',
93 'tvb_get_gint16': 'tvb_get_int16',
94 'tvb_get_guint24': 'tvb_get_uint24',
95 'tvb_get_gint24': 'tvb_get_int24',
96 'tvb_get_guint32': 'tvb_get_uint32',
97 'tvb_get_gint32': 'tvb_get_int32',
98 'tvb_get_guint40': 'tvb_get_uint40',
99 'tvb_get_gint40': 'tvb_get_int40',
100 'tvb_get_guint48': 'tvb_get_uint48',
101 'tvb_get_gint48': 'tvb_get_int48',
102 'tvb_get_guint56': 'tvb_get_uint56',
103 'tvb_get_gint56': 'tvb_get_int56',
104 'tvb_get_guint64': 'tvb_get_uint64',
105 'tvb_get_gint64': 'tvb_get_int64',
106 'tvb_find_guint8': 'tvb_find_uint8',
107 'tvb_find_guint16': 'tvb_find_uint16',
108 'tvb_ws_mempbrk_pattern_guint8': 'tvb_ws_mempbrk_pattern_uint8',
109 'guint32_to_str_buf': 'uint32_to_str_buf',
110 'guint64_to_str_buf': 'uint64_to_str_buf',
111 'get_nonzero_guint32': 'get_nonzero_uint32',
112 'get_guint32': 'get_uint32',
113 'guint8_to_hex': 'uint8_to_hex',
116 def convert_file(file):
119 with
open(file, 'r') as f
:
121 for glib_type
, c99_type
in padded_type_map
.items():
122 lines
= lines
.replace(glib_type
, c99_type
)
123 for glib_type
, c99_type
in type_map
.items():
124 lines
= re
.sub(rf
'([^"])\b{glib_type}\b([^"])', rf
'\1{c99_type}\2', lines
, flags
=re
.MULTILINE
)
125 for glib_define
, c99_define
in definition_map
.items():
126 lines
= re
.sub(rf
'\b{glib_define}\b', rf
'{c99_define}', lines
, flags
=re
.MULTILINE
)
127 for glib_tf_define
, c99_define
in tf_definition_map
.items():
128 lines
= re
.sub(rf
'\b{glib_tf_define}\b([^\'"])', rf
'{c99_define}\1', lines
, flags
=re
.MULTILINE
)
129 for glib_fmt_spec
, c99_fmt_spec
in format_spec_map
.items():
130 lines
= re
.sub(rf
'\b{glib_fmt_spec}\b', rf
'{c99_fmt_spec}', lines
, flags
=re
.MULTILINE
)
131 for glib_api
, c99_api
in api_map
.items():
132 lines
= re
.sub(rf
'\b{glib_api}\b', rf
'{c99_api}', lines
, flags
=re
.MULTILINE
)
133 except IsADirectoryError
:
134 sys
.stderr
.write(f
'{file} is a directory.\n')
136 except UnicodeDecodeError:
137 sys
.stderr
.write(f
"{file} isn't valid UTF-8.\n")
140 sys
.stderr
.write(f
'Unable to open {file}.\n')
143 with
open(file, 'w') as f
:
145 print(f
'Converted {file}')
148 parser
= argparse
.ArgumentParser(description
='Convert glib types to their C and C99 equivalents.')
149 parser
.add_argument('files', metavar
='FILE', nargs
='*')
150 args
= parser
.parse_args()
152 # Build a padded version of type_map which attempts to preserve alignment
153 for glib_type
, c99_type
in type_map
.items():
154 pg_type
= glib_type
+ ' '
155 pc_type
= c99_type
+ ' '
156 pad_len
= max(len(pg_type
), len(pc_type
))
157 padded_type_map
[f
'{pg_type:{pad_len}s}'] = f
'{pc_type:{pad_len}s}'
160 if platform
.system() == 'Windows':
161 for arg
in args
.files
:
162 files
+= glob
.glob(arg
)
171 if __name__
== "__main__":