Beispiel #1
0
/*
 * Sortof like ftruncate, except won't make the
 * file shorter.
 */
static int
fgrow(const int fd, const off_t len)
{
	struct ffc_stat_s sb;
	struct ffsw sw;
	if (fffcntl(fd, FC_STAT, &sb, &sw) < 0)
		return errno;
	if (len < sb.st_size)
		return ENOERR;
	{
		const long dumb = 0;
			/* cache current position */
		const off_t pos = ffseek(fd, 0, SEEK_CUR);
		if(pos < 0)
			return errno;
		if (ffseek(fd, len-sizeof(dumb), SEEK_SET) < 0)
			return errno;
		if(ffwrite(fd, (void *)&dumb, sizeof(dumb)) < 0)
			return errno;
		if (ffseek(fd, pos, SEEK_SET) < 0)
			return errno;
	}
	/* else */
	return ENOERR;
}
Beispiel #2
0
/*
 * Sortof like ftruncate, except won't make the file shorter.  Differs
 * from fgrow by only writing one byte at designated seek position, if
 * needed.
 */
static int
fgrow2(const int fd, const off_t len)
{
	struct ffc_stat_s sb;
	struct ffsw sw;
	if (fffcntl(fd, FC_STAT, &sb, &sw) < 0)
		return errno;
	if (len <= sb.st_size)
		return ENOERR;
	{
	    const char dumb = 0;
	    /* we don't use ftruncate() due to problem with FAT32 file systems */
	    /* cache current position */
	    const off_t pos = ffseek(fd, 0, SEEK_CUR);
	    if(pos < 0)
		return errno;
	    if (ffseek(fd, len-1, SEEK_SET) < 0)
		return errno;
	    if(ffwrite(fd, (void *)&dumb, sizeof(dumb)) < 0)
		return errno;
	    if (ffseek(fd, pos, SEEK_SET) < 0)
		return errno;
	}
	return ENOERR;
}
Beispiel #3
0
// *****************************************************************************************************************
void beispiele(void){

  char datei[12]="TEST    TXT";		// hier muss platz für 11 zeichen sein (8.3), da fat_str diesen string benutzt !!
  fat_str(datei);						// wandelt "test.txt" in das fat format 8.3 der form: "TEST    TXT" muss immer dieses Format haben, auch ordner !!

  // 0.) ______________löschen von dateien/ordnern (ordner rekursiv)____________________________________________
  ffrm( datei );								// löschen der datei/ordner falls vorhanden

  // 1.) ______________anlegen und schreiben____________________________________________________________________
  // 	öffnet datei, wenn nicht vorhanden, legt ffopen datei an (rückgabewert = 1 datei existiert, also nur öffnen, 2 = angelegt).   
  ffopen( datei );						

  // schreibt string 
  ffwrites((char*)"Hallo Datei :)");
  // neue zeile in der datei
  ffwrite(0x0D);
  ffwrite(0x0A);

  // schließt datei
  ffclose();

  // 2.)________________ändern von vorhandenen daten in dateien__________________________________________________
  ffopen( datei );		// siehe oben...
  ffseek(12);				// spult in datei auf position 12 vor (fängt immer bei 0 an zu zählen !)
  ffwrite(';');			// schreibt dann ab position 12 (überschreibt daten der datei, hier nur 1 zeichen)
  ffclose();				// schließt datei  

  // 3.)________________lesen von dateien_________________________________________________________________________
  ffopen( datei );							// siehe oben...
  unsigned long int seek=file.length;	// eine variable setzen und runterzählen bis 0 geht am schnellsten !
  do{
	 uputc(ffread());							// liest ein zeichen und gibt es über uart aus !
	 }while(--seek);							// liest solange bytes da sind (von datei länge bis 0)
  ffclose();									// schließt datei

  uputc('\n');	// neue zeile weil neue unteraufgabe

  // 4.)________________anhängen von daten an datei_______________________________________________________________
  ffopen( datei);				// siehe oben...
  ffseek(file.length);			// spult in datei ans ende
  ffwrites((char*)"Dies ist ein Test...");	  // siehe oben
  // neue zeile in der datei
  ffwrite(0x0D);
  ffwrite(0x0A);
  ffclose();						// schließt datei  

  // 3.)________________lesen von dateien_________________________________________________________________________
  ffopen( datei );		// siehe oben...
  seek=file.length;		// eine variable setzen und runterzählen bis 0 geht am schnellsten !
  do{
	 uputc(ffread());		// liest ein zeichen und gibt es über uart aus !
	 }while(--seek);		// liest solange bytes da sind (von datei länge bis 0)
  ffclose();				// schließt datei
  
}
Beispiel #4
0
static int
ffio_pgout(ncio *const nciop,
	off_t const offset,  const size_t extent,
	const void *const vp, off_t *posp)
{
#ifdef X_ALIGN
	assert(offset % X_ALIGN == 0);
	assert(extent % X_ALIGN == 0);
#endif

	if(*posp != offset)
	{
		if(ffseek(nciop->fd, offset, SEEK_SET) != offset)
		{
			return errno;
		}
		*posp = offset;
	}
	if(ffwrite(nciop->fd, vp, extent) != extent)
	{
		return errno;
	}
	*posp += extent;

	return ENOERR;
}
Beispiel #5
0
/*
 * Get file size in bytes.
 * Is use of ffseek() really necessary, or could we use standard fstat() call
 * and get st_size member?
 */
