Exemplo n.º 1
0
_f77_xbksp(struct fdinfo *fio, struct ffsw *stat)
{
	struct fdinfo *llfio;
	ssize_t rret;
	int ret, whence;
	ff_offt pos, cwpos;
	long ii;
	struct f77_xf *xf_info;
	char *cb;
	int ijk;

	xf_info = (struct f77_xf *)fio->lyr_info;

/*
 *	If the file's been reading, determine whether our desired position is
 *	within the buffer.
 */
	if (fio->rwflag == READIN) {
		/* We want to be at last_lrdwaddr. However, we need to read the */
		/* control word before last_lrdwaddr. */
		/* Our current position is lrdwaddr  - rembytes */
		off_t offset;
		char *tmpptr;
		offset = xf_info->lrdwaddr -  xf_info->rembytes -
			xf_info->last_lrdwaddr;
		if ((xf_info->_ptr - xf_info->_base) >=	(offset+RDWLEN)){
			/* desired position is within the buffer */
			xf_info->lrdwaddr -= offset;
			xf_info->_ptr -= offset;
			if (xf_info->lrdwaddr == 0) {
				xf_info->last_lrdwaddr = 0;
			}
			else {
				/* read the trailing control word from the previous */
				/* record. */
				ii = 0;
				cb = (char *)ⅈ
				tmpptr = xf_info->_ptr - RDWLEN;
				for (ijk = 0; ijk < RDWLEN; ijk++) {
					*cb++ = *tmpptr++;
				}
#if	!(defined(_LITTLE_ENDIAN) && defined(_LP64))
				ii = (unsigned long)ii >> ((sizeof(ii) -4) *8);
#endif
				if (fio->rtype == TR_UX_MIPS) SWAPB(ii);
				xf_info->last_lrdwaddr = xf_info->lrdwaddr - ii - 2*RDWLEN;
			}
			xf_info->_cnt += offset;
			xf_info->flag |= ATEOR;
			xf_info->recbytes = 0;
			xf_info->rembytes = 0;
			fio->ateod = 0;
			/* Leave flag as READIN, since we have data in the buffer */
			return(0);
		}
Exemplo n.º 2
0
AFILE *
AFrdEShead (FILE *fp)

{
  AFILE *AFp;
  int NgI, Nv;
  long int offs;
  UT_float8_t STime;
  char Info[ES_MAXINFO];
  struct ES_preamb Fpreamb;
  struct ES_fixhead FheadF;
  struct ES_FEAhead FheadV;
  char GenItems[ES_MAXGENERIC];
  char str[20];
  struct AF_read AFr;

/* Set the long jump environment; on error return a NULL */
  if (setjmp (AFR_JMPENV))
    return NULL;	/* Return from a header read error */

/* Defaults and inital values */
  AFr = AFr_default;
  AFr.InfoX.Info = Info;
  AFr.InfoX.Nmax = ES_MAXINFO;

/* Read selected preamble values */
/* We do not know the byte order until after we have read the file magic */
  offs = RSKIP (fp, 8L);
  offs += RHEAD_V (fp, Fpreamb.Data_offset, DS_NATIVE);
  offs += RHEAD_V (fp, Fpreamb.Record_size, DS_NATIVE);
  offs += RHEAD_S (fp, Fpreamb.Magic);

/* Check the preamble file magic */
  if (SAME_CSTR (Fpreamb.Magic, FM_ESPS_BE))
    AFr.DFormat.Swapb = DS_EB;
  else if (SAME_CSTR (Fpreamb.Magic, FM_ESPS_LE))
    AFr.DFormat.Swapb = DS_EL;
  else {
    UTwarn ("AFrdEShead - %s", AFM_ES_BadId);
    return NULL;
  }

/* Fix up the words we have already read */
  if (UTswapCode (AFr.DFormat.Swapb) == DS_SWAP) {
    SWAPB (Fpreamb.Data_offset);
    SWAPB (Fpreamb.Record_size);
  }
 
/* Read selected values from the fixed part of the header */
  offs += RSKIP (fp, 32 - offs);
  offs += RHEAD_V (fp, FheadF.Type, AFr.DFormat.Swapb);
  offs += RSKIP (fp, 2);
  offs += RHEAD_S (fp, FheadF.Magic);
  offs += AFrdTextAFsp (fp, 26, "date: ", &AFr.InfoX, 1);
  offs += AFrdTextAFsp (fp,  8, "header_version: ", &AFr.InfoX, 1);
  offs += AFrdTextAFsp (fp, 16, "program_name: ", &AFr.InfoX, 1);
  offs += AFrdTextAFsp (fp,  8, "program_version: ", &AFr.InfoX, 1);
  offs += AFrdTextAFsp (fp, 26, "program_compile_date: ", &AFr.InfoX, 1);
  offs += RHEAD_V (fp, FheadF.Ndrec, AFr.DFormat.Swapb);
  offs += RSKIP (fp, 4);
  offs += RHEAD_V (fp, FheadF.Ndouble, AFr.DFormat.Swapb);
  offs += RHEAD_V (fp, FheadF.Nfloat, AFr.DFormat.Swapb);
  offs += RHEAD_V (fp, FheadF.Nlong, AFr.DFormat.Swapb);
  offs += RHEAD_V (fp, FheadF.Nshort, AFr.DFormat.Swapb);
  offs += RHEAD_V (fp, FheadF.Nchar, AFr.DFormat.Swapb);
  offs += RSKIP (fp, 8);
  offs += AFrdTextAFsp (fp, 8, "user: "******"AFrdEShead - %s: \"%d\"", AFM_ES_UnsType, (int) FheadF.Type);
    return NULL;
  }
  if (! SAME_CSTR (FheadF.Magic, Fpreamb.Magic)) {
    UTwarn ("AFrdEShead - %s", AFM_ES_IdMatch);
    return NULL;
  }
  if (FheadV.Fea_type != ES_FEA_SD) {
     UTwarn ("AFrdEShead - %s: \"%d\"", AFM_ES_UnsFea, (int) FheadV.Fea_type);
     return NULL;
  }

/* Determine the data format */
  if (FheadF.Nshort != 0) {
    AFr.NData.Nchan = FheadF.Nshort;
    AFr.DFormat.Format = FD_INT16;
  }
  else if (FheadF.Nlong != 0) {
    AFr.NData.Nchan = FheadF.Nlong;
    AFr.DFormat.Format = FD_INT32;
  }
  else if (FheadF.Nfloat != 0) {
    AFr.NData.Nchan = FheadF.Nfloat;
    AFr.DFormat.Format = FD_FLOAT32;
  }
  else if (FheadF.Ndouble != 0) {
    AFr.NData.Nchan = FheadF.Ndouble;
    AFr.DFormat.Format = FD_FLOAT64;
  }
  else {
    UTwarn ("AFrdEShead - %s", AFM_ES_UnsData);
    return NULL;
  }
  if (Fpreamb.Record_size != AF_DL[AFr.DFormat.Format] * AFr.NData.Nchan) {
    UTwarn ("AFrdEShead - %s", AFM_ES_UnsEncod);
    return NULL;
  }

/* Get the sampling frequency */
  if (! AF_getGeneric (GenItems, NgI, "record_freq", AFr.DFormat.Swapb,
		       1, ES_DOUBLE, &AFr.Sfreq)) {
    UTwarn ("AFrdEShead - %s", AFM_ES_NoSFreq);
    return NULL;
  }

  /* Other Generic Items */
  if (AF_getGeneric (GenItems, NgI, "start_time", AFr.DFormat.Swapb,
		     1, ES_DOUBLE, &STime)) {
    Nv = sprintf (str, "%.7g", STime);
    AFaddAFspRec ("start_time: ", str, Nv, &AFr.InfoX); 
  }
  /* Pick up "max_value" only if it is a single value */
  if (AF_getGeneric (GenItems, NgI, "max_value", AFr.DFormat.Swapb,
		     1, ES_DOUBLE, &STime)) {
    Nv = sprintf (str, "%.7g", STime);
    AFaddAFspRec ("max_value: ", str, Nv, &AFr.InfoX); 
  }

/* Set the parameters for file access */
  if (FheadF.Ndrec != 0)
    AFr.NData.Nsamp = FheadF.Ndrec * AFr.NData.Nchan;

  AFp = AFsetRead (fp, FT_ESPS, &AFr, AF_FIX_NSAMP_LOW | AF_FIX_NSAMP_HIGH);

  return AFp;
}
Exemplo n.º 3
0
/*
 * Write a f77 class file
 * Parameters:
 *      fio     - Pointer to fdinfo block
 *	bufptr	- bit pointer to where data is to go.
 *	nbytes	- Number of bytes to be written
 *	stat	- pointer to status return word
 *	fulp	- full or partial write mode flag
 *	ubc	- pointer to unused bit count (not used for IBM)
 */
ssize_t
_f77_xwrite(
struct fdinfo	*fio,
bitptr		bufptr,
size_t		nbytes,
struct ffsw	*stat,
int		fulp,
int		*ubc)
{
	ssize_t ret;
	size_t  bytomove, moved, bytes;
	struct f77_xf *xfinfo;	
	struct fflistreq list_array[1];
	long left;
	char *cbufptr;
	long ii;
	char *cb;
	int ijk;
	int cwbytes;
	int ernum;
	int zero = 0;

	cbufptr = BPTR2CP(bufptr);
	if ((BPBITOFF(bufptr) & 07) != 0) {
		ernum = FDC_ERR_REQ;
		goto eret;
	}
	if (*ubc != 0){
		ernum = FDC_ERR_UBC;
		goto eret;
	}
        xfinfo = (struct f77_xf *)fio->lyr_info;
/*
 *	If we've been reading, then try to switch the buffer into write mode.
 */
	if (fio->rwflag == READIN) {
		/*
		 * Issue an error if we are not positioned at a record
		 * boundary.   ffweof would terminate the current record, but
 		 * _cos_write overwrites the current record.   We need to
		 * decide which is the proper approach before permitting this
		 * here.
		 */
		if (!(xfinfo->flag & ATEOR) && !(fio->ateod ) && !(fio->ateof)) {
		ernum = FDC_ERR_NOTREC;
		goto eret;
		}
		ret = f77_xwrard(fio, stat);
		if (ret < 0) return(ERR);
	}

	fio->rwflag = WRITIN;

/*
 *	initialize a new record, if needed.
 */
	bytomove = nbytes;
	moved = 0;
/*
 *	Check for record size exceeded.
 */
	if (bytomove > 0) {
		if ((xfinfo->maxrecsize > 0) &&
			(xfinfo->recbytes + bytomove) > xfinfo->maxrecsize){
			ernum = FDC_ERR_MXREC;
			goto eret;
		}
	}
	if (xfinfo->recbytes == 0) {
		/* This is the start of the record */
		ii = bytomove;
		if (fio->rtype == TR_UX_MIPS) {SWAPB(ii);}
		if ((bytomove > 0) || (fulp == FULL)) {
/*
 *			Put our guess at a control word in the buffer.
 *			This is the control word at the beginning of record.
 */
			cwbytes = RDWLEN;
			cb = (char *)&ii;
#if	!(defined(_LITTLE_ENDIAN) && defined(_LP64))
			cb += sizeof(ii) - RDWLEN;	/* The control word is only RDWLEN bytes long */
#endif
			if ((xfinfo->_cnt + RDWLEN) >= xfinfo->_ffbufsiz) {
				/* only part of the control word will fit */
				/* in this buffer.  Insert what will fit. */
				for (ijk = 0; ijk < xfinfo->_ffbufsiz - xfinfo->_cnt; ijk++){
					*(xfinfo->_ptr++) = *cb++;
					cwbytes--;
				}
				/* buffer is full. write it out. */
				if (_f77_put_block(fio, stat, (size_t)xfinfo->_ffbufsiz) != 0)
					return(ERR);
			}
			for (ijk = 0; ijk < cwbytes; ijk++){
				*(xfinfo->_ptr++) = *cb++;
			}
			xfinfo->_cnt += cwbytes;
			xfinfo->recbytes += RDWLEN;
			xfinfo->cwwritten = 1;
		}
	}

	else {
		/* This record has already been started. */
		ii = (xfinfo->recbytes + bytomove - RDWLEN) ;
		if (fio->rtype == TR_UX_MIPS) {SWAPB(ii);}
		if (bytomove != 0) {
/*
 *			If the control word at the start of the 
 *			record is in the buffer, update it.
 */
			if (xfinfo->recbytes <= xfinfo->_cnt){
				char *tbptr;
				/* the whole control word is in the buffer */
				cb = (char *)&ii;
#if	!(defined(_LITTLE_ENDIAN) && defined(_LP64))
				cb += sizeof(ii) - RDWLEN;	/* The control word is only RDWLEN bytes long */
#endif
				tbptr = xfinfo->_ptr - xfinfo->recbytes;
				for (ijk = 0; ijk < RDWLEN; ijk++)
					*(tbptr++) = *cb++;
				xfinfo->cwwritten = 1;
			}		
			else if ((xfinfo->recbytes - RDWLEN) <= xfinfo->_cnt){
				char *tbptr;
				int istart;
				/* part of the control word is in the buffer */
				/* Insert what will fit. */
				cb = (char *)&ii;
#if	!(defined(_LITTLE_ENDIAN) && defined(_LP64))
				cb += sizeof(ii) - RDWLEN;	/* The control word is only RDWLEN bytes long */
#endif
				istart = xfinfo->recbytes -xfinfo->_cnt;
				cb += istart;
				tbptr = xfinfo->_base;
				for (ijk = istart; ijk < RDWLEN; ijk++)
					*(tbptr++) = *cb++;
				xfinfo->cwwritten = 0; /* 0 because this is */
						/* not the whole thing*/
			}
			else 
				xfinfo->cwwritten = 0;
		}
	}
/*
 *	loop putting data in buffer 
 */
	while (bytomove > 0) {
/*
 *		bytes tells when data has been moved.  Set it to zero
 *		unless someone moves some data in the loop
 */
/*
 *		If enough room for bytes, put them in the buffer
 */
		left = xfinfo->_ffbufsiz - xfinfo->_cnt;
		if (left == 0) {
			if (_f77_put_block(fio, stat, (size_t)xfinfo->_cnt) != 0)
				return(ERR);
			left = xfinfo->_ffbufsiz;
#ifdef	__CRAY
#pragma _CRI inline _f77_aligned
#elif	defined(__mips) || defined(_LITTLE_ENDIAN)
#pragma inline _f77_aligned
#endif
			if ((bytomove >= left) && _f77_aligned(cbufptr)) {
				/* We write directly from the user's buffer */
				bytes = bytomove - bytomove%xfinfo->_ffbufsiz;
				ret = XRCALL(fio->fioptr, writertn) fio->fioptr,
					CPTR2BP(cbufptr), bytes, stat, PARTIAL,
					&zero);
				if (ret != bytes){
					return(ERR);
				}	
				bytomove -= bytes;
				cbufptr += bytes;
				moved += bytes;
			}
		}
		bytes = (bytomove < left)? bytomove : left;
		memcpy(xfinfo->_ptr, cbufptr, bytes);
		xfinfo->_cnt += bytes;
		xfinfo->_ptr += bytes;
		cbufptr += bytes;
		bytomove -= bytes;
		moved += bytes;
	}
Exemplo n.º 4
0
AFILE *
AFrdBLhead (FILE *fp)

{
  AFILE *AFp;
  int Lb, offs;
  char Info[BL_MAXINFO];
  struct SPPACK_head Fhead;
  struct AF_read AFr;

/* Set the long jump environment; on error return a NULL */
  if (setjmp (AFR_JMPENV))
    return NULL;	/* Return from a header read error */

/* Defaults and inital values */
  AFr = AFr_default;
  AFr.InfoX.Info = Info;
  AFr.InfoX.Nmax = BL_MAXINFO;

/* Read the first part of the header */
  offs = AFrdTextAFsp (fp, 80, "cs1: ", &AFr.InfoX, 1);
  offs += AFrdTextAFsp (fp, 80, "cs2: ", &AFr.InfoX, 1);
  offs += AFrdTextAFsp (fp, 80, "cmd: ", &AFr.InfoX, 1);
  offs += RHEAD_V (fp, Fhead.Domain, DS_NATIVE);
  offs += RSKIP (fp, 2);	/* skip FrameSize */
  offs += RHEAD_V (fp, Fhead.Sfreq, DS_NATIVE);
  offs += RSKIP (fp, 4);	/* skip two fill values */
  offs += RHEAD_S (fp, Fhead.Magic);

/* Check the preamble file magic */
  if (SAME_CSTR (Fhead.Magic, FM_SPPACK_BE))
    AFr.DFormat.Swapb = DS_EB;
  else if (SAME_CSTR (Fhead.Magic, FM_SPPACK_LE))
    AFr.DFormat.Swapb = DS_EL;
  else {
    UTwarn ("AFrdBLhead - %s", AFM_BL_BadId);
    return NULL;
  }

/* Fix up the words we have already read */
  if (UTswapCode (AFr.DFormat.Swapb) == DS_SWAP) {
    SWAPB (Fhead.Domain);
    SWAPB (Fhead.Sfreq);
  }
 
/* Continue reading the header */
  offs += RHEAD_V (fp, Fhead.Dtype, AFr.DFormat.Swapb);
  offs += RHEAD_V (fp, Fhead.Resolution, AFr.DFormat.Swapb);
  offs += RHEAD_V (fp, Fhead.Compand, AFr.DFormat.Swapb);
  offs += RSKIP (fp, 240);
  offs += RHEAD_V (fp, Fhead.dl_magic, AFr.DFormat.Swapb);
  offs += RHEAD_V (fp, Fhead.left, AFr.DFormat.Swapb);
  offs += RHEAD_V (fp, Fhead.right, AFr.DFormat.Swapb);
  RSKIP (fp, (LHEAD - offs));

/* Set up the data format parameters */
  if (Fhead.Dtype == S_SAMPLEDDATA) {
    switch (Fhead.Compand) {
    case C_MULAW:
      Lb = 8;
      AFr.DFormat.Format = FD_MULAW8;
      break;
    case C_ALAW:
      Lb = 8;
      AFr.DFormat.Format = FD_ALAW8;
      break;
    case C_UNIFORM:
      Lb = 16;
      AFr.DFormat.Format = FD_INT16;
      break;
    default:
      UTwarn ("AFrdBLhead - %s: \"%d\"", AFM_BL_UnsComp, (int) Fhead.Compand);
      return NULL;
    }
  }
  else {
    UTwarn ("AFrdBLhead - %s: \"%d\"", AFM_BL_UnsData, (int) Fhead.Dtype);
    return NULL;
  }

  /* Error checks */
  if (Fhead.Resolution != Lb) {
    UTwarn ("AFrdBLhead - %s: \"%d\"", AFM_BL_UnsWLen, (int) Fhead.Resolution);
    return NULL;
  }
  if (Fhead.Domain != D_TIME) {
    UTwarn ("AFrdBLhead - %s: \"%d\"", AFM_BL_UnsDomain, (int) Fhead.Domain);
    return NULL;
  }
  if (Fhead.dl_magic == DLMAGIC) {
    AFr.NData.Nchan = 0L;
    if (Fhead.right)
      ++AFr.NData.Nchan;
    else if (Fhead.left)
      ++AFr.NData.Nchan;
  }

/* Set the parameters for file access */
  AFr.Sfreq = (double) Fhead.Sfreq;

  AFp = AFsetRead (fp, FT_SPPACK, &AFr, AF_NOFIX);

  return AFp;
}