📄 tcutil.h
字号:
/************************************************************************************************* * The utility API of Tokyo Cabinet * Copyright (C) 2006-2008 Mikio Hirabayashi * This file is part of Tokyo Cabinet. * Tokyo Cabinet 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 any later version. Tokyo Cabinet 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 Tokyo * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA. *************************************************************************************************/#ifndef _TCUTIL_H /* duplication check */#define _TCUTIL_H#if defined(__cplusplus)#define __TCUTIL_CLINKAGEBEGIN extern "C" {#define __TCUTIL_CLINKAGEEND }#else#define __TCUTIL_CLINKAGEBEGIN#define __TCUTIL_CLINKAGEEND#endif__TCUTIL_CLINKAGEBEGIN#include <stdlib.h>#include <stdbool.h>#include <stdint.h>#include <time.h>#include <limits.h>#include <math.h>/************************************************************************************************* * basic utilities *************************************************************************************************//* String containing the version information. */extern const char *tcversion;/* Pointer to the call back function for handling a fatal error. The argument specifies the error message. The initial value of this variable is `NULL'. If the value is `NULL', the default function is called when a fatal error occurs. A fatal error occurs when memory allocation is failed. */extern void (*tcfatalfunc)(const char *);/* Allocate a region on memory. `size' specifies the size of the region. The return value is the pointer to the allocated region. This function handles failure of memory allocation implicitly. Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call when it is no longer in use. */void *tcmalloc(size_t size);/* Allocate a nullified region on memory. `nmemb' specifies the number of elements. `size' specifies the size of each element. The return value is the pointer to the allocated nullified region. This function handles failure of memory allocation implicitly. Because the region of the return value is allocated with the `calloc' call, it should be released with the `free' call when it is no longer in use. */void *tccalloc(size_t nmemb, size_t size);/* Re-allocate a region on memory. `ptr' specifies the pointer to the region. `size' specifies the size of the region. The return value is the pointer to the re-allocated region. This function handles failure of memory allocation implicitly. Because the region of the return value is allocated with the `realloc' call, it should be released with the `free' call when it is no longer in use. */void *tcrealloc(void *ptr, size_t size);/* Duplicate a region on memory. `ptr' specifies the pointer to the region. `size' specifies the size of the region. The return value is the pointer to the allocated region of the duplicate. Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call when it is no longer in use. */void *tcmemdup(const void *ptr, size_t size);/* Duplicate a string on memory. `str' specifies the string. The return value is the allocated string equivalent to the specified string. Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call when it is no longer in use. */char *tcstrdup(const void *str);/* Free a region on memory. `ptr' specifies the pointer to the region. If it is `NULL', this function has no effect. Although this function is just a wrapper of `free' call, this is useful in applications using another package of the `malloc' series. */void tcfree(void *ptr);/************************************************************************************************* * extensible string *************************************************************************************************/typedef struct { /* type of structure for an extensible string object */ char *ptr; /* pointer to the region */ int size; /* size of the region */ int asize; /* size of the allocated region */} TCXSTR;/* Create an extensible string object. The return value is the new extensible string object. */TCXSTR *tcxstrnew(void);/* Create an extensible string object from a character string. `str' specifies the string of the initial content. The return value is the new extensible string object containing the specified string. */TCXSTR *tcxstrnew2(const char *str);/* Create an extensible string object with the initial allocation size. `asiz' specifies the initial allocation size. The return value is the new extensible string object. */TCXSTR *tcxstrnew3(int asiz);/* Copy an extensible string object. `xstr' specifies the extensible string object. The return value is the new extensible string object equivalent to the specified object. */TCXSTR *tcxstrdup(const TCXSTR *xstr);/* Delete an extensible string object. `xstr' specifies the extensible string object. Note that the deleted object and its derivatives can not be used anymore. */void tcxstrdel(TCXSTR *xstr);/* Concatenate a region to the end of an extensible string object. `xstr' specifies the extensible string object. `ptr' specifies the pointer to the region to be appended. `size' specifies the size of the region. */void tcxstrcat(TCXSTR *xstr, const void *ptr, int size);/* Concatenate a character string to the end of an extensible string object. `xstr' specifies the extensible string object. `str' specifies the string to be appended. */void tcxstrcat2(TCXSTR *xstr, const char *str);/* Get the pointer of the region of an extensible string object. `xstr' specifies the extensible string object. The return value is the pointer of the region of the object. Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. */const void *tcxstrptr(const TCXSTR *xstr);/* Get the size of the region of an extensible string object. `xstr' specifies the extensible string object. The return value is the size of the region of the object. */int tcxstrsize(const TCXSTR *xstr);/* Clear an extensible string object. `xstr' specifies the extensible string object. The internal buffer of the object is cleared and the size is set zero. */void tcxstrclear(TCXSTR *xstr);/* Convert an extensible string object into a usual allocated region. `xstr' specifies the extensible string object. The return value is the pointer to the allocated region of the object. Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call when it is no longer in use. Because the region of the original object is deleted, it should not be deleted again. */void *tcxstrtomalloc(TCXSTR *xstr);/* Create an extensible string object from an allocated region. `ptr' specifies the pointer to the region allocated with `malloc' call. `size' specifies the size of the region. The return value is the new extensible string object wrapping the specified region. Note that the specified region is released when the object is deleted. */TCXSTR *tcxstrfrommalloc(void *ptr, int size);/* Perform formatted output into an extensible string object. `xstr' specifies the extensible string object. `format' specifies the printf-like format string. The conversion character `%' can be used with such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', `@', `?', `b', and `%'. `@' works as with `s' but escapes meta characters of XML. `?' works as with `s' but escapes meta characters of URL. `b' converts an integer to the string as binary numbers. The other conversion character work as with each original. The other arguments are used according to the format string. */void tcxstrprintf(TCXSTR *xstr, const char *format, ...);/* Allocate a formatted string on memory. `format' specifies the printf-like format string. The conversion character `%' can be used with such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', `@', `?', `b', and `%'. `@' works as with `s' but escapes meta characters of XML. `?' works as with `s' but escapes meta characters of URL. `b' converts an integer to the string as binary numbers. The other conversion character work as with each original. The other arguments are used according to the format string. The return value is the pointer to the region of the result string. Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call when it is no longer in use. */char *tcsprintf(const char *format, ...);/************************************************************************************************* * array list *************************************************************************************************/typedef struct { /* type of structure for an element of a list */ char *ptr; /* pointer to the region */ int size; /* size of the effective region */} TCLISTDATUM;typedef struct { /* type of structure for an array list */ TCLISTDATUM *array; /* array of data */ int anum; /* number of the elements of the array */ int start; /* start index of used elements */ int num; /* number of used elements */} TCLIST;/* Create a list object. The return value is the new list object. */TCLIST *tclistnew(void);/* Create a list object with expecting the number of elements. `anum' specifies the number of elements expected to be stored in the list. The return value is the new list object. */TCLIST *tclistnew2(int anum);/* Copy a list object. `list' specifies the list object. The return value is the new list object equivalent to the specified object. */TCLIST *tclistdup(const TCLIST *list);/* Delete a list object. `list' specifies the list object. Note that the deleted object and its derivatives can not be used anymore. */void tclistdel(TCLIST *list);/* Get the number of elements of a list object. `list' specifies the list object. The return value is the number of elements of the list. */int tclistnum(const TCLIST *list);/* Get the pointer to the region of an element of a list object. `list' specifies the list object. `index' specifies the index of the element. `sp' specifies the pointer to the variable into which the size of the region of the return value is assigned. The return value is the pointer to the region of the value. Because an additional zero code is appended at the end of the region of the return value,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -