summaryrefslogtreecommitdiff
path: root/src/z-util.cc
blob: 6ec1373f6e39f732d0f197428aeb955007242a72 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "z-util.hpp"

#include <boost/algorithm/string/predicate.hpp>
#include <cassert>

using boost::algorithm::equals;

/**
 * Captialize letter
 */
void capitalize(char *s)
{
	char *p = s;
	assert(s != nullptr);

	for (; *p; p++)
	{
		if (!isspace(*p))
		{
			if (islower(*p))
			{
				*p = toupper(*p);
			}
			/* Done */
			break;
		}
	}
}


/*
 * Redefinable "plog" action
 */
void (*plog_aux)(const char *) = nullptr;

/*
 * Print (or log) a "warning" message (ala "perror()")
 * Note the use of the (optional) "plog_aux" hook.
 */
void plog(const char *str)
{
	/* Use the "alternative" function if possible */
	if (plog_aux) (*plog_aux)(str);

	/* Just do a labeled fprintf to stderr */
	else (void)(fprintf(stderr, "%s\n", str));
}



/*
 * Redefinable "quit" action
 */
void (*quit_aux)(const char *) = nullptr;

/*
 * Exit (ala "exit()").  If 'str' is NULL, do "exit(0)".
 * If 'str' begins with "+" or "-", do "exit(atoi(str))".
 * Otherwise, plog() 'str' and exit with an error code of -1.
 * But always use 'quit_aux', if set, before anything else.
 */
void quit(const char *str)
{
	/* Attempt to use the aux function */
	if (quit_aux) (*quit_aux)(str);

	/* Success */
	if (!str) (void)(exit(0));

	/* Extract a "special error code" */
	if ((str[0] == '-') || (str[0] == '+')) (void)(exit(atoi(str)));

	/* Send the string to plog() */
	plog(str);

	/* Failure */
	(void)(exit( -1));
}