Emulator core geared towards emulating ZZT and Super ZZT.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

posix_vfs.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2018 Adrian Siekierka
  3. *
  4. * This file is part of Zeta.
  5. *
  6. * Zeta is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Zeta is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Zeta. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <ncurses.h>
  24. #include "zzt.h"
  25. long vfs_time_ms() {
  26. clock_t c = clock();
  27. return c / (CLOCKS_PER_SEC/1000);
  28. }
  29. #define MAX_FNLEN 259
  30. static FILE* file_pointers[MAX_FILES];
  31. static char vfs_fnbuf[MAX_FNLEN+1];
  32. static int vfs_fnprefsize;
  33. int vfs_findfirst(u8* ptr, u16 mask, char* spec) { return -1; }
  34. int vfs_findnext(u8* ptr) { return -1; }
  35. void init_posix_vfs(const char* path) {
  36. strncpy(vfs_fnbuf, path, MAX_FNLEN);
  37. vfs_fnprefsize = strlen(vfs_fnbuf);
  38. for (int i = 0; i < MAX_FILES; i++) {
  39. file_pointers[i] = NULL;
  40. }
  41. }
  42. int vfs_open(const char* filename, int mode) {
  43. int len = strlen(filename);
  44. if (len > (MAX_FNLEN - vfs_fnprefsize)) {
  45. return -1;
  46. }
  47. int pos = 0;
  48. while (pos < MAX_FILES && file_pointers[pos] != NULL) pos++;
  49. if (pos == MAX_FILES) return -1;
  50. for (int i = 0; i < strlen(filename); i++) {
  51. char c = filename[i];
  52. if (c >= 'a' && c <= 'z') c -= 'a' - 'A';
  53. vfs_fnbuf[i+vfs_fnprefsize] = c;
  54. }
  55. vfs_fnbuf[len+vfs_fnprefsize] = 0;
  56. FILE* file = fopen(vfs_fnbuf, (mode & 0x10000) ? "w+b" : (((mode & 0x03) == 0) ? "rb" : "r+b"));
  57. if (file == NULL) return -1;
  58. file_pointers[pos] = file;
  59. return pos+1;
  60. }
  61. int vfs_read(int handle, u8* ptr, int amount) {
  62. if (handle <= 0 || handle > MAX_FILES) return -1;
  63. FILE* fptr = file_pointers[handle-1];
  64. return fread(ptr, 1, amount, fptr);
  65. }
  66. int vfs_write(int handle, u8* ptr, int amount) {
  67. if (handle <= 0 || handle > MAX_FILES) return -1;
  68. FILE* fptr = file_pointers[handle-1];
  69. return fwrite(ptr, 1, amount, fptr);
  70. }
  71. int vfs_seek(int handle, int amount, int type) {
  72. if (handle <= 0 || handle > MAX_FILES) return -1;
  73. FILE* fptr = file_pointers[handle-1];
  74. switch (type) {
  75. default:
  76. case VFS_SEEK_SET: return fseek(fptr, amount, SEEK_SET);
  77. case VFS_SEEK_CUR: return fseek(fptr, amount, SEEK_CUR);
  78. case VFS_SEEK_END: return fseek(fptr, amount, SEEK_END);
  79. }
  80. }
  81. int vfs_close(int handle) {
  82. if (handle <= 0 || handle > MAX_FILES) return -1;
  83. FILE* fptr = file_pointers[handle-1];
  84. return fclose(fptr);
  85. }