FreeBASIC  0.91.0
file_putback.c
Go to the documentation of this file.
1 /* some kind of ungetc function */
2 
3 #include "fb.h"
4 
5 int fb_FilePutBackEx( FB_FILE *handle, const void *src, size_t chars )
6 {
7  int res;
8  size_t bytes;
9 
10  if( !FB_HANDLE_USED(handle) )
12 
13  FB_LOCK();
14 
16 
17  /* UTF? */
18  if( handle->encod != FB_FILE_ENCOD_ASCII )
19  bytes = chars * sizeof( FB_WCHAR );
20  else
21  bytes = chars;
22 
23  if( handle->putback_size + bytes > sizeof(handle->putback_buffer) )
24  {
26  }
27  else
28  {
29  /* note: if encoding != ASCII, putback buffer will be in
30  wchar format, not in UTF */
31  if( handle->putback_size )
32  {
33  memmove( handle->putback_buffer + bytes,
34  handle->putback_buffer,
35  handle->putback_size );
36  }
37 
38  if( handle->encod == FB_FILE_ENCOD_ASCII )
39  memcpy( handle->putback_buffer, src, bytes );
40  else
41  {
42  /* char to wchar */
43  FB_WCHAR *dst = (FB_WCHAR *)handle->putback_buffer;
44  const char *patch = (const char *)src;
45  while( chars-- > 0 )
46  *dst++ = *patch++;
47  }
48 
49  handle->putback_size += bytes;
50  }
51 
52  FB_UNLOCK();
53 
54  return res;
55 }
56 
57 FBCALL int fb_FilePutBack( int fnum, const void *data, size_t length )
58 {
59  return fb_FilePutBackEx( FB_FILE_TO_HANDLE(fnum), data, length );
60 }