From 856c92de0cfccee39750fda1d04d410fdb06bbe2 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 12 Aug 2014 15:39:39 +0200 Subject: macros for ato* with cast MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/macro-bits.h | 159 +++++++++++++++++++++++++++++++++++++++++ src/libmdsserver/macros.h | 1 + src/libmdsserver/mds-message.c | 2 +- src/libmdsserver/util.c | 5 +- 4 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 src/libmdsserver/macro-bits.h (limited to 'src/libmdsserver') diff --git a/src/libmdsserver/macro-bits.h b/src/libmdsserver/macro-bits.h new file mode 100644 index 0000000..f1feddd --- /dev/null +++ b/src/libmdsserver/macro-bits.h @@ -0,0 +1,159 @@ +/** + * mds — A micro-display server + * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + * + * This program 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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 . + */ +#ifndef MDS_LIBMDSSERVER_MACRO_BITS_H +#define MDS_LIBMDSSERVER_MACRO_BITS_H + + +#include +#include + + +#ifndef __WORDSIZE +# if defined(__x86_64__) && !defined(__ILP32__) +# define __WORDSIZE 64 +# else +# define __WORDSIZE 32 +# endif +#endif + + +/** + * Convert the beginning of a `const char*` to a `size_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atoz(str) ((size_t)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `ssize_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atosz(str) ((ssize_t)atol(str)) + +/** + * Convert the beginning of a `const char*` to a `short int` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atoh(str) ((short)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `unsigned short int` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atouh(str) ((unsigned short)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `unsigned int` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atou(str) ((unsigned int)atoi(str)) + +/** + * Convert the beginning of a `const char*` to an `unsigned long int` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atoul(str) ((unsigned long)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `unsigned long long int` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +#define atoull(str) ((unsigned long long)atoll(str)) + +#if __WORDSIZE == 64 +/** + * Convert the beginning of a `const char*` to an `int32_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define ato32(str) ((int32_t)atoi(str)) + +/** + * Convert the beginning of a `const char*` to an `uint32_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define atou32(str) ((uint32_t)atou(str)) + +/** + * Convert the beginning of a `const char*` to an `int64_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define ato64(str) ((int64_t)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `uint64_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define atou64(str) ((uint64_t)atoul(str)) +#else +/** + * Convert the beginning of a `const char*` to an `int32_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define ato32(str) ((int32_t)atol(str)) + +/** + * Convert the beginning of a `const char*` to an `uint32_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define atou32(str) ((uint32_t)atoul(str)) + +/** + * Convert the beginning of a `const char*` to an `int64_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define ato64(str) ((int64_t)atoll(str)) + +/** + * Convert the beginning of a `const char*` to an `uint64_t` + * + * @param str:const char* The string that begins with an integer + * @return The integer at the beginning of the string + */ +# define atou64(str) ((uint64_t)atoull(str)) +#endif + + +#endif + diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index b7eb1d8..2d3f7a3 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -20,6 +20,7 @@ #include "config.h" +#include "macro-bits.h" #include #include diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c index 5726b7a..8a3590c 100644 --- a/src/libmdsserver/mds-message.c +++ b/src/libmdsserver/mds-message.c @@ -163,7 +163,7 @@ static int get_payload_length(mds_message_t* restrict this) { /* Store the message length. */ header = this->headers[i] + strlen("Length: "); - this->payload_size = (size_t)atoll(header); + this->payload_size = atoz(header); /* Do not except a length that is not correctly formated. */ for (; *header; header++) diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c index 469b616..7047d9b 100644 --- a/src/libmdsserver/util.c +++ b/src/libmdsserver/util.c @@ -57,9 +57,8 @@ uint64_t parse_client_id(const char* str) strcpy(client_high = client_words, str); client_low = rawmemchr(client_words, ':'); *client_low++ = '\0'; - client = (uint64_t)atoll(client_high); - client <<= 32; - client |= (uint64_t)atoll(client_low); + client = atou64(client_high) << 32; + client |= atou64(client_low); return client; } -- cgit v1.2.3-70-g09d2