238 Star 922 Fork 314

GVPtboox / tbox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

一个用c语言实现的跨平台开发库

项目支持

通过成为赞助者来支持该项目。您的logo将显示在此处,并带有指向您网站的链接。🙏 [成为赞助商]

简介

TBOX是一个用c语言实现的跨平台开发库。

针对各个平台,封装了统一的接口,简化了各类开发过程中常用操作,使你在开发过程中,更加关注实际应用的开发,而不是把时间浪费在琐碎的接口兼容性上面,并且充分利用了各个平台独有的一些特性进行优化。

这个项目的目的,是为了使C开发更加的简单高效。

目前支持的平台有: Windows, Macosx, Linux, Android, iOS, *BSD等等。

通过xmake支持各种编译模式:

  • Release: 正式版编译,禁用调试信息、断言,各种检测机制,启用编译器优化
  • Debug: 调试模式,默认启用详细调试信息、断言、内存越界检测、内存泄漏、锁竞争分析等检测机制
  • Small: 最小化编译,默认禁用所有扩展模块,启用编译器最小化优化
  • Micro: 针对嵌入式平台,仅仅编译tbox微内核,仅提供最基础的跨平台接口,生成库仅64K左右(内置轻量libc接口实现)

如果你想了解更多,请参考:在线文档, Github以及Gitee

特性

流库

针对http、file、socket、data等流数据,实现统一接口进行读写,并且支持: 阻塞、非阻塞、异步 三种读写模式。 支持中间增加多层filter流进行流过滤,实现边读取,内部边进行解压、编码转换、加密等操作,极大的减少了内存使用。

主要提供以下模块:

  • stream:通用非阻塞流,用于一般的单独io处理,同时支持协程以实现异步传输。
  • transfer:流传输器,维护两路流的传输。
  • static_stream:针对静态数据buffer优化的静态流,用于轻量快速的数据解析。

协程库

  • 快速高效的协程切换支持
  • 提供跨平台支持,核心切换算法参考boost,并且对其进行重写和优化,目前支持架构:x86, x86_64, arm, arm64, mips32
  • 提供channel协程间数据通信支持,基于生产、消费者模型
  • 提供信号量、协程锁支持
  • socket、stream都模块原生支持协程,并且可在线程和协程间进行无缝切换
  • 提供http、file等基于协程的简单服务器实例,只需几百行代码,就可以从socket开始写个高性能io服务器,代码逻辑比异步回调模式更加清晰
  • 同时提供stackfull, stackless两种协程模式支持,stackless协程更加的轻量(每个协程只占用几十个bytes),切换更快(会牺牲部分易用性)
  • 支持epoll, kqueue, poll, select 和 IOCP
  • 在协程和poller中支持同时等待和调度socket,pipe io和process

数据库

  • 统一并简化数据库操作接口,适配各种数据源,通过统一的url来自动连接打开支持的数据库,数据的枚举采用迭代器模型。
  • 目前支持sqlite3以及mysql两种关系型数据库,也可自定义扩展使用其他关系型数据库。

xml库

  • 针对xml提供DOM和SAX两种解析模式,SAX方式采用外部迭代模式,灵活性和性能更高,并且可以选择指定路径,进行解析。
  • 解析过程完全基于stream,所以是高度流化的,可以实现边下载、边解压、边转码、边解析一条龙服务,使用较低的内存也可以解析大规模数据。
  • 提供xml writer以支持对xml生成

内存库

  • 参考linux内核内存管理机制的实现,并对其进行各种改造和优化,所实现的TBOX独有的一整套内存池管理架构。
  • 调试模式下,可以轻松检测并定位内存泄露、内存越界溢出、内存重叠覆盖等常见内存问题,并对整体内存的使用进行了统计和简要分析。
  • 针对大块数据、小块数据、字符串数据进行了充分的利用,避免了大量外部碎片和内部碎片的产生。分配操作进行了各种优化,96%的情况下,效率都是在O(1)。

容器库

  • 提供哈希、链表、数组、队列、堆栈、最小最大堆等常用容器。
  • 支持各种常用成员类型,在原有的容器期初上,其成员类型还可以完全自定义扩展。
  • 所有容器都支持迭代器操作。
  • 大部分容器都可以支持基于stream的序列化和反序列化操作。

算法库

  • 提供各种排序算法:冒泡排序、堆排序、快速排序、插入排序。
  • 提供各种查找算法:线性遍历、二分法搜索。
  • 提供各种遍历、删除、统计算法。
  • 以迭代器为接口,实现算法和容器的分离,类似stl,但是c实现的,更加轻量。

网络库

  • 实现http客户端模块
  • 实现cookies
  • 实现dns解析与缓存
  • 实现ssl(支持openssl, polarssl, mbedtls)
  • 支持ipv4、ipv6
  • 支持通过协程实现异步模式

数学运算库

  • 提供各种精度的定点运算支持
  • 提供随机数生成器

