Advertisement
Guest User

Ankur Patel

a guest
Mar 13th, 2010
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. Cyg_ErrNo
  2. Cyg_StdioStream::refill_read_buffer( void )
  3. {
  4.     Cyg_ErrNo read_err;
  5.     cyg_uint8 *buffer;
  6.     cyg_uint32 len;
  7.  
  8.     CYG_ASSERTCLASS( this, "Stream object is not a valid stream!" );
  9.    
  10.     if (!lock_me())
  11.         return EBADF;  // assume file is now invalid
  12.  
  13.     // first just check that we _can_ read this device!
  14.     if (!flags.opened_for_read) {
  15.         unlock_me();
  16.         return EINVAL;
  17.     }
  18.    
  19. #ifdef CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO
  20.     // If there is pending output to write, then this will check and
  21.     // write it
  22.     if (flags.buffering) {
  23.         read_err = flush_output_unlocked();
  24.  
  25.         // we're now reading
  26.         flags.last_buffer_op_was_read = true;
  27.  
  28.         // flush ALL streams
  29.         if (read_err == ENOERR)
  30.             read_err = cyg_libc_stdio_flush_all_but(this);
  31.  
  32.         if (read_err != ENOERR) {
  33.             unlock_me();
  34.             return read_err;
  35.         } // if
  36.  
  37.         len = io_buf.get_buffer_addr_to_write( (cyg_uint8**)&buffer );
  38.     if(len <= 0)
  39.       len = 10;
  40.         if (!len) { // no buffer space available
  41.             unlock_me();
  42.             return ENOERR;  // isn't an error, just needs user to read out data
  43.         } // if
  44.     }
  45.     else
  46. #endif
  47.  
  48.     if (!flags.readbuf_char_in_use) {
  49.         len = 1;
  50.         buffer = &readbuf_char;
  51.     }
  52.     else {
  53.         // no buffer space available
  54.         unlock_me();
  55.         return ENOERR;  // isn't an error, just needs user to read out data
  56.     } // else
  57.    
  58.     read_err = cyg_stdio_read(my_device, buffer, &len);
  59.  
  60.  
  61. #ifdef CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO
  62.     if (flags.buffering)
  63.         io_buf.set_bytes_written( len );
  64.     else
  65. #endif
  66.         flags.readbuf_char_in_use = len ? 1 : 0;
  67.  
  68.     unlock_me();
  69.  
  70.     if (read_err == ENOERR) {
  71.         if (len == 0) {
  72.             read_err = EAGAIN;
  73.             flags.at_eof = true;
  74.         }
  75.         else
  76.             flags.at_eof = false;
  77.     } // if
  78.    
  79.     return read_err;
  80. } // refill_read_buffer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement