mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
!61 Support seek() and tell() for FILEIO
* fix FILEIO.seek() * return ftell in FILEIO.seek() * fix test * support seek() and tell() for FILEIO
This commit is contained in:
parent
108dbebace
commit
d9349b7812
4
examples/BuiltIn/seek.py
Normal file
4
examples/BuiltIn/seek.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
f = open('test/assets/test.jpg', 'rb')
|
||||||
|
len = f.seek(0, 2)
|
||||||
|
print(len)
|
||||||
|
f.close()
|
@ -80,7 +80,7 @@ class String:
|
|||||||
def strip(self) -> str: ...
|
def strip(self) -> str: ...
|
||||||
|
|
||||||
|
|
||||||
class ByteArray(TinyObj):
|
class ByteArray:
|
||||||
# convert a string to ByteArray
|
# convert a string to ByteArray
|
||||||
def __init__(self, bytes: any): ...
|
def __init__(self, bytes: any): ...
|
||||||
# support for loop
|
# support for loop
|
||||||
@ -97,8 +97,10 @@ class ByteArray(TinyObj):
|
|||||||
class FILEIO:
|
class FILEIO:
|
||||||
def init(self, path: str, mode: str): ...
|
def init(self, path: str, mode: str): ...
|
||||||
def read(self, size: int) -> any: ...
|
def read(self, size: int) -> any: ...
|
||||||
def write(self, s: any): ...
|
def write(self, s: any) -> int: ...
|
||||||
def close(self): ...
|
def close(self): ...
|
||||||
|
def seek(self, offset: int, *fromwhere) -> int: ...
|
||||||
|
def tell(self) -> int: ...
|
||||||
|
|
||||||
|
|
||||||
class Utils:
|
class Utils:
|
||||||
|
@ -55,20 +55,48 @@ Arg* PikaStdData_FILEIO_read(PikaObj* self, int size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PikaStdData_FILEIO_write(PikaObj* self, Arg* s) {
|
int PikaStdData_FILEIO_write(PikaObj* self, Arg* s) {
|
||||||
FILE* f = obj_getPtr(self, "_f");
|
FILE* f = obj_getPtr(self, "_f");
|
||||||
|
int res = -1;
|
||||||
if (f == NULL) {
|
if (f == NULL) {
|
||||||
obj_setErrorCode(self, PIKA_RES_ERR_IO);
|
obj_setErrorCode(self, PIKA_RES_ERR_IO);
|
||||||
__platform_printf("Error: can't write to file\n");
|
__platform_printf("Error: can't write to file\n");
|
||||||
return;
|
return res;
|
||||||
}
|
}
|
||||||
char* mode = obj_getStr(self, "_mode");
|
char* mode = obj_getStr(self, "_mode");
|
||||||
if (strIsContain(mode, 'b')) {
|
if (strIsContain(mode, 'b')) {
|
||||||
/* binary */
|
/* binary */
|
||||||
__platform_fwrite(arg_getBytes(s), 1, arg_getSize(s), f);
|
res = __platform_fwrite(arg_getBytes(s), 1, arg_getSize(s), f);
|
||||||
} else {
|
} else {
|
||||||
/* text */
|
/* text */
|
||||||
char* str = arg_getStr(s);
|
char* str = arg_getStr(s);
|
||||||
__platform_fwrite(str, 1, strlen(str), f);
|
res = __platform_fwrite(str, 1, strlen(str), f);
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int PikaStdData_FILEIO_seek(PikaObj* self, PikaTuple* fromwhere, int offset) {
|
||||||
|
FILE* f = obj_getPtr(self, "_f");
|
||||||
|
if (f == NULL) {
|
||||||
|
obj_setErrorCode(self, PIKA_RES_ERR_IO);
|
||||||
|
__platform_printf("Error: can't seek in file\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (tuple_getSize(fromwhere) == 1) {
|
||||||
|
int whence = tuple_getInt(fromwhere, 0);
|
||||||
|
__platform_fseek(f, offset, whence);
|
||||||
|
return __platform_ftell(f);
|
||||||
|
}
|
||||||
|
__platform_fseek(f, offset, 0);
|
||||||
|
return __platform_ftell(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
int PikaStdData_FILEIO_tell(PikaObj* self) {
|
||||||
|
FILE* f = obj_getPtr(self, "_f");
|
||||||
|
if (f == NULL) {
|
||||||
|
obj_setErrorCode(self, PIKA_RES_ERR_IO);
|
||||||
|
__platform_printf("Error: can't tell in file\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return __platform_ftell(f);
|
||||||
}
|
}
|
||||||
|
@ -80,7 +80,7 @@ class String:
|
|||||||
def strip(self) -> str: ...
|
def strip(self) -> str: ...
|
||||||
|
|
||||||
|
|
||||||
class ByteArray(TinyObj):
|
class ByteArray:
|
||||||
# convert a string to ByteArray
|
# convert a string to ByteArray
|
||||||
def __init__(self, bytes: any): ...
|
def __init__(self, bytes: any): ...
|
||||||
# support for loop
|
# support for loop
|
||||||
@ -97,8 +97,10 @@ class ByteArray(TinyObj):
|
|||||||
class FILEIO:
|
class FILEIO:
|
||||||
def init(self, path: str, mode: str): ...
|
def init(self, path: str, mode: str): ...
|
||||||
def read(self, size: int) -> any: ...
|
def read(self, size: int) -> any: ...
|
||||||
def write(self, s: any): ...
|
def write(self, s: any) -> int: ...
|
||||||
def close(self): ...
|
def close(self): ...
|
||||||
|
def seek(self, offset: int, *fromwhere) -> int: ...
|
||||||
|
def tell(self) -> int: ...
|
||||||
|
|
||||||
|
|
||||||
class Utils:
|
class Utils:
|
||||||
|
@ -55,20 +55,48 @@ Arg* PikaStdData_FILEIO_read(PikaObj* self, int size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PikaStdData_FILEIO_write(PikaObj* self, Arg* s) {
|
int PikaStdData_FILEIO_write(PikaObj* self, Arg* s) {
|
||||||
FILE* f = obj_getPtr(self, "_f");
|
FILE* f = obj_getPtr(self, "_f");
|
||||||
|
int res = -1;
|
||||||
if (f == NULL) {
|
if (f == NULL) {
|
||||||
obj_setErrorCode(self, PIKA_RES_ERR_IO);
|
obj_setErrorCode(self, PIKA_RES_ERR_IO);
|
||||||
__platform_printf("Error: can't write to file\n");
|
__platform_printf("Error: can't write to file\n");
|
||||||
return;
|
return res;
|
||||||
}
|
}
|
||||||
char* mode = obj_getStr(self, "_mode");
|
char* mode = obj_getStr(self, "_mode");
|
||||||
if (strIsContain(mode, 'b')) {
|
if (strIsContain(mode, 'b')) {
|
||||||
/* binary */
|
/* binary */
|
||||||
__platform_fwrite(arg_getBytes(s), 1, arg_getSize(s), f);
|
res = __platform_fwrite(arg_getBytes(s), 1, arg_getSize(s), f);
|
||||||
} else {
|
} else {
|
||||||
/* text */
|
/* text */
|
||||||
char* str = arg_getStr(s);
|
char* str = arg_getStr(s);
|
||||||
__platform_fwrite(str, 1, strlen(str), f);
|
res = __platform_fwrite(str, 1, strlen(str), f);
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int PikaStdData_FILEIO_seek(PikaObj* self, PikaTuple* fromwhere, int offset) {
|
||||||
|
FILE* f = obj_getPtr(self, "_f");
|
||||||
|
if (f == NULL) {
|
||||||
|
obj_setErrorCode(self, PIKA_RES_ERR_IO);
|
||||||
|
__platform_printf("Error: can't seek in file\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (tuple_getSize(fromwhere) == 1) {
|
||||||
|
int whence = tuple_getInt(fromwhere, 0);
|
||||||
|
__platform_fseek(f, offset, whence);
|
||||||
|
return __platform_ftell(f);
|
||||||
|
}
|
||||||
|
__platform_fseek(f, offset, 0);
|
||||||
|
return __platform_ftell(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
int PikaStdData_FILEIO_tell(PikaObj* self) {
|
||||||
|
FILE* f = obj_getPtr(self, "_f");
|
||||||
|
if (f == NULL) {
|
||||||
|
obj_setErrorCode(self, PIKA_RES_ERR_IO);
|
||||||
|
__platform_printf("Error: can't tell in file\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return __platform_ftell(f);
|
||||||
}
|
}
|
||||||
|
@ -35,3 +35,21 @@ TEST(builtin, type1) {
|
|||||||
EXPECT_EQ(pikaMemNow(), 0);
|
EXPECT_EQ(pikaMemNow(), 0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if PIKA_SYNTAX_LEVEL == PIKA_SYNTAX_LEVEL_MAXIMAL
|
||||||
|
TEST(builtin, seek) {
|
||||||
|
/* init */
|
||||||
|
pikaMemInfo.heapUsedMax = 0;
|
||||||
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||||
|
/* run */
|
||||||
|
__platform_printf("BEGIN\r\n");
|
||||||
|
pikaVM_runSingleFile(pikaMain, "../../examples/BuiltIn/seek.py");
|
||||||
|
/* collect */
|
||||||
|
int len = obj_getInt(pikaMain, "len");
|
||||||
|
/* assert */
|
||||||
|
EXPECT_EQ(len, 3576);
|
||||||
|
/* deinit */
|
||||||
|
obj_deinit(pikaMain);
|
||||||
|
EXPECT_EQ(pikaMemNow(), 0);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
@ -101,6 +101,7 @@ PIKA_WEAK void __platform_wait(void) {
|
|||||||
while (1) {
|
while (1) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
PIKA_WEAK void* __platform_memset(void* mem, int ch, size_t size) {
|
PIKA_WEAK void* __platform_memset(void* mem, int ch, size_t size) {
|
||||||
return memset(mem, ch, size);
|
return memset(mem, ch, size);
|
||||||
}
|
}
|
||||||
@ -119,6 +120,7 @@ PIKA_WEAK char __platform_getchar(void) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fopen */
|
||||||
PIKA_WEAK FILE* __platform_fopen(const char* filename, const char* modes) {
|
PIKA_WEAK FILE* __platform_fopen(const char* filename, const char* modes) {
|
||||||
#if defined(__linux) || defined(_WIN32)
|
#if defined(__linux) || defined(_WIN32)
|
||||||
return fopen(filename, modes);
|
return fopen(filename, modes);
|
||||||
@ -129,6 +131,7 @@ PIKA_WEAK FILE* __platform_fopen(const char* filename, const char* modes) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fclose */
|
||||||
PIKA_WEAK int __platform_fclose(FILE* stream) {
|
PIKA_WEAK int __platform_fclose(FILE* stream) {
|
||||||
#if defined(__linux) || defined(_WIN32)
|
#if defined(__linux) || defined(_WIN32)
|
||||||
return fclose(stream);
|
return fclose(stream);
|
||||||
@ -139,6 +142,7 @@ PIKA_WEAK int __platform_fclose(FILE* stream) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fwrite */
|
||||||
PIKA_WEAK size_t __platform_fwrite(const void* ptr,
|
PIKA_WEAK size_t __platform_fwrite(const void* ptr,
|
||||||
size_t size,
|
size_t size,
|
||||||
size_t n,
|
size_t n,
|
||||||
@ -152,6 +156,7 @@ PIKA_WEAK size_t __platform_fwrite(const void* ptr,
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fread */
|
||||||
PIKA_WEAK size_t __platform_fread(void* ptr,
|
PIKA_WEAK size_t __platform_fread(void* ptr,
|
||||||
size_t size,
|
size_t size,
|
||||||
size_t n,
|
size_t n,
|
||||||
@ -164,3 +169,25 @@ PIKA_WEAK size_t __platform_fread(void* ptr,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fseek */
|
||||||
|
PIKA_WEAK int __platform_fseek(FILE* stream, long offset, int whence) {
|
||||||
|
#if defined(__linux) || defined(_WIN32)
|
||||||
|
return fseek(stream, offset, whence);
|
||||||
|
#else
|
||||||
|
__platform_printf("[error]: __platform_fseek need implementation!\r\n");
|
||||||
|
while (1) {
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ftell */
|
||||||
|
PIKA_WEAK long __platform_ftell(FILE* stream) {
|
||||||
|
#if defined(__linux) || defined(_WIN32)
|
||||||
|
return ftell(stream);
|
||||||
|
#else
|
||||||
|
__platform_printf("[error]: __platform_ftell need implementation!\r\n");
|
||||||
|
while (1) {
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
@ -152,8 +152,10 @@ FILE* __platform_fopen(const char* filename, const char* modes);
|
|||||||
int __platform_fclose(FILE* stream);
|
int __platform_fclose(FILE* stream);
|
||||||
size_t __platform_fwrite(const void* ptr, size_t size, size_t n, FILE* stream);
|
size_t __platform_fwrite(const void* ptr, size_t size, size_t n, FILE* stream);
|
||||||
size_t __platform_fread(void* ptr, size_t size, size_t n, FILE* stream);
|
size_t __platform_fread(void* ptr, size_t size, size_t n, FILE* stream);
|
||||||
|
int __platform_fseek(FILE* stream, long offset, int whence);
|
||||||
|
|
||||||
/* error */
|
/* error */
|
||||||
void __platform_error_handle(void);
|
void __platform_error_handle(void);
|
||||||
|
long __platform_ftell(FILE* stream);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -2,4 +2,4 @@
|
|||||||
#define PIKA_VERSION_MINOR 9
|
#define PIKA_VERSION_MINOR 9
|
||||||
#define PIKA_VERSION_MICRO 1
|
#define PIKA_VERSION_MICRO 1
|
||||||
|
|
||||||
#define PIKA_EDIT_TIME "2022/07/15 10:48:23"
|
#define PIKA_EDIT_TIME "2022/07/18 13:14:40"
|
||||||
|
@ -551,6 +551,26 @@ Arg* list_getArg(PikaList* self, int index) {
|
|||||||
return args_getArg(&self->super, i_str);
|
return args_getArg(&self->super, i_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int list_getInt(PikaList* self, int index) {
|
||||||
|
Arg* arg = list_getArg(self, index);
|
||||||
|
return arg_getInt(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
double list_getFloat(PikaList* self, int index) {
|
||||||
|
Arg* arg = list_getArg(self, index);
|
||||||
|
return arg_getFloat(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* list_getStr(PikaList* self, int index) {
|
||||||
|
Arg* arg = list_getArg(self, index);
|
||||||
|
return arg_getStr(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* list_getPtr(PikaList* self, int index) {
|
||||||
|
Arg* arg = list_getArg(self, index);
|
||||||
|
return arg_getPtr(arg);
|
||||||
|
}
|
||||||
|
|
||||||
PIKA_RES list_append(PikaList* self, Arg* arg) {
|
PIKA_RES list_append(PikaList* self, Arg* arg) {
|
||||||
int top = args_getInt(&self->super, "top");
|
int top = args_getInt(&self->super, "top");
|
||||||
char buff[11];
|
char buff[11];
|
||||||
|
@ -162,6 +162,10 @@ PikaDict* New_dict(void);
|
|||||||
#define list_deinit(self) (args_deinit((&((self)->super))))
|
#define list_deinit(self) (args_deinit((&((self)->super))))
|
||||||
PIKA_RES list_append(PikaList* self, Arg* arg);
|
PIKA_RES list_append(PikaList* self, Arg* arg);
|
||||||
PIKA_RES list_setArg(PikaList* self, int index, Arg* arg);
|
PIKA_RES list_setArg(PikaList* self, int index, Arg* arg);
|
||||||
|
int list_getInt(PikaList* self, int index);
|
||||||
|
double list_getFloat(PikaList* self, int index);
|
||||||
|
char* list_getStr(PikaList* self, int index);
|
||||||
|
void* list_getPtr(PikaList* self, int index);
|
||||||
Arg* list_getArg(PikaList* self, int index);
|
Arg* list_getArg(PikaList* self, int index);
|
||||||
size_t list_getSize(PikaList* self);
|
size_t list_getSize(PikaList* self);
|
||||||
char* strsFormatArg(Args* out_buffs, char* fmt, Arg* arg);
|
char* strsFormatArg(Args* out_buffs, char* fmt, Arg* arg);
|
||||||
@ -170,6 +174,12 @@ char* strsFormatArg(Args* out_buffs, char* fmt, Arg* arg);
|
|||||||
#define tuple_deinit(self) (list_deinit((&((self)->super))))
|
#define tuple_deinit(self) (list_deinit((&((self)->super))))
|
||||||
#define tuple_getArg(self, index) (list_getArg((&((self)->super)), (index)))
|
#define tuple_getArg(self, index) (list_getArg((&((self)->super)), (index)))
|
||||||
#define tuple_getSize(self) (list_getSize((&((self)->super))))
|
#define tuple_getSize(self) (list_getSize((&((self)->super))))
|
||||||
|
#define tuple_getInt(self, index) (list_getInt((&((self)->super)), (index)))
|
||||||
|
#define tuple_getFloat(self, index) \
|
||||||
|
(list_getFloat((&((self)->super)), (index)))
|
||||||
|
#define tuple_getStr(self, index) (list_getStr((&((self)->super)), (index)))
|
||||||
|
#define tuple_getPtr(self, index) (list_getPtr((&((self)->super)), (index)))
|
||||||
|
#define tuple_getType(self, index) (list_getType((&((self)->super)), (index)))
|
||||||
|
|
||||||
PikaList* New_list(void);
|
PikaList* New_list(void);
|
||||||
PikaTuple* New_tuple(void);
|
PikaTuple* New_tuple(void);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user