libc库

  • libc的一个轻量级实现,完全跨平台,并且针对不同架构进行了优化。
  • 支持大部分字符串、宽字符串操作。
  • 扩展字符串、宽字符串的各种大小写不敏感操作接口
  • 扩展memset_u16memset_u32等接口,并对其进行高度优化,尤其适合图形渲染程序

libm库

  • libm部分接口的一个轻量级实现,以及对常用系统接口的封装。(目前只实现了部分,之后有时间会完全实现掉)
  • 扩展部分常用接口,增加对sqrt、log2等常用函数的整数版本计算,进行高度优化,不涉及浮点运算,适合嵌入式环境使用。

object库

  • 轻量级类apple的CoreFoundation库,支持object、dictionary、array、string、number、date、data等常用对象,并且可以方便扩展自定义对象的序列化。
  • 支持对xml、json、binary以及apple的plist(xplist/bplist)格式序列化和反序列化。 并且实现自有的binary序列化格式, 针对明文进行了简单的加密,在不影响性能的前提下,序列化后的大小比bplist节省30%。

平台库

  • 提供file、directory、socket、thread、time等常用系统接口
  • 提供atomic、atomic64接口
  • 提供高精度、低精度定时器
  • 提供高性能的线程池操作
  • 提供event、mutex、semaphore、spinlock等事件、互斥、信号量、自旋锁操作
  • 提供获取函数堆栈信息的接口,方便调试和错误定位
  • 提供跨平台动态库加载接口(如果系统支持的话)
  • 提供io轮询器,针对epoll, poll, select, kqueue进行跨平台封装
  • 提供跨平台上下文切换接口,主要用于协程实现,切换效率非常高

压缩库

  • 支持zlib/zlibraw/gzip的压缩与解压(需要第三方zlib库支持)。

字符编码库

  • 支持utf8、utf16、gbk、gb2312、uc2、uc4 之间的互相转码,并且支持大小端格式。

实用工具库

  • 实现base64/32编解码
  • 实现crc32、adler32、md5、sha1等常用hash算法
  • 实现日志输出、断言等辅助调试工具
  • 实现url编解码
  • 实现位操作相关接口,支持各种数据格式的解析,可以对8bits、16bits、32bits、64bits、float、double以及任意bits的字段进行解析操作,并且同时支持大端、小端和本地端模式,并针对部分操作进行了优化,像static_stream、stream都有相关接口对其进行了封装,方便在流上进行快速数据解析。
  • 实现swap16、swap32、swap64等位交换操作,并针对各个平台进行了优化。
  • 实现一些高级的位处理接口,例如:位0的快速统计、前导0和前导1的快速位计数、后导01的快速位计数
  • 实现单例模块,可以对静态对象、实例对象进行快速的单例封装,实现全局线程安全
  • 实现option模块,对命令行参数进行解析,提供快速方便的命令行选项建立和解析操作,对于写终端程序还是很有帮助的

正则表达式库

  • 支持匹配和替换操作
  • 支持全局、多行、大小写不敏感等模式
  • 使用pcre, pcre2和posix正则库

一些使用tbox的项目:

使用 Xmake 编译

请先安装: xmake

# 默认直接编译当前主机平台
$ cd ./tbox
$ xmake

# 编译mingw平台
$ cd ./tbox
$ xmake f -p mingw --sdk=/home/mingwsdk
$ xmake

# 编译iphoneos平台
$ cd ./tbox
$ xmake f -p iphoneos
$ xmake

# 编译android平台
$ cd ./tbox
$ xmake f -p android --ndk=xxxxx
$ xmake

# 交叉编译
$ cd ./tbox
$ xmake f -p linux --sdk=/home/sdk #--bin=/home/sdk/bin
$ xmake

使用 xmake.sh 编译

$ ./configure
$ make

例子

#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;
}

技术支持

你可以考虑赞助我们也获取技术支持服务,[成为赞助商]

联系方式

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2009-present TBOOX Open Source Group Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ------------------------------------------------------------------------------- ## SUBCOMPONENTS The TBOX project contains subcomponents with separate copyright notices and license terms. Your use of the source code for the these subcomponents is subject to the terms and conditions of the following licenses. #### Boost Software License Boost Software License - Version 1.0 - August 17th, 2003 Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For src/tbox/platform/arch/{arm,arm64,mips,x86,x64}/context.{S,asm}: ``` /* Copyright Oliver Kowalke 2009. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) */ ``` #### ZLib License Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Jean-loup Gailly Mark Adler For src/tbox/hash/adler32.c: ``` /* adler32.c -- compute the Adler-32 checksum of a data stream * Copyright (C) 1995-2011, 2016 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ ```

简介

跨平台的c开发库,提供asio、stream、容器、算法、xml/json/plist解析、数据库等常用模块 展开 收起
Apache-2.0
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/tboox/tbox.git
git@gitee.com:tboox/tbox.git
tboox
tbox
tbox
master

搜索帮助

14c37bed 8189591 565d56ea 8189591