|
| 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 | + |
| 10 | +void os___init__(PikaObj* self) { |
| 11 | + // obj_newDirectObj(self,"os",New_TinyObj); |
| 12 | + |
| 13 | + obj_setInt(self, "O_RDONLY", FILE_RDONLY); |
| 14 | + obj_setInt(self, "O_WRONLY", FILE_WRONLY); |
| 15 | + obj_setInt(self, "O_RDWR", FILE_RDWR); |
| 16 | + obj_setInt(self, "O_APPEND", FILE_APPEND); |
| 17 | + obj_setInt(self, "O_CREAT", FILE_CREAT); |
| 18 | +} |
| 19 | +//#undef _WIN32 |
| 20 | + |
| 21 | +int os_fileStat_st_size(PikaObj* self) { |
| 22 | + int size = obj_getInt(self, "st_size"); |
| 23 | + return size; |
| 24 | +} |
| 25 | + |
| 26 | +PikaObj* os_fstat(PikaObj* self, PikaObj* fd) { |
| 27 | + int size = 0; |
| 28 | + size = os_getFileSize(fd); |
| 29 | + |
| 30 | + PikaObj* stat_obj = newNormalObj(New_os_fileStat); |
| 31 | + |
| 32 | + obj_setInt(stat_obj, "st_size", size); |
| 33 | + return stat_obj; |
| 34 | +} |
| 35 | + |
| 36 | +PikaObj* os_open(PikaObj* self, char* filename, int flags) { |
| 37 | + return os_open_platform(filename, flags); |
| 38 | +} |
| 39 | + |
| 40 | +char* os_read(PikaObj* self, PikaObj* fd, int len) { |
| 41 | + return os_read_platform(self, fd, len); |
| 42 | +} |
| 43 | + |
| 44 | +int os_write(PikaObj* self, PikaObj* fd, Arg* buf) { |
| 45 | + if (arg_getType(buf) != ARG_TYPE_BYTES) { |
| 46 | + obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM); |
| 47 | + pika_platform_printf( |
| 48 | + "TypeError: a bytes-like object is required, not 'str'\r\n"); |
| 49 | + return -1; |
| 50 | + } |
| 51 | + return os_write_platform(arg_getBytes(buf), arg_getBytesSize(buf), fd); |
| 52 | +} |
| 53 | + |
| 54 | +int os_lseek(PikaObj* self, PikaObj* fd, int how, int pos) { |
| 55 | + return os_lseek_platform(fd, how, pos); |
| 56 | +} |
| 57 | + |
| 58 | +void os_close(PikaObj* self, PikaObj* fd) { |
| 59 | + if (os_close_platform(fd) < 0) { |
| 60 | + obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR); |
| 61 | + pika_platform_printf("close file error\r\n"); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +char* os_getcwd(PikaObj* self) { |
| 66 | + return os_getcwd_platform(self); |
| 67 | +} |
| 68 | + |
| 69 | +PikaObj* os_listdir(PikaObj* self, char* path) { |
| 70 | + return os_listdir_platform(path); |
| 71 | +} |
| 72 | + |
| 73 | +void os_mkdir(PikaObj* self, char* path, PikaTuple* mode) { |
| 74 | + int iMode = 0; |
| 75 | + if (pikaTuple_getSize(mode) == 0) { |
| 76 | + iMode = 0777; |
| 77 | + } else { |
| 78 | + iMode = pikaTuple_getInt(mode, 0); |
| 79 | + } |
| 80 | + if (os_mkdir_platform(iMode, path) < 0) { |
| 81 | + obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR); |
| 82 | + pika_platform_printf("mkdir error\r\n"); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void os_chdir(PikaObj* self, char* path) { |
| 87 | + if (os_chdir_platform(path) < 0) { |
| 88 | + obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR); |
| 89 | + pika_platform_printf("chdir error\r\n"); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +void os_rmdir(PikaObj* self, char* path) { |
| 94 | + if (os_rmdir_platform(path) < 0) { |
| 95 | + obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR); |
| 96 | + pika_platform_printf("rmdir error\r\n"); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void os_remove(PikaObj* self, char* filename) { |
| 101 | + if (os_remove_platform(filename) < 0) { |
| 102 | + obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR); |
| 103 | + pika_platform_printf("remove error\r\n"); |
| 104 | + } |
| 105 | +} |
0 commit comments