TBOX Cross-Platform C Library

repository·dev·Indexed 26 days ago

https://github.com/tboox/tbox

A glib-like cross-platform C library providing unified interfaces for streams, coroutines, containers, networking, and database support. It supports Windows, macOS, Linux, Android, iOS, and *BSD. Key features include a custom memory pool architecture, HTTP/DNS/SSL networking, XML/JSON parsing, and a high-performance coroutine library. TBOX can be built using xmake or a shell script configuration, with available build modes including Release, Debug, Small, and Micro for embedded systems.

Tokens
1.9K
Snippets
6
Records
14
Agent score
88%

What's inside TBOX

  1. Overview of TBOX cross-platform library

    dev

    TBOX is a cross-platform development library implemented in C. It provides unified interfaces for various platform-specific operations, allowing developers to focus on application logic rather than interface compatibility. It supports multiple platforms including Windows, macOS, Linux, Android, iOS, and *BSD.

    Key features include:

    • Stream Library: Unified interface for HTTP, file, socket, and data streams with support for blocking, non-blocking, and asynchronous modes.
    • Coroutine Library: High-performance coroutine switching (supporting x86, x86_64, arm, arm64, mips32) with channel-based communication and native support for sockets and streams.
    • Database Support: Unified interface for SQLite3 and MySQL via URL-based connections.
    • XML/JSON/Object Support: DOM/SAX XML parsing and an object library (similar to Apple's CoreFoundation) for handling various serialization formats.
    • Memory Management: Custom memory pool architecture with leak and overflow detection in debug mode.
    • Networking: HTTP client, DNS caching, and SSL support (OpenSSL, PolarSSL, mbedtls).
    • Utility Modules: Containers (hash, list, array, etc.), algorithms, math, regex, and bit manipulation.
  2. Compile TBOX using Xmake

    dev

    TBOX supports various compilation modes via Xmake:

    • Release: Optimized for production (no debug info/assertions).
    • Debug: Enables detailed debugging, assertions, memory leak, and lock contention analysis.
    • Small: Minimalist build, disables extension modules.
    • Micro: For embedded platforms, compiles only the micro-kernel (~64K, includes lightweight libc).

    Ensure xmake is installed before proceeding.

    # Default build for the host platform
    $ cd ./tbox
    $ xmake
    
    # Build for mingw platform
    $ cd ./tbox
    $ xmake f -p mingw --sdk=/home/mingwsdk
    $ xmake
    
    # Build for iphoneos platform
    $ cd ./tbox
    $ xmake f -p iphoneos
    $ xmake
    
    # Build for android platform
    $ cd ./tbox
    $ xmake f -p android --ndk=xxxxx
    $ xmake
    
    # Cross-compile for linux
    $ cd ./tbox
    $ xmake f -p linux --sdk=/home/sdk
    $ xmake
  3. Build TBOX using xmake

    dev

    TBOX uses xmake as its primary build system. You can build for the host platform or various cross-compilation targets by configuring the platform (-p) and providing the necessary SDK or NDK paths.

    # build for the host platform
    $ cd ./tbox
    $ xmake
    
    # build for the mingw platform
    $ cd ./tbox
    $ xmake f -p mingw --sdk=/home/mingwsdk
    $ xmake
    
    # build for the iphoneos platform
    $ cd ./tbox
    $ xmake f -p iphoneos
    $ xmake
    
    # build for the android platform
    $ cd ./tbox
    $ xmake f -p android --ndk=xxxxx
    $ xmake
    
    # build for the linux cross-platform
    $ cd ./tbox
    $ xmake f -p linux --sdk=/home/sdk # --bin=/home/sdk/bin
    $ xmake
  4. Configure TBOX build modes

    dev

    When using xmake, you can select different compilation profiles to suit your needs:

    • Release: Disables debug information, assertion, and memory checking; enables optimization.
    • Debug: Enables debug information, assertion, and memory checking; disables optimization.
    • Small: Disables all extensional modules and enables space optimization.
    • Micro: Compiles a micro library (~64K) for embedded systems.
  5. Basic usage example with tb_vector

    dev

    The following example demonstrates how to initialize the tbox library, create a dynamic vector of strings (tb_vector_ref_t), insert elements, iterate through them using the tb_for_all macro, and perform proper cleanup using tb_exit().

    #include "tbox/tbox.h"
    
    int main(int argc, char** argv) {
        if (!tb_init(tb_null, tb_null)) return 0;
    
        tb_vector_ref_t vector = tb_vector_init(0, tb_element_str(tb_true));
        if (vector) {
            tb_vector_insert_tail(vector, "hello");
            tb_vector_insert_tail(vector, "tbox");
    
            tb_for_all (tb_char_t const*, cstr, vector) {
                tb_trace_i("%s", cstr);
            }
            tb_vector_exit(vector);
        }
        tb_exit();
        return 0;
    }
  6. Basic usage example with TBOX

    dev

    To use TBOX, include tbox/tbox.h, initialize the library with tb_init(), and ensure you call tb_exit() before the program terminates. The following example demonstrates initializing a vector, inserting strings, and iterating through them.

    #include "tbox/tbox.h"
    
    int main(int argc, char** argv) {
        if (!tb_init(tb_null, tb_null)) return 0;
    
        tb_vector_ref_t vector = tb_vector_init(0, tb_element_str(tb_true));
        if (vector) {
            tb_vector_insert_tail(vector, "hello");
            tb_vector_insert_tail(vector, "tbox");
    
            tb_for_all (tb_char_t const*, cstr, vector) {
                tb_trace_i("%s", cstr);
            }
            tb_vector_exit(vector);
        }
        tb_exit();
        return 0;
    }