Пример #1
0
/**
 * @param hfile  valid GslHFile
 * @return offset of first zero byte or -1
 *
 * Find the offset of the first zero byte in a GslHFile.
 * This function is MT-safe and may be called from any thread.
 */
GslLong
gsl_hfile_zoffset (GslHFile *hfile)
{
  GslLong zoffset, l;
  guint8 sdata[1024], *p;
  gboolean seen_zero = FALSE;

  errno = EFAULT;
  g_return_val_if_fail (hfile != NULL, -1);
  g_return_val_if_fail (hfile->ocount > 0, -1);

  sfi_mutex_lock (&hfile->mutex);
  if (hfile->zoffset > -2) /* got valid offset already */
    {
      zoffset = hfile->zoffset;
      sfi_mutex_unlock (&hfile->mutex);
      return zoffset;
    }
  if (!hfile->ocount) /* bad */
    {
      sfi_mutex_unlock (&hfile->mutex);
      return -1;
    }
  hfile->ocount += 1; /* keep open for a while */
  sfi_mutex_unlock (&hfile->mutex);

  /* seek to literal '\0' */
  zoffset = 0;
  do
    {
      do
	l = gsl_hfile_pread (hfile, zoffset, sizeof (sdata), sdata);
      while (l < 0 && errno == EINTR);
      if (l < 0)
	{
	  gsl_hfile_close (hfile);
	  return -1;
	}

      p = memchr (sdata, 0, l);
      seen_zero = p != NULL;
      zoffset += seen_zero ? p - sdata : l;
    }
  while (!seen_zero && l);
  if (!seen_zero)
    zoffset = -1;

  sfi_mutex_lock (&hfile->mutex);
  if (hfile->zoffset < -1)
    hfile->zoffset = zoffset;
  sfi_mutex_unlock (&hfile->mutex);

  gsl_hfile_close (hfile);

  return zoffset;
}
Пример #2
0
static void
wave_handle_close (GslDataHandle *dhandle)
{
  WaveHandle *whandle = (WaveHandle*) dhandle;
  
  gsl_hfile_close (whandle->hfile);
  whandle->hfile = NULL;
}
Пример #3
0
/**
 * @param rfile  valid GslRFile
 *
 * Close and destroy a GslRFile.
 */
void
gsl_rfile_close (GslRFile *rfile)
{
  errno = EFAULT;
  g_return_if_fail (rfile != NULL);
  
  gsl_hfile_close (rfile->hfile);
  sfi_delete_struct (GslRFile, rfile);
  errno = 0;
}