From 4750e2c989d0f704cd4e958d023818b7be67a2c6 Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Wed, 22 Jan 2014 18:28:58 +0100 Subject: [PATCH] Add convert.h/convert.c --- qmake/compiler_config.pri | 2 +- qmake/fougdatax.pro | 5 ++++- src/convert.c | 21 +++++++++++++++++++++ src/convert.h | 9 +++++++++ src/endian.c | 30 ++++++------------------------ 5 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 src/convert.c create mode 100644 src/convert.h diff --git a/qmake/compiler_config.pri b/qmake/compiler_config.pri index 2a9a995..b5614ca 100644 --- a/qmake/compiler_config.pri +++ b/qmake/compiler_config.pri @@ -4,7 +4,7 @@ CONFIG += warn_on stl rtti exceptions QT -= core gui CONFIG(debug, debug|release) { - DEFINES *= _DEBUG_CONFIG_ + DEFINES += _DEBUG_CONFIG_ CONFIG += console } else { diff --git a/qmake/fougdatax.pro b/qmake/fougdatax.pro index 8cd88ba..6f5feff 100644 --- a/qmake/fougdatax.pro +++ b/qmake/fougdatax.pro @@ -28,12 +28,14 @@ HEADERS += \ ../src/stream.h \ ../src/task_control.h \ ../src/transfer.h \ + ../src/convert.h SOURCES += \ ../src/endian.c \ ../src/error.c \ ../src/stream.c \ - ../src/task_control.c + ../src/task_control.c \ + ../src/convert.c *-g++*:QMAKE_CFLAGS += -ansi -pedantic-errors *-msvc*:QMAKE_CFLAGS += -TC @@ -46,6 +48,7 @@ INSTALLS += global_inc contains(DATAX, stl) { dll:DEFINES += FOUG_DATAX_LIBSTL_DLL \ FOUG_DATAX_LIBSTL_MAKE_DLL + #DEFINES += FOUG_STLB_READWRITE_ALIGNSAFE HEADERS += \ ../src/libstl/stl_global.h \ diff --git a/src/convert.c b/src/convert.c new file mode 100644 index 0000000..12b6ae1 --- /dev/null +++ b/src/convert.c @@ -0,0 +1,21 @@ +#include "convert.h" + +typedef union +{ + uint32_t as_integer; + foug_real32_t as_float; +} _internal_foug_int_real_32_convert_t; + +foug_real32_t foug_convert_real32(uint32_t val) +{ + _internal_foug_int_real_32_convert_t conv; + conv.as_integer = val; + return conv.as_float; +} + +uint32_t foug_convert_uint32(foug_real32_t val) +{ + _internal_foug_int_real_32_convert_t conv; + conv.as_float = val; + return conv.as_integer; +} diff --git a/src/convert.h b/src/convert.h new file mode 100644 index 0000000..06511cc --- /dev/null +++ b/src/convert.h @@ -0,0 +1,9 @@ +#ifndef FOUG_CONVERT_H +#define FOUG_CONVERT_H + +#include "global.h" + +FOUG_LIB_EXPORT foug_real32_t foug_convert_real32(uint32_t val); +FOUG_LIB_EXPORT uint32_t foug_convert_uint32(foug_real32_t val); + +#endif /* FOUG_CONVERT_H */ diff --git a/src/endian.c b/src/endian.c index dc31188..62b7245 100644 --- a/src/endian.c +++ b/src/endian.c @@ -1,5 +1,7 @@ #include "endian.h" +#include "convert.h" + #include typedef union @@ -8,26 +10,6 @@ typedef union uint8_t bytes[4]; } _internal_foug_int_bytes_32_convert_t; -typedef union -{ - uint32_t as_integer; - foug_real32_t as_float; -} _internal_foug_int_real_32_convert_t; - -static foug_real32_t convert_real32(uint32_t val) -{ - _internal_foug_int_real_32_convert_t conv; - conv.as_integer = val; - return conv.as_float; -} - -static uint32_t convert_uint32(foug_real32_t val) -{ - _internal_foug_int_real_32_convert_t conv; - conv.as_float = val; - return conv.as_integer; -} - /*! Endianness (byte order) of the host's CPU architecture */ foug_endianness_t foug_host_endianness() { @@ -133,23 +115,23 @@ void foug_encode_uint32_be(uint32_t val, uint8_t* bytes) /*! Read a 32bit real from memory-location \p bytes (little-endian byte order) */ foug_real32_t foug_decode_real32_le(const uint8_t* bytes) { - return convert_real32(foug_decode_uint32_le(bytes)); + return foug_convert_real32(foug_decode_uint32_le(bytes)); } /*! Read a 32bit real from memory-location \p bytes (mixed-endian byte order) */ foug_real32_t foug_decode_real32_me(const uint8_t* bytes) { - return convert_real32(foug_decode_uint32_me(bytes)); + return foug_convert_real32(foug_decode_uint32_me(bytes)); } /*! Read a 32bit real from memory-location \p bytes (big-endian byte order) */ foug_real32_t foug_decode_real32_be(const uint8_t* bytes) { - return convert_real32(foug_decode_uint32_be(bytes)); + return foug_convert_real32(foug_decode_uint32_be(bytes)); } /*! Write 32bit real \p val to the memory location at \p bytes in little-endian byte order */ void foug_encode_real32_le(foug_real32_t val, uint8_t* bytes) { - foug_encode_uint32_le(convert_uint32(val), bytes); + foug_encode_uint32_le(foug_convert_uint32(val), bytes); }