Beispiel #1
0
static int
ReadNativeData(FieldSpec *field,
               FILE *file)
{
   long    size;                /* size of element (bytes) */
   long    length;              /* number of elements */

   if (file  == NULL || field == NULL || field->type == NO_TYPE)
      {
         DebugMsg(1, "ReadNativeData: NULL argument or type NO_TYPE.");
         return FALSE;
      }

   size = InternTypeSize(field->type);
   length = FieldLength(field);

   if (field->data == NULL && length != 0)
      {
         field->data = malloc(length * size);
         if (field->data == NULL)
            {
               DebugMsg(1, "ReadNativeData: allocation failure.");
               return FALSE;
            }
      }

   return (NativeRead(field->data, field->type, length, file) == length);
}
Beispiel #2
0
void *
AllocSamples(long	nrec,
	     FieldSpec	**fields)
{
    if (fields == NULL || fields[0] == NULL || fields[1] != NULL
	|| fields[0]->occurrence != REQUIRED)
	return NULL;

    return malloc(nrec * FieldLength(fields[0])
		  * InternTypeSize(fields[0]->type));
}
Beispiel #3
0
static int
WriteNativeData(FieldSpec *field,
                FILE      *file)
{
   long    length;              /* number of elements */

   if (file  == NULL || field == NULL || field->type == NO_TYPE)
      return FALSE;

   length = FieldLength(field);

   if (length != 0 && field->data == NULL)
      return FALSE;

   return (NativeWrite(field->data, field->type, length, file) == length);
}
char* SlimList_Serialize(SlimList* self)
{
        char* buf = (char*)malloc(SlimList_SerializedLength(self)+1);
        char* write_ptr = buf;
        int listLength = SlimList_GetLength(self);
        int i;

        write_ptr += sprintf(write_ptr, "[%06d:", listLength);

        for(i = 0; i < listLength; i++)
        {
                unsigned char * nodeString = (unsigned char *) nodeStringAt(self, i);
                write_ptr += sprintf(write_ptr, "%06ld:%s:", FieldLength(nodeString), nodeString);
        }
        strcpy(write_ptr, "]");
        return buf;
}
Beispiel #5
0
long
WriteNativeSamples(void     *data,
                   long     nrec,
                   FieldSpec **fields,
                   FILE     *file)
{
   int          type;
   long length;

   if (data == NULL)
      {
         DebugMsg(1, "WriteNativeSamples: NULL data pointer.");
         return 0;
      }
   if (nrec < 0)
      {
         DebugMsg(1, ("WriteNativeSamples: "
                      "negative number of records specified."));
         return 0;
      }
   if (fields == NULL || fields[0] == NULL || fields[1] != NULL
       || fields[0]->occurrence != REQUIRED)
      {
         DebugMsg(1, "WriteNativeSamples: bad \"fields\" array.");
         return 0;
      }
   if (file == NULL)
      {
         DebugMsg(1, "WriteNativeSamples: NULL file pointer.");
         return 0;
      }

   type = fields[0]->type;

   if (type == NO_TYPE)
      return 0;

   length = FieldLength(fields[0]);

   if (nrec*length == 0)
      return nrec;

   return NativeWrite(data, type, nrec*length, file) / length;
}
Beispiel #6
0
long
NativeRecordSize(FieldList list)
{
   long size;           /* total array size in bytes */
   FieldSpec    **fld_order;
   long i;
   long fld_size;

   fld_order = FieldOrder(list);

   if (fld_order == NULL)
      return 0;

   size = 0;

   for (i = 0 ; fld_order[i] != NULL; i++)
      {
         if (fld_order[i]->occurrence == OPTIONAL)
            {
               size = -1;
               break;
            }

         fld_size =
            FieldLength(fld_order[i])
            * NativeTypeSize(fld_order[i]->type);

         if (fld_size < 0)
            {
               size = -1;
               break;
            }
         else
            size += fld_size;
      }

   free(fld_order);

   return size;
}
Beispiel #7
0
main(int   argc,
     char  **argv)
{
    extern int	    optind;	/* for use of getopt() */
    extern char	    *optarg;	/* for use of getopt() */
    int		    ch;		/* command-line option letter */

    static char	    *ProgName = "esig2fea";	/* name of this program */
    static char	    *Version = SCCS_VERSION;	/* program SCCS version */
    static char	    *Date = SCCS_DATE;		/* program SCCS date */

    char	    **field_names = NULL;
    int		    num_fields = 0;
    int		    alloc_fields = 0;

    int		    rflag = NO;	/* -r option specified? */
    char	    *rrange;	/* arguments of -r option */
    long	    start_rec;	/* starting record number */
    long	    end_rec;	/* ending record number */
    long	    num_recs;	/* number of records to read
				   (0 means all up to end of file) */
    long	    num_read;	/* number of records actually read */

    char	    *iname;	/* input file name */
    FILE	    *ifile;	/* input stream */
    FieldList	    list;	/* input field list */
    int		    inord;	/* input: field order or type order? */
    FieldSpec	    **ifields;	/* input fields in field or type order */

    char	    *subtype = NULL;		/* FEA subtype name */
    int		    subtype_code = NONE;	/* numeric subtype code */
    FieldSpec	    *fld;	/* spec of various special fields */

    char	    *oname;	/* output file name */
    FILE	    *ofile;	/* output stream */
    struct header   *ohd;	/* output file header */
    struct fea_data *orec;	/* output record */
    int		    outord = TYPE_ORDER;

    char	    *version;	/* version from input preamble */
    int		    arch;	/* architecture from input preamble */
    long	    pre_size;	/* preamble size */
    long	    hdr_size;	/* header size (bytes) from preamble */
    long	    rec_size;	/* record size from preamble */

    double	    rec_freq;
    double	    start_time_offset;
    double	    *data;
    long	    len, i;


    struct header   *source;	/* embedded source-file header */


    while ((ch = getopt(argc, argv, "f:r:x:FT:")) != EOF)
	switch (ch)
	{
	case 'f':
	    if (num_fields >= alloc_fields)
	    {
		size_t	size;

		alloc_fields = num_fields + 1 + num_fields/2;
		size = (alloc_fields + 1) * sizeof(char *);
		field_names = (char **)
		    ((field_names == NULL)
		     ? malloc(size)
		     : realloc(field_names, size));
	    }
	    field_names[num_fields++] = optarg;
	    field_names[num_fields] = NULL;
	    break;
	case 'r':
	    rflag = YES;
	    rrange = optarg;
	    break;
	case 'x':
	    debug_level = atoi(optarg);
	    break;
	case 'F':
	    outord = FIELD_ORDER;
	    break;
	case 'T':
	    subtype = optarg;
	    break;
	default:
	    SYNTAX;
	    break;
	}

    if (argc - optind > 2)
    {
	fprintf(stderr,
		"%s: too many file names specified.\n", ProgName);
	SYNTAX;
    }
    if (argc - optind < 2)
    {
	fprintf(stderr,
		"%s: too few file names specified.\n", ProgName);
	SYNTAX;
    }


    DebugMsgLevel = debug_level;
    DebugMsgFunc = DebugPrint;

    iname = argv[optind++];
    list = OpenIn(iname, &version,
		  &arch, &pre_size, &hdr_size, &rec_size, &ifile);
    REQUIRE(list != NULL, "read header failed");
    if (ifile == stdin)
	iname = "<stdin>";

    oname = argv[optind++];

    start_rec = 0;
    end_rec = LONG_MAX;
    num_recs = 0;	/* 0 means continue to end of file */

    if (rflag)
    {
	lrange_switch(rrange, &start_rec, &end_rec, 0);
	if (end_rec != LONG_MAX)
	    num_recs = end_rec - start_rec + 1;
    }

    REQUIRE(start_rec >= 0, "can't start before beginning of file");
    REQUIRE(end_rec >= start_rec, "empty range of records specified");

    if (debug_level)
	fprintf(stderr,
		"start_rec: %ld.  end_rec: %ld.  num_recs: %ld.\n",
		start_rec, end_rec, num_recs);

    REQUIRE(GetFieldOrdering(list, &inord),
	    "cant get field ordering of input");

    switch (inord)
    {
    case TYPE_ORDER:
	if (debug_level)
	    fprintf(stderr, "making type-ordered field array.\n");
	ifields = TypeOrder(list);
	break;
    case FIELD_ORDER:
	if (debug_level)
	    fprintf(stderr, "making field-ordered field array.\n");
	ifields = FieldOrder(list);
	break;
    default:
	REQUIRE(0, "input order neither TYPE_ORDER nor FIELD_ORDER");
	break;
    }

    ohd = FieldList_to_fea(list, &orec, field_names, FALSE);

    REQUIRE(ohd != NULL,
	    "failure converting input field list to header & record struct");

    if (subtype != NULL)
    {
	subtype_code = lin_search(fea_file_type, subtype);

	if (subtype_code == -1)
	    fprintf(stderr, "%s: unknown FEA file subtype \"%s\" ignored.\n",
		    ProgName, subtype);
	else
	    ohd->hd.fea->fea_type = subtype_code;
    }

    if (outord == FIELD_ORDER)
	ohd->hd.fea->field_order = YES;

    fld = FindField(list, "recordFreq");

    if (fld != NULL && fld->occurrence == GLOBAL && fld->data != NULL)
    {
	(void) type_convert(1L, (char *) fld->data, ElibTypeToEsps(fld->type),
			    (char *) &rec_freq, FDOUBLE, (void (*)()) NULL);

	*add_genhd_d("record_freq", NULL, 1, ohd) = rec_freq;
    }
    else
	rec_freq = 1.0;

    fld = FindField(list, "startTime");

    if (fld != NULL
	&& fld->occurrence == GLOBAL && fld->data != NULL && rec_freq != 0)
    {
	start_time_offset = start_rec / rec_freq;

	len = FieldLength(fld);
	data = (double *) type_convert(len, (char *) fld->data,
				       ElibTypeToEsps(fld->type),
				       (char *) NULL, FDOUBLE,
				       (void (*)()) NULL);

	if (start_time_offset != 0)
	{
	    for (i = 0; i < len; i++)
		data[i] += start_time_offset;
	}

	(void) add_genhd_d("start_time", data, len, ohd);
    }

    (void) strcpy(ohd->common.prog, ProgName);
    (void) strcpy(ohd->common.vers, Version);
    (void) strcpy(ohd->common.progdate, Date);

    source = FieldList_to_fea(list, NULL, NULL, TRUE);
    add_source_file(ohd, savestring(iname), source);

    add_comment(ohd, get_cmd_line(argc, argv));

    oname = eopen(ProgName,
		  oname, "w", NONE, NONE, NULL, &ofile);
    write_header(ohd, ofile);

    num_read = SkipRecs(ifile, start_rec,
			RecordSize(list, arch), ifields, arch);

    if (num_read != start_rec)
    {
	fprintf(stderr,
		"%s: couldn't reach starting record; only %ld skipped.\n",
		ProgName, num_read);
	exit(0);
    }

    for ( ;
	 num_read <= end_rec && ReadRecord(ifields, arch, ifile);
	 num_read++)
    {
	put_fea_rec(orec, ohd, ofile);
    }

    if (num_read <= end_rec && num_recs != 0)
	fprintf(stderr, "esig2fea: only %ld records read.\n",
		num_read - start_rec);

    exit(0);
    /*NOTREACHED*/
}
Beispiel #8
0
static struct header *
FieldList_to_fea(FieldList	    list,
		 struct fea_data    **rec,
		 char		    **fnames,
		 int		    copy_sources)
{
    struct header	*hdr;
    int			i, j;
    FieldSpec		*fld;
    char		**codes;
    FieldList		subfields;
    FieldSpec		*subf;
    char		*name;
    void		*ptr;
    int			spstype;
    long		*dim;
    struct fea_header	*fea;
    struct header	*source;

    if (list == NULL)
	return NULL;

    hdr = new_header(FT_FEA);

    for (i = 0; (fld = list[i]) != NULL; i++)
    {
	codes = NULL;
	if (fld->occurrence != VIRTUAL
	    && (subfields = fld->subfields) != NULL)
	{
	    for (j = 0; (subf = subfields[j]) != NULL; j++)
	    {
		if (strcmp(subf->name, "enumStrings") != 0)
		{
		    if (debug_level)
			fprintf(stderr,
				"FieldList_to_fea: subfields "
				"not supported in ESPS FEA files.\n");
		}
		else if (fld->type != SHORT)
		{
		    if (debug_level)
			fprintf(stderr,
				"FieldList_to_fea: Non-SHORT field "
				"has subfield enumStrings.\n");
		}
		else if (subf->type != CHAR)
		{
		    if (debug_level)
			fprintf(stderr,
				"FieldList_to_fea: enumStrings not CHAR.\n");
		}
		else if (subf->rank != 2)
		{
		    if (debug_level)
			fprintf(stderr,
				"FieldList_to_fea: enumStrings "
				"not of rank 2.\n");
		}
		else
		    codes = StrArrayFromRect(subf->dim, subf->data);
	    }
	}

	if (FieldIsTag(fld) && FindStr(ESPS_TAG, fnames))
	    hdr->common.tag = TRUE;
	else if (FieldIsFeaSubtype(fld))
	    hdr->hd.fea->fea_type = *(short *) fld->data;
	else
	{
	    name = fld->name;

	    switch(fld->occurrence)
	    {
	    case GLOBAL:
		{
		    int     size = (int) FieldLength(fld);

		    ptr = fld->data;

		    if (debug_level >= 2)
			fprintf(stderr,
				"FieldList_to_fea: "
				"global field[%d]: \"%s\".\n",
				i, name);

		    if (fld->rank > 1)
		    {
			if (debug_level)
			    fprintf(stderr,
				    "FieldList_to_fea: rank %d globals "
				    "not supported in ESPS FEA files.\n",
				    fld->rank);
		    }

		    if (size == 0)
		    {
			if (debug_level)
			    fprintf(stderr,
				    "FieldList_to_fea: empty globals "
				    "not supported in ESPS FEA files.\n");
		    }
		    else if (codes != NULL)
			(void) add_genhd_e(name,
					   (short *) ptr, size, codes, hdr);
		    else
			switch (fld->type)
			{
			case DOUBLE:
			    (void) add_genhd_d(name,
					       (double *) ptr, size, hdr);
			    break;
			case FLOAT:
			    (void) add_genhd_f(name,
					       (float *) ptr, size, hdr);
			    break;
			case LONG:
			    (void) add_genhd_l(name,
					       (long *) ptr, size, hdr);
			    break;
			case SHORT:
			    (void) add_genhd_s(name,
					       (short *) ptr, size, hdr);
			    break;
			case SCHAR:
			case UCHAR:
			case CHAR:
			    (void) add_genhd_c(name,
					       (char *) ptr, size, hdr);
			    break;
			default:
			    if (debug_level)
				fprintf(stderr,
					"FieldList_to_fea: global type %d "
					"not supported in ESPS FEA files.\n",
					fld->type);
			}
		}
		break;
	    case REQUIRED:
		{
		    long    size = FieldLength(fld);

		    if (debug_level >= 2)
			fprintf(stderr,
				"FieldList_to_fea: "
				"required field[%d]: \"%s\".\n",
				i, name);

		    if (FindStr(name, fnames))
		    {
			spstype = (codes != NULL) ? CODED
			    : ElibTypeToEsps(fld->type);
			if (spstype != UNDEF)
			{
			    dim = (long *) malloc(fld->rank * sizeof(long));
			    for (j = 0; j < fld->rank; j++)
				dim[j] = fld->dim[j];
			    add_fea_fld(name, size, fld->rank,
					dim, spstype, codes, hdr);
			}
		    }
		}
		break;
	    case OPTIONAL:
		if (debug_level)
		    fprintf(stderr,
			    "FieldList_to_fea: optional fields "
			    "not supported in ESPS FEA files.\n");
		break;
	    case VIRTUAL:
		if (copy_sources)
		{
		    if (strncmp(name, "source_", 7) != 0)
		    {
			if (debug_level)
			    fprintf(stderr, "Field_List_to_fea: VIRTUAL "
				    "field other than source_<n>.\n");
		    }
		    else if ((subfields = fld->subfields) != NULL
			     || fld->type == CHAR)
		    {
			size_t	len;
			char	*data;

			source = FieldList_to_fea(subfields,
						  NULL, NULL, TRUE);
			len = FieldLength(fld);
			data = (char *) malloc(len + 1);
			strncpy(data, fld->data, len);
			data[len] = '\0';

			add_source_file(hdr, data, source);
		    }
		}
		break;
	    case INCLUDED:
		if (debug_level)
		    fprintf(stderr,
			    "FieldList_to_fea: included fields "
			    "not supported in ESPS FEA files.\n");
		break;
	    default:
		spsassert(0, "FieldList_to_fea: "
			  "unrecognized occurrence code.\n");
		break;
	    }
	}
    }

    if (rec != NULL)
    {
	*rec = allo_fea_rec(hdr);
	fea = hdr->hd.fea;

	if (hdr->common.tag)
	{
	    fld = FindField(list, ESPS_TAG);
	    fld->data = &(*rec)->tag;
	}

	for (i = 0; i < (int) fea->field_count; i++)
	{
	    name = fea->names[i];
	    fld = FindField(list, name);
	    fld->data = get_fea_ptr(*rec, name, hdr);
	}
    }

    return hdr;
}