static int
ncio_ffio_filesize(ncio *nciop, off_t *filesizep)
{
    off_t filesize, current, reset;

    if(nciop == NULL)
	return EINVAL;

    current = ffseek(nciop->fd, 0, SEEK_CUR);  /* save current */
    *filesizep = ffseek(nciop->fd, 0, SEEK_END); /* get size */
    reset = ffseek(nciop->fd, current, SEEK_SET); /* reset */

    if(reset != current)
	return EINVAL;
    return ENOERR;
}
Beispiel #6
0
int lo_nacti_vertexlist_out(FFILE f, OBJEKT_BOD ** p_vert)
{
  GENERIC_HEAD head;
  long t = fftell(f);
  float v[3];
  int i;

  if (*p_vert != NULL) {
    return ((int) NULL);
  }

  if (!ffread(&head, sizeof(head), 1, f)) {
    ffseek(f, t, SEEK_SET);
    return ((int) NULL);
  }

  if ((head.h[0] != 'V') ||
    (head.h[1] != 'V') || (head.h[2] != 'V') || (head.h[3] != 'V')) {
    ffseek(f, t, SEEK_SET);
    return ((int) NULL);
  }

  if ((*p_vert =
      (OBJEKT_BOD *) mmalloc(sizeof(OBJEKT_BOD) * head.pocet)) == NULL) {
    chyba("Nedostatek pameti...");
  }

  memset(*p_vert, 0, sizeof(OBJEKT_BOD) * head.pocet);

  for (i = 0; i < head.pocet; i++) {
    if (!ffread(&v, sizeof(v), 1, f)) {
      ffseek(f, t, SEEK_SET);
      null_free((void **) p_vert);
      return ((int) NULL);
    }
    (*p_vert)[i].x = v[0];
    (*p_vert)[i].y = v[1];
    (*p_vert)[i].z = v[2];
    (*p_vert)[i].dr = (*p_vert)[i].dg = (*p_vert)[i].db = 1.0f;
  }

  return (head.pocet);
}
Beispiel #7
0
int lo_nacti_word_matrix_out(FFILE f, GLMATRIX * p_mat)
{
  GENERIC_HEAD head;
  int t = fftell(f);

  if (!ffread(&head, sizeof(head), 1, f)) {
    ffseek(f, t, SEEK_SET);
    return (FALSE);
  }
  if (head.h[0] != 'W' || head.h[1] != 'W' || head.h[2] != 'W'
    || head.h[3] != 'W') {
    ffseek(f, t, SEEK_SET);
    return (FALSE);
  }
  if (!ffread(p_mat, sizeof(GLMATRIX), 1, f)) {
    ffseek(f, t, SEEK_SET);
    return (FALSE);
  }
  return (TRUE);
}
Beispiel #8
0
int
stdiof_ffseek(FILE *stream, long offset, int whence)
{
	int	fd;
	int	rc;

	fd = (long)stream;

	rc = ffseek(fd, offset, whence);

	return ( (rc<0) ? -1 : 0  );
}
Beispiel #9
0
long
stdiof_fftell(FILE *stream)
{
	int	fd;
	off_t	cpos;

	fd = (long)stream;

	cpos = ffseek(fd, 0L, SEEK_CUR);
	
	return ( (long)cpos );

}
Beispiel #10
0
void
stdiof_ffrewind(FILE *stream)
{
	int	fd;
	int	rc;
	struct ffsw	ffsw;

	fd = (long)stream;

	rc = ffflush(fd, &ffsw);
	rc = ffseek (fd, 0L, SEEK_SET);

}
Beispiel #11
0
int lo_nacti_maplist_out(FFILE f, OBJEKT_BOD ** p_vert, int c_text)
{
  GENERIC_HEAD head;
  long t = fftell(f);
  float *p_map;
  float m[2];
  int i;

  if (*p_vert == NULL)
    return ((int) NULL);

  if (!ffread(&head, sizeof(head), 1, f)) {
    ffseek(f, t, SEEK_SET);
    return ((int) NULL);
  }

  if ((head.h[0] != 'M') ||
    (head.h[1] != 'M') || (head.h[2] != 'M') || (head.h[3] != 'M')) {
    ffseek(f, t, SEEK_SET);
    return ((int) NULL);
  }

  if (c_text >= 2) {
    c_text = 1;
  }

  for (i = 0; i < head.pocet; i++) {
    if (!ffread(&m, sizeof(m), 1, f)) {
      ffseek(f, t, SEEK_SET);
      return ((int) NULL);
    }
    p_map = &(*p_vert)[i].tu1;
    p_map[0 + c_text * 2] = m[0];
    p_map[1 + c_text * 2] = -m[1];
  }

  return (head.pocet);
}
Beispiel #12
0
int lo_nacti_barvy_out(FFILE f, OBJ_VERTEX * p_vert, word pocet)
{
  GENERIC_HEAD head;
  dword barva;
  long t = fftell(f);
  int i;

  if (p_vert == NULL)
    return (FALSE);

  for (i = 0; i < pocet; i++) {
    p_vert[i].dr = p_vert[i].dg = p_vert[i].db = 1.0f;
  }

  if (!ffread(&head, sizeof(head), 1, f)) {
    ffseek(f, t, SEEK_SET);
    return (FALSE);
  }

  if ((head.h[0] != 'D') ||
    (head.h[1] != 'D') || (head.h[2] != 'D') || (head.h[3] != 'D')) {
    ffseek(f, t, SEEK_SET);
    return (FALSE);
  }

  for (i = 0; i < head.pocet; i++) {
    if (!ffread(&barva, sizeof(dword), 1, f)) {
      ffseek(f, t, SEEK_SET);
      return (FALSE);
    }
    else {
      rgb_float(barva, &p_vert[i].dr);
    }
  }

  return (head.pocet);
}
Beispiel #13
0
int lo_nacti_facelist_out(FFILE f, FACE ** p_face)
{
  GENERIC_HEAD head;
  long t = fftell(f);


  if (*p_face != NULL)
    return ((int) NULL);

  if (!ffread(&head, sizeof(head), 1, f)) {
    ffseek(f, t, SEEK_SET);
    return ((int) NULL);
  }

  if ((head.h[0] != 'F') ||
    (head.h[1] != 'F') || (head.h[2] != 'F') || (head.h[3] != 'F')) {
    ffseek(f, t, SEEK_SET);
    return ((int) NULL);
  }

  head.pocet *= 3;
  if (!head.pocet)
    chyba("load facelist -> 0 facu !");

  if ((*p_face = (FACE *) mmalloc(sizeof(FACE) * head.pocet)) == NULL) {
    chyba("Nedostatek pameti...");
  }

  if (!ffread(*p_face, sizeof(FACE), head.pocet, f)) {
    null_free((void **) p_face);
    ffseek(f, t, SEEK_SET);
    return ((int) NULL);
  }

  return (head.pocet);
}
Beispiel #14
0
int lo_je_tag(FFILE f, char *p_tag)
{
  long t = fftell(f);
  char string[10];

  ffgets(string, 5, f);
  string[4] = 0;

  if (strcmp(string, p_tag) != 0) {
    ffseek(f, t, SEEK_SET);
    return ((int) NULL);
  }

  return (TRUE);
}
Beispiel #15
0
void conv(FILE * fp, size_t iosize)
{
   long adrs, n;
   int i;
   char ibuf[BUFSIZE], obuf[BUFSIZE];
   int ffseek(FILE * fp, long off);

   if (ffseek(fp, adrs = start + iosize * sno))
      return;

   for (n = sno; (adrs <= _end) && (n <= eno); adrs += iosize, ++n) {
      freadx(ibuf, iosize, 1, fp);
      if (feof(fp))
         break;
      for (i = 0; i < (int) iosize; ++i)
         obuf[i] = ibuf[iosize - 1 - i];
      fwritex(obuf, iosize, 1, stdout);
   }

   return;
}
Beispiel #16
0
static int
ffio_pgin(ncio *const nciop,
	off_t const offset, const size_t extent,
	void *const vp, size_t *nreadp, off_t *posp)
{
	int status;
	ssize_t nread;

#ifdef X_ALIGN
	assert(offset % X_ALIGN == 0);
	assert(extent % X_ALIGN == 0);
#endif

	if(*posp != offset)
	{
		if(ffseek(nciop->fd, offset, SEEK_SET) != offset)
		{
			status = errno;
			return status;
		}
		*posp = offset;
	}

	errno = 0;
	nread = ffread(nciop->fd, vp, extent);
	if(nread != extent)
	{
		status = errno;
		if(nread == -1 || status != ENOERR)
			return status;
		/* else it's okay we read 0. */
	}
	*nreadp = nread;
	*posp += nread;

	return ENOERR;
}
Beispiel #17
0
Errcode poll_abort()
/* use whenever you wish to poll if user has requested an abort using a 
 * key hit or right pen click */
{
Errcode ret;
ULONG time;
static ULONG last_time;
BYTE macro_mode;
SHORT *pcount;
Abortnest *an;

	macro_mode = icb.macro_mode;
	ret = Success; /* non abort most common case */

	if(abort.nest > 0)
	{
		switch(macro_mode)
		{
			case MAKE_MACRO:
			{
				if(Mcb.ar.flags & AR_ABORTLEVEL) /* only one abort per atom */
					goto done;
				icb.macro_mode &= ~MACRO_OK; /* dont record polling input */ 
				++abort.count;       /* one more poll */
			}
			case USE_MACRO:
			{
				if(poll_macro_abort() < Success)
					goto error;

				++abort.count;
				if(!Mcb.ar.flags) /* havnt read abort rec yet */
				{
					if(!IS_MRTYPE(Mcb.next.b[0],MR_POLLABORT))
						goto done; /* not yet, go on */
					if(read_abort_rec() < Success)
						goto error;
					goto done;
				}

				if(!(Mcb.ar.flags & AR_ABORTLEVEL))
					goto done;

				if(Mcb.ab_level != Mcb.mab_level) /* not on aborted level */
					goto done;

				/* if all counts match counts on all levels ABORT! */
				pcount = Mcb.ar.counts;
				for(an = &abort;an != NULL;an = an->pop)
				{
					if(*pcount++ != an->count) /* no match */
						goto done; 
				}

				Mcb.ar.flags &= ~AR_ABORTLEVEL;  /* took care of this */
				_poll_input(0); /* get input that was recorded */
				verify_abort(); /* macro WILL abort, we don't record cancels */
				goto aborted;
			}
			default:
				break;
		}
	}

	time = (ULONG)pj_clock_1000();
	if(macro_mode == USE_MACRO || (time-last_time) > POLL_INTERVAL)
	{
		last_time = time;
		icb.waithit = KEYHIT|MBRIGHT;
		if(0 == (icb.waithit = _poll_input(0))) /* will clear waithit */
			goto done;
		icb.waithit = 0;

		if(macro_mode == MAKE_MACRO && abort.nest > 0)
		{
		LONG foffset;

			if((foffset = fftell(Mcb.fp)) < 0)
			{
				macro_read_error((Errcode)foffset);
				goto error;
			}

			/* flag to write record for abort at this level */ 
			Mcb.ar.flags |= AR_ABORTLEVEL; 
			if(write_abort_rec() < Success)
				goto error;
			if(put_macro(TRUE) < Success) /* write input that caused abort */
				goto error;
			if(verify_abort())
				goto aborted;

			/* Abort canceled! disregard aborting an cancelling input 
			 * unflag abort done */

			Mcb.ar.flags &= ~AR_ABORTLEVEL; 
			if((foffset = ffseek(Mcb.fp,foffset,SEEK_SET)) < 0)
			{
				macro_read_error(foffset);
				goto aborted;
			}
		}
		else if(verify_abort()) 
			goto aborted;
	}
	else if(icb.macro_mode == MAKE_MACRO) 
	{
		/* always increment macro count if making a macro.
		 * NOTE: that above we always check the input when using
		 * a macro and macro will always be ~MACRO_OK when polling in
		 * an abort atom NOTE: put_macro with FALSE will never return 
		 * error */

		put_macro(FALSE);
	}
	goto done;

error:
	if(!verify_abort()) /* error will be reported already */
		goto done;
aborted:
	ret = Err_abort;
done:
	icb.macro_mode |= MACRO_OK;
	return(ret);
}