~nabijaczleweli/voreutils

ref: cb91cf21c628ab3d5e42d89694d99fcca6c8efc8 voreutils/include/vore-print -rw-r--r-- 784 bytes
cb91cf21наб Add uname (and arch). Centralise spaced print a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// SPDX-License-Identifier: 0BSD


#pragma once

#include <cstdio>
#include <cstring>
#include <errno.h>


namespace vore {
	template <class I>
	int print_spaced(const char * self, I begin, I end, bool newline = true) {
		bool first = true;
		for(; begin != end; ++begin) {
			const char * str = *begin;

			if(!str)
				continue;

			if(first)
				first = false;
			else
				std::fputc(' ', stdout);

			std::fputs(str, stdout);
		}

		if(newline)
			std::fputc('\n', stdout);

		if(std::fflush(stdout)) {
			std::fprintf(stderr, "%s: %s\n", self, std::strerror(errno));
			return 1;
		} else
			return 0;
	}

	template <class C>
	int print_spaced(const char * self, const C & cont, bool newline = true) {
		return print_spaced(self, std::begin(cont), std::end(cont), newline);
	}
}