Skip to content

Commit 69466ec

Browse files
committed
common/util: add parse_double
1 parent 962e1e7 commit 69466ec

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

common/util.c

+11
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ float parse_float(const char *value) {
6464
return flt;
6565
}
6666

67+
double parse_double(const char *value) {
68+
errno = 0;
69+
char *end;
70+
double dbl = strtod(value, &end);
71+
if (*end || errno) {
72+
sway_log(SWAY_DEBUG, "Invalid double value '%s', defaulting to NAN", value);
73+
return NAN;
74+
}
75+
return dbl;
76+
}
77+
6778
enum movement_unit parse_movement_unit(const char *unit) {
6879
if (strcasecmp(unit, "px") == 0) {
6980
return MOVEMENT_UNIT_PX;

include/util.h

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ bool parse_boolean(const char *boolean, bool current);
5757
*/
5858
float parse_float(const char *value);
5959

60+
/**
61+
* Given a string that represents a floating point value, return a double.
62+
* Returns NAN on error.
63+
*/
64+
double parse_double(const char *value);
65+
6066
const char *sway_wl_output_subpixel_to_string(enum wl_output_subpixel subpixel);
6167

6268
bool sway_set_cloexec(int fd, bool cloexec);

0 commit comments

Comments
 (0)