TBOX是一個用c語言實現的多平臺開發庫

LaunaCamari 8年前發布 | 31K 次閱讀 數據庫 C/C++開發

來自: https://github.com/waruqi/tbox

The Treasure Box Library

TBOX is a mutli-platform c library for unix, windows, mac, ios, android, etc.

It is similar to glibc, but more simple and convenience. It includes asio, stream, network, container, algorithm, object, memory, database, string, charset, math, regex, libc, libm, utils and other library modules.

features

the stream library

  • supports file, data, http and socket source
  • supports the stream filter for gzip, charset and ..
  • implements transfer for two stream
  • implements transfer pool (asio) for multi-stream
  • implements the static buffer stream for parsing data

the asynchronous io library

  • supports reactor and proactor mode
  • using epoll, poll, select ,kqueue and iocp os system api

the database library

  • supports mysql and sqlite3 database and enumerates data using the iterator mode

the xml parser library

  • supports DOM and SAX mode and supports xpath.

the serialization and deserialization library.

  • supports xml, json, bplist, xplist, binary formats

the memory library

  • implements some memory pools for optimizating memory.
  • supports fast memory error detecting. it can detect the following types of bugs for the debug mode:
    • out-of-bounds accesses to heap and globals
    • use-after-free
    • double-free, invalid free
    • memory leaks

the container library

  • implements hash table, single list, double list, vector, stack, queue and min/max heap. supports iterator mode for algorithm.

the algorithm library

  • using the iterator mode
  • implements find, binary find and reverse find algorithm.
  • implements sort, bubble sort, quick sort, heap sort and insert sort algorithm.
  • implements count, walk items, reverse walk items, for_all and rfor_all.

the network library

  • implements dns(cached), ssl(openssl and polarssl), http and cookies
  • supports asynchronous io mode for dns, ssl and http using the asio and stream library

the platform library

  • implements timer, faster and lower precision timer
  • implements atomic and atomic64 operation
  • implements spinlock, mutex, event, semaphore, thread and thread pool
  • implements file, socket operation

the charset library

  • supports utf8, utf16, gbk, gb2312, uc2 and uc4
  • supports big endian and little endian mode

the zip library

  • supports gzip, zlibraw, zlib formats using the zlib library if exists
  • implements lzsw, lz77 and rlc algorithm

the utils library

  • implements base32, base64 encoder and decoder
  • implements crc32, adler32, md5 and sha1 hash algorithm
  • implements assert and trace output for the debug mode
  • implements bits operation for parsing u8, u16, u32, u64 data

the math library

  • implements random generator
  • implements fast fixed-point calculation, supports 6-bits, 16-bits, 30-bits fixed-point number

the libc library

  • implements lightweight libc library interfaces, the interface name contains tb_xxx prefix for avoiding conflict.
  • implements strixxx strrxxx wcsixxx wcsrxxx interface extension.
  • optimizates some frequently-used interface, .e.g. memset, memcpy, strcpy ...
  • implements memset_u16, memset_u32, memset_u64 extension interfaces.

the libm library

  • implements lightweight libm library interfaces, the interface name contains tb_xxx prefix for avoiding conflict.
  • supports float and double type.

the regex library

  • supports match and replace
  • supports global/multiline/caseless mode
  • uses pcre, pcre2 and posix regex modules

簡介

TBOX是一個用c語言實現的多平臺開發庫,支持windows、linux、mac、ios、android以及其他嵌入式系統。

針對各個平臺,封裝了統一的接口,簡化了各類開發過程中常用操作,使你在開發過程中,更加關注實際應用的開發,而不是把時間浪費在瑣碎的接口兼容性上面,并且充分利用了各個平臺獨有的一些特性進行優化。

流庫

針對http、file、socket、data等流數據,實現統一接口進行讀寫,并且支持: 阻塞、非阻塞、異步 三種讀寫模式。 支持中間增加多層filter流進行流過濾,實現邊讀取,內部邊進行解壓、編碼轉換、加密等操作,極大的減少了內存使用。

主要提供以下特性:

  1. stream:通用非阻塞流,用于一般的單獨io處理。
  2. async_stream:利用asio實現的純異步流,基于回調模式,可同時處理大量并發io。
  3. transfer:傳輸器,維護兩路流的傳輸,對async_stream的使用進行更上層的封裝,用其可以很方便的實現下載、上傳、復制等io傳輸操作。
  4. transfer_pool:傳輸池,基于asio,維護大量并發的傳輸,可以用于實現爬蟲、批量下載等等。
  5. static_stream:針對靜態數據buffer優化的靜態流,用于輕量快速的數據解析。

