写了一个连接字符串的lib, 请指点一二.
时间:2010-08-02
来源:互联网
- /*
- * FileName: mystring.c
- * eg. MyString *mystr = mystr_new(); mystr->add(mystr, "Hello World\n"); mystr->free(mystr);
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define INITSIZE 1024
- #define EXTSIZE 512
-
- struct _MyString;
- typedef struct _MyString MyString;
-
- struct _MyString {
- unsigned long size;
- unsigned long used;
- char *pstr;
- char *current;
-
- MyString *(*add)(MyString *, char *);
- int (*free)(MyString *);
- char *(*fetch)(MyString *);
- };
-
- MyString *mystr_add(MyString *ptr, char *str) {
- size_t len, newsize;
- char *tmp;
-
- if (!ptr) {
- return NULL;
- }
-
- len = strlen(str);
-
- if (ptr->size - ptr->used >= len) {
- memcpy(ptr->current, str, len);
- ptr->current += len;
- ptr->used += len;
- } else {
- newsize = (len / (ptr->size - ptr->used) + 1) * EXTSIZE + ptr->size; /* new malloc memory size */
- tmp = (char *) malloc ( newsize + 1 );
-
- if (!tmp) {
- return NULL;
- }
-
- memcpy(tmp, ptr->pstr, ptr->used);
- memcpy(tmp + ptr->used, str, len);
- tmp[newsize] = '\0';
-
- free(ptr->pstr);
-
- ptr->size = newsize;
- ptr->used += len;
- ptr->pstr = tmp;
- ptr->current = ptr->pstr + ptr->used;
- }
-
- return ptr;
- }
-
- int mystr_free(MyString *ptr) {
- if (!ptr) {
- return -1;
- }
-
- free(ptr->pstr);
- free(ptr);
-
- return 0;
- }
-
- char *mystr_fetch(MyString *ptr) {
- if (!ptr) {
- return NULL;
- }
-
- *(ptr->current) = '\0';
- return ptr->pstr;
- }
-
- MyString *mystr_new() {
- MyString *ptr;
- ptr = (MyString *) malloc (sizeof (MyString));
- if (!ptr) {
- return NULL;
- }
-
- ptr->pstr = (char *) malloc (INITSIZE + 1);
- if (!ptr->pstr) {
- return NULL;
- }
-
- ptr->pstr[INITSIZE] = '\0';
- ptr->size = INITSIZE;
- ptr->used = 0;
- ptr->current = ptr->pstr;
-
- ptr->add = mystr_add;
- ptr->free = mystr_free;
- ptr->fetch = mystr_fetch;
-
- return ptr;
- }
-
- int main() {
- MyString *mystr;
- mystr = mystr_new();
-
- mystr->add(mystr, "The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc.\n");
- mystr->add(mystr, " The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument. realloc does not zero newly allocated memory in the case of buffer growth.\n");
- mystr->add(mystr, "realloccalls mallocin order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when realloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call\n");
-
- printf("%s", mystr->fetch(mystr));
- printf("\nMemory size: %d Bytes, used: %d Bytes\n", mystr->size, mystr->used);
-
- mystr->free(mystr);
- return 0;
- }
作者: liexusong 发布时间: 2010-08-02
作者: liexusong 发布时间: 2010-08-02
作者: hellioncu 发布时间: 2010-08-02
请问有现成的吗? 我也不想自己写的.
作者: liexusong 发布时间: 2010-08-02
你这样写也没啥问题的,效率虽然没有增强,至少应该不会影响
s
作者: 雨过白鹭洲 发布时间: 2010-08-02
作者: peidright 发布时间: 2010-08-02
作者: rune_zhang 发布时间: 2010-08-02
*(ptr->current) = '\0';为啥要放在fetch而不是add中呢?
add时为啥不用realloc?
MyString *mystr;不能定义成结构只能用指针比较麻烦
作者: hellioncu 发布时间: 2010-08-02
作者: prolj 发布时间: 2010-08-02
作者: 雨过白鹭洲 发布时间: 2010-08-02
效率有增强的。 memcpy vs strcpy。
作者: OwnWaterloo 发布时间: 2010-08-02
作者: liexusong 发布时间: 2010-08-02
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28