Example #1
0
File: buffer.c Project: erg/kayos
char *buffer_token(char *buffer, size_t buffer_length, char **next) {
	if(!buffer && !*next) {
		return 0;
	}
	char *end = buffer + buffer_length;
	char *ptr = buffer_skip_whitespace(buffer, buffer_length);
	char *found = buffer_skip_until(ptr, end - ptr, " \t\r\n", 4);
#ifdef DEBUG
	fprintf(stderr, "end: %p\n", end);
	fprintf(stderr, "ptr: %p\n", ptr);
	fprintf(stderr, "found: %p\n", found);
#endif
	if(found)
		*found = '\0';
	if(found < end)
		*next = found + 1;
	else
		*next = 0;
	return ptr;
}
Example #2
0
File: buffer.c Project: erg/kayos
char *buffer_find_eol(char *buffer, size_t buffer_length) {
	char *end = buffer + buffer_length;
	char *ptr = buffer_skip_until(buffer, buffer_length, "\r\n", 2);
	return buffer_take_while(ptr, end - ptr, "\r\n", 2);
}
Example #3
0
void
xml_read_callback(xmlreader* r, xml_read_callback_fn* fn) {
  ssize_t n;
  buffer* b = r->b;
  stralloc tag, attr, val;
  stralloc_init(&tag);
  stralloc_init(&attr);
  stralloc_init(&val);
  hmap_init(XML_HMAP_BUCKETS, &r->attrmap);

  while((n = buffer_skip_until(b, "<", 1)) > 0) {
    const char* s;
    stralloc_zero(&tag);
    r->self_closing = r->closing = 0;
    s = buffer_peek(b);

    if(*s == '/') {
      r->closing = 1;
      buffer_skipc(b);
    } else if(*s == '?') {
      r->self_closing = 1;
    } else if(*s == '!') {
      if(buffer_skip_until(b, ">", 1) <= 0)
        return;
      continue;
    }
    if((n = buffer_gettok_sa(b, &tag, " \n\t\r\v/>", 7)) < 0)
      return;
    stralloc_nul(&tag);
    buffer_skipspace(b);

    while((s = buffer_peek(b)) && isalpha(*s)) {
      char ch;
      int quoted = 0;
      const char* charset;
      stralloc_zero(&attr);
      stralloc_zero(&val);
      if((n = buffer_gettok_sa(b, &attr, "=", 1)) < 0)
        break;
      if(buffer_skipc(b) < 0)
        return;

      if(*buffer_peek(b) == '"') {
        if(buffer_skipc(b) < 0)
          return;
        quoted = 1;
      }
      charset = quoted ? "\"" : "/> \t\r\n\v";
      if((n = buffer_gettok_sa(b, &val, charset, str_len(charset))) < 0)
        break;
      if(quoted && buffer_skipc(b) < 0)
        return;
      stralloc_nul(&attr);
      stralloc_nul(&val);
      hmap_set(&r->attrmap, attr.s, attr.len, val.s, val.len + 1);
      if(!fn(r, XML_ATTRIBUTE, &attr, &val, NULL))
        return;
      buffer_skipspace(b);
    }
    buffer_skipspace(b);

    if((s = buffer_peek(b)) && str_chr("/?", *s) < 2) {
      r->self_closing = 1;
      r->closing = 0;
      buffer_skipc(b);
    }
    buffer_skipspace(b);
    if((s = buffer_peek(b)) && *s == '>')
      buffer_skipc(b);
    if(!fn(r, XML_ELEMENT, &tag, NULL, &r->attrmap))
      return;

    if(r->attrmap) {
      hmap_destroy(&r->attrmap);
      r->attrmap = NULL;
    }
    hmap_init(XML_HMAP_BUCKETS, &r->attrmap);
    stralloc_zero(&tag);
    if((n = buffer_gettok_sa(b, &tag, "<", 1)) < 0)
      return;
    s = buffer_peek(b);

    if(!is_whitespace(tag.s, tag.len)) {
      if(!fn(r, XML_TEXT, NULL, &tag, NULL))
        return;
    }
  }
}