moreinttypes 1.8.0
Minimal library of enhanced integer types for C
Loading...
Searching...
No Matches
Description

A minimal library of enhanced integer types for C

Features

  • read and write to encapsulated signed/unsigned const integers
  • parse numeric strings with free functions or by calling through "member" functions
  • polymorphic function macros
  • use only what you need:
#include <moreinttypes/core.h> data types*, math and string library
#include <moreinttypes/utils.h> math and string library only
* Since version 1.5, the following types are provided:
  • compiles on Windows, macOS and Linux
  • compatible with code targeting ANSI C (a GNU compiler supporting -ansi is required)

    Note A compiler supporting the C99 standard is required for building.

  • (since version 1.7) compatible with CPM.cmake. See below for a sample configuration.

Examples

Using function macros (they wrap the "member functions")

#include <stdio.h>
#include <moreinttypes/core.h> //< Make sure this comes last!
int main(void) {
Int32 i = integer(122333333);
// access the underlying value . . .
printf("%d\n", i.value);
// "122333333"
// . . . but no direct assignment allowed!
// i.value = 0;
//
// error: assignment of read-only member ‘value’
// i.value = 0;
// ^
// let's set it to the value of a binary string
from_string(i, "10101010", 2);
printf("New value: %d\n", i.value);
// "New value: 170"
// what's that in base-2?
const char *bin_str = as_binary(i);
printf("%s\n", bin_str);
// "1010 1010"
from_string(i, "6", 10);
long double f = factorial(i);
printf("%d! = %.0Lf\n", i.value, f);
// "6! = 720"
return 0;
}
#define integer(x)
Returns an initialized Int32 struct.
Definition: Int32.h:37
#define from_string(i, str, b)
Tries to set the value of the given integer type by parsing a numeric string.
Definition: Integral.h:31
#define factorial(i)
Returns the factorial of the given integer type.
Definition: Integral.h:23
#define as_binary(i)
Returns the binary equivalent of the given integer type as a string.
Definition: Integral.h:37
Encapsulates a 32-bit integer.
Definition: Int32.h:14
const int32_t value
The underlying value of this Int32.
Definition: Int32.h:16

Using free functions

#include <stdio.h>
#include <moreinttypes/utils.h>
int main(void) {
int value = parse_int("10101010", 2);
printf("%d\n", value);
// "170"
char buf[16] = {0};
const char *bin_str = binary_string(buf, 170);
printf("%s\n", bin_str);
// "1010 1010"
long double f = factorial_of(6);
printf("6! = %.0Lf\n", f);
// "6! = 720"
return 0;
}
long double factorial_of(uint32_t n)
Returns the factorial of an unsigned integer.
Definition: numeric.c:50
const char * binary_string(char *buffer, uint32_t n)
Converts an integer to a string representation of its binary equivalent, formatted in space-separated...
Definition: numeric.c:303
int32_t parse_int(const char *str, int base)
Returns the integral value represented by the given numeric string.
Definition: numeric.c:78

CPM integration

Add the following to your project's CMakeLists.txt:

cmake_minimum_required (VERSION 3.14)
# Download the 'get_cpm.cmake' module from https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.40.2/get_cpm.cmake
include (get_cpm.cmake)
project (try-libmoreinttypes)
CPMAddPackage ("https://rdipardo.bitbucket.io/src/moreinttypes/libmoreinttypes-1.8.0.zip")
add_executable (main "main.c")
add_library (libmoreinttypes INTERFACE IMPORTED)
target_link_libraries (main PUBLIC moreinttypes)
target_include_directories (main
PUBLIC
"${libmoreinttypes_SOURCE_DIR}/include"
)
set_target_properties (main moreinttypes
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin"
)

Building

Windows developers can use the Visual C++ compiler, edition 2015 or later.

Compiling with the MinGW toolchain or inside the MSYS2 environment will work also.

Linux devs can use gcc (version 4.9 and up) or clang (version 3.8 and up).

Build options

Default
-DMOREINTTYPES_BUILD_TESTS=OFF|ON build and run unit tests ON
-DMOREINTTYPES_BUILD_EXAMPLES=OFF|ON build sample programs OFF
-DMOREINTTYPES_BUILD_STATIC=OFF|ON build a static library OFF
-DMOREINTTYPES_ENABLE_UBSAN=OFF|ON enable the Undefined Behavior Sanitizer (GCC,Clang only) OFF
-DMOREINTTYPES_RUN_DEMO=OFF|ON run a sample program immediately after the build (requires MOREINTTYPES_BUILD_EXAMPLES) OFF
-DMOREINTTYPES_CHECK_WITH_VALGRIND=OFF|ON run a memory leak check with valgrind, if installed OFF

Deprecated options (since 1.7.0)

Replaced by...
-DBUILD_STATIC=OFF|ON -DMOREINTTYPES_BUILD_STATIC
-DENABLE_UBSAN=OFF|ON -DMOREINTTYPES_ENABLE_UBSAN
-DRUN_DEMO=OFF|ON -DMOREINTTYPES_RUN_DEMO
-DWITH_VALGRIND=OFF|ON -DMOREINTTYPES_CHECK_WITH_VALGRIND

The build steps are the same for all environments, with one exception:

  • if you're using the Visual C++ compiler, start the Developer Command Prompt; all other users can simply log in to their usual shell
  • download the source code and validate the SHA256 checksum
  • unzip the archive and enter the root source directory, e.g., cd libmoreinttypes-1.8.0
  • make and enter the build directory: mkdir cmake-build && cd cmake-build
  • run: cmake .., or (to use a certain makefile generator) cmake .. -G"<name_of_generator>"

Note It's better to run cmake .. -G"NMake Makefiles" when using the Visual C++ compiler. This will prevent cmake from choosing a Visual Studio Generator by default.

  • run:
make

or, if using MinGW:

mingw32-make

or, if using nmake:

nmake

Installing

As the admin user, run:

make install

or

mingw32-make install

or

nmake install

API Documentation

To build the the documentation and serve it at localhost:8000, enter the doxygen directory and run:

./gendocs

or, on Windows,

.\gendocs

Note Make sure doxygen and python are installed and visible to your system's PATH.


This project took inspiration from the following sources:

  • the C Object System. Like this library, it strives to be compatible with ANSI/C89 while taking advantage of C99 features at compile time
  • Schreiner, A.-T. (1993). Object-oriented programming with ANSI-C. You can read the full text of this obscure classic for free online

License

Copyright (c) 2020 Robert Di Pardo and Contributors. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.