codegen: Use - instead of stdout for output to stdout

In command-line tools, ordinary filenames normally do not have
special-cased meanings, so commit 3ef742eb "Don't skip dbus-codegen tests
on Win32" was a command-line API break: in the unlikely event that a
user wanted to write to a file named exactly `stdout`, this would have
been an incompatible change.

There is a conventional pseudo-filename to represent standard output,
which is `-` (for example `cat -` is a no-op filter). Adding support
for this is technically also a command-line API break (in the very
unlikely event that a user wants to write to a file named exactly `-`,
they would now have to write it as `./-`), but filenames starting with
a dash often require special treatment anyway, so this probably will not
come as a surprise to anyone.

When the output filename is `-` we don't want to use `#ifdef _____` as
a header guard, so special-case it as `__STDOUT__` as before.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie
2024-02-06 10:54:51 +00:00
parent 25e68476fa
commit 6a1fdb8145
3 changed files with 38 additions and 55 deletions

View File

@@ -57,6 +57,9 @@ def generate_namespace(namespace):
def generate_header_guard(header_name):
if header_name == "-":
return "STDOUT"
# There might be more characters that are safe to use than these, but lets
# stay conservative.
safe_valid_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

View File

@@ -66,7 +66,7 @@ def find_prop(iface, prop):
@contextmanager
def file_or_stdout(filename):
if filename is None or filename == "stdout":
if filename is None or filename == "-":
yield sys.stdout
else:
with open(filename, "w") as outfile: