summaryrefslogtreecommitdiff
path: root/util.h
blob: 92b96215ea193da81821b54c22df3b94dc0d7f83 (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
/*
 * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
 *
 * This file is part of nios2sim-ng.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License. See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#ifndef _UTIL_H_
#define _UTIL_H_

#include <string.h>

/*
 * Memory allocation
 */

extern void *zalloc(const size_t size);

/*
 * String handling and manipulation
 */

/**
 * Check whether two strings match exactly.
 *
 * @param ctx  the reference string (usually a defined constant or string under
 *             the programs control)
 * @param arg  the string to match against the reference string (usually the
 *             user-supplied string)
 * @return     true if the strings match
 */
static inline bool xstrmatch(const char *ctx, const char *arg)
{
	size_t len = strlen(ctx);
	return (strlen(arg) == len && strncmp(arg, ctx, len) == 0);
}

/**
 * Check whether a string is empty (either NULL or empty string).
 *
 * @param str  string to check for emptiness
 * @return     true if the string is empty
 */
static inline bool xstrisempty(const char *str)
{
	return ((str == NULL) || (xstrmatch("", str)));
}


/*
 * Stringification macro (taken from Linux Kernel source code)
 */

/* Indirect stringification.  Doing two levels allows the parameter to be a
 * macro itself.  For example, compile with -DFOO=bar, __stringify(FOO)
 * converts to "bar".
 */

#define __stringify_1(x...)	#x
#define __stringify(x...)	__stringify_1(x)

#endif /* _UTIL_H_ */