DOHString *DohNewStringWithSize(const DOHString_or_char *so, int len) { int l = 0, max; String *str; char *s; if (DohCheck(so)) { s = (char *) String_data((String *) so); } else { s = (char *) so; } str = (String *) DohMalloc(sizeof(String)); str->hashkey = -1; str->sp = 0; str->line = 1; str->file = 0; max = INIT_MAXSIZE; if (s) { l = (int) len; if ((l + 1) > max) max = l + 1; } str->str = (char *) DohMalloc(max); str->maxsize = max; if (s) { strncpy(str->str, s, len); str->len = l; str->sp = l; } else { str->str[0] = 0; str->len = 0; } return DohObjMalloc(&DohStringType, str); }
DOHString * DohNewString(const DOH *so) { int l = 0, max; String *str; char *s; if (DohCheck(so)) s = Char(so); else s = (char *) so; str = (String *) DohMalloc(sizeof(String)); str->hashkey = -1; str->sp = 0; str->line = 1; str->file = 0; max = INIT_MAXSIZE; if (s) { l = (int) strlen(s); if ((l+1) > max) max = l+1; } str->str = (char *) DohMalloc(max); str->maxsize = max; if (s) { strcpy(str->str,s); str->len = l; str->sp = l; } else { str->str[0] = 0; str->len = 0; } return DohObjMalloc(&DohStringType,str); }
DOHString *DohNewStringEmpty(void) { int max = INIT_MAXSIZE; String *str = (String *) DohMalloc(sizeof(String)); str->hashkey = 0; str->sp = 0; str->line = 1; str->file = 0; str->str = (char *) DohMalloc(max); str->maxsize = max; str->str[0] = 0; str->len = 0; return DohObjMalloc(&DohStringType, str); }
DOH *DohNewList() { List *l; int i; l = (List *) DohMalloc(sizeof(List)); l->nitems = 0; l->maxitems = MAXLISTITEMS; l->items = (void **) DohMalloc(l->maxitems * sizeof(void *)); for (i = 0; i < MAXLISTITEMS; i++) { l->items[i] = 0; } l->file = 0; l->line = 0; return DohObjMalloc(&DohListType, l); }
static void CreatePool() { Pool *p = 0; p = (Pool *) DohMalloc(sizeof(Pool)); assert(p); p->ptr = (DohBase *) DohMalloc(sizeof(DohBase) * PoolSize); assert(p->ptr); memset(p->ptr, 0, sizeof(DohBase) * PoolSize); p->len = PoolSize; p->blen = PoolSize * sizeof(DohBase); p->current = 0; p->pbeg = ((char *) p->ptr); p->pend = p->pbeg + p->blen; p->next = Pools; Pools = p; }
char * swill_guess_mimetype(const char *filename) { char *cfilename; char *c; int i; cfilename = (char *) DohMalloc(strlen(filename)+1); strcpy(cfilename,filename); for (c = cfilename; *c; c++) { *c = tolower(*c); } c = cfilename+strlen(cfilename)-1; while (c >= cfilename) { if (*c == '.') { c++; for (i = 0; types[i].suffix; i++) { if (strcmp(c,types[i].suffix) == 0) { DohFree(cfilename); return types[i].mimetype; } } DohFree(cfilename); return "text/plain"; } c--; } DohFree(cfilename); return "text/plain"; }
DOH *DohNewVoid(void *obj, void (*del) (void *)) { VoidObj *v; v = (VoidObj *) DohMalloc(sizeof(VoidObj)); v->ptr = obj; v->del = del; return DohObjMalloc(&DohVoidType, v); }
static DOH *CopyString(DOH *so) { String *str; String *s = (String *) ObjData(so); str = (String *) DohMalloc(sizeof(String)); str->hashkey = s->hashkey; str->sp = s->sp; str->line = s->line; str->file = s->file; if (str->file) Incref(str->file); str->str = (char *) DohMalloc(s->len + 1); memcpy(str->str, s->str, s->len); str->maxsize = s->len; str->len = s->len; str->str[str->len] = 0; return DohObjMalloc(&DohStringType, str); }
/* ----------------------------------------------------------------------------- * CopyList() * * Make a shallow copy of a list. * ----------------------------------------------------------------------------- */ static DOH *CopyList(DOH *lo) { List *l, *nl; int i; l = (List *) ObjData(lo); nl = (List *) DohMalloc(sizeof(List)); nl->nitems = l->nitems; nl->maxitems = l->maxitems; nl->items = (void **) DohMalloc(l->maxitems * sizeof(void *)); for (i = 0; i < l->nitems; i++) { nl->items[i] = l->items[i]; Incref(nl->items[i]); } nl->file = l->file; if (nl->file) Incref(nl->file); nl->line = l->line; return DohObjMalloc(&DohListType, nl); }
DOH *DohNewFileFromFd(int fd) { DohFile *f; f = (DohFile *) DohMalloc(sizeof(DohFile)); if (!f) return 0; f->filep = 0; f->fd = fd; f->closeondel = 0; return DohObjMalloc(&DohFileType, f); }
DOH *DohNewFileFromFile(FILE *file) { DohFile *f; f = (DohFile *) DohMalloc(sizeof(DohFile)); if (!f) return 0; f->filep = file; f->fd = 0; f->closeondel = 0; return DohObjMalloc(&DohFileType, f); }
static DOH * CopyString(DOH *so) { int max; String *str; String *s = (String *) ObjData(so); str = (String *) DohMalloc(sizeof(String)); str->hashkey = -1; str->sp = s->sp; str->line = s->line; str->file = s->file; if (str->file) Incref(str->file); max = s->maxsize; str->str = (char *) DohMalloc(max+1); memmove(str->str, s->str, max); str->maxsize= max; str->len = s->len; str->str[str->len] = 0; return DohObjMalloc(&DohStringType,str); }
DOHString *DohNewString(const DOHString_or_char *so) { int l = 0, max; String *str; char *s; int hashkey = -1; if (DohCheck(so)) { str = (String *) ObjData(so); s = (char *) String_data((String *) so); l = s ? str->len : 0; hashkey = str->hashkey; } else { s = (char *) so; l = s ? (int) strlen(s) : 0; } str = (String *) DohMalloc(sizeof(String)); str->hashkey = hashkey; str->sp = 0; str->line = 1; str->file = 0; max = INIT_MAXSIZE; if (s) { if ((l + 1) > max) max = l + 1; } str->str = (char *) DohMalloc(max); str->maxsize = max; if (s) { strcpy(str->str, s); str->len = l; str->sp = l; } else { str->str[0] = 0; str->len = 0; } return DohObjMalloc(&DohStringType, str); }
DOH *DohNewFile(DOH *filename, const char *mode, DOHList *newfiles) { DohFile *f; FILE *file; char *filen; filen = Char(filename); file = fopen(filen, mode); if (!file) return 0; f = (DohFile *) DohMalloc(sizeof(DohFile)); if (!f) { fclose(file); return 0; } if (newfiles) Append(newfiles, filename); f->filep = file; f->fd = 0; f->closeondel = 1; return DohObjMalloc(&DohFileType, f); }
static int replace_simple(String *str, char *token, char *rep, int flags, int count, char *(*match) (char *, char *, char *, int)) { int tokenlen; /* Length of the token */ int replen; /* Length of the replacement */ int delta, expand = 0; int ic; int rcount = 0; int noquote = 0; char *c, *s, *t, *first; char *q, *q2; register char *base; int i; /* Figure out if anything gets replaced */ if (!strlen(token)) return 0; base = str->str; tokenlen = strlen(token); s = (*match) (base, base, token, tokenlen); if (!s) return 0; /* No matches. Who cares */ str->hashkey = -1; if (flags & DOH_REPLACE_NOQUOTE) noquote = 1; /* If we are not replacing inside quotes, we need to do a little extra work */ if (noquote) { q = strpbrk(base, "\"\'"); if (!q) { noquote = 0; /* Well, no quotes to worry about. Oh well */ } else { while (q && (q < s)) { /* First match was found inside a quote. Try to find another match */ q2 = end_quote(q); if (!q2) { return 0; } if (q2 > s) { /* Find next match */ s = (*match) (base, q2 + 1, token, tokenlen); } if (!s) return 0; /* Oh well, no matches */ q = strpbrk(q2 + 1, "\"\'"); if (!q) noquote = 0; /* No more quotes */ } } } first = s; replen = strlen(rep); delta = (replen - tokenlen); if (delta <= 0) { /* String is either shrinking or staying the same size */ /* In this case, we do the replacement in place without memory reallocation */ ic = count; t = s; /* Target of memory copies */ while (ic && s) { if (replen) { memcpy(t, rep, replen); t += replen; } rcount++; expand += delta; /* Find the next location */ s += tokenlen; if (ic == 1) break; c = (*match) (base, s, token, tokenlen); if (noquote) { q = strpbrk(s, "\"\'"); if (!q) { noquote = 0; } else { while (q && (q < c)) { /* First match was found inside a quote. Try to find another match */ q2 = end_quote(q); if (!q2) { c = 0; break; } if (q2 > c) c = (*match) (base, q2 + 1, token, tokenlen); if (!c) break; q = strpbrk(q2 + 1, "\"\'"); if (!q) noquote = 0; /* No more quotes */ } } } if (delta) { if (c) { memmove(t, s, c - s); t += (c - s); } else { memmove(t, s, (str->str + str->len) - s + 1); } } else { t += (c - s); } s = c; ic--; } if (s && delta) { memmove(t, s, (str->str + str->len) - s + 1); } str->len += expand; str->str[str->len] = 0; if (str->sp >= str->len) str->sp += expand; /* Fix the end of file pointer */ return rcount; } /* The string is expanding as a result of the replacement */ /* Figure out how much expansion is going to occur and allocate a new string */ { char *ns; int newsize; rcount++; ic = count - 1; s += tokenlen; while (ic && (c = (*match) (base, s, token, tokenlen))) { if (noquote) { q = strpbrk(s, "\"\'"); if (!q) { break; } else { while (q && (q < c)) { /* First match was found inside a quote. Try to find another match */ q2 = end_quote(q); if (!q2) { c = 0; break; } if (q2 > c) { c = (*match) (base, q2 + 1, token, tokenlen); if (!c) break; } q = strpbrk(q2 + 1, "\"\'"); if (!q) noquote = 0; } } } if (c) { rcount++; ic--; s = c + tokenlen; } else { break; } } expand = delta * rcount; /* Total amount of expansion for the replacement */ newsize = str->maxsize; while ((str->len + expand) >= newsize) newsize *= 2; ns = (char *) DohMalloc(newsize); assert(ns); t = ns; s = first; /* Copy the first part of the string */ if (first > str->str) { memcpy(t, str->str, (first - str->str)); t += (first - str->str); } for (i = 0; i < rcount; i++) { memcpy(t, rep, replen); t += replen; s += tokenlen; c = (*match) (base, s, token, tokenlen); if (noquote) { q = strpbrk(s, "\"\'"); if (!q) { noquote = 0; } else { while (q && (q < c)) { /* First match was found inside a quote. Try to find another match */ q2 = end_quote(q); if (!q2) { c = 0; break; } if (q2 > c) { c = (*match) (base, q2 + 1, token, tokenlen); if (!c) break; } q = strpbrk(q2 + 1, "\"\'"); if (!q) noquote = 0; /* No more quotes */ } } } if (i < (rcount - 1)) { memcpy(t, s, c - s); t += (c - s); } else { memcpy(t, s, (str->str + str->len) - s + 1); } s = c; } c = str->str; str->str = ns; if (str->sp >= str->len) str->sp += expand; str->len += expand; str->str[str->len] = 0; str->maxsize = newsize; DohFree(c); return rcount; } }