Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Public domain backport of the core C++11 standard library to C.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 doc
Octocat-spinner-32 etc
Octocat-spinner-32 ffi
Octocat-spinner-32 src
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 AUTHORS
Octocat-spinner-32 CREDITS
Octocat-spinner-32 Makefile.am
Octocat-spinner-32 README
Octocat-spinner-32 README.rst
Octocat-spinner-32 TODO
Octocat-spinner-32 UNLICENSE
Octocat-spinner-32 VERSION
Octocat-spinner-32 autogen.sh
Octocat-spinner-32 configure.ac
Octocat-spinner-32 configure.sh
README.rst

libcpr: C++11 backport to C

Travis CI build status

libcpr is a public domain backport of the core data structures and algorithms from the C++11 standard library to C.

Features

  • Implements a straightforward mapping from C++ to C, with consistent naming.
  • No runtime dependencies other than the system's C++ standard library.
  • No build prerequisites beyond the Autotools toolchain and a C++11 compiler.
  • Compatible with Clang and GCC, or any standard C++11 implementation.
  • Plays nice with others: all exported symbols are prefixed with cpr_.
  • 100% free and unencumbered public domain software, usable in any context and for any purpose.

Motivation

The C standard library is severely deficient in useful data structures essential to modern programming practice. Consequently, most non-trivial C programs and shared libraries include one-off implementations of even the most elementary data structures such as dynamic arrays and hash tables. This harms programmer productivity, bloats the sizes of binaries, wastes machine resources, and inevitably introduces unnecessary bugs--including security vulnerabilities.

The alternative to rolling your own data structures has thus far been to pull in a large, all-purpose library dependency such as GLib. The problem with this approach is that you just wanted a hash table, but you got the kitchen sink with an XML parser thrown in for good measure. These all-purpose libraries have a huge footprint and can be difficult to install, ensuring that your potential user base will prefer competing projects who do roll their own data structures.

libcpr is a new take on an old problem. Given that every modern desktop and server system today is practically guaranteed to already have not merely the C standard library but also the C++ standard library, libcpr provides the until-now missing glue to access that library from pure C. The C++ standard library contains the basic data structures and algorithms needed for most common programming tasks, and adds no bloat since it's already installed on the system regardless.

Error Handling

This library makes a careful distinction between three different classes of error conditions:

  • Logic errors, triggered using cpr_logic_error(). Errors of this class are thrown due to programming errors where the function interfaces are used in violation of documented preconditions. A common strategy for handling this class of error conditions is to abort the program with a core dump, facilitating introspection to locate and remedy the bug.
  • Fatal errors, triggered using cpr_fatal_error(). Errors of this class are thrown due to the exhaustion of critical system resources, in particular available memory (ENOMEM), or due to attempts to exceed applicable system resource limits. A typical strategy for handling this class of error conditions is to terminate the program with a descriptive error message. More robust programs and shared libraries may wish to implement another strategy, such as retrying the operation after first letting most of the call stack unwind in order to free up scarce resources.
  • Runtime errors, triggered using cpr_runtime_error(). Errors of this class are thrown as a matter of course to indicate various exceptional conditions. These conditions are generally recoverable and robust programs will take care to correctly handle them.

Note

The distinction between logic errors and runtime errors mirrors that found in the C++11 standard library, where the <stdexcept> header defines the standard exception base classes std::logic_error and std::runtime_error. The standard exception class std::bad_alloc, on the other hand, is a representative example of a fatal error.

Build Prerequisites

Installation

Installation on Unix

$ ./autogen.sh
$ ./configure                        # on Linux
$ ./configure --with-stdlib=libc++   # on FreeBSD / Mac OS X
$ make
$ sudo make install
$ sudo ldconfig                      # on Linux

Elsewhere

Find this project at: GitHub, Bitbucket, Ohloh, Freecode, SourceForge, Travis CI, Twitter, Tumblr, and COD5.

Author

This project is part of the DRY initiative.

Donations

If you found this software useful and would like to encourage its maintenance and further development, please consider making a donation to the Bitcoin wallet address 1FxcaWrxZ1sVCdbw6ZC8eM6BhwPVnKy5fZ.

License

This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.

Something went wrong with that request. Please try again.