Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <stdlib.h>
  4. //#include "format_scan.h"
  5. #include <errno.h>
  6.  
  7. // non standard realization - support for virtually change ONLY ONE char
  8.  
  9. static int __ungetc_emu(int c, FILE* stream)
  10. {
  11.         unsigned res;
  12.     if(stream){
  13.         errno = EINVAL;
  14.         return EOF;
  15.     }
  16.         if ((stream->mode & 3) != _STDIO_F_R && (stream->mode & _STDIO_F_A) == 0){
  17.         errno = EACCES;
  18.         return EOF;
  19.     }
  20.     ksys_bdfe_t *file_info = malloc(sizeof(ksys_bdfe_t));
  21.     if(file_info==NULL){
  22.         errno = ENOMEM;
  23.         return EOF;
  24.     }
  25.     if(!_ksys_file_get_info(stream->name, file_info)){
  26.         errno = ENFILE;
  27.         return EOF;
  28.     }
  29.         if (stream->position > file_info->size || stream->position == 0 || c == EOF || stream->__ungetc_emu_buff != EOF){
  30.             errno = EOF;
  31.                 return EOF;
  32.         }
  33.        
  34.         stream->__ungetc_emu_buff = c;
  35.         stream->position --;
  36.         return c;
  37. }
  38.  
  39. static int __virtual_getc_file(void *sp, const void *obj)
  40. {
  41.     FILE *f = (FILE *)obj;
  42.     int ch = fgetc(f);
  43.     return ch;
  44. }
  45.  
  46. static void __virtual_ungetc_file(void *sp, int c, const void *obj)
  47. {
  48.     FILE *f = (FILE *)obj;
  49.     if (f) __ungetc_emu(c, f);
  50. }
  51.  
  52. int vfscanf(FILE * stream, const char * format, va_list arg)
  53. {
  54.     return _format_scan(stream, format, arg, &__virtual_getc_file, &__virtual_ungetc_file);
  55. }