http://blog.csdn.net/sunlion81/article/details/8844206 等
- #include <stdio.h>
- #include <pjlib.h>
- #include <pj/types.h> // for pj_init.
- #define THIS_FILE "main.c"
- typedef struct MY_NODE
- {
- // This must be the first member declared in the struct!
- PJ_DECL_LIST_MEMBER(struct MY_NODE);
- int val;
- } MY_NODE;
- /*
- 循环链表测试
- */
- void list_test(void);
- /*
- 打印循环链表信息
- */
- void list_print(pj_list_type* node);
- int main(int argc, char** argv)
- {
- pj_status_t status;
- // pjlib库初始化
- status = pj_init();
- if (status != PJ_SUCCESS)
- {
- char errmsg[PJ_ERR_MSG_SIZE];
- pj_strerror(status, errmsg, sizeof(errmsg));
- PJ_LOG(1,(THIS_FILE, "%s: %s [status=%d]", pj_init, errmsg, status));
- return -1;
- }
- // 循环链表测试
- list_test();
- // pjlib库关闭
- pj_shutdown();
- return 0;
- }
- /*
- 循环链表测试
- */
- void list_test(void)
- {
- int i;
- MY_NODE list;
- MY_NODE array[10];
- // 初始化一个空链表
- pj_list_init(&list);
- // 向链表插入节点
- for (i=0; i<10; ++i)
- {
- array[i].val = i;
- pj_list_insert_before(&list, &array[i]);
- }
- // 打印循环链表信息
- list_print(&list);
- // 删除链表中的节点
- for (i=0; i<10; ++i)
- {
- pj_list_erase(&array[i]);
- }
- // 打印循环链表信息
- list_print(&list);
- // 检查链表是否为空
- pj_assert(pj_list_empty(&list));
- }
- /*
- 打印循环链表信息
- */
- void list_print(pj_list_type* node)
- {
- int i;
- MY_NODE *it;
- MY_NODE *head;
- head = it = (MY_NODE *)node;
- if (it != NULL)
- {
- it = it->next;
- for (i=0; it != head; ++i)
- {
- PJ_LOG(3, (THIS_FILE, "node[%d] = %d", i, it->val));
- it = it->next;
- }
- }
- }
- #include <stdio.h>
- #include <pjlib.h>
- #include <pj/types.h> // for pj_init.
- #define THIS_FILE "main.c"
- /*
- 数组测试
- */
- void array_test(void);
- static pj_status_t matching_cb(const void *value);
- int main(int argc, char** argv)
- {
- pj_status_t status;
- // pjlib库初始化
- status = pj_init();
- if (status != PJ_SUCCESS)
- {
- char errmsg[PJ_ERR_MSG_SIZE];
- pj_strerror(status, errmsg, sizeof(errmsg));
- PJ_LOG(1,(THIS_FILE, "%s: %s [status=%d]", pj_init, errmsg, status));
- return -1;
- }
- // 数组测试
- array_test();
- // pjlib库关闭
- pj_shutdown();
- return 0;
- }
- /*
- 数组测试
- */
- void array_test(void)
- {
- char a = '@';
- char* p = NULL;
- char array[32] = "abc123";
- PJ_LOG(3, (THIS_FILE, "array = %s", array));
- // 向数组中特定位置插入一个元素
- pj_array_insert(array, sizeof(char), (unsigned)strlen(array), 3, &a);
- PJ_LOG(3, (THIS_FILE, "array = %s", array));
- // 删除数组中特定位置的一个元素
- pj_array_erase(array, sizeof(char), (unsigned)strlen(array), 3);
- array[strlen(array)-1] = '\0';
- PJ_LOG(3, (THIS_FILE, "array = %s", array));
- // 查找数组中的特定元素
- pj_array_find(array, sizeof(char), (unsigned)strlen(array), matching_cb, &p);
- }
- static pj_status_t matching_cb(const void *value)
- {
- char *p = (char*)value;
- PJ_LOG(3, (THIS_FILE, "*p = %s", p));
- // 具体匹配的代码需要自行编写
- return PJ_SUCCESS;
- }
- #include <stdio.h>
- #include <pjlib.h>
- #include <pj/types.h> // for pj_init.
- #define THIS_FILE "main.c"
- /*
- 字符串
- struct pj_str_t
- {
- char *ptr;
- pj_ssize_t slen;
- };
- */
- /*
- 字符串测试
- */
- void string_test(void);
- int main(int argc, char** argv)
- {
- pj_status_t status;
- // pjlib库初始化
- status = pj_init();
- if (status != PJ_SUCCESS)
- {
- char errmsg[PJ_ERR_MSG_SIZE];
- pj_strerror(status, errmsg, sizeof(errmsg));
- PJ_LOG(1,(THIS_FILE, "%s: %s [status=%d]", pj_init, errmsg, status));
- return -1;
- }
- // 字符串测试
- string_test();
- // pjlib库关闭
- pj_shutdown();
- return 0;
- }
- /*
- 字符串测试
- */
- void string_test(void)
- {
- {
- // char ==> pj_str_t
- pj_str_t str;
- str = pj_str("Hello PJ_STR_T!");
- PJ_LOG(3, (THIS_FILE, "str = %.*s", str.slen, str.ptr));
- }
- {
- // pj_str_t ==> char
- pj_str_t str;
- char buffer[256];
- // 使用C字符串产生常量字符串
- pj_cstr(&str, "Hello C char!");
- memset(buffer, 0, sizeof(buffer));
- strncpy(buffer, str.ptr, str.slen);
- PJ_LOG(3, (THIS_FILE, "buffer = %s", buffer));
- }
- {
- // 无符号整数转换成字符串
- char buffer[256];
- unsigned val = 123456;
- memset(buffer, 0, sizeof(buffer));
- pj_utoa(val, buffer);
- PJ_LOG(3, (THIS_FILE, "val[%d] => buffer[%s]", val, buffer));
- }
- {
- // 将字符串转换为无符号整数
- unsigned val;
- pj_str_t str;
- str = pj_str("9367");
- val = pj_strtoul(&str);
- PJ_LOG(3, (THIS_FILE, "str[%.*s] => val[%d]", str.slen, str.ptr, val));
- }
- {
- // 删除字符串头尾空白符
- pj_str_t str;
- str = pj_str(" a b c 1 2 3 ");
- PJ_LOG(3, (THIS_FILE, "str[%.*s]", str.slen, str.ptr));
- pj_strtrim(&str);
- PJ_LOG(3, (THIS_FILE, "str[%.*s]", str.slen, str.ptr));
- }
- }
- #include <stdio.h>
- #include <pjlib.h>
- #include <pj/types.h> // for pj_init.
- #define THIS_FILE "main.c"
- int main(int argc, char** argv)
- {
- pj_status_t status;
- pj_caching_pool ch_pool;
- // pjlib库初始化
- status = pj_init();
- if (status != PJ_SUCCESS)
- {
- char errmsg[PJ_ERR_MSG_SIZE];
- pj_strerror(status, errmsg, sizeof(errmsg));
- PJ_LOG(1,(THIS_FILE, "%s: %s [status=%d]", pj_init, errmsg, status));
- return -1;
- }
- // pjlib创建缓冲池工厂
- pj_caching_pool_init(&ch_pool, NULL, 1024*1024);
- /* 分配内存示例 */
- {
- pj_pool_t *pool;
- pj_pool_factory *factory =&ch_pool.factory;
- // 在分配内存前必须创建内存池
- pool = pj_pool_create(factory, "pjlib_demo", 4*1024, 4*1024, NULL);
- if (pool == NULL)
- {
- PJ_LOG(1,(THIS_FILE, "Error:pj_pool_create is NULL."));
- return PJ_ENOMEM;
- }
- // 循环申请内存
- {
- int i;
- for (i=0; i<1000; i++)
- {
- void* p;
- p = pj_pool_alloc(pool, (pj_rand()+1) % 512);
- }
- // 没有释放内存的函数!!!
- }
- // 释放内存池
- pj_pool_release(pool);
- }
- // pjlib销毁缓冲池工厂
- pj_caching_pool_destroy(&ch_pool);
- // pjlib库关闭
- pj_shutdown();
- return 0;
- }
- // 获得当前时间
- pj_time_val tv;
- pj_parsed_time pt;
- pj_gettimeofday(&tv);
- pj_time_decode(&tv, &pt);
- PJ_LOG(3,(THIS_FILE,
- "...%04d-%02d-%02d %02d:%02d:%02d.%03d",
- pt.year, pt.mon, pt.day,
- pt.hour, pt.min, pt.sec, pt.msec));