📄 wbxml_buffers.c
字号:
/* * libwbxml, the WBXML Library. * Copyright (C) 2002-2005 Aymerick Jehanne <aymerick@jehanne.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * LGPL v2.1: http://www.gnu.org/copyleft/lesser.txt * * Contact: libwbxml@aymerick.com * Home: http://libwbxml.aymerick.com */ /** * @file wbxml_buffers.c * @ingroup wbxml_buffers * * @author Aymerick Jehanne <libwbxml@aymerick.com> * @date 02/03/12 * * @brief Generic Buffers Functions * * @note Original idea: Kannel Project (http://www.kannel.org/) */#include <limits.h>#include <ctype.h>#include "wbxml.h"/* Memory management define */#define WBXML_BUFFER_SPLIT_BLOCK 20/** * The Generic Buffer type */struct WBXMLBuffer_s{ WB_UTINY *data; /**< The data */ WB_ULONG len; /**< Length of data in buffer */ WB_ULONG malloced; /**< Length of buffer */ WB_ULONG malloc_block; /**< Malloc Block Size */ WB_BOOL is_static; /**< Is it a static buffer ? */};static WB_BOOL grow_buff(WBXMLBuffer *buffer, WB_ULONG size);static WB_BOOL insert_data(WBXMLBuffer *buffer, WB_ULONG pos, const WB_UTINY *data, WB_ULONG len);/********************************** * Public functions */WBXML_DECLARE(WBXMLBuffer *) wbxml_buffer_create_real(const WB_UTINY *data, WB_ULONG len, WB_ULONG malloc_block){ WBXMLBuffer *buffer = NULL; buffer = (WBXMLBuffer *) wbxml_malloc(sizeof(WBXMLBuffer)); if (buffer == NULL) return NULL; buffer->malloc_block = malloc_block; buffer->is_static = FALSE; if ((len <= 0) || (data == NULL)) { buffer->malloced = 0; buffer->len = 0; buffer->data = NULL; } else { if (len + 1 > malloc_block + 1) buffer->malloced = len + 1 + malloc_block; else buffer->malloced = malloc_block + 1; buffer->data = (WB_UTINY *) wbxml_malloc(buffer->malloced * sizeof(WB_UTINY)); if (buffer->data == NULL) { wbxml_free(buffer); return NULL; } buffer->len = len; memcpy(buffer->data, data, len); buffer->data[len] = '\0'; } return buffer;}WBXML_DECLARE(WBXMLBuffer *) wbxml_buffer_sta_create_real(const WB_UTINY *data, WB_ULONG len){ WBXMLBuffer *buffer = NULL; buffer = (WBXMLBuffer *) wbxml_malloc(sizeof(WBXMLBuffer)); if (buffer == NULL) { return NULL; } buffer->malloc_block = 0; buffer->is_static = TRUE; buffer->data = (WB_UTINY *) data; buffer->len = len; return buffer;}WBXML_DECLARE(void) wbxml_buffer_destroy(WBXMLBuffer *buffer){ if (buffer != NULL) { if (!buffer->is_static) { /* Free dynamic data */ wbxml_free(buffer->data); } /* Free structure */ wbxml_free(buffer); }}WBXML_DECLARE_NONSTD(void) wbxml_buffer_destroy_item(void *buff){ wbxml_buffer_destroy((WBXMLBuffer *) buff);}WBXML_DECLARE(WBXMLBuffer *) wbxml_buffer_duplicate(WBXMLBuffer *buff){ WBXMLBuffer *result = NULL; if (buff == NULL) return NULL; result = wbxml_buffer_create_real(wbxml_buffer_get_cstr(buff), wbxml_buffer_len(buff), wbxml_buffer_len(buff)); return result;}WBXML_DECLARE(WB_ULONG) wbxml_buffer_len(WBXMLBuffer *buffer){ if (buffer == NULL) return 0; return buffer->len;}WBXML_DECLARE(WB_BOOL) wbxml_buffer_get_char(WBXMLBuffer *buffer, WB_ULONG pos, WB_UTINY *result){ if ((buffer == NULL) || (pos >= buffer->len) || (pos < 0)) return FALSE; *result = buffer->data[pos]; return TRUE;}WBXML_DECLARE(void) wbxml_buffer_set_char(WBXMLBuffer *buffer, WB_ULONG pos, WB_UTINY ch){ if ((buffer != NULL) && !buffer->is_static && (pos < buffer->len)) buffer->data[pos] = ch;}WBXML_DECLARE(WB_UTINY *) wbxml_buffer_get_cstr(WBXMLBuffer *buffer){ if ((buffer == NULL) || (buffer->len == 0)) return WBXML_UTINY_NULL_STRING; return buffer->data;}WBXML_DECLARE(WB_BOOL) wbxml_buffer_insert(WBXMLBuffer *to, WBXMLBuffer *buffer, WB_ULONG pos){ if ((to != NULL) && (buffer != NULL) && !to->is_static) return insert_data(to, pos, buffer->data, buffer->len); return FALSE;}WBXML_DECLARE(WB_BOOL) wbxml_buffer_insert_cstr(WBXMLBuffer *to, WB_UTINY *str, WB_ULONG pos){ if ((to != NULL) && (str != NULL) && !to->is_static) return insert_data(to, pos, str, WBXML_STRLEN(str)); return FALSE;}WBXML_DECLARE(WB_BOOL) wbxml_buffer_append(WBXMLBuffer *dest, WBXMLBuffer *buff){ if ((dest == NULL) || dest->is_static) return FALSE; if (buff == NULL) return TRUE; return wbxml_buffer_append_data(dest, wbxml_buffer_get_cstr(buff), wbxml_buffer_len(buff));}WBXML_DECLARE(WB_BOOL) wbxml_buffer_append_data_real(WBXMLBuffer *buffer, const WB_UTINY *data, WB_ULONG len){ if ((buffer == NULL) || buffer->is_static) return FALSE; if ((data == NULL) || (len == 0)) return TRUE; return insert_data(buffer, buffer->len, data, len);}WBXML_DECLARE(WB_BOOL) wbxml_buffer_append_cstr_real(WBXMLBuffer *buffer, const WB_UTINY *data){ if ((buffer == NULL) || buffer->is_static) { return FALSE; } if (data == NULL) return TRUE; return wbxml_buffer_append_data(buffer, data, WBXML_STRLEN(data));}WBXML_DECLARE(WB_BOOL) wbxml_buffer_append_char(WBXMLBuffer *buffer, WB_UTINY ch){ WB_UTINY c = ch; if ((buffer == NULL) || buffer->is_static) return FALSE; return insert_data(buffer, buffer->len, &c, 1);}WBXML_DECLARE(WB_BOOL) wbxml_buffer_append_mb_uint_32(WBXMLBuffer *buffer, WB_ULONG value){ /** * A uintvar is defined to be up to 32 bits large * so it will fit in 5 octets (to handle continuation bits) */ WB_UTINY octets[5]; WB_LONG i = 0, start = 0; if ((buffer == NULL) || buffer->is_static) return FALSE; /** * Handle last byte separately; it has no continuation bit, * and must be encoded even if value is 0. */ octets[4] = (WB_UTINY) (value & 0x7f); value >>= 7; for (i = 3; value > 0 && i >= 0; i--) { octets[i] = (WB_UTINY) (0x80 | (value & 0x7f)); value >>= 7; } start = i + 1; return wbxml_buffer_append_data(buffer, octets + start, 5 - start);}WBXML_DECLARE(void) wbxml_buffer_delete(WBXMLBuffer *buffer, WB_ULONG pos, WB_ULONG len){ if ((buffer == NULL) || buffer->is_static) return; if (pos > buffer->len) pos = buffer->len; if (pos + len > buffer->len) len = buffer->len - pos; if (len > 0) { memmove(buffer->data + pos, buffer->data + pos + len, buffer->len - pos - len); buffer->len -= len; buffer->data[buffer->len] = '\0'; }}WBXML_DECLARE(void) wbxml_buffer_shrink_blanks(WBXMLBuffer *buffer){ WB_ULONG i = 0, j = 0, end = 0; WB_UTINY ch = 0; if ((buffer == NULL) || buffer->is_static) return; end = wbxml_buffer_len(buffer); for (i = 0; i < end; i++) { if (wbxml_buffer_get_char(buffer, i, &ch) && isspace(ch)) { /* Replace space by a whitespace */ if (ch != ' ') wbxml_buffer_set_char(buffer, i, ' '); /* Remove all following spaces */ j = i = i + 1; while (wbxml_buffer_get_char(buffer, j, &ch) && isspace(ch)) j++; if (j - i > 1) wbxml_buffer_delete(buffer, i, j - i); } }}WBXML_DECLARE(void) wbxml_buffer_strip_blanks(WBXMLBuffer *buffer){ WB_ULONG start = 0, end = 0, len = 0; WB_UTINY ch = 0; if ((buffer == NULL) || buffer->is_static) return; /* Remove whitespaces at beginning of buffer... */ while (wbxml_buffer_get_char(buffer, start, &ch) && isspace(ch) && start <= wbxml_buffer_len(buffer)) { start ++; } if (start > 0) wbxml_buffer_delete(buffer, 0, start); /* ... and at the end */ if ((len = wbxml_buffer_len(buffer)) > 0) { end = len = len - 1; while (wbxml_buffer_get_char(buffer, end, &ch) && isspace(ch) && end >= 0) { end--; } wbxml_buffer_delete(buffer, end + 1, len - end); }}WBXML_DECLARE(WB_LONG) wbxml_buffer_compare(WBXMLBuffer *buff1, WBXMLBuffer *buff2){ WB_LONG ret = 0, len = 0; if ((buff1 == NULL) || (buff2 == NULL)) { if ((buff1 == NULL) && (buff2 == NULL)) return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -