https://github.com/tboox/tbox
🎁 A glib-like multi-platform c library
https://github.com/tboox/tbox
algorithm container coroutines cross-platform iterator json network plist stream tbox xmake xml
Last synced: 11 months ago
JSON representation
🎁 A glib-like multi-platform c library
- Host: GitHub
- URL: https://github.com/tboox/tbox
- Owner: tboox
- License: apache-2.0
- Created: 2011-04-10T15:01:20.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2025-04-24T02:37:26.000Z (11 months ago)
- Last Synced: 2025-05-10T15:02:42.294Z (11 months ago)
- Topics: algorithm, container, coroutines, cross-platform, iterator, json, network, plist, stream, tbox, xmake, xml
- Language: C
- Homepage: https://docs.tboox.org
- Size: 30.9 MB
- Stars: 4,995
- Watchers: 209
- Forks: 743
- Open Issues: 37
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- fucking-awesome-cpp - tbox - A glib-like multi-platform c library. [Apache2] [website](http://tboox.org/) (Frameworks)
- awesome-xmake - tbox - like multi-platform c library (Projects)
- awesome-cpp - tbox - A glib-like multi-platform c library. [Apache2] [website](http://tboox.org/) (Frameworks)
- awesome-cpp-with-stars - tbox - like multi-platform c library. [Apache2] website | 2026-02-23 | (Frameworks)
- awesome-cpp - tbox - A glib-like multi-platform c library. [Apache2] [website](http://tboox.org/) (Frameworks)
- StarryDivineSky - tboox/tbox - *数组* (`tbox_array`) 允许动态扩展,支持任意数据结构。 - *链表* 与 *双向链表*(`tbox_linkedlist`, `tbox_dlink`) 用于 FIFO/ LIFO 操作。 - *哈希表*、*平衡树*与 *红黑树* 为快速查找提供 O(1)/O(log n) 的性能。 - *栈/队列*(`tbox_stack`, `tbox_queue`) 用于多线程工作。 4. **内存管理**:TBOX 采用引用计数和可自定义的分配器,既能避免泄漏,又能让不同模块共享同一块内存。提供了 `tbox_malloc`, `tbox_free` 等封装,可在调试模式下检测错误。 5. **文件与 I/O**:抽象化后端口(TBOX_FILE)对 Windows 的 HANDLE、Unix 的 FILE* 进行统一,支持同步/异步读写以及批量操作。 - `tbox_file_open`, `tbox_file_read` 等 API 与标准 C 相似,但提供了路径自动创建与错误码封装。 6. **字符串工具**:除了常规 ASCII 支持,还含 UTF‑8 处理(转义、长度测算等),并可在 GLIB 样式的 “GString” 上做操作。 - `tbox_str_utf8_length`, `tbox_str_replace` 等函数。 7. **线程安全**:提供宏 `TBOX_LOCKED()` 与内部自旋锁,保证多线程读写时不出现数据竞争;在单核或无线程环境下也可禁用以提升性能。 **工作原理简述** - TBOX 的每一模块都封装成头文件 + 对应的源文件。 - 通过 `tbox_config.h` 开关特定功能,避免不必要的代码被编译,从而极大减小二进制体积。 - 它用标准 C99/11 编写,并且在所有平台上都可直接编译(无需额外依赖)。 **使用示例** ```c #include "tbox.h" int main(void){ // 动态数组存 int 值 TBOX_ARRAY *arr = tbox_array_new(sizeof(int)); for (int i=0;i<10;++i) tbox_array_append(arr,&i); // 取值并打印 for (size_t j=0;j<tbox_array_length(arr);++j){ int val; tbox_array_get(arr,j,&val); printf("%d ",val); } tbox_array_destroy(arr); } ``` **安装与构建** - 克隆仓库:`git clone https://github.com/tboox/tbox.git` - 进入目录 `cd tbox`; 执行 `make install`(默认目标为 Linux)或在 Windows 上使用 CMake: ```bash cmake -S . -B build && cmake --build build ``` - 库生成后可直接链接:`gcc main.c -ltbox` **贡献与社区** TBOX 的开发者鼓励任何想法、修复或功能请求。项目的 `issues` 与 `pull requests` 均在 GitHub 上公开维护,所有文档均以 MIT 许可证发布。 --- 以上文字约 **420 字**(300–500字范围内),保留了核心特性与工作原理,可帮助想了解 TBOX 的读者快速掌握其功能与使用方式。 (C/C++程序设计 / 资源传输下载)
README
## Supporting the project
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. 🙏 [[Become a sponsor](https://docs.tboox.org/#/about/sponsor)]
## Introduction ([中文](/README_zh.md))
TBOX is a glib-like cross-platform C library that is simple to use yet powerful in nature.
The project focuses on making C development easier and provides many modules (.e.g stream, coroutine, regex, container, algorithm ...),
so that any developer can quickly pick it up and enjoy the productivity boost when developing in C language.
It supports the following platforms: Windows, Macosx, Linux, Android, iOS, *BSD and etc.
And it provides many compiling options using [xmake](https://github.com/xmake-io/xmake):
* Release: Disable debug information, assertion, memory checking and enable optimization.
* Debug: Enable debug information, assertion, memory checking and disable optimization.
* Small: Disable all extensional modules and enable space optimization.
* Micro: compiling micro library (~64K) for the embed system.
If you want to know more, please refer to: [Documents](https://docs.tboox.org/#/getting_started), [Github](https://github.com/tboox/tbox) and [Gitee](https://gitee.com/tboox/tbox)
## Features
#### The stream library
- Supports file, data, http and socket source
- Supports the stream filter for gzip, charset and...
- Implements stream transfer
- Implements the static buffer stream for parsing data
- Supports coroutine and implements asynchronous operation
#### The coroutine library
- Provides high-performance coroutine switch
- Supports arm, arm64, x86, x86_64 ..
- Provides channel interfaces
- Provides semaphore and lock interfaces
- Supports io socket and stream operation in coroutine
- Provides some io servers (http ..) using coroutine
- Provides stackfull and stackless coroutines
- Support epoll, kqueue, poll, select and IOCP
- Support to wait pipe, socket and process in coroutine and poller at same time
#### 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 optimizing 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
- Uses 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)
- Implements ssl(openssl, polarssl, mbedtls)
- Implements http
- Implements cookies
- Supports ipv4, ipv6
- Supports coroutine
#### The platform library
- Implements timer, fast and low precision timer
- Implements atomic and atomic64 operation
- Implements spinlock, mutex, event, semaphore, thread and thread pool
- Implements file, socket operation
- Implements poller using epoll, poll, select, kqueue ...
- Implements switch context interfaces for coroutine
#### 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 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
- Optimizes 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
#### The hash library
- Implements crc32, adler32, md5 and sha1 hash algorithm
- Implements some string hash algorithms (.e.g bkdr, fnv32, fnv64, sdbm, djb2, rshash, aphash ...)
- Implements uuid generator
## Projects
Some projects using tbox:
* [gbox](https://github.com/tboox/gbox)
* [vm86](https://github.com/tboox/vm86)
* [xmake](http://www.xmake.io)
* [itrace](https://github.com/tboox/itrace)
* [more](https://github.com/tboox/tbox/wiki/tbox-projects)
## Build (xmake)
Please install xmake first: [xmake](https://github.com/xmake-io/xmake)
```console
# 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
```
## Build (xmake.sh)
```console
$ ./configure
$ make
```
## Example
```c
#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;
}
```
## Technical Support
You can also consider sponsoring us to get technical support services, [[Become a sponsor](https://docs.tboox.org/#/about/sponsor)]
## Contacts
* Email:[waruqi@gmail.com](mailto:waruqi@gmail.com)
* Homepage:[tboox.org](https://tboox.org)
* Community:[/r/tboox on reddit](https://www.reddit.com/r/tboox/)
* ChatRoom:[Chat on telegram](https://t.me/tbooxorg), [Chat on gitter](https://gitter.im/tboox/tboox?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
* QQ Group: 343118190(full), 662147501
* Wechat Public: tboox-os