Add jaylink_device_get_usb_bus_ports()
[libjaylink.git] / libjaylink / strutil.c
blob283ed173d97839c3ae0b0e02be6d62c215a78c45
1 /*
2 * This file is part of the libjaylink project.
4 * Copyright (C) 2016 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <errno.h>
24 #include "libjaylink.h"
26 /**
27 * @file
29 * String utility functions.
32 /**
33 * Convert a string representation of a serial number to an integer.
35 * The string representation of the serial number must be in decimal form.
37 * @param[in] str String representation to convert.
38 * @param[out] serial_number Serial number on success, and undefined on
39 * failure.
41 * @retval JAYLINK_OK Success.
42 * @retval JAYLINK_ERR_ARG Invalid arguments.
43 * @retval JAYLINK_ERR Conversion error. Serial number is invalid or string
44 * representation contains invalid character(s).
46 * @since 0.1.0
48 JAYLINK_API int jaylink_parse_serial_number(const char *str,
49 uint32_t *serial_number)
51 char *end_ptr;
52 unsigned long long tmp;
54 if (!str || !serial_number)
55 return JAYLINK_ERR_ARG;
57 errno = 0;
58 tmp = strtoull(str, &end_ptr, 10);
60 if (*end_ptr != '\0' || errno != 0 || tmp > UINT32_MAX)
61 return JAYLINK_ERR;
63 *serial_number = tmp;
65 return JAYLINK_OK;