Skip to content

Initial implementation of file accessing using OS Module #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test_numpy_01.py",
"test_numpy_02.py",
"test_random.py",
"test_os.py",
"test_builtin.py",
"test_builtin_abs.py",
"test_builtin_bool.py",
Expand Down
14 changes: 14 additions & 0 deletions integration_tests/test_os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from ltypes import i64
from os import (open, read, close)

def test():
path: str
path = "integration_tests/test_os.py"
fd: i64
n: i64
fd = open(path, "r")
n = 100
print(read(fd, n))
close(fd)

test()
35 changes: 35 additions & 0 deletions src/runtime/impure/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,38 @@ LFORTRAN_API void _lfortran_dp_rand_num(double *x) {
srand(time(0));
*x = rand() / (double) RAND_MAX;
}

LFORTRAN_API int64_t _lpython_open(char *path, char *flags)
{
FILE *fd;
fd = fopen(path, flags);
if (!fd)
{
printf("Error in opening the file!\n");
perror(path);
exit(1);
}
return (int64_t)fd;
}

LFORTRAN_API char* _lpython_read(int64_t fd, int64_t n)
{
char *c = (char *) calloc(n, sizeof(char));
if (fd < 0)
{
printf("Error in reading the file!\n");
exit(1);
}
int x = fread(c, 1, n, (FILE*)fd);
c[x] = '\0';
return c;
}

LFORTRAN_API void _lpython_close(int64_t fd)
{
if (fclose((FILE*)fd) != 0)
{
printf("Error in closing the file!\n");
exit(1);
}
}
3 changes: 3 additions & 0 deletions src/runtime/impure/lfortran_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ LFORTRAN_API void _lfortran_i64sys_clock(
uint64_t *count, int64_t *rate, int64_t *max);
LFORTRAN_API void _lfortran_sp_rand_num(float *x);
LFORTRAN_API void _lfortran_dp_rand_num(double *x);
LFORTRAN_API int64_t _lpython_open(char *path, char *flags);
LFORTRAN_API char* _lpython_read(int64_t fd, int64_t n);
LFORTRAN_API void _lpython_close(int64_t fd);

#ifdef __cplusplus
}
Expand Down
32 changes: 32 additions & 0 deletions src/runtime/os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from ltypes import i32, i64, ccall

def open(path: str, flag: str) -> i64:
"""
Returns the file descriptor for the newly opened file
"""
return _lpython_open(path, flag)

@ccall
def _lpython_open(path: str, flag: str) -> i64:
pass

def read(fd: i64, n: i64) -> str:
"""
Reads at most `n` bytes from file descriptor
"""
return _lpython_read(fd, n)

@ccall
def _lpython_read(fd: i64, n: i64) -> str:
pass

def close(fd: i64):
"""
Closes the file descriptor
"""
_lpython_close(fd)
return

@ccall
def _lpython_close(fd: i64):
pass