?? cstring.c
字號:
/** SFSEXP: Small, Fast S-Expression Library version 1.0Written by Matthew Sottile (matt@lanl.gov)Copyright (2004). The Regents of the University of California. This materialwas produced under U.S. Government contract W-7405-ENG-36 for Los AlamosNational Laboratory, which is operated by the University of California forthe U.S. Department of Energy. The U.S. Government has rights to use,reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR THEUNIVERSITY MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITYFOR THE USE OF THIS SOFTWARE. If software is modified to produce derivativeworks, such modified software should be clearly marked, so as not to confuseit with the version available from LANL.Additionally, this program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (at youroption) any later version. Accordingly, this program is distributed in thehope that it will be useful, but WITHOUT ANY WARRANTY; without even theimplied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Seethe GNU General Public License for more details.LA-CC-04-094**//** * Implementation of stuff in cstring.h to make ron happy */#include "cstring.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include "sexp_memory.h"/** * growth size for cstrings -- default is 8k. use sgrowsize() to adjust. */static size_t cstring_growsize = 8192;void sgrowsize(size_t s) { assert(s > 0); cstring_growsize = s;}CSTRING *snew(size_t s) { CSTRING *cs; cs = (CSTRING *)sexp_malloc(sizeof(CSTRING)); assert(cs != NULL); cs->len = s; cs->curlen = 0; cs->base = (char *)sexp_calloc(sizeof(char),s); assert(cs->base != NULL); return cs;}CSTRING *sadd(CSTRING *s, char *a) { int alen; char *newbase; /* no string, so bail */ if (s == NULL) { return NULL; } /* nothing to add, return s */ if (a == NULL) { return s; } alen = strlen(a); if (s->curlen + alen >= s->len) { newbase = (char *)sexp_realloc(s->base, s->len+cstring_growsize+alen, s->len); if (! newbase) { perror("realloc string"); s->len = s->curlen = 0; s->base = 0; return 0; } s->len += cstring_growsize + alen; s->base = newbase; } memcpy(&s->base[s->curlen],a,alen); s->curlen += alen; s->base[s->curlen] = 0; return s;}CSTRING *saddch(CSTRING *s, char a) { char *newbase; if (s == NULL) { return NULL; } if (s->curlen + 1 >= s->len) { newbase = (char *)sexp_realloc(s->base, s->len+cstring_growsize+1, s->len); if (! newbase) { perror("realloc string"); s->len = s->curlen = 0; s->base = 0; return 0; } s->len += cstring_growsize+1; s->base = newbase; } s->base[s->curlen] = a; s->curlen++; s->base[s->curlen] = 0; return s;}CSTRING *strim(CSTRING *s) { char *newbase; if (s == NULL) { return NULL; } /* no trimming necessary? */ if (s->len == s->curlen+1) { return s; } newbase = (char *)sexp_realloc(s->base, s->curlen+1, s->len); if (!newbase) { perror("realloc string in trim"); s->len = s->curlen = 0; s->base = 0; return NULL; } s->len = s->curlen+1; s->base = newbase; return s;}char *toCharPtr(CSTRING *s) { return s->base;}void sempty(CSTRING *s) { s->curlen = 0;}void sdestroy(CSTRING *s) { sexp_free(s->base,s->len); sexp_free(s,sizeof(CSTRING));}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -