aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.c
blob: ede541e06759ed26c75000fc24816c50f2f0ae07 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * yawa - Yet Another Wallpaper Application
 * Copyright (C) 2014, The YAWA Developers
 *
 * This file is part of yawa.  yawa is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE
#include <bsd/string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

#include "yawa.h"
#include "utils.h"

signed
parse_int(char *string, const char *arg)
{
	errno = 0;
	char *endptr;
	long val = strtol(string, &endptr, 10);
	if (errno != 0) {
		char *errormsg;
		asprintf(&errormsg, "parse_int: failed to parse %s", arg);

		perror(errormsg);
		free(errormsg);
		exit(-2);
	}

	if (endptr == string) {
		fprintf(stderr, "Valid %s not found\n", arg);
		exit(-2);
	}
	return (signed)val;
}

unsigned
parse_uint(char *string, const char *arg)
{
	errno = 0;
	char *endptr;
	unsigned long val = strtoul(string, &endptr, 10);
	if (errno != 0) {
		char *errormsg;
		asprintf(&errormsg, "parse_uint: failed to parse %s", arg);

		perror(errormsg);
		free(errormsg);
		exit(-2);
	}

	if (endptr == string) {
		fprintf(stderr, "Valid %s not found\n", arg);
		exit(-2);
	}
	return (unsigned)val;
}

double
parse_double(char *string, const char *arg)
{
	errno = 0;
	char *endptr;
	double val = strtod(string, &endptr);
	if (errno != 0) {
		char *errormsg;
		asprintf(&errormsg, "parse_double: failed to parse %s", arg);

		perror(errormsg);
		free(errormsg);
		exit(-2);
	}

	if (endptr == string) {
		fprintf(stderr, "Valid %s not found\n", arg);
		exit(-2);
	}
	return val;
}

bool
parse_color(char *hex, Color *c, signed alpha)
{
	if ((strlen(hex) != 7) && (strlen(hex) != 9)) {
		return false;
	}

	signed len;
	if (strlen(hex) == 9) {
		len = 4;
	} else {
		len = 3;
	}

	hex++;
	signed colors[4];
	for (signed i = 0; i < len; hex += 2, i++) {
		char color[3];
		strlcpy(color, hex, 3);

		char *endptr;
		errno = 0;
		long val = strtol(color, &endptr, 16);
		if (errno != 0) {
			perror("strtol");
			exit(-2);
		}
		if (endptr == color) {
			fprintf(stderr, "No valid hex color found\n");
			exit(-2);
		}

		colors[i] = (signed)val;
	}

	c->r = colors[0];
	c->g = colors[1];
	c->b = colors[2];
	if (len == 4) {
		c->a = colors[3];
	} else {
		c->a = alpha;
	}

	return true;
}