📄 tcutil.h
字号:
/************************************************************************************************* * The utility API of Tokyo Cabinet * Copyright (C) 2006-2009 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);/************************************************************************************************* * basic utilities (for experts) *************************************************************************************************//* type of the pointer to a comparison function. `aptr' specifies the pointer to the region of one key. `asiz' specifies the size of the region of one key. `bptr' specifies the pointer to the region of the other key. `bsiz' specifies the size of the region of the other key. `op' specifies the pointer to the optional opaque object. The return value is positive if the former is big, negative if the latter is big, 0 if both are equivalent. */typedef int (*TCCMP)(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);/* type of the pointer to a encoding or decoding function. `ptr' specifies the pointer to the region. `size' specifies the size of the region. `sp' specifies the pointer to the variable into which the size of the region of the return value is assigned. `op' specifies the pointer to the optional opaque object. If successful, the return value is the pointer to the result object allocated with `malloc' call, else, it is `NULL'. */typedef void *(*TCCODEC)(const void *ptr, int size, int *sp, void *op);/* type of the pointer to a callback function to process record duplication. `vbuf' specifies the pointer to the region of the value. `vsiz' specifies the size of the region of the value. `sp' specifies the pointer to the variable into which the size of the region of the return value is assigned. `op' specifies the pointer to the optional opaque object. The return value is the pointer to the result object allocated with `malloc'. It is released by the caller. If it is `NULL', the record is not modified. */typedef void *(*TCPDPROC)(const void *vbuf, int vsiz, int *sp, void *op);/* type of the pointer to a iterator function. `kbuf' specifies the pointer to the region of the key. `ksiz' specifies the size of the region of the key. `vbuf' specifies the pointer to the region of the value. `vsiz' specifies the size of the region of the value. `op' specifies the pointer to the optional opaque object. The return value is true to continue iteration or false to stop iteration. */typedef bool (*TCITER)(const void *kbuf, int ksiz, const void *vbuf, int vsiz, void *op);/************************************************************************************************* * 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);/* 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, ...);/************************************************************************************************* * extensible string (for experts) *************************************************************************************************//* 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);/*************************************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -