aboutsummaryrefslogtreecommitdiffstats
path: root/libgamepad_get_absolute_axis_by_name.c
blob: 4d5aaa48dce631e397c16ca38ad793e2e1967b6c (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


int16_t
libgamepad_get_absolute_axis_by_name(const char *name)
{
	size_t code;
	int saved_errno;
	char *end;
	if (!name || !*name)
		return -1;
	if (isdigit(*name)) {
		saved_errno = errno;
		errno = 0;
		code = (size_t)strtoul(name, &end, 0);
		if (errno || *end || code > (size_t)ABS_MAX) {
			errno = saved_errno;
			return -1;
		}
		errno = saved_errno;
		return (int16_t)code;
	} else {
		for (code = 0; code < ELEMSOF(libgamepad_absolute_axis_names__); code++)
			if (!strcasecmp(name, libgamepad_absolute_axis_names__[code]))
				return (int16_t)code;
		return -1;
	}
}