⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strheap.c

📁 CFL是Unix下的通用抽象库,以简化Unix下的系统软件开发,CFL库中包含几个分组的函数和宏
💻 C
字号:
/* ----------------------------------------------------------------------------   CFL - A C Foundation Library   Copyright (C) 1994-2003  Mark A Lindner   This file is part of CFL.      This library is free software; you can redistribute it and/or   modify it under the terms of the GNU Library General Public   License as published by the Free Software Foundation; either   version 2 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   Library General Public License for more details.      You should have received a copy of the GNU Library General Public   License along with this library; if not, write to the Free   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   ----------------------------------------------------------------------------   $Log: strheap.c,v $   Revision 1.2  2003/03/14 09:02:30  markl   Code cleanup & bug fixes.   Revision 1.1  2003/03/03 08:20:08  markl   Initial checkin (clean compile on Solaris & OS X)   ----------------------------------------------------------------------------*//* Feature test switches */#include "config.h"/* System headers */#include <string.h>/* Local headers */#include "cfl/defs.h"#include "cfl/data.h"#include "cfl/system.h"/* Macros */#define C_STRHEAP_IBLOCKSZ 40#define C_STRHEAP_DELETED 0x01#define C_STRHEAP_SORTED 0x02#define C_STRHEAP_FRAG 0x04/* Functions */c_strheap_t *C_strheap_create(uint_t blocksz)  {  c_strheap_t *h;  if(blocksz < C_STRHEAP_MIN_BLOCKSZ)    return(NULL);  h = C_new(c_strheap_t);  h->mem = C_newstr(blocksz);  h->index = C_malloc(C_STRHEAP_IBLOCKSZ, struct c_strheap_ent);  h->blk = h->iblk = 1;  h->bot = h->flags = h->size = 0;  h->blocksz = blocksz;  return(h);  }/* */void C_strheap_destroy(c_strheap_t *h)  {  if(!h)    return;  C_free(h->mem);  C_free(h->index);  C_free(h);  }/* */uint_t C_strheap_store(c_strheap_t *h, const char *s)  {  size_t len;  uint_t freespace;  struct c_strheap_ent *n;  if(!h || !s)    return(0);  len = strlen(s) + 1;  freespace = (h->blk * h->blocksz) - h->bot;  if(len >= freespace)    {    h->blk += (((len - freespace) / h->blocksz) + 1);    h->mem = C_realloc(h->mem, h->blk * h->blocksz, char);    }  memcpy((void *)(h->mem + h->bot), (void *)s, len);  if(!(h->size) % C_STRHEAP_IBLOCKSZ)    h->index = C_realloc(h->index, ++(h->iblk) * C_STRHEAP_IBLOCKSZ,                         struct c_strheap_ent);  n = &(h->index[h->size]);  n->offset = h->bot;  n->len = len;  h->bot += len;  return(h->size + 1);  }/* */char *C_strheap_restore(c_strheap_t *h, uint_t index)  {  struct c_strheap_ent *p;  if(!h || !index || index > h->size)    return(NULL);  p = &(h->index[--index]);  return((p->flags & C_STRHEAP_DELETED) ? NULL : (h->mem + p->offset));  }/* */c_bool_t C_strheap_delete(c_strheap_t *h, uint_t index)  {  struct c_strheap_ent *p;  if(!h || !index)    return(FALSE);  if(index > h->size)    return(FALSE);  p = &(h->index[--index]);  if(p->flags & C_STRHEAP_DELETED)    return(FALSE);  p->flags |= C_STRHEAP_DELETED;  h->flags |= C_STRHEAP_FRAG;  return(TRUE);  }/* */c_strheap_t *C_strheap_load(const char *path)  {  FILE *fp;  c_strheap_t *h;  if(!path)    return(NULL);  if(!*path)    return(NULL);  if(!(fp = fopen(path, "r")))    return(NULL);  h = C_new(c_strheap_t);  if((fread((void *)h, sizeof(c_strheap_t), (size_t)1, fp)) != 1)    {    C_free(h);    fclose(fp);    return(NULL);    }  h->index = C_newa(h->iblk * C_STRHEAP_IBLOCKSZ, struct c_strheap_ent);  if((fread((void *)h->index, sizeof(struct c_strheap_ent),	    (size_t)(h->iblk * C_STRHEAP_IBLOCKSZ), fp))     != (size_t)(h->iblk * C_STRHEAP_IBLOCKSZ))    {    C_free(h->index);    C_free(h);    fclose(fp);    return(NULL);    }  h->mem = C_newstr(h->blk * h->blocksz);  if((fread((void *)h->mem, sizeof(char), (size_t)(h->blk * h->blocksz), fp))     != (size_t)(h->blk * h->blocksz))    {    C_free(h->mem);    C_free(h->index);    C_free(h);    fclose(fp);    return(NULL);    }  fclose(fp);  return(NULL);  }/* */c_bool_t C_strheap_save(c_strheap_t *h, const char *path)  {  FILE *fp;  if(!path || !h)    return(FALSE);  if(!*path)    return(FALSE);  if(!(fp = fopen(path, "w")))    return(FALSE);  if((fwrite((void *)h, sizeof(c_strheap_t), (size_t)1, fp)) != 1)    {    fclose(fp);    return(FALSE);    }  if((fwrite((void *)h->index, sizeof(struct c_strheap_ent),	     (size_t)(h->iblk * C_STRHEAP_IBLOCKSZ), fp))     != (size_t)(h->iblk * C_STRHEAP_IBLOCKSZ))    {    fclose(fp);    return(FALSE);    }  if((fwrite((void *)h->mem, sizeof(char), (size_t)(h->blk * h->blocksz), fp))     != (size_t)(h->blk * h->blocksz))    {    fclose(fp);    return(FALSE);    }  fclose(fp);  return(TRUE);  }/* end of source file */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -