示例#1
0
文件: spf.c 项目: anygo/array4j
/*
 * Close feature stream. Close opened files, free memory and,
 * if stream is an output stream, flush buffer. No error code
 * returned, even if flushing generates an error.
 */
void spf_stream_close(spfstream_t *s)
{
    if (s) {

        spf_stream_flush(s);

        if (s->name) {
            free(s->name);
            if (s->f)
                fclose(s->f);
        }

        spf_header_free(s->header);
        spf_buf_free(s->buf);

        free(s);
    }
}
示例#2
0
/*
 * Allocate memory and initialize the header.
 */
spfheader_t *spf_header_init(const spfield_t *fld)
{
  spfheader_t *p;

  if ((p = (spfheader_t *)malloc(sizeof(spfheader_t))) == NULL) {
    fprintf(stderr, "spf_header_init() -- cannot allocate memory\n");
    return(NULL);
  }

  p->nfields = 0;
  p->field = NULL;
  
  if (fld)
    if (spf_header_add(p, fld) == 0) {
      fprintf(stderr, "spf_header_init() -- cannot add header field\n");
      spf_header_free(p);
      return(NULL);
    }

  return(p);
}
示例#3
0
/* 
 * Read header from file. Return a pointer to the allocated (possibly
 * empty) header or NULL in case of error.  
 */
spfheader_t *spf_header_read(FILE *f)
{
  spfheader_t *hp;
  int lino = 1;
  char c, line[5120], *p;
  char *name, *value;
  int n = 0;

  /* create empty header */
  if ((hp = spf_header_init(NULL)) == NULL) {
    fprintf(stderr, "spf_header_read(): cannot allocate memory\n");
    return(NULL);
  }
  
  c = getc(f);
  ungetc(c, f);

  if (c == '<') { /* variable length header */

    if (fgets(line, 5120, f) == NULL) {
      fprintf(stderr, "spf_header_read(): cannot read line %d\n", lino);
      spf_header_free(hp);
      return(NULL);
    }

    if (strcmp(line, "<header>\n") != 0) {
      fprintf(stderr, "spf_header_read(): expecting <header> tag at line %d\n", lino);
      spf_header_free(hp);
      return(NULL);
    }

    while (1) {

      lino++;
      if (fgets(line, 5120, f) == NULL) {
	fprintf(stderr, "spf_header_read(): cannot read line %d (maybe a missing </header> tag)\n", lino);
	spf_header_free(hp);
	return(NULL);
      }
      
      if (strcmp(line, "</header>\n") == 0)
	break;
      
      /* find out field name and value in line */
      p = line; 
      name = value = NULL;
      while (*p && strchr(" \t\n", *p)) /* skip heading blanks */
	p++;
      
      if (! *p) /* it's an empty line ==> skip it! */
	continue;
      
      /* find out name */
      name = p;
      p++;
      while (*p && strchr(" \t=", *p) == NULL)
	p++;
      
      if (! *p) {
	fprintf(stderr, "spf_header_read(): no separator in variable header at line %d\n", lino);
	spf_header_free(hp);
	return(NULL);
      }
    
      *p = 0;
      p++;
      while (*p && strchr(" \t=", *p))
	p++;
      
      if (! *p) {
	fprintf(stderr, "spf_header_read(): no value for attribute %s in variable header at line %d\n", name, lino);
	spf_header_free(hp);
	return(NULL);
      }
      
      value = p;
      p++;
      while (*p && *p != ';')
	p++;
      
      if (! *p) {
	fprintf(stderr, "spf_header_read(): no end delimiter for attribute %s in variable header at line %d\n", name, lino);
	spf_header_free(hp);
	return(NULL);
      }
      
      *p = 0;
      n++;
      
      /* reallocate memory */
      if ((hp->field = (spfield_t *)realloc(hp->field, n * sizeof(spfield_t))) == NULL) {
	fprintf(stderr, "spf_header_read(): cannot allocate memory\n");
	spf_header_free(hp);
	return(NULL);
      }
      hp->nfields = n;
      if ((hp->field[n-1].name = strdup(name)) == NULL || (hp->field[n-1].value = strdup(value)) == NULL) {
	fprintf(stderr, "spf_header_read(): cannot allocate memory\n");
	spf_header_free(hp);
	return(NULL);
      }
    }

  }

  return(hp);
}