Exemple #1
0
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";
}
static void DelList(DOH *lo) {
  List *l = (List *) ObjData(lo);
  int i;
  for (i = 0; i < l->nitems; i++)
    Delete(l->items[i]);
  DohFree(l->items);
  DohFree(l);
}
Exemple #3
0
static void
DelString(DOH *so) {
  String *s = (String *) ObjData(so);
  s->hashkey = -1;
  DohFree(s->str);
  s->str = 0;
  DohFree(s);
}
void DohDelete(DOH *obj) {
  DohBase *b = (DohBase *) obj;
  DohObjInfo *objinfo;

  if (!obj)
    return;
#if SWIG_DEBUG_DELETE
  if (!DohCheck(b)) {
    fputs("DOH: Fatal error. Attempt to delete a non-doh object.\n", stderr);
    abort();
  }
#endif
  if (b->flag_intern)
    return;
  assert(b->refcount > 0);
  b->refcount--;
  if (b->refcount <= 0) {
    objinfo = b->type;
    if (objinfo->doh_del) {
      (objinfo->doh_del) (b);
    } else {
      if (b->data)
	DohFree(b->data);
    }
    DohObjFree(b);
  }
}
Exemple #5
0
static void DelFile(DOH *fo) {
  DohFile *f = (DohFile *) ObjData(fo);
  if (f->closeondel) {
    if (f->filep) {
      fclose(f->filep);
    }
#ifdef DOH_INTFILE
    if (f->fd) {
      close(f->fd);
    }
#endif
  }
  DohFree(f);
}
Exemple #6
0
static void DelString(DOH *so) {
  String *s = (String *) ObjData(so);
  DohFree(s->str);
  DohFree(s);
}
Exemple #7
0
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;
  }
}
Exemple #8
0
static void Void_delete(DOH *vo) {
  VoidObj *v = (VoidObj *) ObjData(vo);
  if (v->del)
    (*v->del) (v->ptr);
  DohFree(v);
}