stdio.c 846 B

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. * Very simple stdio library for mapping stdio functions to brfs.
  3. * Adds buffer for fgetc to improve performance.
  4. */
  5. #define EOF -1
  6. #define FGETC_BUFFER_SIZE 512
  7. word fgetc_buffer[FGETC_BUFFER_SIZE];
  8. word fgetc_buffer_cursor = -1;
  9. // returns the current char at cursor within the opened file (EOF if end of file)
  10. // increments the cursor
  11. word fgetc(word fd, word filesize)
  12. {
  13. if (fgetc_buffer_cursor == -1 || fgetc_buffer_cursor == FGETC_BUFFER_SIZE)
  14. {
  15. word cursor = fs_getcursor(fd);
  16. if (filesize - cursor < FGETC_BUFFER_SIZE)
  17. {
  18. fs_read(fd, fgetc_buffer, filesize - cursor);
  19. fgetc_buffer[filesize - cursor] = EOF;
  20. }
  21. else
  22. {
  23. fs_read(fd, fgetc_buffer, FGETC_BUFFER_SIZE);
  24. }
  25. fgetc_buffer_cursor = 0;
  26. }
  27. char c = fgetc_buffer[fgetc_buffer_cursor];
  28. fgetc_buffer_cursor++;
  29. return c;
  30. }