asio庫

  1. 支持reactor和proactor兩種模型,針對不同平臺,采用epoll/poll/select/kqueue/iocp接口,最大化異步操作的性能。
  2. 并且對http、ssl、dns也提供了純異步模式的實現。基于此庫完全可以很方便的寫出一個高性能的小型服務器。

數據庫

  1. 統一并簡化數據庫操作接口,適配各種數據源,通過統一的url來自動連接打開支持的數據庫,數據的枚舉采用迭代器模型。
  2. 目前支持sqlite3以及mysql兩種關系型數據庫,也可自定義擴展使用其他關系型數據庫。

xml庫

  1. 針對xml提供DOM和SAX兩種解析模式,SAX方式采用外部迭代模式,靈活性和性能更高,并且可以選擇指定路徑,進行解析。
  2. 解析過程完全基于stream,所以是高度流化的,可以實現邊下載、邊解壓、邊轉碼、邊解析一條龍服務,使用較低的內存也可以解析大規模數據。
  3. 提供xml writer以支持對xml生成

內存庫

  1. 參考linux內核內存管理機制的實現,并對其進行各種改造和優化,所實現的TBOX獨有的一整套內存池管理架構。
  2. 調試模式下,可以輕松檢測并定位內存泄露、內存越界溢出、內存重疊覆蓋等常見內存問題,并對整體內存的使用進行了統計和簡要分析。
  3. 針對大塊數據、小塊數據、字符串數據進行了充分的利用,避免了大量外部碎片和內部碎片的產生。分配操作進行了各種優化,96%的情況下,效率都是在O(1)。

容器庫

  1. 提供哈希、鏈表、數組、隊列、堆棧、最小最大堆等常用容器。
  2. 支持各種常用成員類型,在原有的容器期初上,其成員類型還可以完全自定義擴展。
  3. 所有容器都支持迭代器操作。
  4. 大部分容器都可以支持基于stream的序列化和反序列化操作。

算法庫

  1. 提供各種排序算法:冒泡排序、堆排序、快速排序、插入排序。
  2. 提供各種查找算法:線性遍歷、二分法搜索。
  3. 提供各種遍歷、刪除、統計算法。
  4. 以迭代器為接口,實現算法和容器的分離,類似stl,但是c實現的,更加輕量。

網絡庫

  1. 實現http、cookies、dns解析與緩存、ipv4、url的封裝。

數學運算庫

  1. 提供各種精度的定點運算支持
  2. 提供隨機數生成器

libc庫

  1. libc的一個輕量級實現,完全跨平臺,并且針對不同架構進行了優化。
  2. 支持大部分字符串、寬字符串操作。
  3. 擴展字符串、寬字符串的各種大小寫不敏感操作接口
  4. 擴展memset_u16、memset_u32等接口,并對其進行高度優化,尤其適合圖形渲染程序

libm庫

  1. libm部分接口的一個輕量級實現,以及對常用系統接口的封裝。(目前只實現了部分,之后有時間會完全實現掉)
  2. 擴展部分常用接口,增加對sqrt、log2等常用函數的整數版本計算,進行高度優化,不涉及浮點運算,適合嵌入式環境使用。

object庫

  1. 輕量級類apple的CoreFoundation庫,支持object、dictionary、array、string、number、date、data等常用對象,并且可以方便擴展自定義對象的序列化。
  2. 支持對xml、json、binary以及apple的plist(xplist/bplist)格式序列化和反序列化。 并且實現自有的binary序列化格式, 針對明文進行了簡單的加密,在不影響性能的前提下,序列化后的大小比bplist節省30%。

平臺庫

  1. 提供file、directory、socket、thread、time等常用系統接口
  2. 提供atomic、atomic64接口
  3. 提供高精度、低精度定時器
  4. 提供高性能的線程池操作
  5. 提供event、mutex、semaphore、spinlock等事件、互斥、信號量、自旋鎖操作
  6. 提供獲取函數堆棧信息的接口,方便調試和錯誤定位
  7. 提供跨平臺動態庫加載接口(如果系統支持的話)

壓縮庫

  1. 支持zlib/zlibraw/gzip的壓縮與解壓(需要第三方zlib庫支持)。

字符編碼庫

  1. 支持utf8、utf16、gbk、gb2312、uc2、uc4 之間的互相轉碼,并且支持大小端格式。

實用工具庫

  1. 實現base64/32編解碼
  2. 實現crc32、adler32、md5、sha1等常用hash算法
  3. 實現日志輸出、斷言等輔助調試工具
  4. 實現url編解碼
  5. 實現位操作相關接口,支持各種數據格式的解析,可以對8bits、16bits、32bits、64bits、float、double以及任意bits的字段進行解析操作,并且同時支持大端、小端和本地端模式,并針對部分操作進行了優化,像static_stream、stream都有相關接口對其進行了封裝,方便在流上進行快速數據解析。
  6. 實現swap16、swap32、swap64等位交換操作,并針對各個平臺進行了優化。
  7. 實現一些高級的位處理接口,例如:位0的快速統計、前導0和前導1的快速位計數、后導01的快速位計數
  8. 實現單例模塊,可以對靜態對象、實例對象進行快速的單例封裝,實現全局線程安全
  9. 實現option模塊,對命令行參數進行解析,提供快速方便的命令行選項建立和解析操作,對于寫終端程序還是很有幫助的

正則表達式庫

  1. 支持匹配和替換操作
  2. 支持全局、多行、大小寫不敏感等模式
  3. 使用pcre, pcre2和posix正則庫

build

please install xmake first:xmake

// build for the host platform
    cd ./tbox
    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</pre> 

example

#include "tbox/tbox.h"

int main(int argc, char** argv)
{
    /* init tbox
     *
     * @param priv      the platform private data
     *                  pass JavaVM* jvm for android jni
     *                  pass tb_null for other platform
     *
     * @param allocator the allocator, supports:
     *
     *                  - tb_native_allocator()
     *                      uses native memory directly
     *
     *                  - tb_static_allocator(data, size)
     *                      uses the a static small buffer and we can check memory error and leaking
     *
     *                  - tb_default_allocator(data, size)
     *                      uses the a large pool with the static memory and we can check memory error and leaking
     *
     *                  - tb_default_allocator(tb_null, 0)
     *                      uses the a large pool with the native memory and we can check memory error and leaking
     *
     *                  - tb_null
     *                      uses tb_default_allocator(tb_null, 0) for large mode
     *                      uses tb_native_allocator() for small mode, need define __tb_small__ 
     */
    if (!tb_init(tb_null, tb_null)) return 0;

    // print info with tag
    tb_trace_i("hello tbox");

    // print info only for debug
    tb_trace_d("hello tbox"); 

    // print error info
    tb_trace_e("hello tbox");

    // init vector
    tb_vector_ref_t vector = tb_vector_init(0, tb_element_cstr(tb_true));
    if (vector)
    {
        // insert item
        tb_vector_insert_tail(vector, "hello");
        tb_vector_insert_tail(vector, "tbox");

        // walk items
        tb_for_all_if (tb_char_t const*, cstr, vector, cstr)
        {
            // trace
            tb_trace_i("%s", cstr);
        }

        // find item
        tb_size_t itor = tb_find_all(vector, "tbox");
        if (itor != tb_iterator_tail(vector))
        {
            // trace
            tb_trace_i("%s", tb_iterator_item(vector, itor));
        }

        // sort items
        tb_sort_all(vector, tb_null);

        // exit vector
        tb_vector_exit(vector);
    }

    // init stream
    tb_stream_ref_t stream = tb_stream_init_from_url("http://www.xxxx.com/index.html");
    if (stream)
    {
        // save stream data to file
        tb_transfer_done_to_url(stream, "/home/file/index.html", 0, tb_null, tb_null);

        // exit stream
        tb_stream_exit(stream);
    }

    // block: save http to file
    tb_transfer_done_url("http://www.xxxx.com/index.html", "/home/file/index.html", 0, tb_null, tb_null);

    // async: save http to file 
    tb_transfer_pool_done(tb_transfer_pool(), "http://www.xxxx.com/index0.html", "/home/file/index0.html", 0, 0, tb_null, tb_null, tb_null);

    // wait some time
    getchar();

    // exit tbox
    tb_exit();
    return 0;
}</pre> 

contact

donate

alipay

paypal

</article>

 本文由用戶 LaunaCamari 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!