os.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "os.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "PikaObj.h"
  5. #include "PikaStdData_List.h"
  6. #include "TinyObj.h"
  7. #include "os_fileStat.h"
  8. #include "os_platform.h"
  9. #if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 12, 7)
  10. #error "This library requires PikaScript version 1.12.7 or higher"
  11. #endif
  12. void os___init__(PikaObj* self) {
  13. // obj_newDirectObj(self,"os",New_TinyObj);
  14. obj_setInt(self, "O_RDONLY", FILE_RDONLY);
  15. obj_setInt(self, "O_WRONLY", FILE_WRONLY);
  16. obj_setInt(self, "O_RDWR", FILE_RDWR);
  17. obj_setInt(self, "O_APPEND", FILE_APPEND);
  18. obj_setInt(self, "O_CREAT", FILE_CREAT);
  19. }
  20. //#undef _WIN32
  21. int os_fileStat_st_size(PikaObj* self) {
  22. int size = obj_getInt(self, "st_size");
  23. return size;
  24. }
  25. PikaObj* os_fstat(PikaObj* self, PikaObj* fd) {
  26. int size = 0;
  27. size = os_getFileSize(fd);
  28. PikaObj* stat_obj = newNormalObj(New_os_fileStat);
  29. obj_setInt(stat_obj, "st_size", size);
  30. return stat_obj;
  31. }
  32. PikaObj* os_open(PikaObj* self, char* filename, int flags) {
  33. return os_open_platform(filename, flags);
  34. }
  35. char* os_read(PikaObj* self, PikaObj* fd, int len) {
  36. return os_read_platform(self, fd, len);
  37. }
  38. int os_write(PikaObj* self, PikaObj* fd, Arg* buf) {
  39. if (arg_getType(buf) != ARG_TYPE_BYTES) {
  40. obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
  41. obj_setSysOut(self,
  42. "TypeError: a bytes-like object is required, not 'str'");
  43. return -1;
  44. }
  45. return os_write_platform(arg_getBytes(buf), arg_getBytesSize(buf), fd);
  46. }
  47. int os_lseek(PikaObj* self, PikaObj* fd, int how, int pos) {
  48. return os_lseek_platform(fd, how, pos);
  49. }
  50. void os_close(PikaObj* self, PikaObj* fd) {
  51. if (os_close_platform(fd) < 0) {
  52. obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
  53. obj_setSysOut(self, "close file error");
  54. }
  55. }
  56. char* os_getcwd(PikaObj* self) {
  57. return os_getcwd_platform(self);
  58. }
  59. PikaObj* os_listdir(PikaObj* self, char* path) {
  60. return os_listdir_platform(path);
  61. }
  62. void os_mkdir(PikaObj* self, char* path, PikaTuple* mode) {
  63. int iMode = 0;
  64. if (pikaTuple_getSize(mode) == 0) {
  65. iMode = 0777;
  66. } else {
  67. iMode = pikaTuple_getInt(mode, 0);
  68. }
  69. if (os_mkdir_platform(iMode, path) < 0) {
  70. obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
  71. obj_setSysOut(self, "mkdir error");
  72. }
  73. }
  74. void os_chdir(PikaObj* self, char* path) {
  75. if (os_chdir_platform(path) < 0) {
  76. obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
  77. obj_setSysOut(self, "chdir error");
  78. }
  79. }
  80. void os_rmdir(PikaObj* self, char* path) {
  81. if (os_rmdir_platform(path) < 0) {
  82. obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
  83. obj_setSysOut(self, "rmdir error");
  84. }
  85. }
  86. void os_remove(PikaObj* self, char* filename) {
  87. if (os_remove_platform(filename) < 0) {
  88. obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
  89. obj_setSysOut(self, "remove error");
  90. }
  91. }
  92. void os_rename(PikaObj* self, char* old, char* new) {
  93. if (os_rename_platform(old, new) < 0) {
  94. obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
  95. obj_setSysOut(self, "rename error");
  96. }
  97. }