1
0

stdio.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Stdio replacement library
  3. * Maps common stdio functions to brfs functions
  4. * Adds buffers to improve performance.
  5. * Outputfile is written in memory first and then written to disk.
  6. */
  7. #define EOF -1
  8. #define FGETC_BUFFER_SIZE 512
  9. word fgetc_buffer_main[FGETC_BUFFER_SIZE];
  10. word fgetc_buffer_lib[FGETC_BUFFER_SIZE];
  11. word fgetc_buffer_cursor_main = -1;
  12. word fgetc_buffer_cursor_lib = -1;
  13. word fgetc_buffer_fd_main = -1;
  14. word fgetc_buffer_fd_lib = -1;
  15. char *outfileData = (char*) OUTFILE_DATA_ADDR;
  16. word outfileCursor = 0;
  17. // returns the current char at cursor within the opened file (EOF if end of file)
  18. // increments the cursor
  19. // For the buffer, it assumes that not more than two files are open for reading at the same time
  20. word fgetc(word fd, word filesize)
  21. {
  22. if (fgetc_buffer_fd_main == -1)
  23. {
  24. fgetc_buffer_fd_main = fd;
  25. }
  26. if (fd != fgetc_buffer_fd_main)
  27. {
  28. if (fd != fgetc_buffer_fd_lib)
  29. {
  30. // Clear buffer for new lib file
  31. fgetc_buffer_cursor_lib = -1;
  32. }
  33. fgetc_buffer_fd_lib = fd;
  34. }
  35. if (fd == fgetc_buffer_fd_main)
  36. {
  37. if (fgetc_buffer_cursor_main == -1 || fgetc_buffer_cursor_main == FGETC_BUFFER_SIZE)
  38. {
  39. word cursor = fs_getcursor(fd);
  40. if (filesize - cursor < FGETC_BUFFER_SIZE)
  41. {
  42. fs_read(fd, fgetc_buffer_main, filesize - cursor);
  43. fgetc_buffer_main[filesize - cursor] = EOF;
  44. }
  45. else
  46. {
  47. fs_read(fd, fgetc_buffer_main, FGETC_BUFFER_SIZE);
  48. }
  49. fgetc_buffer_cursor_main = 0;
  50. }
  51. char c = fgetc_buffer_main[fgetc_buffer_cursor_main];
  52. fgetc_buffer_cursor_main++;
  53. return c;
  54. }
  55. else if (fd == fgetc_buffer_fd_lib)
  56. {
  57. if (fgetc_buffer_cursor_lib == -1 || fgetc_buffer_cursor_lib == FGETC_BUFFER_SIZE)
  58. {
  59. word cursor = fs_getcursor(fd);
  60. if (filesize - cursor < FGETC_BUFFER_SIZE)
  61. {
  62. fs_read(fd, fgetc_buffer_lib, filesize - cursor);
  63. fgetc_buffer_lib[filesize - cursor] = EOF;
  64. }
  65. else
  66. {
  67. fs_read(fd, fgetc_buffer_lib, FGETC_BUFFER_SIZE);
  68. }
  69. fgetc_buffer_cursor_lib = 0;
  70. }
  71. char c = fgetc_buffer_lib[fgetc_buffer_cursor_lib];
  72. fgetc_buffer_cursor_lib++;
  73. return c;
  74. }
  75. bdos_println("UNEXPECTED ERROR IN FGETC");
  76. return EOF;
  77. }
  78. // Ignore fp as we assume only one output file
  79. void stdio_setcursor(word fp, word pos)
  80. {
  81. outfileCursor = pos;
  82. }
  83. // Ignore fp as we assume only one output file
  84. word stdio_getcursor(word fp)
  85. {
  86. return outfileCursor;
  87. }
  88. word fputs(word fd, char* s)
  89. {
  90. word len = strlen(s);
  91. memcpy(outfileData + outfileCursor, s, len);
  92. outfileCursor += len;
  93. }
  94. word fputc(word fd, char c)
  95. {
  96. outfileData[outfileCursor] = c;
  97. outfileCursor++;
  98. }
  99. // Flush output buffer to filesystem
  100. void stdio_flush(word fd)
  101. {
  102. fs_write(fd, outfileData, outfileCursor);
  103. outfileCursor = 0;
  104. }
  105. word printf(char* s)
  106. {
  107. bdos_print(s);
  108. }
  109. word printd(word d)
  110. {
  111. bdos_printdec(d);
  112. }