通用轻量级C函数库-qlibc
qlibc 是一个轻量级且功能强大的 C 语言库,旨在提供通用的数据结构和算法。它专注于保持体积小巧和高效,适合作为 GLib 的替代品,尤其适用于资源受限的环境。其遵循 BSD-2-Clause 许可证,允许自由地用于商业和非商业项目。qLibc is currently one of the most functionally-complete, publicly-licensed C/C++ libraries. The goal of the qLibc project is to provide a simple and powerful general purpose C/C++ library that includes all kinds of containers and general library routines. It provides a ready-made set of common container APIs with a consistent API look.
核心功能
qlibc 提供了一系列核心功能,包括但不限于:
数据结构:包括二叉树(左倾红黑树)、哈希表、静态哈希表和链表等,这些结构用于存储和组织数据。
对象容器:如列表、向量、队列和栈,支持基本的对象存储和操作。
实用工具:包括字符串处理、I/O 操作、文件处理、进程间通信(IPC)等。
编码和解码:提供 URL、Base64、Hex 等编码和解码功能。
哈希函数:包括 Murmur Hash、FNV Hash、MD5 等哈希算法的实现。
Core API Reference
Containers for Key/Value pairs
Tree Table --- in binary tree(left-leaning red-black tree) data structure.
Hash Table --- in hash-based data structure.
Static Hash Table --- in fixed size memory(array/mmapped/shared).
List Table --- in (doubly) linked-list data structure.
Containers for Objects
List --- Doubly Linked List.
Vector --- implements a growable array of elements.
Queue --- FIFO(First In First Out) implementation.
Stack --- LIFO(Last In First Out) implementation.
General utilities
String --- string trimmer, modifier, replacer, case converter, pattern detectors, ...
I/O --- non-blocking I/O, stream reader/writer, ...
File --- file locking, file/directory hander, path correctors, ...
IPC, Semaphore Shared-memory
En/decoders --- Url en/decoder, Base64 en/decoder, Hex en/decoder, ...
Hashes --- Murmur hases, FNV hases, MD5 hashes, ...
Time --- time diff, time format converstion, ...
Extension API Reference
Apache-style Configuration File Parser.
INI-style Configuration File Parser.
HTTP client.
Rotating File Logger.
Database(MySQL) interface.
Token-Bucket.
代码示例
以下是使用 qlibc 创建哈希表的示例代码:
#include <qlibc/qhashtbl.h>
int main() {
qhashtbl_t *tbl; // 定义哈希表
int value;
// 创建哈希表,QHASHTBL_THREADSAFE 使哈希表线程安全
tbl = qhashtbl(0, QHASHTBL_THREADSAFE);
// 向哈希表添加键值对
qhashtbl_put(tbl, "key1", &value, sizeof(int));
// 从哈希表获取值
int *pvalue = (int *)qhashtbl_get(tbl, "key1");
if (pvalue != NULL) {
printf("Value: %d\n", *pvalue);
}
// 释放哈希表
qhashtbl_free(tbl);
return 0;
}
使用 qlibc 进行内存分配和字符串操作的简单示例:
#include <qlibc/qlibc.h>
int main() {
void *ptr = qlibc_malloc(100); // 分配100字节的内存
if (ptr == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
return -1;
}
// 使用内存...
char *str = qlibc_strdup("Hello, World!");
printf("Copied string: %s\n", str);
qlibc_free(ptr); // 释放内存
qlibc_free(str); // 释放字符串
return 0;
}
在这个示例中使用 qlibc_malloc 函数分配了 100 字节的内存,并使用 qlibc_strdup 函数复制了一个字符串,最后使用 qlibc_free 函数释放了分配的内存和字符串所占用的内存。
qlibc 适用于多种应用场景,特别是在资源受限的系统中,如嵌入式系统、移动设备和低功耗设备。它的跨平台特性使其成为开发跨平台应用程序的理想选择。
最新版本:2.5
项目主页:https://github.com/wolkykim/qlibc